-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathD2S.cs
184 lines (152 loc) · 8.64 KB
/
D2S.cs
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
using D2SLib2.Model;
using D2SLib2.BinaryHandler;
using D2SLib2.Structure.Header;
using D2SLib2.Structure.Other;
using D2SLib2.Structure.Player;
using D2SLib2.Structure.Quests;
using D2SLib2.Structure.Waypoints;
using D2SLib2.Structure.Player.Item;
using Newtonsoft.Json;
using System.Formats.Asn1;
// How to scaffold your database you dipshit:
// Scaffold-DbContext "DataSource=C:\Users\ShadowEvil\source\repos\D2SLib2\Database\D2SLibData.db" Microsoft.EntityFrameworkCore.Sqlite -outputdir tmpModel
namespace D2SLib2
{
public class D2S
{
[JsonIgnore]
public string OpenFilePath = string.Empty;
[JsonIgnore]
public static D2S? instance { get; private set; } = null;
public BitwiseBinaryReader? d2sReader { get; private set; } = null;
public int initialFileSize { get; private set; } = 0;
[JsonIgnore]
public D2slibDataContext? dbContext { get; private set; } = null;
public Header? fileHeader { get; set; }
public PlayerInformation? playerInformation { get; set; } = null;
[JsonIgnore]
public MenuAppearance? menuAppearance { get; set; } = null;
public Mercenary? mercenary { get; set; } = null;
public QuestBook? questBook { get; set; } = null;
public WaypointBook? waypointBook { get; set; } = null;
public NPCIntroduction? NPC { get; set; } = null;
[JsonIgnore]
public HuffmanTree itemCodeTree = new HuffmanTree();
[JsonIgnore]
public bool CloseFlag = false;
public D2S(string filepath)
{
if (!File.Exists(filepath))
throw new FileNotFoundException($"{filepath} not found.");
OpenFilePath = filepath;
instance = this;
itemCodeTree.Build();
dbContext = new D2slibDataContext();
d2sReader = new BitwiseBinaryReader(OpenFilePath);
initialFileSize = d2sReader.ByteArray!.Length;
string fileName = Path.GetFileNameWithoutExtension(OpenFilePath);
Logger.LogPath = Directory.GetCurrentDirectory() + $"\\Logs\\{fileName}.d2slog";
Logger.WriteHeader("#--------------------------------------------------------------------------------------------------------------------#");
Logger.WriteHeader("#- -#");
Logger.WriteHeader("#- D2S Log File for debugging -#");
Logger.WriteHeader("#- v0.5.0.1 -#");
Logger.WriteHeader("#- -#");
Logger.WriteHeader("#--------------------------------------------------------------------------------------------------------------------#");
Logger.WriteBeginSection("[Looking for Inventory]");
Inventory.FindInventoryOffsetInBytes(d2sReader);
Logger.WriteEndSection("[End of Inventory Search]");
Logger.WriteBeginSection("[Looking for Skills]");
SkillsClass.FindSkillOffsetInBytes(d2sReader);
Logger.WriteEndSection("[End of Skills Search]");
Logger.WriteBeginSection("[Begin Reading Header]");
fileHeader = Header.Read(d2sReader);
Logger.WriteEndSection("[End Reading Header]");
if (!CloseFlag)
{
Logger.WriteBeginSection("[Begin Mercenary]");
mercenary = Mercenary.Read(d2sReader);
Logger.WriteEndSection("[End Mercenary]");
Logger.WriteBeginSection("[Begin Player Information]");
playerInformation = PlayerInformation.Read(d2sReader);
Logger.WriteEndSection("[End Player Information]");
Logger.WriteBeginSection("[Begin Inventory Reading]");
d2sReader.SetBitPosition(Inventory.InventoryOffset);
playerInformation.inventory = Inventory.Read(d2sReader);
Inventory.EndInventoryOffset = d2sReader.bitPosition;
Logger.WriteEndSection("[End Inventory Reading]");
Logger.WriteBeginSection("[Begin Corpse Inventory Reading]");
playerInformation.ReadCorpseInformation(d2sReader);
Logger.WriteEndSection("[End Corpse Inventory Reading]");
if (playerInformation.statusClass.Expansion)
{
Logger.WriteBeginSection("[Begin Mercenary Inventory Reading]");
playerInformation.ReadMercenaryInventory(d2sReader);
Logger.WriteEndSection("[End Mercenary Inventory Reading]");
Logger.WriteBeginSection("[Begin Iron Golem Inventory Reading]");
playerInformation.ReadIronGolemInventory(d2sReader);
Logger.WriteEndSection("[End Iron Golem Inventory Reading]");
}
Logger.WriteBeginSection("[Begin D2Menu Appearance]");
menuAppearance = new MenuAppearance();
menuAppearance.Read(d2sReader);
Logger.WriteEndSection("[End D2Menu Appearance]");
Logger.WriteBeginSection("[Begin D2R Appearance]");
menuAppearance.ReadD2R(d2sReader);
Logger.WriteEndSection("[End D2R Appearance]");
Logger.WriteBeginSection("[Begin Quests]");
questBook = QuestBook.Read(d2sReader);
Logger.WriteEndSection("[End Quests]");
Logger.WriteBeginSection("[Begin Waypoints]");
waypointBook = WaypointBook.Read(d2sReader);
Logger.WriteEndSection("[End Waypoints]");
Logger.WriteBeginSection("[Begin NPC]");
NPC = NPCIntroduction.Read(d2sReader);
Logger.WriteEndSection("[End NPC]");
}
Logger.Close();
}
public byte[]? WriteD2S()
{
if (d2sReader == null) return null;
BitwiseBinaryWriter writer = new BitwiseBinaryWriter();
fileHeader! .Write(writer, d2sReader);
playerInformation! .WriteActiveWeapon(writer);
playerInformation! .WriteCharacterStatus(writer);
playerInformation! .WriteProgression(writer);
playerInformation! .WriteClass(writer);
playerInformation! .WritePlayerLevel(writer);
playerInformation! .WriteLastPlayed(writer);
playerInformation! .WriteAssignedSkills(writer);
menuAppearance! .Write(writer);
playerInformation!.Normal! .Write(writer, 0);
playerInformation!.Nightmare! .Write(writer, 1);
playerInformation!.Hell! .Write(writer, 2);
playerInformation! .WriteMapSeed(writer);
mercenary! .Write(writer);
menuAppearance! .WriteD2S(writer);
playerInformation! .WritePlayerName(writer);
questBook! .WriteQuests(writer);
waypointBook! .WriteWaypoints(writer);
NPC! .Write(writer);
playerInformation.attributes! .Write(writer);
playerInformation.skillsClass! .WriteSkills(writer);
// TODO:
playerInformation.inventory! .WriteInventory(writer);
playerInformation! .WriteCorpseInformation(writer);
playerInformation! .WriteMercenaryInventoryInformation(writer);
playerInformation! .WriteIronGolemInformation(writer);
byte[] bytes = writer.GetBytes();
Header.WriteNewFileSize(ref bytes, (uint)bytes.Length);
byte[] checksumBytes = Checksum.ComputeChecksum(bytes);
Checksum.WriteChecksumBytes(ref bytes, checksumBytes);
return bytes;
}
public static void ConvertD2SToJson(D2S d2sInstance)
{
string jsonString = JsonConvert.SerializeObject(d2sInstance, Formatting.Indented);
string? filePathDirectory = Path.GetDirectoryName(d2sInstance.OpenFilePath);
string fileName = d2sInstance.playerInformation!.PlayerName + ".json";
File.WriteAllText(filePathDirectory + "\\" + fileName, jsonString);
}
}
}