~ubuntu-branches/ubuntu/utopic/hedgewars/utopic

« back to all changes in this revision

Viewing changes to hedgewars/ArgParsers.inc

  • Committer: Package Import Robot
  • Author(s): Gianfranco Costamagna
  • Date: 2014-01-02 12:37:23 UTC
  • mfrom: (19.1.5 sid)
  • Revision ID: package-import@ubuntu.com-20140102123723-6pdhmyj8tb5y8xbg
Tags: 0.9.20.3-1
New upstream minor release, suitable for unstable

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
(*
2
 
 * Hedgewars, a free turn based strategy game
3
 
 * Copyright (c) 2004-2013 Andrey Korotaev <unC0Rr@gmail.com>
4
 
 *
5
 
 * This program is free software; you can redistribute it and/or modify
6
 
 * it under the terms of the GNU General Public License as published by
7
 
 * the Free Software Foundation; version 2 of the License
8
 
 *
9
 
 * This program is distributed in the hope that it will be useful,
10
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
 * GNU General Public License for more details.
13
 
 *
14
 
 * You should have received a copy of the GNU General Public License
15
 
 * along with this program; if not, write to the Free Software
16
 
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
17
 
 *)
18
 
 
19
 
procedure GciEasterEgg;
20
 
begin
21
 
    WriteLn(stdout, '                                                                ');
22
 
    WriteLn(stdout, '      /\\\\\\\\\\\\        /\\\\\\\\\  /\\\\\\\\\\\             ');
23
 
    WriteLn(stdout, '     /\\\//////////      /\\\////////  \/////\\\///             ');
24
 
    WriteLn(stdout, '     /\\\               /\\\/               \/\\\               ');
25
 
    WriteLn(stdout, '     \/\\\    /\\\\\\\  /\\\                 \/\\\              ');
26
 
    WriteLn(stdout, '      \/\\\   \/////\\\ \/\\\                 \/\\\             ');
27
 
    WriteLn(stdout, '       \/\\\       \/\\\ \//\\\                \/\\\            ');
28
 
    WriteLn(stdout, '        \/\\\       \/\\\  \///\\\              \/\\\           ');
29
 
    WriteLn(stdout, '         \/\\\\\\\\\\\\\/     \////\\\\\\\\\  /\\\\\\\\\\\      ');
30
 
    WriteLn(stdout, '          \/////////////          \/////////  \///////////      ');
31
 
    WriteLn(stdout, '                                                                ');
32
 
    WriteLn(stdout, ' Command Line Parser Implementation by a Google Code-In Student ');
33
 
    WriteLn(stdout, '             ASCII Art easter egg idea by @sheepluva            ');
34
 
    WriteLn(stdout, '                                                                ');
35
 
end;
36
 
 
37
 
procedure DisplayUsage;
38
 
begin
39
 
    WriteLn(stdout, 'Usage: hwengine <path to replay file> [options]');
40
 
    WriteLn(stdout, '');
41
 
    WriteLn(stdout, 'where [options] can be any of the following:');
42
 
    WriteLn(stdout, ' --prefix [path to folder]');
43
 
    WriteLn(stdout, ' --user-prefix [path to folder]');
44
 
    WriteLn(stdout, ' --locale [name of language file]');
45
 
    WriteLn(stdout, ' --nick [string]');
46
 
    WriteLn(stdout, ' --fullscreen-width [fullscreen width in pixels]');
47
 
    WriteLn(stdout, ' --fullscreen-height [fullscreen height in pixels]');
48
 
    WriteLn(stdout, ' --width [window width in pixels]');
49
 
    WriteLn(stdout, ' --height [window height in pixels]');
50
 
    WriteLn(stdout, ' --volume [sound level]');
51
 
    WriteLn(stdout, ' --frame-interval [milliseconds]');
52
 
    Writeln(stdout, ' --stereo [value]');
53
 
    WriteLn(stdout, ' --raw-quality [flags]');
54
 
    WriteLn(stdout, ' --low-quality');
55
 
    WriteLn(stdout, ' --nomusic');
56
 
    WriteLn(stdout, ' --nosound');
57
 
    WriteLn(stdout, ' --fullscreen');
58
 
    WriteLn(stdout, ' --showfps');
