-
Notifications
You must be signed in to change notification settings - Fork 0
/
search_blockstate_files.py
121 lines (104 loc) · 4.12 KB
/
search_blockstate_files.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
import json
import sys
from pathlib import Path
def load_json_file(filepath):
try:
with filepath.open('r') as file:
return json.load(file)
except json.JSONDecodeError as e:
print(f"Error while loading JSON file {filepath}: {e}")
return None
def find_model_entries(value, search_model, filepath):
rotations = [('o', 0)]
model_found = False
if not isinstance(value, list):
value = [value]
for entry in value:
# print(str(value))
# print(str(entry))
# print(search_model)
# print(entry.get('model'))
if entry.get('model') == search_model:
model_found = True
# print("Found model in blockstate file.")
# Die Einträge "x", "y", "z" extrahieren, falls vorhanden
x = entry.get('x', 0)
y = entry.get('y', 0)
z = entry.get('z', 0)
# print(f'Found in file {filepath}: x={x}, y={y}, z={z}')
if x != 0:
if ('x', x) not in rotations:
rotations.append(('x', x))
# print("x: ", x)
else:
if y != 0:
if ('y', y) not in rotations:
rotations.append(('y', y))
# print("y ", y)
else:
if z != 0:
if ('z', z) not in rotations:
rotations.append(('z', z))
# print("z: ", z)
else:
if ('o', 0) not in rotations:
rotations.append(('o', 0))
# print("o (this should never occur!): ", 0)
# print(str(result))
return [rotations, model_found]
def get_models(data, search_model, filepath):
rotations = [('o', 0)]
model_found = False
if "variants" in data:
for key, value in data.get('variants', {}).items():
# print(str(key)," ",str(value))
result = find_model_entries(value, search_model, filepath)
if result[1]:
model_found = True
for rotation in result[0]:
if rotation not in rotations:
rotations.append(rotation)
elif "multipart" in data:
# print("found Multipart data: ", data["multipart"])
for entry in data["multipart"]:
result = find_model_entries(entry["apply"], search_model, filepath)
if result[1]:
model_found = True
for rotation in result[0]:
if rotation not in rotations:
rotations.append(rotation)
return [rotations, model_found]
def process_directory(directory, search_model):
# print("Searching blockstate files in: ", str(directory))
# directory = Path(directory) # Verzeichnis in ein Path-Objekt umwandeln
all_blockstate_files = []
all_rotations = []
for filepath in directory.rglob("*.json"): # Alle JSON-Dateien rekursiv durchsuchen
# print(f"Blockstate file: {filepath}")
data = load_json_file(filepath)
if data:
blockstate_files_and_rotations = get_models(data, search_model, filepath)
rotations = blockstate_files_and_rotations[0]
model_found = blockstate_files_and_rotations[1]
for key, value in rotations:
if (key, value) not in all_rotations:
all_rotations.append((key, value))
if model_found:
if filepath.name not in all_blockstate_files:
all_blockstate_files.append(filepath.name)
# print(str(result))
result = [all_rotations, all_blockstate_files]
return result
def main():
if len(sys.argv) < 3:
print("Usage: python script.py <directory> <search_model>")
sys.exit(1)
directory = sys.argv[1]
search_model = sys.argv[2]
directory_path = Path(directory)
if not directory_path.is_dir():
print(f"Specified directory {directory} does not exist.")
sys.exit(1)
process_directory(directory_path, search_model)
if __name__ == "__main__":
main()