-
Notifications
You must be signed in to change notification settings - Fork 5
/
cnv.py
176 lines (111 loc) · 3.1 KB
/
cnv.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
""" Copyright 2015-2017 sta256+mpskit at gmail.com
This file is part of mpskit.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See LICENSE file for more details.
"""
from common import *
from madspack import read_madspack, save_madspack, write_madspack, load_madspack
"""
CNV file format
MADSPACK with 7 sections
Section 0: header
off l desc
--------------------
0x0006 2 msg_count
0x0076 4,2? part5 size
Section 1:
Section 2:
Section 3:
Section 4: offsets
Section 5: messages
Section 6:
"""
verbose = 0
def read_cnv(cnv_name):
check_ext(cnv_name, '.CNV')
parts = read_madspack(cnv_name)
assert len(parts) == 7
save_madspack(cnv_name, parts)
h = read_cnv_header(cnv_name)
save_cnv_header(cnv_name, h)
msgs = read_cnv_messages(cnv_name, h.msg_count)
save_cnv_messages(cnv_name, msgs)
def write_cnv(cnv_name):
check_ext(cnv_name, '.CNV')
part0 = open("{}.s00.part".format(cnv_name), 'wb')
part4 = open("{}.s04.part".format(cnv_name), 'wb')
part5 = open("{}.s05.part".format(cnv_name), 'wb')
msgs = load_cnv_messages(cnv_name)
h = load_cnv_header(cnv_name)
offset = 0
for msg in msgs:
enc_msg = encode_string(msg, null_term=True)
# offset first
write_uint16(part4, offset)
offset += write_raw(part5, len(enc_msg), enc_msg)
h.msg_count = len(msgs)
h.part5_size = offset
write_cnv_header(part0, h)
part0.close()
part4.close()
part5.close()
# join to madspack
parts = load_madspack(cnv_name)
assert len(parts) == 7
write_madspack(cnv_name, parts)
output(cnv_name)
def read_cnv_header(cnv_name):
f = open("{}.s00.part".format(cnv_name), 'rb')
h = Record()
f.seek(0x0006)
h.msg_count = read_uint16(f)
f.seek(0x0076)
h.part5_size = read_uint16(f)
f.seek(0)
h.dump = decode_buffer(f.read())
return h
def write_cnv_header(f, h):
f.seek(0)
f.write(encode_buffer(h.dump))
f.seek(0x0006)
write_uint16(f, h.msg_count)
f.seek(0x0076)
write_uint16(f, h.part5_size)
def load_cnv_header(cnv_name):
with open('{}.json'.format(cnv_name), 'r') as f:
return Header.from_dict(json.load(f))
def save_cnv_header(cnv_name, h):
n = '{}.json'.format(cnv_name)
with open(n, 'w') as f:
json.dump(h.as_dict(), f, indent=2)
output(n)
def read_cnv_messages(cnv_name, count):
f = open("{}.s05.part".format(cnv_name), 'rb')
if verbose:
print("reading cnv messages; msg_count={}".format(count))
msgs = []
for _ in range(count):
msg = decode_string(read_until(f,0), null_term=True)
msgs.append(msg)
return msgs
def save_cnv_messages(cnv_name, msgs):
n = cnv_name+'.msg.json'
open(n, 'w').write(
json.dumps(
msgs,
indent=2,
ensure_ascii=False
)
)
output(n)
def load_cnv_messages(cnv_name):
n = cnv_name+'.msg.json'
msgs = json.loads(
open(n,'r').read()
)
return msgs