59
 
    WriteLn(stdout, ' --altdmg');
60
 
    WriteLn(stdout, ' --stats-only');
61
 
    WriteLn(stdout, ' --help');
62
 
    WriteLn(stdout, '');
63
 
    WriteLn(stdout, 'For more detailed help and examples go to:');
64
 
    WriteLn(stdout, 'http://code.google.com/p/hedgewars/wiki/CommandLineOptions');
65
 
    GameType:= gmtSyntax;
66
 
end;
67
 
 
68
 
procedure setDepth(var paramIndex: LongInt);
69
 
begin
70
 
    WriteLn(stdout, 'WARNING: --depth is a deprecated command, which could be removed in a future version!');
71
 
    WriteLn(stdout, '         This option no longer does anything, please consider removing it');
72
 
    WriteLn(stdout, '');
73
 
   inc(ParamIndex);
74
 
end;
75
 
 
76
 
procedure statsOnlyGame;
77
 
begin
78
 
    cOnlyStats:= true;
79
 
    cReducedQuality:= $FFFFFFFF xor rqLowRes;
80
 
    SetSound(false);
81
 
    SetMusic(false);
82
 
    SetVolume(0);
83
 
end;
84
 
 
85
 
procedure setIpcPort(port: LongInt; var wrongParameter:Boolean);
86
 
begin
87
 
    if isInternal then
88
 
        ipcPort := port
89
 
    else
90
 
        begin
91
 
        WriteLn(stderr, 'ERROR: use of --port is not allowed');
92
 
        wrongParameter := true;
93
 
        end
94
 
end;
95
 
 
96
 
function parseNick(nick: String): String;
97
 
begin
98
 
    if isInternal then
99
 
        parseNick:= DecodeBase64(nick)
100
 
    else
101
 
        parseNick:= nick;
102
 
end;
103
 
 
104
 
procedure setStereoMode(tmp: LongInt);
105
 
begin
106
 
    GrayScale:= false;
107
 
{$IFDEF USE_S3D_RENDERING}
108
 
    if (tmp > 6) and (tmp < 13) then
109
 
        begin
110
 
        // set the gray anaglyph rendering
111
 
        GrayScale:= true;
112
 
        cStereoMode:= TStereoMode(max(0, min(ord(high(TStereoMode)), tmp-6)))
113
 
        end
114
 
    else if tmp <= 6 then
115
 
        // set the fullcolor anaglyph
116
 
        cStereoMode:= TStereoMode(max(0, min(ord(high(TStereoMode)), tmp)))
117
 
    else
118
 
        // any other mode
119
 
        cStereoMode:= TStereoMode(max(0, min(ord(high(TStereoMode)), tmp-6)));
120
 
{$ELSE}
121
 
    tmp:= tmp;
122
 
    cStereoMode:= smNone;
123
 
{$ENDIF}
124
 
end;
125
 
 
126
 
procedure startVideoRecording(var paramIndex: LongInt);
127
 
begin
128
 
    // Silence the hint that appears when USE_VIDEO_RECORDING is not defined
129
 
    paramIndex:= paramIndex;
130
 
{$IFDEF USE_VIDEO_RECORDING}
131
 
    GameType:= gmtRecord;
132
 
    inc(paramIndex);
133
 
    cVideoFramerateNum:= StrToInt(ParamStr(paramIndex)); inc(paramIndex);
134
 
    cVideoFramerateDen:= StrToInt(ParamStr(paramIndex)); inc(paramIndex);
135
 
    RecPrefix:= ParamStr(paramIndex);                    inc(paramIndex);
136
 
    cAVFormat:= ParamStr(paramIndex);                    inc(paramIndex);
137
 
    cVideoCodec:= ParamStr(paramIndex);                  inc(paramIndex);
138
 
    cVideoQuality:= StrToInt(ParamStr(paramIndex));      inc(paramIndex);
139
 
    cAudioCodec:= ParamStr(paramIndex);                  inc(paramIndex);
140
 
{$ENDIF}
141
 
end;
142
 
 
143
 
function getLongIntParameter(str:String; var paramIndex:LongInt; var wrongParameter:Boolean): LongInt;
144
 
