-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreplace_strings.py
More file actions
105 lines (73 loc) · 2.67 KB
/
replace_strings.py
File metadata and controls
105 lines (73 loc) · 2.67 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
#python2
import os
import sys
import struct
import argparse
'''
Replaces old lines inside a *.LUB file with new ones from a *.CSV file
Example: python replace_strings.py AS_StringTable.lub AS_StringTable.csv AS_StringTable_new.lub
'''
##############################################################
def read_binary(path):
with open(path, "rb") as f:
return f.read()
return False
def save_binary(path, data):
with open(path, "wb") as f:
return f.write(data)
return False
##############################################################
class LUB_PATCHER:
def __init__(self):
self.csv_list = []
self.lub_data = None
self.counter = 0
def show_result(self):
print('Replaced {} string(s)'.format(self.counter))
def replace(self, lub_path, csv_path, out_path):
self.read_csv(csv_path)
self.read_lub(lub_path)
self.actual_work()
save_binary(out_path, self.lub_data)
self.show_result()
def read_csv(self, csv_path):
data = read_binary(csv_path)
for str in data.split('\r\n'):
if not str: continue
self.csv_list.append([str[:9], str[10:]])
def read_lub(self, lub_path):
self.lub_data = read_binary(lub_path)
def replace_in_lub(self, offset, size, new_str):
self.lub_data = self.lub_data[:offset] + new_str + self.lub_data[offset + size -1:]
self.counter += 1
def get_size_of_lua_str(self, offset):
bin_size = self.lub_data[offset:4 + offset]
int_size = struct.unpack('L', bin_size)
if not int_size: return False
full_size = int_size[0] + 4 + 1
return full_size
def get_old_string(self, str_id):
offset = self.lub_data.find(str_id)
if offset == -1: return False
offset += len(str_id) + 1
str_size = self.get_size_of_lua_str(offset)
return [offset, str_size]
def build_lua_str(self, str_data):
size = struct.pack('L', len(str_data) + 1)
return size + str_data + '\x00'
def actual_work(self):
for csv in self.csv_list:
if len(csv[1]) == 0: continue
old_str = self.get_old_string(csv[0])
if not old_str: raise ValueError('Unknown string - ' + csv[0])
new_str = self.build_lua_str(csv[1])
self.replace_in_lub(old_str[0], old_str[1], new_str)
##############################################################
if __name__ == '__main__':
arg_parser = argparse.ArgumentParser(description='Replaces old lines inside a *.LUB file with new ones from a *.CSV file')
arg_parser.add_argument('lub_path')
arg_parser.add_argument('csv_path')
arg_parser.add_argument('out_path')
args = arg_parser.parse_args()
lub_parser = LUB_PATCHER()
lub_parser.replace(args.lub_path, args.csv_path, args.out_path)