-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSharpUtils.cs
More file actions
217 lines (191 loc) · 6.33 KB
/
Copy pathSharpUtils.cs
File metadata and controls
217 lines (191 loc) · 6.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Random = UnityEngine.Random;
namespace OrbItUtils
{
public static partial class Utils
{
private static HashSet<String> UniqueStringSet;
public static bool AsBool(this int i)
{
return i != 0;
}
public static T GetCustomAttribute<T>(this MemberInfo mInfo) where T : Attribute
{
var infos = mInfo.GetCustomAttributes(typeof (T), false);
if (infos.Length > 0)
{
return (T) infos.ElementAt(0);
}
return null;
}
public static TValue GetOrAdd<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key,
Func<TValue> valueCreator)
{
TValue value;
if (!dictionary.TryGetValue(key, out value))
{
value = valueCreator();
dictionary.Add(key, value);
}
return value;
}
public static TValue GetOrAdd<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key)
where TValue : new()
{
return dictionary.GetOrAdd(key, () => new TValue());
}
public static bool In<T>(this T x, params T[] args) where T : struct, IConvertible
{
return args.Contains(x);
}
public static bool IsGenericType(Type genericType, Type type)
{
return type.IsGenericType && type.GetGenericTypeDefinition() == genericType;
}
public static string LastWord(this string s, char delim)
{
return s.Substring(s.LastIndexOf(delim) + 1);
}
public static string GetSimpleName(this Type t)
{
return t.ToString().LastWord('.');
}
public static object ParsePrimitive(Type primitiveType, String value)
{
string s = value.Trim();
if (primitiveType == typeof (int))
{
int v;
if (Int32.TryParse(s, out v))
{
//fpinfo.SetValue(v, parentItem.obj);
return v;
}
return null;
}
if (primitiveType == typeof (float))
{
float v;
if (float.TryParse(s, out v))
{
//fpinfo.SetValue(v, parentItem.obj);
return v;
}
return null;
}
if (primitiveType == typeof (double))
{
double v;
if (double.TryParse(s, out v))
{
//fpinfo.SetValue(v, parentItem.obj);
return v;
}
return null;
}
if (primitiveType == typeof (byte))
{
byte v;
if (byte.TryParse(s, out v))
{
//fpinfo.SetValue(v, parentItem.obj);
return v;
}
return null;
}
if (primitiveType == typeof (bool))
{
bool v;
if (bool.TryParse(s, out v))
{
//fpinfo.SetValue(v, parentItem.obj);
return v;
}
return null;
}
if (primitiveType.IsEnum)
{
return Enum.GetValues(primitiveType).Cast<object>().FirstOrDefault(
val => val.ToString().ToLower().Equals(s.ToLower()));
}
if (primitiveType == typeof (string))
{
return s;
}
return null;
}
public static string RandomName(List<Type> seedTypes, int tries = 0)
{
int depth = Random.Range(0, seedTypes.Count);
Type t = seedTypes.ElementAt(depth);
var props = t.GetProperties();
int i = Random.Range(0, props.Length);
var pinfo = props.ElementAt(i);
if (tries < 10 && t.GetProperty(pinfo.Name) != null)
{
return RandomName(seedTypes, ++tries);
}
return pinfo.Name;
}
public static string RandomString()
{
Guid g = Guid.NewGuid();
string guidString = Convert.ToBase64String(g.ToByteArray());
guidString = guidString.Replace("=", "");
guidString = guidString.Replace("+", "");
return guidString;
}
public static float SmootherStep(float start, float end, float t)
{
float affection = end - start;
t = t*t*t*(t*(6f*t - 15f) + 10f);
affection *= t;
return start + affection;
}
//thanks, skeet!
public static HashSet<T> ToHashSet<T>(this IEnumerable<T> source)
{
return new HashSet<T>(source);
}
public static int ToInt(this bool b)
{
return b ? 1 : 0;
}
public static string UniqueString()
{
string guidString = RandomString();
UniqueStringSet = UniqueStringSet ?? new HashSet<string>();
while (UniqueStringSet.Contains(guidString))
{
guidString = RandomString();
}
return guidString;
}
public static string UppercaseFirst(this string s)
{
// Check for empty string.
if (string.IsNullOrEmpty(s))
{
return string.Empty;
}
// Return char and concat substring.
return char.ToUpper(s[0]) + s.Substring(1);
}
public static string WordWrap(this string message, int maxCharsPerLine)
{
int chars = maxCharsPerLine;
for (int i = 1; i <= 4; i++)
if (message.Length > chars*i)
for (int j = chars*i; j > (chars*i) - chars; j--)
if (message.ElementAt(j).Equals(' ') || message.ElementAt(j).Equals('/'))
{
message = message.Insert(j + 1, "\n");
break;
}
return message;
}
} // end of class.
}