-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_localization.py
317 lines (261 loc) · 10.8 KB
/
server_localization.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
import os
import re
import shutil
import sys
import mysql.connector
import pandas as pd
from config import config
from utils import create_pattern_table, insert_pattern, update_garbled_text
t_lang = config.T_LANG
db_config = config.DB_CONFIG
sql_dir = config.SQL_DIR
# Tables and columns to export
tables_and_columns = {
'achievement_reward': ['ID', 'Subject', 'Body'],
'achievement_reward_locale': ['ID', 'Subject', 'Text'],
'acore_string': ['Entry', 'Content_Default'],
'areatrigger_tavern': ['ID', 'Name'],
'areatrigger_teleport': ['ID', 'Name'],
'broadcast_text': ['ID', 'MaleText', 'FemaleText'],
'broadcast_text_locale': ['ID', 'MaleText', 'FemaleText'],
'creature_template': ['Entry', 'Name', 'Subname'],
'creature_template_locale': ['Entry', 'Name', 'Title'],
'creature_text': ['CreatureID', 'GroupID', 'ID', 'Text'],
'creature_text_locale': ['CreatureID', 'GroupID', 'ID', 'Text'],
'gameobject_template': ['Entry', 'Name', 'CastBarCaption'],
'gameobject_template_locale': ['Entry', 'Name', 'CastBarCaption'],
'gossip_menu_option': ['MenuID', 'OptionID', 'OptionText'],
'gossip_menu_option_locale': ['MenuID', 'OptionID', 'OptionText'],
'item_set_names': ['Entry', 'Name'],
'item_set_names_locale': ['ID', 'Name'],
'item_template': ['Entry', 'Name', 'Description'],
'item_template_locale': ['ID', 'Name', 'Description'],
'npc_text': [
'ID', 'Text0_0', 'Text0_1', 'Text1_0', 'Text1_1', 'Text2_0', 'Text2_1',
'Text3_0', 'Text3_1', 'Text4_0', 'Text4_1', 'Text5_0', 'Text5_1',
'Text6_0', 'Text6_1', 'Text7_0', 'Text7_1'
],
'npc_text_locale': [
'ID', 'Text0_0', 'Text0_1', 'Text1_0', 'Text1_1', 'Text2_0', 'Text2_1',
'Text3_0', 'Text3_1', 'Text4_0', 'Text4_1', 'Text5_0', 'Text5_1',
'Text6_0', 'Text6_1', 'Text7_0', 'Text7_1'
],
'page_text': ['ID', 'Text'],
'page_text_locale': ['ID', 'Text'],
'pet_name_generation': ['ID', 'Word'],
'pet_name_generation_locale': ['ID', 'Word'],
'points_of_interest': ['ID', 'Name'],
'points_of_interest_locale': ['ID', 'Name'],
'quest_greeting': ['ID', 'Greeting'],
'quest_greeting_locale': ['ID', 'Greeting'],
'quest_offer_reward': ['ID', 'RewardText'],
'quest_offer_reward_locale': ['ID', 'RewardText'],
'quest_request_items': ['ID', 'CompletionText'],
'quest_request_items_locale': ['ID', 'CompletionText'],
'quest_template': [
'ID', 'LogTitle', 'LogDescription', 'QuestDescription', 'AreaDescription',
'QuestCompletionLog', 'ObjectiveText1', 'ObjectiveText2', 'ObjectiveText3',
'ObjectiveText4'
],
'quest_template_locale': [
'ID', 'Title', 'Objectives', 'Details', 'EndText', 'CompletedText',
'ObjectiveText1', 'ObjectiveText2', 'ObjectiveText3', 'ObjectiveText4'
]
}
def fix_garbled_text_in_database():
print(f"Fixing garbled code...")
conn = mysql.connector.connect(**db_config)
create_pattern_table(conn)
# Insert common garbled patterns
patterns = [
'%å%', '%æ%', '%è%', '%ç%', '%ñ%', '%ø%', '%ð%', '%þ%', '%ì%',
'%П%', '%о%', '%м%', '%г%', '%и%', '%т%', '%е%'
]
for pattern in patterns:
insert_pattern(conn, pattern)
locale_tables = {
table: columns for table, columns in tables_and_columns.items() if table.endswith('locale')
}
for table, columns in locale_tables.items():
update_garbled_text(conn, table, columns, t_lang)
conn.close()
def escape_string(value):
return value.replace("'", "''") if value else value
def fetch_data(table, columns):
conn = mysql.connector.connect(**db_config)
columns_str = ', '.join(columns)
if table.endswith('_locale'):
if table.startswith("creature_text"):
query = (
f"SELECT {columns_str} FROM {table} "
f"WHERE locale='{t_lang}' AND {columns[1]}={columns[1]} "
f"AND {columns[2]}={columns[2]} "
f"ORDER BY {columns[0]}"
)
elif table.startswith("gossip_menu_option"):
query = (
f"SELECT {columns_str} FROM {table} "
f"WHERE locale='{t_lang}' AND {columns[1]}={columns[1]} "
f"ORDER BY {columns[0]}"
)
else:
query = (
f"SELECT {columns_str} FROM {table} "
f"WHERE locale='{t_lang}' "
f"ORDER BY {columns[0]}"
)
else:
if table == "creature_text":
query = (
f"SELECT {columns_str} FROM {table} "
f"WHERE {columns[1]}={columns[1]} AND {columns[2]}={columns[2]} "
f"ORDER BY {columns[0]}"
)
elif table == "gossip_menu_option":
query = (
f"SELECT {columns_str} FROM {table} "
f"WHERE {columns[1]}={columns[1]} "
f"ORDER BY {columns[0]}"
)
else:
query = f"SELECT {columns_str} FROM {table} ORDER BY {columns[0]}"
with conn.cursor() as cursor:
cursor.execute(query)
results = cursor.fetchall()
return results
def format_sql_statements(table, columns, results):
if table.startswith("creature_text"):
update_statements = [
f"UPDATE {table} SET " +
', '.join([
f"{col}='{escape_string(value)}'"
for col, value in zip(columns[3:], row[3:])
]) +
f" WHERE {columns[0]}={row[0]} AND {columns[1]}={row[1]} AND {columns[2]}={row[2]};"
for row in results
]
elif table.startswith("gossip_menu_option"):
update_statements = [
f"UPDATE {table} SET " +
', '.join([
f"{col}='{escape_string(value)}'"
for col, value in zip(columns[2:], row[2:])
]) +
f" WHERE {columns[0]}={row[0]} AND {columns[1]}={row[1]};"
for row in results]
else:
update_statements = [
f"UPDATE {table} SET " +
', '.join([
f"{col}='{escape_string(value)}'"
for col, value in zip(columns[1:], row[1:])
]) +
f" WHERE {columns[0]}={row[0]};"
for row in results]
return update_statements
def save_to_sql_file(update_statements, file_path):
os.makedirs(os.path.dirname(file_path), exist_ok=True)
with open(file_path, 'w', encoding='utf-8') as file:
file.writelines(statement + '\n' for statement in update_statements)
def process_locale_file(locale_file, non_locale_table, non_locale_columns):
t_lang_file = locale_file.replace('locale', f'{t_lang}')
shutil.move(locale_file, t_lang_file)
with open(t_lang_file, 'r', encoding='utf-8') as t_lang_file:
content = t_lang_file.read()
# Replace column names
for loc_col, non_loc_col in zip(
tables_and_columns[non_locale_table + '_locale'],
non_locale_columns):
content = content.replace(f"{loc_col}=", f"{non_loc_col}=")
# Replace table name
content = content.replace(non_locale_table + '_locale', non_locale_table)
# Fix 'zhTW' string escaping
if t_lang == 'zhTW':
content = content.replace("\\'", "'")
with open(t_lang_file.name, 'w', encoding='utf-8') as t_lang_file:
t_lang_file.write(content)
print(f"Successfully exported {t_lang_file.name}")
def export_localized_tables():
os.makedirs(sql_dir, exist_ok=True)
print(f"Exporting the localized tables...")
for table, columns in tables_and_columns.items():
results = fetch_data(table, columns)
# Export as SQL file
update_statements = format_sql_statements(table, columns, results)
sql_file = os.path.join(sql_dir, f"{table}.sql")
save_to_sql_file(update_statements, sql_file)
if table.endswith('_locale'):
base_table = table.replace('_locale', '')
if base_table in tables_and_columns:
process_locale_file(sql_file, base_table, tables_and_columns[base_table])
def split_statements(sql):
return re.split(
r';\s*(?=\b(?:CREATE|ALTER|DROP|INSERT|UPDATE|DELETE|SELECT|CALL|BEGIN|END)\b)',
sql
)
def import_sql_files(all=False):
conn = mysql.connector.connect(**db_config)
cursor = conn.cursor()
files = os.listdir(sql_dir)
if all:
files = [
file for file in files
if file.endswith('.sql') and not file.endswith(f'_{t_lang}.sql')
]
else:
files = [file for file in files if file.endswith(f'_{t_lang}.sql')]
total_files = len(files)
processed_files = 0
for file in files:
file_path = os.path.join(sql_dir, file)
print(f"Importing: {file}")
with open(file_path, 'r', encoding='utf-8') as file:
sql = file.read()
statements = split_statements(sql)
for statement in statements:
if statement.strip():
cursor.execute(statement)
conn.commit()
processed_files += 1
sys.stdout.write(f"\r({processed_files}/{total_files}) {os.path.basename(file.name)} \
imported successfully!.\n")
sys.stdout.flush()
cursor.close()
conn.close()
def run_export_locale_tables():
if t_lang in ['zhCN', 'zhTW', 'ruRU', 'koKR']:
fix_garbled_text_in_database()
export_localized_tables()
def import_all_sql_files():
import_sql_files(all=True)
def export_tables_and_columns_to_csv():
conn = mysql.connector.connect(**db_config)
os.makedirs(sql_dir, exist_ok=True)
for table, columns in tables_and_columns.items():
# Construct the query statement
columns_str = ', '.join(columns)
query = f"SELECT {columns_str} FROM {table}"
df = pd.read_sql(query, conn)
first_column = df.columns[0]
df = df.sort_values(by=first_column)
csv_file = f"{sql_dir}/{table}.csv"
df.to_csv(csv_file, index=False)
print(f"Successfully exported {csv_file}.")
conn.close()
alias_to_function = {
'export': run_export_locale_tables,
'import': import_sql_files,
'import_all': import_all_sql_files,
'export_csv': export_tables_and_columns_to_csv,
'fix': fix_garbled_text_in_database
}
if __name__ == "__main__":
if len(sys.argv) > 1:
alias = sys.argv[1]
func = alias_to_function.get(alias)
if func:
func()
else:
print(f"Alias '{alias}' not found.")
else:
print("Please provide an alias as an argument.")