forked from DatZach/CharacterEditor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCharacterData.cs
More file actions
665 lines (586 loc) · 13.6 KB
/
CharacterData.cs
File metadata and controls
665 lines (586 loc) · 13.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
namespace CharacterEditor
{
public class CharacterData
{
public const int EquipmentCount = 13;
public const int InventoryCount = 4;
public const int SkillCount = 11;
public enum ClassType : byte
{
None,
Warrior,
Ranger,
Mage,
Rogue
};
public int DatabaseIndex { get; private set; }
public int EntityId { get; private set; }
public double PositionX { get; private set; }
public double PositionY { get; private set; }
public double PositionZ { get; private set; }
public float Pitch { get; private set; }
public float Roll { get; private set; }
public float Yaw { get; private set; }
public float Health;
public int Experience;
public int Level;
public ClassType Class;
public byte Specialization;
private uint unknown1;
private uint unknown2;
public List<Item> Equipment;
public string Name;
public int Race;
public byte Gender;
public int Face;
public int Hair;
public Color HairColor;
public List<Inventory> Inventories;
public int Coins;
public int PlatinumCoins;
public List<Item> CraftingRecipes;
public List<World> Worlds;
public World LastWorld;
private uint unknown3;
public int PetMasterSkillLevel;
public int PetRidingSkillLevel;
public int ClimbingSkillLevel;
public int HangGlidingSkillLevel;
public int SwimmingSkillLevel;
public int SailingSkillLevel;
public int TierOneSkillLevel;
public int TierTwoSkillLevel;
public int TierThreeSkillLevel;
private int unknown4;
private int unknown5;
// Entity IDs found at: https://docs.google.com/spreadsheet/lv?key=0As7kattQ9kwbdFp6ZTlzV0R1RVRaZklBbmZZb2lBZ2c&f=true&noheader=true&gid=5
public static readonly List<byte> PetKinds = new List<byte>
{
0, 19, 20, 22, 23, 25, 26, 27, 28, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 50, 53, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 73, 74, 75, 85, 86, 87, 88, 89, 90, 91, 92, 93, 98, 99, 102, 103, 104, 105, 106, 151
};
public CharacterData(int index)
{
DatabaseIndex = index;
Equipment = new List<Item>();
Inventories = new List<Inventory>();
CraftingRecipes = new List<Item>();
Worlds = new List<World>();
}
public void Load(Database database)
{
byte[] characterData = database.ReadCharacterBlob(DatabaseIndex);
using (BinaryReader reader = new BinaryReader(new MemoryStream(characterData)))
Read(reader);
using (BinaryWriter writer = new BinaryWriter(new FileStream("read.bin", FileMode.Create)))
writer.Write(characterData);
}
public bool Save(Database database)
{
MemoryStream saveDataStream = new MemoryStream();
using (BinaryWriter writer = new BinaryWriter(saveDataStream))
Write(writer);
using (BinaryWriter writer = new BinaryWriter(new FileStream("wrote.bin", FileMode.Create)))
writer.Write(saveDataStream.ToArray());
return database.WriteCharacterBlob(DatabaseIndex, saveDataStream.ToArray());
}
public void Read(BinaryReader reader)
{
EntityId = reader.ReadInt32();
PositionX = reader.ReadDouble(); // TODO Might need to change this to long to prevent NaN exceptions being thrown
PositionY = reader.ReadDouble();
PositionZ = reader.ReadDouble();
Pitch = reader.ReadSingle();
Roll = reader.ReadSingle();
Yaw = reader.ReadSingle();
Health = reader.ReadSingle();
Experience = reader.ReadInt32();
Level = reader.ReadInt32();
Class = (ClassType)reader.ReadByte();
Specialization = reader.ReadByte();
unknown1 = reader.ReadUInt32();
unknown2 = reader.ReadUInt32();
for (int i = 0; i < EquipmentCount; ++i)
{
Item item = new Item();
item.Read(reader);
Equipment.Add(item);
}
Name = reader.ReadLongString();
Race = reader.ReadInt32();
Gender = reader.ReadByte();
reader.Skip(3);
Face = reader.ReadInt32();
Hair = reader.ReadInt32();
HairColor = Utility.FromAbgr(reader.ReadInt32());
int inventoryCount = reader.ReadInt32();
for (int i = 0; i < inventoryCount; ++i)
{
Inventory inventory = new Inventory();
inventory.Read(reader);
Inventories.Add(inventory);
}
Coins = reader.ReadInt32();
PlatinumCoins = reader.ReadInt32();
int craftingRecipeCount = reader.ReadInt32();
for (int i = 0; i < craftingRecipeCount; ++i)
{
Item item = new Item();
item.Read(reader);
CraftingRecipes.Add(item);
}
int worldCount = reader.ReadInt32();
for (int i = 0; i < worldCount; ++i)
{
World world = new World();
world.Read(reader);
Worlds.Add(world);
}
int lastWorldSeed = reader.ReadInt32();
string lastWorldName = reader.ReadLongString();
LastWorld = Worlds.FirstOrDefault(w => w.Seed == lastWorldSeed && w.Name == lastWorldName);
unknown3 = reader.ReadUInt32();
reader.Skip(4);
PetMasterSkillLevel = reader.ReadInt32();
PetRidingSkillLevel = reader.ReadInt32();
ClimbingSkillLevel = reader.ReadInt32();
HangGlidingSkillLevel = reader.ReadInt32();
SwimmingSkillLevel = reader.ReadInt32();
SailingSkillLevel = reader.ReadInt32();
TierOneSkillLevel = reader.ReadInt32();
TierTwoSkillLevel = reader.ReadInt32();
TierThreeSkillLevel = reader.ReadInt32();
unknown4 = reader.ReadInt32();
unknown5 = reader.ReadInt32();
}
public void Write(BinaryWriter writer)
{
writer.Write(EntityId);
writer.Write(PositionX);
writer.Write(PositionY);
writer.Write(PositionZ);
writer.Write(Pitch);
writer.Write(Roll);
writer.Write(Yaw);
writer.Write(Health);
writer.Write(Experience);
writer.Write(Level);
writer.Write((byte)Class);
writer.Write(Specialization);
writer.Write(unknown1);
writer.Write(unknown2);
foreach (Item equipment in Equipment)
equipment.Write(writer);
writer.WriteLongString(Name); // Should be 0xE76
writer.Write(Race);
writer.Write(Gender);
writer.Skip(3);
writer.Write(Face);
writer.Write(Hair);
writer.Write(Utility.ToAbgr(HairColor));
writer.Write(InventoryCount);
foreach (Inventory inventory in Inventories)
inventory.Write(writer);
writer.Write(Coins);
writer.Write(PlatinumCoins);
writer.Write(CraftingRecipes.Count);
foreach (Item recipe in CraftingRecipes)
recipe.Write(writer);
writer.Write(Worlds.Count);
foreach (World world in Worlds)
world.Write(writer);
writer.Write(LastWorld.Seed);
writer.WriteLongString(LastWorld.Name);
writer.Write(unknown3);
writer.Write(SkillCount);
writer.Write(PetMasterSkillLevel);
writer.Write(PetRidingSkillLevel);
writer.Write(ClimbingSkillLevel);
writer.Write(HangGlidingSkillLevel);
writer.Write(SwimmingSkillLevel);
writer.Write(SailingSkillLevel);
writer.Write(TierOneSkillLevel);
writer.Write(TierTwoSkillLevel);
writer.Write(TierThreeSkillLevel);
writer.Write(unknown4);
writer.Write(unknown5);
}
}
public interface ICharacterDataBlob
{
void Read(BinaryReader reader);
void Write(BinaryWriter writer);
}
public class Item : ICharacterDataBlob
{
public const int AttributeCount = 32;
public static readonly string[] MaterialNames = new[]
{
"",
"Iron",
"Wood",
"",
"",
"Obsidian",
"",
"Bone",
"",
"",
"Copper",
"Gold",
"Silver",
"Emerald",
"Sapphire",
"Ruby",
"Diamond",
"Sandstone",
"Saurian",
"Parrot",
"Mammoth",
"Plant",
"Ice",
"Licht",
"Glass",
"Silk",
"Linen",
"Cotton"
};
// TODO Still kill me
// [type][subtype]
public static readonly string[][] Subtypes = new[]
{
new []
{
""
},
new []
{
"Cookie",
"Life Potion",
"Cactus Potion",
"Mana Potion",
"Ginseng Soup",
"Snowberry Soup",
"Snowberry Mash",
"Mushroom Spit",
"Bomb",
"Pineapple Slice",
"Pumpkin Muffin"
},
new []
{
""
},
new []
{
"Sword",
"Axe",
"Mace",
"Dagger",
"Fist",
"Longsword",
"Bow",
"Crossbow",
"Boomarang",
"Arrow",
"Staff",
"Wand",
"Bracelet",
"Shield",
"Arrows",
"Greatsword",
"Greataxe",
"Greatmace",
"Rake",
"Pickaxe",
"Torch"
},
new []
{
"Chest"
},
new []
{
"Gloves"
},
new []
{
"Boots"
},
new []
{
"Sholder Armor"
},
new []
{
"Amulet"
},
new []
{
"Ring"
},
new []
{
"Block"
},
new []
{
"Nugget",
"Log",
"Feather",
"Horn",
"Claw",
"Fiber",
"Cobweb",
"Hair",
"Crystal",
"Yarn",
"Cube",
"Capsule",
"Flask",
"Orb",
"Spirit",
"Mushroom",
"Pumpkin",
"Pineapple",
"Radish Slice",
"Shimmer Mushroom",
"Ginseng Root",
"Onion Slice",
"Heartflower",
"Prickly Pear",
"Iceflower",
"Soulflower",
"Water Flask",
"Snowberry"
},
new []
{
"Coin"
},
new []
{
"Platinum Coin"
},
new []
{
"Leftovers"
},
new []
{
"Beak"
},
new []
{
"Painting"
},
new []
{
"Vase"
},
new []
{
"Candle",
"Haunted Candle"
},
new []
{
""
},
new []
{
"Fuck pet food"
},
new []
{
"Amulet (Gold)",
"Amulet (sapphire)",
"Jewel Case",
"Key",
"Medicine",
"Antivenom",
"Band Aid",
"Crutch",
"Bandage",
"Salve"
},
new []
{
""
},
new []
{
"Hang Glider",
"Boat"
},
new []
{
"Lamp"
},
new []
{
"Mana Cube"
}
};
public byte Type;
public byte Subtype;
public short Modifier;
private int unknown1;
public byte Rarity;
public byte Material;
public byte Flags;
public short Level;
public List<ItemAttribute> Attributes;
public Item()
{
Attributes = new List<ItemAttribute>();
}
public string FriendlyName
{
get
{
// <Modifier> <Material> <Item Name>
StringBuilder name = new StringBuilder();
if (Material != 0)
{
name.Append(MaterialNames[Material]);
name.Append(" ");
}
name.Append(Type == 0x14 ? "Pet Cage" : Subtypes[Type][Subtype]);
return name.ToString();
}
}
public void Read(BinaryReader reader)
{
Type = reader.ReadByte();
Subtype = reader.ReadByte();
reader.Skip(2);
Modifier = reader.ReadInt16();
reader.Skip(2);
unknown1 = reader.ReadInt32();
Rarity = reader.ReadByte();
Material = reader.ReadByte();
Flags = reader.ReadByte();
reader.Skip(1);
Level = reader.ReadInt16();
reader.Skip(2);
for (int i = 0; i < AttributeCount; ++i)
{
ItemAttribute attribute = new ItemAttribute();
attribute.Read(reader);
Attributes.Add(attribute);
}
// AttributesUsed is calculated on write
reader.Skip(4);
}
public void Write(BinaryWriter writer)
{
writer.Write(Type);
writer.Write(Subtype);
writer.Skip(2);
writer.Write(Modifier);
writer.Skip(2);
writer.Write(unknown1);
writer.Write(Rarity);
writer.Write(Material);
writer.Write(Flags);
writer.Skip(1);
writer.Write(Level);
writer.Skip(2);
foreach (ItemAttribute attribute in Attributes)
attribute.Write(writer);
writer.Write(Attributes.Count(attr => attr.Used));
}
}
public class ItemAttribute : ICharacterDataBlob
{
public byte OffsetX;
public byte OffsetY;
public byte OffsetZ;
public byte Material;
public short Level;
public bool Used
{
get
{
return OffsetX + OffsetY + OffsetZ + Material + Level > 0;
}
}
public void Read(BinaryReader reader)
{
OffsetX = reader.ReadByte();
OffsetY = reader.ReadByte();
OffsetZ = reader.ReadByte();
Material = reader.ReadByte();
Level = reader.ReadInt16();
reader.Skip(2);
}
public void Write(BinaryWriter writer)
{
writer.Write(OffsetX);
writer.Write(OffsetY);
writer.Write(OffsetZ);
writer.Write(Material);
writer.Write(Level);
writer.Skip(2);
}
}
public class Inventory : ICharacterDataBlob
{
public const int DefaultItemCount = 50;
public List<Tuple<int, Item>> Items;
public Inventory()
{
Items = new List<Tuple<int, Item>>();
}
public void Read(BinaryReader reader)
{
int inventoryCount = reader.ReadInt32();
for (int i = 0; i < inventoryCount; ++i)
{
int count = reader.ReadInt32();
Item item = new Item();
item.Read(reader);
Items.Add(new Tuple<int, Item>(count, item));
}
}
public void Write(BinaryWriter writer)
{
writer.Write(Items.Count);
foreach (Tuple<int, Item> item in Items)
{
writer.Write(item.Item1);
item.Item2.Write(writer);
}
}
}
public class World : ICharacterDataBlob
{
public int Seed;
public string Name;
public long X;
public long Y;
public long Z;
public void Read(BinaryReader reader)
{
Seed = reader.ReadInt32();
Name = reader.ReadLongString();
X = reader.ReadInt64();
Y = reader.ReadInt64();
Z = reader.ReadInt64();
}
public void Write(BinaryWriter writer)
{
writer.Write(Seed);
writer.WriteLongString(Name);
writer.Write(X);
writer.Write(Y);
writer.Write(Z);
}
}
}