-
Notifications
You must be signed in to change notification settings - Fork 2
/
Generator.cpp
325 lines (317 loc) · 8.49 KB
/
Generator.cpp
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
#include "precomp.h"
#include "interfaceData.h"
#include "ckall.h"
using namespace std;
class Generator
{
private:
char* genbuffer;
size_t genbuffer_length;
size_t genbuffer_currsor;
void buffer_ensure_space(size_t size_in_char)
{
size_t needed = this->genbuffer_currsor + size_in_char;
if (needed > this->genbuffer_length) {
// if ordered buffer too low, allocated 512 DWORD directly
needed = this->genbuffer_currsor + std::max(size_in_char, 1024u * sizeof(DWORD));
// create new buffer
char* newbuf = new char[needed];
// if current buffer is not null, copy existed data
if (genbuffer != nullptr)
{
std::memcpy(newbuf, genbuffer, genbuffer_length);
}
// free old one and assign new one
delete[] genbuffer;
genbuffer = newbuf;
genbuffer_length = needed;
}
}
void buffer_write_data(const void* data, size_t datasize_in_char) {
buffer_ensure_space(datasize_in_char);
std::memcpy(genbuffer + genbuffer_currsor, data, datasize_in_char);
genbuffer_currsor += datasize_in_char;
}
void* buffer_get_dataptr(size_t off)
{
assert(off < genbuffer_length);
return genbuffer + off;
}
size_t buffer_tell(void)
{
return genbuffer_currsor;
}
public:
char* buffer_getter()
{
return genbuffer;
}
private:
interface_t &m_data;
void generate_int(int data)
{
buffer_write_data(&data, sizeof(data));
}
void generate_float(float data)
{
buffer_write_data(&data, sizeof(data));
}
void generate_char(char data)
{
buffer_write_data(&data, sizeof(data));
}
void generate_char_array(char* pArray, size_t nArraySize)
{
buffer_write_data(pArray, nArraySize);
}
void generate_rect(rect_t &data)
{
generate_float(data.h_pos);
generate_float(data.v_pos);
generate_float(data.h_size);
generate_float(data.v_size);
}
public:
Generator(interface_t &data) :
m_data(data),
genbuffer(nullptr), genbuffer_length(0u), genbuffer_currsor(0u)
{
// allocate buffer first
buffer_ensure_space(1048576u);
}
~Generator()
{
if (genbuffer != nullptr) delete[] genbuffer;
}
void generate_point(point_t &pnt)
{
generate_int(pnt.h);
generate_int(pnt.v);
}
void generate_link(link_t &lnk)
{
generate_int(lnk.type);
generate_int(lnk.id);
generate_int(lnk.start.id);
generate_int(lnk.start.index);
generate_int(lnk.start.type);
generate_int(lnk.point_count);
for(int i=0;i<lnk.point_count;++i)
generate_point(lnk.points[i]);
generate_int(lnk.end.id);
generate_int(lnk.end.index);
generate_int(lnk.end.type);
}
void generate_op(op_t &op)
{
generate_int(op.id);
generate_float(op.h_pos);
generate_float(op.v_pos);
}
void generate_io(int v,int isin)
{
generate_int(v);
generate_int(isin?-1:1);
}
void generate_link_op_comment_localp_sharedp(bb_t &bb)
{
generate_int(bb.links.size());
for(size_t i=0;i<bb.links.size();++i)
generate_link(bb.links[i]);
generate_int(bb.ops.size());
for(size_t i=0;i<bb.ops.size();++i)
generate_op(bb.ops[i]);
generate_int(0);//no comments anyway
generate_int(bb.local_params.size());
for(size_t i=0;i<bb.local_params.size();++i)
{
generate_int(bb.local_params[i].h_pos);
generate_int(bb.local_params[i].v_pos);
}
for(size_t i=0;i<bb.local_params.size();++i)
generate_int(bb.local_params[i].style);
generate_int(bb.shared_params.size());
for(size_t i=0;i<bb.shared_params.size();++i)
{
generate_int(bb.shared_params[i].h_pos);
generate_int(bb.shared_params[i].v_pos);
}
for(size_t i=0;i<bb.shared_params.size();++i)
generate_int(bb.shared_params[i].style);
for(size_t i=0;i<bb.shared_params.size();++i)
generate_int(bb.shared_params[i].source_id);
}
void generate_start()
{
generate_int(m_data.start.id);
for (int i = 0; i < 3; ++i)
generate_int(0);
generate_float(m_data.start.v_start);
generate_float(m_data.start.h_start_pos);
generate_float(m_data.start.v_start_pos);
generate_float(m_data.start.v_size);
for (int i = 0; i < 2; ++i)
generate_int(0);
}
void generate_bb(bb_t &bb)
{
generate_int(bb.id);
generate_int(bb.folded ? 0x200 : 0x0);
generate_int(bb.depth);
generate_rect(bb.size);
generate_float(bb.h_expand_size);
generate_float(bb.v_expand_size);
if (!bb.is_bg)
{
generate_int(0);
generate_int(0);
generate_int(0);
}
else
{
generate_link_op_comment_localp_sharedp(bb);
generate_int(bb.inward_inputs.size());
for(size_t i=0;i<bb.inward_inputs.size();++i)
generate_io(bb.inward_inputs[i],1);
generate_int(bb.outward_inputs.size());
for(size_t i=0;i<bb.outward_inputs.size();++i)
generate_io(bb.outward_inputs[i],1);
generate_int(bb.inward_outputs.size());
for(size_t i=0;i<bb.inward_outputs.size();++i)
generate_io(bb.inward_outputs[i],0);
generate_int(bb.outward_outputs.size());
for(size_t i=0;i<bb.outward_outputs.size();++i)
generate_io(bb.outward_outputs[i],0);
}
}
void generate_pos_info()
{
generate_int(m_data.n_bb + 1 +
m_data.script_root.n_links + m_data.script_root.n_ops +
m_data.script_root.n_comments + m_data.script_root.n_local_param +
m_data.script_root.n_shared_param); // tot obj count
generate_int(0);
generate_int(0);
generate_int(1);
generate_int(0); // size-related-thing, unknown yet
generate_int(0x16);
generate_int(m_data.n_bb + 1);
generate_start();
generate_int(0xc8c8c8);
generate_link_op_comment_localp_sharedp(m_data.script_root);
for (int i = 0; i < m_data.n_bb; ++i)
{
generate_bb(m_data.bbs[i]);
}
}
void generate_interface()
{
generate_int(0xFFFFFFFF);
generate_int(0x70000);
//int *length = (int *)p;
size_t offLength = buffer_tell();
generate_int(-1);
generate_int(0);
//char *begin = p;
size_t offBegin = buffer_tell();
generate_pos_info();
//*length = (p - begin) / 4;
*((int*)buffer_get_dataptr(offLength)) = (buffer_tell() - offBegin) / 4;
generate_int(0x54543B94);
generate_int(0);
generate_int(0);
int n_obj_fake = m_data.n_bb + 1 +
m_data.script_root.n_links + m_data.script_root.n_ops +
m_data.script_root.n_comments + m_data.script_root.n_local_param +
m_data.script_root.n_shared_param;
for (int i = 0; i < n_obj_fake; ++i)
{
generate_int(0);
}
}
void generate_body()
{
//int *length = (int *)p;
size_t offLength = buffer_tell();
generate_int(-1);
//char *begin = p;
size_t offBegin = buffer_tell();
generate_interface();
//*length = (p - begin) / 4;
*((int*)buffer_get_dataptr(offLength)) = (buffer_tell() - offBegin) / 4;
generate_int(0x20);
generate_int(0);
}
void generate_behavior_data()
{
generate_int(0x10);
//int *length = (int *)p;
size_t offLength = buffer_tell();
generate_int(-1);
//char *begin = p;
size_t offBegin = buffer_tell();
generate_body();
//*length = (p - begin) / 4;
*((int*)buffer_get_dataptr(offLength)) = (buffer_tell() - offBegin) / 4;
//for (int i = 0; i < m_data.tail_length; ++i)
//{
// generate_char(m_data.tail[i]);
//}
generate_char_array(m_data.tail, m_data.tail_length);
}
void generate_all()
{
char *none = 0;
generate_int(m_data.class_id);
//int *length = (int *)p;
size_t offLength = buffer_tell();
generate_int(-1);
//char *begin = p;
size_t offBegin = buffer_tell();
generate_behavior_data();
//*length = (p - begin) / 4;
*((int*)buffer_get_dataptr(offLength)) = (buffer_tell() - offBegin) / 4;
generate_int(0x1);
generate_int(0x2); // Some padding
}
int Generate()
{
generate_all();
return buffer_tell() / 4;
}
};
void generate_bb_test(interface_t &interface_data, CKBehavior *bb, CKFile *file)
{
Generator generator = Generator(interface_data);
//char *buffer = new char[1048576];
int length = generator.Generate();
#if defined(VSD_ENABLE_LOG) // yyc mark: only enable logger on Debug config
char filename[1024];
char name[1024];
strcpy(name, bb->GetName());
for (int i = strlen(name) - 1; i >= 0; --i)
{
if (name[i] >= 'a' && name[i] <= 'z') continue;
if (name[i] >= 'A' && name[i] <= 'Z') continue;
if (name[i] >= '0' && name[i] <= '9') continue;
if (name[i] == '.' || name[i] == '_') continue;
name[i] = '_';
}
sprintf(filename, "%s/generator_out_%s.log", VSDTempFolderGenerator, name);
FILE *fout = fopen(filename, "wb");
char* buffer = generator.buffer_getter();
for (int i = 0; i < length * 4; ++i)
{
fputc(buffer[i], fout);
}
fclose(fout);
#endif
extern void parse_string_test(CKBehavior *bb, char *buffer);
parse_string_test(bb, generator.buffer_getter());
CKStateChunk *chunk = CreateCKStateChunk((CK_CLASSID)0x0);
chunk->Clear();
chunk->ConvertFromBuffer(generator.buffer_getter());
bb->Load(chunk, file);
bb->PostLoad();
//delete[]buffer;
}