hedgewars/uCommands.pas
author unc0rr
Sat, 21 Apr 2012 19:51:13 +0400
changeset 6898 344b0dbd9690
parent 6700 e04da46ee43c
child 6919 bf7433e62b9c
permissions -rw-r--r--
- Remove support for variables in command handlers - Make parameter to RegisterVariable typed instead of just pointer - Catch a bug due to wrong type passed to RegisterVariable
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4976
088d40d8aba2 Happy 2011 :)
koda
parents: 4900
diff changeset
     1
(*
088d40d8aba2 Happy 2011 :)
koda
parents: 4900
diff changeset
     2
 * Hedgewars, a free turn based strategy game
6700
e04da46ee43c the most important commit of the year
koda
parents: 6580
diff changeset
     3
 * Copyright (c) 2004-2012 Andrey Korotaev <unC0Rr@gmail.com>
4976
088d40d8aba2 Happy 2011 :)
koda
parents: 4900
diff changeset
     4
 *
088d40d8aba2 Happy 2011 :)
koda
parents: 4900
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
088d40d8aba2 Happy 2011 :)
koda
parents: 4900
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
088d40d8aba2 Happy 2011 :)
koda
parents: 4900
diff changeset
     7
 * the Free Software Foundation; version 2 of the License
088d40d8aba2 Happy 2011 :)
koda
parents: 4900
diff changeset
     8
 *
088d40d8aba2 Happy 2011 :)
koda
parents: 4900
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
088d40d8aba2 Happy 2011 :)
koda
parents: 4900
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
088d40d8aba2 Happy 2011 :)
koda
parents: 4900
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
088d40d8aba2 Happy 2011 :)
koda
parents: 4900
diff changeset
    12
 * GNU General Public License for more details.
088d40d8aba2 Happy 2011 :)
koda
parents: 4900
diff changeset
    13
 *
088d40d8aba2 Happy 2011 :)
koda
parents: 4900
diff changeset
    14
 * You should have received a copy of the GNU General Public License
088d40d8aba2 Happy 2011 :)
koda
parents: 4900
diff changeset
    15
 * along with this program; if not, write to the Free Software
088d40d8aba2 Happy 2011 :)
koda
parents: 4900
diff changeset
    16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
088d40d8aba2 Happy 2011 :)
koda
parents: 4900
diff changeset
    17
 *)
088d40d8aba2 Happy 2011 :)
koda
parents: 4900
diff changeset
    18
4373
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    19
{$INCLUDE "options.inc"}
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    20
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    21
unit uCommands;
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    22
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    23
interface
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    24
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    25
var isDeveloperMode: boolean;
6898
344b0dbd9690 - Remove support for variables in command handlers
unc0rr
parents: 6700
diff changeset
    26
type TCommandHandler = procedure (var params: shortstring);
4373
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    27
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    28
procedure initModule;
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    29
procedure freeModule;
6898
344b0dbd9690 - Remove support for variables in command handlers
unc0rr
parents: 6700
diff changeset
    30
procedure RegisterVariable(Name: shortstring; p: TCommandHandler; Trusted: boolean);
4373
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    31
procedure ParseCommand(CmdStr: shortstring; TrustedSource: boolean);
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    32
procedure StopMessages(Message: Longword);
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    33
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    34
implementation
5554
b27ed6c6f538 Revert ParseCommandOverride change since it appears to be badly screwing up scripting. Need to find out why. This backs out 7f57d0c7816a and the recent workaround.
nemo
parents: 5352
diff changeset
    35
uses Types, uConsts, uVariables, uConsole, uUtils, uDebug;
4373
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    36
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    37
type  PVariable = ^TVariable;
6580
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6450
diff changeset
    38
    TVariable = record
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6450
diff changeset
    39
        Next: PVariable;
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6450
diff changeset
    40
        Name: string[15];
6898
344b0dbd9690 - Remove support for variables in command handlers
unc0rr
parents: 6700
diff changeset
    41
        Handler: TCommandHandler;
6580
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6450
diff changeset
    42
        Trusted: boolean;
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6450
diff changeset
    43
        end;
4373
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    44
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    45
var
6580
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6450
diff changeset
    46
    Variables: PVariable;
4373
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    47
6898
344b0dbd9690 - Remove support for variables in command handlers
unc0rr
parents: 6700
diff changeset
    48
procedure RegisterVariable(Name: shortstring; p: TCommandHandler; Trusted: boolean);
6580
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6450
diff changeset
    49
var
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6450
diff changeset
    50
    value: PVariable;
4373
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    51
begin
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    52
New(value);
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    53
TryDo(value <> nil, 'RegisterVariable: value = nil', true);
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    54
FillChar(value^, sizeof(TVariable), 0);
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    55
value^.Name:= Name;
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    56
value^.Handler:= p;
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    57
value^.Trusted:= Trusted;
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    58
6580
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6450
diff changeset
    59
