continuity/Wonderking/Game/DataReader.cs
Timothy Schenk f5cd6c380e
Some checks failed
Build, Package and Push Images / preprocess (push) Successful in 2s
Build, Package and Push Images / build (push) Successful in 27s
Build, Package and Push Images / sonarqube (push) Has been skipped
Build, Package and Push Images / sbom-scan (push) Successful in 33s
Build, Package and Push Images / container-build (push) Failing after 1m10s
Build, Package and Push Images / container-sbom-scan (push) Has been skipped
feat: Itemdata pooling & benchmarks for storage options
2023-11-08 19:04:37 +01:00

50 lines
1.4 KiB
C#

using System.Reflection;
namespace Wonderking.Game;
public abstract class DataReader<T>
{
protected DataReader(string path)
{
Path = path;
DatFileContent = new(GetDatFileContent(path).ToArray());
}
private protected string Path { get; init; }
public abstract uint GetAmountOfEntries();
public abstract T GetEntry(uint entryId);
protected ushort GetSizeOfEntry()
{
return typeof(T).GetCustomAttribute<GameDataMetadataAttribute>()?.DataEntrySize ??
throw new NotSupportedException("DataEntrySize is null");
}
private static string GetDatFileName()
{
return typeof(T).GetCustomAttribute<GameDataMetadataAttribute>()?.DatFileName ??
throw new NotSupportedException("DatFileName is null");
}
private static byte GetXorKey()
{
return typeof(T).GetCustomAttribute<GameDataMetadataAttribute>()?.XorKey ??
throw new NotSupportedException("XorKey is null");
}
protected MemoryStream DatFileContent { get; }
private static Span<byte> GetDatFileContent(string path)
{
var fileData = File.ReadAllBytes(path + GetDatFileName());
var data = new byte[fileData.Length];
for (var i = 0; i < fileData.Length; i++)
{
data[i] = (byte)(fileData[i] ^ GetXorKey());
}
return data;
}
}