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