DEADSOFTWARE

95692d90bcb0c49d5f78d449012caca91cb929c3
[d2df-editor.git] / src / editor / Editor.lpr
1 program Editor;
3 {$INCLUDE ../shared/a_modes.inc}
5 uses
6 {$IFDEF DARWIN}
7 MacOSAll, CocoaAll,
8 {$ENDIF}
9 Forms, Interfaces, Dialogs,
10 GL, GLExt, SysUtils,
11 e_graphics in '../engine/e_graphics.pas',
12 e_log in '../engine/e_log.pas',
13 e_textures in '../engine/e_textures.pas',
14 MAPSTRUCT in '../shared/MAPSTRUCT.pas',
15 MAPREADER in '../shared/MAPREADER.pas',
16 MAPWRITER in '../shared/MAPWRITER.pas',
17 MAPDEF in '../shared/MAPDEF.pas',
18 WADEDITOR in '../shared/WADEDITOR.pas',
19 WADSTRUCT in '../shared/WADSTRUCT.pas',
20 CONFIG in '../shared/CONFIG.pas',
21 xstreams in '../shared/xstreams.pas',
22 dfzip in '../shared/dfzip.pas',
23 sfs in '../sfs/sfs.pas',
24 sfsPlainFS in '../sfs/sfsPlainFS.pas',
25 sfsZipFS in '../sfs/sfsZipFS.pas',
27 f_about in 'f_about.pas' {AboutForm},
28 f_options in 'f_options.pas' {OptionsForm},
29 f_main in 'f_main.pas' {MainForm},
30 g_map in 'g_map.pas',
31 f_mapoptions in 'f_mapoptions.pas' {MapOptionsForm},
32 f_activationtype in 'f_activationtype.pas' {ActivationTypeForm},
33 f_addresource in 'f_addresource.pas' {AddResourceForm},
34 f_keys in 'f_keys.pas' {KeysForm},
35 f_mapcheck in 'f_mapcheck.pas' {MapCheckForm},
36 f_mapoptimization in 'f_mapoptimization.pas' {MapOptimizationForm},
37 g_basic in 'g_basic.pas',
38 g_textures in 'g_textures.pas',
39 f_addresource_texture in 'f_addresource_texture.pas' {AddTextureForm},
40 f_savemap in 'f_savemap.pas' {SaveMapForm},
41 f_selectmap in 'f_selectmap.pas' {SelectMapForm},
42 f_addresource_sky in 'f_addresource_sky.pas' {AddSkyForm},
43 f_addresource_sound in 'f_addresource_sound.pas' {AddSoundForm},
44 spectrum in 'spectrum.pas',
45 f_saveminimap in 'f_saveminimap.pas' {SaveMiniMapForm},
46 f_packmap in 'f_packmap.pas' {PackMapForm},
47 f_maptest in 'f_maptest.pas' {MapTestForm},
48 f_choosetype in 'f_choosetype.pas' {ChooseTypeForm},
49 {$IFNDEF NOSOUND}
50 fmod,
51 fmoderrors,
52 fmodpresets,
53 fmodtypes,
54 {$ENDIF}
55 ImagingTypes,
56 Imaging,
57 ImagingUtility,
58 g_options in 'g_options.pas',
59 g_language in 'g_language.pas',
60 f_selectlang in 'f_selectlang.pas' {SelectLanguageForm};
62 {$IFDEF WINDOWS}
63 {$R *.res}
64 {$ENDIF}
66 type
67 THandlerObject = class (TObject)
68 procedure ExceptionHandler (Sender: TObject; e: Exception);
69 end;
71 var
72 LogFileName: AnsiString = '';
73 ParamFileIndex: Integer = 1;
75 {$IFDEF DARWIN}
76 function NSStringToAnsiString (s: NSString): AnsiString;
77 var i: Integer;
78 begin
79 result := '';
80 for i := 0 to s.length - 1 do
81 result := result + AnsiChar(s.characterAtIndex(i));
82 end;
84 function GetBundlePath (): AnsiString;
85 var pathRef: CFURLRef; pathCFStr: CFStringRef; pathStr: ShortString;
86 begin
87 pathRef := CFBundleCopyBundleURL(CFBundleGetMainBundle());
88 pathCFStr := CFURLCopyFileSystemPath(pathRef, kCFURLPOSIXPathStyle);
89 CFStringGetPascalString(pathCFStr, @pathStr, 255, CFStringGetSystemEncoding());
90 CFRelease(pathRef);
91 CFRelease(pathCFStr);
92 Result := pathStr;
93 end;
94 {$ENDIF}
96 procedure THandlerObject.ExceptionHandler (Sender: TObject; e: Exception);
97 begin
98 e_WriteStackTrace(e.message);
99 MessageDlg('Unhandled exception: ' + e.message + ' (see Editor.log for more information)', mtError, [mbOK], 0);
100 end;
102 procedure CheckParamOptions;
103 var i: Integer; p: AnsiString;
104 begin
105 i := 1;
106 while (i <= ParamCount) and (Length(ParamStr(i)) > 0) and (ParamStr(i)[1] = '-') do
107 begin
108 p := ParamStr(i);
109 if p = '--log-file' then
110 begin
111 if i + 1 <= ParamCount then
112 begin
113 Inc(i);
114 LogFileName := ParamStr(i);
115 end;
116 end
117 else if p = '--config' then
118 begin
119 if i + 1 <= ParamCount then
120 begin
121 Inc(i);
122 CfgFileName := ParamStr(i);
123 end;
124 end
125 else if p = '--game-wad' then
126 begin
127 if i + 1 <= ParamCount then
128 begin
129 Inc(i);
130 GameWad := ParamStr(i);
131 end;
132 end
133 else if p = '--editor-wad' then
134 begin
135 if i + 1 <= ParamCount then
136 begin
137 Inc(i);
138 EditorWad := ParamStr(i);
139 end;
140 end
141 else if p = '--wads-dir' then
142 begin
143 if i + 1 <= ParamCount then
144 begin
145 Inc(i);
146 WadsDir := ParamStr(i);
147 end;
148 end;
149 Inc(i);
150 end;
151 ParamFileIndex := i;
152 end;
154 procedure CheckParamFiles;
155 var i: Integer;
156 begin
157 i := ParamFileIndex;
158 if i <= ParamCount then
159 StartMap := ParamStr(i);
160 end;
162 procedure InitPathes;
163 {$IFDEF DARWIN}
164 var BundlePath, DFPath, DocPath: AnsiString; ns: NSString;
165 var ApplicationSupportDirs, DocumentDirs: NSArray;
166 var count: Integer;
167 {$ELSE}
168 var EditorDir: AnsiString;
169 {$ENDIF}
170 begin
171 {$IFDEF DARWIN}
172 BundlePath := GetBundlePath();
173 ApplicationSupportDirs := NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, true);
174 count := ApplicationSupportDirs.count;
175 ns := ApplicationSupportDirs.objectAtIndex(count - 1);
176 DFPath := NSStringToAnsiString(ns) + DirectorySeparator + 'Doom 2D Forever';
177 DocumentDirs := NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true);
178 count := DocumentDirs.count;
179 ns := DocumentDirs.objectAtIndex(count - 1);
180 DocPath := NSStringToAnsiString(ns) + DirectorySeparator + 'Doom 2D Forever';
181 GameExeFile := 'Doom 2D Forever.app';
182 CfgFileName := DFPath + DirectorySeparator + 'Editor.cfg';
183 LogFileName := DFPath + DirectorySeparator + 'Editor.log';
184 MapsDir := DocPath + DirectorySeparator + 'Maps';
185 WadsDir := BundlePath + DirectorySeparator + 'Contents' + DirectorySeparator + 'Resources' + DirectorySeparator + 'wads';
186 GameWad := BundlePath + DirectorySeparator + 'Contents' + DirectorySeparator + 'Resources' + DirectorySeparator + 'data' + DirectorySeparator + 'game.wad';
187 EditorWad := BundlePath + DirectorySeparator + 'Contents' + DirectorySeparator + 'Resources' + DirectorySeparator + 'data' + DirectorySeparator + 'editor.wad';
188 {$ELSE}
189 EditorDir := ExtractFilePath(Application.ExeName);
190 {$IFDEF WINDOWS}
191 GameExeFile := 'Doom2DF.exe';
192 {$ELSE}
193 GameExeFile := 'Doom2DF';
194 {$ENDIF}
195 CfgFileName := EditorDir + DirectorySeparator + 'Editor.cfg';
196 LogFileName := EditorDir + DirectorySeparator + 'Editor.log';
197 MapsDir := EditorDir + DirectorySeparator + 'maps';
198 WadsDir := EditorDir + DirectorySeparator + 'wads';
199 GameWad := EditorDir + DirectorySeparator + 'data' + DirectorySeparator + 'game.wad';
200 EditorWad := EditorDir + DirectorySeparator + 'data' + DirectorySeparator + 'editor.wad';
201 {$ENDIF}
202 ForceDirectories(MapsDir);
203 ForceDirectories(WadsDir);
204 end;
206 procedure InitLogs;
207 begin
208 e_InitLog(LogFileName, WM_NEWFILE);
210 {$IF DECLARED(UseHeapTrace)}
211 (* http://wiki.freepascal.org/heaptrc *)
212 GlobalSkipIfNoLeaks := True;
213 //SetHeapTraceOutput('EditorLeaks.log');
214 //HaltOnError := False;
215 {$ENDIF}
217 e_WriteLog('Used file pathes:', MSG_NOTIFY);
218 e_WriteLog(' GameExeFile = ' + GameExeFile, MSG_NOTIFY);
219 e_WriteLog(' CfgFileName = ' + CfgFileName, MSG_NOTIFY);
220 e_WriteLog(' LogFileName = ' + LogFileName, MSG_NOTIFY);
221 e_WriteLog(' MapsDir = ' + MapsDir, MSG_NOTIFY);
222 e_WriteLog(' WadsDir = ' + WadsDir, MSG_NOTIFY);
223 e_WriteLog(' GameWad = ' + GameWad, MSG_NOTIFY);
224 e_WriteLog(' EditorWad = ' + EditorWad, MSG_NOTIFY);
225 end;
227 begin
228 Application.ExceptionDialog := aedOkMessageBox;
229 Application.AddOnExceptionHandler(THandlerObject.ExceptionHandler, True);
230 Application.Initialize();
231 {$IFDEF DARWIN}
232 // Disable icons in menu on OSX by default
233 Application.ShowMenuGlyphs := sbgNever;
234 {$ENDIF}
236 InitPathes;
237 CheckParamOptions;
238 InitLogs;
240 Application.CreateForm(TMainForm, MainForm);
241 Application.CreateForm(TOptionsForm, OptionsForm);
242 Application.CreateForm(TAboutForm, AboutForm);
243 Application.CreateForm(TMapOptionsForm, MapOptionsForm);
244 Application.CreateForm(TActivationTypeForm, ActivationTypeForm);
245 Application.CreateForm(TAddResourceForm, AddResourceForm);
246 Application.CreateForm(TKeysForm, KeysForm);
247 Application.CreateForm(TMapCheckForm, MapCheckForm);
248 Application.CreateForm(TMapOptimizationForm, MapOptimizationForm);
249 Application.CreateForm(TAddTextureForm, AddTextureForm);
250 Application.CreateForm(TSaveMapForm, SaveMapForm);
251 Application.CreateForm(TSelectMapForm, SelectMapForm);
252 Application.CreateForm(TAddSkyForm, AddSkyForm);
253 Application.CreateForm(TAddSoundForm, AddSoundForm);
254 Application.CreateForm(TSaveMiniMapForm, SaveMiniMapForm);
255 Application.CreateForm(TPackMapForm, PackMapForm);
256 Application.CreateForm(TMapTestForm, MapTestForm);
257 Application.CreateForm(TChooseTypeForm, ChooseTypeForm);
258 Application.CreateForm(TSelectLanguageForm, SelectLanguageForm);
260 CheckParamFiles;
262 Application.Run();
263 end.