@d2runewizard/d2s 2.0.125 → 2.0.126

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/lib/binary/bitreader.d.ts +23 -23
  2. package/lib/binary/bitreader.js +107 -107
  3. package/lib/binary/bitwriter.d.ts +21 -21
  4. package/lib/binary/bitwriter.js +117 -117
  5. package/lib/binary/constants.d.ts +4 -4
  6. package/lib/binary/constants.js +8 -8
  7. package/lib/binary/seekToSequence.d.ts +2 -2
  8. package/lib/binary/seekToSequence.js +12 -12
  9. package/lib/d2/attribute_enhancer.d.ts +5 -5
  10. package/lib/d2/attribute_enhancer.js +595 -595
  11. package/lib/d2/attributes.d.ts +4 -4
  12. package/lib/d2/attributes.js +98 -98
  13. package/lib/d2/constants.d.ts +4 -4
  14. package/lib/d2/constants.js +15 -15
  15. package/lib/d2/d2s.d.ts +10 -10
  16. package/lib/d2/d2s.js +115 -115
  17. package/lib/d2/header.d.ts +8 -8
  18. package/lib/d2/header.js +94 -94
  19. package/lib/d2/items.d.ts +19 -19
  20. package/lib/d2/items.js +869 -831
  21. package/lib/d2/items.js.map +1 -1
  22. package/lib/d2/skills.d.ts +4 -4
  23. package/lib/d2/skills.js +45 -45
  24. package/lib/d2/stash.d.ts +3 -3
  25. package/lib/d2/stash.js +243 -243
  26. package/lib/d2/types.d.ts +438 -420
  27. package/lib/d2/types.js +14 -14
  28. package/lib/d2/versions/default_header.d.ts +5 -5
  29. package/lib/d2/versions/default_header.js +750 -750
  30. package/lib/data/parser.d.ts +3 -3
  31. package/lib/data/parser.js +626 -626
  32. package/lib/data/versions/105_constant_data.d.ts +16150 -16150
  33. package/lib/data/versions/105_constant_data.js +24316 -24316
  34. package/lib/data/versions/96_constant_data.d.ts +14427 -14427
  35. package/lib/data/versions/96_constant_data.js +4 -4
  36. package/lib/data/versions/99_constant_data.d.ts +15572 -15572
  37. package/lib/data/versions/99_constant_data.js +4 -4
  38. package/lib/index.d.ts +8 -8
  39. package/lib/index.js +46 -46
  40. package/package.json +46 -46
