hedgewars/ArgParsers.inc
changeset 8160 f837447298c3
parent 8150 6b30a4cd7c7c
child 8213 81553c5b9fd0
equal deleted inserted replaced
8159:e226be871b6e 8160:f837447298c3
    62     cVideoQuality:= StrToInt(ParamStr(23));
    62     cVideoQuality:= StrToInt(ParamStr(23));
    63     cAudioCodec:= ParamStr(24);
    63     cAudioCodec:= ParamStr(24);
    64 end;
    64 end;
    65 {$ENDIF}
    65 {$ENDIF}
    66 
    66 
    67 procedure DisplayUsage;
    67 procedure setVideo(screenWidth: LongInt; screenHeight: LongInt; bitsStr: LongInt);
    68 begin
    68 begin
    69     WriteLn(stdout, 'Usage:');
    69     cScreenWidth:= screenWidth;
    70     WriteLn(stdout, '');
    70     cScreenHeight:= screenHeight;
    71     WriteLn(stdout, '  hwengine <path to user hedgewars folder> <path to global data folder> <path to replay file> [options]');
    71     cBits:= bitsStr
    72     WriteLn(stdout, '');
    72 end;
    73     WriteLn(stdout, 'where [options] are any of the following:');
    73 
    74     WriteLn(stdout, ' --locale [path to language file]');
    74 procedure setVideoWithParameters(screenWidthParam: string; screenHeightParam: string; bitsParam: string);
    75     WriteLn(stdout, ' --width  [screen width in pixels]');
    75 var screenWidthAsInt, screenHeightAsInt, bitsStrAsInt, c: LongInt;
    76     WriteLn(stdout, ' --height [screen height in pixels]');
    76 begin
    77     WriteLn(stdout, ' --depth  [color depth]');
    77     val(screenWidthParam, screenWidthAsInt, c);
    78     WriteLn(stdout, ' --volume [sound level]');
    78     val(screenHeightParam, screenHeightAsInt, c);
    79     WriteLn(stdout, ' --time   [number of seconds]');
    79     val(bitsParam, bitsStrAsInt, c);
    80     WriteLn(stdout, ' --nomusic');
    80     setVideo(screenWidthAsInt,screenHeightAsInt,bitsStrAsInt)
    81     WriteLn(stdout, ' --nosound');
    81 end;
    82     WriteLn(stdout, ' --fullscreen');
    82 
    83     WriteLn(stdout, ' --showfps');
    83 procedure setOtherOptions(languageFile: string; fullScreen: boolean);
    84     WriteLn(stdout, ' --altdmg');
    84 begin
    85     WriteLn(stdout, ' --lowquality');
    85     cLocaleFName:= languageFile;
    86     WriteLn(stdout, ' --stats-only');
    86     cFullScreen:= fullScreen
    87     WriteLn(stdout, ' --help');
    87 end;
    88     WriteLn(stdout, '');
    88 
    89     WriteLn(stdout, 'Deprecated options:');
    89 procedure setShowFPS(showFPS: boolean);
    90     WriteLn(stdout, ' --set-video [screen width] [screen height] [color dept]');
    90 begin
    91     WriteLn(stdout, ' --set-audio [volume] [enable music] [enable sounds]');
    91     cShowFPS:= showFPS
    92     WriteLn(stdout, ' --set-other [language file] [full screen] [show FPS]');
    92 end;
    93     WriteLn(stdout, ' --set-multimedia [screen width] [screen height] [color dept] [volume] [enable music] [enable sounds] [language file] [full screen]');
    93 
    94     WriteLn(stdout, ' --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]');
    94 procedure setOtherOptionsWithParameters(languageFileParam: string; fullScreenParam: string; showFPSParam: string);
    95     WriteLn(stdout, '');
    95 var fullScreen, showFPS: boolean;
    96     WriteLn(stdout, 'For a more detailed help and examples go to:');
    96 begin
    97     WriteLn(stdout, 'http://code.google.com/p/hedgewars/wiki/CommandLineOptions');
    97     fullScreen:= fullScreenParam = '1';
    98 end;
    98     showFPS:= showFPSParam = '1';
    99 
    99     setOtherOptions(languageFileParam,fullScreen);
   100 function getLongIntParameter(str:String; var paramIndex:LongInt; var wrongParameter:Boolean): LongInt;
   100     setShowFPS(showFPS)
   101 var tmpInt, c: LongInt;
   101 end;
   102 begin
   102 
   103     paramIndex:= paramIndex + 1;
   103 procedure setAudio(initialVolume: LongInt; musicEnabled: boolean; soundEnabled: boolean);
   104     val(str, tmpInt, c);
   104 begin
   105     wrongParameter:= c <> 0;
   105     SetVolume(initialVolume);
   106     if wrongParameter then
   106     SetMusic(musicEnabled);
   107         WriteLn(stderr, 'ERROR: '+ParamStr(paramIndex-1)+' expects a number, you passed "'+str+'"');
   107     SetSound(soundEnabled);
   108     getLongIntParameter:= tmpInt;
   108 end;
   109 end;
   109 
   110 
   110 procedure setAudioWithParameters(initialVolumeParam: string; musicEnabledParam: string; soundEnabledParam: string);
   111 function getStringParameter(str:String; var paramIndex:LongInt): String;
   111 var initialVolumeAsInt, c: LongInt;
   112 begin
   112     musicEnabled, soundEnabled: boolean;
   113     paramIndex:= paramIndex + 1;
   113 begin
   114     getStringParameter:= str;
   114     val(initialVolumeParam, initialVolumeAsInt, c);
   115 end;
   115     musicEnabled:= musicEnabledParam = '1';
   116 
   116     soundEnabled:= soundEnabledParam = '1';
   117 procedure parseClassicParameter(cmdArray: Array of String; size:LongInt; var paramIndex:LongInt); Forward;
   117     setAudio(initialVolumeAsInt,musicEnabled, soundEnabled)
   118 
   118 end;
   119 function parseParameter(cmd:String; arg:String; var paramIndex:LongInt): Boolean;
   119 
   120 const videoArray: Array [1..3] of String = ('--width','--height','--depth');
   120 procedure setMultimediaOptionsWithParameters(screenWidthParam, screenHeightParam, bitsParam: string;
   121 const audioArray: Array [1..3] of String = ('--volume','--nomusic','--nosound');
   121                                              initialVolumeParam, musicEnabledParam, soundEnabledParam: string;
   122 const otherArray: Array [1..3] of String = ('--locale','--fullscreen','--showfps');
   122                                              languageFileParam, fullScreenParam: string);
   123 const mediaArray: Array [1..8] of String = ('--width','--height','--depth','--volume','--nomusic','--nosound','--locale','--fullscreen');
   123 begin
   124 const allArray: Array [1..12] of String = ('--width','--height','--depth','--volume','--nomusic','--nosound','--locale','--fullscreen','--showfps','--altdmg','--time','--lowquality');
   124     setVideoWithParameters(screenWidthParam,screenHeightParam, bitsParam);
   125 begin
   125     setAudioWithParameters(initialVolumeParam,musicEnabledParam,soundEnabledParam);
   126     parseParameter:= false;
   126     setOtherOptions(languageFileParam,fullScreenParam = '1')
   127     case cmd of
   127 end;
   128         '--locale'     : cLocaleFName   := getStringParameter (arg, paramIndex);
   128 
   129         '--width'      : cScreenWidth   := getLongIntParameter(arg, paramIndex, parseParameter);
   129 procedure setAltDamageTimerValueAndQuality(altDamage: boolean; timeIterval: LongInt; reducedQuality: boolean);
   130         '--height'     : cScreenHeight  := getLongIntParameter(arg, paramIndex, parseParameter);
   130 begin
   131         '--depth'      : cBits          := getLongIntParameter(arg, paramIndex, parseParameter);
   131     cAltDamage:= altDamage;
   132         '--time'       : cTimerInterval := getLongIntParameter(arg, paramIndex, parseParameter);
   132     cTimerInterval:= timeIterval;
   133         '--volume'     : SetVolume       ( getLongIntParameter(arg, paramIndex, parseParameter) );
   133     if (reducedQuality) then        //HACK
   134         '--nomusic'    : SetMusic        ( false );
   134         cReducedQuality:= $FFFFFFFF xor rqLowRes
   135         '--nosound'    : SetSound        ( false );
   135 end;
   136         '--fullscreen' : cFullScreen    := true;
   136 
   137         '--showfps'    : cShowFPS       := true;
   137 procedure setAllOptionsWithParameters(screenWidthParam:string; screenHeightParam:string; bitsParam:string;
   138         '--altdmg'     : cAltDamage     := true;
   138                                       initialVolumeParam:string; musicEnabledParam:string; soundEnabledParam:string;
   139         '--lowquality' : cReducedQuality:= ($FFFFFFFF * getLongIntParameter(arg, paramIndex, parseParameter)) xor rqLowRes; //HACK!
   139                                       languageFileParam:string; fullScreenParam:string; showFPSParam:string;
   140         '--set-video'      : parseClassicParameter(videoArray,3,paramIndex);
   140                                       altDamageParam:string; timeItervalParam:string; reducedQualityParam: string);
   141         '--set-audio'      : parseClassicParameter(audioArray,3,paramIndex);
   141 var showFPS, altDamage, reducedQuality: boolean;
   142         '--set-other'      : parseClassicParameter(otherArray,3,paramIndex);
   142     timeIterval, c: LongInt;
   143         '--set-multimedia' : parseClassicParameter(mediaArray,8,paramIndex);
   143 begin
   144         '--set-everything' : parseClassicParameter(allArray,12,paramIndex);
   144     setMultimediaOptionsWithParameters(screenWidthParam,screenHeightParam, bitsParam,
   145         '--stats-only' : begin
   145                                        initialVolumeParam,musicEnabledParam,soundEnabledParam,
   146                          cOnlyStats:= true;
   146                                        languageFileParam,fullScreenParam);
   147                          SetSound(false);
   147     showFPS := showFPSParam = '1';
   148                          SetMusic(false);
   148     setShowFPS(showFPS);
   149                          cReducedQuality:= $FFFFFFFF xor rqLowRes;
   149 
   150                          end;
   150     altDamage:= altDamageParam = '1';
   151         '--gci' : begin            //     We had to make up all this saved space some how...     \\
   151     val(timeItervalParam, timeIterval, c);
   152                   WriteLn(stdout, '                                                                ');
   152     reducedQuality:= reducedQualityParam = '1';
   153                   WriteLn(stdout, '      /\\\\\\\\\\\\        /\\\\\\\\\  /\\\\\\\\\\\             ');
   153     setAltDamageTimerValueAndQuality(altDamage,timeIterval,reducedQuality);
   154                   WriteLn(stdout, '     /\\\//////////      /\\\////////  \/////\\\///             ');
   154 end;
   155                   WriteLn(stdout, '     /\\\               /\\\/               \/\\\               ');
   155 
   156                   WriteLn(stdout, '     \/\\\    /\\\\\\\  /\\\                 \/\\\              ');
   156 procedure playReplayFileWithParameters(paramIndex : LongInt);
   157                   WriteLn(stdout, '      \/\\\   \/////\\\ \/\\\                 \/\\\             ');
   157 var wrongParameter: boolean;
   158                   WriteLn(stdout, '       \/\\\       \/\\\ \//\\\                \/\\\            ');
   158 begin
   159                   WriteLn(stdout, '        \/\\\       \/\\\  \///\\\              \/\\\           ');
   159     WriteLn(stdout,IntToStr(paramIndex));
   160                   WriteLn(stdout, '         \//\\\\\\\\\\\\/     \////\\\\\\\\\  /\\\\\\\\\\\      ');
   160     wrongParameter:= false;
   161                   WriteLn(stdout, '          \////////////           \/////////  \///////////      ');
   161     while (paramIndex <= ParamCount) and (not wrongParameter) do
   162                   WriteLn(stdout, '                                                                ');
       
   163                   WriteLn(stdout, ' Command Line Parser Implementation by a Google Code-In Student ');
       
   164                   WriteLn(stdout, '             ASCII Art easter egg idea by @sheepluva            ');
       
   165                   WriteLn(stdout, '                                                                ');
       
   166                   end;
       
   167         '--help' : begin
       
   168                    DisplayUsage();
       
   169                    GameType:= gmtSyntax;
       
   170                    end;
       
   171     else
       
   172         begin
   162         begin
   173         WriteLn(stderr, 'ERROR: '+cmd+' is not a valid argument');
   163         if ParamStr(paramIndex) = '--set-video'  then
   174         parseParameter:= true;
   164 //--set-video [screen width] [screen height] [color dept]
   175         end
   165             begin
   176     end;
   166             if(ParamCount-paramIndex < 3) then
   177 end;
   167                 begin
   178 
   168                 wrongParameter:= true;
   179 procedure parseClassicParameter(cmdArray: Array of String; size:LongInt; var paramIndex:LongInt);
   169                 GameType:= gmtSyntax
   180 var index, tmpInt: LongInt;
   170                 end;
   181     isBool: Boolean;
   171             setVideoWithParameters(ParamStr(paramIndex+1), ParamStr(paramIndex+2), ParamStr(paramIndex+3));
   182 begin
   172             paramIndex:= paramIndex + 4
   183     index:= 0;
   173             end
   184     tmpInt:= 1;
   174         else
   185     while (index < size) do
   175 //--set-audio [volume] [enable music] [enable sounds]
   186         begin
   176             if ParamStr(paramIndex) = '--set-audio'  then
   187         paramIndex:= paramIndex+1;
   177                 begin
   188         //This next line is a really strange (but short), way to check if the parameter is a boolean one
   178                 if(ParamCount-paramIndex < 3) then
   189         isBool:= true; case cmdArray[index] of '--nomusic':;'--nosound':;'--fullscreen':;'--showfps':;'--altdmg':;'--lowquality':; else isBool:= false; end;
   179                     begin
   190         if (not isBool) or ((ParamStr(paramIndex)='1') and (cmdArray[index]<>'--nomusic') and (cmdArray[index]<>'--nosound')) then
   180                     wrongParameter := true;
   191             parseParameter(cmdArray[index], ParamStr(paramIndex), tmpInt);
   181                     GameType:= gmtSyntax
   192         //if isBool then
   182                     end;
   193         //  paramIndex:= paramIndex+1;
   183                 setAudioWithParameters(ParamStr(paramIndex+1),ParamStr(paramIndex+2), ParamStr(paramIndex+3));
   194         index:= index+1;
   184                 paramIndex:= paramIndex + 4
   195         end;
   185                 end
   196 end;
   186             else
   197 
   187 // --set-other [language file] [full screen] [show FPS]
   198 procedure playReplayFileWithParameters();
   188                 if ParamStr(paramIndex) = '--set-other'  then
   199 var paramIndex, tmpInt: LongInt;
   189                     begin
   200     wrongParameter: boolean;
   190                     if(ParamCount-paramIndex < 3) then
   201 begin
   191                         begin
   202     UserPathPrefix:= ParamStr(1);
   192                         wrongParameter:= true;
   203     PathPrefix:= ParamStr(2);
   193                         GameType:= gmtSyntax
   204     recordFileName:= ParamStr(3);
   194                         end;
   205     paramIndex:= 4;
   195                     setOtherOptionsWithParameters(ParamStr(paramIndex+1),ParamStr(paramIndex+2), ParamStr(paramIndex+3));
   206     wrongParameter:= false;
   196                     paramIndex:= paramIndex + 4
   207     while (paramIndex <= ParamCount) do
   197                     end
   208         begin
   198                 else
   209         if parseParameter( ParamStr(paramIndex), ParamStr(paramIndex+1), paramIndex) then
   199 //--set-multimedia [screen width] [screen height] [color dept] [volume] [enable music] [enable sounds] [language file] [full screen]
   210             wrongParameter:= true;
   200                     if ParamStr(paramIndex) = '--set-multimedia'  then
   211         paramIndex:= paramIndex+1;
   201                         begin
   212         end;
   202                         if ParamCount-paramIndex < 8  then
   213     if wrongParameter = true then
   203                             begin
   214         begin
   204                             wrongParameter:= true;
   215         WriteLn(stderr, 'Please use --help to see possible arguments and their usage');
   205                             GameType:= gmtSyntax
   216         GameType:= gmtSyntax;
   206                             end;
   217         end
   207                         setMultimediaOptionsWithParameters(ParamStr(paramIndex+1),ParamStr(paramIndex+2),ParamStr(paramIndex+3),
   218 end;
   208                                                            ParamStr(paramIndex+4),ParamStr(paramIndex+5),ParamStr(paramIndex+6),
       
   209                                                            ParamStr(paramIndex+7),ParamStr(paramIndex+8));
       
   210                         paramIndex:= paramIndex + 9
       
   211                         end
       
   212                     else
       
   213 //--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]
       
   214                         if ParamStr(paramIndex) = '--set-everything'  then
       
   215                             begin
       
   216                             if ParamCount-paramIndex < 12  then
       
   217                                 begin
       
   218                                 wrongParameter:= true;
       
   219                                 GameType:= gmtSyntax
       
   220                                 end;
       
   221                             setAllOptionsWithParameters(ParamStr(paramIndex+1),ParamStr(paramIndex+2),ParamStr(paramIndex+3),
       
   222                                                         ParamStr(paramIndex+4),ParamStr(paramIndex+5),ParamStr(paramIndex+6),
       
   223                                                         ParamStr(paramIndex+7),ParamStr(paramIndex+8),ParamStr(paramIndex+9),
       
   224                                                         ParamStr(paramIndex+10),ParamStr(paramIndex+11),ParamStr(paramIndex+12));
       
   225                             paramIndex:= paramIndex + 13
       
   226                             end
       
   227                         else
       
   228                             if ParamStr(paramIndex) = '--stats-only'  then
       
   229                                 begin
       
   230                                 cOnlyStats:= true;
       
   231                                 SetSound(false);
       
   232                                 SetMusic(false);
       
   233                                 cReducedQuality:= $FFFFFFFF xor rqLowRes; // HACK
       
   234                                 paramIndex:= paramIndex + 1
       
   235                                 end
       
   236                             else
       
   237                                 begin
       
   238                                 wrongParameter:= true;
       
   239                                 GameType:= gmtSyntax
       
   240                                 end
       
   241     end
       
   242 end;
       
   243