FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
daScript/src/simulate/bin_serializer.cpp at master · nolankramer/daScript · GitHub
nolankramer
/
daScript
Public
forked from
GaijinEntertainment/daScript
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
daScript
/
src
/
simulate
/
bin_serializer.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
321 lines (313 loc) · 11.7 KB
Breadcrumbs
daScript
/
src
/
simulate
/
bin_serializer.cpp
Copy path
File metadata and controls
321 lines (313 loc) · 11.7 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#
include
"
daScript/misc/platform.h
"
#
include
"
daScript/simulate/bin_serializer.h
"
#
include
"
daScript/simulate/simulate.h
"
#
include
"
daScript/simulate/hash.h
"
namespace
das
{
#
ifndef
DEBUG_BIN_DATA
#
define
DEBUG_BIN_DATA
(...)
#
endif
struct
BinDataSerialize
: DataWalker {
char
* bytesAt =
nullptr
;
LineInfo * debugInfo =
nullptr
;
uint32_t
bytesAllocated =
0
;
uint32_t
bytesWritten =
0
;
uint32_t
bytesGrow =
1024
;
//
writer
BinDataSerialize
( Context & ctx, LineInfo * at ) {
DEBUG_BIN_DATA
(
"
writing
\n
"
);
context = &ctx;
debugInfo = at;
reading =
false
;
}
BinDataSerialize
( Context & ctx, LineInfo * at,
char
* b,
uint32_t
l ) {
context = &ctx;
debugInfo = at;
reading =
true
;
bytesAt = b;
bytesAllocated = l;
DEBUG_BIN_DATA
(
"
reading %i bytes
\n
"
, bytesAllocated);
}
__forceinline
void
read
(
void
* data,
uint32_t
size ) {
if
( bytesWritten + size <= bytesAllocated ) {
DEBUG_BIN_DATA
(
"
reading %i bytes at %i
\n
"
, size, bytesWritten );
memcpy
( data, bytesAt + bytesWritten, size );
bytesWritten += size;
}
else
{
error
(
"
binary data too short
"
);
}
}
__forceinline
void
write
(
void
* data,
uint32_t
size ) {
if
( bytesWritten + size > bytesAllocated ) {
uint32_t
newSize =
das::max
( bytesAllocated + bytesGrow, bytesWritten + size );
bytesAt = context->
reallocate
(bytesAt, bytesAllocated, newSize, debugInfo);
context->
heap
->
mark_comment
(bytesAt,
"
binary serializer write
"
);
bytesAllocated = newSize;
}
DEBUG_BIN_DATA
(
"
writing %i bytes at %i
\n
"
, size, bytesWritten );
memcpy
( bytesAt + bytesWritten, data, size );
bytesWritten += size;
}
template
<
typename
TT
>
__forceinline
void
save
(
TT
& data ) {
write
( &data,
sizeof
(
TT
) );
}
template
<
typename
TT
>
__forceinline
void
load
(
TT
& data ) {
read
( &data,
sizeof
(
TT
) );
}
template
<
typename
TT
>
__forceinline
void
serialize
(
TT
& data ) {
if
( reading ) {
read
( &data,
sizeof
(
TT
) );
}
else
{
write
( &data,
sizeof
(
TT
) );
}
}
__forceinline
void
verify
(
uint32_t
& data ) {
if
( reading ) {
uint32_t
test =
0
;
load
(test);
DEBUG_BIN_DATA
(
"
verify, reading %xi
\n
"
, test);
if
( test != data ) {
error
(
"
binary data type mismatch
"
);
}
}
else
{
DEBUG_BIN_DATA
(
"
verify, writing %xi
\n
"
, data);
save
(data);
}
}
__forceinline
void
verify_hash
(
uint64_t
& data ) {
if
( reading ) {
uint64_t
test =
0
;
load
(test);
DEBUG_BIN_DATA
(
"
verify, reading %xi
\n
"
, test);
if
( test != data ) {
error
(
"
binary data type mismatch
"
);
}
}
else
{
DEBUG_BIN_DATA
(
"
verify, writing %xi
\n
"
, data);
save
(data);
}
}
void
close
() {
if
( !reading && bytesAt ) {
DEBUG_BIN_DATA
(
"
close at %i bytes
\n\n
"
, bytesWritten);
bytesAt = context->
reallocate
(bytesAt, bytesAllocated, bytesWritten, debugInfo);
}
}
//
data structures
virtual
void
beforeStructure
(
char
*, StructInfo * si )
override
{
verify_hash
(si->
hash
);
}
virtual
void
beforeDim
(
char
*, TypeInfo * ti )
override
{
verify_hash
(ti->
hash
);
verify
(ti->
dimSize
);
}
virtual
void
beforeArray
( Array * pa, TypeInfo * ti )
override
{
verify_hash
(ti->
hash
);
if
( reading ) {
uint32_t
newSize =
0
;
load
(newSize);
array_clear
(*context, *pa,
/*
at
*/
nullptr
);
array_resize
(*context, *pa, newSize,
getTypeBaseSize
(ti),
true
,
/*
at
*/
nullptr
);
}
else
{
save
(pa->
size
);
}
}
virtual
void
beforeTable
( Table *, TypeInfo * )
override
{
error
(
"
binary serialization of tables is not supported
"
);
}
virtual
void
beforePtr
(
char
*, TypeInfo * )
override
{
error
(
"
binary serialization of pointers is not supported
"
);
}
virtual
void
beforeHandle
(
char
*, TypeInfo * ti )
override
{
verify_hash
(ti->
hash
);
}
//
types
virtual
void
String
(
char
* & data )
override
{
DEBUG_BIN_DATA
(
"
string
\n
"
);
if
( reading ) {
uint32_t
length =
0
;
load
( length );
char
* temp =
new
char
[length+
1
];
read
( temp, length );
data = (
char
*) context->
allocateString
(temp,length, debugInfo);
delete []
temp;
}
else
{
uint32_t
length =
stringLengthSafe
(*context, data);
save
( length );
write
( data, length );
}
}
virtual
void
Bool
(
bool
& data )
override
{
serialize
(data);
}
virtual
void
Int8
(
int8_t
& data )
override
{
serialize
(data);
}
virtual
void
UInt8
(
uint8_t
& data )
override
{
serialize
(data);
}
virtual
void
Int16
(
int16_t
& data )
override
{
serialize
(data);
}
virtual
void
UInt16
(
uint16_t
& data )
override
{
serialize
(data);
}
virtual
void
Int64
(
int64_t
& data )
override
{
serialize
(data);
}
virtual
void
UInt64
(
uint64_t
& data )
override
{
serialize
(data);
}
virtual
void
Double
(
double
& data )
override
{
serialize
(data);
}
virtual
void
Float
(
float
& data )
override
{
serialize
(data);
}
virtual
void
Int
(
int32_t
& data )
override
{
serialize
(data);
}
virtual
void
UInt
(
uint32_t
& data )
override
{
serialize
(data);
}
virtual
void
Bitfield
(
uint32_t
& data, TypeInfo * )
override
{
serialize
(data);
}
virtual
void
Bitfield8
(
uint8_t
& data, TypeInfo * )
override
{
serialize
(data);
}
virtual
void
Bitfield16
(
uint16_t
& data, TypeInfo * )
override
{
serialize
(data);
}
virtual
void
Bitfield64
(
uint64_t
& data, TypeInfo * )
override
{
serialize
(data);
}
virtual
void
Int2
( int2 & data )
override
{
serialize
(data);
}
virtual
void
Int3
( int3 & data )
override
{
serialize
(data);
}
virtual
void
Int4
( int4 & data )
override
{
serialize
(data);
}
virtual
void
UInt2
( uint2 & data )
override
{
serialize
(data);
}
virtual
void
UInt3
( uint3 & data )
override
{
serialize
(data);
}
virtual
void
UInt4
( uint4 & data )
override
{
serialize
(data);
}
virtual
void
Float2
( float2 & data )
override
{
serialize
(data);
}
virtual
void
Float3
( float3 & data )
override
{
serialize
(data);
}
virtual
void
Float4
( float4 & data )
override
{
serialize
(data);
}
virtual
void
Range
( range & data )
override
{
serialize
(data);
}
virtual
void
URange
( urange & data )
override
{
serialize
(data);
}
virtual
void
Range64
( range64 & data )
override
{
serialize
(data);
}
virtual
void
URange64
( urange64 & data )
override
{
serialize
(data);
}
virtual
void
FakeContext
( Context * )
override
{
DAS_ASSERT
(
0
&&
"
can't serialize context
"
);
}
virtual
void
WalkEnumeration
(
int32_t
& data, EnumInfo * ei )
override
{
verify_hash
(ei->
hash
);
serialize
(data);
}
virtual
void
WalkEnumeration8
(
int8_t
& data, EnumInfo * ei )
override
{
verify_hash
(ei->
hash
);
serialize
(data);
}
virtual
void
WalkEnumeration16
(
int16_t
& data, EnumInfo * ei )
override
{
verify_hash
(ei->
hash
);
serialize
(data);
}
virtual
void
WalkEnumeration64
(
int64_t
& data, EnumInfo * ei )
override
{
verify_hash
(ei->
hash
);
serialize
(data);
}
virtual
void
Null
( TypeInfo * )
override
{
error
(
"
binary serialization of null pointers is not supported
"
);
}
virtual
void
VoidPtr
(
void
* & )
override
{
error
(
"
binary serialization of void pointers is not supported
"
);
}
virtual
void
beforeIterator
( Sequence *, TypeInfo * )
override
{
error
(
"
binary serialization of iterators is not supported
"
);
}
virtual
void
WalkBlock
( Block * )
override
{
error
(
"
binary serialization of blocks is not supported
"
);
}
virtual
void
WalkFunction
( Func * func )
override
{
if
( reading ) {
uint64_t
mnh;
serialize
(mnh);
func->
PTR
= context->
fnByMangledName
(mnh);
}
else
{
uint64_t
mnh = func->
PTR
? func->
PTR
->
mangledNameHash
:
0
;
serialize
(mnh);
}
}
virtual
void
beforeLambda
( Lambda * lambda, TypeInfo * )
override
{
TypeInfo * info =
nullptr
;
if
( reading ) {
uint64_t
hash =
0
;
serialize
(hash);
info = context->
debugInfo
->
lookup
[hash];
//
TODO: verify if there is capture, all that
DAS_ASSERTF
(info,
"
type info not found. how did we get type, which is not in the typeinfo hash?
"
);
uint32_t
size =
getTypeSize
(info) +
16
;
char
* ptr = context->
allocate
(size);
context->
heap
->
mark_comment
(ptr,
"
lambda (via bin serializer)
"
);
memset
( ptr,
0
, size );
*((TypeInfo **)ptr) = info;
ptr +=
16
;
lambda->
capture
= ptr;
}
else
{
info = lambda->
getTypeInfo
();
serialize
(info->
hash
);
}
}
};
//
save ( obj, block<(bytesAt)> )
vec4f
_builtin_binary_save
( Context & context, SimNode_CallBase * call, vec4f * args ) {
BinDataSerialize
writer
(context, &call->
debugInfo
);
//
args
Block * block = cast<Block *>::
to
(args[
1
]);
auto
info = call->
types
[
0
];
writer.
walk
(args[
0
], info);
writer.
close
();
Array arr;
array_mark_locked
(arr, writer.
bytesAt
, writer.
bytesWritten
);
vec4f arg = cast<
char
*>::
from
((
char
*)&arr);
context.
invoke
(*block, &arg,
nullptr
, &call->
debugInfo
);
return
v_zero
();
}
//
load ( obj, bytesAt )
void
_builtin_binary_load
( Context & context, LineInfo * at, TypeInfo* info,
const
char
*data,
uint32_t
len,
char
*to) {
if
( !(info->
flags
&(TypeInfo::flag_refType | TypeInfo::flag_ref)) )
return
;
BinDataSerialize
reader
(context, at,
const_cast
<
char
*>(data), len);
reader.
walk
(to, info);
}
vec4f
_builtin_binary_load
( Context & context, SimNode_CallBase * call, vec4f * args ) {
Array * ba = cast<Array *>::
to
(args[
1
]);
_builtin_binary_load
(context, &call->
debugInfo
, call->
types
[
0
], ba->
data
, ba->
size
, cast<
char
*>::
to
(args[
0
]));
return
v_zero
();
}
}
Back
|
FazBrowse Home
|
New Git URL