DatabaseService and other fixes

This commit is contained in:
Alex
2025-01-07 10:45:40 +00:00
parent 3fcf9a0657
commit ffd3712ce5
6 changed files with 509 additions and 13 deletions
+49
View File
@@ -0,0 +1,49 @@
using Core.Annotations;
using Core.Models.Enums;
using Core.Models.Spt.Config;
using Core.Servers;
using Core.Services;
using ILogger = Core.Models.Utils.ILogger;
namespace Core.Utils;
[Injectable(InjectionType.Singleton)]
public class App
{
protected Dictionary<string, long> _onUpdateLastRun;
protected CoreConfig _coreConfig;
private ILogger _logger;
private TimeUtil _timeUtil;
private LocalisationService _localisationService;
private ConfigServer _configServer;
private EncodingUtil _encodingUtil;
private HttpServer _httpServer;
private DatabaseService _databaseService;
private IEnumerable<OnLoad> _onLoad;
private IEnumerable<OnUpdate> _onUpdate;
public App(
ILogger logger,
TimeUtil timeUtil,
LocalisationService localisationService,
ConfigServer configServer,
EncodingUtil encodingUtil,
HttpServer httpServer,
DatabaseService databaseService,
IEnumerable<OnLoad> onLoadComponents,
IEnumerable<OnUpdate> onUpdateComponents
) {
_logger = logger;
_timeUtil = timeUtil;
_localisationService = localisationService;
_configServer = configServer;
_encodingUtil = encodingUtil;
_httpServer = httpServer;
_databaseService = databaseService;
_onLoad = onLoadComponents;
_onUpdate = onUpdateComponents;
_coreConfig = configServer.GetConfig<CoreConfig>(ConfigTypes.CORE);
}
}
+64
View File
@@ -0,0 +1,64 @@
using System.Text;
using Core.Annotations;
namespace Core.Utils;
[Injectable(InjectionType.Singleton)]
public class EncodingUtil
{
public string Encode(string value, EncodeType encode)
{
return encode switch
{
EncodeType.BASE64 => Convert.ToBase64String(Encoding.Default.GetBytes(value)),
EncodeType.HEX => Convert.ToHexString(Encoding.Default.GetBytes(value)),
EncodeType.ASCII => Encoding.ASCII.GetString(Encoding.Default.GetBytes(value)),
EncodeType.UTF8 => Encoding.UTF8.GetString(Encoding.Default.GetBytes(value)),
_ => throw new ArgumentOutOfRangeException(nameof(encode), encode, null)
};
}
public string Decode(string value, EncodeType encode)
{
switch (encode)
{
case EncodeType.BASE64:
return Encoding.UTF8.GetString(Convert.FromBase64String(value));
case EncodeType.HEX:
return Encoding.UTF8.GetString(Convert.FromHexString(value));
case EncodeType.ASCII:
return Encoding.ASCII.GetString(Encoding.Default.GetBytes(value));
case EncodeType.UTF8:
return Encoding.UTF8.GetString(Encoding.Default.GetBytes(value));
default:
throw new ArgumentOutOfRangeException(nameof(encode), encode, null);
}
}
public string FromBase64(string value)
{
return Decode(value, EncodeType.BASE64);
}
public string ToBase64(string value)
{
return Encode(value, EncodeType.BASE64);
}
public string FromHex(string value)
{
return Decode(value, EncodeType.HEX);
}
public string ToHex(string value)
{
return Encode(value, EncodeType.HEX);
}
}
public enum EncodeType {
BASE64,
HEX,
ASCII,
UTF8
}
+11 -12
View File
@@ -6,13 +6,15 @@ using Core.Annotations;
namespace Core.Utils;
[Injectable(InjectionType.Singleton)]
public partial class HashUtil
public class HashUtil
{
private readonly Regex MongoIdRegex = new Regex("^[a-fA-F0-9]{24}$");
/// <summary>
/// Create a 24 character id using the sha256 algorithm + current timestamp
/// </summary>
/// <returns>24 character hash</returns>
public static string Generate()
public string Generate()
{
throw new NotImplementedException();
}
@@ -22,22 +24,22 @@ public partial class HashUtil
/// </summary>
/// <param name="stringToCheck">String to check</param>
/// <returns>True when string is a valid mongo id</returns>
public static bool IsValidMongoId(string stringToCheck)
public bool IsValidMongoId(string stringToCheck)
{
return MongoIdRegex().IsMatch(stringToCheck);
return MongoIdRegex.IsMatch(stringToCheck);
}
public static string GenerateMd5ForData(string data)
public string GenerateMd5ForData(string data)
{
return GenerateHashForData(HashingAlgorithm.MD5, data);
}
public static string GenerateSha1ForData(string data)
public string GenerateSha1ForData(string data)
{
return GenerateHashForData(HashingAlgorithm.SHA1, data);
}
public static string GenerateCrc32ForData(string data)
public string GenerateCrc32ForData(string data)
{
// TODO: Could not find a ms way of doing this.
// May need a custom impl to avoid an external lib. - CJ
@@ -51,7 +53,7 @@ public partial class HashUtil
/// <param name="data">data to be hashed</param>
/// <returns>hash value</returns>
/// <exception cref="NotImplementedException">thrown if the provided algorithm is not implemented</exception>>
public static string GenerateHashForData(HashingAlgorithm algorithm, string data)
public string GenerateHashForData(HashingAlgorithm algorithm, string data)
{
switch (algorithm)
{
@@ -71,7 +73,7 @@ public partial class HashUtil
/// Generates an account ID for a profile
/// </summary>
/// <returns>Generated account ID</returns>
public static int GenerateAccountId()
public int GenerateAccountId()
{
const int min = 1000000;
const int max = 1999999;
@@ -80,9 +82,6 @@ public partial class HashUtil
return random.Next() * (max - min + 1) + min;
}
[GeneratedRegex("^[a-fA-F0-9]{24}$", RegexOptions.IgnoreCase, "en")]
private static partial Regex MongoIdRegex();
}
public enum HashingAlgorithm