forked from Jacky720/NieR2Blender2NieR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
materials_miner.py
278 lines (240 loc) · 11.2 KB
/
materials_miner.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
import sys
import json
import traceback
import random
verbose = False
global_shaders = {}
def shared_chars(s1, s2):
count = 0
for i, char in enumerate(s1):
if (i > len(s2) - 1):
return count
if (char == s2[i]):
count += 1
return count
def find_pattern_count(pattern, list):
count = 0
for i in range(len(list)-len(pattern)+1):
if (list[i:i + len(pattern)] == pattern):
count += 1
return count
def checkConsecutive(l):
return sorted(l) == list(range(min(l), max(l)+1))
def extractMats(fp):
file = open(fp)
data = json.load(file)
file.close()
dump = open("./dump.json", "a+")
dump.seek(0)
try:
shaders = json.load(dump)
except:
shaders = {}
for matName, mat in data.items():
if ("Shader_Name" not in mat):
break
shaderName = mat["Shader_Name"]
technique = mat["Technique_Name"]
parameters = mat["ParameterGroups"]
variables = mat["Variables"]
if technique != "Default":
continue
if len(parameters) == 0:
continue
if shaderName not in global_shaders:
global_shaders[shaderName] = []
global_shaders[shaderName].append([variables, parameters, matName])
if shaderName not in shaders:
shaders[shaderName] = {}
shaders[shaderName]["Variables"] = list(variables.keys())
shaders[shaderName]["Parameters"] = ["Unknown"] * len(parameters[0])
mappedParameters = ["Unknown"] * len(parameters[0])
# Find singles
varVals = list(variables.values())
for name, value in variables.items():
for i, param in enumerate(parameters[0]):
if (value == param) and (parameters[0].count(value) == 1) and(varVals.count(value) == 1):
mappedParameters[i] = name
"""
# Find vector2
i = 0
variablesList = list(variables.keys())
valuesList = list(variables.values())
for i in range(len(valuesList)-1):
if (find_pattern_count(valuesList[i:i+2], parameters[0]) == 1 and find_pattern_count(valuesList[i:i+2], valuesList) == 1):
sharedWithFirst = shared_chars(variablesList[i], variablesList[i+1])
if (sharedWithFirst > 10):
for j in range(len(parameters[0])):
if (parameters[0][j] == valuesList[i] and parameters[0][j+1] == valuesList[i+1]):
mappedParameters[j] = variablesList[i]
mappedParameters[j+1] = variablesList[i+1]
break
# Find vector3
i = 0
variablesList = list(variables.keys())
valuesList = list(variables.values())
for i in range(len(valuesList)-2):
if (find_pattern_count(valuesList[i:i+3], parameters[0]) == 1 and find_pattern_count(valuesList[i:i+3], valuesList) == 1):
sharedWithFirst = shared_chars(variablesList[i], variablesList[i+1])
sharedWithSecond = shared_chars(variablesList[i], variablesList[i+2])
if (sharedWithFirst > 10 and sharedWithSecond > 10 and sharedWithFirst == sharedWithSecond):
for j in range(len(parameters[0])):
if (parameters[0][j] == valuesList[i] and parameters[0][j+1] == valuesList[i+1] and parameters[0][j+2] == valuesList[i+2]):
mappedParameters[j] = variablesList[i]
mappedParameters[j+1] = variablesList[i+1]
mappedParameters[j+2] = variablesList[i+2]
break
"""
for i, param in enumerate(mappedParameters):
if (shaders[shaderName]["Parameters"][i] != "Unknown") or (param == "Unknown"):
continue
shaders[shaderName]["Parameters"][i] = param
print("Found new", param, "match in material", matName, "for shader", shaderName, "index", i)
dump.truncate(0)
json.dump(shaders, dump, indent= 4)
dump.close()
def find_diffs(shaderName, instances):
dump = open("./dump.json", "a+")
dump.seek(0)
found_shaders = json.load(dump)
found_params = found_shaders[shaderName]["Parameters"]
changedIndices = []
for instance in instances:
for otherInstance in instances:
if instance == otherInstance:
continue
variables = instance[0]
otherVariables = otherInstance[0]
paramGroups = instance[1]
otherParamGroups = otherInstance[1]
if (len(paramGroups) != len(otherParamGroups)) or (len(variables) != len(otherVariables)):
continue
if (len(paramGroups) != 2 or len(otherParamGroups) != 2):
continue
diffs = []
for index, (first, second) in enumerate(zip(paramGroups[0], otherParamGroups[0])):
if first != second:
if (found_params[index] != "Unknown"):
continue
#print(index, first, second)
diffs.append(index)
diffs2 = []
for i in range(len(paramGroups[1])):
if (paramGroups[1][i] != otherParamGroups[1][i]):
diffs2.append(i)
varDiffs = []
for index, (first, second) in enumerate(zip(variables.values(), otherVariables.values())):
if first != second:
if "g_" not in list(variables.keys())[index]:
print(list(variables.keys())[index])
continue
#print(index, list(variables.keys())[index], first, second)
varDiffs.append(index)
if (len(diffs) > 0):
if verbose:
print('#'*25)
print(len(diffs), "x Instance differences:")
for diff in diffs:
print(diff, ":", paramGroups[0][diff], otherParamGroups[0][diff])
if (len(varDiffs) > 0):
print('#'*25)
print(len(varDiffs), "x Variable differences:")
for diff in varDiffs:
print(diff, ":", list(variables.keys())[diff])
if (len(diffs) == len(varDiffs)):
if checkConsecutive(diffs) and checkConsecutive(varDiffs):
print("Found differences in shader", shaderName, "for variable indices", varDiffs, "- parameter indices", diffs)
for i, diff in enumerate(diffs):
found_params[diff] = list(variables.keys())[varDiffs[i]]
if (diff in changedIndices):
print("THE SAME INDEX WAS CHANGED TWICE! MAJOR PROBLEM!")
return
else:
changedIndices.append(diff)
"""
if (len(diffs) == 1 and len(varDiffs) == 1 and len(diffs2) == 0):
print("Found difference in shader", shaderName, "for variable", varDiffs[0], "index", diffs[0])
found_params[diffs[0]] = varDiffs[0]
if (diffs[0] in changedIndices):
print("THE SAME INDEX WAS CHANGED TWICE! MAJOR PROBLEM!")
return
else:
changedIndices.append(diffs[0])
"""
dump.truncate(0)
json.dump(found_shaders, dump, indent= 4)
dump.close()
return changedIndices
def find_common_param_neighbours(shader_names):
dump = open("./dump.json", "a+")
dump.seek(0)
found_shaders = json.load(dump)
params_before = {}
params_after = {}
inconsisstent_params = []
changes_found = 0
for shader_name in shader_names:
found_params = found_shaders[shader_name]["Parameters"]
for i in range(len(found_params)):
if found_params[i] == "Unknown":
continue
if i != 0 and found_params[i-1] != "Unknown":
if (found_params[i] in params_before) and params_before[found_params[i]] != found_params[i-1]:
inconsisstent_params.append(found_params[i])
else:
params_before[found_params[i]] = found_params[i-1]
if i+1 != len(found_params) and found_params[i+1] != "Unknown":
if (found_params[i] in params_after) and params_after[found_params[i]] != found_params[i+1]:
inconsisstent_params.append(found_params[i])
else:
params_after[found_params[i]] = found_params[i+1]
for param in found_params:
if (param != "Unknown") and (param not in inconsisstent_params):
if param in params_before:
for shader_name in shader_names:
if param not in found_shaders[shader_name]["Parameters"] or params_before[param] not in found_shaders[shader_name]["Variables"]:
continue
param_index = found_shaders[shader_name]["Parameters"].index(param)
if found_shaders[shader_name]["Parameters"][param_index - 1] == "Unknown":
found_shaders[shader_name]["Parameters"][param_index - 1] = params_before[param]
print("Identified common neighbour", params_before[param], "before", param, "in shader", shader_name)
changes_found += 1
if param in params_after:
for shader_name in shader_names:
if param not in found_shaders[shader_name]["Parameters"] or params_after[param] not in found_shaders[shader_name]["Variables"]:
continue
param_index = found_shaders[shader_name]["Parameters"].index(param)
if found_shaders[shader_name]["Parameters"][param_index + 1] == "Unknown":
found_shaders[shader_name]["Parameters"][param_index + 1] = params_after[param]
print("Identified common neighbour", params_after[param], "after", param, "in shader", shader_name)
changes_found += 1
dump.truncate(0)
json.dump(found_shaders, dump, indent= 4)
dump.close()
return changes_found
if __name__ == "__main__":
matFiles = sys.argv[1::]
#random.shuffle(matFiles)
try:
print(matFiles)
for matFile in matFiles:
print(matFile)
try:
extractMats(matFile)
except Exception as e:
traceback.print_exc()
break
totalChanges = 1
while totalChanges > 0:
print("Starting diff finder pass...")
totalChanges = 0
for name, instances in global_shaders.items():
print("Checking shader:", name)
totalChanges += len(find_diffs(name, instances))
totalChanges += find_common_param_neighbours(list(global_shaders.keys()))
print("Found", totalChanges, "new parameters.")
input("Press Enter to Exit")
except Exception as e:
traceback.print_exc()
print(e)
input("ERROR! Press Enter to Exit")