Skip to content

Commit

Permalink
Add MapId support
Browse files Browse the repository at this point in the history
  • Loading branch information
Darxoon committed Jun 20, 2024
1 parent dc3a32e commit 6aae0f7
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/elf/dataType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ export enum DataType {
Aobj,
Bshape,

// registries
MapId,

// this is the end of the actual file types
TypeAmount,
}
30 changes: 30 additions & 0 deletions src/elf/fileTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,36 @@ const typedefs = {
field_0x48: "int",
field_0x4c: "int",
},
[DataType.MapId]: {
__: {
displayName: "Map",
},

id: "string",
field_0x8: "string",
field_0x10: "string",
field_0x18: "string",
battleStage: "string",
field_0x28: "string",
field_0x30: "string",
field_0x38: "string",
field_0x40: "int",
field_0x44: "int",
field_0x48: "string",
field_0x50: "string",
field_0x58: "int",
field_0x5c: "int",
field_0x60: "int",
field_0x64: "int",
field_0x68: "string",
field_0x70: "int",
field_0x74: "int",
field_0x78: "int",
field_0x7c: "int",
field_0x80: "string",
field_0x88: "string",
field_0x90: "string",
},
} as const satisfies {[dataType: number]: TypeDefinition}


Expand Down
31 changes: 27 additions & 4 deletions src/elf/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,23 @@ export default function parseElfBinary(dataType: DataType, arrayBuffer: ArrayBuf
data = null
break

case DataType.MapId: {
const dataSection = findSection('.data')
const stringSection = findSection('.rodata.str1.1')

const dataView = new DataView(dataSection.content)

let mainSymbol = findSymbol("wld::fld::data::s_data")
let countSymbol = findSymbol("wld::fld::data::kNum")

let count = dataView.getInt32(countSymbol.location.value, true)

data = {}
data.main = parseSymbol(dataSection, stringSection, mainSymbol, DataType.MapId, count)

break
}

// parse .data section by data type
default: {
const dataSection = findSection('.data')
Expand Down Expand Up @@ -230,15 +247,22 @@ function objFromReader(reader: BinaryReader, dataType: DataType): UuidTagged {
}

for (const [fieldName, fieldType] of Object.entries(FILE_TYPES[dataType].typedef)) {

switch (fieldType) {
case "string":
if (reader.readInt32() != 0)
throw new Error(`Field '${fieldName}' on DataType ${DataType[dataType]} is a string when it contains non-pointer data`)
if (reader.readInt32() != 0)
throw new Error(`Field '${fieldName}' on DataType ${DataType[dataType]} is a string when it contains non-pointer data`)

result[fieldName] = null
reader.position += 8
break
case "symbol":
if (reader.readInt32() != 0)
throw new Error(`Field '${fieldName}' on DataType ${DataType[dataType]} is a symbol when it contains non-pointer data`)
if (reader.readInt32() != 0)
throw new Error(`Field '${fieldName}' on DataType ${DataType[dataType]} is a symbol when it contains non-pointer data`)

result[fieldName] = null
reader.position += 8
break
case "Vector3":
result[fieldName] = new Vector3(reader.readFloat32(), reader.readFloat32(), reader.readFloat32())
Expand Down Expand Up @@ -271,7 +295,6 @@ function objFromReader(reader: BinaryReader, dataType: DataType): UuidTagged {
default:
throw new Error(`Unknown data type ${JSON.stringify(fieldType)}`)
}

}

return result
Expand Down
20 changes: 20 additions & 0 deletions src/elf/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ export default function serializeElfBinary(dataType: DataType, binary: ElfBinary
return sections.find(section => section.name == sectionName)
}

function findSectionIndex(sectionName: string): number {
return sections.findIndex(section => section.name == sectionName)
}

function findSymbol(name: string): Symbol {
return binary.symbolTable.find(symbol => symbol.name === name)
}
Expand All @@ -75,6 +79,21 @@ export default function serializeElfBinary(dataType: DataType, binary: ElfBinary
stringRelocations.set(".data", dataStringRelocations)

switch (dataType) {
case DataType.MapId: {
let data: SerializeContext = {
writer: dataWriter,
stringRelocations: dataStringRelocations,
}

serializeObjects(data, DataType.MapId, binary.data.main, { padding: 1 })

// count symbol
symbolLocationReference.set("wld::fld::data::kNum", new Pointer(dataWriter.size))
dataWriter.writeInt32(binary.data.main.length)

break
}

default: {
let data: SerializeContext = {
writer: dataWriter,
Expand Down Expand Up @@ -433,6 +452,7 @@ export default function serializeElfBinary(dataType: DataType, binary: ElfBinary
let dataView = new DataView(output)
dataView.setBigInt64(0x28, BigInt(sectionHeaderTableLocation), true)
dataView.setInt16(0x3C, sections.length, true)
dataView.setInt16(0x3E, findSectionIndex(".shstrtab"), true)



Expand Down
1 change: 1 addition & 0 deletions src/lib/editor/fileEditor/welcomeScreen/FileTree.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"data_Mobj",
"data_Aobj",
"data_Bshape",
"MapId",
]
const dispatch = createEventDispatcher()
Expand Down
3 changes: 3 additions & 0 deletions src/lib/modals/DataTypePrompt.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"data_Mobj": DataType.Mobj,
"data_Aobj": DataType.Aobj,
"data_Bshape": DataType.Bshape,
"MapId": DataType.MapId,
}
onMount(() => {
Expand Down Expand Up @@ -66,6 +67,8 @@
<option value="Mobj">Mobj Placement (data_Mobj)</option>
<option value="Aobj">Aobj Placement (data_Aobj)</option>
<option value="Bshape">Bshape Placement (data_Bshape)</option>

<option value="MapId">Map Registry (MapId)</option>
</select>

<div class="checkbox" style="margin-top: 0.3rem;">
Expand Down

0 comments on commit 6aae0f7

Please sign in to comment.