DEADSOFTWARE

system: add option --lang-dir
[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 else if p = '--lang-dir' then
148 begin
149 if i + 1 <= ParamCount then
150 begin
151 Inc(i);
152 LangDir := ParamStr(i);
153 end;
154 end;
155 Inc(i);
156 end;
157 ParamFileIndex := i;
158 end;
160 procedure CheckParamFiles;
161 var i: Integer;
162 begin
163 i := ParamFileIndex;
164 if i <= ParamCount then
165 StartMap := ParamStr(i);
166 end;
168 procedure InitPathes;
169 {$IFDEF DARWIN}
170 var BundlePath, DFPath, DocPath: AnsiString; ns: NSString;
171 var ApplicationSupportDirs, DocumentDirs: NSArray;
172 var count: Integer;
173 {$ELSE}
174 var EditorDir: AnsiString;
175 {$ENDIF}
176 begin
177 {$IFDEF DARWIN}
178 BundlePath := GetBundlePath();
179 ApplicationSupportDirs := NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, true);
180 count := ApplicationSupportDirs.count;
181 ns := ApplicationSupportDirs.objectAtIndex(count - 1);
182 DFPath := NSStringToAnsiString(ns) + DirectorySeparator + 'Doom 2D Forever';
183 DocumentDirs := NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true);
184 count := DocumentDirs.count;
185 ns := DocumentDirs.objectAtIndex(count - 1);
186 DocPath := NSStringToAnsiString(ns) + DirectorySeparator + 'Doom 2D Forever';
187 GameExeFile := 'Doom 2D Forever.app';
188 CfgFileName := DFPath + DirectorySeparator + 'Editor.cfg';
189 LogFileName := DFPath + DirectorySeparator + 'Editor.log';
190 MapsDir := DocPath + DirectorySeparator + 'Maps';
191 WadsDir := BundlePath + DirectorySeparator + 'Contents' + DirectorySeparator + 'Resources' + DirectorySeparator + 'wads';
192 LangDIr := BundlePath + DirectorySeparator + 'Contents' + DirectorySeparator + 'Resources' + DirectorySeparator + 'data' + DirectorySeparator + 'lang';
193 GameWad := BundlePath + DirectorySeparator + 'Contents' + DirectorySeparator + 'Resources' + DirectorySeparator + 'data' + DirectorySeparator + 'game.wad';
194 EditorWad := BundlePath + DirectorySeparator + 'Contents' + DirectorySeparator + 'Resources' + DirectorySeparator + 'data' + DirectorySeparator + 'editor.wad';
195 {$ELSE}
196 EditorDir := ExtractFilePath(Application.ExeName);
197 {$IFDEF WINDOWS}
198 GameExeFile := 'Doom2DF.exe';
199 {$ELSE}
200 GameExeFile := 'Doom2DF';
201 {$ENDIF}
202 CfgFileName := EditorDir + DirectorySeparator + 'Editor.cfg';
203 LogFileName := EditorDir + DirectorySeparator + 'Editor.log';
204 MapsDir := EditorDir + DirectorySeparator + 'maps';
205 WadsDir := EditorDir + DirectorySeparator + 'wads';
206 LangDir := EditorDir + DirectorySeparator + 'data' + DirectorySeparator + 'lang';
207 GameWad := EditorDir + DirectorySeparator + 'data' + DirectorySeparator + 'game.wad';
208 EditorWad := EditorDir + DirectorySeparator + 'data' + DirectorySeparator + 'editor.wad';
209 {$ENDIF}
210 ForceDirectories(MapsDir);
211 ForceDirectories(WadsDir);
212 end;
214 procedure InitLogs;
215 begin
216 e_InitLog(LogFileName, WM_NEWFILE);
218 {$IF DECLARED(UseHeapTrace)}
219 (* http://wiki.freepascal.org/heaptrc *)
220 GlobalSkipIfNoLeaks := True;
221 //SetHeapTraceOutput('EditorLeaks.log');
222 //HaltOnError := False;
223 {$ENDIF}
225 e_WriteLog('Used file pathes:', MSG_NOTIFY);
226 e_WriteLog(' GameExeFile = ' + GameExeFile, MSG_NOTIFY);
227 e_WriteLog(' CfgFileName = ' + CfgFileName, MSG_NOTIFY);
228 e_WriteLog(' LogFileName = ' + LogFileName, MSG_NOTIFY);
229 e_WriteLog(' MapsDir = ' + MapsDir, MSG_NOTIFY);
230 e_WriteLog(' WadsDir = ' + WadsDir, MSG_NOTIFY);
231 e_WriteLog(' LangDir = ' + LangDir, MSG_NOTIFY);
232 e_WriteLog(' GameWad = ' + GameWad, MSG_NOTIFY);
233 e_WriteLog(' EditorWad = ' + EditorWad, MSG_NOTIFY);
234 end;
236 begin
237 Application.ExceptionDialog := aedOkMessageBox;
238 Application.AddOnExceptionHandler(THandlerObject.ExceptionHandler, True);
239 Application.Initialize();
240 {$IFDEF DARWIN}
241 // Disable icons in menu on OSX by default
242 Application.ShowMenuGlyphs := sbgNever;
243 {$ENDIF}
245 InitPathes;
246 CheckParamOptions;
247 InitLogs;
249 Application.CreateForm(TMainForm, MainForm);
250 Application.CreateForm(TOptionsForm, OptionsForm);
251 Application.CreateForm(TAboutForm, AboutForm);
252 Application.CreateForm(TMapOptionsForm, MapOptionsForm);
253 Application.CreateForm(TActivationTypeForm, ActivationTypeForm);
254 Application.CreateForm(TAddResourceForm, AddResourceForm);
255 Application.CreateForm(TKeysForm, KeysForm);
256 Application.CreateForm(TMapCheckForm, MapCheckForm);
257 Application.CreateForm(TMapOptimizationForm, MapOptimizationForm);
258 Application.CreateForm(TAddTextureForm, AddTextureForm);
259 Application.CreateForm(TSaveMapForm, SaveMapForm);
260 Application.CreateForm(TSelectMapForm, SelectMapForm);
261 Application.CreateForm(TAddSkyForm, AddSkyForm);
262 Application.CreateForm(TAddSoundForm, AddSoundForm);
263 Application.CreateForm(TSaveMiniMapForm, SaveMiniMapForm);
264 Application.CreateForm(TPackMapForm, PackMapForm);
265 Application.CreateForm(TChooseTypeForm, ChooseTypeForm);
267 g_Language_Set(gLanguage);
269 CheckParamFiles;
271 Application.Run();
272 end.