if Variables = nil then
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6450
diff changeset
    60
    Variables:= value
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6450
diff changeset
    61
else
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6450
diff changeset
    62
    begin
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6450
diff changeset
    63
    value^.Next:= Variables;
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6450
diff changeset
    64
    Variables:= value
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6450
diff changeset
    65
    end;
4373
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    66
end;
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    67
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    68
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    69
procedure ParseCommand(CmdStr: shortstring; TrustedSource: boolean);
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    70
var ii: LongInt;
5554
b27ed6c6f538 Revert ParseCommandOverride change since it appears to be badly screwing up scripting. Need to find out why. This backs out 7f57d0c7816a and the recent workaround.
nemo
parents: 5352
diff changeset
    71
    s: shortstring;
4373
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    72
    t: PVariable;
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    73
    c: char;
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    74
begin
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    75
//WriteLnToConsole(CmdStr);
6580
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6450
diff changeset
    76
if CmdStr[0]=#0 then
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6450
diff changeset
    77
    exit;
4373
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    78
c:= CmdStr[1];
6580
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6450
diff changeset
    79
if (c = '/') or (c = '$') then
6898
344b0dbd9690 - Remove support for variables in command handlers
unc0rr
parents: 6700
diff changeset
    80
    Delete(CmdStr, 1, 1);
4373
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    81
s:= '';
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    82
SplitBySpace(CmdStr, s);
6898
344b0dbd9690 - Remove support for variables in command handlers
unc0rr
parents: 6700
diff changeset
    83
AddFileLog('[Cmd] ' + CmdStr + ' (' + inttostr(length(s)) + ')');
4373
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    84
t:= Variables;
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    85
while t <> nil do
6580
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6450
diff changeset
    86
    begin
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6450
diff changeset
    87
    if t^.Name = CmdStr then
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6450
diff changeset
    88
        begin
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6450
diff changeset
    89
        if TrustedSource or t^.Trusted then
6898
344b0dbd9690 - Remove support for variables in command handlers
unc0rr
parents: 6700
diff changeset
    90
            t^.Handler(s);
344b0dbd9690 - Remove support for variables in command handlers
unc0rr
parents: 6700
diff changeset
    91
        exit
344b0dbd9690 - Remove support for variables in command handlers
unc0rr
parents: 6700
diff changeset
    92
        end
344b0dbd9690 - Remove support for variables in command handlers
unc0rr
parents: 6700
diff changeset
    93
    else
344b0dbd9690 - Remove support for variables in command handlers
unc0rr
parents: 6700
diff changeset
    94
        t:= t^.Next
344b0dbd9690 - Remove support for variables in command handlers
unc0rr
parents: 6700
diff changeset
    95
    end;
4373
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    96
case c of
6580
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6450
diff changeset
    97
    '$': WriteLnToConsole(errmsgUnknownVariable + ': "$' + CmdStr + '"')
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6450
diff changeset
    98
    else
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6450
diff changeset
    99
        WriteLnToConsole(errmsgUnknownCommand  + ': "/' + CmdStr + '"') end
4373
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
   100
end;
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
   101
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
   102
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
   103
procedure StopMessages(Message: Longword);
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
   104
begin
6580
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6450
diff changeset
   105
if (Message and gmLeft) <> 0 then
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6450
diff changeset
   106
    ParseCommand('/-left', true)
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6450
diff changeset
   107
else if (Message and gmRight) <> 0 then
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6450
diff changeset
   108
    ParseCommand('/-right', true) 
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6450
diff changeset
   109
else if (Message and gmUp) <> 0 then
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6450
diff changeset
   110
    ParseCommand('/-up', true)
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6450
diff changeset
   111
else if (Message and gmDown) <> 0 then
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6450
diff changeset
   112
    ParseCommand('/-down', true)
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6450
diff changeset
   113
else if (Message and gmAttack) <> 0 then
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6450
diff changeset
   114
    ParseCommand('/-attack', true)
4373
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
   115
end;
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
   116
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
   117
procedure initModule;
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
   118
begin
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
   119
    Variables:= nil;
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
   120
    isDeveloperMode:= true;
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
   121
end;
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
   122
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
   123
procedure freeModule;
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
   124
var t, tt: PVariable;
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
   125
begin
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
   126
    tt:= Variables;
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
   127
    Variables:= nil;
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
   128
    while tt <> nil do
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
   129
    begin
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
   130
        t:= tt;
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
   131
        tt:= tt^.Next;
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
   132
        Dispose(t)
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
   133
    end;
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
   134
end;
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
   135
4406
beb4de0af990 Increase teams to 8 to match the 8 colours, fix issue #108, reenable rope length modifier
nemo
parents: 4403
diff changeset
   136
end.