import scripts from dmitmel/factorio-tools@8f8f9725e5 written on 2020-05-25

This commit is contained in:
Dmytro Meleshko 2021-04-02 21:51:47 +03:00
parent a90b41a8cc
commit 8dfc96c910
2 changed files with 101 additions and 0 deletions

View file

@ -0,0 +1,65 @@
# <https://wiki.factorio.com/Property_tree>
# <factorio.https://wiki.factorio.com/Data_types>
# <https://github.com/credomane/factoriomodsettings/blob/master/src/ModSettingsDeserialiser.js>
# <https://github.com/credomane/factoriomodsettings/blob/master/src/ModSettingsSerialiser.js>
# <https://www.devdungeon.com/content/working-binary-data-python>
import struct
def read_bool(buf):
return buf.read(1)[0] == 1
def read_number(buf):
return struct.unpack("<d", buf.read(8))[0]
def _read_length(buf):
return struct.unpack("<I", buf.read(4))[0]
def read_string(buf):
is_empty = read_bool(buf)
if is_empty:
return ""
len_ = buf.read(1)[0]
if len_ == 0xFF:
len_ = _read_length(buf)
return buf.read(len_).decode("utf8")
def read_dictionary(buf):
len_ = _read_length(buf)
value = {}
for _ in range(len_):
key = read_string(buf)
value[key] = read(buf)
return value
def read_list(buf):
len_ = _read_length(buf)
value = []
for _ in range(len_):
read_string(buf)
value.append(read(buf))
return value
def read(buf):
type_, _any_type_flag = buf.read(2)
if type_ == 0:
return None
elif type_ == 1:
return read_bool(buf)
elif type_ == 2:
return read_number(buf)
elif type_ == 3:
return read_string(buf)
elif type_ == 4:
return read_list(buf)
elif type_ == 5:
return read_dictionary(buf)
else:
raise Exception("unknown property tree type 0x{:02x}".format(type_))

View file

@ -0,0 +1,36 @@
#!/usr/bin/env python3
# <https://wiki.factorio.com/Mod_settings_file_format>
# <https://forums.factorio.com/59851>
# <https://www.dropbox.com/sh/uscmj9y3cjfwpsr/AAD35_ZZu64EBi0awLA07fxga?dl=0>
# <https://github.com/credomane/factoriomodsettings/blob/master/src/FactorioModSettings.js>
import sys
import os
from pathlib import Path
import struct
import json
sys.path.insert(1, os.path.join(os.path.dirname(__file__), "..", "script-resources"))
import factorio.property_tree
with open(Path.home() / ".factorio" / "mods" / "mod-settings.dat", "rb") as f:
version_main, version_major, version_minor, version_developer = struct.unpack(
"<HHHH", f.read(8)
)
always_false_flag = factorio.property_tree.read_bool(f)
assert not always_false_flag
deserialized_data = {
"factorio_version": {
"main": version_main,
"major": version_major,
"minor": version_minor,
"developer": version_developer,
},
"data": factorio.property_tree.read(f),
}
print(json.dumps(deserialized_data, indent=2))