@d2runewizard/d2s 2.0.125 → 2.0.127

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