DEADSOFTWARE

Game: Use proper syntax of sets for game options instead of raw bitwise operations
[d2df-sdl.git] / src / game / g_holmes_cmd.inc
index 60e72b4f1f712390a0fabfe00d5b3c4b8a9c65ec..93f030194db1e09cd09780573f2ddf3865ea8aff 100644 (file)
@@ -1,9 +1,8 @@
-(* Copyright (C)  DooM 2D:Forever Developers
+(* Copyright (C)  Doom 2D: Forever Developers
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
+ * the Free Software Foundation, version 3 of the License ONLY.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 type
   TBindArgLessCB = procedure ();
   TBindToggleCB = procedure (arg: Integer); // -1: no arg
+  TBindStringCB = procedure (s: AnsiString);
 
   PHolmesCommand = ^THolmesCommand;
   THolmesCommand = record
   public
-    type TType = (TArgLess, TToggle);
+    type TType = (TArgLess, TToggle, TString);
 
   public
     name: AnsiString;
@@ -43,7 +43,7 @@ type
     function cmdName (): AnsiString;
   end;
 
-  TCmdHash = specialize THashBase<AnsiString, PHolmesCommand>;
+  TCmdHash = specialize THashBase<AnsiString, PHolmesCommand, THashKeyStr>;
 
 
 // ////////////////////////////////////////////////////////////////////////// //
@@ -69,33 +69,49 @@ end;
 procedure THolmesCommand.execute (pr: TTextParser);
 var
   a: Integer = -1;
+  s: AnsiString = '';
 begin
   if not assigned(cb) then exit;
-  if (ctype = TType.TToggle) then
-  begin
-    if pr.skipBlanks() then
-    begin
-           if pr.eatId('true') or pr.eatId('tan') or pr.eatId('yes') then a := 1
-      else if pr.eatId('false') or pr.eatId('ona') or pr.eatId('no') then a := 0
-      else begin conwritefln('%s: invalid argument', [name]); exit; end;
-    end;
-  end;
-  if pr.skipBlanks() then begin conwritefln('%s: too many arguments', [name]); exit; end;
   case ctype of
-    TType.TArgLess: TBindArgLessCB(cb)();
-    TType.TToggle: TBindToggleCB(cb)(a);
+    TType.TToggle:
+      begin
+        if (pr.tokType <> pr.TTEOF) then
+        begin
+               if pr.eatId('true') or pr.eatId('tan') or pr.eatId('yes') then a := 1
+          else if pr.eatId('false') or pr.eatId('ona') or pr.eatId('no') then a := 0
+          else begin conwritefln('%s: invalid argument', [name]); exit; end;
+          if (pr.tokType <> pr.TTEOF) then begin conwritefln('%s: too many arguments', [name]); exit; end;
+        end;
+        TBindToggleCB(cb)(a);
+      end;
+    TType.TArgLess:
+      begin
+        if (pr.tokType <> pr.TTEOF) then begin conwritefln('%s: too many arguments', [name]); exit; end;
+        TBindArgLessCB(cb)();
+      end;
+    TType.TString:
+      begin
+        if (pr.tokType <> pr.TTEOF) then
+        begin
+          if (pr.tokType = pr.TTStr) then s := pr.expectStr(false) else s := pr.expectId;
+          if (pr.tokType <> pr.TTEOF) then begin conwritefln('%s: too many arguments', [name]); exit; end;
+        end
+        else
+        begin
+          conwritefln('%s: string argument expected', [name]);
+          exit;
+        end;
+        TBindStringCB(cb)(s);
+      end;
     else assert(false);
   end;
 end;
 
 
 // ////////////////////////////////////////////////////////////////////////// //
-function ansistrEquCB (constref a, b: AnsiString): Boolean; begin result := (a = b); end;
-function ansistrHashCB (constref a: AnsiString): LongWord; begin if (Length(a) > 0) then result := fnvHash(PChar(a)^, Length(a)) else result := 0; end;
-
 function hashNewCommand (): TCmdHash;
 begin
-  result := TCmdHash.Create(ansistrHashCB, ansistrEquCB);
+  result := TCmdHash.Create();
 end;
 
 
@@ -135,6 +151,7 @@ begin
 end;
 
 
+// ////////////////////////////////////////////////////////////////////////// //
 procedure cmdAdd (const aname: AnsiString; cb: TBindArgLessCB; const ahelp: AnsiString; const asection: AnsiString); overload;
 var
   cmd: PHolmesCommand;
@@ -157,6 +174,18 @@ begin
 end;
 
 
+procedure cmdAdd (const aname: AnsiString; cb: TBindStringCB; const ahelp: AnsiString; const asection: AnsiString); overload;
+var
+  cmd: PHolmesCommand;
+begin
+  if (Length(aname) = 0) or not assigned(cb) then exit;
+  cmd := cmdNewInternal(aname, ahelp, asection);
+  cmd.cb := Pointer(@cb);
+  cmd.ctype := cmd.TType.TString;
+end;
+
+
+// ////////////////////////////////////////////////////////////////////////// //
 function getCommandHelp (const aname: AnsiString): AnsiString;
 var
   cmd: PHolmesCommand = nil;
@@ -250,7 +279,7 @@ begin
 end;
 
 
-function keybindExecute (var ev: THKeyEvent): Boolean;
+function keybindExecute (var ev: TFUIEvent): Boolean;
 var
   f: Integer;
 begin
@@ -268,7 +297,7 @@ begin
 end;
 
 
-function msbindExecute (var ev: THMouseEvent): Boolean;
+function msbindExecute (var ev: TFUIEvent): Boolean;
 var
   f: Integer;
 begin