daily update on 20240422 by nitload youmu, ssd structure improve

This commit is contained in:
YoumuLiu
2024-04-22 02:47:17 +08:00
parent 12345d8405
commit 0162d71d16
30 changed files with 708 additions and 331 deletions
+139 -94
View File
@@ -28,7 +28,10 @@
//
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
using StgSharp.Graphics.ShaderEdit;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
@@ -46,18 +49,20 @@ namespace StgSharp.Data.FileIO
{
public const string FileType = "ssdraw";
public const int SegmentCapacity = 16 * 16;
private Area home;
private SSDrawHead _head;
private string _name;
private string _name;
private SSDSegment currentSegment;
private int previousHash;
private List<SSDSegment> segments;
public int ID
internal SSDraw()
{
get { return _head._head.globalID; }
segments = new List<SSDSegment>();
ID = home.
}
public SSDraw(string name)
@@ -66,10 +71,41 @@ namespace StgSharp.Data.FileIO
segments = new List<SSDSegment>();
}
public int ID
{
get { return _head._head.globalID; }
set { _head._head.globalID = value; }
}
internal SSDrawHead Head
{
get{return _head; }
}
//TODO 分析头,加载文件头,进一步加载全部的数据块
/// <summary>
///
/// </summary>
/// <param name="head"></param>
/// <returns></returns>
public static SSDraw AnalyzeHead(SSDrawHead head)
{
if (head._head.globalID == 0)
{
return null;
}
SSDraw ret = new SSDraw();
foreach (var item in head)
{
}
}
public void CreateRawFile(string outputFolder, bool rewrite)
{
string route =
$"{(((outputFolder == string.Empty) ? " " : ($"{outputFolder}\\")))}{($"{_name}.{FileType}")}";
$"{((outputFolder == string.Empty) ? " " : ($"{outputFolder}\\"))}{$"{_name}.{FileType}"}";
if (File.Exists(route) && !rewrite)
{
throw new Exception("This SSDRAW has already existed.");
@@ -86,6 +122,7 @@ namespace StgSharp.Data.FileIO
f.Close();
}
//TODO read raw 没写完
public static SSDraw readRaw(string route)
{
string[] routeFolders = route.Split('\\');
@@ -93,9 +130,9 @@ namespace StgSharp.Data.FileIO
{
throw new Exception($"File to open is not a {FileType} file.");
}
FileStream stream = File.Open(route, FileMode.Open);
return new SSDraw(string.Empty);
}
public void WriteRaw(byte[] dataStream)
@@ -133,121 +170,129 @@ namespace StgSharp.Data.FileIO
}
[StructLayout(LayoutKind.Explicit)]
public unsafe struct SSDSegment
public unsafe class SSDrawHead : IEnumerable<int>
{
[FieldOffset(16)] public fixed byte Data[16 * 16];
[FieldOffset(12)] public int DataHash;
[FieldOffset(0 * 16)] public SSDSegmentHead Head;
[FieldOffset(8)] public int PreviousHash;
private List<int> freeSegments;
private string name;
public SSDSegment(int previousHash)
private List<(int, int)> segmentRange;
internal SSDSegmentHead _head;
internal SSDrawHead(string name)
{
this.PreviousHash = previousHash;
int hash;
this.name = name;
using (SHA256 sha256Hash = SHA256.Create())
{
byte[] bytes = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(name));
hash = BitConverter.ToInt32(bytes, 0);
}
_head.previousHash = hash;
segmentRange = new List<(int, int)>();
freeSegments = new List<int>();
}
public byte this[int index]
public SSDrawHead()
{
get
segmentRange = new List<(int, int)>();
freeSegments = new List<int>();
}
public byte DiscIndex
{
get => _head.selfDefine_2;
internal set => _head.selfDefine_2 = value;
}
public byte TypeCode
{
get => _head.selfDefine_1;
internal set => _head.selfDefine_1 = value;
}
public void AddSingleSegment(int id)
{
freeSegments.Add(id);
}
public unsafe byte[] GetBytes(out int size)
{
byte[] namecode = Encoding.UTF8.GetBytes(name);
size = 16 + 4 +
(segmentRange.Count * 8) +
(freeSegments.Count * 4) +
namecode.Length;
byte[] ret = new byte[size];
fixed (byte* bptr = ret)
{
if (index > (16 * 14) - 1)
*(SSDSegmentHead*)bptr = _head;
*(bptr + 16) = (byte)segmentRange.Count;
*(bptr + 17) = (byte)namecode.Length;
*(bptr + 18) = (byte)freeSegments.Count;
int t = 20;
for (int i = 0; i < segmentRange.Count; i++, t += 8)
{
throw new IndexOutOfRangeException();
*(int*)(bptr + t) = segmentRange[i].Item1;
*(int*)(bptr + t + 4) = segmentRange[i].Item2;
}
return Data[index];
}
set
{
if (index > (16 * 14) - 1)
for (int i = 0; i < freeSegments.Count; i++, t += 4)
{
throw new IndexOutOfRangeException();
*(int*)(bptr + t) = freeSegments[i];
}
Data[index] = value;
}
}
public short Size => Head.size;
public void FillRandomBytes(int begin)
{
if (begin > 255)
{
return;
}
for (int i = begin; i < 255; i++)
{
Data[i] = 0; //TODO: 使用STGSHARP内部随机数工具处理它
}
}
public byte[] GetBytes()
{
byte[] ret = new byte[16 * 18];
fixed (M128* mptr = &Head.data)
{
fixed (byte* aptr = ret)
for (int i = 0; i < namecode.Length; i++, t++)
{
Vector4* vsptr = (Vector4*)mptr;
Vector4* vtptr = (Vector4*)aptr;
for (int i = 0; i < 18; i++)
{
*(vtptr + i) = *(vsptr + i);
}
}
}
return ret;
}
public int GetDataHash()
{
Vector4 v = Vector4.One;
fixed (M128* mptr = &Head.data)
{
Vector4* vptr = (Vector4*)mptr;
for (int i = 0; i < 16; i += 1)
{
v += (*vptr) * 31;
}
}
M128 m = new M128(v);
DataHash = m.vec.GetHashCode();
return DataHash;
}
public override int GetHashCode()
{
int ret = 17;
fixed (M128* mptr = &Head.data)
{
Vector4* vptr = (Vector4*)mptr;
for (int i = 0; i < 18; i++)
{
ret = (ret + (*vptr).GetHashCode()) * 31;
*(bptr + t) = namecode[i];
}
}
return ret;
}
public unsafe T ReadData<T>(int index) where T : struct
public void WriteRange(int begin, int end)
{
fixed (byte* bptr = Data)
segmentRange.Add((begin, end));
}
internal void MarkSegment(int id)
{
freeSegments.Add(id);
}
IEnumerator IEnumerable.GetEnumerator()
{
foreach (var tuple in segmentRange)
{
T* tptr = (T*)bptr;
return *(tptr + index);
for (int i = tuple.Item1; i <= tuple.Item2; i++)
{
yield return i;
}
}
foreach (var item in freeSegments)
{
yield return item;
}
}
public unsafe void WriteData<T>(int index, T value) where T : struct
IEnumerator<int> IEnumerable<int>.GetEnumerator()
{
fixed (byte* bptr = Data)
foreach (var tuple in segmentRange)
{
T* tptr = (T*)bptr;
*(tptr + index) = value;
for (int i = tuple.Item1; i <= tuple.Item2; i++)
{
yield return i;
}
}
foreach (var item in freeSegments)
{
yield return item;
}
}
}
}