forked from karantras/Translation-CDDA-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities_user.py
249 lines (210 loc) · 6.91 KB
/
utilities_user.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
# -*- coding: utf-8 -*-
import os
import json
from pathlib import Path
from configparser import ConfigParser
import tkinter.filedialog as fd
names = []
desc = []
j_desc = []
start_name = []
sounds = []
mo_mess = []
messages = []
attacks = []
# configs = ConfigParser()
# configs.read('configs.ini')
# mod = configs.get('Mods', 'mod')
# game_folder = configs.get('Folders', 'game folder')
# translator_folder = configs.get('Folders', 'translator folder')
# mods_folder = configs.get('Folders', 'mod folder')
# string_folder = translator_folder+"\\strings\\"+ configs.get('Mods', 'mod')
# user_folder = translator_folder+"\\user\\"+ configs.get('Mods', 'mod')
### Replacer's functions ###
def open_file(file):
with open (file,'r', encoding = 'utf-8') as t:
text = t.read()
objects = json.loads(text)
return objects
def replacer(user_path, mod_path, mod):
for root, dirs, files in os.walk(user_path):
for file in files:
### Временная функция для того, чтобы не переписывать файлы игры ###
new_root = (mod.join(root.split(mod)[-1:]))
base_file = mod_path + "\\" + new_root + "\\" + file
user_file = root + "\\" + file
global names
global desc
global j_desc
global start_name
global sounds
global messages
global attacks
if file != ".json":
try:
user_objects = open_file(user_file)[0]
base_objects = open_file(base_file)
except json.decoder.JSONDecodeError:
print(f"{file} has some problems")
else:
if "names" in user_objects.keys():
names = user_objects["names"]
if "descriptions" in user_objects.keys():
desc = user_objects["descriptions"]
if "job_description" in user_objects.keys():
j_desc = user_objects["job_description"]
if "start_name" in user_objects.keys():
start_name = user_objects["start_name"]
if "sound" in user_objects.keys():
sounds = user_objects["sound"]
if "messages" in user_objects.keys():
messages = user_objects["messages"]
if "attacks" in user_objects.keys():
attacks = user_objects["attacks"]
### Replacer part (Will add new tags)###
for objects in base_objects:
for key, item in objects.items():
try:
if key == "name":
minor_rep(objects, item, key, names)
if key == "description":
minor_rep(objects, item, key, desc)
if key == "desc":
minor_rep(objects, item, key, desc)
if key == "job_description":
minor_rep(objects, item, key, j_desc)
except KeyError:
print(f"Errno3 - missing key. Key {item} doesn't exist in user {file}.")
if key == "sound":
minor_rep(objects, item, key, sounds)
if ((key == "messages") or
(key == "mutagen_message") or
(key == "memorial_message") or
(key == "iv_message")):
minor_rep(objects, item, key, messages)
if key == "spawn_item" and "message" in item:
minor_rep(item, item["message"], "message", messages)
if key == "use_action" and "msg" in item:
minor_rep(item, item["msg"], "msg", messages)
if key == "attacks":
if type(item) == list:
item = item[0]
if "attack_text_npc" in item.keys():
minor_rep(item, item["attack_text_npc"], "attack_text_npc", attacks)
### Write new json part
final_folder = root.replace("user","translated")
if not os.path.exists(final_folder):
os.makedirs(final_folder)
with open (final_folder + "\\" + file, "w", encoding = "utf-8") as file:
print("[", file = file)
count = len(base_objects)
for record in base_objects:
print(' {', file = file)
count -= 1
m_count = len(record)
for key, value in record.items():
m_count -= 1
x = 4
value = req_new(value, x)
value = final_check(value, x)
file.write(x*' '+'"' + key + '" : '+ value)
if m_count > 0:
file.write(",")
file.write ('\n')
file.write (' }')
if count != 0:
file.write (',\n')
print(f"\n]", file = file)
names.clear()
desc.clear()
def minor_rep(objects, item, key, user_objects):
if type(item) == dict:
for key, value in item.items():
item[key] = user_objects[value]
elif type(item) == list:
x = 0
for value in item:
item[x] = user_objects[value]
x += 1
else:
objects[key] = user_objects[item]
### Formating functions ###
def clear_sym(string):
string = string
### Проверка спецсимвола \n ###
if "\n" in string:
string = string.replace("\n", "\\n")
#### Проверка на наличие прямой речи ####
if '\"' in string:
string = string.replace("\"", '\\"')
return string
def req_new(value, x, string = "", key = ""):
### Конечные случаи ###
if type(value) == int or type(value) == float:
value = str(value)
elif type(value) == bool:
value = str(value).lower()
elif type(value) == str:
value = '"' + clear_sym(value) + '"' #### Проверка на наличие спецсимволов (Необходимо только для строк)
### Комплексные структуры ###
elif type(value) == list or type(value) == dict:
x += 2
count = len(value)
### Определение длины структуры
if len(str(value)) >= 100:
string_type = "long"
else:
string_type = "short"
### Распаковка списка ###
if type(value) == list:
string = ""
for item in value:
count -= 1
if string_type == "short":
value_new = req_new(item, x)
if count > 0:
value_new += ", "
elif string_type == "long":
value_new = x*" " + req_new(item, x)
if count > 0:
value_new += ',\n'
else:
value_new += '\n'
string += value_new
value = '[' + string + ']'
### Распаковка словаря ###
if type(value) == dict:
string = ""
for key, item in value.items():
count -= 1
if string_type == "short":
value_new = req_new(item, x)
value_new = '"' + key + '" : ' + value_new + ""
if count > 0:
value_new += ", "
elif string_type == "long":
value_new = req_new(item, x)
value_new = x*" " + '"' + key + '" : ' + value_new
if count > 0:
value_new += ',\n'
else:
value_new += '\n'
string += value_new
value = "{" + string + "}"
x -= 2
return value
def final_check(string, x):
if '[ ["' in string:
string = string.replace('[ ["', '[\n ["')
if '[ "' in string:
string = string.replace('[ "', '[\n "')
if '\n]'in string:
string = string.replace('\n]', '\n ]')
if '{ {"' in string:
string = string.replace('{ {"', '{\n {"')
if '{ "' in string:
string = string.replace('{ "', '{\n "')
if '\n}'in string:
string = string.replace('\n}', '\n }')
return string
# replacer(user_folder, mods_folder)