patch by lucass (polished by me) - command line parsing is now much more flexible
authorkoda
Sat, 31 Jul 2010 08:19:56 +0200
changeset 3678 00428183300f
parent 3676 fa29572fa56a
child 3680 aaf832c6fbd7
patch by lucass (polished by me) - command line parsing is now much more flexible
hedgewars/ArgParsers.inc
hedgewars/hwengine.pas
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/hedgewars/ArgParsers.inc	Sat Jul 31 08:19:56 2010 +0200
@@ -0,0 +1,212 @@
+
+procedure internalSetGameTypeLandPreviewFromParameters();
+begin
+    val(ParamStr(2), ipcPort);
+    GameType:= gmtLandPreview;
+    if ParamStr(3) <> 'landpreview' then
+        GameType:= gmtSyntax;
+end;
+
+procedure internalStartGameWithParameters();
+begin
+    val(ParamStr(2), cScreenWidth);
+    val(ParamStr(3), cScreenHeight);
+    cBitsStr:= ParamStr(4);
+    val(cBitsStr, cBits);
+    val(ParamStr(5), ipcPort);
+    cFullScreen:= ParamStr(6) = '1';
+    isSoundEnabled:= ParamStr(7) = '1';
+    //cVSyncInUse:= ParamStr(8) = '1';      //merged with rqFlags
+    //cWeaponTooltips:= ParamStr(9) = '1';  //merged with rqFlags
+    cLocaleFName:= ParamStr(10);
+    val(ParamStr(11), cInitVolume);
+    val(ParamStr(12), cTimerInterval);
+    PathPrefix:= ParamStr(13);
+    cShowFPS:= ParamStr(14) = '1';
+    cAltDamage:= ParamStr(15) = '1';
+    UserNick:= DecodeBase64(ParamStr(16));
+    isMusicEnabled:= ParamStr(17) = '1';
+
+    if (ParamStr(18) = '1') then        //HACK - always disable rqLowRes as it is a game breaker
+        cReducedQuality:= $FFFFFFFF xor rqLowRes
+    else
+        val(ParamStr(18), cReducedQuality);
+    
+    if (ParamStr(8) = '0') then         //HACK - ifcVSyncInUse not true, disable it
+        cReducedQuality:= cReducedQuality xor rqDesyncVBlank;
+    if (ParamStr(9) = '0') then         //HACK - if cWeaponTooltips not true, disable it
+        cReducedQuality:= cReducedQuality xor rqTooltipsOff;
+end;
+
+procedure setVideo(screenWidth: LongInt; screenHeight: LongInt; bitsStr: LongInt);
+begin
+    cScreenWidth:= screenWidth;
+    cScreenHeight:= screenHeight;
+    cBits:= bitsStr;
+end;
+
+procedure setVideoWithParameters(screenWidthParam: string; screenHeightParam: string; bitsParam: string);
+var screenWidthAsInt, screenHeightAsInt, bitsStrAsInt: LongInt;
+begin
+    val(screenWidthParam, screenWidthAsInt);
+    val(screenHeightParam, screenHeightAsInt);
+    cBitsStr:= bitsParam;
+    val(cBitsStr, bitsStrAsInt);
+    setVideo(screenWidthAsInt,screenHeightAsInt,bitsStrAsInt);
+end;
+
+procedure setOtherOptions(languageFile: string; fullScreen: boolean);
+begin
+    cLocaleFName:= languageFile;
+    cFullScreen:= fullScreen;
+end;
+
+procedure setShowFPS(showFPS: boolean);
+begin
+    cShowFPS:= showFPS;
+end;
+
+procedure setOtherOptionsWithParameters(languageFileParam: string; fullScreenParam: string; showFPSParam: string);
+var fullScreen, showFPS: boolean;
+begin
+    fullScreen:= fullScreenParam = '1';
+    showFPS:= showFPSParam = '1';
+    setOtherOptions(languageFileParam,fullScreen);
+    setShowFPS(showFPS);
+end;
+
+procedure setAudio(initialVolume: LongInt; musicEnabled: boolean; soundEnabled: boolean);
+begin
+    cInitVolume:= initialVolume;
+    isMusicEnabled:= musicEnabled;
+    isSoundEnabled:= soundEnabled; 
+end;
+
+procedure setAudioWithParameters(initialVolumeParam: string; musicEnabledParam: string; soundEnabledParam: string);
+var initialVolumeAsInt: LongInt;
+    musicEnabled, soundEnabled: boolean;
+begin
+    val(initialVolumeParam, initialVolumeAsInt);
+    musicEnabled:= musicEnabledParam = '1';
+    soundEnabled:= soundEnabledParam = '1';
+    setAudio(initialVolumeAsInt,musicEnabled, soundEnabled);
+end;
+
+procedure setMultimediaOptionsWithParameters(screenWidthParam, screenHeightParam, bitsParam: string;
+                                             initialVolumeParam, musicEnabledParam, soundEnabledParam: string;
+                                             languageFileParam, fullScreenParam: string);
+begin
+    setVideoWithParameters(screenWidthParam,screenHeightParam, bitsParam);
+    setAudioWithParameters(initialVolumeParam,musicEnabledParam,soundEnabledParam);
+    setOtherOptions(languageFileParam,fullScreenParam = '1');
+end;
+
+procedure setAltDamageTimerValueAndQuality(altDamage: boolean; timeIterval: LongInt; reducedQuality: boolean);
+begin
+    cAltDamage:= altDamage;
+    cTimerInterval:= timeIterval;
+    if (reducedQuality) then        //HACK
+        cReducedQuality:= $FFFFFFFF xor rqLowRes
+end;
+
+procedure setAllOptionsWithParameters(screenWidthParam:string; screenHeightParam:string; bitsParam:string;
+                                      initialVolumeParam:string; musicEnabledParam:string; soundEnabledParam:string;
+                                      languageFileParam:string; fullScreenParam:string; showFPSParam:string; 
+                                      altDamageParam:string; timeItervalParam:string; reducedQualityParam: string);
+var showFPS, altDamage, reducedQuality: boolean;
+    timeIterval: LongInt;
+begin
+    setMultimediaOptionsWithParameters(screenWidthParam,screenHeightParam, bitsParam,
+                                       initialVolumeParam,musicEnabledParam,soundEnabledParam,
+                                       languageFileParam,fullScreenParam);                       
+    showFPS := showFPSParam = '1';
+    setShowFPS(showFPS);
+    
+    altDamage:= altDamageParam = '1';
+    val(timeItervalParam, timeIterval);
+    reducedQuality:= reducedQualityParam = '1';
+    setAltDamageTimerValueAndQuality(altDamage,timeIterval,reducedQuality);    
+end;
+
+procedure playReplayFileWithParameters();
+var paramIndex: LongInt;
+    wrongParameter: boolean;
+begin
+    PathPrefix:= ParamStr(1);
+    recordFileName:= ParamStr(2);
+    paramIndex:= 3;
+    wrongParameter:= false;
+    while (paramIndex <= ParamCount) and not wrongParameter do
+    begin
+        //--set-video [screen width] [screen height] [color dept]
+        if(ParamStr(paramIndex) = '--set-video') then
+        begin
+	    if(ParamCount-paramIndex < 3) then
+	    begin
+	        wrongParameter:= true;
+	        GameType:= gmtSyntax;
+	    end;
+	    setVideoWithParameters(ParamStr(paramIndex+1), ParamStr(paramIndex+2), ParamStr(paramIndex+3));
+	    paramIndex:= paramIndex + 4;
+        end 
+        else 
+        //--set-audio [volume] [enable music] [enable sounds]
+        if(ParamStr(paramIndex) = '--set-audio') then
+        begin
+    	    if(ParamCount-paramIndex < 3) then
+	    begin
+	        wrongParameter := true;
+	        GameType:= gmtSyntax;
+	    end;
+	    setAudioWithParameters(ParamStr(paramIndex+1),ParamStr(paramIndex+2), ParamStr(paramIndex+3));
+	    paramIndex:= paramIndex + 4;
+        end 
+        else 
+        // --set-other [language file] [full screen] [show FPS]
+        if(ParamStr(paramIndex) = '--set-other') then
+        begin
+            if(ParamCount-paramIndex < 3) then
+            begin
+                wrongParameter:= true;
+                GameType:= gmtSyntax;
+	    end;
+	    setOtherOptionsWithParameters(ParamStr(paramIndex+1),ParamStr(paramIndex+2), ParamStr(paramIndex+3));
+	    paramIndex:= paramIndex + 4;
+	end 
+	else 
+	//--set-multimedia [screen width] [screen height] [color dept] [volume] [enable music] [enable sounds] [language file] [full screen]
+	if(ParamStr(paramIndex) = '--set-multimedia') then
+	begin
+	    if(ParamCount-paramIndex < 8) then
+	    begin
+	        wrongParameter:= true;
+	        GameType:= gmtSyntax;
+	    end;
+	    setMultimediaOptionsWithParameters(ParamStr(paramIndex+1),ParamStr(paramIndex+2),ParamStr(paramIndex+3),
+		   			       ParamStr(paramIndex+4),ParamStr(paramIndex+5),ParamStr(paramIndex+6),
+					       ParamStr(paramIndex+7),ParamStr(paramIndex+8));
+	    paramIndex:= paramIndex + 9;
+        end 
+        else
+        //--set-everything [screen width] [screen height] [color dept] [volume] [enable music] [enable sounds] [language file] [full screen] [show FPS] [alternate damage] [timer value] [reduced quality]
+        if(ParamStr(paramIndex) = '--set-everything') then
+        begin
+	    if(ParamCount-paramIndex < 12) then
+	    begin
+	        wrongParameter:= true;
+	        GameType:= gmtSyntax;
+	    end;
+	    setAllOptionsWithParameters(ParamStr(paramIndex+1),ParamStr(paramIndex+2),ParamStr(paramIndex+3),
+				        ParamStr(paramIndex+4),ParamStr(paramIndex+5),ParamStr(paramIndex+6),
+				        ParamStr(paramIndex+7),ParamStr(paramIndex+8),ParamStr(paramIndex+9),
+				        ParamStr(paramIndex+10),ParamStr(paramIndex+11),ParamStr(paramIndex+12));
+	    paramIndex:= paramIndex + 13;
+        end 
+        else
+        begin
+	    wrongParameter:= true;
+	    GameType:= gmtSyntax;
+        end;
+    end;
+end;
+
--- a/hedgewars/hwengine.pas	Sat Jul 31 07:05:06 2010 +0200
+++ b/hedgewars/hwengine.pas	Sat Jul 31 08:19:56 2010 +0200
@@ -32,11 +32,12 @@
 uses SDLh, uMisc, uConsole, uGame, uConsts, uLand, uAmmos, uVisualGears, uGears, uStore, uWorld, uKeys, uSound, 
      uScript, uTeams, uStats, uIO, uLocale, uChat, uAI, uAIMisc, uRandom, uLandTexture, uCollisions, sysutils;
      
