mirror of
https://github.com/dragonpilot/dragonpilot.git
synced 2026-02-26 16:03:54 +08:00
version: dragonpilot v0.8.16 beta for EON/C2 date: 2022-08-11T09:38:43 dp-dev(priv2) master commit: c6cd233f23f60e7a6055d42850bc593c0e69082e
32 lines
841 B
Python
32 lines
841 B
Python
import struct, json
|
|
|
|
def load_thneed(fn):
|
|
with open(fn, "rb") as f:
|
|
json_len = struct.unpack("I", f.read(4))[0]
|
|
jdat = json.loads(f.read(json_len).decode('latin_1'))
|
|
weights = f.read()
|
|
ptr = 0
|
|
for o in jdat['objects']:
|
|
if o['needs_load']:
|
|
nptr = ptr + o['size']
|
|
o['data'] = weights[ptr:nptr]
|
|
ptr = nptr
|
|
for o in jdat['binaries']:
|
|
nptr = ptr + o['length']
|
|
o['data'] = weights[ptr:nptr]
|
|
ptr = nptr
|
|
return jdat
|
|
|
|
def save_thneed(jdat, fn):
|
|
new_weights = []
|
|
for o in jdat['objects'] + jdat['binaries']:
|
|
if 'data' in o:
|
|
new_weights.append(o['data'])
|
|
del o['data']
|
|
new_weights = b''.join(new_weights)
|
|
with open(fn, "wb") as f:
|
|
j = json.dumps(jdat, ensure_ascii=False).encode('latin_1')
|
|
f.write(struct.pack("I", len(j)))
|
|
f.write(j)
|
|
f.write(new_weights)
|