]> git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/plugin.cpp
Hack around segfault at launch, patch from danfe: https://github.com/TTimo/GtkRadiant...
[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 class RadiantCoreAPI
117 {
118 _QERFuncTable_1 m_radiantcore;
119 public:
120 typedef _QERFuncTable_1 Type;
121 STRING_CONSTANT( Name, "*" );
122
123 RadiantCoreAPI(){
124         m_radiantcore.getEnginePath = &EnginePath_get;
125         m_radiantcore.getLocalRcPath = &LocalRcPath_get;
126         m_radiantcore.getAppPath = &AppPath_get;
127         m_radiantcore.getGameToolsPath = &GameToolsPath_get;
128         m_radiantcore.getSettingsPath = &SettingsPath_get;
129         m_radiantcore.getMapsPath = &getMapsPath;
130
131         m_radiantcore.getGameName = &gamename_get;
132         m_radiantcore.getGameMode = &gamemode_get;
133
134         m_radiantcore.getMapName = &getMapName;
135         m_radiantcore.getMapWorldEntity = getMapWorldEntity;
136         m_radiantcore.getGridSize = GetGridSize;
137
138         m_radiantcore.getGameDescriptionKeyValue = &GameDescription_getKeyValue;
139         m_radiantcore.getRequiredGameDescriptionKeyValue = &GameDescription_getRequiredKeyValue;
140
141         m_radiantcore.attachGameToolsPathObserver = Radiant_attachGameToolsPathObserver;
142         m_radiantcore.detachGameToolsPathObserver = Radiant_detachGameToolsPathObserver;
143         m_radiantcore.attachEnginePathObserver = Radiant_attachEnginePathObserver;
144         m_radiantcore.detachEnginePathObserver = Radiant_detachEnginePathObserver;
145         m_radiantcore.attachGameNameObserver = Radiant_attachGameNameObserver;
146         m_radiantcore.detachGameNameObserver = Radiant_detachGameNameObserver;
147         m_radiantcore.attachGameModeObserver = Radiant_attachGameModeObserver;
148         m_radiantcore.detachGameModeObserver = Radiant_detachGameModeObserver;
149
150         m_radiantcore.XYWindowDestroyed_connect = XYWindowDestroyed_connect;
151         m_radiantcore.XYWindowDestroyed_disconnect = XYWindowDestroyed_disconnect;
152         m_radiantcore.XYWindowMouseDown_connect = XYWindowMouseDown_connect;
153         m_radiantcore.XYWindowMouseDown_disconnect = XYWindowMouseDown_disconnect;
154         m_radiantcore.XYWindow_getViewType = XYWindow_getViewType;
155         m_radiantcore.XYWindow_windowToWorld = XYWindow_windowToWorld;
156         m_radiantcore.TextureBrowser_getSelectedShader = TextureBrowser_getSelectedShader;
157
158         m_radiantcore.m_pfnMessageBox = &gtk_MessageBox;
159         m_radiantcore.m_pfnFileDialog = &file_dialog;
160         m_radiantcore.m_pfnColorDialog = &color_dialog;
161         m_radiantcore.m_pfnDirDialog = &dir_dialog;
162         m_radiantcore.m_pfnNewImage = &new_plugin_image;
163 }
164 _QERFuncTable_1* getTable(){
165         return &m_radiantcore;
166 }
167 };
168
169 typedef SingletonModule<RadiantCoreAPI> RadiantCoreModule;
170 typedef Static<RadiantCoreModule> StaticRadiantCoreModule;
171 StaticRegisterModule staticRegisterRadiantCore( StaticRadiantCoreModule::instance() );
172
173
174 class RadiantDependencies :
175         public GlobalRadiantModuleRef,
176         public GlobalFileSystemModuleRef,
177         public GlobalEntityModuleRef,
178         public GlobalShadersModuleRef,
179         public GlobalBrushModuleRef,
180         public GlobalSceneGraphModuleRef,
181         public GlobalShaderCacheModuleRef,
182         public GlobalFiletypesModuleRef,
183         public GlobalSelectionModuleRef,
184         public GlobalReferenceModuleRef,
185         public GlobalOpenGLModuleRef,
186         public GlobalEntityClassManagerModuleRef,
187         public GlobalUndoModuleRef,
188         public GlobalScripLibModuleRef,
189         public GlobalNamespaceModuleRef
190 {
191 ImageModulesRef m_image_modules;
192 MapModulesRef m_map_modules;
193 ToolbarModulesRef m_toolbar_modules;
194 PluginModulesRef m_plugin_modules;
195
196 public:
197 RadiantDependencies() :
198         GlobalEntityModuleRef( GlobalRadiant().getRequiredGameDescriptionKeyValue( "entities" ) ),
199         GlobalShadersModuleRef( GlobalRadiant().getRequiredGameDescriptionKeyValue( "shaders" ) ),
200         GlobalBrushModuleRef( GlobalRadiant().getRequiredGameDescriptionKeyValue( "brushtypes" ) ),
201         GlobalEntityClassManagerModuleRef( GlobalRadiant().getRequiredGameDescriptionKeyValue( "entityclass" ) ),
202         m_image_modules( GlobalRadiant().getRequiredGameDescriptionKeyValue( "texturetypes" ) ),
203         m_map_modules( GlobalRadiant().getRequiredGameDescriptionKeyValue( "maptypes" ) ),
204         m_toolbar_modules( "*" ),
205         m_plugin_modules( "*" ){
206 }
207
208 ImageModules& getImageModules(){
209         return m_image_modules.get();
210 }
211 MapModules& getMapModules(){
212         return m_map_modules.get();
213 }
214 ToolbarModules& getToolbarModules(){
215         return m_toolbar_modules.get();
216 }
217 PluginModules& getPluginModules(){
218         return m_plugin_modules.get();
219 }
220 };
221
222 class Radiant : public TypeSystemRef
223 {
224 public:
225 Radiant(){
226         Preferences_Init();
227
228         GlobalFiletypes().addType( "sound", "wav", filetype_t( "PCM sound files", "*.wav" ) );
229
230         Selection_construct();
231         HomePaths_Construct();
232         VFS_Construct();
233         Grid_construct();
234         MultiMon_Construct();
235         MRU_Construct();
236         Pointfile_Construct();
237         GLWindow_Construct();
238         BuildMenu_Construct();
239         Map_Construct();
240         EntityList_Construct();
241         MainFrame_Construct();
242         GroupDialog_Construct();
243         SurfaceInspector_Construct();
244         PatchInspector_Construct();
245         CamWnd_Construct();
246         XYWindow_Construct();
247         BuildMonitor_Construct();
248         TextureBrowser_Construct();
249         Entity_Construct();
250         Autosave_Construct();
251         EntityInspector_construct();
252         FindTextureDialog_Construct();
253         NullModel_construct();
254         MapRoot_construct();
255
256         EnginePath_verify();
257         EnginePath_Realise();
258 }
259 ~Radiant(){
260         EnginePath_Unrealise();
261
262         MapRoot_destroy();
263         NullModel_destroy();
264         FindTextureDialog_Destroy();
265         EntityInspector_destroy();
266         Autosave_Destroy();
267         Entity_Destroy();
268         TextureBrowser_Destroy();
269         BuildMonitor_Destroy();
270         XYWindow_Destroy();
271         CamWnd_Destroy();
272         PatchInspector_Destroy();
273         SurfaceInspector_Destroy();
274         GroupDialog_Destroy();
275         MainFrame_Destroy();
276         EntityList_Destroy();
277         Map_Destroy();
278         BuildMenu_Destroy();
279         GLWindow_Destroy();
280         Pointfile_Destroy();
281         MRU_Destroy();
282         MultiMon_Destroy();
283         Grid_destroy();
284         VFS_Destroy();
285         HomePaths_Destroy();
286         Selection_destroy();
287 }
288 };
289
290 namespace
291 {
292 bool g_RadiantInitialised = false;
293 RadiantDependencies* g_RadiantDependencies;
294 Radiant* g_Radiant;
295 }
296
297
298
299 bool Radiant_Construct( ModuleServer& server ){
300         GlobalModuleServer::instance().set( server );
301         StaticModuleRegistryList().instance().registerModules();
302
303         g_RadiantDependencies = new RadiantDependencies();
304
305         g_RadiantInitialised = !server.getError();
306
307         if ( g_RadiantInitialised ) {
308                 g_Radiant = new Radiant;
309         }
310
311         return g_RadiantInitialised;
312 }
313 void Radiant_Destroy(){
314         if ( g_RadiantInitialised ) {
315                 delete g_Radiant;
316         }
317
318         delete g_RadiantDependencies;
319 }
320
321 ImageModules& Radiant_getImageModules(){
322         return g_RadiantDependencies->getImageModules();
323 }
324 MapModules& Radiant_getMapModules(){
325         return g_RadiantDependencies->getMapModules();
326 }
327 ToolbarModules& Radiant_getToolbarModules(){
328         return g_RadiantDependencies->getToolbarModules();
329 }
330 PluginModules& Radiant_getPluginModules(){
331         return g_RadiantDependencies->getPluginModules();
332 }