This repository has been archived by the owner on Jun 16, 2018. It is now read-only.
/
SMObjects.inc
326 lines (290 loc) · 6.69 KB
/
SMObjects.inc
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/** Double-include prevention */
#if defined SMObjects_included
#endinput
#endif
#define SMObjects_included
enum ObjectType
{
Type_Boolean = 0,
Type_Integer,
Type_String,
Type_Float,
Type_CellArray
};
methodmap Object < Handle
{
/**
* Constructs an Object and returns the handle
*
* @return Object handle
*/
public native Object();
/**
* Grabs an integer from a specified key.
*
* @param char[] key to pull value from
* @return integer value
*/
public native int GetInt(const char[] key);
/**
* Sets an integer for a specified key.
*
* @param char[] key to pull value from
* @param int value
* @return true on success
*/
public native bool SetInt(const char[] key, int value);
/**
* Grabs a cell type for a specified key.
* Note: Use GetObjectInt for integers
* @param char[] key to pull value from
* @return any value
*/
public any GetCell(const char[] key)
{
view_as<any>(this.GetInt(key));
}
/**
* Sets a cell type for a specified key.
* Note: Use SetObjectInt for integers
* @param char[] key to pull value from
* @param any value
* @return true on success
*/
public bool SetCell(const char[] key, any value)
{
this.SetInt(key, view_as<int>(value));
}
/**
* Grabs a bool for a specified key.
*
* @param char[] key to pull value from
* @return boolean value
*/
public native bool GetBool(const char[] key);
/**
* Sets a bool for a specified key.
*
* @param char[] key to pull value from
* @param bool value
* @return true on success
*/
public native bool SetBool(const char[] key, bool value);
/**
* Grabs a float for a specified key.
*
* @param char[] key to pull value from
* @return float value
*/
public native float GetFloat(const char[] key);
/**
* Sets a float for a specified key.
*
* @param char[] key to pull value from
* @param float value
* @return true on success
*/
public native bool SetFloat(const char[] key, float value);
/**
* Grabs a string for a specified key.
*
* @param char[] key to pull value from
* @param char[] output buffer
* @param int max # of bytes to write
* @return true on success
*/
public native bool GetString(const char[] key, char[] buffer, int length);
/**
* Sets a string for a specified key.
*
* @param char[] key to pull value from
* @param char[] input string
* @return true on success
*/
public native bool SetString(const char[] key, const char[] value);
/**
* Gets an array for a specified key.
*
* @param char[] key to pull value from
* @param any[] output buffer
* @param int max # of bytes to write
* @return true on success
*/
public native bool GetArray(const char[] key, any[] buffer, int length);
/**
* Sets an array for a specified key.
*
* @param char[] key to pull value from
* @param any[] input buffer
* @param int max # of bytes to read
* @return true on success
*/
public native bool SetArray(const char[] key, any[] buffer, int length);
/**
* Grabs the size of a written array
*
* @param char[] key to pull value from
* @return buffer size
*/
public native int GetArraySize(const char[] key);
/**
* Grabs the amount of members in the object
*
* @return int Size
*/
property int MemberCount
{
public native get();
}
/**
* Gets the type of a specified index
*
* @param index member index
* @return none
*/
public native ObjectType GetMemberType(int index);
/**
* Grabs the key name for a member index
*
* @param index member index
* @param buffer buffer to write to
* @param maxlen max number of bytes to write
* @return none
*/
public native int GetMemberKey(int index, const char[] buffer, int maxlen);
/**
* Gets the int from a member index
*
* @param index member index
* @return int value
*/
public native int GetMemberInt(int index);
/**
* Gets the float from a member index
*
* @param index member index
* @return float value
*/
public native float GetMemberFloat(int index);
/**
* Gets the bool from a member index
*
* @param index member index
* @return bool value
*/
public native bool GetMemberBool(int index);
/**
* Gets the string from a member index
*
* @param index member index
* @param buffer buffer to write to
* @param maxlen max length
* @return none
*/
public native void GetMemberString(int index, const char[] buffer, int maxlen);
/**
* Gets the any[] array from a member index
*
* @param index member index
* @param buffer buffer to write to
* @param maxlen max length
* @return none
*/
public native void GetMemberArray(int index, any[] buffer, int maxlen);
/**
* Get's a member's array size (required for GetMemberArray)
*
* @param index member index
* @return int size
*/
public native int GetMemberArraySize(int index);
/**
* Determines if the object has a name set.
*
* @return bool true if has name
*/
property bool HasName
{
public native get();
}
/**
* Gets the name of an object
*
* @param char[] buffer to write to
* @param int max length
* @return void
*/
public native void GetName(const char[] buffer, int maxlen);
/**
* Sets the name of an object
*
* @param char[] string to save
* @return void
*/
public native void SetName(const char[] buffer);
/**
* Converts an SMObject type into a KeyValues structure.
*
* Creates a new handle that must be closed.
*
* @return KeyValues KV Structure
*/
public KeyValues ToKeyValues()
{
// Create new KV key
char keyName[16];
if (this.HasName)
{
this.GetName(keyName, sizeof(keyName));
}
else
{
keyName = "SMObject";
}
KeyValues kv = new KeyValues(keyName);
for (int i = 0; i < this.MemberCount; i++)
{
// Get Current SMObject key
char key[32];
this.GetMemberKey(i, key, sizeof(key));
// Get type
ObjectType type = this.GetMemberType(i);
// Write type and key to KeyValues
switch (type)
{
case Type_Integer:
kv.SetNum(key, this.GetMemberInt(i));
case Type_Boolean:
kv.SetNum(key, view_as<int>(this.GetMemberBool(i)));
case Type_Float:
kv.SetFloat(key, this.GetMemberFloat(i));
case Type_String:
{
char value[128];
this.GetMemberString(i, value, sizeof(value));
kv.SetString(key, value);
}
case Type_CellArray:
{
// do nothing, what would we do?
}
}
}
return kv;
}
};
public Extension __ext_objects =
{
name = "Objects",
file = "objects.ext",
#if defined AUTOLOAD_EXTENSIONS
autoload = 1,
#else
autoload = 0,
#endif
#if defined REQUIRE_EXTENSIONS
required = 1,
#else
required = 0,
#endif
};