forked from Windower/ResourceExtractor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fixes.cs
224 lines (206 loc) · 8.18 KB
/
Fixes.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
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
// <copyright file="ExtensionMethods.cs" company="Windower Team">
// Copyright © 2014 Windower Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
// </copyright>
namespace ResourceExtractor
{
using Microsoft.CSharp.RuntimeBinder;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Runtime.CompilerServices;
using System.Xml.Linq;
internal static class Fixes
{
public static void Apply(object obj, string path = "fixes.xml")
{
Apply(obj, XDocument.Load(path));
}
public static void Apply(object obj, Stream stream)
{
Apply(obj, XDocument.Load(stream));
}
public static void Apply(object obj, XDocument document)
{
Apply(obj, document.Root);
}
private static void Apply(object obj, XElement element)
{
if (IsList(obj.GetType()))
{
//// var count = (int)obj.GetType().GetProperty("Count").GetValue(obj);
foreach (var e in element.Elements("update"))
{
var key = (int)e.Attribute("key");
var value = (string)e.Attribute("value");
var type = (string)e.Attribute("type");
if (value != null)
{
SetIndex(obj, key, Convert(value, type));
}
else if (e.HasElements)
{
var current = GetIndex(obj, key);
if (type == "list")
{
if (current == null || !IsList(current.GetType()))
{
current = new List<object>();
SetIndex(obj, key, current);
}
}
else if (current == null)
{
current = new ModelObject();
SetIndex(obj, key, current);
}
Apply(current, e);
}
}
}
else
{
foreach (var e in element.Elements("update"))
{
var key = (string)e.Attribute("key");
if (key != null)
{
var value = (string)e.Attribute("value");
var type = (string)e.Attribute("type");
if (value != null)
{
SetDynamic(obj, key, Convert(value, type));
}
else if (e.HasElements)
{
var current = GetDynamic(obj, key);
if (type == "list")
{
if (current == null || !IsList(current.GetType()))
{
current = new List<object>();
SetDynamic(obj, key, current);
}
}
else if (current == null)
{
current = new ModelObject();
SetDynamic(obj, key, current);
}
Apply(current, e);
}
}
}
}
}
private static bool IsList(Type type)
{
foreach (var i in type.GetInterfaces())
{
if (i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IList<>))
{
return true;
}
}
return false;
}
private static object GetIndex(object obj, int key)
{
dynamic dyn = obj;
try
{
return dyn[key];
}
catch (ArgumentOutOfRangeException)
{
return null;
}
}
private static void SetIndex(object obj, int key, object value)
{
dynamic dyn = obj;
if (key < dyn.Count)
{
dyn[key] = value;
}
else
{
dyn.Insert(key, value);
}
}
private static object GetDynamic(object obj, string key)
{
try
{
var binder = Binder.GetMember(CSharpBinderFlags.None, key, obj.GetType(), new[] { CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null) });
var callsite = CallSite<Func<CallSite, object, object>>.Create(binder);
return callsite.Target(callsite, obj);
}
catch (RuntimeBinderException)
{
return null;
}
}
private static void SetDynamic(object obj, string key, object value)
{
var binder = Binder.SetMember(CSharpBinderFlags.None, key, obj.GetType(), new[] { CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null), CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null) });
var callsite = CallSite<Func<CallSite, object, object, object>>.Create(binder);
callsite.Target(callsite, obj, value);
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity")]
private static object Convert(string value, string type)
{
switch (type)
{
case null:
case "string":
return value;
case "bool":
return bool.Parse(value);
case "sbyte":
return sbyte.Parse(value, CultureInfo.InvariantCulture);
case "short":
return short.Parse(value, CultureInfo.InvariantCulture);
case "int":
return int.Parse(value, CultureInfo.InvariantCulture);
case "long":
return long.Parse(value, CultureInfo.InvariantCulture);
case "byte":
return byte.Parse(value, CultureInfo.InvariantCulture);
case "ushort":
return ushort.Parse(value, CultureInfo.InvariantCulture);
case "uint":
return uint.Parse(value, CultureInfo.InvariantCulture);
case "ulong":
return ulong.Parse(value, CultureInfo.InvariantCulture);
case "float":
return float.Parse(value, CultureInfo.InvariantCulture);
case "double":
return double.Parse(value, CultureInfo.InvariantCulture);
case "decimal":
return decimal.Parse(value, CultureInfo.InvariantCulture);
case "char":
return char.Parse(value);
}
return null;
}
}
}