forked from Elfocrash/L2dotNET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtilz.cs
More file actions
203 lines (171 loc) · 7.2 KB
/
Copy pathUtilz.cs
File metadata and controls
203 lines (171 loc) · 7.2 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
using L2dotNET.Network;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Text;
using NLog;
namespace L2dotNET.Utility
{
public static class Utilz
{
private static readonly Logger Log = LogManager.GetCurrentClassLogger();
public static string CurrentTime => DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss");
public static TimeSpan ProcessUptime => DateTime.Now.Subtract(Process.GetCurrentProcess().StartTime);
public static string SystemSummary()
{
StringBuilder strBuilder = new StringBuilder();
strBuilder.Append($"Date: {CurrentTime}\r\n");
strBuilder.Append($"OS: {Environment.OSVersion}\r\n");
strBuilder.Append($"Environment version: {Environment.Version}\r\n");
strBuilder.Append($"Processors count: {Environment.ProcessorCount}\r\n");
strBuilder.Append($"Working set: {Environment.WorkingSet} bytes\r\n");
strBuilder.Append($"Domain name: {AppDomain.CurrentDomain.FriendlyName}\r\n");
strBuilder.Append($"Service Uptime: {ProcessUptime}\r\n");
strBuilder.Append(Environment.NewLine);
return strBuilder.ToString();
}
public static double DistanceSq(double x1, double y1, double x2, double y2)
{
return Math.Pow(x2 - x1, 2) + Math.Pow(y2 - y1, 2);
}
public static double Distance(double x1, double y1, double x2, double y2)
{
return Math.Sqrt(DistanceSq(x1, y1, x2, y2));
}
public static double LengthSq(double x, double y)
{
return Math.Pow(x, 2) + Math.Pow(y, 2);
}
public static double Length(double x, double y)
{
return Math.Sqrt(LengthSq(x, y));
}
public static IEnumerable<Type> GetTypesInNamespace(Assembly assembly, string nameSpace)
{
return assembly.GetTypes().Where(t => string.Equals(t.Namespace, nameSpace, StringComparison.Ordinal) && t.BaseType != typeof(object)).ToArray();
}
public static T GetEnumFromString<T>(string value, T defaultValue) where T : struct, IConvertible
{
if (!typeof(T).IsEnum)
throw new ArgumentException("T must be an enumerated type");
if (string.IsNullOrEmpty(value))
return defaultValue;
foreach (T item in Enum.GetValues(typeof(T)).Cast<T>().Where(item => item.ToString(CultureInfo.InvariantCulture).ToLower().Equals(value.Trim().ToLower())))
return item;
return defaultValue;
}
private static readonly DateTime Jan1st1970 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
public static long CurrentTimeMillis()
{
return (long)(DateTime.UtcNow - Jan1st1970).TotalMilliseconds;
}
/// <summary>
/// String extension method in order
/// to check if two strings match no matter the case
/// </summary>
/// <param name="str"></param>
/// <param name="stringToCompare"></param>
/// <returns></returns>
public static bool EqualsIgnoreCase(this string str, string stringToCompare)
{
return str.Equals(stringToCompare, StringComparison.InvariantCultureIgnoreCase);
}
public static Packet ToPacket(this byte[] byteArray, int extraBytes = 0)
{
return new Packet(1 + extraBytes, byteArray);
}
/// <summary>
/// String extension method in order
/// to check if two strings match with the exact the case
/// </summary>
/// <param name="str"></param>
/// <param name="stringToCompare"></param>
/// <returns></returns>
public static bool EqualsMatchCase(this string str, string stringToCompare)
{
return str.Equals(stringToCompare, StringComparison.InvariantCulture);
}
/// <summary>
/// </summary>
/// <param name="str"></param>
/// <param name="stringToCompare"></param>
/// <returns></returns>
public static bool StartsWithIgnoreCase(this string str, string stringToCompare)
{
return str.StartsWith(stringToCompare, StringComparison.InvariantCultureIgnoreCase);
}
/// <summary>
/// </summary>
/// <param name="str"></param>
/// <param name="stringToCompare"></param>
/// <returns></returns>
public static bool StartsWithMatchCase(this string str, string stringToCompare)
{
return str.StartsWith(stringToCompare, StringComparison.InvariantCulture);
}
public static bool EndsWithIgnoreCase(this string str, string stringToCompare)
{
return str.EndsWith(stringToCompare, StringComparison.InvariantCultureIgnoreCase);
}
public static bool EndsWithMatchCase(this string str, string stringToCompare)
{
return str.EndsWith(stringToCompare, StringComparison.InvariantCulture);
}
public static SortedList<TKey, TValue> ToSortedList<TSource, TKey, TValue>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TValue> valueSelector)
{
SortedList<TKey, TValue> ret = new SortedList<TKey, TValue>();
foreach (TSource item in source)
ret.Add(keySelector(item), valueSelector(item));
return ret;
}
public static string GetTypeLower<T>(T obj)
{
Type t;
if (obj == null)
t = typeof(T);
else
t = obj.GetType();
return t.Name.ToLower();
}
public static bool IsLocalIpAddress(string host)
{
try
{
// get host IP addresses
IPAddress[] hostIPs = Dns.GetHostAddresses(host);
// get local IP addresses
IPAddress[] localIPs = Dns.GetHostAddresses(Dns.GetHostName());
// test if any host IP equals to any local IP or to localhost
foreach (IPAddress hostIp in hostIPs)
{
// is localhost
if (IPAddress.IsLoopback(hostIp))
return true;
// is local address
if (localIPs.Contains(hostIp))
return true;
}
}
catch (Exception ex)
{
Log.Error(ex.Message);
}
return false;
}
public static bool IsLocalIpAddress(this EndPoint host)
{
return IsLocalIpAddress(((IPEndPoint)host).Address.ToString());
}
public static string GetDescription(this Enum value)
{
FieldInfo fi = value.GetType().GetField(value.ToString());
DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
return attributes.Length > 0 ? attributes[0].Description : value.ToString();
}
}
}