hedgewars/uCommands.pas
changeset 6580 6155187bf599
parent 6450 14224c9b4594
child 6700 e04da46ee43c
equal deleted inserted replaced
6579:fc52f7c22c9b 6580:6155187bf599
    22 
    22 
    23 interface
    23 interface
    24 
    24 
    25 var isDeveloperMode: boolean;
    25 var isDeveloperMode: boolean;
    26 type TVariableType = (vtCommand, vtLongInt, vtBoolean);
    26 type TVariableType = (vtCommand, vtLongInt, vtBoolean);
    27      TCommandHandler = procedure (var params: shortstring);
    27     TCommandHandler = procedure (var params: shortstring);
    28 
    28 
    29 procedure initModule;
    29 procedure initModule;
    30 procedure freeModule;
    30 procedure freeModule;
    31 procedure RegisterVariable(Name: shortstring; VType: TVariableType; p: pointer; Trusted: boolean);
    31 procedure RegisterVariable(Name: shortstring; VType: TVariableType; p: pointer; Trusted: boolean);
    32 procedure ParseCommand(CmdStr: shortstring; TrustedSource: boolean);
    32 procedure ParseCommand(CmdStr: shortstring; TrustedSource: boolean);
    34 
    34 
    35 implementation
    35 implementation
    36 uses Types, uConsts, uVariables, uConsole, uUtils, uDebug;
    36 uses Types, uConsts, uVariables, uConsole, uUtils, uDebug;
    37 
    37 
    38 type  PVariable = ^TVariable;
    38 type  PVariable = ^TVariable;
    39       TVariable = record
    39     TVariable = record
    40                      Next: PVariable;
    40         Next: PVariable;
    41                      Name: string[15];
    41         Name: string[15];
    42                     VType: TVariableType;
    42         VType: TVariableType;
    43                   Handler: pointer;
    43         Handler: pointer;
    44                   Trusted: boolean;
    44         Trusted: boolean;
    45                   end;
    45         end;
    46 
    46 
    47 var
    47 var
    48       Variables: PVariable;
    48     Variables: PVariable;
    49 
    49 
    50 procedure RegisterVariable(Name: shortstring; VType: TVariableType; p: pointer; Trusted: boolean);
    50 procedure RegisterVariable(Name: shortstring; VType: TVariableType; p: pointer; Trusted: boolean);
    51 var value: PVariable;
    51 var
       
    52     value: PVariable;
    52 begin
    53 begin
    53 New(value);
    54 New(value);
    54 TryDo(value <> nil, 'RegisterVariable: value = nil', true);
    55 TryDo(value <> nil, 'RegisterVariable: value = nil', true);
    55 FillChar(value^, sizeof(TVariable), 0);
    56 FillChar(value^, sizeof(TVariable), 0);
    56 value^.Name:= Name;
    57 value^.Name:= Name;
    57 value^.VType:= VType;
    58 value^.VType:= VType;
    58 value^.Handler:= p;
    59 value^.Handler:= p;
    59 value^.Trusted:= Trusted;
    60 value^.Trusted:= Trusted;
    60 
    61 
    61 if Variables = nil then Variables:= value
    62 if Variables = nil then
    62                    else begin
    63     Variables:= value
    63                         value^.Next:= Variables;
    64 else
    64                         Variables:= value
    65     begin
    65                         end;
    66     value^.Next:= Variables;
       
    67     Variables:= value
       
    68     end;
    66 end;
    69 end;
    67 
    70 
    68 
    71 
    69 procedure ParseCommand(CmdStr: shortstring; TrustedSource: boolean);
    72 procedure ParseCommand(CmdStr: shortstring; TrustedSource: boolean);
    70 var ii: LongInt;
    73 var ii: LongInt;
    71     s: shortstring;
    74     s: shortstring;
    72     t: PVariable;
    75     t: PVariable;
    73     c: char;
    76     c: char;
    74 begin
    77 begin
    75 //WriteLnToConsole(CmdStr);
    78 //WriteLnToConsole(CmdStr);
    76 if CmdStr[0]=#0 then exit;
    79 if CmdStr[0]=#0 then
       
    80     exit;
    77 c:= CmdStr[1];
    81 c:= CmdStr[1];
    78 if (c = '/') or (c = '$') then Delete(CmdStr, 1, 1) else c:= '/';
    82 if (c = '/') or (c = '$') then
       
    83     Delete(CmdStr, 1, 1)
       
    84 else
       
    85     c:= '/';
    79 s:= '';
    86 s:= '';
    80 SplitBySpace(CmdStr, s);
    87 SplitBySpace(CmdStr, s);
    81 AddFileLog('[Cmd] ' + c + CmdStr + ' (' + inttostr(length(s)) + ')');
    88 AddFileLog('[Cmd] ' + c + CmdStr + ' (' + inttostr(length(s)) + ')');
    82 t:= Variables;
    89 t:= Variables;
    83 while t <> nil do
    90 while t <> nil do
    84       begin
    91     begin
    85       if t^.Name = CmdStr then
    92     if t^.Name = CmdStr then
    86          begin
    93         begin
    87          if TrustedSource or t^.Trusted then
    94         if TrustedSource or t^.Trusted then
    88             case t^.VType of
    95             case t^.VType of
    89               vtCommand: if c='/' then
    96                 vtCommand: if c='/' then
    90                          begin
    97                     begin
    91                          TCommandHandler(t^.Handler)(s);
    98                     TCommandHandler(t^.Handler)(s);
    92                          end;
    99                     end;
    93               vtLongInt: if c='$' then
   100                 vtLongInt: if c='$' then
    94                          if s[0]=#0 then
   101                     if s[0]=#0 then
    95                             begin
   102                         begin
    96                             str(PLongInt(t^.Handler)^, s);
   103                         str(PLongInt(t^.Handler)^, s);
    97                             WriteLnToConsole('$' + CmdStr + ' is "' + s + '"');
   104                         WriteLnToConsole('$' + CmdStr + ' is "' + s + '"');
    98                             end else val(s, PLongInt(t^.Handler)^);
   105                         end
    99              vtBoolean: if c='$' then
   106                     else
   100                          if s[0]=#0 then
   107                         val(s, PLongInt(t^.Handler)^);
   101                             begin
   108                 vtBoolean: if c='$' then
   102                             str(ord(boolean(t^.Handler^)), s);
   109                     if s[0]=#0 then
   103                             WriteLnToConsole('$' + CmdStr + ' is "' + s + '"');
   110                         begin
   104                             end else
   111                         str(ord(boolean(t^.Handler^)), s);
   105                             begin
   112                         WriteLnToConsole('$' + CmdStr + ' is "' + s + '"');
   106                             val(s, ii);
   113                         end
   107                             boolean(t^.Handler^):= not (ii = 0)
   114                     else
   108                             end;
   115                         begin
   109               end;
   116                         val(s, ii);
   110          exit
   117                         boolean(t^.Handler^):= not (ii = 0)
   111          end else t:= t^.Next
   118                         end;
   112       end;
   119                 end;
       
   120             exit
       
   121             end
       
   122         else
       
   123             t:= t^.Next
       
   124         end;
   113 case c of
   125 case c of
   114      '$': WriteLnToConsole(errmsgUnknownVariable + ': "$' + CmdStr + '"')
   126     '$': WriteLnToConsole(errmsgUnknownVariable + ': "$' + CmdStr + '"')
   115      else WriteLnToConsole(errmsgUnknownCommand  + ': "/' + CmdStr + '"') end
   127     else
       
   128         WriteLnToConsole(errmsgUnknownCommand  + ': "/' + CmdStr + '"') end
   116 end;
   129 end;
   117 
   130 
   118 
   131 
   119 procedure StopMessages(Message: Longword);
   132 procedure StopMessages(Message: Longword);
   120 begin
   133 begin
   121 if (Message and gmLeft) <> 0 then ParseCommand('/-left', true) else
   134 if (Message and gmLeft) <> 0 then
   122 if (Message and gmRight) <> 0 then ParseCommand('/-right', true) else
   135     ParseCommand('/-left', true)
   123 if (Message and gmUp) <> 0 then ParseCommand('/-up', true) else
   136 else if (Message and gmRight) <> 0 then
   124 if (Message and gmDown) <> 0 then ParseCommand('/-down', true) else
   137     ParseCommand('/-right', true) 
   125 if (Message and gmAttack) <> 0 then ParseCommand('/-attack', true)
   138 else if (Message and gmUp) <> 0 then
       
   139     ParseCommand('/-up', true)
       
   140 else if (Message and gmDown) <> 0 then
       
   141     ParseCommand('/-down', true)
       
   142 else if (Message and gmAttack) <> 0 then
       
   143     ParseCommand('/-attack', true)
   126 end;
   144 end;
   127 
   145 
   128 procedure initModule;
   146 procedure initModule;
   129 begin
   147 begin
   130     Variables:= nil;
   148     Variables:= nil;