mirror of
https://github.com/islehorse/HISP.git
synced 2025-06-07 03:21:27 +12:00
Change Util.cs to Helper.cs, and move ThreadSafeList to HISP.Util namespace.
This commit is contained in:
parent
c36eda289a
commit
825d3b4740
37 changed files with 197 additions and 169 deletions
52
HorseIsleServer/LibHISP/Util/Helper.cs
Normal file
52
HorseIsleServer/LibHISP/Util/Helper.cs
Normal file
|
@ -0,0 +1,52 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace HISP.Util
|
||||
{
|
||||
public class Helper
|
||||
{
|
||||
// Thanks Stackoverflow (https://stackoverflow.com/questions/321370/how-can-i-convert-a-hex-string-to-a-byte-array)
|
||||
private static int getHexVal(char hex)
|
||||
{
|
||||
int val = (int)hex;
|
||||
return val - (val < 58 ? 48 : (val < 97 ? 55 : 87));
|
||||
}
|
||||
public static byte[] StringToByteArray(string hex)
|
||||
{
|
||||
if (hex.Length % 2 == 1)
|
||||
throw new ArgumentException("The binary key cannot have an odd number of digits");
|
||||
|
||||
byte[] arr = new byte[hex.Length >> 1];
|
||||
|
||||
for (int i = 0; i < hex.Length >> 1; ++i)
|
||||
{
|
||||
arr[i] = (byte)((getHexVal(hex[i << 1]) << 4) + (getHexVal(hex[(i << 1) + 1])));
|
||||
}
|
||||
|
||||
return arr;
|
||||
}
|
||||
|
||||
public static double PointsToDistance(int x1, int y1, int x2, int y2)
|
||||
{
|
||||
return Math.Sqrt(Math.Pow((x2 - x1), 2) + Math.Pow((y2 - y1), 2));
|
||||
}
|
||||
public static string CapitalizeFirstLetter(string str)
|
||||
{
|
||||
char firstChar = char.ToUpper(str[0]);
|
||||
return firstChar + str.Substring(1);
|
||||
}
|
||||
public static DateTime UnixTimeStampToDateTime(double unixTimeStamp)
|
||||
{
|
||||
// Unix timestamp is seconds past epoch
|
||||
System.DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
|
||||
dtDateTime = dtDateTime.AddSeconds(unixTimeStamp).ToUniversalTime();
|
||||
return dtDateTime;
|
||||
}
|
||||
|
||||
public static void ByteArrayToByteList(byte[] byteArray, List<byte> byteList)
|
||||
{
|
||||
byteList.AddRange(byteArray.ToList());
|
||||
}
|
||||
}
|
||||
}
|
67
HorseIsleServer/LibHISP/Util/ThreadSafeList.cs
Normal file
67
HorseIsleServer/LibHISP/Util/ThreadSafeList.cs
Normal file
|
@ -0,0 +1,67 @@
|
|||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
|
||||
namespace HISP.Util
|
||||
{
|
||||
public class ThreadSafeList<T> : List<T>
|
||||
{
|
||||
private Mutex listLock = new Mutex();
|
||||
new public void AddRange(IEnumerable<T> collection)
|
||||
{
|
||||
listLock.WaitOne();
|
||||
base.AddRange(collection);
|
||||
listLock.ReleaseMutex();
|
||||
}
|
||||
new public void Add(T value)
|
||||
{
|
||||
listLock.WaitOne();
|
||||
base.Add(value);
|
||||
listLock.ReleaseMutex();
|
||||
}
|
||||
new public void Clear()
|
||||
{
|
||||
listLock.WaitOne();
|
||||
base.Clear();
|
||||
listLock.ReleaseMutex();
|
||||
}
|
||||
|
||||
new public bool Contains(T value)
|
||||
{
|
||||
listLock.WaitOne();
|
||||
bool test = base.Contains(value);
|
||||
listLock.ReleaseMutex();
|
||||
|
||||
return test;
|
||||
}
|
||||
new public IEnumerator GetEnumerator()
|
||||
{
|
||||
listLock.WaitOne();
|
||||
IEnumerator res = base.ToArray().GetEnumerator();
|
||||
listLock.ReleaseMutex();
|
||||
return res;
|
||||
}
|
||||
|
||||
new public void Insert(int index, T value)
|
||||
{
|
||||
listLock.WaitOne();
|
||||
base.Insert(index, value);
|
||||
listLock.ReleaseMutex();
|
||||
}
|
||||
|
||||
new public void Remove(T value)
|
||||
{
|
||||
listLock.WaitOne();
|
||||
base.Remove(value);
|
||||
listLock.ReleaseMutex();
|
||||
}
|
||||
|
||||
new public void RemoveAt(int index)
|
||||
{
|
||||
listLock.WaitOne();
|
||||
base.RemoveAt(index);
|
||||
listLock.ReleaseMutex();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue