Change assets.py to output assembly (#752)

Issue was raised on discord, this should correct for it.

Previously assets.py would output raw binary data. This makes it more
difficult for the build chain to understand.

Now, it outputs an assembly file (which is just a big list of `.byte`
entries) which will represent the parsed data, and that assembly data
then gets assembled. Therefore, the symbols now exist.

In order to make this work, I adjusted the format of the splat, so now
we have lines like `[0x7718, assets, accessory, g_AccessoryDefs]` where
the entries are [Address, assets, configuration json file to use, name
of the output symbol].

I think this addresses the issue, but I am more than happy to iterate on
this if needed.
This commit is contained in:
bismurphy 2023-11-07 17:05:07 -05:00 committed by GitHub
parent 0726fd6f83
commit b3c0524bc8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 12 deletions

View File

@ -429,8 +429,8 @@ $(BUILD_DIR)/$(ASSETS_DIR)/%.animset.json.o: $(ASSETS_DIR)/%.animset.json
./tools/splat_ext/animset.py gen-asm $< $(BUILD_DIR)/$(ASSETS_DIR)/$*.s ./tools/splat_ext/animset.py gen-asm $< $(BUILD_DIR)/$(ASSETS_DIR)/$*.s
$(AS) $(AS_FLAGS) -o $(BUILD_DIR)/$(ASSETS_DIR)/$*.o $(BUILD_DIR)/$(ASSETS_DIR)/$*.s $(AS) $(AS_FLAGS) -o $(BUILD_DIR)/$(ASSETS_DIR)/$*.o $(BUILD_DIR)/$(ASSETS_DIR)/$*.s
$(BUILD_DIR)/$(ASSETS_DIR)/%.json.o: $(ASSETS_DIR)/%.json $(BUILD_DIR)/$(ASSETS_DIR)/%.json.o: $(ASSETS_DIR)/%.json
./tools/splat_ext/assets.py $< $(BUILD_DIR)/$(ASSETS_DIR)/$*.bin ./tools/splat_ext/assets.py $< $(BUILD_DIR)/$(ASSETS_DIR)/$*.s
$(LD) -r -b binary -o $(BUILD_DIR)/$(ASSETS_DIR)/$*.o $(BUILD_DIR)/$(ASSETS_DIR)/$*.bin $(AS) $(AS_FLAGS) -o $(BUILD_DIR)/$(ASSETS_DIR)/$*.o $(BUILD_DIR)/$(ASSETS_DIR)/$*.s
$(BUILD_DIR)/$(ASSETS_DIR)/%.tilelayout.bin.o: $(ASSETS_DIR)/%.tilelayout.bin $(BUILD_DIR)/$(ASSETS_DIR)/%.tilelayout.bin.o: $(ASSETS_DIR)/%.tilelayout.bin
$(LD) -r -b binary -o $(BUILD_DIR)/$(ASSETS_DIR)/$*.o $(ASSETS_DIR)/$*.tilelayout.bin $(LD) -r -b binary -o $(BUILD_DIR)/$(ASSETS_DIR)/$*.o $(ASSETS_DIR)/$*.tilelayout.bin
$(BUILD_DIR)/$(ASSETS_DIR)/%.bin.o: $(ASSETS_DIR)/%.bin $(BUILD_DIR)/$(ASSETS_DIR)/%.bin.o: $(ASSETS_DIR)/%.bin

View File

@ -40,15 +40,15 @@ segments:
- [0x2EE8, data] - [0x2EE8, data]
- [0x3C40, data] - [0x3C40, data]
- [0x4A00, data] - [0x4A00, data]
- [0x4B04, assets, equipment] - [0x4B04, assets, equipment, g_EquipDefs]
- [0x7718, assets, accessory] - [0x7718, assets, accessory, g_AccessoryDefs]
- [0x8258, data] - [0x8258, data]
- [0x8900, assets, enemydefs] - [0x8900, assets, enemydefs, g_EnemyDefs]
- [0xC780, data] - [0xC780, data]
- [0xCEB0, data] - [0xCEB0, data]
- [0xCF18, .data, 6D59C] - [0xCF18, .data, 6D59C]
- [0xCF4C, data] - [0xCF4C, data]
- [0xD1D4, assets, factory_blueprint] - [0xD1D4, assets, factory_blueprint, g_FactoryBlueprints]
- [0xD4B8, data] - [0xD4B8, data]
- [0x2217C, data] - [0x2217C, data]
- [0x227B0, data] - [0x227B0, data]

View File

@ -71,12 +71,14 @@ def get_parser_and_size(dataType: str):
def serialize_asset(content: str, asset_config: str) -> bytearray: def serialize_asset(content: str, asset_config: str) -> bytearray:
obj = json.loads(content) raw_json_data = json.loads(content)
config = json.loads(asset_config) config = json.loads(asset_config)
item_count = len(obj) symbol_name = raw_json_data["symbol_name"]
asset_data = raw_json_data["asset_data"]
item_count = len(asset_data)
serialized_data = bytearray() serialized_data = bytearray()
for i in range(0, item_count): for i in range(0, item_count):
item = obj[i] item = asset_data[i]
for entry, entryType in config["struct"].items(): for entry, entryType in config["struct"].items():
serializer = get_serializer(entryType) serializer = get_serializer(entryType)
json_value = item[entry] json_value = item[entry]
@ -115,7 +117,13 @@ def serialize_asset(content: str, asset_config: str) -> bytearray:
# Anything else can go straight to serializer # Anything else can go straight to serializer
else: else:
serialized_data += serializer(item[entry]) serialized_data += serializer(item[entry])
return serialized_data # Take serialized data (raw bytes) and turn into asm file
asm_output = ""
asm_output += ".section .data\n\n"
asm_output += f".global {symbol_name}\n\n"
for databyte in serialized_data:
asm_output += f".byte {int(databyte)}\n"
return asm_output
class PSXSegAssets(N64Segment): class PSXSegAssets(N64Segment):
@ -133,8 +141,11 @@ class PSXSegAssets(N64Segment):
path.parent.mkdir(parents=True, exist_ok=True) path.parent.mkdir(parents=True, exist_ok=True)
data = self.parse_asset(rom_bytes[self.rom_start : self.rom_end], rom_bytes) data = self.parse_asset(rom_bytes[self.rom_start : self.rom_end], rom_bytes)
json_output = {}
json_output["symbol_name"] = self.args[0]
json_output["asset_data"] = data
with open(path, "w") as f: with open(path, "w") as f:
f.write(json.dumps(data, indent=4)) f.write(json.dumps(json_output, indent=4))
def parse_asset(self, data: bytearray, rom: bytearray) -> list: def parse_asset(self, data: bytearray, rom: bytearray) -> list:
def get_ptr_data(src_ptr_data): def get_ptr_data(src_ptr_data):
@ -226,5 +237,5 @@ if __name__ == "__main__":
with open(input_file_name, "r") as f_in: with open(input_file_name, "r") as f_in:
data = serialize_asset(f_in.read(), config_json) data = serialize_asset(f_in.read(), config_json)
with open(output_file_name, "wb") as f_out: with open(output_file_name, "w") as f_out:
f_out.write(data) f_out.write(data)