-type arrayofpchar = array[0..9] of PChar;
 var isTerminated: boolean = false;
     alsoShutdownFrontend: boolean = false;
 
 {$IFDEF HWLIBRARY}
+type arrayofpchar = array[0..9] of PChar;
+
 procedure initEverything(complete:boolean);
 procedure freeEverything(complete:boolean);
 
@@ -417,140 +418,38 @@
 begin
     WriteLn('Wrong argument format: correct configurations is');
     WriteLn();
-    WriteLn('  hwengine <path to data folder> <path to replay file> [option]');
+    WriteLn('  hwengine <path to data folder> <path to replay file> [options]');
     WriteLn();
-    WriteLn('where [option] must be specified either as');
+    WriteLn('where [options] must be specified either as:');
     WriteLn(' --set-video [screen width] [screen height] [color dept]');
     WriteLn(' --set-audio [volume] [enable music] [enable sounds]');
     WriteLn(' --set-other [language file] [full screen] [show FPS]');
-    WriteLn(' --set-multimedia [screen height] [screen width] [color dept] [volume] [enable music] [enable sounds] [language file] [full screen]');
-    WriteLn(' --set-everything [screen height] [screen width] [color dept] [volume] [enable music] [enable sounds] [language file] [full screen] [show FPS] [alternate damage] [timer value] [reduced quality]');
+    WriteLn(' --set-multimedia [screen width] [screen height] [color dept] [volume] [enable music] [enable sounds] [language file] [full screen]');
+    WriteLn(' --set-everything [screen width] [screen height] [color dept] [volume] [enable music] [enable sounds] [language file] [full screen] [show FPS] [alternate damage] [timer value] [reduced quality]');
     WriteLn();
