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
53 ShaderPlugPluginDependencies() :
54 GlobalShadersModuleRef(GlobalRadiant().getRequiredGameDescriptionKeyValue("shaders"))
63 std::vector<const char*> archives;
64 std::set<std::string> shaders;
65 std::set<std::string> textures;
67 XmlTagBuilder TagBuilder;
70 const char* init(void* hApp, void* pMainWidget)
72 g_window = GTK_WINDOW(pMainWidget);
79 const char* getCommandList()
81 return "About;Create tag file";
83 const char* getCommandTitleList()
87 void dispatch(const char* command, float* vMin, float* vMax, bool bSingleBrush)
89 if(string_equal(command, "About"))
91 GlobalRadiant().m_pfnMessageBox(GTK_WIDGET(g_window), "Shaderplug (1.0)\n\n"
92 "by Shaderman (shaderman@gmx.net)",
97 if(string_equal(command, "Create tag file"))
103 void loadArchiveFile(const char* filename)
105 archives.push_back(filename);
108 typedef FreeCaller1<const char*, loadArchiveFile> LoadArchiveFileCaller;
110 void LoadTextureFile(const char* filename)
112 std::string s_filename = filename;
115 strcpy(buffer, "textures/");
117 // append filename without trailing file extension (.tga or .jpg for example)
118 strncat(buffer, filename, s_filename.length() - 4);
120 std::set<std::string>::iterator iter;
121 iter = shaders.find(buffer);
123 // a shader with this name already exists
124 if(iter == shaders.end())
126 textures.insert(buffer);
130 typedef FreeCaller1<const char*, LoadTextureFile> LoadTextureFileCaller;
132 void GetTextures(char* extension)
134 GlobalFileSystem().forEachFile("textures/", extension, LoadTextureFileCaller(), 0);
137 void LoadShaderList(const char* filename)
139 if(string_equal_prefix(filename, "textures/"))
141 shaders.insert(filename);
145 typedef FreeCaller1<const char*, LoadShaderList> LoadShaderListCaller;
149 GlobalShaderSystem().foreachShaderName(LoadShaderListCaller());
152 void GetArchiveList()
154 GlobalFileSystem().forEachArchive(LoadArchiveFileCaller());
155 globalOutputStream() << "Shaderplug: " << (const Unsigned)Shaderplug::archives.size() << " archives found.\n";
160 const char* shader_type = GlobalRadiant().getGameDescriptionKeyValue("shaders");
163 globalOutputStream() << "Shaderplug: " << (const Unsigned)shaders.size() << " shaders found.\n";
165 if(string_equal(shader_type, "quake3"))
171 globalOutputStream() << "Shaderplug: " << (const Unsigned)textures.size() << " textures found.\n";
174 if(shaders.size() || textures.size() != 0)
176 globalOutputStream() << "Shaderplug: Creating XML tag file.\n";
178 TagBuilder.CreateXmlDocument();
180 std::set<std::string>::reverse_iterator r_iter;
182 for(r_iter = textures.rbegin(); r_iter != textures.rend(); ++r_iter)
184 TagBuilder.AddShaderNode(const_cast<char*>((*r_iter).c_str()), STOCK, TEXTURE);
187 for(r_iter = shaders.rbegin(); r_iter != shaders.rend(); ++r_iter)
189 TagBuilder.AddShaderNode(const_cast<char*>((*r_iter).c_str()), STOCK, SHADER);
193 StringOutputStream tagFileStream(256);
194 tagFileStream << GlobalRadiant().getLocalRcPath() << SHADERTAG_FILE;
195 char* tagFile = tagFileStream.c_str();
198 strcpy(message, "Tag file saved to\n");
199 strcat(message, tagFile);
200 strcat(message, "\nPlease restart Radiant now.\n");
202 if(file_exists(tagFile))
204 EMessageBoxReturn result = GlobalRadiant().m_pfnMessageBox(GTK_WIDGET(g_window),
205 "WARNING! A tag file already exists! Overwrite it?", "Overwrite tag file?",
211 TagBuilder.SaveXmlDoc(tagFile);
212 GlobalRadiant().m_pfnMessageBox(GTK_WIDGET(g_window), message, "INFO", eMB_OK, eMB_ICONASTERISK);
215 TagBuilder.SaveXmlDoc(tagFile);
216 GlobalRadiant().m_pfnMessageBox(GTK_WIDGET(g_window), message, "INFO", eMB_OK, eMB_ICONASTERISK);
219 GlobalRadiant().m_pfnMessageBox(GTK_WIDGET(g_window),
220 "No shaders or textures found. No XML tag file created!\n"
229 class ShaderPluginModule
231 _QERPluginTable m_plugin;
233 typedef _QERPluginTable Type;
234 STRING_CONSTANT(Name, "ShaderPlug");
238 m_plugin.m_pfnQERPlug_Init = &Shaderplug::init;
239 m_plugin.m_pfnQERPlug_GetName = &Shaderplug::getName;
240 m_plugin.m_pfnQERPlug_GetCommandList = &Shaderplug::getCommandList;
241 m_plugin.m_pfnQERPlug_GetCommandTitleList = &Shaderplug::getCommandTitleList;
242 m_plugin.m_pfnQERPlug_Dispatch = &Shaderplug::dispatch;
244 _QERPluginTable* getTable()
250 typedef SingletonModule<ShaderPluginModule, ShaderPlugPluginDependencies> SingletonShaderPluginModule;
252 SingletonShaderPluginModule g_ShaderPluginModule;
254 extern "C" void RADIANT_DLLEXPORT Radiant_RegisterModules(ModuleServer& server)
256 initialiseModule(server);
258 g_ShaderPluginModule.selfRegister();