DEADSOFTWARE

1ca2d20f5378c543ada9d7d268f207fec2b88100
[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_choosetype in 'f_choosetype.pas' {ChooseTypeForm},
48 {$IFNDEF NOSOUND}
49 fmod,
50 fmoderrors,
51 fmodpresets,
52 fmodtypes,
53 {$ENDIF}
54 ImagingTypes,
55 Imaging,
56 ImagingUtility,
57 g_options in 'g_options.pas',
58 g_language in 'g_language.pas';
60 {$IFDEF WINDOWS}
61 {$R *.res}
62 {$ENDIF}
64 type
65 THandlerObject = class (TObject)
66 procedure ExceptionHandler (Sender: TObject; e: Exception);
67 end;
69 var
70 LogFileName: AnsiString = '';
71 ParamFileIndex: Integer = 1;
73 {$IFDEF DARWIN}
74 function NSStringToAnsiString (s: NSString): AnsiString;
75 var i: Integer;
76 begin
77 result := '';
78 for i := 0 to s.length - 1 do
79 result := result + AnsiChar(s.characterAtIndex(i));
80 end;
82 function GetBundlePath (): AnsiString;
83 var pathRef: CFURLRef; pathCFStr: CFStringRef; pathStr: ShortString;
84 begin
85 pathRef := CFBundleCopyBundleURL(CFBundleGetMainBundle());
86 pathCFStr := CFURLCopyFileSystemPath(pathRef, kCFURLPOSIXPathStyle);
87 CFStringGetPascalString(pathCFStr, @pathStr, 255, CFStringGetSystemEncoding());
88 CFRelease(pathRef);
89 CFRelease(pathCFStr);
90 Result := pathStr;
91 end;
92 {$ENDIF}
94 procedure THandlerObject.ExceptionHandler (Sender: TObject; e: Exception);
95 begin
96 e_WriteStackTrace(e.message);
97 MessageDlg('Unhandled exception: ' + e.message + ' (see Editor.log for more information)', mtError, [mbOK], 0);
98 end;
100 procedure CheckParamOptions;
101 var i: Integer; p: AnsiString;
102 begin
103 i := 1;
104 while (i <= ParamCount) and (Length(ParamStr(i)) > 0) and (ParamStr(i)[1] = '-') do
105 begin
106 p := ParamStr(i);
107 if p = '--log-file' then
108 begin
109 if i + 1 <= ParamCount then
110 begin
111 Inc(i);
112 LogFileName := ParamStr(i);
113 end;
114 end
115 else if p = '--config' then
116 begin
117 if i + 1 <= ParamCount then
118 begin
119 Inc(i);
120 CfgFileName := ParamStr(i);
121 end;
122 end
123 else if p = '--game-wad' then
124 begin
125 if i + 1 <= ParamCount then
126 begin
127 Inc(i);
128 GameWad := ParamStr(i);
129 end;
130 end
131 else if p = '--editor-wad' then
132 begin
133 if i + 1 <= ParamCount then
134 begin
135 Inc(i);
136 EditorWad := ParamStr(i);
137 end;
138 end
139 else if p = '--wads-dir' then
140 begin
141 if i + 1 <= ParamCount then
142 begin
143 Inc(i);
144 WadsDir := ParamStr(i);
145 end;
146 end;
147 Inc(i);
148 end;
149 ParamFileIndex := i;
150 end;
152 procedure CheckParamFiles;
153 var i: Integer;
154 begin
155 i := ParamFileIndex;
156 if i <= ParamCount then
157 StartMap := ParamStr(i);
158 end;
160 procedure InitPathes;
161 {$IFDEF DARWIN}
162 var BundlePath, DFPath, DocPath: AnsiString; ns: NSString;
163 var ApplicationSupportDirs, DocumentDirs: NSArray;
164 var count: Integer;
165 {$ELSE}
166 var EditorDir: AnsiString;
167 {$ENDIF}
168 begin
169 {$IFDEF DARWIN}
170 BundlePath := GetBundlePath();
171 ApplicationSupportDirs := NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, true);
172 count := ApplicationSupportDirs.count;
173 ns := ApplicationSupportDirs.objectAtIndex(count - 1);
174 DFPath := NSStringToAnsiString(ns) + DirectorySeparator + 'Doom 2D Forever';
175 DocumentDirs := NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true);
176 count := DocumentDirs.count;
177 ns := DocumentDirs.objectAtIndex(count - 1);
178 DocPath := NSStringToAnsiString(ns) + DirectorySeparator + 'Doom 2D Forever';
179 GameExeFile := 'Doom 2D Forever.app';
180 CfgFileName := DFPath + DirectorySeparator + 'Editor.cfg';
181 LogFileName := DFPath + DirectorySeparator + 'Editor.log';
182 MapsDir := DocPath + DirectorySeparator + 'Maps';
183 WadsDir := BundlePath + DirectorySeparator + 'Contents' + DirectorySeparator + 'Resources' + DirectorySeparator + 'wads';
184 LangDIr := BundlePath + DirectorySeparator + 'Contents' + DirectorySeparator + 'Resources' + DirectorySeparator + 'data' + DirectorySeparator + 'lang';
185 GameWad := BundlePath + DirectorySeparator + 'Contents' + DirectorySeparator + 'Resources' + DirectorySeparator + 'data' + DirectorySeparator + 'game.wad';
186 EditorWad := BundlePath + DirectorySeparator + 'Contents' + DirectorySeparator + 'Resources' + DirectorySeparator + 'data' + DirectorySeparator + 'editor.wad';
187 {$ELSE}
188 EditorDir := ExtractFilePath(Application.ExeName);
189 {$IFDEF WINDOWS}
190 GameExeFile := 'Doom2DF.exe';
191 {$ELSE}
192 GameExeFile := 'Doom2DF';
193 {$ENDIF}
194 CfgFileName := EditorDir + DirectorySeparator + 'Editor.cfg';
195 LogFileName := EditorDir + DirectorySeparator + 'Editor.log';
196 MapsDir := EditorDir + DirectorySeparator + 'maps';
197 WadsDir := EditorDir + DirectorySeparator + 'wads';
198 LangDir := EditorDir + DirectorySeparator + 'data' + DirectorySeparator + 'lang';
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(' LangDir = ' + LangDir, MSG_NOTIFY);
224 e_WriteLog(' GameWad = ' + GameWad, MSG_NOTIFY);
225 e_WriteLog(' EditorWad = ' + EditorWad, MSG_NOTIFY);
226 end;
228 begin
229 Application.ExceptionDialog := aedOkMessageBox;
230 Application.AddOnExceptionHandler(THandlerObject.ExceptionHandler, True);
231 Application.Initialize();
232 {$IFDEF DARWIN}
233 // Disable icons in menu on OSX by default
234 Application.ShowMenuGlyphs := sbgNever;
235 {$ENDIF}
237 InitPathes;
238 CheckParamOptions;
239 InitLogs;
241 Application.CreateForm(TMainForm, MainForm);
242 Application.CreateForm(TOptionsForm, OptionsForm);
243 Application.CreateForm(TAboutForm, AboutForm);
244 Application.CreateForm(TMapOptionsForm, MapOptionsForm);
245 Application.CreateForm(TActivationTypeForm, ActivationTypeForm);
246 Application.CreateForm(TAddResourceForm, AddResourceForm);
247 Application.CreateForm(TKeysForm, KeysForm);
248 Application.CreateForm(TMapCheckForm, MapCheckForm);
249 Application.CreateForm(TMapOptimizationForm, MapOptimizationForm);
250 Application.CreateForm(TAddTextureForm, AddTextureForm);
251 Application.CreateForm(TSaveMapForm, SaveMapForm);
252 Application.CreateForm(TSelectMapForm, SelectMapForm);
253 Application.CreateForm(TAddSkyForm, AddSkyForm);
254 Application.CreateForm(TAddSoundForm, AddSoundForm);
255 Application.CreateForm(TSaveMiniMapForm, SaveMiniMapForm);
256 Application.CreateForm(TPackMapForm, PackMapForm);
257 Application.CreateForm(TChooseTypeForm, ChooseTypeForm);
259 g_Language_Set(gLanguage);
261 CheckParamFiles;
263 Application.Run();
264 end.