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