unvanquished filesystem
[xonotic/netradiant.git] / radiant / plugin.cpp
1 /*
2    Copyright (C) 2001-2006, William Joseph.
3    All Rights Reserved.
4
5    This file is part of GtkRadiant.
6
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.
11
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.
16
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
20  */
21
22 #include "plugin.h"
23
24 #include "debugging/debugging.h"
25
26 #include "qerplugin.h"
27 #include "ifilesystem.h"
28 #include "ishaders.h"
29 #include "ientity.h"
30 #include "ieclass.h"
31 #include "irender.h"
32 #include "iscenegraph.h"
33 #include "iselection.h"
34 #include "ifilter.h"
35 #include "iscriplib.h"
36 #include "igl.h"
37 #include "iundo.h"
38 #include "itextures.h"
39 #include "ireference.h"
40 #include "ifiletypes.h"
41 #include "preferencesystem.h"
42 #include "ibrush.h"
43 #include "ipatch.h"
44 #include "iimage.h"
45 #include "itoolbar.h"
46 #include "iplugin.h"
47 #include "imap.h"
48 #include "namespace.h"
49
50 #include "gtkutil/messagebox.h"
51 #include "gtkutil/filechooser.h"
52 #include "maplib.h"
53
54 #include "error.h"
55 #include "map.h"
56 #include "qe3.h"
57 #include "entityinspector.h"
58 #include "entitylist.h"
59 #include "points.h"
60 #include "gtkmisc.h"
61 #include "texwindow.h"
62 #include "mainframe.h"
63 #include "build.h"
64 #include "mru.h"
65 #include "multimon.h"
66 #include "surfacedialog.h"
67 #include "groupdialog.h"
68 #include "patchdialog.h"
69 #include "camwindow.h"
70 #include "watchbsp.h"
71 #include "xywindow.h"
72 #include "entity.h"
73 #include "select.h"
74 #include "preferences.h"
75 #include "autosave.h"
76 #include "plugintoolbar.h"
77 #include "findtexturedialog.h"
78 #include "nullmodel.h"
79 #include "grid.h"
80
81 #include "modulesystem/modulesmap.h"
82 #include "modulesystem/singletonmodule.h"
83
84 #include "generic/callback.h"
85
86 const char* GameDescription_getKeyValue( const char* key ){
87         return g_pGameDescription->getKeyValue( key );
88 }
89
90 const char* GameDescription_getRequiredKeyValue( const char* key ){
91         return g_pGameDescription->getRequiredKeyValue( key );
92 }
93
94 const char* getMapName(){
95         return Map_Name( g_map );
96 }
97
98 scene::Node& getMapWorldEntity(){
99         return Map_FindOrInsertWorldspawn( g_map );
100 }
101
102 VIEWTYPE XYWindow_getViewType(){
103         return g_pParentWnd->GetXYWnd()->GetViewType();
104 }
105
106 Vector3 XYWindow_windowToWorld( const WindowVector& position ){
107         Vector3 result( 0, 0, 0 );
108         g_pParentWnd->GetXYWnd()->XY_ToPoint( static_cast<int>( position.x() ), static_cast<int>( position.y() ), result );
109         return result;
110 }
111
112 const char* TextureBrowser_getSelectedShader(){
113         return TextureBrowser_GetSelectedShader( GlobalTextureBrowser() );
114 }
115
116 const char* getGameFile(){
117         return g_GamesDialog.m_sGameFile.c_str();
118 }
119
120 class RadiantCoreAPI
121 {
122 _QERFuncTable_1 m_radiantcore;
123 public:
124 typedef _QERFuncTable_1 Type;
125 STRING_CONSTANT( Name, "*" );
126
127 RadiantCoreAPI(){
128         m_radiantcore.getEnginePath = &EnginePath_get;
129         m_radiantcore.getLocalRcPath = &LocalRcPath_get;
130         m_radiantcore.getAppPath = &AppPath_get;
131         m_radiantcore.getGameToolsPath = &GameToolsPath_get;
132         m_radiantcore.getSettingsPath = &SettingsPath_get;
133         m_radiantcore.getMapsPath = &getMapsPath;
134
135         m_radiantcore.getGameFile = &getGameFile;
136         m_radiantcore.getGameName = &gamename_get;
137         m_radiantcore.getGameMode = &gamemode_get;
138
139         m_radiantcore.getMapName = &getMapName;
140         m_radiantcore.getMapWorldEntity = getMapWorldEntity;
141         m_radiantcore.getGridSize = GetGridSize;
142
143         m_radiantcore.getGameDescriptionKeyValue = &GameDescription_getKeyValue;
144         m_radiantcore.getRequiredGameDescriptionKeyValue = &GameDescription_getRequiredKeyValue;
145
146         m_radiantcore.XYWindowDestroyed_connect = XYWindowDestroyed_connect;
147         m_radiantcore.XYWindowDestroyed_disconnect = XYWindowDestroyed_disconnect;
148         m_radiantcore.XYWindowMouseDown_connect = XYWindowMouseDown_connect;
149         m_radiantcore.XYWindowMouseDown_disconnect = XYWindowMouseDown_disconnect;
150         m_radiantcore.XYWindow_getViewType = XYWindow_getViewType;
151         m_radiantcore.XYWindow_windowToWorld = XYWindow_windowToWorld;
152         m_radiantcore.TextureBrowser_getSelectedShader = TextureBrowser_getSelectedShader;
153
154         m_radiantcore.m_pfnMessageBox = &gtk_MessageBox;
155         m_radiantcore.m_pfnFileDialog = &file_dialog;
156         m_radiantcore.m_pfnColorDialog = &color_dialog;
157         m_radiantcore.m_pfnDirDialog = &dir_dialog;
158         m_radiantcore.m_pfnNewImage = &new_plugin_image;
159 }
160 _QERFuncTable_1* getTable(){
161         return &m_radiantcore;
162 }
163 };
164
165 typedef SingletonModule<RadiantCoreAPI> RadiantCoreModule;
166 typedef Static<RadiantCoreModule> StaticRadiantCoreModule;
167 StaticRegisterModule staticRegisterRadiantCore( StaticRadiantCoreModule::instance() );
168
169
170 class RadiantDependencies :
171         public GlobalRadiantModuleRef,
172         public GlobalFileSystemModuleRef,
173         public GlobalEntityModuleRef,
174         public GlobalShadersModuleRef,
175         public GlobalBrushModuleRef,
176         public GlobalSceneGraphModuleRef,
177         public GlobalShaderCacheModuleRef,
178         public GlobalFiletypesModuleRef,
179         public GlobalSelectionModuleRef,
180         public GlobalReferenceModuleRef,
181         public GlobalOpenGLModuleRef,
182         public GlobalEntityClassManagerModuleRef,
183         public GlobalUndoModuleRef,
184         public GlobalScripLibModuleRef,
185         public GlobalNamespaceModuleRef
186 {
187 ImageModulesRef m_image_modules;
188 MapModulesRef m_map_modules;
189 ToolbarModulesRef m_toolbar_modules;
190 PluginModulesRef m_plugin_modules;
191
192 public:
193 RadiantDependencies() :
194         GlobalEntityModuleRef( GlobalRadiant().getRequiredGameDescriptionKeyValue( "entities" ) ),
195         GlobalShadersModuleRef( GlobalRadiant().getRequiredGameDescriptionKeyValue( "shaders" ) ),
196         GlobalBrushModuleRef( GlobalRadiant().getRequiredGameDescriptionKeyValue( "brushtypes" ) ),
197         GlobalEntityClassManagerModuleRef( GlobalRadiant().getRequiredGameDescriptionKeyValue( "entityclass" ) ),
198         m_image_modules( GlobalRadiant().getRequiredGameDescriptionKeyValue( "texturetypes" ) ),
199         m_map_modules( GlobalRadiant().getRequiredGameDescriptionKeyValue( "maptypes" ) ),
200         m_toolbar_modules( "*" ),
201         m_plugin_modules( "*" ){
202 }
203
204 ImageModules& getImageModules(){
205         return m_image_modules.get();
206 }
207 MapModules& getMapModules(){
208         return m_map_modules.get();
209 }
210 ToolbarModules& getToolbarModules(){
211         return m_toolbar_modules.get();
212 }
213 PluginModules& getPluginModules(){
214         return m_plugin_modules.get();
215 }
216 };
217
218 class Radiant : public TypeSystemRef
219 {
220 public:
221 Radiant(){
222         Preferences_Init();
223
224         GlobalFiletypes().addType( "sound", "wav", filetype_t( "PCM sound files", "*.wav" ) );
225
226         Selection_construct();
227         HomePaths_Construct();
228         VFS_Construct();
229         Grid_construct();
230         MultiMon_Construct();
231         MRU_Construct();
232         Pointfile_Construct();
233         GLWindow_Construct();
234         BuildMenu_Construct();
235         Map_Construct();
236         EntityList_Construct();
237         MainFrame_Construct();
238         GroupDialog_Construct();
239         SurfaceInspector_Construct();
240         PatchInspector_Construct();
241         CamWnd_Construct();
242         XYWindow_Construct();
243         BuildMonitor_Construct();
244         TextureBrowser_Construct();
245         Entity_Construct();
246         Autosave_Construct();
247         EntityInspector_construct();
248         FindTextureDialog_Construct();
249         NullModel_construct();
250         MapRoot_construct();
251
252         EnginePath_verify();
253         BindMapFileObservers();
254         EnginePath_Realise();
255 }
256 ~Radiant(){
257         EnginePath_Unrealise();
258         UnBindMapFileObservers();
259
260         MapRoot_destroy();
261         NullModel_destroy();
262         FindTextureDialog_Destroy();
263         EntityInspector_destroy();
264         Autosave_Destroy();
265         Entity_Destroy();
266         TextureBrowser_Destroy();
267         BuildMonitor_Destroy();
268         XYWindow_Destroy();
269         CamWnd_Destroy();
270         PatchInspector_Destroy();
271         SurfaceInspector_Destroy();
272         GroupDialog_Destroy();
273         MainFrame_Destroy();
274         EntityList_Destroy();
275         Map_Destroy();
276         BuildMenu_Destroy();
277         GLWindow_Destroy();
278         Pointfile_Destroy();
279         MRU_Destroy();
280         MultiMon_Destroy();
281         Grid_destroy();
282         VFS_Destroy();
283         HomePaths_Destroy();
284         Selection_destroy();
285 }
286 };
287
288 namespace
289 {
290 bool g_RadiantInitialised = false;
291 RadiantDependencies* g_RadiantDependencies;
292 Radiant* g_Radiant;
293 }
294
295
296
297 bool Radiant_Construct( ModuleServer& server ){
298         GlobalModuleServer::instance().set( server );
299         StaticModuleRegistryList().instance().registerModules();
300
301         g_RadiantDependencies = new RadiantDependencies();
302
303         g_RadiantInitialised = !server.getError();
304
305         if ( g_RadiantInitialised ) {
306                 g_Radiant = new Radiant;
307         }
308
309         return g_RadiantInitialised;
310 }
311 void Radiant_Destroy(){
312         if ( g_RadiantInitialised ) {
313                 delete g_Radiant;
314         }
315
316         delete g_RadiantDependencies;
317 }
318
319 ImageModules& Radiant_getImageModules(){
320         return g_RadiantDependencies->getImageModules();
321 }
322 MapModules& Radiant_getMapModules(){
323         return g_RadiantDependencies->getMapModules();
324 }
325 ToolbarModules& Radiant_getToolbarModules(){
326         return g_RadiantDependencies->getToolbarModules();
327 }
328 PluginModules& Radiant_getPluginModules(){
329         return g_RadiantDependencies->getPluginModules();
330 }