-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerator.py
executable file
·107 lines (81 loc) · 4.25 KB
/
generator.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
from components.version import create_versions
from components.name_utils import process_name, process_array_size, process_enum_name
from components.enums import get_enums
from components.bitfields import get_bitfields
from components.compounds import get_compounds
from components.objects import get_module_names, get_module
from components.expression import Expression
from components.generator_context import GeneratorContext, get_basic_types, get_pointer_types, get_string_types
from components.typedefs import get_typedefs
from components.factory import FactoryMap
from util.replace_tokens import replace_tokens
import os
import xml.dom
import xml.dom.minidom
import sys
if __name__ == '__main__':
path = 'nif.xml'
if os.path.exists(path):
document = xml.dom.minidom.parse(path)
else:
raise ImportError("nif.xml not found")
# Replace all tokens in attributes with values from <token> elements.
document = replace_tokens(document)
# Get a version object for each engine version for a game.
versions = create_versions(document, 'Fallout NV')
for version in versions:
if not os.path.exists(version.id):
os.mkdir(version.id)
# Create a context for this version's code.
context = GeneratorContext(version, get_basic_types(), get_string_types(), get_pointer_types())
# Write the typedefs file.
typedefs = get_typedefs()
with open(str(version.id) + '/typedefs.hpp', 'w', encoding='utf-8') as f:
f.write(typedefs)
# Parse and register the enum types.
# Write the enums to a file.
enums = get_enums(context, document, version, 'NiEnums')
with open(str(version.id) + '/enums.hpp', 'w', encoding='utf-8') as f:
f.write(enums)
# Parse and register the bitfield types.
# Write the bitfields to a file.
bitfields = get_bitfields(context, document, version)
with open(str(version.id) + '/bitfields.hpp', 'w', encoding='utf-8') as f:
f.write(bitfields)
# Parse and register the compound types.
# Write the compound headers to a file.
compounds_hpp, compounds_cpp = get_compounds(context, document, version)
with open(str(version.id) + '/structs.hpp', 'w', encoding='utf-8') as f:
f.write(compounds_hpp)
# Write the compounds compilaton unit.
with open(str(version.id) + '/structs.cpp', 'w', encoding='utf-8') as f:
f.write(compounds_cpp)
# Get the names of all available modules.
module_names = get_module_names(document)
factory = FactoryMap()
for module_name in module_names:
# Parse the object types in the module.
module = get_module(context, document, version, module_name)
if not os.path.exists(str(version.id) + '/' + module_name):
os.mkdir(str(version.id) + '/' + module_name)
# Register the module for inclusion.
factory.append_module(module_name)
module_header = ''
for name, hpp, cpp in module.get_module_codes():
# Register factory method.
factory.append_mapping(name)
# Register object in module-level include file.
module_header += '#include "' + name + '"\n'
# Write the header and compilation unit code of the object.
with open(str(version.id) + '/' + module_name + '/' + name + '.hpp', 'w', encoding='utf-8') as f:
f.write(hpp)
with open(str(version.id) + '/' + module_name + '/' + name + '.cpp', 'w', encoding='utf-8') as f:
f.write(cpp)
# Write the module-level include file.
with open(str(version.id) + '/' + module_name + '/' + module_name + '.hpp', 'w', encoding='utf-8') as f:
f.write(module_header)
# Write the map of factory methods.
with open(str(version.id) + '/factory.hpp', 'w', encoding='utf-8') as f:
f.write(factory.get_hpp())
with open(str(version.id) + '/factory.cpp', 'w', encoding='utf-8') as f:
f.write(factory.get_cpp())