-    WriteLn('Read documentation online at http://www.hedgewars.org/node/1465 for more information');
-    Write('parsed command: ');
+    WriteLn('Read documentation online at http://code.google.com/p/hedgewars/wiki/CommandLineOptions for more information');
+    WriteLn();
+    Write('PARSED COMMAND: ');
     for i:=0 to ParamCount do
         Write(ParamStr(i) + ' ');
     WriteLn();
 end;
 
 ////////////////////
+{$INCLUDE "ArgParsers.inc"}
+
 procedure GetParams;
 begin
-    case ParamCount of
-        18: begin
-            val(ParamStr(2), cScreenWidth);
-            val(ParamStr(3), cScreenHeight);
-            cBitsStr:= ParamStr(4);
-            val(cBitsStr, cBits);
-            val(ParamStr(5), ipcPort);
-            cFullScreen:= ParamStr(6) = '1';
-            isSoundEnabled:= ParamStr(7) = '1';
-            //cVSyncInUse:= ParamStr(8) = '1';      //merged with rqFlags
-            //cWeaponTooltips:= ParamStr(9) = '1';  //merged with rqFlags
-            cLocaleFName:= ParamStr(10);
-            val(ParamStr(11), cInitVolume);
-            val(ParamStr(12), cTimerInterval);
-            PathPrefix:= ParamStr(13);
-            cShowFPS:= ParamStr(14) = '1';
-            cAltDamage:= ParamStr(15) = '1';
-            UserNick:= DecodeBase64(ParamStr(16));
-            isMusicEnabled:= ParamStr(17) = '1';
-
-            if (ParamStr(18) = '1') then        //HACK - always disable rqLowRes as it's a game breaker
-                cReducedQuality:= $FFFFFFFF xor rqLowRes
-            else
-                val(ParamStr(18), cReducedQuality);
-            
-            if (ParamStr(8) = '0') then         //HACK - ifcVSyncInUse not true, disable it
-                cReducedQuality:= cReducedQuality xor rqDesyncVBlank;
-            if (ParamStr(9) = '0') then         //HACK - if cWeaponTooltips not true, disable it
-                cReducedQuality:= cReducedQuality xor rqTooltipsOff;
-        end;
-        3: begin
-            val(ParamStr(2), ipcPort);
-            GameType:= gmtLandPreview;
-            if ParamStr(3) <> 'landpreview' then 
-                OutError(errmsgShouldntRun, true);
-        end;
-        2: begin
-            PathPrefix:= ParamStr(1);
-            recordFileName:= ParamStr(2);
-        end;
-        6: begin
-            PathPrefix:= ParamStr(1);
-            recordFileName:= ParamStr(2);
-
-            if ParamStr(3) = '--set-video'  then
-            begin
-                val(ParamStr(4), cScreenWidth);
-                val(ParamStr(5), cScreenHeight);
-                cBitsStr:= ParamStr(6);
-                val(cBitsStr, cBits);
-            end
+    if (ParamCount < 2) then
+        GameType:= gmtSyntax
+    else
+        if (ParamCount = 3) then
+            internalSetGameTypeLandPreviewFromParameters()
+        else
+            if (ParamCount = 18) then
+                internalStartGameWithParameters()
             else
