hedgewars/uFLScripts.pas
author unc0rr
Wed, 03 Dec 2014 00:07:33 +0300
branchqmlfrontend
changeset 10606 8e95911cb86b
parent 10517 844bd43db47a
child 10612 eb3c1a289a23
permissions -rw-r--r--
merge default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10458
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
     1
unit uFLScripts;
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
     2
interface
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
     3
uses uFLTypes;
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
     4
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
     5
function getScriptsList: PPChar; cdecl;
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
     6
procedure freeScriptsList;
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
     7
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
     8
implementation
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
     9
uses uFLUtils, uFLIPC, uPhysFSLayer, uFLData;
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    10
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    11
const MAX_SCRIPT_NAMES = 64;
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    12
type
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    13
    TScript = record
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    14
            scriptName: shortstring;
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    15
            description: shortstring;
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    16
            gameScheme, weapons: shortstring;
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    17
        end;
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    18
    PScript = ^TScript;
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    19
var
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    20
    scriptsList: PScript;
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    21
    scriptsNumber: Longword;
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    22
    listOfScriptNames: array[0..MAX_SCRIPT_NAMES] of PChar;
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    23
10517
844bd43db47a getScriptsList implementation
unc0rr
parents: 10458
diff changeset
    24
procedure loadScript(var script: TScript; scriptName, fileName: shortstring);
10458
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    25
var f: PFSFile;
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    26
begin
10517
844bd43db47a getScriptsList implementation
unc0rr
parents: 10458
diff changeset
    27
    underScore2Space(scriptName);
844bd43db47a getScriptsList implementation
unc0rr
parents: 10458
diff changeset
    28
    script.scriptName:= scriptName;
844bd43db47a getScriptsList implementation
unc0rr
parents: 10458
diff changeset
    29
    script.description:= scriptName + ' script description';
10458
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    30
10517
844bd43db47a getScriptsList implementation
unc0rr
parents: 10458
diff changeset
    31
    f:= pfsOpenRead(copy(fileName, 1, length(fileName) - 4) + '.txt');
844bd43db47a getScriptsList implementation
unc0rr
parents: 10458
diff changeset
    32
844bd43db47a getScriptsList implementation
unc0rr
parents: 10458
diff changeset
    33
    script.gameScheme:= '';
844bd43db47a getScriptsList implementation
unc0rr
parents: 10458
diff changeset
    34
    script.weapons:= '';
844bd43db47a getScriptsList implementation
unc0rr
parents: 10458
diff changeset
    35
844bd43db47a getScriptsList implementation
unc0rr
parents: 10458
diff changeset
    36
    if f <> nil then
10458
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    37
    begin
10517
844bd43db47a getScriptsList implementation
unc0rr
parents: 10458
diff changeset
    38
        if not pfsEOF(f) then
844bd43db47a getScriptsList implementation
unc0rr
parents: 10458
diff changeset
    39
        begin
844bd43db47a getScriptsList implementation
unc0rr
parents: 10458
diff changeset
    40
            pfsReadLn(f, script.gameScheme);
10458
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    41
10517
844bd43db47a getScriptsList implementation
unc0rr
parents: 10458
diff changeset
    42
            if not pfsEOF(f) then
844bd43db47a getScriptsList implementation
unc0rr
parents: 10458
diff changeset
    43
                pfsReadLn(f, script.weapons);
844bd43db47a getScriptsList implementation
unc0rr
parents: 10458
diff changeset
    44
        end;
844bd43db47a getScriptsList implementation
unc0rr
parents: 10458
diff changeset
    45
844bd43db47a getScriptsList implementation
unc0rr
parents: 10458
diff changeset
    46
        pfsClose(f)
844bd43db47a getScriptsList implementation
unc0rr
parents: 10458
diff changeset
    47
    end
10458
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    48
end;
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    49
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    50
procedure loadScripts;
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    51
var filesList, tmp: PPChar;
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    52
    script: PScript;
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    53
    s: shortstring;
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    54
    l: Longword;
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    55
begin
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    56
    filesList:= pfsEnumerateFiles('/Scripts/Multiplayer');
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    57
    scriptsNumber:= 0;
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    58
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    59
    tmp:= filesList;
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    60
    while tmp^ <> nil do
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    61
    begin
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    62
        s:= shortstring(tmp^);
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    63
        writeln(stderr, '> ', s);
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    64
        l:= length(s);
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    65
        if (l > 4) and (copy(s, l - 3, 4) = '.lua') then inc(scriptsNumber);
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    66
        inc(tmp)
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    67
    end;
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    68
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    69
    scriptsList:= GetMem(sizeof(scriptsList^) * scriptsNumber);
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    70
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    71
    script:= scriptsList;
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    72
    tmp:= filesList;
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    73
    while tmp^ <> nil do
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    74
    begin
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    75
        s:= shortstring(tmp^);
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    76
        l:= length(s);
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    77
        if (l > 4) and (copy(s, l - 3, 4) = '.lua') then 
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    78
            begin
10517
844bd43db47a getScriptsList implementation
unc0rr
parents: 10458
diff changeset
    79
                loadScript(script^, copy(s, 1, l - 4), '/Config/Scripts/' + s);
10458
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    80
                inc(script)
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    81
            end;
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    82
        inc(tmp)
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    83
    end;
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    84
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    85
    pfsFreeList(filesList)
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    86
end;
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    87
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    88
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    89
function getScriptsList: PPChar; cdecl;
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    90
var i, t, l: Longword;
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    91
    script: PScript;
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    92
begin
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    93
    if scriptsList = nil then
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    94
        loadScripts;
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    95
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    96
    t:= scriptsNumber;
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    97
    if t >= MAX_SCRIPT_NAMES then 
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    98
        t:= MAX_SCRIPT_NAMES;
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    99
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
   100
    script:= scriptsList;
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
   101
    for i:= 0 to Pred(t) do
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
   102
    begin
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
   103
        l:= length(script^.scriptName);
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
   104
        if l >= 255 then l:= 254;
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
   105
        script^.scriptName[l + 1]:= #0;
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
   106
        listOfScriptNames[i]:= @script^.scriptName[1];
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
   107
        inc(script)
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
   108
    end;
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
   109
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
   110
    listOfScriptNames[t]:= nil;
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
   111
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
   112
    getScriptsList:= listOfScriptNames
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
   113
end;
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
   114
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
   115
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
   116
procedure freeScriptsList;
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
   117
begin
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
   118
    if scriptsList <> nil then
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
   119
        FreeMem(scriptsList, sizeof(scriptsList^) * scriptsNumber)
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
   120
end;
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
   121
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
   122
end.