Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 48 additions & 36 deletions src/translators/ObjectsTranslator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { HexBuffer } from '../HexBuffer';
import { W3Buffer } from '../W3Buffer';
import { WarResult, JsonResult } from '../CommonInterfaces'
import {HexBuffer} from '../HexBuffer';
import {W3Buffer} from '../W3Buffer';
import {WarResult, JsonResult} from '../CommonInterfaces'

enum TableType {
original = 'original',
Expand Down Expand Up @@ -85,7 +85,10 @@ export abstract class ObjectsTranslator {
// Original and new object ids
if (tableType === TableType.original) {
outBufferToWar.addChars(defKey);
outBufferToWar.addByte(0); outBufferToWar.addByte(0); outBufferToWar.addByte(0); outBufferToWar.addByte(0); // no new Id is assigned
outBufferToWar.addByte(0);
outBufferToWar.addByte(0);
outBufferToWar.addByte(0);
outBufferToWar.addByte(0); // no new Id is assigned
} else {
// e.g. "h000:hfoo"
outBufferToWar.addChars(defKey.substring(5, 9)); // original id
Expand Down Expand Up @@ -174,7 +177,7 @@ export abstract class ObjectsTranslator {
}

public static warToJson(type: string, buffer: Buffer): JsonResult<ObjectModificationTable> {
const result = { original: {}, custom: {} };
const result = {original: {}, custom: {}};
const outBufferToJSON = new W3Buffer(buffer);

const fileVersion = outBufferToJSON.readInt();
Expand All @@ -185,42 +188,51 @@ export abstract class ObjectsTranslator {
for (let i = 0; i < numTableModifications; i++) {
const objectDefinition = []; // object definition will store one or more modification objects

const originalId = outBufferToJSON.readChars(4),
customId = outBufferToJSON.readChars(4),
modificationCount = outBufferToJSON.readInt();

for (let j = 0; j < modificationCount; j++) {
const modification: Modification = {
id: '',
type: ModificationType.string,
level: 0,
column: 0,
value: {}
};

modification.id = outBufferToJSON.readChars(4);
modification.type = this.varTypes[outBufferToJSON.readInt()]; // 'int' | 'real' | 'unreal' | 'string',
const originalId = outBufferToJSON.readChars(4);
const customId = outBufferToJSON.readChars(4);
let sets = 1;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also can you clarify since I haven't looked at a 1.33 object data diff: the entire modification table is now wrapped in an additional loop? What do the sets represent?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not really sure, I'll have to do some research, this was copied from mdx-model-viewer

@ChiefOfGxBxL ChiefOfGxBxL Dec 31, 2023

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea if you could also dig around to figure out what it is, that would be useful.

Here's an experiment I did:

  1. Create a new map in World Editor
  2. Modify the (human) Peasant original object - edit just one field, in my case base damage
  3. Annotate the bytes in the .w3u file to check for new data

I am seeing some new bytes that aren't accounted for in the version 2 of the parser:

03 00 00 00  :: version 3

01 00 00 00  :: num table modifications (original)

:: modification table 1 - Peasant
68 70 65 61  :: "hpea" (human peasant)
00 00 00 00  :: custom id - 0 = original object
01 00 00 00  :: modifications to this object: 1

00 00 00 00  :: <<<< NEW 4 BYTES ??
01 00 00 00  :: <<<< NEW 4 BYTES ??

  :: modification 1 - Peasant - Base Damage
  75 61 31 62  :: mod ID: "ua1b" (unit attack 1 - base damage)
  00 00 00 00  :: mod type: 0 = int
  0C 00 00 00  :: value = 12 (base damage updated to 12)
  68 70 65 61  :: "hpea" (repeated object ID)

00 00 00 00  :: num table modifications (custom)

const setsFlag: number[] = [];
if (fileVersion >= 3) {
sets = outBufferToJSON.readInt();
}

if (type === ObjectType.Doodads || type === ObjectType.Abilities || type === ObjectType.Upgrades) {
modification.level = outBufferToJSON.readInt();
modification.column = outBufferToJSON.readInt();
for (let set = 0; set < sets; set++) {
if(fileVersion >=3) {
setsFlag[set] = outBufferToJSON.readInt();

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is setsFlag information we want to store in the JSON object to write it back to a w3-file later?

It doesn't look like you're using this variable anywhere.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could probably remove as we don't patch existing object Giles, only generate new ones

}
const modificationCount = outBufferToJSON.readInt();
for (let j = 0; j < modificationCount; j++) {
const modification: Modification = {
id: '',
type: ModificationType.string,
level: 0,
column: 0,
value: {}
};

modification.id = outBufferToJSON.readChars(4);
modification.type = this.varTypes[outBufferToJSON.readInt()]; // 'int' | 'real' | 'unreal' | 'string',

if (type === ObjectType.Doodads || type === ObjectType.Abilities || type === ObjectType.Upgrades) {
modification.level = outBufferToJSON.readInt();
modification.column = outBufferToJSON.readInt();
}

if (modification.type === 'int') {
modification.value = outBufferToJSON.readInt();
} else if (modification.type === 'real' || modification.type === 'unreal') {
modification.value = outBufferToJSON.readFloat();
} else { // modification.type === 'string'
modification.value = outBufferToJSON.readString();
}
if (modification.type === 'int') {
modification.value = outBufferToJSON.readInt();
} else if (modification.type === 'real' || modification.type === 'unreal') {
modification.value = outBufferToJSON.readFloat();
} else { // modification.type === 'string'
modification.value = outBufferToJSON.readString();
}

if (isOriginalTable) {
outBufferToJSON.readInt(); // should be 0 for original objects
} else {
outBufferToJSON.readChars(4); // should be object ID for custom objects
if (isOriginalTable) {
outBufferToJSON.readInt(); // should be 0 for original objects
} else {
outBufferToJSON.readChars(4); // should be object ID for custom objects
}
objectDefinition.push(modification);
}

objectDefinition.push(modification);
}

if (isOriginalTable) {
Expand Down