more standard stuff to abstrractions

This commit is contained in:
Kayori
2025-09-20 16:54:42 +02:00
parent 1106d25933
commit f26baf35a4
8 changed files with 94 additions and 35 deletions
+38
View File
@@ -0,0 +1,38 @@
using System;
using System.Threading.Tasks;
namespace Abstractions.Handlers;
public abstract class ResponseHandler<TResponse>
{
public string HandlerModule { get; set; } = "";
public string HandlerMethod { get; set; } = "";
public string? HandlerGameCode { get; set; }
public string? HandlerMinVer { get; set; }
public string? HandlerMaxVer { get; set; }
public virtual void Configure() { throw new NotImplementedException("Configure method not implemented."); }
protected void Module(string module)
{
this.HandlerModule = module;
}
protected void Method(string method)
{
this.HandlerMethod = method;
}
protected void GameCode(string gameCode)
{
this.HandlerGameCode = gameCode;
}
protected void MinVer(string minVer)
{
this.HandlerMinVer = minVer;
}
public virtual TResponse Handle(string model) { throw new NotImplementedException("Handle method not implemented."); }
public virtual Task<TResponse> HandleAsync(string model) { throw new NotImplementedException("Handle method not implemented."); }
}
@@ -0,0 +1,12 @@
using System.Xml.Serialization;
namespace Abstractions.SerializationTypes;
public struct XrpcInt(int value)
{
[XmlAttribute("__type")]
public string Type { get; set; } = "s32";
[XmlText]
public int Value { get; set; } = value;
}
@@ -0,0 +1,12 @@
using System.Xml.Serialization;
namespace Abstractions.SerializationTypes;
public struct XrpcString(string value)
{
[XmlAttribute("__type")]
public string Type { get; set; } = "str";
[XmlText]
public string Value { get; set; } = value;
}
@@ -65,7 +65,7 @@ namespace Server.Handlers.Boot
"slobby", "sglobby"
];
foreach(string service in kfcServices)
foreach(var service in kfcServices)
services.Add(new XElement("item", new XAttribute("name", service), new XAttribute("url", sdvxurl)));
}
@@ -77,7 +77,7 @@ namespace Server.Handlers.Boot
"slobby", "sglobby"
];
foreach(string service in mdxServices)
foreach(var service in mdxServices)
services.Add(new XElement("item", new XAttribute("name", service), new XAttribute("url", mdxurl)));
}
@@ -89,7 +89,7 @@ namespace Server.Handlers.Boot
"slobby", "sglobby"
];
foreach(string service in m39Services)
foreach(var service in m39Services)
services.Add(new XElement("item", new XAttribute("name", service), new XAttribute("url", m39url)));
}
@@ -17,7 +17,7 @@ namespace Server.Handlers.Common
var itemElement = rootCallElement?.Element("pcbevent")?.Element("item");
var timeElement = itemElement?.Element("time");
string time = timeElement is not null ? UnixTimeStampToDateTime(double.Parse(timeElement.Value)).ToString() : "";
var time = timeElement is not null ? UnixTimeStampToDateTime(double.Parse(timeElement.Value)).ToString() : "";
_logger.LogInformation("PCBEvent: tag={Tag} srcid={SrcId} model={Model} name={Name} time={Time}",
rootCallElement?.Attribute("tag")?.Value ?? "",
@@ -38,7 +38,7 @@ namespace Server.Handlers.Common
public static DateTime UnixTimeStampToDateTime(double unixTimeStamp)
{
// Unix timestamp is seconds past epoch
DateTime dateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
var dateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
dateTime = dateTime.AddSeconds(unixTimeStamp).ToLocalTime();
return dateTime;
}
+11 -14
View File
@@ -9,7 +9,7 @@ namespace Server.Services;
public class CardService : ICardService
{
private const string Alphabet = "0123456789ABCDEFGHJKLMNPRSTUWXYZ";
private readonly byte[] RawKey = Encoding.ASCII.GetBytes("?I'llB2c.YouXXXeMeHaYpy!");
private readonly byte[] _rawKey = "?I'llB2c.YouXXXeMeHaYpy!"u8.ToArray();
private readonly DesEncryption _desEncryption;
private readonly ILogger<CardService> _logger;
@@ -17,7 +17,7 @@ public class CardService : ICardService
public CardService(ILogger<CardService> logger, AppDbContext context)
{
var encryptionKey = RawKey.Select(x => (byte)(x * 2)).ToArray();
var encryptionKey = _rawKey.Select(x => (byte)(x * 2)).ToArray();
_desEncryption = new DesEncryption(encryptionKey);
_logger = logger;
_context = context;
@@ -48,7 +48,7 @@ public class CardService : ICardService
return string.Empty;
}
for(int i = 13; i > 0; i--)
for(var i = 13; i > 0; i--)
{
card[i] ^= card[i - 1];
}
@@ -58,15 +58,12 @@ public class CardService : ICardService
var packed = Pack5(card.Take(13).ToArray()).Take(8).ToArray();
var decrypted = _desEncryption.Decrypt(packed).Reverse().ToArray();
string cardId = BitConverter.ToString(decrypted).Replace("-", "").ToUpper();
var cardId = Convert.ToHexString(decrypted).ToUpper();
if(cardType == 1 && !cardId.StartsWith("E004") || cardType == 2 && cardId[0] != '0')
{
_logger.LogError("Invalid card `type");
return string.Empty;
}
if ((cardType != 1 || cardId.StartsWith("E004")) && (cardType != 2 || cardId[0] == '0')) return cardId;
_logger.LogError("Invalid card `type");
return string.Empty;
return cardId;
}
public string ConvertUidToKonamiId(string uid)
@@ -77,7 +74,7 @@ public class CardService : ICardService
return string.Empty;
}
int cardType = uid.StartsWith("E004") ? 1 : uid[0] == '0' ? 2 : -1;
var cardType = uid.StartsWith("E004") ? 1 : uid[0] == '0' ? 2 : -1;
if(cardType == -1)
{
@@ -106,7 +103,7 @@ public class CardService : ICardService
}
var unpackedId = Unpack5(encryptedId);
unpackedId = unpackedId.Take(13).Concat(new byte[] { 0, 0, 0 }).ToArray();
unpackedId = unpackedId.Take(13).Concat("\0\0\0"u8.ToArray()).ToArray();
if(unpackedId.Length != 16)
{
@@ -123,7 +120,7 @@ public class CardService : ICardService
}
unpackedId[14] = (byte)cardType;
unpackedId[15] = (byte)CalculateChecksum(unpackedId);
unpackedId[15] = CalculateChecksum(unpackedId);
return string.Concat(unpackedId.Select(u => Alphabet[u]));
}
@@ -158,7 +155,7 @@ public class CardService : ICardService
private static byte[] Pack5(byte[] data)
{
var packed = new StringBuilder();
foreach(byte b in data)
foreach(var b in data)
{
packed.Append(Convert.ToString(b, 2).PadLeft(5, '0'));
}
+10 -10
View File
@@ -5,15 +5,15 @@ public class LZ77
public static byte[] Decompress(byte[] data)
{
// init a buffer, size comes from eamuemu
byte[] res = new byte[0x190000];
var res = new byte[0x190000];
// current data location
int p = 0;
var p = 0;
// current output location
int r = 0;
var r = 0;
// traceback location
int t = 0;
var t = 0;
// bitmask location
int b = 8; // read next bitmask byte on start
var b = 8; // read next bitmask byte on start
byte mask = 0;
while(true)
@@ -50,7 +50,7 @@ public class LZ77
count = (count & 0x0F) + 3;
// copy earlier result bytes to the end
t = r - distance; // initialize traceback location
for(int i = 0; i < count; i++)
for(var i = 0; i < count; i++)
{
res[r] = t < 0 ? (byte)0x00 : res[t];
r += 1;
@@ -62,16 +62,16 @@ public class LZ77
b += 1;
}
// r = result length
byte[] output = new byte[r];
var output = new byte[r];
Array.Copy(res, output, r);
return output;
}
public static byte[] CompressEmpty(byte[] data)
{
byte[] res = new byte[data.Length + data.Length / 8 + 3];
int p = 0;
for(int i = 0; i < data.Length; i++)
var res = new byte[data.Length + data.Length / 8 + 3];
var p = 0;
for(var i = 0; i < data.Length; i++)
{
if(i % 8 == 0)
{
+6 -6
View File
@@ -6,14 +6,14 @@ namespace Server.Utils
{
public static string Encrypt(string key, string data)
{
Encoding unicode = Encoding.Unicode;
var unicode = Encoding.Unicode;
return Convert.ToBase64String(Encrypt(unicode.GetBytes(key), unicode.GetBytes(data)));
}
public static string Decrypt(string key, string data)
{
Encoding unicode = Encoding.Unicode;
var unicode = Encoding.Unicode;
return unicode.GetString(Encrypt(unicode.GetBytes(key), Convert.FromBase64String(data)));
}
@@ -30,7 +30,7 @@ namespace Server.Utils
private static byte[] InitializeKeyStream(byte[] key)
{
byte[] keyStream = Enumerable.Range(0, 256)
var keyStream = Enumerable.Range(0, 256)
.Select(i => (byte)i)
.ToArray();
@@ -46,10 +46,10 @@ namespace Server.Utils
private static IEnumerable<byte> EncryptOutput(byte[] key, IEnumerable<byte> data)
{
byte[] keyStream = InitializeKeyStream(key);
var keyStream = InitializeKeyStream(key);
int keyStreamIndex = 0;
int keyIndex = 0;
var keyStreamIndex = 0;
var keyIndex = 0;
return data.Select((dataByte) =>
{