DEADSOFTWARE

fix file opening from command line
[d2df-editor.git] / src / editor / Editor.lpr
index 8bd60c2e3f98df376d9203d5c42743332cebdd36..171c8564c25c7313e70c80e65dc5cd8d0ee7aa10 100644 (file)
@@ -3,8 +3,8 @@ program Editor;
 {$INCLUDE ../shared/a_modes.inc}
 
 uses
-  Forms, Interfaces,
-  GL, GLExt,
+  Forms, Interfaces, Dialogs,
+  GL, GLExt, SysUtils,
   e_graphics in '../engine/e_graphics.pas',
   e_log in '../engine/e_log.pas',
   e_textures in '../engine/e_textures.pas',
@@ -15,6 +15,12 @@ uses
   WADEDITOR in '../shared/WADEDITOR.pas',
   WADSTRUCT in '../shared/WADSTRUCT.pas',
   CONFIG in '../shared/CONFIG.pas',
+  xstreams in '../shared/xstreams.pas',
+  dfzip in '../shared/dfzip.pas',
+  sfs in '../sfs/sfs.pas',
+  sfsPlainFS in '../sfs/sfsPlainFS.pas',
+  sfsZipFS in '../sfs/sfsZipFS.pas',
+
   f_about in 'f_about.pas' {AboutForm},
   f_options in 'f_options.pas' {OptionsForm},
   f_main in 'f_main.pas' {MainForm},
@@ -46,6 +52,7 @@ uses
   ImagingTypes,
   Imaging,
   ImagingUtility,
+  g_options in 'g_options.pas',
   g_language in 'g_language.pas',
   f_selectlang in 'f_selectlang.pas' {SelectLanguageForm};
 
@@ -53,10 +60,102 @@ uses
   {$R *.res}
 {$ENDIF}
 
+  type
+    THandlerObject = class (TObject)
+      procedure ExceptionHandler (Sender: TObject; e: Exception);
+    end;
+
+  var
+    LogFileName: AnsiString = '';
+    ParamFileIndex: Integer = 1;
+
+  procedure THandlerObject.ExceptionHandler (Sender: TObject; e: Exception);
+  begin
+    e_WriteStackTrace(e.message);
+    MessageDlg('Unhandled exception: ' + e.message + ' (see Editor.log for more information)', mtError, [mbOK], 0);
+  end;
+
+  procedure CheckParamOptions;
+    var i: Integer; p: AnsiString;
+  begin
+    i := 1;
+    while (i <= ParamCount) and (Length(ParamStr(i)) > 0) and (ParamStr(i)[1] = '-') do
+    begin
+      p := ParamStr(i);
+      if p = '--log-file' then
+      begin
+        if i + 1 <= ParamCount then
+        begin
+          Inc(i);
+          LogFileName := ParamStr(i);
+        end;
+      end
+      else if p = '--config' then
+      begin
+        if i + 1 <= ParamCount then
+        begin
+          Inc(i);
+          CfgFileName := ParamStr(i);
+        end;
+      end
+      else if p = '--game-wad' then
+      begin
+        if i + 1 <= ParamCount then
+        begin
+          Inc(i);
+          GameWad := ParamStr(i);
+        end;
+      end
+      else if p = '--editor-wad' then
+      begin
+        if i + 1 <= ParamCount then
+        begin
+          Inc(i);
+          EditorWad := ParamStr(i);
+        end;
+      end;
+      Inc(i);
+    end;
+    ParamFileIndex := i;
+  end;
+
+  procedure CheckParamFiles;
+    var i: Integer;
+  begin
+    i := ParamFileIndex;
+    if i <= ParamCount then
+      StartMap := ParamStr(i);
+  end;
+
+  procedure InitLogs;
+  begin
+    if LogFileName = '' then
+      LogFileName := 'Editor.log';
+
+    if LogFileName <> '' then
+      e_InitLog(LogFileName, WM_NEWFILE);
+
+    {$IF DECLARED(UseHeapTrace)}
+      (* http://wiki.freepascal.org/heaptrc *)
+      GlobalSkipIfNoLeaks := True;
+      //SetHeapTraceOutput('EditorLeaks.log');
+      //HaltOnError := False;
+    {$ENDIF}
+  end;
+
 begin
   Application.ExceptionDialog := aedOkMessageBox;
+  Application.AddOnExceptionHandler(THandlerObject.ExceptionHandler, True);
   Application.Initialize();
 
+  EditorDir := ExtractFilePath(Application.ExeName);
+  CfgFileName := EditorDir + DirectorySeparator + 'Editor.cfg';
+  GameWad := EditorDir + DirectorySeparator + 'data' + DirectorySeparator + 'game.wad';
+  EditorWad := EditorDir + DirectorySeparator + 'data' + DirectorySeparator + 'editor.wad';
+
+  CheckParamOptions;
+  InitLogs;
+
   Application.CreateForm(TMainForm, MainForm);
   Application.CreateForm(TOptionsForm, OptionsForm);
   Application.CreateForm(TAboutForm, AboutForm);
@@ -77,6 +176,7 @@ begin
   Application.CreateForm(TChooseTypeForm, ChooseTypeForm);
   Application.CreateForm(TSelectLanguageForm, SelectLanguageForm);
 
-  if ParamStr(1) <> '' then OpenMap(ParamStr(1), '');
+  CheckParamFiles;
+
   Application.Run();
 end.