-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserialization.as
More file actions
77 lines (61 loc) · 1.89 KB
/
serialization.as
File metadata and controls
77 lines (61 loc) · 1.89 KB
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
67
68
69
70
71
72
73
74
75
76
77
/*
Copyright (c) 2019, Jannik Kolodziej, All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library.
*/
namespace Serialization
{
void SerializeStringDictionary( Dictionary @dict, String filename )
{
if( @dict == null )
return;
String output = "dict";
String@[]@ keys = dict.getKeys();
if( @keys == null )
return;
for( uint i = 0; i < keys.length; i++ )
{
if( @keys[i] == null )
continue;
String @value;
dict.get( keys[i], @value );
if( @value == null )
continue;
output += keys[i] + "\u0002" + value + "\u0003";
}
if( filename.length() == 0 )
return;
::G_WriteFile( filename, output );
}
Dictionary@ DeserializeStringDictionary( String filename )
{
if( filename.length() == 0 || !::G_FileExists( filename ) )
return null;
String content = ::G_LoadFile( filename );
if( content.substr( 0, 4 ) != "dict" )
return null;
content = content.substr( 4 );
String@[]@ pairs = StringUtils::Split( content, "\u0003" );
if( @pairs == null || pairs.length == 0 )
return null;
Dictionary dict();
for( uint i = 0; i < pairs.length; i++ )
{
if( @pairs[i] == null )
continue;
String@[]@ parts = StringUtils::Split( pairs[i], "\u0002" );
if( @parts == null || parts.length != 2 || @parts[0] == null || @parts[1] == null )
continue;
dict.set( parts[0], parts[1] );
}
return dict;
}
}