Skip to content

Commit c85e503

Browse files
committed
wip. make binary reader easier to debug
1 parent 497718d commit c85e503

File tree

5 files changed

+22
-10
lines changed

5 files changed

+22
-10
lines changed

src/binary/bitwriter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export class BitWriter {
99
}
1010

1111
public WriteBit(value: number): BitWriter {
12-
if (this.offset > this.bits.length) {
12+
if (this.offset >= this.bits.length) {
1313
const resized = new Uint8Array(this.bits.length + 8192);
1414
resized.set(this.bits, 0);
1515
this.bits = resized;

src/d2/attribute_enhancer.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,8 @@ function _groupAttributes(all_attributes: types.IMagicProperty[], constants: typ
484484
if (prop.dF === 23) {
485485
return e.id === magic_attribute.id && e.values[0] === magic_attribute.values[0] && e.values[1] === magic_attribute.values[1];
486486
}
487-
if (prop.s === "state") {
487+
if (prop.s === "state"
488+
|| prop.s === "item_nonclassskill") {
488489
//state
489490
return e.id === magic_attribute.id && e.values[0] === magic_attribute.values[0] && e.values[1] === magic_attribute.values[1];
490491
}

src/d2/items.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,7 @@ export function _writeMagicProperties(writer: BitWriter, properties: types.IMagi
682682
default:
683683
break;
684684
}
685-
writer.WriteUInt16(param, prop.sP);
685+
writer.WriteUInt32(param, prop.sP);
686686
}
687687
let v = property.values[valueIdx++]!;
688688
if (prop.sA) {
@@ -698,7 +698,7 @@ export function _writeMagicProperties(writer: BitWriter, properties: types.IMagi
698698
if (!prop.sB) {
699699
throw new Error(`Save Bits is undefined for stat: ${property.id}:${prop.s}`);
700700
}
701-
writer.WriteUInt16(v, prop.sB);
701+
writer.WriteUInt32(v, prop.sB);
702702
}
703703
}
704704
}

tests/d2/stash.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ describe("stash", () => {
1414
expect(jsonData.version, "version").to.eq("02");
1515
});
1616

17-
xit("should provide read and write consistency for plugy shared stash file", async () => {
17+
it("should provide read and write consistency for plugy shared stash file", async () => {
1818
const buffer = fs.readFileSync(path.join(__dirname, `../../examples/stash/_LOD_SharedStashSave.sss`));
1919
const jsonData = await read(buffer, constants, 0x60);
2020
const newBuffer = await write(jsonData, constants, 0x60);

tests/util.spec.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,23 @@ describe("utils", () => {
1515
});
1616

1717
it("should read/write", async () => {
18-
const buffer = Buffer.from("4a4d100880", "hex");
19-
const reader = new BitReader(buffer);
2018
const writer = new BitWriter();
21-
writer.WriteByte(reader.ReadByte()).WriteUInt16(reader.ReadUInt16()).WriteBytes(reader.ReadBytes(2));
22-
const result = Buffer.from(writer.ToArray()).toString("hex");
23-
expect(result).to.eq("4a4d100880");
19+
for(let i = 0; i < 90; i ++) {
20+
writer.WriteByte(125)
21+
.WriteUInt16(125)
22+
.WriteUInt16(125, 9)
23+
.WriteUInt32(125)
24+
.WriteUInt32(125, 27);
25+
}
26+
27+
const reader = new BitReader(writer.ToArray());
28+
for(let i = 0; i < 90; i ++) {
29+
expect(reader.ReadByte()).to.eq(125);
30+
expect(reader.ReadUInt16()).to.eq(125);
31+
expect(reader.ReadUInt16(9)).to.eq(125);
32+
expect(reader.ReadUInt32()).to.eq(125);
33+
expect(reader.ReadUInt32(27)).to.eq(125);
34+
}
2435
});
2536

2637
it("should write bit", async () => {

0 commit comments

Comments
 (0)