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