@d2runewizard/d2s 2.0.133 → 2.0.135

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