-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathFileReader.cs
More file actions
258 lines (231 loc) · 8.33 KB
/
FileReader.cs
File metadata and controls
258 lines (231 loc) · 8.33 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
using ModShardLauncher.Mods;
using Serilog;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
namespace ModShardLauncher
{
public class FileChunk
{
public string name = string.Empty;
public int offset;
public int length;
}
public class ModSource
{
public string Name { get; set; }
public string Path { get; set; }
public bool Existed => File.Exists(Path);
public override string ToString()
{
return Name;
}
}
public enum PatchStatus
{
None,
Patching,
Success,
}
public class ModFile
{
public string Name;
public string Version { get; set; }
public List<FileChunk> Files = new();
public Assembly Assembly;
public int FileOffset;
public FileStream Stream;
public string Path;
public Mod instance { get; set; }
public bool Enabled { get; set; }
public PatchStatus PatchStatus { get; set; } = PatchStatus.None;
public bool Existed => File.Exists(Path);
public byte[] Icon { get; set; } = Array.Empty<byte>();
public override string ToString()
{
return instance.ToString();
}
public byte[] GetFile(string fileName)
{
if(!Existed)
{
Log.Error("The mod {0} which was located at {1} does not exist anymore.", Name, Path);
ModLoader.LoadFiles();
return Array.Empty<byte>();
}
// https://sonarsource.github.io/rspec/#/rspec/S6602/csharp
// for list, Find should be used instead of FirstOrDefault
FileChunk? file = Files.Find(t => System.IO.Path.GetFileName(t.name) == System.IO.Path.GetFileName(fileName));
if (file != null)
{
if(!Stream.CanRead) Stream = new FileStream(Path, FileMode.Open);
Stream.Position = FileOffset;
FileReader.Read(Stream, file.offset);
byte[] fileStream = FileReader.Read(Stream, file.length);
Stream.Close();
return fileStream;
}
throw new FileNotFoundException(string.Format("File {0} not found in the packed sml.", fileName));
}
public string GetCode(string fileName)
{
byte[] data = GetFile(fileName);
if (data.Length == 0)
{
Log.Warning("{0} is empty.", fileName);
return "";
}
// if a BOM is found aka: 0xEF 0xBB 0xBF at the beginning of the file, remove it since UTMT will not understand these characters.
// BOM are produced if a script is made through Visual Studio
if (data[0] == 0xEF && data[1] == 0xBB && data[2] == 0xBF) data = data.Skip(3).ToArray();
string text = Encoding.UTF8.GetString(data);
if(text.Length == 0)
{
Log.Error("The mod {0} which was located at {1} does not exist anymore.", Name, Path);
throw new ArgumentException("String cannot be of length zero");
}
return text;
}
public bool FileExist(string fileName)
{
return GetFile(fileName).Length > 0;
}
}
public static class FileReader
{
public static ModFile? Read(string path)
{
FileStream fs = new(path, FileMode.Open);
string nameMod = fs.Name.Split("\\")[^1].Replace(".sml", "");
ModFile file = new()
{
Stream = fs,
Name = nameMod
};
if (Encoding.UTF8.GetString(Read(fs, 4)) != "MSLM")
{
fs.Close();
return null;
}
Regex? reg = new("0([0-9])");
byte pointBytes = 0x2E;
byte zeroBytes = 0x30;
byte nineBytes = 0x39;
// the version number should be at least 24 bytes
byte[] readbytes = Read(fs, 24);
// resizing
int size = 24;
// i = 0 is a v
for (int i = 1; i < 24; i++)
{
// either a point or in a range of [0-9]
if (readbytes[i] != pointBytes && (readbytes[i] > nineBytes || readbytes[i] < zeroBytes))
{
size = i;
break;
}
}
if (size == 24)
{
Log.Warning("Version number seems ill formed");
}
// restarting the buffer
fs.Seek(-24, SeekOrigin.Current);
// reading the correct version
byte[] versionbytes = Read(fs, size);
file.Version = reg.Replace(Encoding.UTF8.GetString(versionbytes), "$1");
Log.Information("Reading {{{0}}} built with version {{{1}}}", nameMod, file.Version);
// read textures
int count = BitConverter.ToInt32(Read(fs, 4), 0);
for (int i = 0; i < count; i++)
{
int len = BitConverter.ToInt32(Read(fs, 4));
FileChunk chunk = new()
{
name = Encoding.UTF8.GetString(Read(fs, len)),
offset = BitConverter.ToInt32(Read(fs, 4)),
length = BitConverter.ToInt32(Read(fs, 4))
};
file.Files.Add(chunk);
}
// scripts
count = BitConverter.ToInt32(Read(fs, 4), 0);
for (int i = 0; i < count; i++)
{
int len = BitConverter.ToInt32(Read(fs, 4), 0);
FileChunk chunk = new()
{
name = Encoding.UTF8.GetString(Read(fs, len)),
offset = BitConverter.ToInt32(Read(fs, 4)),
length = BitConverter.ToInt32(Read(fs, 4))
};
file.Files.Add(chunk);
}
// codes
count = BitConverter.ToInt32(Read(fs, 4), 0);
for (int i = 0; i < count; i++)
{
int len = BitConverter.ToInt32(Read(fs, 4), 0);
FileChunk chunk = new()
{
name = Encoding.UTF8.GetString(Read(fs, len)),
offset = BitConverter.ToInt32(Read(fs, 4)),
length = BitConverter.ToInt32(Read(fs, 4))
};
file.Files.Add(chunk);
}
// assembly
count = BitConverter.ToInt32(Read(fs, 4), 0);
for (int i = 0; i < count; i++)
{
int len = BitConverter.ToInt32(Read(fs, 4), 0);
FileChunk chunk = new()
{
name = Encoding.UTF8.GetString(Read(fs, len)),
offset = BitConverter.ToInt32(Read(fs, 4)),
length = BitConverter.ToInt32(Read(fs, 4))
};
file.Files.Add(chunk);
}
file.FileOffset = (int)fs.Position;
int fileCount = file.Files.Count;
if(fileCount > 0)
{
FileChunk? f = file.Files[fileCount - 1];
Read(fs, f.offset + f.length);
}
count = BitConverter.ToInt32(Read(fs, 4), 0);
file.Assembly = Assembly.Load(Read(fs, count));
file.Path = path;
try
{
file.Icon = file.GetFile(file.Name + "\\icon.png");
}
catch
{
Log.Information("Cannot find the icon.png associated to {0}", fs.Name.Split("\\")[^1]);
}
fs.Close();
return file;
}
public static byte[] Read(FileStream fs, int length)
{
byte[] bytes = new byte[length];
if(fs.Length - fs.Position < length)
{
fs.Close();
throw new Exception($"In FileReader.Read cannot read {length} bytes in the mod {fs.Name.Split("\\")[^1]}");
}
fs.Read(bytes, 0, length);
return bytes;
}
}
}