migrating to .NET Core 2.0
This commit is contained in:
62
SharedLibraryCore/Helpers/AsyncStatus.cs
Normal file
62
SharedLibraryCore/Helpers/AsyncStatus.cs
Normal file
@ -0,0 +1,62 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SharedLibraryCore.Helpers
|
||||
{
|
||||
public sealed class AsyncStatus
|
||||
{
|
||||
DateTime StartTime;
|
||||
int TimesRun;
|
||||
int UpdateFrequency;
|
||||
public double RunAverage { get; private set; }
|
||||
public object Dependant { get; private set; }
|
||||
public Task RequestedTask { get; private set; }
|
||||
public CancellationTokenSource TokenSrc { get; private set; }
|
||||
|
||||
public AsyncStatus(object dependant, int frequency)
|
||||
{
|
||||
TokenSrc = new CancellationTokenSource();
|
||||
StartTime = DateTime.Now;
|
||||
Dependant = dependant;
|
||||
UpdateFrequency = frequency;
|
||||
// technically 0 but it's faster than checking for division by 0
|
||||
TimesRun = 1;
|
||||
}
|
||||
|
||||
public CancellationToken GetToken()
|
||||
{
|
||||
return TokenSrc.Token;
|
||||
}
|
||||
|
||||
public double ElapsedMillisecondsTime()
|
||||
{
|
||||
return (DateTime.Now - StartTime).TotalMilliseconds;
|
||||
}
|
||||
|
||||
public void Update(Task<bool> T)
|
||||
{
|
||||
// reset the token source
|
||||
TokenSrc.Dispose();
|
||||
TokenSrc = new CancellationTokenSource();
|
||||
|
||||
RequestedTask = T;
|
||||
// Console.WriteLine($"Starting Task {T.Id} ");
|
||||
RequestedTask.Start();
|
||||
|
||||
if (TimesRun > 25)
|
||||
TimesRun = 1;
|
||||
|
||||
RunAverage = RunAverage + ((DateTime.Now - StartTime).TotalMilliseconds - RunAverage - UpdateFrequency) / TimesRun;
|
||||
StartTime = DateTime.Now;
|
||||
}
|
||||
|
||||
public void Abort()
|
||||
{
|
||||
RequestedTask = null;
|
||||
}
|
||||
}
|
||||
}
|
53
SharedLibraryCore/Helpers/BaseConfigurationHandler.cs
Normal file
53
SharedLibraryCore/Helpers/BaseConfigurationHandler.cs
Normal file
@ -0,0 +1,53 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Newtonsoft.Json;
|
||||
using SharedLibraryCore.Interfaces;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SharedLibraryCore.Configuration
|
||||
{
|
||||
public class BaseConfigurationHandler<T> : IConfigurationHandler<T> where T : IBaseConfiguration
|
||||
{
|
||||
string Filename;
|
||||
IConfigurationRoot ConfigurationRoot { get; set; }
|
||||
T _configuration;
|
||||
|
||||
public BaseConfigurationHandler(string fn)
|
||||
{
|
||||
Filename = fn;
|
||||
Build();
|
||||
}
|
||||
|
||||
public void Build()
|
||||
{
|
||||
ConfigurationRoot = new ConfigurationBuilder()
|
||||
.AddJsonFile($"{AppDomain.CurrentDomain.BaseDirectory}{Filename}.json", true)
|
||||
.Build();
|
||||
|
||||
_configuration = ConfigurationRoot.Get<T>();
|
||||
|
||||
if (_configuration == null)
|
||||
_configuration = default(T);
|
||||
}
|
||||
|
||||
public Task Save()
|
||||
{
|
||||
var appConfigJSON = JsonConvert.SerializeObject(_configuration, Formatting.Indented);
|
||||
return Task.Factory.StartNew(() =>
|
||||
{
|
||||
File.WriteAllText($"{AppDomain.CurrentDomain.BaseDirectory}{Filename}.json", appConfigJSON);
|
||||
});
|
||||
}
|
||||
|
||||
public T Configuration() => _configuration;
|
||||
|
||||
public void Set(T config)
|
||||
{
|
||||
_configuration = config;
|
||||
}
|
||||
}
|
||||
}
|
42
SharedLibraryCore/Helpers/Hashing.cs
Normal file
42
SharedLibraryCore/Helpers/Hashing.cs
Normal file
@ -0,0 +1,42 @@
|
||||
using SimpleCrypto;
|
||||
|
||||
namespace SharedLibraryCore.Helpers
|
||||
{
|
||||
public class Hashing
|
||||
{
|
||||
/// <summary>
|
||||
/// Generate password hash and salt
|
||||
/// </summary>
|
||||
/// <param name="password">plaintext password</param>
|
||||
/// <returns></returns>
|
||||
public static string[] Hash(string password, string saltStr = null)
|
||||
{
|
||||
|
||||
string hash;
|
||||
string salt;
|
||||
var CryptoSvc = new PBKDF2();
|
||||
|
||||
// generate new hash
|
||||
if (saltStr == null)
|
||||
{
|
||||
hash = CryptoSvc.Compute(password);
|
||||
salt = CryptoSvc.Salt;
|
||||
return new string[]
|
||||
{
|
||||
hash,
|
||||
salt
|
||||
};
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
hash = CryptoSvc.Compute(password, saltStr);
|
||||
return new string[]
|
||||
{
|
||||
hash,
|
||||
""
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
20
SharedLibraryCore/Helpers/MessageToken.cs
Normal file
20
SharedLibraryCore/Helpers/MessageToken.cs
Normal file
@ -0,0 +1,20 @@
|
||||
using System;
|
||||
|
||||
namespace SharedLibraryCore.Helpers
|
||||
{
|
||||
public class MessageToken
|
||||
{
|
||||
public string Name { get; private set; }
|
||||
Func<string> Value;
|
||||
public MessageToken(string Name, Func<string> Value)
|
||||
{
|
||||
this.Name = Name;
|
||||
this.Value = Value;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return Value().ToString();
|
||||
}
|
||||
}
|
||||
}
|
24
SharedLibraryCore/Helpers/ParseEnum.cs
Normal file
24
SharedLibraryCore/Helpers/ParseEnum.cs
Normal file
@ -0,0 +1,24 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SharedLibraryCore.Helpers
|
||||
{
|
||||
public class ParseEnum<T>
|
||||
{
|
||||
public static T Get(string e, Type type)
|
||||
{
|
||||
try
|
||||
{
|
||||
return (T)Enum.Parse(type, e);
|
||||
}
|
||||
|
||||
catch (Exception)
|
||||
{
|
||||
return (T)(Enum.GetValues(type).GetValue(0));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
51
SharedLibraryCore/Helpers/PlayerHistory.cs
Normal file
51
SharedLibraryCore/Helpers/PlayerHistory.cs
Normal file
@ -0,0 +1,51 @@
|
||||
using System;
|
||||
|
||||
namespace SharedLibraryCore.Helpers
|
||||
{
|
||||
public class PlayerHistory
|
||||
{
|
||||
// how many minutes between updates
|
||||
public static readonly int UpdateInterval = 5;
|
||||
|
||||
public PlayerHistory(int cNum)
|
||||
{
|
||||
DateTime t = DateTime.UtcNow;
|
||||
When = new DateTime(t.Year, t.Month, t.Day, t.Hour, Math.Min(59, UpdateInterval * (int)Math.Round(t.Minute / (float)UpdateInterval)), 0);
|
||||
PlayerCount = cNum;
|
||||
}
|
||||
|
||||
#if DEBUG
|
||||
public PlayerHistory(DateTime t, int cNum)
|
||||
{
|
||||
When = new DateTime(t.Year, t.Month, t.Day, t.Hour, Math.Min(59, UpdateInterval * (int)Math.Round(t.Minute / (float)UpdateInterval)), 0);
|
||||
PlayerCount = cNum;
|
||||
}
|
||||
#endif
|
||||
|
||||
private DateTime When;
|
||||
private int PlayerCount;
|
||||
|
||||
/// <summary>
|
||||
/// Used by CanvasJS as a point on the x axis
|
||||
/// </summary>
|
||||
public string x
|
||||
{
|
||||
get
|
||||
{
|
||||
return When.ToString("yyyy-MM-ddTHH:mm:ssZ");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Used by CanvasJS as a point on the y axis
|
||||
/// </summary>
|
||||
public int y
|
||||
{
|
||||
get
|
||||
{
|
||||
return PlayerCount;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
48
SharedLibraryCore/Helpers/ThreadSafe.cs
Normal file
48
SharedLibraryCore/Helpers/ThreadSafe.cs
Normal file
@ -0,0 +1,48 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SharedLibraryCore.Helpers
|
||||
{
|
||||
/// <summary>
|
||||
/// Excuse this monstrosity
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public class ThreadSafe<T>
|
||||
{
|
||||
private bool _lock;
|
||||
private T instance;
|
||||
|
||||
public ThreadSafe(T instance)
|
||||
{
|
||||
this.instance = instance;
|
||||
_lock = true;
|
||||
}
|
||||
|
||||
public T Value
|
||||
{
|
||||
get
|
||||
{
|
||||
// shush
|
||||
if (_lock)
|
||||
return Value;
|
||||
_lock = true;
|
||||
return instance;
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
if (_lock)
|
||||
{
|
||||
Value = Value;
|
||||
return;
|
||||
}
|
||||
instance = Value;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
53
SharedLibraryCore/Helpers/Vector3.cs
Normal file
53
SharedLibraryCore/Helpers/Vector3.cs
Normal file
@ -0,0 +1,53 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SharedLibraryCore.Helpers
|
||||
{
|
||||
public class Vector3
|
||||
{
|
||||
public float X { get; protected set; }
|
||||
public float Y { get; protected set; }
|
||||
public float Z { get; protected set; }
|
||||
|
||||
public Vector3(float x, float y, float z)
|
||||
{
|
||||
X = x;
|
||||
Y = y;
|
||||
Z = z;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"({X}, {Y}, {Z})";
|
||||
}
|
||||
|
||||
public static Vector3 Parse(string s)
|
||||
{
|
||||
bool valid = Regex.Match(s, @"\(-?[0-9]+.?[0-9]*,\ -?[0-9]+.?[0-9]*,\ -?[0-9]+.?[0-9]*\)").Success;
|
||||
if (!valid)
|
||||
throw new FormatException("Vector3 is not in correct format");
|
||||
|
||||
string removeParenthesis = s.Substring(1, s.Length - 2);
|
||||
string[] eachPoint = removeParenthesis.Split(',');
|
||||
return new Vector3(float.Parse(eachPoint[0]), float.Parse(eachPoint[1]), float.Parse(eachPoint[2]));
|
||||
}
|
||||
|
||||
public static double Distance(Vector3 a, Vector3 b)
|
||||
{
|
||||
return Math.Sqrt(Math.Pow(b.X - a.X, 2) + Math.Pow(b.Y - a.Y, 2) + Math.Pow(b.Z - a.Z, 2));
|
||||
}
|
||||
|
||||
public static Vector3 Subtract(Vector3 a, Vector3 b) => new Vector3(b.X - a.X, b.Y - a.Y, b.Z - a.Z);
|
||||
|
||||
public double DotProduct(Vector3 a) => (a.X * this.X) + (a.Y * this.Y) + (a.Z * this.Z);
|
||||
|
||||
public double Magnitude() => Math.Sqrt((X * X) + (Y * Y) + (Z * Z));
|
||||
|
||||
public double AngleBetween(Vector3 a) => Math.Acos(this.DotProduct(a) / (a.Magnitude() * this.Magnitude()));
|
||||
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user