Skip to content

Instantly share code, notes, and snippets.

@N3X15
Last active April 5, 2022 20:57
Show Gist options
  • Save N3X15/892f32c66a5c773e0dc0 to your computer and use it in GitHub Desktop.
Save N3X15/892f32c66a5c773e0dc0 to your computer and use it in GitHub Desktop.
Generate Assembler Blueprints for Space Engineers mods.
'''
Spengies Assembler Blueprint Generator
Copyright (c) 2015 Rob "N3X15" Nelson <nexisentertainment@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
@end-license
Run on an EXTRACTED mod.
Requires lxml.
'''
import os
import sys
import argparse
from lxml import etree
'''
<?xml version="1.0"?>
<Definitions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<BlueprintClassEntries>
<Entry Class="LargeBlocks" BlueprintSubtypeId="AirVent/ConveyorAirVent" />
<Entry Class="LargeBlocks" BlueprintSubtypeId="AirVent/ConveyorAirVentSlope" />
<Entry Class="SmallBlocks" BlueprintSubtypeId="AirVent/SmallConveyorAirVent" />
<Entry Class="SmallBlocks" BlueprintSubtypeId="AirVent/SmallConveyorAirVentSlope" />
</BlueprintClassEntries>
</Definitions>
'''
NS = {
"xsi": "http://www.w3.org/2001/XMLSchema-instance",
"xsd": "http://www.w3.org/2001/XMLSchema"
}
def element(name, attr={}, children=[], nsmap={}):
e = etree.Element(name, nsmap=nsmap)
if len(attr) > 0:
for k, v in attr.items():
e.set(k, v)
if len(children) > 0:
for v in children:
e.append(v)
return e
class AsmBlueprintEntry:
def __init__(self):
self.Class = ''
self.Subtype = ''
@staticmethod
def Create(cls, subtype):
abe = AsmBlueprintEntry()
abe.Class = cls
abe.Subtype = subtype
return abe
def ToXML(self):
return element("Entry", {'Class': self.Class, 'BlueprintSubtypeId': self.Subtype})
class AssemblerBlueprints:
def __init__(self):
self.entries = []
def AddEntry(self, cls, subtype):
print('{} -> {}'.format(subtype, cls))
self.entries.append(AsmBlueprintEntry.Create(cls, subtype))
def ToXML(self):
entries = element("BlueprintClassEntries", children=[x.ToXML() for x in self.entries])
defs = element("Definitions", {}, [entries], nsmap=NS)
return defs
def __str__(self):
return etree.tostring(self.ToXML(), pretty_print=True)
def ReadCubeBlocks(filename, def_callback):
defs = None
with open(filename, 'r') as f:
defs = etree.parse(f)
ids = []
for idparts in defs.xpath("//Definition/Id", namespaces=NS):
typeID = ''
subtypeID = ''
for idpart in idparts:
if idpart.tag == 'TypeId':
typeID = idpart.text
elif idpart.tag == 'SubtypeId':
subtypeID = idpart.text
_id = '{}/{}'.format(typeID, subtypeID)
if typeID == '' or subtypeID == '':
print("Invalid type id {}".format(_id))
continue
if _id in ids:
print("Ignoring duplicated type id {}".format(_id))
continue
ids.append(_id)
def_callback(_id, idparts.getparent())
def mkAsmBlueprints(data_dir):
bp = AssemblerBlueprints()
def addToBlueprints(cbID, cbDef):
sz = cbDef.find("CubeSize").text
bp.AddEntry(sz + 'Blocks', cbID)
ReadCubeBlocks(os.path.join(data_dir, 'CubeBlocks.sbc'), addToBlueprints)
bpf = os.path.join(data_dir, 'AssemblerBlueprints.sbc')
print('Writing {}...'.format(bpf))
with open(bpf, 'w') as f:
f.write(etree.tostring(bp.ToXML(), pretty_print=True, xml_declaration=True))
def FixMod(mod_dir):
data_dir = os.path.join(mod_dir, 'Data')
mkAsmBlueprints(data_dir)
def main():
parser = argparse.ArgumentParser(description='Generates assembler blueprints for a given mod.')
parser.add_argument('mod', type=str, nargs='+', help='Extracted mod\'s base directory.')
args = parser.parse_args()
for mod in args.mod:
FixMod(mod)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment