-
Notifications
You must be signed in to change notification settings - Fork 1
/
fileio.tscript
66 lines (61 loc) · 1.43 KB
/
fileio.tscript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
$saveRecord = new arrayobject();
$saveRecord.add("<general>","");
function writeSave(%name)
{
if (%name $= "") return;
$saveRecord.empty();
getPrefpath();
%file = new FileObject();
if(%file.openForWrite($prefpath @"/saves/" @ %name @ ".sav"))
{
callonModules("insertSaveData","Game");
saveEntries(%file);
%file.close();
}
else
{
error( "Failed to open " @ %name @ " for writing" );
}
%file.delete();
}
function readSave(%name)
{
if (%name $= "") return;
$saveRecord.empty();
%file = new FileObject();
%couldRead = false;
if(%file.openForRead(getPrefpath() @"/saves/" @ %name @ ".sav"))
{
readEntries(%file);
callonModules("onSaveLoaded","Game");
%couldRead = true;
}
%file.close();
%file.delete();
return %couldRead;
}
function saveEntries(%file)
{
%count = $saveRecord.count();
for(%i=0;%i<%count;%i++)
{
%line = $saveRecord.getKey(%i) SPC $saveRecord.getValue(%i);
%file.writeLine(%line);
}
}
function readEntries(%file)
{
%curModule = "general";
while (!%file.isEof())
{
%temp = %file.readLine();
if (strchrpos(getword(%temp,0),"<")>-1)
{
%curModule = stripChars(%temp,"< >");
$saveRecord.add("<"@%curModule@">","");
continue;
}
$saveRecord.add(getword(%temp,0),getwords(%temp,1,getWordCount(%temp)));
}
callonModules("parseSaveData","Game");
}