hedgewars/uFLScripts.pas
author unC0Rr
Tue, 14 Apr 2015 11:18:14 +0300
branchqmlfrontend
changeset 10894 6157d057f04e
parent 10888 a04e04aaf599
child 11461 ab77e2ea2f82
permissions -rw-r--r--
Racer challenge map #17
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');
10612
eb3c1a289a23 Script combobox.wiki
unc0rr
parents: 10517
diff changeset
    57
    scriptsNumber:= 1;
10458
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
        l:= length(s);
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    64
        if (l > 4) and (copy(s, l - 3, 4) = '.lua') then inc(scriptsNumber);
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    65
        inc(tmp)
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    66
    end;
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    67
10612
eb3c1a289a23 Script combobox.wiki
unc0rr
parents: 10517
diff changeset
    68
    scriptsList:= GetMem(sizeof(scriptsList^) * (scriptsNumber + 1));
10458
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    69
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    70
    script:= scriptsList;
10612
eb3c1a289a23 Script combobox.wiki
unc0rr
parents: 10517
diff changeset
    71
eb3c1a289a23 Script combobox.wiki
unc0rr
parents: 10517
diff changeset
    72
    // add 'normal' script
eb3c1a289a23 Script combobox.wiki
unc0rr
parents: 10517
diff changeset
    73
    script^.scriptName:= 'Normal';
eb3c1a289a23 Script combobox.wiki
unc0rr
parents: 10517
diff changeset
    74
    script^.description:= 'Normal gameplay';
eb3c1a289a23 Script combobox.wiki
unc0rr
parents: 10517
diff changeset
    75
    inc(script);
eb3c1a289a23 Script combobox.wiki
unc0rr
parents: 10517
diff changeset
    76
eb3c1a289a23 Script combobox.wiki
unc0rr
parents: 10517
diff changeset
    77
    // fill the rest from *.lua list
10458
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    78
    tmp:= filesList;
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    79
    while tmp^ <> nil do
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    80
    begin
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    81
        s:= shortstring(tmp^);
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    82
        l:= length(s);
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    83
        if (l > 4) and (copy(s, l - 3, 4) = '.lua') then 
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    84
            begin
10517
844bd43db47a getScriptsList implementation
unc0rr
parents: 10458
diff changeset
    85
                loadScript(script^, copy(s, 1, l - 4), '/Config/Scripts/' + s);
10458
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    86
                inc(script)
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    87
            end;
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    88
        inc(tmp)
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    89
    end;
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    90
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    91
    pfsFreeList(filesList)
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    92
end;
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    93
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    94
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    95
function getScriptsList: PPChar; cdecl;
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    96
var i, t, l: Longword;
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    97
    script: PScript;
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    98
begin
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
    99
    if scriptsList = nil then
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
   100
        loadScripts;
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
   101
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
   102
    t:= scriptsNumber;
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
   103
    if t >= MAX_SCRIPT_NAMES then 
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
   104
        t:= MAX_SCRIPT_NAMES;
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
   105
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
   106
    script:= scriptsList;
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
   107
    for i:= 0 to Pred(t) do
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
   108
    begin
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
   109
        l:= length(script^.scriptName);
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
   110
        if l >= 255 then l:= 254;
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
   111
        script^.scriptName[l + 1]:= #0;
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
   112
        listOfScriptNames[i]:= @script^.scriptName[1];
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
   113
        inc(script)
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
   114
    end;
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
   115
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
   116
    listOfScriptNames[t]:= nil;
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
   117
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
   118
    getScriptsList:= listOfScriptNames
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
   119
end;
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
   120
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
   121
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
   122
procedure freeScriptsList;
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
   123
begin
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
   124
    if scriptsList <> nil then
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
   125
        FreeMem(scriptsList, sizeof(scriptsList^) * scriptsNumber)
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
   126
end;
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
   127
f7a199346c3e Scripts list module
unc0rr
parents:
diff changeset
   128
end.