-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathModUtilities.cs
130 lines (117 loc) · 5.35 KB
/
ModUtilities.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
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using StardewModdingAPI;
using StardewValley;
using MultiplayerMod;
using MultiplayerMod.Framework;
using System.Reflection;
using Microsoft.Xna.Framework.Graphics;
using StardewValley.Network;
namespace MultiplayerMod.Framework
{
public static class ModUtilities
{
public static bool IsAndroid
{
get
{
return Constants.TargetPlatform == GamePlatform.Android;
}
}
public static IMonitor ModMonitor;
public static Multiplayer multiplayer
{
get
{
return Helper.Reflection.GetField<Multiplayer>(typeof(Game1), "multiplayer").GetValue();
}
set
{
Helper.Reflection.GetField<Multiplayer>(typeof(Game1), "multiplayer").SetValue(value);
}
}
public static IModHelper Helper;
internal static Config ModConfig;
#region Reflection
public static T GetPrivatePropertyValue<T>(this object obj, string propName)
{
if (obj == null) throw new ArgumentNullException("obj"); PropertyInfo pi = obj.GetType().GetProperty(propName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
if (pi == null) throw new ArgumentOutOfRangeException("propName", string.Format("Property {0} was not found in Type {1}", propName, obj.GetType().FullName));
return (T)pi.GetValue(obj, null);
}
public static T GetPrivateFieldValue<T>(this object obj, string propName)
{
if (obj == null) throw new ArgumentNullException("obj");
Type t = obj.GetType(); FieldInfo fi = null; while (fi == null && t != null) { fi = t.GetField(propName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); t = t.BaseType; }
if (fi == null) throw new ArgumentOutOfRangeException("propName", string.Format("Field {0} was not found in Type {1}", propName, obj.GetType().FullName)); return (T)fi.GetValue(obj);
}
public static void SetPrivatePropertyValue<T>(this object obj, string propName, T val)
{
Type t = obj.GetType();
if (t.GetProperty(propName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance) == null) throw new ArgumentOutOfRangeException("propName", string.Format("Property {0} was not found in Type {1}", propName, obj.GetType().FullName)); t.InvokeMember(propName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.SetProperty | BindingFlags.Instance, null, obj, new object[] { val });
}
public static void SetPrivateFieldValue<T>(this object obj, string propName, T val)
{
if (obj == null) throw new ArgumentNullException("obj");
Type t = obj.GetType(); FieldInfo fi = null; while (fi == null && t != null) { fi = t.GetField(propName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); t = t.BaseType; }
if (fi == null) throw new ArgumentOutOfRangeException("propName", string.Format("Field {0} was not found in Type {1}", propName, obj.GetType().FullName)); fi.SetValue(obj, val);
}
public static T GetPrivateMethod<T>(this Type type, string name, BindingFlags bindingFlags, object[] obj)
{
var method = type.GetMethod(name, bindingFlags);
return (T)method.Invoke(null, obj);
}
public static T GetPrivateMethodStatic<T>(this Type type, string name, object[] obj) => GetPrivateMethod<T>(type, name, BindingFlags.Static | BindingFlags.NonPublic, obj);
public static T CreateInstanceProtected<T>(this Type type, object[] obj)
{
ConstructorInfo constructor = type.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, obj.ToTypes(), null);
object o = constructor.Invoke(obj);
if (o is T t)
{
return t;
}
return default(T);
}
public static T CreateInstanceInternal<T>(this Type type, object[] obj)
{
object o = Activator.CreateInstance(type, obj);
if (o is T t)
{
return t;
}
return default(T);
}
public static T CreateInstance<T>(this Type type, object[] obj)
{
ConstructorInfo constructor = type.GetConstructor(BindingFlags.Public | BindingFlags.Instance, null, obj.ToTypes(), null);
object o = constructor.Invoke(obj);
if (o is T t)
{
return t;
}
return default(T);
}
public static T CreateInstance<T>(this Type type, object[] obj, Type[] types)
{
ConstructorInfo constructor = type.GetConstructor(BindingFlags.Public | BindingFlags.Instance, null, types, null);
object o = constructor.Invoke(obj);
if (o is T t)
{
return t;
}
return default(T);
}
public static Type[] ToTypes<T>(this T[] value)
{
List<Type> types = new List<Type>();
for (int i = 0; i < value.Length; i++)
{
types.Add(value[i].GetType());
}
return types.ToArray();
}
#endregion
}
}