forked from ReClassNET/ReClass.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReClassScanFile.cs
More file actions
169 lines (149 loc) · 5.46 KB
/
Copy pathReClassScanFile.cs
File metadata and controls
169 lines (149 loc) · 5.46 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
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using ReClassNET.Logger;
using ReClassNET.MemoryScanner;
namespace ReClassNET.DataExchange.Scanner
{
public class ReClassScanFile : IScannerImport, IScannerExport
{
public const string FormatName = "ReClass.NET Scanner File";
public const string FileExtension = ".rcnetscan";
private const string Version1 = "1";
private const string DataFileName = "Data.xml";
public const string XmlRootElement = "records";
public const string XmlRecordElement = "record";
public const string XmlVersionAttribute = "version";
public const string XmlPlatformAttribute = "platform";
public const string XmlValueTypeAttribute = "type";
public const string XmlAddressAttribute = "address";
public const string XmlModuleAttribute = "module";
public const string XmlDescriptionAttribute = "description";
public const string XmlValueLengthAttribute = "length";
public const string XmlEncodingAttribute = "encoding";
public IEnumerable<MemoryRecord> Load(string filePath, ILogger logger)
{
using (var fs = new FileStream(filePath, FileMode.Open))
{
using (var archive = new ZipArchive(fs, ZipArchiveMode.Read))
{
var dataEntry = archive.GetEntry(DataFileName);
if (dataEntry == null)
{
throw new FormatException();
}
using (var entryStream = dataEntry.Open())
{
var document = XDocument.Load(entryStream);
if (document.Root == null)
{
logger.Log(LogLevel.Error, "File has not the correct format.");
yield break;
}
//var version = document.Root.Attribute(XmlVersionAttribute)?.Value;
var platform = document.Root.Attribute(XmlPlatformAttribute)?.Value;
if (platform != Constants.Platform)
{
logger.Log(LogLevel.Warning, $"The platform of the file ({platform}) doesn't match the program platform ({Constants.Platform}).");
}
foreach (var element in document.Root.Elements(XmlRecordElement))
{
var valueTypeStr = element.Attribute(XmlValueTypeAttribute)?.Value ?? string.Empty;
if (!Enum.TryParse<ScanValueType>(valueTypeStr, out var valueType))
{
logger?.Log(LogLevel.Warning, $"Unknown value type: {valueTypeStr}");
continue;
}
var description = element.Attribute(XmlDescriptionAttribute)?.Value ?? string.Empty;
var addressStr = element.Attribute(XmlAddressAttribute)?.Value ?? string.Empty;
var moduleName = element.Attribute(XmlModuleAttribute)?.Value ?? string.Empty;
long.TryParse(addressStr, NumberStyles.HexNumber, null, out var address);
var record = new MemoryRecord
{
Description = description,
AddressOrOffset = (IntPtr)address,
ValueType = valueType
};
if (!string.IsNullOrEmpty(moduleName))
{
record.ModuleName = moduleName;
}
if (valueType == ScanValueType.ArrayOfBytes || valueType == ScanValueType.String)
{
var lengthStr = element.Attribute(XmlValueLengthAttribute)?.Value ?? string.Empty;
int.TryParse(lengthStr, NumberStyles.Integer, null, out var valueLength);
record.ValueLength = Math.Max(1, valueLength);
if (valueType == ScanValueType.String)
{
switch (element.Attribute(XmlEncodingAttribute)?.Value ?? string.Empty)
{
default:
record.Encoding = Encoding.UTF8;
break;
case "UTF16":
record.Encoding = Encoding.Unicode;
break;
case "UTF32":
record.Encoding = Encoding.UTF32;
break;
}
}
}
yield return record;
}
}
}
}
}
public void Save(IEnumerable<MemoryRecord> records, string filePath, ILogger logger)
{
using (var fs = new FileStream(filePath, FileMode.Create))
{
using (var archive = new ZipArchive(fs, ZipArchiveMode.Create))
{
var dataEntry = archive.CreateEntry(DataFileName);
using (var entryStream = dataEntry.Open())
{
var document = new XDocument(
new XComment($"{Constants.ApplicationName} Scanner {Constants.ApplicationVersion} by {Constants.Author}"),
new XComment($"Website: {Constants.HomepageUrl}"),
new XElement(
XmlRootElement,
new XAttribute(XmlVersionAttribute, Version1),
new XAttribute(XmlPlatformAttribute, Constants.Platform),
records.Select(r =>
{
var temp = new XElement(
XmlRecordElement,
new XAttribute(XmlValueTypeAttribute, r.ValueType.ToString()),
new XAttribute(XmlDescriptionAttribute, r.Description ?? string.Empty),
new XAttribute(XmlAddressAttribute, r.AddressOrOffset.ToString(Constants.AddressHexFormat))
);
if (r.IsRelativeAddress)
{
temp.SetAttributeValue(XmlModuleAttribute, r.ModuleName);
}
if (r.ValueType == ScanValueType.ArrayOfBytes || r.ValueType == ScanValueType.String)
{
temp.SetAttributeValue(XmlValueLengthAttribute, r.ValueLength);
if (r.ValueType == ScanValueType.String)
{
temp.SetAttributeValue(XmlEncodingAttribute, r.Encoding == Encoding.UTF8 ? "UTF8" : r.Encoding == Encoding.Unicode ? "UTF16" : "UTF32");
}
}
return temp;
})
)
);
document.Save(entryStream);
}
}
}
}
}
}