-
Notifications
You must be signed in to change notification settings - Fork 1
/
CBSE.py
executable file
·628 lines (480 loc) · 22.6 KB
/
CBSE.py
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
#!/usr/bin/env python
# Daemon CBSE Source Code
# Copyright (c) 2014-2015, Daemon Developers
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# * Neither the name of Daemon CBSE nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import jinja2
import yaml
from collections import namedtuple, OrderedDict
import argparse, sys
import os.path
import re
class CommonAttribute:
def __init__(self, name, typ):
self.name = name
self.typ = typ
def get_declaration(self):
return self.typ + " " + self.name
def get_initializer(self):
return self.name + "(" + self.name + ")"
def get_name(self):
return self.name
class Message:
def __init__(self, name, args):
self.name = name
self.args = args
def get_name(self):
return self.name
def get_num_args(self):
return len(self.args)
def get_handler_name(self):
return 'Handle' + self.name
def get_function_args(self):
return ', '.join([arg[1] + ' ' + arg[0] for arg in self.args])
def get_handler_declaration(self):
return self.get_handler_name() + '(' + self.get_function_args() + ')'
def get_return_type(self):
return 'void';
def get_tuple_type(self):
if self.args == []:
return 'std::nullptr_t'
return 'std::tuple<{}>'.format(', '.join(list(zip(*self.args))[1]))
def get_arg_names(self):
return [arg[0] for arg in self.args]
# TODO: Rename to something that doesn't clash as much with get_arg_names
def get_args_names(self):
return ', '.join(self.get_arg_names())
def get_unpacked_tuple_args(self, tuple_name):
return ', '.join(['std::get<' + str(i) + '>(' + tuple_name +')' for i in range(len(self.args))])
def __repr__(self):
return 'Message({}, {})'.format(self.name, self.args)
Parameter = namedtuple('Parameter', ['name', 'typ', 'default'])
class Component:
def __init__(self, name, parameters=None, messages=None, requires=None, inherits=None, defaults=None):
self.name = name
self.param_list = []
self.parameters = OrderedDict()
# Create the list of parameters, with potential default parameters
for (name, typ) in parameters.items():
p = None
if name in defaults:
p = Parameter(name, typ, defaults[name])
else:
p = Parameter(name, typ, None)
self.param_list.append(p)
self.parameters[name] = p
self.param_list = sorted(self.param_list, key = lambda param: param.name)
self.messages = messages
self.priority = None
# First level of required components
self.requiredComponents = requires.keys() # Only names for now.
self.requiredParameters = requires
self.inherits = inherits
# List of required components of level n >= 2, contains pairs in
# the form dependency => firstLevel so that firstLevel is on a path
# from this to dependency
self.furtherDependencies = OrderedDict()
def gather_messages(self, messages):
self.messages = [messages[m] for m in self.messages]
def gather_component_dependencies(self, components):
self.requiredComponents = [components[c] for c in self.requiredComponents]
self.inherits = [components[c] for c in self.inherits]
def gather_full_dependencies(self):
registered = set(self.requiredComponents)
for firstLevel in self.requiredComponents:
for dependency in firstLevel.get_full_dependencies():
if dependency in registered:
continue
registered.add(dependency)
self.furtherDependencies[dependency] = firstLevel
def get_full_dependencies(self):
return self.requiredComponents + list(self.furtherDependencies.keys())
def for_each_component_dependencies(self, fun):
for require in self.requiredComponents:
fun(require)
for inherit in self.inherits:
fun(inherit)
def get_name(self):
return self.name
def get_type_name(self):
return self.name + "Component"
def get_base_type_name(self):
return self.name + "ComponentBase"
def get_variable_name(self):
return "c_" + self.get_type_name()
def get_priority(self):
return self.priority
def get_messages_to_handle(self):
return self.messages
def get_param_declarations(self):
return [p.typ + ' ' + p.name for p in self.param_list]
def get_own_param_declarations(self):
#TODO
return [p.typ + ' ' + p.name for p in self.param_list]
def get_param_names(self):
return [p.name for p in self.param_list]
def get_own_param_names(self):
#TODO
return [p.name for p in self.param_list]
def get_required_components(self):
return self.requiredComponents
def get_required_parameters(self):
return self.requiredParameters
def get_own_required_components(self):
#TODO
return self.requiredComponents
def get_required_component_declarations(self):
return [c.get_type_name() + '& r_' + c.get_type_name() for c in self.requiredComponents]
def get_own_required_component_declarations(self):
#TODO
return [c.get_type_name() + '& r_' + c.get_type_name() for c in self.requiredComponents]
def get_required_component_names(self):
return ['r_' + c.get_type_name() for c in self.requiredComponents]
def get_own_required_component_names(self):
#TODO
return ['r_' + c.get_type_name() for c in self.requiredComponents]
def get_own_further_dependencies(self):
return self.furtherDependencies
def __repr__(self):
return "Component({}, ...)".format(self.name)
class Entity:
def __init__(self, name, params):
self.name = name
self.params = params
def gather_components(self, components, mandatory):
self.components = [components[c] for c in mandatory] + \
[components[c] for c in self.params.keys() if c not in mandatory]
# Add dependency components.
# TODO more efficient algorithm? (this is worst case O(nComponents)^3)
gathering_dependencies = True
while gathering_dependencies:
gathering_dependencies = False
for component in self.components:
for dependency in component.get_required_components():
if dependency not in self.components:
self.components.append(dependency)
gathering_dependencies = True
self.components = sorted(self.components, key = lambda component: component.priority)
# Gather the list of all messages this entity can receive
self.messages = set()
for component in self.components:
self.messages |= set(component.get_messages_to_handle())
self.messages = list(self.messages)
# Initialize the parameter state
self.user_params = OrderedDict()
self.has_user_params = False
for component in self.components:
self.user_params[component.name] = OrderedDict()
if not component.name in self.params:
self.params[component.name] = OrderedDict()
# Choose the value for each component parameter (unless it is a user defined param)
for component in self.components:
for param in component.param_list:
if not param.name in self.params[component.name]:
# Use the parameters own default value, if it has one, as fallback
self.params[component.name][param.name] = param.default
# Look for a value given by another component that depends on the current one
dependency_sets_parameter = False
for dependent in self.components:
required_parameters = dependent.get_required_parameters()
if component.name not in required_parameters:
continue
if required_parameters[component.name] is None:
continue
for required_param, required_value in required_parameters[component.name].items():
if param.name != required_param:
continue
if dependency_sets_parameter:
raise Exception("Multiple components set a default value for the same parameter of a required component.")
self.params[component.name][param.name] = required_value
dependency_sets_parameter = True
# If we still have not found a value, let the user decide
if not param.name in self.params[component.name]:
self.params[component.name][param.name] = None
# Let the user decide on the value
if self.params[component.name][param.name] == None:
self.params[component.name].pop(param.name)
self.user_params[component.name][param.name] = param
self.has_user_params = True
def get_type_name(self):
return self.name + "Entity"
def get_components(self):
return self.components
def get_params(self):
return self.params
def get_user_params(self):
return self.user_params
def get_has_user_params(self):
return self.has_user_params
def get_messages_to_handle(self):
return self.messages
def get_message_handler_name(self, message):
return "h_" + self.name + "_" + message.name
#############################################################################
def convert_params(params):
# Convert defaults to strings that C++ understands.
for param, value in params.items():
if type(value) == bool:
value = str(value).lower()
elif value is None:
pass
else:
value = str(value)
params[param] = value
def load_general(definitions):
if definitions is None:
definitions = {}
common_entity_attributes = []
mandatory_components = []
if 'mandatory_components' in definitions:
mandatory_components += definitions['mandatory_components']
if 'common_entity_attributes' in definitions:
for attrib in definitions['common_entity_attributes']:
common_entity_attributes.append(CommonAttribute(attrib['name'], attrib['type']))
return namedtuple('general', ['common_entity_attributes', 'mandatory_components']) \
(common_entity_attributes, mandatory_components)
def load_messages(definitions):
if definitions is None:
definitions = {}
messages = OrderedDict()
for (name, args) in definitions.items():
if args == None:
args = []
messages[name] = Message(name, [(arg['name'], arg['type']) for arg in args])
return messages
def load_components(definitions):
if definitions is None:
definitions = {}
components = OrderedDict()
for (name, kwargs) in definitions.items():
if kwargs is None:
kwargs = {}
if not 'messages' in kwargs:
kwargs['messages'] = []
if not 'parameters' in kwargs:
kwargs['parameters'] = OrderedDict()
if not 'defaults' in kwargs:
kwargs['defaults'] = OrderedDict()
if not 'requires' in kwargs:
kwargs['requires'] = OrderedDict()
if not 'inherits' in kwargs:
kwargs['inherits'] = OrderedDict()
else:
raise Exception("inherits not handled for now")
convert_params(kwargs['defaults'])
for component in kwargs['requires'].keys():
if kwargs['requires'][component] is not None:
convert_params(kwargs['requires'][component])
components[name] = Component(name, **kwargs)
return components
def load_entities(definitions):
if definitions is None:
definitions = {}
entities = OrderedDict()
for (name, kwargs) in definitions.items():
if not 'components' in kwargs or kwargs['components'] == None:
kwargs['components'] = OrderedDict()
for component_name, component_params in kwargs['components'].items():
if component_params != None:
convert_params(component_params)
else:
kwargs['components'][component_name] = OrderedDict()
entities[name] = Entity(name, kwargs['components'])
return entities
############################################################################
def topo_sort_components(components):
sorted_components = []
NOT_VISITED, VISITING, VISITED = range(0, 3)
for component in components:
component.temp_visited = NOT_VISITED
def handle_component(component):
if component.temp_visited == VISITING:
raise 'The component dep graph has a cycle'
elif component.temp_visited == NOT_VISITED:
component.temp_visited = VISITING
component.for_each_component_dependencies(handle_component)
component.temp_visited = VISITED
sorted_components.append(component)
for component in components:
handle_component(component)
for component in components:
del component.temp_visited
return sorted_components
def parse_definitions(definitions):
# Do the basic loading of objects, independently of other objects
general = load_general(definitions['general'])
messages = load_messages(definitions['messages'])
message_list = list(messages.values())
components = load_components(definitions['components'])
component_list = list(components.values())
entities = load_entities(definitions['entities'])
entity_list = list(entities.values())
# Link objects together
# First make each component get its dependencies and then compute their topological order
for component in component_list:
component.gather_component_dependencies(components)
sorted_components = topo_sort_components(component_list)
for (i, component) in enumerate(sorted_components):
component.priority = i
for component in sorted_components:
component.gather_full_dependencies();
for component in sorted_components:
component.gather_messages(messages)
for entity in entity_list:
entity.gather_components(components, general.mandatory_components)
definitions = {
'general': general,
'messages': message_list,
'components': sorted_components,
'entities': entity_list,
}
return definitions
############################################################################
# A custom Jinja2 template loader that removes the extra indentation
# of the template blocks so that the output is correctly indented
class PreprocessingLoader(jinja2.BaseLoader):
def __init__(self, path):
self.path = path
def get_source(self, environment, template):
path = os.path.join(self.path, template)
if not os.path.exists(path):
raise jinja2.TemplateNotFound(template)
mtime = os.path.getmtime(path)
with open(path) as f:
source = self.preprocess(f.read())
return source, path, lambda: mtime == os.path.getmtime(path)
def preprocess(self, source):
# Remove the trailing newline
lines = source.split("\n")
# Compute the current indentation level of the template blocks and remove their indentation
result = []
indentation_level = 0
for line in lines:
if self.blockstart.match(line) != None:
indentation_level += 1
elif self.blockend.match(line) != None:
indentation_level -=1
result.append(self.remove_indentation(line, indentation_level))
return self.my_filter('\n'.join(result))
def remove_indentation(self, line, n):
for _ in range(n):
if line.startswith(' '):
line = line[4:]
elif line.startswith('\t'):
line = line[1:]
return line
blockstart = re.compile('^\s*{%-?\s*(if|for).*$')
blockend = re.compile('^\s*{%-?\s*end(if|for).*$')
def my_filter(self, text):
lines = []
for line in text.split('\n'):
# Remove the "template" comments
if line.strip().startswith("//*"):
continue
lines.append(line)
return '\n'.join(lines)
# A yaml loader that uses OrderedDict instead of dict internally.
class OrderedLoader(yaml.Loader):
@staticmethod
def OrderedMapping(loader, node):
loader.flatten_mapping(node)
return OrderedDict(loader.construct_pairs(node))
def __init__(self, stream):
yaml.Loader.__init__(self, stream)
self.add_constructor(yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG, self.OrderedMapping)
if __name__ == '__main__':
# Command line args
parser = argparse.ArgumentParser(
description="Outputs C++ plumbing code for the gamelogic given a component/entity definition file.",
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument('definitions', metavar='DEFINITION_FILE', type=open, help ="The definitions to use, - for stdin.")
parser.add_argument('-t', '--template-dir', default="templates", type=str, help="Directory with template files.")
parser.add_argument('-o', '--output-dir', default=None, type=str, help="Output directory for the generated source files.")
parser.add_argument('-s', '--skeletons', action="store_true", help="Put latest skeleton files in a subfolder.")
parser.add_argument('-p', '--header', default=None, type=argparse.FileType('r'), help="Prepend file contents as a header to all source files.")
args = parser.parse_args()
# Load everything from the definition file
definitions = parse_definitions(yaml.load(args.definitions, OrderedLoader))
args.definitions.close()
# Manage output directories and files
Output = namedtuple('Output', ['template', 'subdir', 'outname', 'overwrite'])
subdir_names = {
'backend': 'backend',
'components': 'components',
'skeletons': 'skeletons'
}
unique_files = {
'backend': Output('Backend.h', subdir_names['backend'], 'CBSEBackend.h', True),
'backend_cpp': Output('Backend.cpp', subdir_names['backend'], 'CBSEBackend.cpp', True),
'components': Output('Components.h', subdir_names['backend'], 'CBSEComponents.h', True),
'entities': Output('Entities.h', subdir_names['backend'], 'CBSEEntities.h', True),
'helper': Output('Helper.h', '', 'CBSE.h', False)
}
template_params = [definitions, {
'files': dict([(key, val.outname) for (key, val) in unique_files.items()]),
'dirs': subdir_names
}]
if args.header != None:
template_params += [{'header': args.header.read().strip().splitlines()}]
if args.output_dir != None:
base_dir = args.output_dir + os.path.sep
components_dir = base_dir + subdir_names['components'] + os.path.sep
skeletons_dir = components_dir + subdir_names['skeletons'] + os.path.sep
# Generate a list of files to create, params_dicts will get squashed to create the template parameters
FileToRender = namedtuple('FileToRender', ['template', 'output', 'params_dicts', 'overwrite'])
to_render = []
# Add unique files
for (key, val) in unique_files.items():
to_render.append(FileToRender(val.template, base_dir + val.subdir + os.path.sep + val.outname, \
template_params, val.overwrite))
# Add skeleton files
for component in definitions['components']:
params = template_params + [{'component': component}]
if args.skeletons:
to_render.append(FileToRender('Component.h', skeletons_dir + component.get_type_name() + '.h', params, True))
to_render.append(FileToRender('Component.cpp', skeletons_dir + component.get_type_name() + '.cpp', params, True))
to_render.append(FileToRender('Component.h', components_dir + component.get_type_name() + '.h', params, False))
to_render.append(FileToRender('Component.cpp', components_dir + component.get_type_name() + '.cpp', params, False))
# Now render the files
env = jinja2.Environment(loader=PreprocessingLoader(args.template_dir), trim_blocks=True, lstrip_blocks=True)
env.globals["enumerate"] = enumerate
for render in to_render:
if not render.overwrite and os.path.exists(render.output):
continue
params = OrderedDict()
for param_dict in render.params_dicts:
params.update(param_dict)
content = env.get_template(render.template).render(**params) + "\n"
directory = os.path.dirname(render.output)
if not os.path.exists(directory):
os.makedirs(directory)
with open(render.output, 'w') as outfile:
outfile.write(content)
if not render.overwrite:
print('Added file, ' + render.output)
# vi:ts=4:et:ai