@d2runewizard/d2s 2.0.36

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 (53) hide show
  1. package/README.md +59 -0
  2. package/lib/binary/bitreader.d.ts +22 -0
  3. package/lib/binary/bitreader.js +100 -0
  4. package/lib/binary/bitreader.js.map +1 -0
  5. package/lib/binary/bitwriter.d.ts +21 -0
  6. package/lib/binary/bitwriter.js +118 -0
  7. package/lib/binary/bitwriter.js.map +1 -0
  8. package/lib/binary/constants.d.ts +4 -0
  9. package/lib/binary/constants.js +9 -0
  10. package/lib/binary/constants.js.map +1 -0
  11. package/lib/d2/attribute_enhancer.d.ts +5 -0
  12. package/lib/d2/attribute_enhancer.js +593 -0
  13. package/lib/d2/attribute_enhancer.js.map +1 -0
  14. package/lib/d2/attributes.d.ts +4 -0
  15. package/lib/d2/attributes.js +99 -0
  16. package/lib/d2/attributes.js.map +1 -0
  17. package/lib/d2/constants.d.ts +4 -0
  18. package/lib/d2/constants.js +16 -0
  19. package/lib/d2/constants.js.map +1 -0
  20. package/lib/d2/d2s.d.ts +10 -0
  21. package/lib/d2/d2s.js +106 -0
  22. package/lib/d2/d2s.js.map +1 -0
  23. package/lib/d2/header.d.ts +8 -0
  24. package/lib/d2/header.js +95 -0
  25. package/lib/d2/header.js.map +1 -0
  26. package/lib/d2/items.d.ts +17 -0
  27. package/lib/d2/items.js +728 -0
  28. package/lib/d2/items.js.map +1 -0
  29. package/lib/d2/skills.d.ts +4 -0
  30. package/lib/d2/skills.js +45 -0
  31. package/lib/d2/skills.js.map +1 -0
  32. package/lib/d2/stash.d.ts +3 -0
  33. package/lib/d2/stash.js +196 -0
  34. package/lib/d2/stash.js.map +1 -0
  35. package/lib/d2/types.d.ts +402 -0
  36. package/lib/d2/types.js +15 -0
  37. package/lib/d2/types.js.map +1 -0
  38. package/lib/d2/versions/default_header.d.ts +5 -0
  39. package/lib/d2/versions/default_header.js +721 -0
  40. package/lib/d2/versions/default_header.js.map +1 -0
  41. package/lib/data/parser.d.ts +3 -0
  42. package/lib/data/parser.js +627 -0
  43. package/lib/data/parser.js.map +1 -0
  44. package/lib/data/versions/96_constant_data.d.ts +14427 -0
  45. package/lib/data/versions/96_constant_data.js +5 -0
  46. package/lib/data/versions/96_constant_data.js.map +1 -0
  47. package/lib/data/versions/99_constant_data.d.ts +15572 -0
  48. package/lib/data/versions/99_constant_data.js +5 -0
  49. package/lib/data/versions/99_constant_data.js.map +1 -0
  50. package/lib/index.d.ts +8 -0
  51. package/lib/index.js +47 -0
  52. package/lib/index.js.map +1 -0
  53. package/package.json +46 -0
