5
5
// / @param struct/array The data to be encoded. Can contain structs, arrays, strings, and numbers. N.B. Will not encode ds_list, ds_map etc.
6
6
// / @param [alphabetizeStructs=false] Whether to alphabetize struct variable names. Incurs a performance penalty is set to <true>
7
7
// /
8
- // / @jujuadams 2022 -10-30
8
+ // / @jujuadams 2023 -10-27
9
9
10
10
/*
11
11
0x00 - terminator
23
23
*/
24
24
25
25
function SnapBufferWriteBinary (_buffer, _value, _alphabetizeStructs = false )
26
+ {
27
+ // Determine if we need to use the legacy codebase by checking against struct_foreach()
28
+ static _useLegacy = undefined;
29
+ if (_useLegacy == undefined)
30
+ {
31
+ try
32
+ {
33
+ struct_foreach ({}, function () {});
34
+ _useLegacy = false ;
35
+ }
36
+ catch (_error)
37
+ {
38
+ _useLegacy = true ;
39
+ }
40
+ }
41
+
42
+ if (_useLegacy)
43
+ {
44
+ return __SnapBufferWriteBinaryLegacy (_buffer, _value, _alphabetizeStructs);
45
+ }
46
+ else
47
+ {
48
+ with (method_get_self (__SnapBufferWriteBinaryStructIteratorMethod ()))
49
+ {
50
+ __buffer = _buffer;
51
+ __alphabetizeStructs = _alphabetizeStructs;
52
+ }
53
+
54
+ return __SnapBufferWriteBinary (_buffer, _value, _alphabetizeStructs);
55
+ }
56
+ }
57
+
58
+ // We have to use this weird workaround because you can't static_get() a function you haven't run before
59
+ function __SnapBufferWriteBinaryStructIteratorMethod ()
60
+ {
61
+ static _method = method (
62
+ {
63
+ __buffer: undefined,
64
+ __alphabetizeStructs: false ,
65
+ },
66
+ function (_name, _value)
67
+ {
68
+ if (!is_string (_name)) show_error (" SNAP:\n Keys must be strings\n " , true );
69
+
70
+ buffer_write (__buffer, buffer_string, _name);
71
+ __SnapBufferWriteBinary (__buffer, _value, __alphabetizeStructs);
72
+ }
73
+ );
74
+
75
+ return _method;
76
+ }
77
+
78
+ function __SnapBufferWriteBinary (_buffer, _value, _alphabetizeStructs)
79
+ {
80
+ static _structIteratorMethod = __SnapBufferWriteBinaryStructIteratorMethod ();
81
+
82
+ if (is_method (_value)) // Implicitly also a struct so we have to check this first
83
+ {
84
+ buffer_write (_buffer, buffer_u8, 0x03 ); // Convert all methods to strings
85
+ buffer_write (_buffer, buffer_string, string (_value));
86
+ }
87
+ else if (is_struct (_value))
88
+ {
89
+ var _struct = _value;
90
+ var _count = variable_struct_names_count (_struct);
91
+
92
+ buffer_write (_buffer, buffer_u8, 0x01 ); // Struct
93
+ buffer_write (_buffer, buffer_u64, _count);
94
+
95
+ if (_count > 0 )
96
+ {
97
+ if (_alphabetizeStructs)
98
+ {
99
+ var _names = variable_struct_get_names (_struct);
100
+ array_sort (_names, true );
101
+ var _i = 0 ;
102
+ repeat (_count)
103
+ {
104
+ var _name = _names[_i];
105
+ if (!is_string (_name)) show_error (" SNAP:\n Keys must be strings\n " , true );
106
+
107
+ buffer_write (_buffer, buffer_string, _name);
108
+ __SnapBufferWriteBinary (_buffer, _struct[$ _name], _alphabetizeStructs);
109
+
110
+ ++_i;
111
+ }
112
+ }
113
+ else
114
+ {
115
+ struct_foreach (_struct, _structIteratorMethod);
116
+ }
117
+ }
118
+ }
119
+ else if (is_array (_value))
120
+ {
121
+ var _array = _value;
122
+ var _count = array_length (_array);
123
+
124
+ buffer_write (_buffer, buffer_u8, 0x02 ); // /Array
125
+ buffer_write (_buffer, buffer_u64, _count);
126
+
127
+ var _i = 0 ;
128
+ repeat (_count)
129
+ {
130
+ __SnapBufferWriteBinary (_buffer, _array[_i], _alphabetizeStructs);
131
+ ++_i;
132
+ }
133
+ }
134
+ else if (is_string (_value))
135
+ {
136
+ buffer_write (_buffer, buffer_u8, 0x03 ); // String
137
+ buffer_write (_buffer, buffer_string, _value);
138
+ }
139
+ else if (is_real (_value))
140
+ {
141
+ if (_value == 0 )
142
+ {
143
+ buffer_write (_buffer, buffer_u8, 0x05 ); // <false>
144
+ }
145
+ else if (_value == 1 )
146
+ {
147
+ buffer_write (_buffer, buffer_u8, 0x06 ); // <true>
148
+ }
149
+ else
150
+ {
151
+ buffer_write (_buffer, buffer_u8, 0x04 ); // f64
152
+ buffer_write (_buffer, buffer_f64, _value);
153
+ }
154
+ }
155
+ else if (is_bool (_value))
156
+ {
157
+ buffer_write (_buffer, buffer_u8, _value? 0x06 : 0x05 ); // <true> or <false>
158
+ }
159
+ else if (is_undefined (_value))
160
+ {
161
+ buffer_write (_buffer, buffer_u8, 0x07 ); // <undefined>
162
+ }
163
+ else if (is_int32 (_value))
164
+ {
165
+ buffer_write (_buffer, buffer_u8, 0x08 ); // s32
166
+ buffer_write (_buffer, buffer_s32, _value);
167
+ }
168
+ else if (is_int64 (_value))
169
+ {
170
+ buffer_write (_buffer, buffer_u8, 0x09 ); // u64
171
+ buffer_write (_buffer, buffer_u64, _value);
172
+ }
173
+ else if (is_ptr (_value))
174
+ {
175
+ buffer_write (_buffer, buffer_u8, 0x0A ); // pointer
176
+ buffer_write (_buffer, buffer_u64, int64 (_value));
177
+ }
178
+ else if (typeof (_value) == " ref" ) // is_ref() doesn't exist as of 2022-10-23
179
+ {
180
+ buffer_write (_buffer, buffer_u8, 0x0B ); // instance ID reference
181
+ buffer_write (_buffer, buffer_u64, int64 (real (_value))); // Serialize the numeric part of the reference
182
+ }
183
+ else
184
+ {
185
+ show_message (" Datatype \" " + typeof (_value) + " \" not supported" );
186
+ }
187
+
188
+ return _buffer;
189
+ }
190
+
191
+
192
+
193
+
194
+
195
+ // Legacy version for LTS use
196
+ function __SnapBufferWriteBinaryLegacy (_buffer, _value, _alphabetizeStructs)
26
197
{
27
198
if (is_method (_value)) // Implicitly also a struct so we have to check this first
28
199
{
@@ -47,7 +218,7 @@ function SnapBufferWriteBinary(_buffer, _value, _alphabetizeStructs = false)
47
218
if (!is_string (_name)) show_error (" SNAP:\n Keys must be strings\n " , true );
48
219
49
220
buffer_write (_buffer, buffer_string, string (_name));
50
- SnapBufferWriteBinary (_buffer, _struct[$ _name], _alphabetizeStructs);
221
+ __SnapBufferWriteBinaryLegacy (_buffer, _struct[$ _name], _alphabetizeStructs);
51
222
52
223
++_i;
53
224
}
@@ -63,7 +234,7 @@ function SnapBufferWriteBinary(_buffer, _value, _alphabetizeStructs = false)
63
234
var _i = 0 ;
64
235
repeat (_count)
65
236
{
66
- SnapBufferWriteBinary (_buffer, _array[_i], _alphabetizeStructs);
237
+ __SnapBufferWriteBinaryLegacy (_buffer, _array[_i], _alphabetizeStructs);
67
238
++_i;
68
239
}
69
240
}
@@ -122,4 +293,4 @@ function SnapBufferWriteBinary(_buffer, _value, _alphabetizeStructs = false)
122
293
}
123
294
124
295
return _buffer;
125
- }
296
+ }
0 commit comments