-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUtil.cs
More file actions
162 lines (125 loc) · 5.27 KB
/
Util.cs
File metadata and controls
162 lines (125 loc) · 5.27 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
namespace Pluton.Core
{
using Pluton.Core.PluginLoaders;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text.RegularExpressions;
using UnityEngine;
public class Util : Singleton<Util>, ISingleton
{
public readonly Dictionary<string, Type> typeCache = new Dictionary<string, Type>();
public void DestroyObject(GameObject go) => UnityEngine.Object.DestroyImmediate(go);
public bool DumpObjToFile(string path, object obj, string prefix = "")
=> DumpObjToFile(path, obj, 1, 30, false, false, prefix);
public bool DumpObjToFile(string path, object obj, int depth, string prefix = "")
=> DumpObjToFile(path, obj, depth, 30, false, false, prefix);
public bool DumpObjToFile(string path, object obj, int depth, int maxItems, string prefix = "")
=> DumpObjToFile(path, obj, depth, maxItems, false, false, prefix);
public bool DumpObjToFile(string path, object obj, int depth, int maxItems, bool disPrivate, string prefix = "")
=> DumpObjToFile(path, obj, depth, maxItems, disPrivate, false, prefix);
public bool DumpObjToFile(string path, object obj, int depth, int maxItems, bool disPrivate, bool fullClassName, string prefix = "")
{
path = DataStore.GetInstance().RemoveChars(path);
path = Path.Combine(Path.Combine(GetPublicFolder(), "Dumps"), path + ".dump");
if (path == null)
return false;
string result = String.Empty;
var settings = new DumpSettings();
settings.MaxDepth = depth;
settings.MaxItems = maxItems;
settings.DisplayPrivate = disPrivate;
settings.UseFullClassNames = fullClassName;
result = Dump.ToDump(obj, obj.GetType(), prefix, settings);
string dumpHeader =
"Object type: " + obj.GetType().ToString() + "\r\n" +
"TimeNow: " + DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString() + "\r\n" +
"Depth: " + depth.ToString() + "\r\n" +
"MaxItems: " + maxItems.ToString() + "\r\n" +
"ShowPrivate: " + disPrivate.ToString() + "\r\n" +
"UseFullClassName: " + fullClassName.ToString() + "\r\n\r\n";
File.AppendAllText(path, dumpHeader);
File.AppendAllText(path, result + "\r\n\r\n");
return true;
}
public string NormalizePath(string path) => path.Replace(@"\\", @"\").Replace("//", "/").Trim();
public string GetAbsoluteFilePath(string fileName) => Path.Combine(GetPublicFolder(), fileName);
public string GetPluginsFolder() => Path.Combine(GetPublicFolder(), "Plugins");
public virtual string GetPublicFolder() => Path.Combine(GetRootFolder(), "Pluton");
public string GetRootFolder() => Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)));
public string GetServerFolder() => Path.GetDirectoryName(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
public string GetManagedFolder() => Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
public void Initialize() { }
public float GetVectorsDistance(Vector3 v1, Vector3 v2) => Vector3.Distance(v1, v2);
public string[] GetQuotedArgs(string[] sArr)
{
bool inQuote = false;
string current = "";
var final = new List<string>();
foreach (string str in sArr) {
inQuote |= str.StartsWith("\"", StringComparison.CurrentCulture);
inQuote &= !str.EndsWith("\"", StringComparison.CurrentCulture);
if (inQuote) {
if (current != "")
current += " " + str;
if (current == "")
current = str;
}
if (!inQuote) {
if (current != "")
final.Add((current + " " + str).Replace("\"", ""));
if (current == "")
final.Add(str.Replace("\"", ""));
current = "";
}
}
return final.ToArray();
}
public static Hashtable HashtableFromFile(string path)
{
using (FileStream stream = new FileStream(path, FileMode.Open)) {
var formatter = new BinaryFormatter();
return (Hashtable)formatter.Deserialize(stream);
}
}
public static void HashtableToFile(Hashtable ht, string path)
{
using (FileStream stream = new FileStream(path, FileMode.Create)) {
var formatter = new BinaryFormatter();
formatter.Serialize(stream, ht);
}
}
public Vector3 Infront(Vector3 v3, float length) => v3 + Vector3.forward * length;
public bool IsNull(object obj) => obj == null;
public void Log(string str) => Logger.Log(str);
public Match Regex(string input, string match) => new Regex(input).Match(match);
public Quaternion RotateX(Quaternion q, float angle) => q *= Quaternion.Euler(angle, 0f, 0f);
public Quaternion RotateY(Quaternion q, float angle) => q *= Quaternion.Euler(0f, angle, 0f);
public Quaternion RotateZ(Quaternion q, float angle) => q *= Quaternion.Euler(0f, 0f, angle);
public bool TryFindType(string typeName, out Type t)
{
lock (typeCache) {
if (!typeCache.TryGetValue(typeName, out t)) {
foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies()) {
t = assembly.GetType(typeName);
if (t != null) {
break;
}
}
typeCache[typeName] = t;
}
}
return (t != null);
}
public Type TryFindReturnType(string typeName)
{
Type t;
if (TryFindType(typeName, out t))
return t;
throw new Exception("Type not found " + typeName);
}
}
}