var tmpInt, c: LongInt;
145
 
begin
146
 
    inc(paramIndex);
147
 
    val(str, tmpInt, c);
148
 
    wrongParameter:= c <> 0;
149
 
    if wrongParameter then
150
 
        WriteLn(stderr, 'ERROR: '+ParamStr(paramIndex-1)+' expects a number, you passed "'+str+'"');
151
 
    getLongIntParameter:= tmpInt;
152
 
end;
153
 
 
154
 
function getStringParameter(str:String; var paramIndex:LongInt; var wrongParameter:Boolean): String;
155
 
begin
156
 
    inc(paramIndex);
157
 
    wrongParameter:= (str='') or (Copy(str,1,2) = '--');
158
 
    if wrongParameter then
159
 
         WriteLn(stderr, 'ERROR: '+ParamStr(paramIndex-1)+' expects a string, you passed "'+str+'"');
160
 
    getStringParameter:= str;
161
 
end;
162
 
 
163
 
 
164
 
procedure parseClassicParameter(cmdArray: Array of String; size:LongInt; var paramIndex:LongInt); Forward;
165
 
 
166
 
function parseParameter(cmd:String; arg:String; var paramIndex:LongInt): Boolean;
167
 
const videoArray: Array [1..5] of String = ('--fullscreen-width','--fullscreen-height', '--width', '--height', '--depth');
168
 
      audioArray: Array [1..3] of String = ('--volume','--nomusic','--nosound');
169
 
      otherArray: Array [1..3] of String = ('--locale','--fullscreen','--showfps');
170
 
      mediaArray: Array [1..10] of String = ('--fullscreen-width', '--fullscreen-height', '--width', '--height', '--depth', '--volume','--nomusic','--nosound','--locale','--fullscreen');
171
 
      allArray: Array [1..14] of String = ('--fullscreen-width','--fullscreen-height', '--width', '--height', '--depth','--volume','--nomusic','--nosound','--locale','--fullscreen','--showfps','--altdmg','--frame-interval','--low-quality');
172
 
      reallyAll: array[0..30] of shortstring = (
173
 
                '--prefix', '--user-prefix', '--locale', '--fullscreen-width', '--fullscreen-height', '--width',
174
 
                '--height', '--frame-interval', '--volume','--nomusic', '--nosound',
175
 
                '--fullscreen', '--showfps', '--altdmg', '--low-quality', '--raw-quality', '--stereo', '--nick',
176
 
  {deprecated}  '--depth', '--set-video', '--set-audio', '--set-other', '--set-multimedia', '--set-everything',
177
 
  {internal}    '--internal', '--port', '--recorder', '--landpreview',
178
 
  {misc}        '--stats-only', '--gci', '--help');
179
 
var cmdIndex: byte;
180
 
begin
181
 
    parseParameter:= false;
182
 
    cmdIndex:= 0;
183
 
 
184
 
    //NOTE: Any update to the list of parameters must be reflected in the case statement below, the reallyAll array above,
185
 
    //      the the DisplayUsage() procedure, the HWForm::getDemoArguments() function, and the online wiki
186
 
 
187
 
    while (cmdIndex <= High(reallyAll)) and (cmd <> reallyAll[cmdIndex]) do inc(cmdIndex);
188
 
    case cmdIndex of
189
 
        {--prefix}               0 : PathPrefix        := getStringParameter (arg, paramIndex, parseParameter);
190
 
        {--user-prefix}          1 : UserPathPrefix    := getStringParameter (arg, paramIndex, parseParameter);
191
 
        {--locale}               2 : cLocaleFName      := getStringParameter (arg, paramIndex, parseParameter);
192
 
        {--fullscreen-width}     3 : cFullscreenWidth  := max(getLongIntParameter(arg, paramIndex, parseParameter), cMinScreenWidth);
193
 
        {--fullscreen-height}    4 : cFullscreenHeight := max(getLongIntParameter(arg, paramIndex, parseParameter), cMinScreenHeight);
194
 
        {--width}                5 : cWindowedWidth    := max(2 * (getLongIntParameter(arg, paramIndex, parseParameter) div 2), cMinScreenWidth);
