-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.py
299 lines (231 loc) · 8.31 KB
/
convert.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
import definitions
import functions
import lark
import argparse
import collections
import logging
import re
import sys
GRAMMAR = """
start: (define | comment1 | comment2 | NEWLINE)*
define: "#define" name expression
name: /[a-zA-Z_][a-zA-Z0-9_]*/
expression: number | string | division | brackets | function
number: /-?\d+(\.\d+)?/
string: /".*?(?<!\\\\)"/ | name
division: expression "/" expression
brackets: "(" expression ")"
function: name "(" expression ("," expression)* ")"
comment1: "//" /.+/?
comment2: "/*" /.*?\*\//s
%ignore " "
%ignore "\\t"
%import common.NEWLINE
"""
class Transformer(lark.Transformer):
def __init__(self):
self.used_effect_names = []
self.unknown_uses = {}
self.parameters_by_effect_name = {}
self.unused_defines_by_effect_name = {}
self.any_warning = False
def start(self, children):
pass
def define(self, children):
name, value = children
if name.startswith("USE_"):
effect_name = definitions.effect_name_by_use.get(name, None)
if effect_name is None:
self.unknown_uses[name] = bool(evaluate(value))
return
if evaluate(value) == 0:
return
self.used_effect_names.append(effect_name)
return
reshade_parameter_target = definitions.reshade_parameter_target_by_reshade_define_name.get(
name, None
)
if reshade_parameter_target is not None:
if reshade_parameter_target.parameter_name is None:
self.unused_defines_by_effect_name.setdefault(
reshade_parameter_target.effect_name, {}
)[name] = value
return
parameters = self.parameters_by_effect_name.setdefault(
reshade_parameter_target.effect_name, {}
)
if reshade_parameter_target.parameter_index is None:
parameters[reshade_parameter_target.parameter_name] = value
else:
values = parameters.setdefault(
reshade_parameter_target.parameter_name, Array()
)
self._ensure_capacity(
values, reshade_parameter_target.parameter_index + 1
)
values[reshade_parameter_target.parameter_index] = value
return
logging.warning("unused define: {} = {}".format(name, value))
self.any_warning = True
def _ensure_capacity(self, x, length):
x.extend(None for _ in range(length - len(x)))
def expression(self, children):
result, = children
return result
def function(self, children):
name = children[0]
str_ = lambda: "{}({})".format(
children[0], ", ".join(str(expression) for expression in children[1:])
)
if name == "float":
def evaluate_():
expression, = children[1:]
value = evaluate(expression)
result = float(value)
return result
result = Expression(str_, evaluate_)
return result
if name in ["float2", "float3"]:
n = 2 if name == "float2" else 3
def evaluate_():
expressions = children[1:]
assert len(expressions) == n
result = Array(
float(evaluate(expression)) for expression in expressions
)
return result
result = Expression(str_, evaluate_)
return result
result = Expression(
str_,
lambda: getattr(functions, children[0])(
[evaluate(child) for child in children[1:]]
),
)
return result
def division(self, children):
numerator, denominator = children
result = Expression(
lambda: "{} / {}".format(numerator, denominator),
lambda: evaluate(numerator) / evaluate(denominator),
)
return result
def brackets(self, children):
expression, = children
result = Expression(
lambda: "({})".format(expression), lambda: evaluate(expression)
)
return result
def name(self, children):
token, = children
result = token.value
return result
def number(self, children):
token, = children
value = token.value
# result = float(result) if '.' in result else int(result)
result = Expression(
lambda: value, lambda: (float if "." in value else int)(value)
)
return result
def string(self, children):
result, = children
if isinstance(result, lark.Token):
result = result.value
else:
result = '"{}"'.format(result)
return result
def comment1(self, children):
pass
def comment2(self, children):
pass
def __default__(self, data, children, meta):
raise NotImplementedError(data)
class Expression:
def __init__(self, str_function, evaluate_function):
self.str_function = str_function
self.evaluate_function = evaluate_function
def evaluate(self):
result = self.evaluate_function()
return result
def __str__(self):
result = self.str_function()
return result
def __repr__(self):
result = str(self)
return result
class Array(list, Expression):
def __init__(self, items=None):
list.__init__(self, [] if items is None else items)
Expression.__init__(self, self._str, self._evaluate)
def _evaluate(self):
result = Array(evaluate(value) for value in self)
return result
def _str(self):
result = ",".join(str(value) for value in self)
return result
def evaluate(x):
if isinstance(x, Expression):
result = x.evaluate()
return result
return x
logging.basicConfig(level="INFO")
# parse arguments
parser = argparse.ArgumentParser()
parser.parse_args()
#
parser = lark.Lark(GRAMMAR)
tree = parser.parse(sys.stdin.read())
transformer = Transformer()
transformer.transform(tree)
any_warning = False
if len(transformer.unknown_uses) != 0:
unknown_uses = {name for name, used in transformer.unknown_uses.items() if used}
if len(unknown_uses):
logging.warning("unknown uses: {}".format(repr(unknown_uses)))
any_warning = True
unknown_uses = {name for name, used in transformer.unknown_uses.items() if not used}
if len(unknown_uses):
logging.info("unknown uses: {}".format(repr(unknown_uses)))
print("Effects", "=", ",".join(sorted(transformer.used_effect_names)), sep="")
all_techniques = []
for effect_name in transformer.used_effect_names:
techniques = definitions.techniques_by_effect_name.get(effect_name, [])
if 1 < len(techniques):
logging.warning(
"multiple techniques for effect {}; might need disabling; techniques: {}".format(
repr(effect_name), repr(techniques)
)
)
any_warning = True
all_techniques.extend(techniques)
print("Techniques", "=", ",".join(all_techniques), sep="")
print("TechniqueSorting", "=", ",".join(all_techniques), sep="")
for effect_name in sorted(
transformer.parameters_by_effect_name.keys()
| transformer.unused_defines_by_effect_name.keys()
):
print()
print("[", effect_name, "]", sep="")
used = effect_name in transformer.used_effect_names
parameters = transformer.parameters_by_effect_name.get(effect_name, {})
unused_defines = transformer.unused_defines_by_effect_name.get(effect_name, [])
if len(unused_defines) != 0:
if len(parameters) == 0:
(logging.warning if used else logging.info)(
"unused effect {}; defines: {}".format(
repr(effect_name), repr(unused_defines)
)
)
any_warning |= used
else:
(logging.warning if used else logging.info)(
"unused defines for effect {}; possibly kills effect; defines: {}".format(
repr(effect_name), repr(unused_defines)
)
)
any_warning |= used
for name, value in parameters.items():
print(name, "=", evaluate(value), sep="")
if any_warning:
logging.warning("finished with warnings")