@@ -0,0 +1,728 @@
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.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 readCorpseItems(char, reader, constants, config) {
93
+ char.corpse_items = [];
94
+ const header = reader.ReadString(2); //0x0000 [item list header = 0x4a, 0x4d "JM"]
95
+ if (header !== "JM") {
96
+ // header is not present in first save after char is created
97
+ if (char.header.level === 1) {
98
+ char.is_dead = 0;
99
+ return;
100
+ }
101
+ throw new Error(`Corpse header 'JM' not found at position ${reader.offset - 2 * 8}`);
102
+ }
103
+ char.is_dead = reader.ReadUInt16(); //0x0002 [corpse count]
104
+ for (let i = 0; i < char.is_dead; i++) {
105
+ reader.SkipBytes(12); //0x0004 [unk4, x_pos, y_pos]
106
+ char.corpse_items = char.corpse_items.concat(await readItems(reader, char.header.version, constants, config, char));
107
+ }
108
+ }
109
+ exports.readCorpseItems = readCorpseItems;
110
+ async function writeCorpseItem(char, constants, config) {
111
+ const writer = new bitwriter_1.BitWriter();
112
+ writer.WriteString("JM", 2);
113
+ writer.WriteUInt16(char.is_dead);
114
+ //json struct doesnt support multiple corpses without modifying it
115
+ if (char.is_dead) {
116
+ writer.WriteArray(new Uint8Array(12));
117
+ char.corpse_items = char.corpse_items || [];
118
+ writer.WriteArray(await writeItems(char.corpse_items, char.header.version, constants, config));
119
+ }
120
+ return writer.ToArray();
121
+ }
122
+ exports.writeCorpseItem = writeCorpseItem;
123
+ async function readItems(reader, version, constants, config, char) {
124
+ const items = [];
125
+ const header = reader.ReadString(2); //0x0000 [item list header = 0x4a, 0x4d "JM"]
126
+ if (header !== "JM") {
127
+ // header is not present in first save after char is created
128
+ if ((char === null || char === void 0 ? void 0 : char.header.level) === 1) {
129
+ return []; // TODO: return starter items based on class
130
+ }
131
+ throw new Error(`Item list header 'JM' not found at position ${reader.offset - 2 * 8}`);
132
+ }
133
+ const count = reader.ReadUInt16(); //0x0002
134
+ for (let i = 0; i < count; i++) {
135
+ items.push(await readItem(reader, version, constants, config));
136
+ }
137
+ return items;
138
+ }
139
+ exports.readItems = readItems;
140
+ async function writeItems(items, version, constants, config) {
141
+ const writer = new bitwriter_1.BitWriter();
142
+ writer.WriteString("JM", 2);
143
+ writer.WriteUInt16(items.length);
144
+ for (let i = 0; i < items.length; i++) {
145
+ writer.WriteArray(await writeItem(items[i], version, constants, config));
146
+ }
147
+ return writer.ToArray();
148
+ }
149
+ exports.writeItems = writeItems;
150
+ async function readItem(reader, version, originalConstants, config, parent) {
151
+ if (version <= 0x60) {
152
+ const header = reader.ReadString(2); //0x0000 [item header = 0x4a, 0x4d "JM"]
153
+ if (header !== "JM") {
154
+ throw new Error(`Item header 'JM' not found at position ${reader.offset - 2 * 8}`);
155
+ }
156
+ }
157
+ const constants = originalConstants;
158
+ const item = {};
159
+ _readSimpleBits(item, reader, version, constants, config);
160
+ if (!item.simple_item) {
161
+ item.id = reader.ReadUInt32(32);
162
+ item.level = reader.ReadUInt8(7);
163
+ item.quality = reader.ReadUInt8(4);
164
+ item.multiple_pictures = reader.ReadBit();
165
+ if (item.multiple_pictures) {
166
+ item.picture_id = reader.ReadUInt8(3);
167
+ }
168
+ item.class_specific = reader.ReadBit();
169
+ if (item.class_specific) {
170
+ item.auto_affix_id = reader.ReadUInt16(11);
171
+ }
172
+ switch (item.quality) {
173
+ case Quality.Low:
174
+ item.low_quality_id = reader.ReadUInt8(3);
175
+ break;
176
+ case Quality.Normal:
177
+ break;
178
+ case Quality.Superior:
179
+ item.file_index = reader.ReadUInt8(3);
180
+ break;
181
+ case Quality.Magic:
182
+ item.magic_prefix = reader.ReadUInt16(11);
183
+ if (item.magic_prefix)
184
+ item.magic_prefix_name = constants.magic_prefixes[item.magic_prefix] ? constants.magic_prefixes[item.magic_prefix].n : null;
185
+ item.magic_suffix = reader.ReadUInt16(11);
186
+ if (item.magic_suffix)
187
+ item.magic_suffix_name = constants.magic_suffixes[item.magic_suffix] ? constants.magic_suffixes[item.magic_suffix].n : null;
188
+ break;
189
+ case Quality.Set:
190
+ item.set_id = reader.ReadUInt16(12);
191
+ item.set_name = constants.set_items[item.set_id] ? constants.set_items[item.set_id].n : null;
192
+ break;
193
+ case Quality.Unique:
194
+ item.unique_id = reader.ReadUInt16(12);
195
+ item.unique_name = constants.unq_items[item.unique_id] ? constants.unq_items[item.unique_id].n : null;
196
+ break;
197
+ case Quality.Rare:
198
+ case Quality.Crafted:
199
+ item.rare_name_id = reader.ReadUInt8(8);
200
+ if (item.rare_name_id)
201
+ item.rare_name = constants.rare_names[item.rare_name_id] ? constants.rare_names[item.rare_name_id].n : null;
202
+ item.rare_name_id2 = reader.ReadUInt8(8);
203
+ if (item.rare_name_id2)
204
+ item.rare_name2 = constants.rare_names[item.rare_name_id2] ? constants.rare_names[item.rare_name_id2].n : null;
205
+ item.magical_name_ids = [];
206
+ for (let i = 0; i < 6; i++) {
207
+ const prefix = reader.ReadBit();
208
+ if (prefix === 1) {
209
+ item.magical_name_ids[i] = reader.ReadUInt16(11);
210
+ }
211
+ else {
212
+ item.magical_name_ids[i] = null;
213
+ }
214
+ }
215
+ break;
216
+ default:
217
+ break;
218
+ }
219
+ if (item.given_runeword) {
220
+ item.runeword_id = reader.ReadUInt16(12);
221
+ //fix delerium on d2gs??? why is this a thing?
222
+ if (item.runeword_id == 2718) {
223
+ item.runeword_id = 48;
224
+ }
225
+ if (constants.runewords[item.runeword_id]) {
226
+ item.runeword_name = constants.runewords[item.runeword_id].n;
227
+ }
228
+ reader.ReadUInt8(4);
229
+ }
230
+ if (item.personalized) {
231
+ const arr = new Uint8Array(16);
232
+ for (let i = 0; i < arr.length; i++) {
233
+ if (version > 0x61) {
234
+ arr[i] = reader.ReadUInt8(8);
235
+ }
236
+ else {
237
+ arr[i] = reader.ReadUInt8(7);
238
+ }
239
+ if (arr[i] === 0x00) {
240
+ break;
241
+ }
242
+ }
243
+ item.personalized_name = new bitreader_1.BitReader(arr).ReadString(16).trim().replace(/\0/g, "");
244
+ }
245
+ //tomes
246
+ if (item.type === "tbk" || item.type == "ibk") {
247
+ reader.ReadUInt8(5);
248
+ }
249
+ //realm data
250
+ item.timestamp = reader.ReadUInt8(1);
251
+ if (item.type_id === ItemType.Armor) {
252
+ item.defense_rating = reader.ReadUInt16(constants.magical_properties[31].sB) - constants.magical_properties[31].sA;
253
+ }
254
+ if (item.type_id === ItemType.Armor || item.type_id === ItemType.Weapon) {
255
+ item.max_durability = reader.ReadUInt16(constants.magical_properties[73].sB) - constants.magical_properties[73].sA;
256
+ if (item.max_durability > 0) {
257
+ item.current_durability = reader.ReadUInt16(constants.magical_properties[72].sB) - constants.magical_properties[72].sA;
258
+ }
259
+ }
260
+ if (constants.stackables[item.type]) {
261
+ item.quantity = reader.ReadUInt16(9);
262
+ }
263
+ if (item.socketed === 1) {
264
+ item.total_nr_of_sockets = reader.ReadUInt8(4);
265
+ }
266
+ /**
267
+ * 5 bits. any of the 5 bits can be set. if a bit is set that means
268
+ * means +1 to the set_list_count
269
+ */
270
+ let plist_flag = 0;
271
+ if (item.quality === Quality.Set) {
272
+ plist_flag = reader.ReadUInt8(5);
273
+ item.set_list_count = 0;
274
+ item._unknown_data.plist_flag = plist_flag;
275
+ }
276
+ //magical properties
277
+ let magic_attributes = _readMagicProperties(reader, constants);
278
+ item.magic_attributes = magic_attributes;
279
+ while (plist_flag > 0) {
280
+ if (plist_flag & 1) {
281
+ item.set_list_count += 1;
282
+ magic_attributes = _readMagicProperties(reader, constants);
283
+ if (item.set_attributes) {
284
+ item.set_attributes.push(magic_attributes);
285
+ }
286
+ else {
287
+ item.set_attributes = [magic_attributes];
288
+ }
289
+ }
290
+ plist_flag >>>= 1;
291
+ }
292
+ if (item.given_runeword === 1) {
293
+ magic_attributes = _readMagicProperties(reader, constants);
294
+ if (magic_attributes && magic_attributes.length > 0) {
295
+ item.runeword_attributes = magic_attributes;
296
+ }
297
+ }
298
+ }
299
+ reader.Align();
300
+ if (item.nr_of_items_in_sockets > 0 && item.simple_item === 0) {
301
+ item.socketed_items = [];
302
+ for (let i = 0; i < item.nr_of_items_in_sockets; i++) {
303
+ item.socketed_items.push(await readItem(reader, version, constants, config, item));
304
+ }
305
+ }
306
+ //console.log(JSON.stringify(item));
307
+ return item;
308
+ }
309
+ exports.readItem = readItem;
310
+ async function writeItem(item, version, constants, config) {
311
+ var _a;
312
+ if (item._unknown_data === undefined) {
313
+ item._unknown_data = {};
314
+ }
315
+ if (item.categories === undefined) {
316
+ item.categories = (_a = _GetItemTXT(item, constants)) === null || _a === void 0 ? void 0 : _a.c;
317
+ }
318
+ const writer = new bitwriter_1.BitWriter();
319
+ if (version <= 0x60) {
320
+ writer.WriteString("JM", 2);
321
+ }
322
+ _writeSimpleBits(writer, version, item, constants, config);
323
+ if (!item.simple_item) {
324
+ writer.WriteUInt32(item.id, 32);
325
+ writer.WriteUInt8(item.level, 7);
326
+ writer.WriteUInt8(item.quality, 4);
327
+ writer.WriteUInt8(item.multiple_pictures, 1);
328
+ if (item.multiple_pictures) {
329
+ writer.WriteUInt8(item.picture_id, 3);
330
+ }
331
+ writer.WriteUInt8(item.class_specific, 1);
332
+ if (item.class_specific === 1) {
333
+ writer.WriteUInt16(item.auto_affix_id || 0, 11);
334
+ }
335
+ switch (item.quality) {
336
+ case Quality.Low:
337
+ writer.WriteUInt8(item.low_quality_id, 3);
338
+ break;
339
+ case Quality.Normal:
340
+ break;
341
+ case Quality.Superior:
342
+ writer.WriteUInt8(item.file_index || 0, 3);
343
+ break;
344
+ case Quality.Magic:
345
+ writer.WriteUInt16(item.magic_prefix, 11);
346
+ writer.WriteUInt16(item.magic_suffix, 11);
347
+ break;
348
+ case Quality.Set:
349
+ writer.WriteUInt16(item.set_id, 12);
350
+ break;
351
+ case Quality.Unique:
352
+ writer.WriteUInt16(item.unique_id, 12);
353
+ break;
354
+ case Quality.Rare:
355
+ case Quality.Crafted:
356
+ writer.WriteUInt8(item.rare_name_id !== undefined ? item.rare_name_id : _lookupRareId(item.rare_name, constants), 8);
357
+ writer.WriteUInt8(item.rare_name_id2 !== undefined ? item.rare_name_id2 : _lookupRareId(item.rare_name2, constants), 8);
358
+ for (let i = 0; i < 6; i++) {
359
+ const magical_name_id = item.magical_name_ids[i];
360
+ if (magical_name_id) {
361
+ writer.WriteBit(1);
362
+ writer.WriteUInt16(magical_name_id, 11);
363
+ }
364
+ else {
365
+ writer.WriteBit(0);
366
+ }
367
+ }
368
+ break;
369
+ default:
370
+ break;
371
+ }
372
+ if (item.given_runeword) {
373
+ //fix delerium on d2gs??? why is this a thing?
374
+ let runeword_id = item.runeword_id;
375
+ if (runeword_id == 2718) {
376
+ runeword_id = 48;
377
+ }
378
+ writer.WriteUInt16(runeword_id, 12);
379
+ writer.WriteUInt8(5, 4); //always 5?
380
+ }
381
+ if (item.personalized) {
382
+ const name = item.personalized_name.substring(0, 16);
383
+ for (let i = 0; i < name.length; i++) {
384
+ if (version > 0x61) {
385
+ writer.WriteUInt8(name.charCodeAt(i), 8);
386
+ }
387
+ else {
388
+ writer.WriteUInt8(name.charCodeAt(i) & 0x7f, 7);
389
+ }
390
+ }
391
+ writer.WriteUInt8(0x00, version > 0x61 ? 8 : 7);
392
+ }
393
+ if (item.type === "tbk") {
394
+ writer.WriteUInt8(0, 5);
395
+ }
396
+ else if (item.type === "ibk") {
397
+ writer.WriteUInt8(1, 5);
398
+ }
399
+ writer.WriteUInt8(item.timestamp, 1);
400
+ if (item.type_id === ItemType.Armor || item.type_id === ItemType.Shield) {
401
+ writer.WriteUInt16(item.defense_rating + constants.magical_properties[31].sA, constants.magical_properties[31].sB);
402
+ }
403
+ if (item.type_id === ItemType.Armor || item.type_id === ItemType.Shield || item.type_id === ItemType.Weapon) {
404
+ writer.WriteUInt16(item.max_durability || 0, constants.magical_properties[73].sB);
405
+ if (item.max_durability > 0) {
406
+ writer.WriteUInt16(item.current_durability, constants.magical_properties[72].sB);
407
+ }
408
+ }
409
+ if (constants.stackables[item.type]) {
410
+ writer.WriteUInt16(item.quantity, 9);
411
+ }
412
+ if (item.socketed === 1) {
413
+ writer.WriteUInt8(item.total_nr_of_sockets, 4);
414
+ }
415
+ if (item.quality === Quality.Set) {
416
+ const set_attribute_count = item.set_attributes != null ? item.set_attributes.length : 0;
417
+ //reduced by -1 removed as this seems to be wrong
418
+ const plist_flag = (1 << set_attribute_count) - 1;
419
+ writer.WriteUInt8(item._unknown_data.plist_flag || plist_flag, 5);
420
+ }
421
+ _writeMagicProperties(writer, item.magic_attributes, constants);
422
+ if (item.set_attributes && item.set_attributes.length > 0) {
423
+ for (let i = 0; i < item.set_attributes.length; i++) {
424
+ _writeMagicProperties(writer, item.set_attributes[i], constants);
425
+ }
426
+ }
427
+ if (item.given_runeword === 1) {
428
+ _writeMagicProperties(writer, item.runeword_attributes, constants);
429
+ }
430
+ }
431
+ writer.Align();
432
+ if (item.nr_of_items_in_sockets > 0 && item.simple_item === 0) {
433
+ for (let i = 0; i < item.nr_of_items_in_sockets; i++) {
434
+ writer.WriteArray(await writeItem(item.socketed_items[i], version, constants, config));
435
+ }
436
+ }
437
+ return writer.ToArray();
438
+ }
439
+ exports.writeItem = writeItem;
440
+ function _readSimpleBits(item, reader, version, constants, config) {
441
+ var _a, _b, _c;
442
+ //init so we do not have npe's
443
+ item._unknown_data = {};
444
+ //1.10-1.14d
445
+ //[flags:32][version:10][mode:3]([invloc:4][x:4][y:4][page:3])([itemcode:32])([sockets:3])
446
+ //1.15
447
+ //[flags:32][version:3][mode:3]([invloc:4][x:4][y:4][page:3])([itemcode:variable])([sockets:3])
448
+ item._unknown_data.b0_3 = reader.ReadBitArray(4);
449
+ item.identified = reader.ReadBit();
450
+ item._unknown_data.b5_10 = reader.ReadBitArray(6);
451
+ item.socketed = reader.ReadBit();
452
+ item._unknown_data.b12 = reader.ReadBitArray(1);
453
+ item.new = reader.ReadBit();
454
+ item._unknown_data.b14_15 = reader.ReadBitArray(2);
455
+ item.is_ear = reader.ReadBit();
456
+ item.starter_item = reader.ReadBit();
457
+ item._unknown_data.b18_20 = reader.ReadBitArray(3);
458
+ item.simple_item = reader.ReadBit();
459
+ item.ethereal = reader.ReadBit();
460
+ item._unknown_data.b23 = reader.ReadBitArray(1);
461
+ item.personalized = reader.ReadBit();
462
+ item._unknown_data.b25 = reader.ReadBitArray(1);
463
+ item.given_runeword = reader.ReadBit();
464
+ item._unknown_data.b27_31 = reader.ReadBitArray(5);
465
+ if (version <= 0x60) {
466
+ item.version = reader.ReadUInt16(10).toString(10);
467
+ }
468
+ else if (version >= 0x61) {
469
+ item.version = reader.ReadUInt16(3).toString(2);
470
+ }
471
+ item.location_id = reader.ReadUInt8(3);
472
+ item.equipped_id = reader.ReadUInt8(4);
473
+ item.position_x = reader.ReadUInt8(4);
474
+ item.position_y = reader.ReadUInt8(4);
475
+ item.alt_position_id = reader.ReadUInt8(3);
476
+ if (item.is_ear) {
477
+ const clazz = reader.ReadUInt8(3);
478
+ const level = reader.ReadUInt8(7);
479
+ const arr = new Uint8Array(15);
480
+ for (let i = 0; i < arr.length; i++) {
481
+ arr[i] = reader.ReadUInt8(7);
482
+ if (arr[i] === 0x00) {
483
+ break;
484
+ }
485
+ }
486
+ const name = new bitreader_1.BitReader(arr).ReadString(15).trim().replace(/\0/g, "");
487
+ item.ear_attributes = {
488
+ class: clazz,
489
+ level: level,
490
+ name: name,
491
+ };
492
+ }
493
+ else {
494
+ if (version <= 0x60) {
495
+ item.type = reader.ReadString(4);
496
+ }
497
+ else if (version >= 0x61) {
498
+ item.type = "";
499
+ //props to d07riv
500
+ //https://github.com/d07RiV/d07riv.github.io/blob/master/d2r.html#L11-L20
501
+ for (let i = 0; i < 4; i++) {
502
+ let node = HUFFMAN;
503
+ do {
504
+ node = node[reader.ReadBit()];
505
+ } while (Array.isArray(node));
506
+ item.type += node;
507
+ }
508
+ }
509
+ item.type = item.type.trim().replace(/\0/g, "");
510
+ let details = _GetItemTXT(item, constants);
511
+ item.categories = details === null || details === void 0 ? void 0 : details.c;
512
+ if ((_a = item === null || item === void 0 ? void 0 : item.categories) === null || _a === void 0 ? void 0 : _a.includes("Any Armor")) {
513
+ item.type_id = ItemType.Armor;
514
+ }
515
+ else if ((_b = item === null || item === void 0 ? void 0 : item.categories) === null || _b === void 0 ? void 0 : _b.includes("Weapon")) {
516
+ item.type_id = ItemType.Weapon;
517
+ details = constants.weapon_items[item.type];
518
+ }
519
+ else {
520
+ item.type_id = ItemType.Other;
521
+ }
522
+ let bits = item.simple_item ? 1 : 3;
523
+ if ((_c = item.categories) === null || _c === void 0 ? void 0 : _c.includes("Quest")) {
524
+ item.quest_difficulty = reader.ReadUInt16(constants.magical_properties[356].sB) - constants.magical_properties[356].sA;
525
+ bits = 1;
526
+ }
527
+ item.nr_of_items_in_sockets = reader.ReadUInt8(bits);
528
+ }
529
+ }
530
+ function _lookupRareId(name, constants) {
531
+ //some inconsistencies with txt data and nokka. so have to hack it with startsWith
532
+ return constants.rare_names.findIndex((k) => k && (k.n.toLowerCase().startsWith(name.toLowerCase()) || name.toLowerCase().startsWith(k.n.toLowerCase())));
533
+ }
534
+ function _writeSimpleBits(writer, version, item, constants, config) {
535
+ var _a;
536
+ writer.WriteBits(item._unknown_data.b0_3 || new Uint8Array(4), 4);
537
+ writer.WriteBit(item.identified);
538
+ writer.WriteBits(item._unknown_data.b5_10 || new Uint8Array(6), 6);
539
+ writer.WriteBit(item.socketed);
540
+ writer.WriteBits(item._unknown_data.b12 || new Uint8Array(1), 1);
541
+ writer.WriteBit(item.new);
542
+ writer.WriteBits(item._unknown_data.b14_15 || new Uint8Array(2), 2);
543
+ writer.WriteBit(item.is_ear);
544
+ writer.WriteBit(item.starter_item);
545
+ writer.WriteBits(item._unknown_data.b18_20 || new Uint8Array(3), 3);
546
+ writer.WriteBit(item.simple_item);
547
+ writer.WriteBit(item.ethereal);
548
+ writer.WriteBits(item._unknown_data.b23 || new Uint8Array([1]), 1); //always 1? IFLAG_JUSTSAVED
549
+ writer.WriteBit(item.personalized);
550
+ writer.WriteBits(item._unknown_data.b25 || new Uint8Array(1), 1); //IFLAG_LOWQUALITY
551
+ writer.WriteBit(item.given_runeword);
552
+ writer.WriteBits(item._unknown_data.b27_31 || new Uint8Array(5), 5);
553
+ const itemVersion = item.version != null ? item.version : "101";
554
+ if (version <= 0x60) {
555
+ // 0 = pre-1.08; 1 = 1.08/1.09 normal; 2 = 1.10 normal; 100 = 1.08/1.09 expansion; 101 = 1.10 expansion
556
+ writer.WriteUInt16(parseInt(itemVersion, 10), 10);
557
+ }
558
+ else if (version >= 0x61) {
559
+ writer.WriteUInt16(parseInt(itemVersion, 2), 3);
560
+ }
561
+ writer.WriteUInt8(item.location_id, 3);
562
+ writer.WriteUInt8(item.equipped_id, 4);
563
+ writer.WriteUInt8(item.position_x, 4);
564
+ writer.WriteUInt8(item.position_y, 4);
565
+ writer.WriteUInt8(item.alt_position_id, 3);
566
+ if (item.is_ear) {
567
+ writer.WriteUInt8(item.ear_attributes.class, 3);
568
+ writer.WriteUInt8(item.ear_attributes.level, 7);
569
+ const name = item.ear_attributes.name.substring(0, 15);
570
+ for (let i = 0; i < name.length; i++) {
571
+ writer.WriteUInt8(name.charCodeAt(i) & 0x7f, 7);
572
+ }
573
+ writer.WriteUInt8(0x00, 7);
574
+ }
575
+ else {
576
+ const t = item.type.padEnd(4, " ");
577
+ if (version <= 0x60) {
578
+ writer.WriteString(t, 4);
579
+ }
580
+ else {
581
+ for (const c of t) {
582
+ const n = HUFFMAN_LOOKUP[c];
583
+ writer.WriteUInt16(n.v, n.l);
584
+ }
585
+ }
586
+ let bits = item.simple_item ? 1 : 3;
587
+ if ((_a = item.categories) === null || _a === void 0 ? void 0 : _a.includes("Quest")) {
588
+ const difficulty = item.quest_difficulty || 0;
589
+ writer.WriteUInt16(difficulty + constants.magical_properties[356].sA, constants.magical_properties[356].sB);
590
+ bits = 1;
591
+ }
592
+ writer.WriteUInt8(item.nr_of_items_in_sockets, bits);
593
+ }
594
+ }
595
+ function _readMagicProperties(reader, constants) {
596
+ let id = reader.ReadUInt16(9);
597
+ const magic_attributes = [];
598
+ while (id != 0x1ff) {
599
+ const values = [];
600
+ if (id > constants.magical_properties.length) {
601
+ throw new Error(`Invalid Stat Id: ${id} at position ${reader.offset - 9}`);
602
+ }
603
+ const num_of_properties = constants.magical_properties[id].np || 1;
604
+ for (let i = 0; i < num_of_properties; i++) {
605
+ const prop = constants.magical_properties[id + i];
606
+ if (prop == null) {
607
+ throw new Error(`Cannot find Magical Property for id: ${id} at position ${reader.offset}`);
608
+ }
609
+ if (prop.sP) {
610
+ let param = reader.ReadUInt16(prop.sP);
611
+ switch (prop.dF) {
612
+ case 14: //+skill to skilltab
613
+ values.push(param & 0x7);
614
+ param = (param >> 3) & 0x1fff;
615
+ break;
616
+ default:
617
+ break;
618
+ }
619
+ //encode
620
+ switch (prop.e) {
621
+ case 1:
622
+ //throw new Error(`Unimplemented encoding: ${prop.encode}`);
623
+ break;
624
+ case 2: //chance to cast
625
+ case 3: //charges
626
+ values.push(param & 0x3f); //skill level
627
+ param = (param >> 6) & 0x3ff; //skll id
628
+ break;
629
+ default:
630
+ break;
631
+ }
632
+ values.push(param);
633
+ }
634
+ if (!prop.sB) {
635
+ throw new Error(`Save Bits is undefined for stat: ${id}:${prop.s} at position ${reader.offset}`);
636
+ }
637
+ let v = reader.ReadUInt16(prop.sB);
638
+ if (prop.sA) {
639
+ v -= prop.sA;
640
+ }
641
+ switch (prop.e) {
642
+ case 3:
643
+ values.push(v & 0xff); // current charges
644
+ values.push((v >> 8) & 0xff); //max charges
645
+ break;
646
+ default:
647
+ values.push(v);
648
+ break;
649
+ }
650
+ }
651
+ magic_attributes.push({
652
+ id: id,
653
+ values: values,
654
+ name: constants.magical_properties[id].s,
655
+ });
656
+ id = reader.ReadUInt16(9);
657
+ }
658
+ return magic_attributes;
659
+ }
660
+ exports._readMagicProperties = _readMagicProperties;
661
+ function _writeMagicProperties(writer, properties, constants) {
662
+ if (properties) {
663
+ for (let i = 0; i < properties.length; i++) {
664
+ const property = properties[i];
665
+ let valueIdx = 0;
666
+ writer.WriteUInt16(property.id, 9);
667
+ const num_of_properties = constants.magical_properties[property.id].np || 1;
668
+ for (let j = 0; j < num_of_properties; j++) {
669
+ const prop = constants.magical_properties[property.id + j];
670
+ if (prop == null) {
671
+ throw new Error(`Cannot find Magical Property for id: ${property.id}`);
672
+ }
673
+ if (prop.sP) {
674
+ let param = property.values[valueIdx++];
675
+ switch (prop.dF) {
676
+ case 14: //+skill to skilltab
677
+ param |= (property.values[valueIdx++] & 0x1fff) << 3;
678
+ break;
679
+ default:
680
+ break;
681
+ }
682
+ //encode
683
+ switch (prop.e) {
684
+ case 1:
685
+ //throw new Error(`Unimplemented encoding: ${prop.encode}`);
686
+ break;
687
+ case 2: //chance to cast
688
+ case 3: //charges
689
+ param |= (property.values[valueIdx++] & 0x3ff) << 6;
690
+ break;
691
+ default:
692
+ break;
693
+ }
694
+ writer.WriteUInt32(param, prop.sP);
695
+ }
696
+ let v = property.values[valueIdx++];
697
+ if (prop.sA) {
698
+ v += prop.sA;
699
+ }
700
+ switch (prop.e) {
701
+ case 3:
702
+ v |= (property.values[valueIdx++] & 0xff) << 8;
703
+ break;
704
+ default:
705
+ break;
706
+ }
707
+ if (!prop.sB) {
708
+ throw new Error(`Save Bits is undefined for stat: ${property.id}:${prop.s}`);
709
+ }
710
+ writer.WriteUInt32(v, prop.sB);
711
+ }
712
+ }
713
+ }
714
+ writer.WriteUInt16(0x1ff, 9);
715
+ }
716
+ exports._writeMagicProperties = _writeMagicProperties;
717
+ function _GetItemTXT(item, constants) {
718
+ if (constants.armor_items[item.type]) {
719
+ return constants.armor_items[item.type];
720
+ }
721
+ else if (constants.weapon_items[item.type]) {
722
+ return constants.weapon_items[item.type];
723
+ }
724
+ else if (constants.other_items[item.type]) {
725
+ return constants.other_items[item.type];
726
+ }
727
+ }
728
+ //# sourceMappingURL=items.js.map