195
 
        {--height}               6 : cWindowedHeight   := max(2 * (getLongIntParameter(arg, paramIndex, parseParameter) div 2), cMinScreenHeight);
196
 
        {--frame-interval}       7 : cTimerInterval    := getLongIntParameter(arg, paramIndex, parseParameter);
197
 
        {--volume}               8 : SetVolume          ( max(getLongIntParameter(arg, paramIndex, parseParameter), 0) );
198
 
        {--nomusic}              9 : SetMusic           ( false );
199
 
        {--nosound}             10 : SetSound           ( false );
200
 
        {--fullscreen}          11 : cFullScreen       := true;
201
 
        {--showfps}             12 : cShowFPS          := true;
202
 
        {--altdmg}              13 : cAltDamage        := true;
203
 
        {--low-quality}         14 : cReducedQuality   := $FFFFFFFF xor rqLowRes;
204
 
        {--raw-quality}         15 : cReducedQuality   := getLongIntParameter(arg, paramIndex, parseParameter);
205
 
        {--stereo}              16 : setStereoMode      ( getLongIntParameter(arg, paramIndex, parseParameter) );
206
 
        {--nick}                17 : UserNick          := parseNick( getStringParameter(arg, paramIndex, parseParameter) );
207
 
        {deprecated options}
208
 
        {--depth}               18 : setDepth(paramIndex);
209
 
        {--set-video}           19 : parseClassicParameter(videoArray,5,paramIndex);
210
 
        {--set-audio}           20 : parseClassicParameter(audioArray,3,paramIndex);
211
 
        {--set-other}           21 : parseClassicParameter(otherArray,3,paramIndex);
212
 
        {--set-multimedia}      22 : parseClassicParameter(mediaArray,10,paramIndex);
213
 
        {--set-everything}      23 : parseClassicParameter(allArray,14,paramIndex);
214
 
        {"internal" options}
215
 
        {--internal}            24 : {$IFDEF HWLIBRARY}isInternal:= true{$ENDIF};
216
 
        {--port}                25 : setIpcPort( getLongIntParameter(arg, paramIndex, parseParameter), parseParameter );
217
 
        {--recorder}            26 : startVideoRecording(paramIndex);
218
 
        {--landpreview}         27 : GameType := gmtLandPreview;
219
 
        {anything else}
220
 
        {--stats-only}          28 : statsOnlyGame();
221
 
        {--gci}                 29 : GciEasterEgg();
222
 
        {--help}                30 : DisplayUsage();
223
 
    else
224
 
        begin
225
 
        //Asusme the first "non parameter" is the replay file, anything else is invalid
226
 
        if (recordFileName = '') and (Copy(cmd,1,2) <> '--') then
227
 
            recordFileName := cmd
228
 
        else
229
 
            begin
230
 
            WriteLn(stderr, '"'+cmd+'" is not a valid option');
231
 
            parseParameter:= true;
232
 
            end;
233
 
        end;
234
 
    end;
235
 
end;
236
 
 
237
 
procedure parseClassicParameter(cmdArray: Array of String; size:LongInt; var paramIndex:LongInt);
238
 
var index, tmpInt: LongInt;
239
 
    isBool, isValid: Boolean;
240
 
    cmd, arg, newSyntax: String;
241
 
begin
242
 
    WriteLn(stdout, 'WARNING: you are using a deprecated command, which could be removed in a future version!');
243
 
    WriteLn(stdout, '         Consider updating to the latest syntax, which is much more flexible!');
244
 
    WriteLn(stdout, '         Run `hwegine --help` to learn it!');
245
 
    WriteLn(stdout, '');
246
 
 
247
 
    index:= 0;
248
 
    tmpInt:= 1;
249
 
    while (index < size) do
250
 
        begin
251
 
        newSyntax:= '';
252
 
        inc(paramIndex);
253
 
        cmd:= cmdArray[index];
254
 
        arg:= ParamStr(paramIndex);
255
 
        isValid:= (cmd<>'--depth');
256
 
 
257
 
        // check if the parameter is a boolean one
258
 
        isBool:= (cmd = '--nomusic') or (cmd = '--nosound') or (cmd = '--fullscreen') or (cmd = '--showfps') or (cmd = '--altdmg');
259
 
        if isBool and (arg='0') then
