daily update on 20240422 by nitload youmu, ssd structure improve
This commit is contained in:
@@ -35,7 +35,7 @@ using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace StgSharpData
|
||||
namespace StgSharp.Data
|
||||
{
|
||||
public class EncodeStream
|
||||
{
|
||||
|
||||
@@ -28,7 +28,7 @@
|
||||
//
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
using StgSharpData;
|
||||
using StgSharp.Data;
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.Data
|
||||
{
|
||||
|
||||
public partial class Area
|
||||
{
|
||||
private static byte leastUnusedDisc;
|
||||
|
||||
public byte NewDisc(byte raidWith)
|
||||
{
|
||||
byte ret = leastUnusedDisc;
|
||||
leastUnusedDisc++;
|
||||
return ret;
|
||||
}
|
||||
|
||||
public void ClearDiscIDBuffer()
|
||||
{
|
||||
leastUnusedDisc = 0;
|
||||
}
|
||||
|
||||
private static int leastUnusedID;
|
||||
|
||||
public static int NewID()
|
||||
{
|
||||
int ret = leastUnusedID;
|
||||
if (ret == int.MaxValue)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException("Reaches max IDs that one ssd file can provide.");
|
||||
}
|
||||
leastUnusedID++;
|
||||
return ret;
|
||||
}
|
||||
|
||||
public static void ClearIDBuffer()
|
||||
{
|
||||
leastUnusedID = 1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
using StgSharp.Data;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp
|
||||
{
|
||||
public static unsafe partial class StgSharp
|
||||
{
|
||||
private static SSD defaultIdProvider = new SSD();
|
||||
private static List<Area> root = new List<Area>();
|
||||
|
||||
private static byte leastUnusedArea;
|
||||
|
||||
public static byte NewArea()
|
||||
{
|
||||
byte ret = leastUnusedArea;
|
||||
leastUnusedArea++;
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
}//------------------ end of class ---------------------
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.Data
|
||||
{
|
||||
public partial class Area
|
||||
{
|
||||
private byte _id;
|
||||
private List<SSD> discList;
|
||||
|
||||
|
||||
public Area()
|
||||
{
|
||||
_id = StgSharp.NewArea();
|
||||
}
|
||||
|
||||
public SSDSegment FindSegment(int id, byte SSD)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -44,13 +44,13 @@ namespace StgSharp.Data
|
||||
{
|
||||
|
||||
[FieldOffset(0)] internal M128 data;
|
||||
[FieldOffset(12)] internal byte size;
|
||||
|
||||
[FieldOffset(0)] public int globalID;
|
||||
[FieldOffset(4)] public int nextID;
|
||||
|
||||
|
||||
[FieldOffset(8)] public int previousHash;
|
||||
|
||||
|
||||
[FieldOffset(12)] internal byte size;
|
||||
[FieldOffset(13)] public byte selfDefine_1;
|
||||
[FieldOffset(14)] public byte selfDefine_2;
|
||||
[FieldOffset(15)] public byte selfDefine_3;
|
||||
@@ -85,115 +85,4 @@ namespace StgSharp.Data
|
||||
}
|
||||
|
||||
|
||||
public unsafe class SSDrawHead : IEnumerable<int>
|
||||
{
|
||||
|
||||
private List<int> freeSegments;
|
||||
private string name;
|
||||
|
||||
private List<(int, int)> segmentRange;
|
||||
internal SSDSegmentHead _head;
|
||||
|
||||
internal SSDrawHead(string name)
|
||||
{
|
||||
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 SSDrawHead()
|
||||
{
|
||||
segmentRange = new List<(int, int)>();
|
||||
freeSegments = new List<int>();
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
*(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)
|
||||
{
|
||||
*(int*)(bptr + t) = segmentRange[i].Item1;
|
||||
*(int*)(bptr + t + 4) = segmentRange[i].Item2;
|
||||
}
|
||||
for (int i = 0; i < freeSegments.Count; i++, t += 4)
|
||||
{
|
||||
*(int*)(bptr + t) = freeSegments[i];
|
||||
}
|
||||
for (int i = 0; i < namecode.Length; i++, t++)
|
||||
{
|
||||
*(bptr + t) = namecode[i];
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
public void WriteRange(int begin, int end)
|
||||
{
|
||||
segmentRange.Add((begin, end));
|
||||
}
|
||||
|
||||
internal void MarkSegment(int id)
|
||||
{
|
||||
freeSegments.Add(id);
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
foreach (var tuple in segmentRange)
|
||||
{
|
||||
for (int i = tuple.Item1; i <= tuple.Item2; i++)
|
||||
{
|
||||
yield return i;
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var item in freeSegments)
|
||||
{
|
||||
yield return item;
|
||||
}
|
||||
}
|
||||
|
||||
IEnumerator<int> IEnumerable<int>.GetEnumerator()
|
||||
{
|
||||
foreach (var tuple in segmentRange)
|
||||
{
|
||||
for (int i = tuple.Item1; i <= tuple.Item2; i++)
|
||||
{
|
||||
yield return i;
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var item in freeSegments)
|
||||
{
|
||||
yield return item;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,134 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Numerics;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.Data
|
||||
{
|
||||
[StructLayout(LayoutKind.Explicit)]
|
||||
public unsafe struct SSDSegment
|
||||
{
|
||||
|
||||
[FieldOffset(16)] public fixed byte Data[16 * 16];
|
||||
[FieldOffset(12)] public int DataHash;
|
||||
[FieldOffset(0 * 16)] public SSDSegmentHead Head;
|
||||
[FieldOffset(8)] public int PreviousHash;
|
||||
|
||||
public SSDSegment(int previousHash)
|
||||
{
|
||||
this.PreviousHash = previousHash;
|
||||
}
|
||||
|
||||
public int ID
|
||||
{
|
||||
get { return Head.globalID; }
|
||||
set { Head.globalID = value; }
|
||||
}
|
||||
|
||||
public byte this[int index]
|
||||
{
|
||||
get
|
||||
{
|
||||
if (index > (16 * 14) - 1)
|
||||
{
|
||||
throw new IndexOutOfRangeException();
|
||||
}
|
||||
return Data[index];
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
if (index > (16 * 14) - 1)
|
||||
{
|
||||
throw new IndexOutOfRangeException();
|
||||
}
|
||||
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)
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
public unsafe T ReadData<T>(int index) where T : struct
|
||||
{
|
||||
fixed (byte* bptr = Data)
|
||||
{
|
||||
T* tptr = (T*)bptr;
|
||||
return *(tptr + index);
|
||||
}
|
||||
}
|
||||
public unsafe void WriteData<T>(int index, T value) where T : struct
|
||||
{
|
||||
fixed (byte* bptr = Data)
|
||||
{
|
||||
T* tptr = (T*)bptr;
|
||||
*(tptr + index) = value;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -32,18 +32,32 @@ using StgSharp.Data.FileIO;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection.Metadata.Ecma335;
|
||||
using System.Text;
|
||||
|
||||
namespace StgSharp.Data
|
||||
{
|
||||
public partial class SSD
|
||||
{
|
||||
private SSDrawHead head;
|
||||
|
||||
private SSDFolder _root;
|
||||
private Dictionary<string, int> idDict;
|
||||
private Dictionary<int,SSDraw> fileList;
|
||||
private Dictionary<int, SSDraw> drawList;
|
||||
|
||||
private List<SSDSegment> segments;
|
||||
private FileStream file; //很多时候
|
||||
|
||||
|
||||
public SSDSegment FindSegment(int id)
|
||||
{
|
||||
SSDSegment s = segments[id];
|
||||
if (s.)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -31,43 +31,99 @@
|
||||
using StgSharp.Data.FileIO;
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace StgSharp.Data
|
||||
{
|
||||
public class SSDFolder: SSDSerializable
|
||||
public class SSDFolder : SSDSerializable, IEnumerable<SSDSegmentHead>
|
||||
{
|
||||
internal string _name;
|
||||
private SSD local;
|
||||
private SSDrawHead _head;
|
||||
internal int _folderID;
|
||||
internal List<SSDraw> files;
|
||||
internal List<SSDFolder> folders;
|
||||
internal string _name;
|
||||
internal Dictionary<int, SSDraw> files;
|
||||
internal Dictionary<int, SSDFolder> folders;
|
||||
internal Dictionary<string, int> idIndex;
|
||||
|
||||
public SSDFolder(string name)
|
||||
internal SSDFolder()
|
||||
{
|
||||
folders = new Dictionary<int, SSDFolder>();
|
||||
files = new Dictionary<int, SSDraw>();
|
||||
}
|
||||
|
||||
internal SSDFolder(string name)
|
||||
{
|
||||
if (name == "root")
|
||||
{
|
||||
throw new ArgumentNullException("Name cannot be root");
|
||||
throw new ArgumentNullException(
|
||||
"Name cannot be root," +
|
||||
"it is preserved for root file indexer.");
|
||||
}
|
||||
if (name == "default")
|
||||
{
|
||||
throw new ArgumentNullException(
|
||||
"Name cannot be default," +
|
||||
"it is preserved for default file id provider.");
|
||||
}
|
||||
_name = name;
|
||||
folders = new List<SSDFolder>();
|
||||
|
||||
folders = new Dictionary<int, SSDFolder>();
|
||||
files = new Dictionary<int, SSDraw>();
|
||||
}
|
||||
|
||||
public override byte SSDTypeCode => SerializableTypeCode.VirtualFolder;
|
||||
|
||||
/// <summary>
|
||||
/// Check out if this virtual folder contains certain file.
|
||||
/// </summary>
|
||||
/// <param name="name">Name of the file</param>
|
||||
/// <param name="type">Type code defined in <see cref="SerializableTypeCode"/></param>
|
||||
/// <returns>Id of the file in its virtual disc</returns>
|
||||
public int FindFileId(string name, byte type)
|
||||
{
|
||||
int fileid = 0;
|
||||
if (idIndex.TryGetValue($"{name}{SerializableTypeCode.GetNameTail(type)}"
|
||||
, out fileid) && !(type == SerializableTypeCode.VirtualFolder))
|
||||
{
|
||||
return fileid;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Check out if this virtual folder contains certain file.
|
||||
/// </summary>
|
||||
/// <param name="name">Name of the file</param>
|
||||
/// <returns>Id of the folder in its virtual disc</returns>
|
||||
public int FindFolderId(string name)
|
||||
{
|
||||
int folderid = 0;
|
||||
if (idIndex.TryGetValue($"{name}_f", out folderid))
|
||||
{
|
||||
return folderid;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public SSDraw CreateFile(string name, byte type)
|
||||
{
|
||||
SSDraw ret = new SSDraw(name + SerializableTypeCode.GetNameTail(type));
|
||||
ret.ID = local.
|
||||
}
|
||||
|
||||
//TODO 虚拟文件夹的GetBytes好难写
|
||||
public override byte[] GetBytes()
|
||||
{
|
||||
List<byte> rawBytes =new List<byte>();
|
||||
List<byte> rawBytes = new List<byte>();
|
||||
byte[] size = new byte[5];
|
||||
List<int> fileid = new List<int>();
|
||||
fileid.Add(4);
|
||||
foreach (var item in files)
|
||||
{
|
||||
fileid.Add(item.)
|
||||
fileid.Add(item.);
|
||||
}
|
||||
|
||||
|
||||
return rawBytes.ToArray();
|
||||
}
|
||||
|
||||
@@ -80,5 +136,30 @@ namespace StgSharp.Data
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
foreach (var f in files)
|
||||
{
|
||||
yield return f.Value.Head._head;
|
||||
}
|
||||
foreach (var f in folders)
|
||||
{
|
||||
yield return f.Value._head._head;
|
||||
}
|
||||
}
|
||||
|
||||
IEnumerator<SSDSegmentHead> IEnumerable<SSDSegmentHead>.GetEnumerator()
|
||||
{
|
||||
foreach (var f in files)
|
||||
{
|
||||
yield return f.Value.Head._head;
|
||||
}
|
||||
foreach (var f in folders)
|
||||
{
|
||||
yield return f.Value._head._head;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
+139
-94
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ namespace StgSharp.Data
|
||||
internal abstract void BuildFromByteStream(byte[] stream);
|
||||
}
|
||||
|
||||
public class SerializableTypeCode
|
||||
public static class SerializableTypeCode
|
||||
{
|
||||
public const byte IndexTable = 1;
|
||||
public const byte Image = 2;
|
||||
@@ -27,6 +27,30 @@ namespace StgSharp.Data
|
||||
public const byte Text = 5;
|
||||
public const byte RawData = 6;
|
||||
public const byte VirtualFolder = 7;
|
||||
|
||||
|
||||
public static string GetNameTail(byte typeCode)
|
||||
{
|
||||
switch (typeCode)
|
||||
{
|
||||
case IndexTable:
|
||||
return "_indexT";
|
||||
case Image:
|
||||
return "_img";
|
||||
case Audio:
|
||||
return "_wav";
|
||||
case Shader:
|
||||
return "_gls";
|
||||
case Text:
|
||||
return "_txt";
|
||||
case RawData:
|
||||
return "_raw";
|
||||
case VirtualFolder:
|
||||
return "_f";
|
||||
default:
|
||||
throw new ArgumentException("Unknown file type");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static partial class Serializer
|
||||
|
||||
Reference in New Issue
Block a user