-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.py
More file actions
142 lines (114 loc) · 4.93 KB
/
util.py
File metadata and controls
142 lines (114 loc) · 4.93 KB
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
import re
import os
import jinja2
AUTOGEN_PATH = os.path.join(
"C:\\",
"Program Files (x86)",
"Steam",
"steamapps",
"common",
"Eco",
"Eco_Data",
"Server",
"Mods",
"__core__",
"AutoGen",
)
class TextProcessingException(Exception):
pass
def process_recipes(recipes_changes, target_path):
item_pattern = r".*(class \w+Item) .*"
block_pattern = r".*(class \w+Block) .*"
skill_pattern = r".*(class \w+Skill) .*"
skill_book_pattern = r".*(class \w+SkillBook) .*"
skill_scroll_pattern = r".*(class \w+SkillScroll) .*"
for file, changes in recipes_changes.items():
print(f"Reading {file}")
with open(os.path.join(AUTOGEN_PATH, file), "r", encoding=",utf-8") as f:
recipe_data = f.read()
# Remove items, we never need to duplicate them.
recipe_data = remove_pattern(recipe_data, file, item_pattern)
# Remove blocks, we never need to duplicate them.
recipe_data = remove_pattern(recipe_data, file, block_pattern)
# Remove skills / skill books / skill scrolls, we never need to duplicate them.
recipe_data = remove_pattern(recipe_data, file, skill_pattern)
recipe_data = remove_pattern(recipe_data, file, skill_book_pattern)
recipe_data = remove_pattern(recipe_data, file, skill_scroll_pattern)
# Replace keys in the recipe data.
for key, configs in changes.items():
recipe_data = replace_key(recipe_data, file, key, configs)
print(f"\tWriting {file}")
folder = file.split("\\", maxsplit=1)[0]
os.makedirs(os.path.join(target_path, folder), exist_ok=True)
with open(os.path.join(target_path, file), "w", encoding="utf-8") as f:
f.write(recipe_data)
def remove_pattern(recipe_data, file, pattern):
recipes_lines = recipe_data.split("\n")
for line in recipes_lines:
if match := re.match(pattern, line):
recipe_data = remove_class(recipe_data, file, line.strip(), match.group(1))
return recipe_data
def remove_class(recipe_data, file, key, class_name):
recipe_lines = recipe_data.split("\n")
print(f'\tRemoving "{class_name}" from recipe')
# Step 1: Identify the line where the class starts.
class_start_line = 0
for line, value in enumerate(recipe_lines):
if class_name in value:
class_start_line = line
break
if class_start_line == 0:
raise TextProcessingException(f"\t\tCouldn't find {class_name} in {file}:{key}")
print(f'\t\tFound "{class_name}" at line {class_start_line}')
# Step 2: Move class start line upwards if the class has any annotations.
for index in range(class_start_line, 0, -1):
line = recipe_lines[index].strip()
if line.startswith("["):
class_start_line = index
elif class_name in line:
continue
else:
break
print(f"\t\tMoved class start line to {class_start_line} due to annotations")
# Step 3: Count brackets to find the end of the class.
bracket_count = 0
class_end_index = 0
recipe_data = "\n".join(recipe_lines)
class_start_index = recipe_data.find(class_name)
backets_started = False
for index in range(class_start_index, len(recipe_data)):
char = recipe_data[index]
if char == "{":
bracket_count += 1
backets_started = True
elif char == "}":
bracket_count -= 1
elif backets_started and bracket_count == 0:
class_end_index = index
break
if bracket_count != 0 or class_end_index == 0:
raise TextProcessingException(f"\t\tCouldn't find end of class in {file}:{key}")
class_end_line = recipe_data[:class_end_index].count("\n") + 1
print(f"\t\tclass end bracket found at line {class_end_line}")
# Step 4: Translate the class end index to a line number, remove the lines.
print(f"\t\tRemoving lines {class_start_line} to {class_end_line}")
recipe_lines = recipe_data.split("\n")
del recipe_lines[class_start_line:class_end_line]
# Step -1: Join the lines back together to get a single string.
recipe_data = "\n".join(recipe_lines)
return recipe_data
def replace_key(recipe_data, file, key, configs):
print(f"\tReplacing {key}")
if configs[0] not in recipe_data:
raise TextProcessingException(f"\t\tCouldn't find {configs[0]} in {file}:{key}")
recipe_data = recipe_data.replace(configs[0], configs[1])
return recipe_data
def template_recipes(recipes_templates, template, target_path):
env = jinja2.Environment(loader=jinja2.FileSystemLoader("templates/"))
print(f"Rendering templates for {template}")
for path, values in recipes_templates.items():
template = env.get_template(template)
content = template.render(**values)
with open(os.path.join(target_path, path), mode="w", encoding="utf-8") as recipe:
recipe.write(content)
print(f"\t {path}")