260
 
            isValid:= false;
261
 
        if (cmd='--nomusic') or (cmd='--nosound') then
262
 
            isValid:= not isValid;
263
 
 
264
 
        if isValid then
265
 
            begin
266
 
            parseParameter(cmd, arg, tmpInt);
267
 
            newSyntax := newSyntax + cmd + ' ';
268
 
            if not isBool then
269
 
                newSyntax := newSyntax + arg + ' ';
270
 
            end;
271
 
        inc(index);
272
 
        end;
273
 
 
274
 
    WriteLn(stdout, 'Attempted to automatically convert to the new syntax:');
275
 
    WriteLn(stdout, newSyntax);
276
 
    WriteLn(stdout, '');
277
 
end;
278
 
 
279
 
procedure parseCommandLine{$IFDEF HWLIBRARY}(argc: LongInt; argv: PPChar){$ENDIF};
280
 
var paramIndex: LongInt;
281
 
    paramTotal: LongInt;
282
 
    index, nextIndex: LongInt;
283
 
    wrongParameter: boolean;
284
 
//var tmpInt: LongInt;
285
 
begin
286
 
    paramIndex:= {$IFDEF HWLIBRARY}0{$ELSE}1{$ENDIF};
287
 
    paramTotal:= {$IFDEF HWLIBRARY}argc-1{$ELSE}ParamCount{$ENDIF}; //-1 because pascal enumeration is inclusive
288
 
    (*
289
 
    WriteLn(stdout, 'total parameters: ' + inttostr(paramTotal));
290
 
    tmpInt:= 0;
291
 
    while (tmpInt <= paramTotal) do
292
 
        begin
293
 
        WriteLn(stdout, inttostr(tmpInt) + ': ' + {$IFDEF HWLIBRARY}argv[tmpInt]{$ELSE}paramCount(tmpInt){$ENDIF});
294
 
        inc(tmpInt);
295
 
        end;
296
 
    *)
297
 
    wrongParameter:= false;
298
 
    while (paramIndex <= paramTotal) do
299
 
        begin
300
 
        // avoid going past the number of paramTotal (esp. w/ library)
301
 
        index:= paramIndex;
302
 
        if index = paramTotal then nextIndex:= index
303
 
        else nextIndex:= index+1;
304
 
        {$IFDEF HWLIBRARY}
305
 
        wrongParameter:= parseParameter( argv[index], argv[nextIndex], paramIndex);
306
 
        {$ELSE}
307
 
        wrongParameter:= parseParameter( ParamStr(index), ParamStr(nextIndex), paramIndex);
308
 
        {$ENDIF}
309
 
        inc(paramIndex);
310
 
        end;
311
 
    if wrongParameter = true then
312
 
        GameType:= gmtSyntax;
313
 
end;
314
 
 
315
 
{$IFNDEF HWLIBRARY}
316
 
procedure GetParams;
317
 
begin
318
 
    isInternal:= (ParamStr(1) = '--internal');
319
 
 
320
 
    UserPathPrefix := '.';
321
 
    PathPrefix     := cDefaultPathPrefix;
322
 
    recordFileName := '';
323
 
    parseCommandLine();
324
 
 
325
 
    if (isInternal) and (ParamCount<=1) then
326
 
        begin
327
 
        WriteLn(stderr, '--internal should not be manually used');
328
 
        GameType := gmtSyntax;
329
 
        end;
330
 
 
331
 
    if (not isInternal) and (recordFileName = '') then
332
 
        begin
333
 
        WriteLn(stderr, 'You must specify a replay file');
334
 
        GameType := gmtSyntax;
335
 
        end
336
 
    else if (recordFileName <> '') then
337
 
        WriteLn(stdout, 'Attempting to play demo file "' + recordFilename + '"');
338
 
 
339
 
    if (GameType = gmtSyntax) then
340
 
        WriteLn(stderr, 'Please use --help to see possible arguments and their usage');
341
 
 
342
 
    (*
343
 
    WriteLn(stdout,'PathPrefix:     ' + PathPrefix);
344
 
    WriteLn(stdout,'UserPathPrefix: ' + UserPathPrefix);
345
 
    *)
346
 
end;
347
 
{$ENDIF}
348