package/lib/d2/items.js CHANGED
@@ -1,832 +1,870 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports._writeMagicProperties = exports._readMagicProperties = exports.writeItem = exports.readItem = exports.writeItems = exports.readItems = exports.writeCorpseItem = exports.readCorpseItems = exports.writeDemon = exports.readDemon = exports.writeGolemItems = exports.readGolemItems = exports.writeMercItems = exports.readMercItems = exports.writeCharItems = exports.readCharItems = void 0;
4
- const bitreader_1 = require("../binary/bitreader");
5
- const bitwriter_1 = require("../binary/bitwriter");
6
- var ItemType;
7
- (function (ItemType) {
8
- ItemType[ItemType["Armor"] = 1] = "Armor";
9
- ItemType[ItemType["Shield"] = 2] = "Shield";
10
- ItemType[ItemType["Weapon"] = 3] = "Weapon";
11
- ItemType[ItemType["Other"] = 4] = "Other";
12
- })(ItemType || (ItemType = {}));
13
- var Quality;
14
- (function (Quality) {
15
- Quality[Quality["Low"] = 1] = "Low";
16
- Quality[Quality["Normal"] = 2] = "Normal";
17
- Quality[Quality["Superior"] = 3] = "Superior";
18
- Quality[Quality["Magic"] = 4] = "Magic";
19
- Quality[Quality["Set"] = 5] = "Set";
20
- Quality[Quality["Rare"] = 6] = "Rare";
21
- Quality[Quality["Unique"] = 7] = "Unique";
22
- Quality[Quality["Crafted"] = 8] = "Crafted";
23
- })(Quality || (Quality = {}));
24
- // prettier-ignore
25
- //huffman tree
26
- const HUFFMAN = [[[[["w", "u"], [["8", ["y", ["5", ["j", []]]]], "h"]], ["s", [["2", "n"], "x"]]], [[["c", ["k", "f"]], "b"], [["t", "m"], ["9", "7"]]]], [" ", [[[["e", "d"], "p"], ["g", [[["z", "q"], "3"], ["v", "6"]]]], [["r", "l"], ["a", [["1", ["4", "0"]], ["i", "o"]]]]]]];
27
- // prettier-ignore
28
- const HUFFMAN_LOOKUP = { "0": { "v": 223, "l": 8 }, "1": { "v": 31, "l": 7 }, "2": { "v": 12, "l": 6 }, "3": { "v": 91, "l": 7 }, "4": { "v": 95, "l": 8 }, "5": { "v": 104, "l": 8 }, "6": { "v": 123, "l": 7 }, "7": { "v": 30, "l": 5 }, "8": { "v": 8, "l": 6 }, "9": { "v": 14, "l": 5 }, " ": { "v": 1, "l": 2 }, "a": { "v": 15, "l": 5 }, "b": { "v": 10, "l": 4 }, "c": { "v": 2, "l": 5 }, "d": { "v": 35, "l": 6 }, "e": { "v": 3, "l": 6 }, "f": { "v": 50, "l": 6 }, "g": { "v": 11, "l": 5 }, "h": { "v": 24, "l": 5 }, "i": { "v": 63, "l": 7 }, "j": { "v": 232, "l": 9 }, "k": { "v": 18, "l": 6 }, "l": { "v": 23, "l": 5 }, "m": { "v": 22, "l": 5 }, "n": { "v": 44, "l": 6 }, "o": { "v": 127, "l": 7 }, "p": { "v": 19, "l": 5 }, "q": { "v": 155, "l": 8 }, "r": { "v": 7, "l": 5 }, "s": { "v": 4, "l": 4 }, "t": { "v": 6, "l": 5 }, "u": { "v": 16, "l": 5 }, "v": { "v": 59, "l": 7 }, "w": { "v": 0, "l": 5 }, "x": { "v": 28, "l": 5 }, "y": { "v": 40, "l": 7 }, "z": { "v": 27, "l": 8 } };
29
- async function readCharItems(char, reader, constants, config) {
30
- char.items = await readItems(reader, char.header.version, constants, config, char);
31
- }
32
- exports.readCharItems = readCharItems;
33
- async function writeCharItems(char, constants, config) {
34
- const writer = new bitwriter_1.BitWriter();
35
- writer.WriteArray(await writeItems(char.items, char.header.version, constants, config));
36
- return writer.ToArray();
37
- }
38
- exports.writeCharItems = writeCharItems;
39
- async function readMercItems(char, reader, constants, config) {
40
- char.merc_items = [];
41
- const header = reader.ReadString(2); //0x0000 [merc item list header = "jf"]
42
- if (header !== "jf") {
43
- // header is not present in first save after char is created
44
- if ((char === null || char === void 0 ? void 0 : char.header.level) === 1) {
45
- return;
46
- }
47
- throw new Error(`Mercenary header 'jf' not found at position ${reader.offset - 2 * 8}`);
48
- }
49
- if (char.header.merc_id && parseInt(char.header.merc_id, 16) !== 0) {
50
- char.merc_items = await readItems(reader, char.header.version, constants, config, char);
51
- }
52
- }
53
- exports.readMercItems = readMercItems;
54
- async function writeMercItems(char, constants, config) {
55
- const writer = new bitwriter_1.BitWriter();
56
- writer.WriteString("jf", 2);
57
- if (char.header.merc_id && parseInt(char.header.merc_id, 16) !== 0) {
58
- char.merc_items = char.merc_items || [];
59
- writer.WriteArray(await writeItems(char.merc_items, char.header.version, constants, config));
60
- }
61
- return writer.ToArray();
62
- }
63
- exports.writeMercItems = writeMercItems;
64
- async function readGolemItems(char, reader, constants, config) {
65
- const header = reader.ReadString(2); //0x0000 [golem item list header = "kf"]
66
- if (header !== "kf") {
67
- // header is not present in first save after char is created
68
- if ((char === null || char === void 0 ? void 0 : char.header.level) === 1) {
69
- return;
70
- }
71
- throw new Error(`Golem header 'kf' not found at position ${reader.offset - 2 * 8}`);
72
- }
73
- const has_golem = reader.ReadUInt8();
74
- if (has_golem === 1) {
75
- char.golem_item = await readItem(reader, char.header.version, constants, config);
76
- }
77
- }
78
- exports.readGolemItems = readGolemItems;
79
- async function writeGolemItems(char, constants, config) {
80
- const writer = new bitwriter_1.BitWriter();
81
- writer.WriteString("kf", 2);
82
- if (char.golem_item) {
83
- writer.WriteUInt8(1);
84
- writer.WriteArray(await writeItem(char.golem_item, char.header.version, constants, config));
85
- }
86
- else {
87
- writer.WriteUInt8(0);
88
- }
89
- return writer.ToArray();
90
- }
91
- exports.writeGolemItems = writeGolemItems;
92
- async function readDemon(char, reader, constants, config) {
93
- reader.SkipBytes(2);
94
- const header = reader.ReadString(2); // 0x6C 0x66 lf Demon header
95
- if (header === "lf") {
96
- char.demon = {
97
- data: Array.from(reader.ReadBytes((reader.bits.length - reader.offset) / 8)),
98
- };
99
- }
100
- }
101
- exports.readDemon = readDemon;
102
- async function writeDemon(char, constants, config) {
103
- var _a;
104
- const writer = new bitwriter_1.BitWriter();
105
- writer.WriteArray(new Uint8Array([0x01, 0x00]));
106
- writer.WriteString("lf", 2);
107
- if ((_a = char.demon) === null || _a === void 0 ? void 0 : _a.data) {
108
- writer.WriteBytes(new Uint8Array(char.demon.data));
109
- }
110
- else {
111
- writer.WriteArray(new Uint8Array([0x00, 0x00]));
112
- }
113
- return writer.ToArray();
114
- }
115
- exports.writeDemon = writeDemon;
116
- async function readCorpseItems(char, reader, constants, config) {
117
- char.corpse_items = [];
118
- const header = reader.ReadString(2); //0x0000 [item list header = 0x4a, 0x4d "JM"]
119
- if (header !== "JM") {
120
- // header is not present in first save after char is created
121
- if (char.header.level === 1) {
122
- char.is_dead = 0;
123
- return;
124
- }
125
- throw new Error(`Corpse header 'JM' not found at position ${reader.offset - 2 * 8}`);
126
- }
127
- char.is_dead = reader.ReadUInt16(); //0x0002 [corpse count]
128
- for (let i = 0; i < char.is_dead; i++) {
129
- reader.SkipBytes(12); //0x0004 [unk4, x_pos, y_pos]
130
- char.corpse_items = char.corpse_items.concat(await readItems(reader, char.header.version, constants, config, char));
131
- }
132
- }
133
- exports.readCorpseItems = readCorpseItems;
134
- async function writeCorpseItem(char, constants, config) {
135
- const writer = new bitwriter_1.BitWriter();
136
- writer.WriteString("JM", 2);
137
- writer.WriteUInt16(char.is_dead);
138
- //json struct doesnt support multiple corpses without modifying it
139
- if (char.is_dead) {
140
- writer.WriteArray(new Uint8Array(12));
141
- char.corpse_items = char.corpse_items || [];
142
- writer.WriteArray(await writeItems(char.corpse_items, char.header.version, constants, config));
143
- }
144
- return writer.ToArray();
145
- }
146
- exports.writeCorpseItem = writeCorpseItem;
147
- async function readItems(reader, version, constants, config, char) {
148
- const items = [];
149
- const header = reader.ReadString(2); //0x0000 [item list header = 0x4a, 0x4d "JM"]
150
- if (header !== "JM") {
151
- // header is not present in first save after char is created
152
- if ((char === null || char === void 0 ? void 0 : char.header.level) === 1) {
153
- return []; // TODO: return starter items based on class
154
- }
155
- throw new Error(`Item list header 'JM' not found at position ${reader.offset - 2 * 8}`);
156
- }
157
- const count = reader.ReadUInt16(); //0x0002
158
- for (let i = 0; i < count; i++) {
159
- /*if (version === 0x69) {
160
- const bitStr = reader.bits.join("");
161
- const searchStart = reader.offset;
162
- const marker1 = "000000001 00001000 10100000".replace(/ /g, ""); // search for next item
163
- const marker2 = "000000001 00000000 10100000".replace(/ /g, ""); // search for next item
164
- const id1 = bitStr.indexOf(marker1, searchStart);
165
- const id2 = bitStr.indexOf(marker2, searchStart);
166
- const id = id1 !== -1 && id2 !== -1 ? Math.min(id1, id2) : Math.max(id1, id2);
167
- reader.SeekBit(id - 15);
168
- }*/
169
- items.push(await readItem(reader, version, constants, config));
170
- }
171
- return items;
172
- }
173
- exports.readItems = readItems;
174
- async function writeItems(items, version, constants, config) {
175
- const writer = new bitwriter_1.BitWriter();
176
- writer.WriteString("JM", 2);
177
- writer.WriteUInt16(items.length);
178
- for (let i = 0; i < items.length; i++) {
179
- writer.WriteArray(await writeItem(items[i], version, constants, config));
180
- }
181
- return writer.ToArray();
182
- }
183
- exports.writeItems = writeItems;
184
- async function readItem(reader, version, originalConstants, config, parent) {
185
- if (version <= 0x60) {
186
- const header = reader.ReadString(2); //0x0000 [item header = 0x4a, 0x4d "JM"]
187
- if (header !== "JM") {
188
- throw new Error(`Item header 'JM' not found at position ${reader.offset - 2 * 8}`);
189
- }
190
- }
191
- const constants = originalConstants;
192
- const item = {};
193
- _readSimpleBits(item, reader, version, constants, config);
194
- if (!item.simple_item) {
195
- item.id = reader.ReadUInt32(32);
196
- item.level = reader.ReadUInt8(7);
197
- item.quality = reader.ReadUInt8(4);
198
- item.multiple_pictures = reader.ReadBit();
199
- if (item.multiple_pictures) {
200
- item.picture_id = reader.ReadUInt8(3);
201
- }
202
- item.class_specific = reader.ReadBit();
203
- if (item.class_specific) {
204
- item.auto_affix_id = reader.ReadUInt16(11);
205
- }
206
- switch (item.quality) {
207
- case Quality.Low:
208
- item.low_quality_id = reader.ReadUInt8(3);
209
- break;
210
- case Quality.Normal:
211
- break;
212
- case Quality.Superior:
213
- item.file_index = reader.ReadUInt8(3);
214
- break;
215
- case Quality.Magic:
216
- item.magic_prefix = reader.ReadUInt16(11);
217
- if (item.magic_prefix)
218
- item.magic_prefix_name = constants.magic_prefixes[item.magic_prefix] ? constants.magic_prefixes[item.magic_prefix].n : null;
219
- item.magic_suffix = reader.ReadUInt16(11);
220
- if (item.magic_suffix)
221
- item.magic_suffix_name = constants.magic_suffixes[item.magic_suffix] ? constants.magic_suffixes[item.magic_suffix].n : null;
222
- break;
223
- case Quality.Set:
224
- item.set_id = reader.ReadUInt16(12);
225
- item.set_name = constants.set_items[item.set_id] ? constants.set_items[item.set_id].n : null;
226
- break;
227
- case Quality.Unique:
228
- item.unique_id = reader.ReadUInt16(12);
229
- item.unique_name = constants.unq_items[item.unique_id] ? constants.unq_items[item.unique_id].n : null;
230
- break;
231
- case Quality.Rare:
232
- case Quality.Crafted:
233
- item.rare_name_id = reader.ReadUInt8(8);
234
- if (item.rare_name_id)
235
- item.rare_name = constants.rare_names[item.rare_name_id] ? constants.rare_names[item.rare_name_id].n : null;
236
- item.rare_name_id2 = reader.ReadUInt8(8);
237
- if (item.rare_name_id2)
238
- item.rare_name2 = constants.rare_names[item.rare_name_id2] ? constants.rare_names[item.rare_name_id2].n : null;
239
- item.magical_name_ids = [];
240
- for (let i = 0; i < 6; i++) {
241
- const prefix = reader.ReadBit();
242
- if (prefix === 1) {
243
- item.magical_name_ids[i] = reader.ReadUInt16(11);
244
- }
245
- else {
246
- item.magical_name_ids[i] = null;
247
- }
248
- }
249
- break;
250
- default:
251
- break;
252
- }
253
- if (item.given_runeword) {
254
- item.runeword_id = reader.ReadUInt16(12);
255
- //fix delerium on d2gs??? why is this a thing?
256
- if (item.runeword_id == 2718) {
257
- item.runeword_id = 48;
258
- }
259
- const mappedId = {
260
- 2784: 196,
261
- 2785: 197,
262
- 2786: 198,
263
- 2787: 199,
264
- 2788: 200,
265
- 2789: 201,
266
- 2790: 202,
267
- 2791: 203,
268
- 2792: 204,
269
- 3074: 205,
270
- 3075: 206,
271
- 3076: 207,
272
- }[item.runeword_id] || item.runeword_id;
273
- if (constants.runewords[mappedId]) {
274
- item.runeword_name = constants.runewords[mappedId].n;
275
- }
276
- reader.ReadUInt8(4);
277
- }
278
- if (item.personalized) {
279
- const arr = new Uint8Array(16);
280
- for (let i = 0; i < arr.length; i++) {
281
- if (version > 0x61) {
282
- arr[i] = reader.ReadUInt8(8);
283
- }
284
- else {
285
- arr[i] = reader.ReadUInt8(7);
286
- }
287
- if (arr[i] === 0x00) {
288
- break;
289
- }
290
- }
291
- item.personalized_name = new bitreader_1.BitReader(arr).ReadString(16).trim().replace(/\0/g, "");
292
- }
293
- //tomes
294
- if (item.type === "tbk" || item.type == "ibk") {
295
- reader.ReadUInt8(5);
296
- }
297
- //realm data
298
- item.timestamp = reader.ReadUInt8(1);
299
- if (item.type_id === ItemType.Armor) {
300
- item.defense_rating = reader.ReadUInt16(constants.magical_properties[31].sB) - constants.magical_properties[31].sA;
301
- }
302
- if (item.type_id === ItemType.Armor || item.type_id === ItemType.Weapon) {
303
- item.max_durability = reader.ReadUInt16(constants.magical_properties[73].sB) - constants.magical_properties[73].sA;
304
- if (item.max_durability > 0) {
305
- item.current_durability = reader.ReadUInt16(constants.magical_properties[72].sB) - constants.magical_properties[72].sA;
306
- }
307
- }
308
- if (version === 0x69) {
309
- reader.SkipBits(1); //unknown bit in RotW
310
- }
311
- if (constants.stackables[item.type]) {
312
- item.quantity = reader.ReadUInt16(9);
313
- }
314
- if (item.socketed === 1) {
315
- item.total_nr_of_sockets = reader.ReadUInt8(4);
316
- }
317
- /**
318
- * 5 bits. any of the 5 bits can be set. if a bit is set that means
319
- * means +1 to the set_list_count
320
- */
321
- let plist_flag = 0;
322
- if (item.quality === Quality.Set) {
323
- plist_flag = reader.ReadUInt8(5);
324
- item.set_list_count = 0;
325
- item._unknown_data.plist_flag = plist_flag;
326
- }
327
- //magical properties
328
- const skipMagicPeroperties = ["qll", "spe", "sol", "fng", "jaw", "scz", "flg", "tal"].includes(item.type);
329
- let magic_attributes = _readMagicProperties(reader, constants, skipMagicPeroperties);
330
- item.magic_attributes = magic_attributes;
331
- while (plist_flag > 0) {
332
- if (plist_flag & 1) {
333
- item.set_list_count += 1;
334
- magic_attributes = _readMagicProperties(reader, constants);
335
- if (item.set_attributes) {
336
- item.set_attributes.push(magic_attributes);
337
- }
338
- else {
339
- item.set_attributes = [magic_attributes];
340
- }
341
- }
342
- plist_flag >>>= 1;
343
- }
344
- if (item.given_runeword === 1) {
345
- magic_attributes = _readMagicProperties(reader, constants);
346
- if (magic_attributes && magic_attributes.length > 0) {
347
- item.runeword_attributes = magic_attributes;
348
- }
349
- }
350
- }
351
- /*if (item._unknown_data.b27_31 && item._unknown_data.b27_31?.some((b) => b === 1)) {
352
- reader.SkipBits(53); // skip 50 - 53
353
- reader.Align();
354
- }*/
355
- if (version === 0x69) {
356
- if (!item.identified && (item.quality === Quality.Unique || item.quality === Quality.Set)) {
357
- item.chronicle = {
358
- monsterId: reader.ReadUInt16(),
359
- timestamp: reader.ReadUInt32(),
360
- };
361
- reader.SkipBits(4);
362
- }
363
- item._unknown_data.chest_stackable = reader.ReadBit(); //unknown bit in RotW (stackable in chest?)
364
- if (item._unknown_data.chest_stackable) {
365
- item.amount_in_shared_stash = reader.ReadUInt8();
366
- }
367
- }
368
- reader.Align();
369
- if (item.nr_of_items_in_sockets > 0 && item.simple_item === 0) {
370
- item.socketed_items = [];
371
- for (let i = 0; i < item.nr_of_items_in_sockets; i++) {
372
- item.socketed_items.push(await readItem(reader, version, constants, config, item));
373
- }
374
- }
375
- //console.log(JSON.stringify(item));
376
- return item;
377
- }
378
- exports.readItem = readItem;
379
- async function writeItem(item, version, constants, config) {
380
- var _a;
381
- if (item._unknown_data === undefined) {
382
- item._unknown_data = {};
383
- }
384
- if (item.categories === undefined) {
385
- item.categories = (_a = _GetItemTXT(item, constants)) === null || _a === void 0 ? void 0 : _a.c;
386
- }
387
- const writer = new bitwriter_1.BitWriter();
388
- if (version <= 0x60) {
389
- writer.WriteString("JM", 2);
390
- }
391
- _writeSimpleBits(writer, version, item, constants, config);
392
- if (!item.simple_item) {
393
- writer.WriteUInt32(item.id, 32);
394
- writer.WriteUInt8(item.level, 7);
395
- writer.WriteUInt8(item.quality, 4);
396
- writer.WriteUInt8(item.multiple_pictures, 1);
397
- if (item.multiple_pictures) {
398
- writer.WriteUInt8(item.picture_id, 3);
399
- }
400
- writer.WriteUInt8(item.class_specific, 1);
401
- if (item.class_specific === 1) {
402
- writer.WriteUInt16(item.auto_affix_id || 0, 11);
403
- }
404
- switch (item.quality) {
405
- case Quality.Low:
406
- writer.WriteUInt8(item.low_quality_id, 3);
407
- break;
408
- case Quality.Normal:
409
- break;
410
- case Quality.Superior:
411
- writer.WriteUInt8(item.file_index || 0, 3);
412
- break;
413
- case Quality.Magic:
414
- writer.WriteUInt16(item.magic_prefix, 11);
415
- writer.WriteUInt16(item.magic_suffix, 11);
416
- break;
417
- case Quality.Set:
418
- writer.WriteUInt16(item.set_id, 12);
419
- break;
420
- case Quality.Unique:
421
- writer.WriteUInt16(item.unique_id, 12);
422
- break;
423
- case Quality.Rare:
424
- case Quality.Crafted:
425
- writer.WriteUInt8(item.rare_name_id !== undefined ? item.rare_name_id : _lookupRareId(item.rare_name, constants), 8);
426
- writer.WriteUInt8(item.rare_name_id2 !== undefined ? item.rare_name_id2 : _lookupRareId(item.rare_name2, constants), 8);
427
- for (let i = 0; i < 6; i++) {
428
- const magical_name_id = item.magical_name_ids[i];
429
- if (magical_name_id) {
430
- writer.WriteBit(1);
431
- writer.WriteUInt16(magical_name_id, 11);
432
- }
433
- else {
434
- writer.WriteBit(0);
435
- }
436
- }
437
- break;
438
- default:
439
- break;
440
- }
441
- if (item.given_runeword) {
442
- //fix delerium on d2gs??? why is this a thing?
443
- let runeword_id = item.runeword_id;
444
- if (runeword_id == 2718) {
445
- runeword_id = 48;
446
- }
447
- writer.WriteUInt16(runeword_id, 12);
448
- writer.WriteUInt8(5, 4); //always 5?
449
- }
450
- if (item.personalized) {
451
- const name = item.personalized_name.substring(0, 16);
452
- for (let i = 0; i < name.length; i++) {
453
- if (version > 0x61) {
454
- writer.WriteUInt8(name.charCodeAt(i), 8);
455
- }
456
- else {
457
- writer.WriteUInt8(name.charCodeAt(i) & 0x7f, 7);
458
- }
459
- }
460
- writer.WriteUInt8(0x00, version > 0x61 ? 8 : 7);
461
- }
462
- if (item.type === "tbk") {
463
- writer.WriteUInt8(0, 5);
464
- }
465
- else if (item.type === "ibk") {
466
- writer.WriteUInt8(1, 5);
467
- }
468
- writer.WriteUInt8(item.timestamp, 1);
469
- if (item.type_id === ItemType.Armor || item.type_id === ItemType.Shield) {
470
- writer.WriteUInt16(item.defense_rating + constants.magical_properties[31].sA, constants.magical_properties[31].sB);
471
- }
472
- if (item.type_id === ItemType.Armor || item.type_id === ItemType.Shield || item.type_id === ItemType.Weapon) {
473
- writer.WriteUInt16(item.max_durability || 0, constants.magical_properties[73].sB);
474
- if (item.max_durability > 0) {
475
- writer.WriteUInt16(item.current_durability, constants.magical_properties[72].sB);
476
- }
477
- }
478
- if (version === 0x69) {
479
- writer.WriteUInt8(constants.stackables[item.type] ? 1 : 0, 1); //unknown bit in RotW
480
- }
481
- if (constants.stackables[item.type]) {
482
- writer.WriteUInt16(item.quantity, 9);
483
- }
484
- if (item.socketed === 1) {
485
- writer.WriteUInt8(item.total_nr_of_sockets, 4);
486
- }
487
- if (item.quality === Quality.Set) {
488
- const set_attribute_count = item.set_attributes != null ? item.set_attributes.length : 0;
489
- //reduced by -1 removed as this seems to be wrong
490
- const plist_flag = (1 << set_attribute_count) - 1;
491
- writer.WriteUInt8(item._unknown_data.plist_flag || plist_flag, 5);
492
- }
493
- _writeMagicProperties(writer, item.magic_attributes, constants);
494
- if (item.set_attributes && item.set_attributes.length > 0) {
495
- for (let i = 0; i < item.set_attributes.length; i++) {
496
- _writeMagicProperties(writer, item.set_attributes[i], constants);
497
- }
498
- }
499
- if (item.given_runeword === 1) {
500
- _writeMagicProperties(writer, item.runeword_attributes, constants);
501
- }
502
- }
503
- if (version === 0x69) {
504
- if (!item.identified && (item.quality === Quality.Unique || item.quality === Quality.Set) && item.chronicle) {
505
- writer.WriteUInt16(item.chronicle.monsterId);
506
- writer.WriteUInt32(item.chronicle.timestamp);
507
- writer.WriteUInt8(0, 4); //unknown bit in RotW
508
- }
509
- writer.WriteUInt8(item._unknown_data.chest_stackable || 0, 1);
510
- if (item._unknown_data.chest_stackable) {
511
- writer.WriteUInt8(item.amount_in_shared_stash || 0, 8); //unknown bit in RotW (stackable in chest?)
512
- }
513
- }
514
- writer.Align();
515
- if (item.nr_of_items_in_sockets > 0 && item.simple_item === 0) {
516
- for (let i = 0; i < item.nr_of_items_in_sockets; i++) {
517
- writer.WriteArray(await writeItem(item.socketed_items[i], version, constants, config));
518
- }
519
- }
520
- return writer.ToArray();
521
- }
522
- exports.writeItem = writeItem;
523
- function _readSimpleBits(item, reader, version, constants, config) {
524
- var _a, _b, _c;
525
- //init so we do not have npe's
526
- item._unknown_data = {};
527
- //1.10-1.14d
528
- //[flags:32][version:10][mode:3]([invloc:4][x:4][y:4][page:3])([itemcode:32])([sockets:3])
529
- //1.15
530
- //[flags:32][version:3][mode:3]([invloc:4][x:4][y:4][page:3])([itemcode:variable])([sockets:3])
531
- item._unknown_data.b0_3 = reader.ReadBitArray(4); // 0 byte 1
532
- item.identified = reader.ReadBit(); // 4
533
- item._unknown_data.b5_10 = reader.ReadBitArray(6); // 5
534
- item.socketed = reader.ReadBit(); // 11
535
- item._unknown_data.b12 = reader.ReadBitArray(1); // 12
536
- item.new = reader.ReadBit(); // 13
537
- item._unknown_data.b14_15 = reader.ReadBitArray(2); // 14
538
- item.is_ear = reader.ReadBit(); // 16 byte 3
539
- item.starter_item = reader.ReadBit(); // 17
540
- item._unknown_data.b18_20 = reader.ReadBitArray(3); // 18
541
- item.simple_item = reader.ReadBit(); // 21
542
- item.ethereal = reader.ReadBit(); // 22
543
- item._unknown_data.b23 = reader.ReadBitArray(1); // 23
544
- item.personalized = reader.ReadBit(); // 24 byte 4
545
- item._unknown_data.b25 = reader.ReadBitArray(1); // 25
546
- item.given_runeword = reader.ReadBit(); // 26
547
- item._unknown_data.b27_31 = reader.ReadBitArray(5); // 27
548
- if (version <= 0x60) {
549
- item.version = reader.ReadUInt16(10).toString(10);
550
- }
551
- else if (version >= 0x61) {
552
- item.version = reader.ReadUInt16(3).toString(2); // 32 byte 5
553
- }
554
- item.location_id = reader.ReadUInt8(3); // 34
555
- item.equipped_id = reader.ReadUInt8(4); // 37
556
- item.position_x = reader.ReadUInt8(4); // 41
557
- item.position_y = reader.ReadUInt8(4); // 45
558
- item.alt_position_id = reader.ReadUInt8(3); // 49
559
- if (item.is_ear) {
560
- const clazz = reader.ReadUInt8(3);
561
- const level = reader.ReadUInt8(7);
562
- const arr = new Uint8Array(15);
563
- for (let i = 0; i < arr.length; i++) {
564
- arr[i] = reader.ReadUInt8(7);
565
- if (arr[i] === 0x00) {
566
- break;
567
- }
568
- }
569
- const name = new bitreader_1.BitReader(arr).ReadString(15).trim().replace(/\0/g, "");
570
- item.ear_attributes = {
571
- class: clazz,
572
- level: level,
573
- name: name,
574
- };
575
- }
576
- else {
577
- if (version <= 0x60) {
578
- item.type = reader.ReadString(4);
579
- }
580
- else if (version >= 0x61) {
581
- item.type = "";
582
- //props to d07riv
583
- //https://github.com/d07RiV/d07riv.github.io/blob/master/d2r.html#L11-L20
584
- for (let i = 0; i < 4; i++) {
585
- let node = HUFFMAN;
586
- do {
587
- node = node[reader.ReadBit()]; // 50
588
- } while (Array.isArray(node));
589
- item.type += node;
590
- }
591
- }
592
- item.type = item.type.trim().replace(/\0/g, "");
593
- let details = _GetItemTXT(item, constants);
594
- item.categories = details === null || details === void 0 ? void 0 : details.c;
595
- if ((_a = item === null || item === void 0 ? void 0 : item.categories) === null || _a === void 0 ? void 0 : _a.includes("Any Armor")) {
596
- item.type_id = ItemType.Armor;
597
- }
598
- else if ((_b = item === null || item === void 0 ? void 0 : item.categories) === null || _b === void 0 ? void 0 : _b.includes("Weapon")) {
599
- item.type_id = ItemType.Weapon;
600
- details = constants.weapon_items[item.type];
601
- }
602
- else {
603
- item.type_id = ItemType.Other;
604
- }
605
- let bits = item.simple_item ? 1 : 3;
606
- if ((_c = item.categories) === null || _c === void 0 ? void 0 : _c.includes("Quest")) {
607
- item.quest_difficulty = reader.ReadUInt16(constants.magical_properties[356].sB) - constants.magical_properties[356].sA;
608
- bits = 1;
609
- }
610
- item.nr_of_items_in_sockets = reader.ReadUInt8(bits);
611
- }
612
- }
613
- function _lookupRareId(name, constants) {
614
- //some inconsistencies with txt data and nokka. so have to hack it with startsWith
615
- return constants.rare_names.findIndex((k) => k && (k.n.toLowerCase().startsWith(name.toLowerCase()) || name.toLowerCase().startsWith(k.n.toLowerCase())));
616
- }
617
- function _writeSimpleBits(writer, version, item, constants, config) {
618
- var _a;
619
- writer.WriteBits(item._unknown_data.b0_3 || new Uint8Array(4), 4);
620
- writer.WriteBit(item.identified);
621
- writer.WriteBits(item._unknown_data.b5_10 || new Uint8Array(6), 6);
622
- writer.WriteBit(item.socketed);
623
- writer.WriteBits(item._unknown_data.b12 || new Uint8Array(1), 1);
624
- writer.WriteBit(item.new);
625
- writer.WriteBits(item._unknown_data.b14_15 || new Uint8Array(2), 2);
626
- writer.WriteBit(item.is_ear);
627
- writer.WriteBit(item.starter_item);
628
- writer.WriteBits(item._unknown_data.b18_20 || new Uint8Array(3), 3);
629
- writer.WriteBit(item.simple_item);
630
- writer.WriteBit(item.ethereal);
631
- writer.WriteBits(item._unknown_data.b23 || new Uint8Array([1]), 1); //always 1? IFLAG_JUSTSAVED
632
- writer.WriteBit(item.personalized);
633
- writer.WriteBits(item._unknown_data.b25 || new Uint8Array(1), 1); //IFLAG_LOWQUALITY
634
- writer.WriteBit(item.given_runeword);
635
- writer.WriteBits(item._unknown_data.b27_31 || new Uint8Array(5), 5);
636
- const itemVersion = item.version != null ? item.version : "101";
637
- if (version <= 0x60) {
638
- // 0 = pre-1.08; 1 = 1.08/1.09 normal; 2 = 1.10 normal; 100 = 1.08/1.09 expansion; 101 = 1.10 expansion
639
- writer.WriteUInt16(parseInt(itemVersion, 10), 10);
640
- }
641
- else if (version >= 0x61) {
642
- writer.WriteUInt16(parseInt(itemVersion, 2), 3);
643
- }
644
- writer.WriteUInt8(item.location_id, 3);
645
- writer.WriteUInt8(item.equipped_id, 4);
646
- writer.WriteUInt8(item.position_x, 4);
647
- writer.WriteUInt8(item.position_y, 4);
648
- writer.WriteUInt8(item.alt_position_id, 3);
649
- if (item.is_ear) {
650
- writer.WriteUInt8(item.ear_attributes.class, 3);
651
- writer.WriteUInt8(item.ear_attributes.level, 7);
652
- const name = item.ear_attributes.name.substring(0, 15);
653
- for (let i = 0; i < name.length; i++) {
654
- writer.WriteUInt8(name.charCodeAt(i) & 0x7f, 7);
655
- }
656
- writer.WriteUInt8(0x00, 7);
657
- }
658
- else {
659
- const t = item.type.padEnd(4, " ");
660
- if (version <= 0x60) {
661
- writer.WriteString(t, 4);
662
- }
663
- else {
664
- for (const c of t) {
665
- const n = HUFFMAN_LOOKUP[c];
666
- writer.WriteUInt16(n.v, n.l);
667
- }
668
- }
669
- let bits = item.simple_item ? 1 : 3;
670
- if ((_a = item.categories) === null || _a === void 0 ? void 0 : _a.includes("Quest")) {
671
- const difficulty = item.quest_difficulty || 0;
672
- writer.WriteUInt16(difficulty + constants.magical_properties[356].sA, constants.magical_properties[356].sB);
673
- bits = 1;
674
- }
675
- writer.WriteUInt8(item.nr_of_items_in_sockets, bits);
676
- }
677
- }
678
- function _readMagicProperties(reader, constants, skipMagicProps) {
679
- if (skipMagicProps) {
680
- // Scan for the 0x1FF end marker (9 bits of 1)
681
- const bitStr = reader.bits.join("");
682
- const searchStart = reader.offset;
683
- const marker = "111111111"; // 9-bit 0x1FF marker
684
- const idx = bitStr.indexOf(marker, searchStart);
685
- if (idx !== -1) {
686
- reader.SeekBit(idx + 9); // Move reader past the 0x1FF marker
687
- }
688
- else {
689
- console.warn("0x1FF end marker not found, skipping failed");
690
- }
691
- return []; // Return empty list since we're skipping parsing
692
- }
693
- let id = reader.ReadUInt16(9);
694
- const magic_attributes = [];
695
- let zeroCounter = 0;
696
- while (id != 0x1ff) {
697
- if (id === 0) {
698
- zeroCounter++;
699
- }
700
- if (zeroCounter > 10) {
701
- throw new Error(`Possible malformed data at position ${reader.offset - 9}`);
702
- }
703
- const values = [];
704
- if (id > constants.magical_properties.length) {
705
- throw new Error(`Invalid Stat Id: ${id} at position ${reader.offset - 9}`);
706
- }
707
- const num_of_properties = constants.magical_properties[id].np || 1;
708
- for (let i = 0; i < num_of_properties; i++) {
709
- const prop = constants.magical_properties[id + i];
710
- if (prop == null) {
711
- throw new Error(`Cannot find Magical Property for id: ${id} at position ${reader.offset}`);
712
- }
713
- if (prop.sP) {
714
- let param = reader.ReadUInt16(prop.sP);
715
- switch (prop.dF) {
716
- case 14: //+skill to skilltab
717
- values.push(param & 0x7);
718
- param = (param >> 3) & 0x1fff;
719
- break;
720
- default:
721
- break;
722
- }
723
- //encode
724
- switch (prop.e) {
725
- case 1:
726
- //throw new Error(`Unimplemented encoding: ${prop.encode}`);
727
- break;
728
- case 2: //chance to cast
729
- case 3: //charges
730
- values.push(param & 0x3f); //skill level
731
- param = (param >> 6) & 0x3ff; //skll id
732
- break;
733
- default:
734
- break;
735
- }
736
- values.push(param);
737
- }
738
- if (!prop.sB) {
739
- throw new Error(`Save Bits is undefined for stat: ${id}:${prop.s} at position ${reader.offset}`);
740
- }
741
- let v = reader.ReadUInt16(prop.sB);
742
- if (prop.sA) {
743
- v -= prop.sA;
744
- }
745
- switch (prop.e) {
746
- case 3:
747
- values.push(v & 0xff); // current charges
748
- values.push((v >> 8) & 0xff); //max charges
749
- break;
750
- default:
751
- values.push(v);
752
- break;
753
- }
754
- }
755
- magic_attributes.push({
756
- id: id,
757
- values: values,
758
- name: constants.magical_properties[id].s,
759
- });
760
- id = reader.ReadUInt16(9);
761
- }
762
- return magic_attributes;
763
- }
764
- exports._readMagicProperties = _readMagicProperties;
765
- function _writeMagicProperties(writer, properties, constants) {
766
- if (properties) {
767
- for (let i = 0; i < properties.length; i++) {
768
- const property = properties[i];
769
- let valueIdx = 0;
770
- writer.WriteUInt16(property.id, 9);
771
- const num_of_properties = constants.magical_properties[property.id].np || 1;
772
- for (let j = 0; j < num_of_properties; j++) {
773
- const prop = constants.magical_properties[property.id + j];
774
- if (prop == null) {
775
- throw new Error(`Cannot find Magical Property for id: ${property.id}`);
776
- }
777
- if (prop.sP) {
778
- let param = property.values[valueIdx++];
779
- switch (prop.dF) {
780
- case 14: //+skill to skilltab
781
- param |= (property.values[valueIdx++] & 0x1fff) << 3;
782
- break;
783
- default:
784
- break;
785
- }
786
- //encode
787
- switch (prop.e) {
788
- case 1:
789
- //throw new Error(`Unimplemented encoding: ${prop.encode}`);
790
- break;
791
- case 2: //chance to cast
792
- case 3: //charges
793
- param |= (property.values[valueIdx++] & 0x3ff) << 6;
794
- break;
795
- default:
796
- break;
797
- }
798
- writer.WriteUInt32(param, prop.sP);
799
- }
800
- let v = property.values[valueIdx++];
801
- if (prop.sA) {
802
- v += prop.sA;
803
- }
804
- switch (prop.e) {
805
- case 3:
806
- v |= (property.values[valueIdx++] & 0xff) << 8;
807
- break;
808
- default:
809
- break;
810
- }
811
- if (!prop.sB) {
812
- throw new Error(`Save Bits is undefined for stat: ${property.id}:${prop.s}`);
813
- }
814
- writer.WriteUInt32(v, prop.sB);
815
- }
816
- }
817
- }
818
- writer.WriteUInt16(0x1ff, 9);
819
- }
820
- exports._writeMagicProperties = _writeMagicProperties;
821
- function _GetItemTXT(item, constants) {
822
- if (constants.armor_items[item.type]) {
823
- return constants.armor_items[item.type];
824
- }
825
- else if (constants.weapon_items[item.type]) {
826
- return constants.weapon_items[item.type];
827
- }
828
- else if (constants.other_items[item.type]) {
829
- return constants.other_items[item.type];
830
- }
831
- }
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports._writeMagicProperties = exports._readMagicProperties = exports.writeItem = exports.readItem = exports.writeItems = exports.readItems = exports.writeCorpseItem = exports.readCorpseItems = exports.writeDemon = exports.readDemon = exports.writeGolemItems = exports.readGolemItems = exports.writeMercItems = exports.readMercItems = exports.writeCharItems = exports.readCharItems = void 0;
4
+ const bitreader_1 = require("../binary/bitreader");
5
+ const bitwriter_1 = require("../binary/bitwriter");
6
+ var ItemType;
7
+ (function (ItemType) {
8
+ ItemType[ItemType["Armor"] = 1] = "Armor";
9
+ ItemType[ItemType["Shield"] = 2] = "Shield";
10
+ ItemType[ItemType["Weapon"] = 3] = "Weapon";
11
+ ItemType[ItemType["Other"] = 4] = "Other";
12
+ })(ItemType || (ItemType = {}));
13
+ var Quality;
14
+ (function (Quality) {
15
+ Quality[Quality["Low"] = 1] = "Low";
16
+ Quality[Quality["Normal"] = 2] = "Normal";
17
+ Quality[Quality["Superior"] = 3] = "Superior";
18
+ Quality[Quality["Magic"] = 4] = "Magic";
19
+ Quality[Quality["Set"] = 5] = "Set";
20
+ Quality[Quality["Rare"] = 6] = "Rare";
21
+ Quality[Quality["Unique"] = 7] = "Unique";
22
+ Quality[Quality["Crafted"] = 8] = "Crafted";
23
+ })(Quality || (Quality = {}));
24
+ // prettier-ignore
25
+ //huffman tree
26
+ const HUFFMAN = [[[[["w", "u"], [["8", ["y", ["5", ["j", []]]]], "h"]], ["s", [["2", "n"], "x"]]], [[["c", ["k", "f"]], "b"], [["t", "m"], ["9", "7"]]]], [" ", [[[["e", "d"], "p"], ["g", [[["z", "q"], "3"], ["v", "6"]]]], [["r", "l"], ["a", [["1", ["4", "0"]], ["i", "o"]]]]]]];
27
+ // prettier-ignore
28
+ const HUFFMAN_LOOKUP = { "0": { "v": 223, "l": 8 }, "1": { "v": 31, "l": 7 }, "2": { "v": 12, "l": 6 }, "3": { "v": 91, "l": 7 }, "4": { "v": 95, "l": 8 }, "5": { "v": 104, "l": 8 }, "6": { "v": 123, "l": 7 }, "7": { "v": 30, "l": 5 }, "8": { "v": 8, "l": 6 }, "9": { "v": 14, "l": 5 }, " ": { "v": 1, "l": 2 }, "a": { "v": 15, "l": 5 }, "b": { "v": 10, "l": 4 }, "c": { "v": 2, "l": 5 }, "d": { "v": 35, "l": 6 }, "e": { "v": 3, "l": 6 }, "f": { "v": 50, "l": 6 }, "g": { "v": 11, "l": 5 }, "h": { "v": 24, "l": 5 }, "i": { "v": 63, "l": 7 }, "j": { "v": 232, "l": 9 }, "k": { "v": 18, "l": 6 }, "l": { "v": 23, "l": 5 }, "m": { "v": 22, "l": 5 }, "n": { "v": 44, "l": 6 }, "o": { "v": 127, "l": 7 }, "p": { "v": 19, "l": 5 }, "q": { "v": 155, "l": 8 }, "r": { "v": 7, "l": 5 }, "s": { "v": 4, "l": 4 }, "t": { "v": 6, "l": 5 }, "u": { "v": 16, "l": 5 }, "v": { "v": 59, "l": 7 }, "w": { "v": 0, "l": 5 }, "x": { "v": 28, "l": 5 }, "y": { "v": 40, "l": 7 }, "z": { "v": 27, "l": 8 } };
29
+ async function readCharItems(char, reader, constants, config) {
30
+ char.items = await readItems(reader, char.header.version, constants, config, char);
31
+ }
32
+ exports.readCharItems = readCharItems;
33
+ async function writeCharItems(char, constants, config) {
34
+ const writer = new bitwriter_1.BitWriter();
35
+ writer.WriteArray(await writeItems(char.items, char.header.version, constants, config));
36
+ return writer.ToArray();
37
+ }
38
+ exports.writeCharItems = writeCharItems;
39
+ async function readMercItems(char, reader, constants, config) {
40
+ char.merc_items = [];
41
+ const header = reader.ReadString(2); //0x0000 [merc item list header = "jf"]
42
+ if (header !== "jf") {
43
+ // header is not present in first save after char is created
44
+ if ((char === null || char === void 0 ? void 0 : char.header.level) === 1) {
45
+ return;
46
+ }
47
+ throw new Error(`Mercenary header 'jf' not found at position ${reader.offset - 2 * 8}`);
48
+ }
49
+ if (char.header.merc_id && parseInt(char.header.merc_id, 16) !== 0) {
50
+ char.merc_items = await readItems(reader, char.header.version, constants, config, char);
51
+ }
52
+ }
53
+ exports.readMercItems = readMercItems;
54
+ async function writeMercItems(char, constants, config) {
55
+ const writer = new bitwriter_1.BitWriter();
56
+ writer.WriteString("jf", 2);
57
+ if (char.header.merc_id && parseInt(char.header.merc_id, 16) !== 0) {
58
+ char.merc_items = char.merc_items || [];
59
+ writer.WriteArray(await writeItems(char.merc_items, char.header.version, constants, config));
60
+ }
61
+ return writer.ToArray();
62
+ }
63
+ exports.writeMercItems = writeMercItems;
64
+ async function readGolemItems(char, reader, constants, config) {
65
+ const header = reader.ReadString(2); //0x0000 [golem item list header = "kf"]
66
+ if (header !== "kf") {
67
+ // header is not present in first save after char is created
68
+ if ((char === null || char === void 0 ? void 0 : char.header.level) === 1) {
69
+ return;
70
+ }
71
+ throw new Error(`Golem header 'kf' not found at position ${reader.offset - 2 * 8}`);
72
+ }
73
+ const has_golem = reader.ReadUInt8();
74
+ if (has_golem === 1) {
75
+ char.golem_item = await readItem(reader, char.header.version, constants, config);
76
+ }
77
+ }
78
+ exports.readGolemItems = readGolemItems;
79
+ async function writeGolemItems(char, constants, config) {
80
+ const writer = new bitwriter_1.BitWriter();
81
+ writer.WriteString("kf", 2);
82
+ if (char.golem_item) {
83
+ writer.WriteUInt8(1);
84
+ writer.WriteArray(await writeItem(char.golem_item, char.header.version, constants, config));
85
+ }
86
+ else {
87
+ writer.WriteUInt8(0);
88
+ }
89
+ return writer.ToArray();
90
+ }
91
+ exports.writeGolemItems = writeGolemItems;
92
+ async function readDemon(char, reader, constants, config) {
93
+ reader.SkipBytes(2);
94
+ const header = reader.ReadString(2); // 0x6C 0x66 lf Demon header
95
+ if (header === "lf") {
96
+ char.demon = {
97
+ //data: Array.from(reader.ReadBytes((reader.bits.length - reader.offset) / 8)),
98
+ };
99
+ char.demon._unknown_data.b0_4 = reader.ReadBytes(4);
100
+ char.demon.isSuperUnique = reader.ReadUInt16();
101
+ char.demon.index = reader.ReadUInt16();
102
+ char.demon._unknown_data.b9_15 = reader.ReadBytes(6);
103
+ char.demon.difficulty = reader.ReadUInt8();
104
+ char.demon._unknown_data.b17_28 = reader.ReadBytes(11);
105
+ char.demon.levelId = reader.ReadUInt16();
106
+ char.demon._unknown_data.b31_32 = reader.ReadBytes(2);
107
+ char.demon.level = reader.ReadUInt8();
108
+ char.demon.isDesecrated = reader.ReadUInt8();
109
+ char.demon._unknown_data.b35_57 = reader.ReadBytes(22);
110
+ char.demon.difficulty2 = reader.ReadUInt8();
111
+ char.demon._unknown_data.b59_61 = reader.ReadBytes(3);
112
+ char.demon.difficulty3 = reader.ReadUInt8();
113
+ char.demon._unknown_data.b63_86 = reader.ReadBytes(23);
114
+ reader.SkipBytes(23);
115
+ for (let i = 0; i < 9; i++) {
116
+ char.demon.mods.push(reader.ReadUInt8());
117
+ }
118
+ char.demon.stats = Array.from(reader.ReadBytes((reader.bits.length - reader.offset) / 8));
119
+ }
120
+ }
121
+ exports.readDemon = readDemon;
122
+ async function writeDemon(char, constants, config) {
123
+ const writer = new bitwriter_1.BitWriter();
124
+ writer.WriteArray(new Uint8Array([0x01, 0x00]));
125
+ writer.WriteString("lf", 2);
126
+ if (char.demon) {
127
+ writer.WriteArray(char.demon._unknown_data.b0_4);
128
+ writer.WriteUInt16(char.demon.isSuperUnique);
129
+ writer.WriteUInt16(char.demon.index);
130
+ writer.WriteArray(char.demon._unknown_data.b9_15);
131
+ writer.WriteUInt8(char.demon.difficulty);
132
+ writer.WriteArray(char.demon._unknown_data.b17_28);
133
+ writer.WriteUInt16(char.demon.levelId);
134
+ writer.WriteArray(char.demon._unknown_data.b31_32);
135
+ writer.WriteUInt8(char.demon.level);
136
+ writer.WriteUInt8(char.demon.isDesecrated);
137
+ writer.WriteArray(char.demon._unknown_data.b35_57);
138
+ writer.WriteUInt8(char.demon.difficulty2);
139
+ writer.WriteArray(char.demon._unknown_data.b59_61);
140
+ writer.WriteUInt8(char.demon.difficulty3);
141
+ writer.WriteArray(char.demon._unknown_data.b63_86);
142
+ for (let i = 0; i < 9; i++) {
143
+ writer.WriteUInt8(char.demon.mods[i] || 0);
144
+ }
145
+ writer.WriteArray(new Uint8Array(char.demon.stats));
146
+ //writer.WriteBytes(new Uint8Array(char.demon.data));
147
+ }
148
+ else {
149
+ writer.WriteArray(new Uint8Array([0x00, 0x00]));
150
+ }
151
+ return writer.ToArray();
152
+ }
153
+ exports.writeDemon = writeDemon;
154
+ async function readCorpseItems(char, reader, constants, config) {
155
+ char.corpse_items = [];
156
+ const header = reader.ReadString(2); //0x0000 [item list header = 0x4a, 0x4d "JM"]
157
+ if (header !== "JM") {
158
+ // header is not present in first save after char is created
159
+ if (char.header.level === 1) {
160
+ char.is_dead = 0;
161
+ return;
162
+ }
163
+ throw new Error(`Corpse header 'JM' not found at position ${reader.offset - 2 * 8}`);
164
+ }
165
+ char.is_dead = reader.ReadUInt16(); //0x0002 [corpse count]
166
+ for (let i = 0; i < char.is_dead; i++) {
167
+ reader.SkipBytes(12); //0x0004 [unk4, x_pos, y_pos]
168
+ char.corpse_items = char.corpse_items.concat(await readItems(reader, char.header.version, constants, config, char));
169
+ }
170
+ }
171
+ exports.readCorpseItems = readCorpseItems;
172
+ async function writeCorpseItem(char, constants, config) {
173
+ const writer = new bitwriter_1.BitWriter();
174
+ writer.WriteString("JM", 2);
175
+ writer.WriteUInt16(char.is_dead);
176
+ //json struct doesnt support multiple corpses without modifying it
177
+ if (char.is_dead) {
178
+ writer.WriteArray(new Uint8Array(12));
179
+ char.corpse_items = char.corpse_items || [];
180
+ writer.WriteArray(await writeItems(char.corpse_items, char.header.version, constants, config));
181
+ }
182
+ return writer.ToArray();
183
+ }
184
+ exports.writeCorpseItem = writeCorpseItem;
185
+ async function readItems(reader, version, constants, config, char) {
186
+ const items = [];
187
+ const header = reader.ReadString(2); //0x0000 [item list header = 0x4a, 0x4d "JM"]
188
+ if (header !== "JM") {
189
+ // header is not present in first save after char is created
190
+ if ((char === null || char === void 0 ? void 0 : char.header.level) === 1) {
191
+ return []; // TODO: return starter items based on class
192
+ }
193
+ throw new Error(`Item list header 'JM' not found at position ${reader.offset - 2 * 8}`);
194
+ }
195
+ const count = reader.ReadUInt16(); //0x0002
196
+ for (let i = 0; i < count; i++) {
197
+ /*if (version === 0x69) {
198
+ const bitStr = reader.bits.join("");
199
+ const searchStart = reader.offset;
200
+ const marker1 = "000000001 00001000 10100000".replace(/ /g, ""); // search for next item
201
+ const marker2 = "000000001 00000000 10100000".replace(/ /g, ""); // search for next item
202
+ const id1 = bitStr.indexOf(marker1, searchStart);
203
+ const id2 = bitStr.indexOf(marker2, searchStart);
204
+ const id = id1 !== -1 && id2 !== -1 ? Math.min(id1, id2) : Math.max(id1, id2);
205
+ reader.SeekBit(id - 15);
206
+ }*/
207
+ items.push(await readItem(reader, version, constants, config));
208
+ }
209
+ return items;
210
+ }
211
+ exports.readItems = readItems;
212
+ async function writeItems(items, version, constants, config) {
213
+ const writer = new bitwriter_1.BitWriter();
214
+ writer.WriteString("JM", 2);
215
+ writer.WriteUInt16(items.length);
216
+ for (let i = 0; i < items.length; i++) {
217
+ writer.WriteArray(await writeItem(items[i], version, constants, config));
218
+ }
219
+ return writer.ToArray();
220
+ }
221
+ exports.writeItems = writeItems;
222
+ async function readItem(reader, version, originalConstants, config, parent) {
223
+ if (version <= 0x60) {
224
+ const header = reader.ReadString(2); //0x0000 [item header = 0x4a, 0x4d "JM"]
225
+ if (header !== "JM") {
226
+ throw new Error(`Item header 'JM' not found at position ${reader.offset - 2 * 8}`);
227
+ }
228
+ }
229
+ const constants = originalConstants;
230
+ const item = {};
231
+ _readSimpleBits(item, reader, version, constants, config);
232
+ if (!item.simple_item) {
233
+ item.id = reader.ReadUInt32(32);
234
+ item.level = reader.ReadUInt8(7);
235
+ item.quality = reader.ReadUInt8(4);
236
+ item.multiple_pictures = reader.ReadBit();
237
+ if (item.multiple_pictures) {
238
+ item.picture_id = reader.ReadUInt8(3);
239
+ }
240
+ item.class_specific = reader.ReadBit();
241
+ if (item.class_specific) {
242
+ item.auto_affix_id = reader.ReadUInt16(11);
243
+ }
244
+ switch (item.quality) {
245
+ case Quality.Low:
246
+ item.low_quality_id = reader.ReadUInt8(3);
247
+ break;
248
+ case Quality.Normal:
249
+ break;
250
+ case Quality.Superior:
251
+ item.file_index = reader.ReadUInt8(3);
252
+ break;
253
+ case Quality.Magic:
254
+ item.magic_prefix = reader.ReadUInt16(11);
255
+ if (item.magic_prefix)
256
+ item.magic_prefix_name = constants.magic_prefixes[item.magic_prefix] ? constants.magic_prefixes[item.magic_prefix].n : null;
257
+ item.magic_suffix = reader.ReadUInt16(11);
258
+ if (item.magic_suffix)
259
+ item.magic_suffix_name = constants.magic_suffixes[item.magic_suffix] ? constants.magic_suffixes[item.magic_suffix].n : null;
260
+ break;
261
+ case Quality.Set:
262
+ item.set_id = reader.ReadUInt16(12);
263
+ item.set_name = constants.set_items[item.set_id] ? constants.set_items[item.set_id].n : null;
264
+ break;
265
+ case Quality.Unique:
266
+ item.unique_id = reader.ReadUInt16(12);
267
+ item.unique_name = constants.unq_items[item.unique_id] ? constants.unq_items[item.unique_id].n : null;
268
+ break;
269
+ case Quality.Rare:
270
+ case Quality.Crafted:
271
+ item.rare_name_id = reader.ReadUInt8(8);
272
+ if (item.rare_name_id)
273
+ item.rare_name = constants.rare_names[item.rare_name_id] ? constants.rare_names[item.rare_name_id].n : null;
274
+ item.rare_name_id2 = reader.ReadUInt8(8);
275
+ if (item.rare_name_id2)
276
+ item.rare_name2 = constants.rare_names[item.rare_name_id2] ? constants.rare_names[item.rare_name_id2].n : null;
277
+ item.magical_name_ids = [];
278
+ for (let i = 0; i < 6; i++) {
279
+ const prefix = reader.ReadBit();
280
+ if (prefix === 1) {
281
+ item.magical_name_ids[i] = reader.ReadUInt16(11);
282
+ }
283
+ else {
284
+ item.magical_name_ids[i] = null;
285
+ }
286
+ }
287
+ break;
288
+ default:
289
+ break;
290
+ }
291
+ if (item.given_runeword) {
292
+ item.runeword_id = reader.ReadUInt16(12);
293
+ //fix delerium on d2gs??? why is this a thing?
294
+ if (item.runeword_id == 2718) {
295
+ item.runeword_id = 48;
296
+ }
297
+ const mappedId = {
298
+ 2784: 196,
299
+ 2785: 197,
300
+ 2786: 198,
301
+ 2787: 199,
302
+ 2788: 200,
303
+ 2789: 201,
304
+ 2790: 202,
305
+ 2791: 203,
306
+ 2792: 204,
307
+ 3074: 205,
308
+ 3075: 206,
309
+ 3076: 207,
310
+ }[item.runeword_id] || item.runeword_id;
311
+ if (constants.runewords[mappedId]) {
312
+ item.runeword_name = constants.runewords[mappedId].n;
313
+ }
314
+ reader.ReadUInt8(4);
315
+ }
316
+ if (item.personalized) {
317
+ const arr = new Uint8Array(16);
318
+ for (let i = 0; i < arr.length; i++) {
319
+ if (version > 0x61) {
320
+ arr[i] = reader.ReadUInt8(8);
321
+ }
322
+ else {
323
+ arr[i] = reader.ReadUInt8(7);
324
+ }
325
+ if (arr[i] === 0x00) {
326
+ break;
327
+ }
328
+ }
329
+ item.personalized_name = new bitreader_1.BitReader(arr).ReadString(16).trim().replace(/\0/g, "");
330
+ }
331
+ //tomes
332
+ if (item.type === "tbk" || item.type == "ibk") {
333
+ reader.ReadUInt8(5);
334
+ }
335
+ //realm data
336
+ item.timestamp = reader.ReadUInt8(1);
337
+ if (item.type_id === ItemType.Armor) {
338
+ item.defense_rating = reader.ReadUInt16(constants.magical_properties[31].sB) - constants.magical_properties[31].sA;
339
+ }
340
+ if (item.type_id === ItemType.Armor || item.type_id === ItemType.Weapon) {
341
+ item.max_durability = reader.ReadUInt16(constants.magical_properties[73].sB) - constants.magical_properties[73].sA;
342
+ if (item.max_durability > 0) {
343
+ item.current_durability = reader.ReadUInt16(constants.magical_properties[72].sB) - constants.magical_properties[72].sA;
344
+ }
345
+ }
346
+ if (version === 0x69) {
347
+ reader.SkipBits(1); //unknown bit in RotW
348
+ }
349
+ if (constants.stackables[item.type]) {
350
+ item.quantity = reader.ReadUInt16(9);
351
+ }
352
+ if (item.socketed === 1) {
353
+ item.total_nr_of_sockets = reader.ReadUInt8(4);
354
+ }
355
+ /**
356
+ * 5 bits. any of the 5 bits can be set. if a bit is set that means
357
+ * means +1 to the set_list_count
358
+ */
359
+ let plist_flag = 0;
360
+ if (item.quality === Quality.Set) {
361
+ plist_flag = reader.ReadUInt8(5);
362
+ item.set_list_count = 0;
363
+ item._unknown_data.plist_flag = plist_flag;
364
+ }
365
+ //magical properties
366
+ const skipMagicPeroperties = ["qll", "spe", "sol", "fng", "jaw", "scz", "flg", "tal"].includes(item.type);
367
+ let magic_attributes = _readMagicProperties(reader, constants, skipMagicPeroperties);
368
+ item.magic_attributes = magic_attributes;
369
+ while (plist_flag > 0) {
370
+ if (plist_flag & 1) {
371
+ item.set_list_count += 1;
372
+ magic_attributes = _readMagicProperties(reader, constants);
373
+ if (item.set_attributes) {
374
+ item.set_attributes.push(magic_attributes);
375
+ }
376
+ else {
377
+ item.set_attributes = [magic_attributes];
378
+ }
379
+ }
380
+ plist_flag >>>= 1;
381
+ }
382
+ if (item.given_runeword === 1) {
383
+ magic_attributes = _readMagicProperties(reader, constants);
384
+ if (magic_attributes && magic_attributes.length > 0) {
385
+ item.runeword_attributes = magic_attributes;
386
+ }
387
+ }
388
+ }
389
+ /*if (item._unknown_data.b27_31 && item._unknown_data.b27_31?.some((b) => b === 1)) {
390
+ reader.SkipBits(53); // skip 50 - 53
391
+ reader.Align();
392
+ }*/
393
+ if (version === 0x69) {
394
+ if (!item.identified && (item.quality === Quality.Unique || item.quality === Quality.Set)) {
395
+ item.chronicle = {
396
+ monsterId: reader.ReadUInt16(),
397
+ timestamp: reader.ReadUInt32(),
398
+ };
399
+ reader.SkipBits(4);
400
+ }
401
+ item._unknown_data.chest_stackable = reader.ReadBit(); //unknown bit in RotW (stackable in chest?)
402
+ if (item._unknown_data.chest_stackable) {
403
+ item.amount_in_shared_stash = reader.ReadUInt8();
404
+ }
405
+ }
406
+ reader.Align();
407
+ if (item.nr_of_items_in_sockets > 0 && item.simple_item === 0) {
408
+ item.socketed_items = [];
409
+ for (let i = 0; i < item.nr_of_items_in_sockets; i++) {
410
+ item.socketed_items.push(await readItem(reader, version, constants, config, item));
411
+ }
412
+ }
413
+ //console.log(JSON.stringify(item));
414
+ return item;
415
+ }
416
+ exports.readItem = readItem;
417
+ async function writeItem(item, version, constants, config) {
418
+ var _a;
419
+ if (item._unknown_data === undefined) {
420
+ item._unknown_data = {};
421
+ }
422
+ if (item.categories === undefined) {
423
+ item.categories = (_a = _GetItemTXT(item, constants)) === null || _a === void 0 ? void 0 : _a.c;
424
+ }
425
+ const writer = new bitwriter_1.BitWriter();
426
+ if (version <= 0x60) {
427
+ writer.WriteString("JM", 2);
428
+ }
429
+ _writeSimpleBits(writer, version, item, constants, config);
430
+ if (!item.simple_item) {
431
+ writer.WriteUInt32(item.id, 32);
432
+ writer.WriteUInt8(item.level, 7);
433
+ writer.WriteUInt8(item.quality, 4);
434
+ writer.WriteUInt8(item.multiple_pictures, 1);
435
+ if (item.multiple_pictures) {
436
+ writer.WriteUInt8(item.picture_id, 3);
437
+ }
438
+ writer.WriteUInt8(item.class_specific, 1);
439
+ if (item.class_specific === 1) {
440
+ writer.WriteUInt16(item.auto_affix_id || 0, 11);
441
+ }
442
+ switch (item.quality) {
443
+ case Quality.Low:
444
+ writer.WriteUInt8(item.low_quality_id, 3);
445
+ break;
446
+ case Quality.Normal:
447
+ break;
448
+ case Quality.Superior:
449
+ writer.WriteUInt8(item.file_index || 0, 3);
450
+ break;
451
+ case Quality.Magic:
452
+ writer.WriteUInt16(item.magic_prefix, 11);
453
+ writer.WriteUInt16(item.magic_suffix, 11);
454
+ break;
455
+ case Quality.Set:
456
+ writer.WriteUInt16(item.set_id, 12);
457
+ break;
458
+ case Quality.Unique:
459
+ writer.WriteUInt16(item.unique_id, 12);
460
+ break;
461
+ case Quality.Rare:
462
+ case Quality.Crafted:
463
+ writer.WriteUInt8(item.rare_name_id !== undefined ? item.rare_name_id : _lookupRareId(item.rare_name, constants), 8);
464
+ writer.WriteUInt8(item.rare_name_id2 !== undefined ? item.rare_name_id2 : _lookupRareId(item.rare_name2, constants), 8);
465
+ for (let i = 0; i < 6; i++) {
466
+ const magical_name_id = item.magical_name_ids[i];
467
+ if (magical_name_id) {
468
+ writer.WriteBit(1);
469
+ writer.WriteUInt16(magical_name_id, 11);
470
+ }
471
+ else {
472
+ writer.WriteBit(0);
473
+ }
474
+ }
475
+ break;
476
+ default:
477
+ break;
478
+ }
479
+ if (item.given_runeword) {
480
+ //fix delerium on d2gs??? why is this a thing?
481
+ let runeword_id = item.runeword_id;
482
+ if (runeword_id == 2718) {
483
+ runeword_id = 48;
484
+ }
485
+ writer.WriteUInt16(runeword_id, 12);
486
+ writer.WriteUInt8(5, 4); //always 5?
487
+ }
488
+ if (item.personalized) {
489
+ const name = item.personalized_name.substring(0, 16);
490
+ for (let i = 0; i < name.length; i++) {
491
+ if (version > 0x61) {
492
+ writer.WriteUInt8(name.charCodeAt(i), 8);
493
+ }
494
+ else {
495
+ writer.WriteUInt8(name.charCodeAt(i) & 0x7f, 7);
496
+ }
497
+ }
498
+ writer.WriteUInt8(0x00, version > 0x61 ? 8 : 7);
499
+ }
500
+ if (item.type === "tbk") {
501
+ writer.WriteUInt8(0, 5);
502
+ }
503
+ else if (item.type === "ibk") {
504
+ writer.WriteUInt8(1, 5);
505
+ }
506
+ writer.WriteUInt8(item.timestamp, 1);
507
+ if (item.type_id === ItemType.Armor || item.type_id === ItemType.Shield) {
508
+ writer.WriteUInt16(item.defense_rating + constants.magical_properties[31].sA, constants.magical_properties[31].sB);
509
+ }
510
+ if (item.type_id === ItemType.Armor || item.type_id === ItemType.Shield || item.type_id === ItemType.Weapon) {
511
+ writer.WriteUInt16(item.max_durability || 0, constants.magical_properties[73].sB);
512
+ if (item.max_durability > 0) {
513
+ writer.WriteUInt16(item.current_durability, constants.magical_properties[72].sB);
514
+ }
515
+ }
516
+ if (version === 0x69) {
517
+ writer.WriteUInt8(constants.stackables[item.type] ? 1 : 0, 1); //unknown bit in RotW
518
+ }
519
+ if (constants.stackables[item.type]) {
520
+ writer.WriteUInt16(item.quantity, 9);
521
+ }
522
+ if (item.socketed === 1) {
523
+ writer.WriteUInt8(item.total_nr_of_sockets, 4);
524
+ }
525
+ if (item.quality === Quality.Set) {
526
+ const set_attribute_count = item.set_attributes != null ? item.set_attributes.length : 0;
527
+ //reduced by -1 removed as this seems to be wrong
528
+ const plist_flag = (1 << set_attribute_count) - 1;
529
+ writer.WriteUInt8(item._unknown_data.plist_flag || plist_flag, 5);
530
+ }
531
+ _writeMagicProperties(writer, item.magic_attributes, constants);
532
+ if (item.set_attributes && item.set_attributes.length > 0) {
533
+ for (let i = 0; i < item.set_attributes.length; i++) {
534
+ _writeMagicProperties(writer, item.set_attributes[i], constants);
535
+ }
536
+ }
537
+ if (item.given_runeword === 1) {
538
+ _writeMagicProperties(writer, item.runeword_attributes, constants);
539
+ }
540
+ }
541
+ if (version === 0x69) {
542
+ if (!item.identified && (item.quality === Quality.Unique || item.quality === Quality.Set) && item.chronicle) {
543
+ writer.WriteUInt16(item.chronicle.monsterId);
544
+ writer.WriteUInt32(item.chronicle.timestamp);
545
+ writer.WriteUInt8(0, 4); //unknown bit in RotW
546
+ }
547
+ writer.WriteUInt8(item._unknown_data.chest_stackable || 0, 1);
548
+ if (item._unknown_data.chest_stackable) {
549
+ writer.WriteUInt8(item.amount_in_shared_stash || 0, 8); //unknown bit in RotW (stackable in chest?)
550
+ }
551
+ }
552
+ writer.Align();
553
+ if (item.nr_of_items_in_sockets > 0 && item.simple_item === 0) {
554
+ for (let i = 0; i < item.nr_of_items_in_sockets; i++) {
555
+ writer.WriteArray(await writeItem(item.socketed_items[i], version, constants, config));
556
+ }
557
+ }
558
+ return writer.ToArray();
559
+ }
560
+ exports.writeItem = writeItem;
561
+ function _readSimpleBits(item, reader, version, constants, config) {
562
+ var _a, _b, _c;
563
+ //init so we do not have npe's
564
+ item._unknown_data = {};
565
+ //1.10-1.14d
566
+ //[flags:32][version:10][mode:3]([invloc:4][x:4][y:4][page:3])([itemcode:32])([sockets:3])
567
+ //1.15
568
+ //[flags:32][version:3][mode:3]([invloc:4][x:4][y:4][page:3])([itemcode:variable])([sockets:3])
569
+ item._unknown_data.b0_3 = reader.ReadBitArray(4); // 0 byte 1
570
+ item.identified = reader.ReadBit(); // 4
571
+ item._unknown_data.b5_10 = reader.ReadBitArray(6); // 5
572
+ item.socketed = reader.ReadBit(); // 11
573
+ item._unknown_data.b12 = reader.ReadBitArray(1); // 12
574
+ item.new = reader.ReadBit(); // 13
575
+ item._unknown_data.b14_15 = reader.ReadBitArray(2); // 14
576
+ item.is_ear = reader.ReadBit(); // 16 byte 3
577
+ item.starter_item = reader.ReadBit(); // 17
578
+ item._unknown_data.b18_20 = reader.ReadBitArray(3); // 18
579
+ item.simple_item = reader.ReadBit(); // 21
580
+ item.ethereal = reader.ReadBit(); // 22
581
+ item._unknown_data.b23 = reader.ReadBitArray(1); // 23
582
+ item.personalized = reader.ReadBit(); // 24 byte 4
583
+ item._unknown_data.b25 = reader.ReadBitArray(1); // 25
584
+ item.given_runeword = reader.ReadBit(); // 26
585
+ item._unknown_data.b27_31 = reader.ReadBitArray(5); // 27
586
+ if (version <= 0x60) {
587
+ item.version = reader.ReadUInt16(10).toString(10);
588
+ }
589
+ else if (version >= 0x61) {
590
+ item.version = reader.ReadUInt16(3).toString(2); // 32 byte 5
591
+ }
592
+ item.location_id = reader.ReadUInt8(3); // 34
593
+ item.equipped_id = reader.ReadUInt8(4); // 37
594
+ item.position_x = reader.ReadUInt8(4); // 41
595
+ item.position_y = reader.ReadUInt8(4); // 45
596
+ item.alt_position_id = reader.ReadUInt8(3); // 49
597
+ if (item.is_ear) {
598
+ const clazz = reader.ReadUInt8(3);
599
+ const level = reader.ReadUInt8(7);
600
+ const arr = new Uint8Array(15);
601
+ for (let i = 0; i < arr.length; i++) {
602
+ arr[i] = reader.ReadUInt8(7);
603
+ if (arr[i] === 0x00) {
604
+ break;
605
+ }
606
+ }
607
+ const name = new bitreader_1.BitReader(arr).ReadString(15).trim().replace(/\0/g, "");
608
+ item.ear_attributes = {
609
+ class: clazz,
610
+ level: level,
611
+ name: name,
612
+ };
613
+ }
614
+ else {
615
+ if (version <= 0x60) {
616
+ item.type = reader.ReadString(4);
617
+ }
618
+ else if (version >= 0x61) {
619
+ item.type = "";
620
+ //props to d07riv
621
+ //https://github.com/d07RiV/d07riv.github.io/blob/master/d2r.html#L11-L20
622
+ for (let i = 0; i < 4; i++) {
623
+ let node = HUFFMAN;
624
+ do {
625
+ node = node[reader.ReadBit()]; // 50
626
+ } while (Array.isArray(node));
627
+ item.type += node;
628
+ }
629
+ }
630
+ item.type = item.type.trim().replace(/\0/g, "");
631
+ let details = _GetItemTXT(item, constants);
632
+ item.categories = details === null || details === void 0 ? void 0 : details.c;
633
+ if ((_a = item === null || item === void 0 ? void 0 : item.categories) === null || _a === void 0 ? void 0 : _a.includes("Any Armor")) {
634
+ item.type_id = ItemType.Armor;
635
+ }
636
+ else if ((_b = item === null || item === void 0 ? void 0 : item.categories) === null || _b === void 0 ? void 0 : _b.includes("Weapon")) {
637
+ item.type_id = ItemType.Weapon;
638
+ details = constants.weapon_items[item.type];
639
+ }
640
+ else {
641
+ item.type_id = ItemType.Other;
642
+ }
643
+ let bits = item.simple_item ? 1 : 3;
644
+ if ((_c = item.categories) === null || _c === void 0 ? void 0 : _c.includes("Quest")) {
645
+ item.quest_difficulty = reader.ReadUInt16(constants.magical_properties[356].sB) - constants.magical_properties[356].sA;
646
+ bits = 1;
647
+ }
648
+ item.nr_of_items_in_sockets = reader.ReadUInt8(bits);
649
+ }
650
+ }
651
+ function _lookupRareId(name, constants) {
652
+ //some inconsistencies with txt data and nokka. so have to hack it with startsWith
653
+ return constants.rare_names.findIndex((k) => k && (k.n.toLowerCase().startsWith(name.toLowerCase()) || name.toLowerCase().startsWith(k.n.toLowerCase())));
654
+ }
655
+ function _writeSimpleBits(writer, version, item, constants, config) {
656
+ var _a;
657
+ writer.WriteBits(item._unknown_data.b0_3 || new Uint8Array(4), 4);
658
+ writer.WriteBit(item.identified);
659
+ writer.WriteBits(item._unknown_data.b5_10 || new Uint8Array(6), 6);
660
+ writer.WriteBit(item.socketed);
661
+ writer.WriteBits(item._unknown_data.b12 || new Uint8Array(1), 1);
662
+ writer.WriteBit(item.new);
663
+ writer.WriteBits(item._unknown_data.b14_15 || new Uint8Array(2), 2);
664
+ writer.WriteBit(item.is_ear);
665
+ writer.WriteBit(item.starter_item);
666
+ writer.WriteBits(item._unknown_data.b18_20 || new Uint8Array(3), 3);
667
+ writer.WriteBit(item.simple_item);
668
+ writer.WriteBit(item.ethereal);
669
+ writer.WriteBits(item._unknown_data.b23 || new Uint8Array([1]), 1); //always 1? IFLAG_JUSTSAVED
670
+ writer.WriteBit(item.personalized);
671
+ writer.WriteBits(item._unknown_data.b25 || new Uint8Array(1), 1); //IFLAG_LOWQUALITY
672
+ writer.WriteBit(item.given_runeword);
673
+ writer.WriteBits(item._unknown_data.b27_31 || new Uint8Array(5), 5);
674
+ const itemVersion = item.version != null ? item.version : "101";
675
+ if (version <= 0x60) {
676
+ // 0 = pre-1.08; 1 = 1.08/1.09 normal; 2 = 1.10 normal; 100 = 1.08/1.09 expansion; 101 = 1.10 expansion
677
+ writer.WriteUInt16(parseInt(itemVersion, 10), 10);
678
+ }
679
+ else if (version >= 0x61) {
680
+ writer.WriteUInt16(parseInt(itemVersion, 2), 3);
681
+ }
682
+ writer.WriteUInt8(item.location_id, 3);
683
+ writer.WriteUInt8(item.equipped_id, 4);
684
+ writer.WriteUInt8(item.position_x, 4);
685
+ writer.WriteUInt8(item.position_y, 4);
686
+ writer.WriteUInt8(item.alt_position_id, 3);
687
+ if (item.is_ear) {
688
+ writer.WriteUInt8(item.ear_attributes.class, 3);
689
+ writer.WriteUInt8(item.ear_attributes.level, 7);
690
+ const name = item.ear_attributes.name.substring(0, 15);
691
+ for (let i = 0; i < name.length; i++) {
692
+ writer.WriteUInt8(name.charCodeAt(i) & 0x7f, 7);
693
+ }
694
+ writer.WriteUInt8(0x00, 7);
695
+ }
696
+ else {
697
+ const t = item.type.padEnd(4, " ");
698
+ if (version <= 0x60) {
699
+ writer.WriteString(t, 4);
700
+ }
701
+ else {
702
+ for (const c of t) {
703
+ const n = HUFFMAN_LOOKUP[c];
704
+ writer.WriteUInt16(n.v, n.l);
705
+ }
706
+ }
707
+ let bits = item.simple_item ? 1 : 3;
708
+ if ((_a = item.categories) === null || _a === void 0 ? void 0 : _a.includes("Quest")) {
709
+ const difficulty = item.quest_difficulty || 0;
710
+ writer.WriteUInt16(difficulty + constants.magical_properties[356].sA, constants.magical_properties[356].sB);
711
+ bits = 1;
712
+ }
713
+ writer.WriteUInt8(item.nr_of_items_in_sockets, bits);
714
+ }
715
+ }
716
+ function _readMagicProperties(reader, constants, skipMagicProps) {
717
+ if (skipMagicProps) {
718
+ // Scan for the 0x1FF end marker (9 bits of 1)
719
+ const bitStr = reader.bits.join("");
720
+ const searchStart = reader.offset;
721
+ const marker = "111111111"; // 9-bit 0x1FF marker
722
+ const idx = bitStr.indexOf(marker, searchStart);
723
+ if (idx !== -1) {
724
+ reader.SeekBit(idx + 9); // Move reader past the 0x1FF marker
725
+ }
726
+ else {
727
+ console.warn("0x1FF end marker not found, skipping failed");
728
+ }
729
+ return []; // Return empty list since we're skipping parsing
730
+ }
731
+ let id = reader.ReadUInt16(9);
732
+ const magic_attributes = [];
733
+ let zeroCounter = 0;
734
+ while (id != 0x1ff) {
735
+ if (id === 0) {
736
+ zeroCounter++;
737
+ }
738
+ if (zeroCounter > 10) {
739
+ throw new Error(`Possible malformed data at position ${reader.offset - 9}`);
740
+ }
741
+ const values = [];
742
+ if (id > constants.magical_properties.length) {
743
+ throw new Error(`Invalid Stat Id: ${id} at position ${reader.offset - 9}`);
744
+ }
745
+ const num_of_properties = constants.magical_properties[id].np || 1;
746
+ for (let i = 0; i < num_of_properties; i++) {
747
+ const prop = constants.magical_properties[id + i];
748
+ if (prop == null) {
749
+ throw new Error(`Cannot find Magical Property for id: ${id} at position ${reader.offset}`);
750
+ }
751
+ if (prop.sP) {
752
+ let param = reader.ReadUInt16(prop.sP);
753
+ switch (prop.dF) {
754
+ case 14: //+skill to skilltab
755
+ values.push(param & 0x7);
756
+ param = (param >> 3) & 0x1fff;
757
+ break;
758
+ default:
759
+ break;
760
+ }
761
+ //encode
762
+ switch (prop.e) {
763
+ case 1:
764
+ //throw new Error(`Unimplemented encoding: ${prop.encode}`);
765
+ break;
766
+ case 2: //chance to cast
767
+ case 3: //charges
768
+ values.push(param & 0x3f); //skill level
769
+ param = (param >> 6) & 0x3ff; //skll id
770
+ break;
771
+ default:
772
+ break;
773
+ }
774
+ values.push(param);
775
+ }
776
+ if (!prop.sB) {
777
+ throw new Error(`Save Bits is undefined for stat: ${id}:${prop.s} at position ${reader.offset}`);
778
+ }
779
+ let v = reader.ReadUInt16(prop.sB);
780
+ if (prop.sA) {
781
+ v -= prop.sA;
782
+ }
783
+ switch (prop.e) {
784
+ case 3:
785
+ values.push(v & 0xff); // current charges
786
+ values.push((v >> 8) & 0xff); //max charges
787
+ break;
788
+ default:
789
+ values.push(v);
790
+ break;
791
+ }
792
+ }
793
+ magic_attributes.push({
794
+ id: id,
795
+ values: values,
796
+ name: constants.magical_properties[id].s,
797
+ });
798
+ id = reader.ReadUInt16(9);
799
+ }
800
+ return magic_attributes;
801
+ }
802
+ exports._readMagicProperties = _readMagicProperties;
803
+ function _writeMagicProperties(writer, properties, constants) {
804
+ if (properties) {
805
+ for (let i = 0; i < properties.length; i++) {
806
+ const property = properties[i];
807
+ let valueIdx = 0;
808
+ writer.WriteUInt16(property.id, 9);
809
+ const num_of_properties = constants.magical_properties[property.id].np || 1;
810
+ for (let j = 0; j < num_of_properties; j++) {
811
+ const prop = constants.magical_properties[property.id + j];
812
+ if (prop == null) {
813
+ throw new Error(`Cannot find Magical Property for id: ${property.id}`);
814
+ }
815
+ if (prop.sP) {
816
+ let param = property.values[valueIdx++];
817
+ switch (prop.dF) {
818
+ case 14: //+skill to skilltab
819
+ param |= (property.values[valueIdx++] & 0x1fff) << 3;
820
+ break;
821
+ default:
822
+ break;
823
+ }
824
+ //encode
825
+ switch (prop.e) {
826
+ case 1:
827
+ //throw new Error(`Unimplemented encoding: ${prop.encode}`);
828
+ break;
829
+ case 2: //chance to cast
830
+ case 3: //charges
831
+ param |= (property.values[valueIdx++] & 0x3ff) << 6;
832
+ break;
833
+ default:
834
+ break;
835
+ }
836
+ writer.WriteUInt32(param, prop.sP);
837
+ }
838
+ let v = property.values[valueIdx++];
839
+ if (prop.sA) {
840
+ v += prop.sA;
841
+ }
842
+ switch (prop.e) {
843
+ case 3:
844
+ v |= (property.values[valueIdx++] & 0xff) << 8;
845
+ break;
846
+ default:
847
+ break;
848
+ }
849
+ if (!prop.sB) {
850
+ throw new Error(`Save Bits is undefined for stat: ${property.id}:${prop.s}`);
851
+ }
852
+ writer.WriteUInt32(v, prop.sB);
853
+ }
854
+ }
855
+ }
856
+ writer.WriteUInt16(0x1ff, 9);
857
+ }
858
+ exports._writeMagicProperties = _writeMagicProperties;
859
+ function _GetItemTXT(item, constants) {
860
+ if (constants.armor_items[item.type]) {
861
+ return constants.armor_items[item.type];
862
+ }
863
+ else if (constants.weapon_items[item.type]) {
864
+ return constants.weapon_items[item.type];
865
+ }
866
+ else if (constants.other_items[item.type]) {
867
+ return constants.other_items[item.type];
868
+ }
869
+ }
832
870
  //# sourceMappingURL=items.js.map