2 Copyright (C) 2006, Stefan Greven.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "shaderplug.h"
24 #include "debugging/debugging.h"
28 #include "string/string.h"
29 #include "modulesystem/singletonmodule.h"
30 #include "stream/stringstream.h"
36 #include "qerplugin.h"
37 #include "ifilesystem.h"
40 #include "iscriplib.h"
42 #include "generic/callback.h"
45 const char SHADERTAG_FILE[] = "shadertags.xml";
48 class ShaderPlugPluginDependencies : public GlobalRadiantModuleRef,
49 public GlobalFileSystemModuleRef,
50 public GlobalShadersModuleRef {
52 ShaderPlugPluginDependencies() :
53 GlobalShadersModuleRef(GlobalRadiant().getRequiredGameDescriptionKeyValue("shaders"))
58 namespace Shaderplug {
59 ui::Window g_window{ui::null};
61 std::vector<const char *> archives;
62 std::set<std::string> shaders;
63 std::set<std::string> textures;
65 XmlTagBuilder TagBuilder;
69 const char *init(void *hApp, void *pMainWidget)
71 g_window = ui::Window::from(pMainWidget);
80 const char *getCommandList()
82 return "About;Create tag file";
85 const char *getCommandTitleList()
90 void dispatch(const char *command, float *vMin, float *vMax, bool bSingleBrush)
92 if (string_equal(command, "About")) {
93 GlobalRadiant().m_pfnMessageBox(g_window, "Shaderplug (1.0)\n\n"
94 "by Shaderman (shaderman@gmx.net)",
99 if (string_equal(command, "Create tag file")) {
104 void loadArchiveFile(const char *filename)
106 archives.push_back(filename);
109 void LoadTextureFile(const char *filename)
111 std::string s_filename = filename;
114 strcpy(buffer, "textures/");
116 // append filename without trailing file extension (.tga or .jpg for example)
117 strncat(buffer, filename, s_filename.length() - 4);
119 std::set<std::string>::iterator iter;
120 iter = shaders.find(buffer);
122 // a shader with this name already exists
123 if (iter == shaders.end()) {
124 textures.insert(buffer);
128 void GetTextures(const char *extension)
130 GlobalFileSystem().forEachFile("textures/", extension, makeCallbackF(LoadTextureFile), 0);
133 void LoadShaderList(const char *filename)
135 if (string_equal_prefix(filename, "textures/")) {
136 shaders.insert(filename);
142 GlobalShaderSystem().foreachShaderName(makeCallbackF(LoadShaderList));
145 void GetArchiveList()
147 GlobalFileSystem().forEachArchive(makeCallbackF(loadArchiveFile));
148 globalOutputStream() << "Shaderplug: " << (const Unsigned) Shaderplug::archives.size() << " archives found.\n";
153 const char *shader_type = GlobalRadiant().getGameDescriptionKeyValue("shaders");
156 globalOutputStream() << "Shaderplug: " << (const Unsigned) shaders.size() << " shaders found.\n";
158 if (string_equal(shader_type, "quake3")) {
163 globalOutputStream() << "Shaderplug: " << (const Unsigned) textures.size() << " textures found.\n";
166 if (shaders.size() || textures.size() != 0) {
167 globalOutputStream() << "Shaderplug: Creating XML tag file.\n";
169 TagBuilder.CreateXmlDocument();
171 std::set<std::string>::reverse_iterator r_iter;
173 for (r_iter = textures.rbegin(); r_iter != textures.rend(); ++r_iter) {
174 TagBuilder.AddShaderNode(const_cast<char *>((*r_iter).c_str()), STOCK, TEXTURE);
177 for (r_iter = shaders.rbegin(); r_iter != shaders.rend(); ++r_iter) {
178 TagBuilder.AddShaderNode(const_cast<char *>((*r_iter).c_str()), STOCK, SHADER);
182 StringOutputStream tagFileStream(256);
183 tagFileStream << GlobalRadiant().getLocalRcPath() << SHADERTAG_FILE;
184 char *tagFile = tagFileStream.c_str();
187 strcpy(message, "Tag file saved to\n");
188 strcat(message, tagFile);
189 strcat(message, "\nPlease restart Radiant now.\n");
191 if (file_exists(tagFile)) {
192 EMessageBoxReturn result = GlobalRadiant().m_pfnMessageBox(g_window,
193 "WARNING! A tag file already exists! Overwrite it?",
194 "Overwrite tag file?",
198 if (result == eIDYES) {
199 TagBuilder.SaveXmlDoc(tagFile);
200 GlobalRadiant().m_pfnMessageBox(g_window, message, "INFO", eMB_OK, eMB_ICONASTERISK);
203 TagBuilder.SaveXmlDoc(tagFile);
204 GlobalRadiant().m_pfnMessageBox(g_window, message, "INFO", eMB_OK, eMB_ICONASTERISK);
207 GlobalRadiant().m_pfnMessageBox(g_window,
208 "No shaders or textures found. No XML tag file created!\n"
217 class ShaderPluginModule {
218 _QERPluginTable m_plugin;
220 typedef _QERPluginTable Type;
222 STRING_CONSTANT(Name, "ShaderPlug");
226 m_plugin.m_pfnQERPlug_Init = &Shaderplug::init;
227 m_plugin.m_pfnQERPlug_GetName = &Shaderplug::getName;
228 m_plugin.m_pfnQERPlug_GetCommandList = &Shaderplug::getCommandList;
229 m_plugin.m_pfnQERPlug_GetCommandTitleList = &Shaderplug::getCommandTitleList;
230 m_plugin.m_pfnQERPlug_Dispatch = &Shaderplug::dispatch;
233 _QERPluginTable *getTable()
239 typedef SingletonModule<ShaderPluginModule, ShaderPlugPluginDependencies> SingletonShaderPluginModule;
241 SingletonShaderPluginModule g_ShaderPluginModule;
243 extern "C" void RADIANT_DLLEXPORT Radiant_RegisterModules(ModuleServer &server)
245 initialiseModule(server);
247 g_ShaderPluginModule.selfRegister();