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" ) ){
60 ui::Window g_window{ui::null};
62 std::vector<const char*> archives;
63 std::set<std::string> shaders;
64 std::set<std::string> textures;
66 XmlTagBuilder TagBuilder;
69 const char* init( void* hApp, void* pMainWidget ){
70 g_window = ui::Window(GTK_WINDOW( pMainWidget ));
73 const char* getName(){
76 const char* getCommandList(){
77 return "About;Create tag file";
79 const char* getCommandTitleList(){
82 void dispatch( const char* command, float* vMin, float* vMax, bool bSingleBrush ){
83 if ( string_equal( command, "About" ) ) {
84 GlobalRadiant().m_pfnMessageBox( g_window, "Shaderplug (1.0)\n\n"
85 "by Shaderman (shaderman@gmx.net)",
90 if ( string_equal( command, "Create tag file" ) ) {
95 void loadArchiveFile( const char* filename ){
96 archives.push_back( filename );
99 typedef FreeCaller1<const char*, loadArchiveFile> LoadArchiveFileCaller;
101 void LoadTextureFile( const char* filename ){
102 std::string s_filename = filename;
105 strcpy( buffer, "textures/" );
107 // append filename without trailing file extension (.tga or .jpg for example)
108 strncat( buffer, filename, s_filename.length() - 4 );
110 std::set<std::string>::iterator iter;
111 iter = shaders.find( buffer );
113 // a shader with this name already exists
114 if ( iter == shaders.end() ) {
115 textures.insert( buffer );
119 typedef FreeCaller1<const char*, LoadTextureFile> LoadTextureFileCaller;
121 void GetTextures( char* extension ){
122 GlobalFileSystem().forEachFile( "textures/", extension, LoadTextureFileCaller(), 0 );
125 void LoadShaderList( const char* filename ){
126 if ( string_equal_prefix( filename, "textures/" ) ) {
127 shaders.insert( filename );
131 typedef FreeCaller1<const char*, LoadShaderList> LoadShaderListCaller;
133 void GetAllShaders(){
134 GlobalShaderSystem().foreachShaderName( LoadShaderListCaller() );
137 void GetArchiveList(){
138 GlobalFileSystem().forEachArchive( LoadArchiveFileCaller() );
139 globalOutputStream() << "Shaderplug: " << (const Unsigned)Shaderplug::archives.size() << " archives found.\n";
142 void CreateTagFile(){
143 const char* shader_type = GlobalRadiant().getGameDescriptionKeyValue( "shaders" );
146 globalOutputStream() << "Shaderplug: " << (const Unsigned)shaders.size() << " shaders found.\n";
148 if ( string_equal( shader_type, "quake3" ) ) {
149 GetTextures( "jpg" );
150 GetTextures( "tga" );
151 GetTextures( "png" );
153 globalOutputStream() << "Shaderplug: " << (const Unsigned)textures.size() << " textures found.\n";
156 if ( shaders.size() || textures.size() != 0 ) {
157 globalOutputStream() << "Shaderplug: Creating XML tag file.\n";
159 TagBuilder.CreateXmlDocument();
161 std::set<std::string>::reverse_iterator r_iter;
163 for ( r_iter = textures.rbegin(); r_iter != textures.rend(); ++r_iter )
165 TagBuilder.AddShaderNode( const_cast<char*>( ( *r_iter ).c_str() ), STOCK, TEXTURE );
168 for ( r_iter = shaders.rbegin(); r_iter != shaders.rend(); ++r_iter )
170 TagBuilder.AddShaderNode( const_cast<char*>( ( *r_iter ).c_str() ), STOCK, SHADER );
174 StringOutputStream tagFileStream( 256 );
175 tagFileStream << GlobalRadiant().getLocalRcPath() << SHADERTAG_FILE;
176 char* tagFile = tagFileStream.c_str();
179 strcpy( message, "Tag file saved to\n" );
180 strcat( message, tagFile );
181 strcat( message, "\nPlease restart Radiant now.\n" );
183 if ( file_exists( tagFile ) ) {
184 EMessageBoxReturn result = GlobalRadiant().m_pfnMessageBox( g_window ,
185 "WARNING! A tag file already exists! Overwrite it?", "Overwrite tag file?",
189 if ( result == eIDYES ) {
190 TagBuilder.SaveXmlDoc( tagFile );
191 GlobalRadiant().m_pfnMessageBox( g_window, message, "INFO", eMB_OK, eMB_ICONASTERISK );
195 TagBuilder.SaveXmlDoc( tagFile );
196 GlobalRadiant().m_pfnMessageBox( g_window, message, "INFO", eMB_OK, eMB_ICONASTERISK );
200 GlobalRadiant().m_pfnMessageBox( g_window,
201 "No shaders or textures found. No XML tag file created!\n"
210 class ShaderPluginModule
212 _QERPluginTable m_plugin;
214 typedef _QERPluginTable Type;
215 STRING_CONSTANT( Name, "ShaderPlug" );
217 ShaderPluginModule(){
218 m_plugin.m_pfnQERPlug_Init = &Shaderplug::init;
219 m_plugin.m_pfnQERPlug_GetName = &Shaderplug::getName;
220 m_plugin.m_pfnQERPlug_GetCommandList = &Shaderplug::getCommandList;
221 m_plugin.m_pfnQERPlug_GetCommandTitleList = &Shaderplug::getCommandTitleList;
222 m_plugin.m_pfnQERPlug_Dispatch = &Shaderplug::dispatch;
224 _QERPluginTable* getTable(){
229 typedef SingletonModule<ShaderPluginModule, ShaderPlugPluginDependencies> SingletonShaderPluginModule;
231 SingletonShaderPluginModule g_ShaderPluginModule;
233 extern "C" void RADIANT_DLLEXPORT Radiant_RegisterModules( ModuleServer& server ){
234 initialiseModule( server );
236 g_ShaderPluginModule.selfRegister();