SCHALE.GameServer/SCHALE.Common/Crypto/TableService.cs

116 lines
4.8 KiB
C#
Raw Normal View History

2024-04-29 15:52:45 +00:00
using Google.FlatBuffers;
2024-12-31 02:14:06 +00:00
using System.Data.SQLite;
2024-04-29 15:52:45 +00:00
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
2024-04-21 00:21:57 +00:00
using SCHALE.Common.Crypto.XXHash;
2024-04-29 15:52:45 +00:00
using SCHALE.Common.FlatData;
using System.Reflection;
2024-04-21 00:21:57 +00:00
using System.Text;
2024-12-31 02:14:06 +00:00
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;
2024-04-21 00:21:57 +00:00
namespace SCHALE.Common.Crypto
{
public static class TableService
{
/// <summary>
/// General password gen by file name, encode to base64 for zips password
/// </summary>
/// <param name="key"></param>
/// <param name="length"></param>
/// <returns></returns>
public static byte[] CreatePassword(string key, int length = 20)
{
byte[] password = GC.AllocateUninitializedArray<byte>((int)Math.Round((decimal)(length / 4 * 3)));
using var xxhash = XXHash32.Create();
xxhash.ComputeHash(Encoding.UTF8.GetBytes(key));
2024-04-29 15:52:45 +00:00
var mt = new MersenneTwister((int)xxhash.HashUInt32);
2024-04-21 00:21:57 +00:00
int i = 0;
while (i < password.Length)
{
2024-04-29 15:52:45 +00:00
Array.Copy(BitConverter.GetBytes(mt.Next()), 0, password, i, Math.Min(4, password.Length - i));
2024-04-21 00:21:57 +00:00
i += 4;
}
return password;
}
2024-04-29 15:52:45 +00:00
#if DEBUG
2024-12-31 02:14:06 +00:00
public static List<object> GetExcelList(Type type, string exceldbDir, string schema)
{
var excelList = new List<object>();
using (var dbConnection = new SQLiteConnection($"Data Source = {exceldbDir}"))
{
dbConnection.Open();
var command = dbConnection.CreateCommand();
command.CommandText = $"SELECT Bytes FROM {schema}";
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
excelList.Add(type.GetMethod($"GetRootAs{type.Name}", BindingFlags.Static | BindingFlags.Public, [typeof(ByteBuffer)])!
.Invoke(null, [new ByteBuffer((byte[])reader[0])]));
}
}
}
return excelList;
}
public static void DumpExcelDB(string exceldbDir, string destDir)
{
Directory.CreateDirectory(destDir);
using (var dbConnection = new SQLiteConnection($"Data Source = {exceldbDir}"))
{
dbConnection.Open();
string query = "SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%';";
using (var command = new SQLiteCommand(query, dbConnection))
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
string dbSchemaName = reader.GetString(0);
string excelName = dbSchemaName.Replace("DBSchema", "Excel");
var type = Assembly.GetAssembly(typeof(AcademyFavorScheduleExcelTable))!.GetTypes().Where(t => t.IsAssignableTo(typeof(IFlatbufferObject)) && (t.Name.Equals(excelName))).FirstOrDefault();
var list = GetExcelList(type, exceldbDir, dbSchemaName);
File.WriteAllText(Path.Join(destDir, $"{type.Name}.json"), JsonConvert.SerializeObject(list, Formatting.Indented, new StringEnumConverter()));
Console.WriteLine($"Dumped {type.Name} from {dbSchemaName} successfully");
}
}
}
}
2024-04-29 15:52:45 +00:00
public static void DumpExcels(string bytesDir, string destDir)
{
2024-12-31 02:14:06 +00:00
Directory.CreateDirectory(destDir);
2024-04-29 15:52:45 +00:00
foreach (var type in Assembly.GetAssembly(typeof(AcademyFavorScheduleExcelTable))!.GetTypes().Where(t => t.IsAssignableTo(typeof(IFlatbufferObject)) && t.Name.EndsWith("ExcelTable")))
{
var bytesFilePath = Path.Join(bytesDir, $"{type.Name}.bytes");
2024-04-29 15:52:45 +00:00
if (!File.Exists(bytesFilePath))
{
Console.WriteLine($"bytes files for {type.Name} not found. skipping...");
continue;
}
var bytes = File.ReadAllBytes(bytesFilePath);
TableEncryptionService.XOR(type.Name, bytes);
var inst = type.GetMethod($"GetRootAs{type.Name}", BindingFlags.Static | BindingFlags.Public, [typeof(ByteBuffer)])!.Invoke(null, [new ByteBuffer(bytes)]);
2024-12-31 02:14:06 +00:00
var obj = type.GetMethod("UnPack", BindingFlags.Instance | BindingFlags.Public)!.Invoke(inst, null);
File.WriteAllText(Path.Join(destDir, $"{type.Name}.json"), JsonConvert.SerializeObject(obj, Formatting.Indented, new StringEnumConverter()));
Console.WriteLine($"Dumped {type.Name} successfully");
2024-04-29 15:52:45 +00:00
}
}
#endif
2024-04-21 00:21:57 +00:00
}
}