-            begin
-                if ParamStr(3) = '--set-audio' then
-                begin
-                    val(ParamStr(4), cInitVolume);
-                    isMusicEnabled:= ParamStr(5) = '1';
-                    isSoundEnabled:= ParamStr(6) = '1';
-                end
-                else
-                begin
-                    if ParamStr(3) = '--set-other' then
-                    begin
-                        cLocaleFName:= ParamStr(4);
-                        cFullScreen:= ParamStr(5) = '1';
-                        cShowFPS:= ParamStr(6) = '1';
-                    end
-                    else GameType:= gmtSyntax;
-                end
-            end;
-        end;
-        11: begin
-            PathPrefix:= ParamStr(1);
-            recordFileName:= ParamStr(2);
-
-            if ParamStr(3) = '--set-multimedia' then
-            begin
-                val(ParamStr(4), cScreenWidth);
-                val(ParamStr(5), cScreenHeight);
-                cBitsStr:= ParamStr(6);
-                val(cBitsStr, cBits);
-                val(ParamStr(7), cInitVolume);
-                isMusicEnabled:= ParamStr(8) = '1';
-                isSoundEnabled:= ParamStr(9) = '1';
-                cLocaleFName:= ParamStr(10);
-                cFullScreen:= ParamStr(11) = '1';
-            end
-            else GameType:= gmtSyntax;
-        end;
-        15: begin
-            PathPrefix:= ParamStr(1);
-            recordFileName:= ParamStr(2);
-            if ParamStr(3) = '--set-everything' then
-            begin
-                val(ParamStr(4), cScreenWidth);
-                val(ParamStr(5), cScreenHeight);
-                cBitsStr:= ParamStr(6);
-                val(cBitsStr, cBits);
-                val(ParamStr(7), cInitVolume);
-                isMusicEnabled:= ParamStr(8) = '1';
-                isSoundEnabled:= ParamStr(9) = '1';
-                cLocaleFName:= ParamStr(10);
-                cFullScreen:= ParamStr(11) = '1';
-                cAltDamage:= ParamStr(12) = '1';
-                cShowFPS:= ParamStr(13) = '1';
-                val(ParamStr(14), cTimerInterval);
-                if (ParamStr(15) = '1') then        //HACK
-                    cReducedQuality:= $FFFFFFFF xor rqLowRes
-                else
-                    val(ParamStr(15), cReducedQuality);
-            end
-            else GameType:= gmtSyntax;
-        end;
-        else GameType:= gmtSyntax;
-    end;
+                playReplayFileWithParameters();
 end;
 
 ////////////////////////////////////////////////////////////////////////////////