-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGMSList.cs
195 lines (176 loc) · 5.82 KB
/
GMSList.cs
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DeltaruneSaveConverter
{
enum GMSListItemType
{
GMSListReal,
GMSListString
}
class GMSListItem
{
public GMSListItemType type;
}
class GMSListStringItem : GMSListItem
{
public string stringValue;
public GMSListStringItem(string value)
{
type = GMSListItemType.GMSListString;
stringValue = value;
}
public override string ToString()
{
return stringValue;
}
}
class GMSListRealItem : GMSListItem
{
public double realValue;
public GMSListRealItem(double value)
{
type = GMSListItemType.GMSListReal;
realValue = value;
}
public override string ToString()
{
return realValue.ToString();
}
}
class GMSListDecoder
{
private byte[] rawlist;
private List<GMSListItem> list;
public GMSListDecoder(string listhex)
{
if (!listhex.StartsWith("2E010000") && !listhex.StartsWith("2F010000"))
{
throw new Exception("String was passed to GMSListDecoder that is not a ds_list.");
} else if (listhex.Length % 2 != 0)
{
throw new Exception("String was passed to GMSListDecoder that is not an even length.");
}
rawlist = new byte[listhex.Length / 2];
for (int i = 0; i < listhex.Length; i += 2) rawlist[i / 2] = Convert.ToByte(listhex.Substring(i, 2), 16);
//items = BitConverter.ToInt32(rawlist, 4);
list = new List<GMSListItem>();
for (int i = 8; i < rawlist.Length;)
{
GMSListItemType type = (GMSListItemType)BitConverter.ToInt32(rawlist, i);
i += 4;
if (type == GMSListItemType.GMSListReal)
{
double value = BitConverter.ToDouble(rawlist, i);
i += 8;
list.Add(new GMSListRealItem(value));
} else if (type == GMSListItemType.GMSListString)
{
int stringLength = BitConverter.ToInt32(rawlist, i);
i += 4;
string stringValue = "";
if (stringLength > 0)
stringValue = Encoding.ASCII.GetString(rawlist, i, stringLength);
list.Add(new GMSListStringItem(stringValue));
i += stringLength;
} else
{
throw new Exception("Unknown list type found in GMSListDecoder");
}
}
}
public int ListSize()
{
return list.Count;
}
public string GetString(int i)
{
if (GetType(i) != GMSListItemType.GMSListString) return "";
return ((GMSListStringItem)list[i]).stringValue;
}
public double GetReal(int i)
{
if (GetType(i) != GMSListItemType.GMSListReal) return 0;
return ((GMSListRealItem)list[i]).realValue;
}
public void ToRealArray(ref double[] output, int length)
{
for (int i = 0; i < length; i++)
{
output[i] = GetReal(i);
}
}
public void ToStringArray(ref string[] output, int length)
{
for (int i = 0; i < length; i++)
{
output[i] = GetString(i);
}
}
public GMSListItemType GetType(int i)
{
return list[i].type;
}
}
class GMSListEncoder
{
private byte[] rawlist;
private List<GMSListItem> list;
public GMSListEncoder()
{
list = new List<GMSListItem>();
}
public GMSListEncoder(IEnumerable<double> reallist)
{
list = new List<GMSListItem>();
foreach(double item in reallist)
list.Add(new GMSListRealItem(item));
}
public GMSListEncoder(IEnumerable<string> stringlist)
{
list = new List<GMSListItem>();
foreach (string item in stringlist)
list.Add(new GMSListStringItem(item));
}
public void AddReal(double real)
{
list.Add(new GMSListRealItem(real));
}
public void AddString(string stringVal)
{
list.Add(new GMSListStringItem(stringVal));
}
public byte[] GetRaw()
{
List<byte> temp = new();
temp.AddRange(BitConverter.GetBytes(0x12E));
temp.AddRange(BitConverter.GetBytes(list.Count));
for (int i = 0; i < list.Count; i++)
{
GMSListItemType type = list[i].type;
temp.AddRange(BitConverter.GetBytes((int)type));
if (type == GMSListItemType.GMSListReal)
{
temp.AddRange(BitConverter.GetBytes(((GMSListRealItem)list[i]).realValue));
} else if (type == GMSListItemType.GMSListString)
{
string stringValue = ((GMSListStringItem)list[i]).stringValue;
temp.AddRange(BitConverter.GetBytes(stringValue.Length));
temp.AddRange(Encoding.UTF8.GetBytes(stringValue, 0, stringValue.Length));
} else
{
throw new Exception("Unknown list type found in GMSListEncoder");
}
}
rawlist = temp.ToArray();
return rawlist;
}
public string GetString()
{
GetRaw();
return BitConverter.ToString(rawlist).Replace("-", "");
}
}
}