daily update on 20240610 by nitload youmu
This commit is contained in:
@@ -100,6 +100,8 @@ namespace StgSharp.Data
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
#if NET5_0_OR_GREATER
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
|
||||
@@ -13,70 +13,8 @@ namespace StgSharp.Data
|
||||
/// Contains several <see cref="SSD"/> files reference as virtual disc.
|
||||
/// Data is actually stored in <see cref="SSD"/>s in certain RAID mode.
|
||||
/// </summary>
|
||||
public partial class Area:SSDFolder
|
||||
public partial class Area
|
||||
{
|
||||
private byte _id;
|
||||
private List<SSD> discList;
|
||||
|
||||
public Area()
|
||||
{
|
||||
_id = StgSharp.NewArea();
|
||||
}
|
||||
|
||||
public SSDSegment FindSegment(int id, byte SSD)
|
||||
{
|
||||
return discList[SSD].FindSegment(id);
|
||||
}
|
||||
|
||||
/// <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 ((int, int) item in head)
|
||||
{
|
||||
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
public SSDraw FindFile(string route, SerializableTypeCode typeCode)
|
||||
{
|
||||
int id = 0;
|
||||
if (route.Contains('/'))
|
||||
{
|
||||
if (route.Contains(Serializer.GetNameTail(typeCode)))
|
||||
{
|
||||
route += Serializer.GetNameTail(typeCode);
|
||||
}
|
||||
id = idIndex[route];
|
||||
return this.files[id];
|
||||
}
|
||||
if (route.Contains(Serializer.GetNameTail(typeCode)))
|
||||
{
|
||||
route += Serializer.GetNameTail(typeCode);
|
||||
}
|
||||
string[] folderRoute = route.Split('/');
|
||||
int index = 0;
|
||||
if (folderRoute[0] != ".")
|
||||
{
|
||||
index = 1;
|
||||
}
|
||||
SSDFolder temp = this;
|
||||
for (; index < folderRoute.Length - 1; index++)
|
||||
{
|
||||
id = temp.idIndex[folderRoute[index]];
|
||||
temp = temp.folders[id];
|
||||
}
|
||||
id = idIndex[folderRoute[folderRoute.Length-1]];
|
||||
return temp.files[id];
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,29 +41,24 @@ namespace StgSharp.Data
|
||||
{
|
||||
private SSDrawHead head;
|
||||
|
||||
private Dictionary<string, int> idDict;
|
||||
private Dictionary<int,SSDraw> fileList;
|
||||
private Dictionary<int, SSDraw> drawList;
|
||||
private Dictionary<string, SSDraw> fileList;
|
||||
|
||||
private List<SSDSegment> segments;
|
||||
private FileStream file; //很多时候
|
||||
private FileStream file;
|
||||
|
||||
|
||||
public SSDSegment FindSegment(int id)
|
||||
public SSD()
|
||||
{
|
||||
SSDSegment s = segments[id];
|
||||
if (s.ID != id)
|
||||
{
|
||||
foreach (var item in segments)
|
||||
{
|
||||
if (item.ID == id)
|
||||
{
|
||||
s = item;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return s;
|
||||
fileList = new Dictionary<string, SSDraw>();
|
||||
}
|
||||
|
||||
public SSD(FileStream fileStream)
|
||||
{
|
||||
fileList = new Dictionary<string, SSDraw>();
|
||||
file = fileStream;
|
||||
}
|
||||
|
||||
public static SSD FromFile(string fileRoute)
|
||||
{
|
||||
return new SSD(new FileStream(fileRoute, FileMode.Open, FileAccess.Read)) { };
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -37,131 +37,48 @@ using System.Text;
|
||||
|
||||
namespace StgSharp.Data
|
||||
{
|
||||
public class SSDFolder : SSDSerializable, IEnumerable<SSDSegmentHead>
|
||||
public class SSDFolder : ISSDSerializable
|
||||
{
|
||||
private Area local;
|
||||
private SSDrawHead _head;
|
||||
internal int _folderID;
|
||||
internal string _name;
|
||||
internal Dictionary<int, SSDraw> files;
|
||||
internal Dictionary<int, SSDFolder> folders;
|
||||
internal Dictionary<string, int> idIndex;
|
||||
internal Dictionary<string, SSDFolder> folders;
|
||||
|
||||
internal SSDFolder()
|
||||
{
|
||||
folders = new Dictionary<int, SSDFolder>();
|
||||
files = new Dictionary<int, SSDraw>();
|
||||
folders = new Dictionary<string, SSDFolder>();
|
||||
}
|
||||
|
||||
internal SSDFolder(string name)
|
||||
{
|
||||
if (name == "root")
|
||||
{
|
||||
throw new ArgumentNullException(
|
||||
throw new ArgumentException(
|
||||
"Name cannot be root," +
|
||||
"it is preserved for root file indexer.");
|
||||
}
|
||||
if (name == "default")
|
||||
{
|
||||
throw new ArgumentNullException(
|
||||
throw new ArgumentException(
|
||||
"Name cannot be default," +
|
||||
"it is preserved for default file id provider.");
|
||||
}
|
||||
_name = name;
|
||||
folders = new Dictionary<int, SSDFolder>();
|
||||
files = new Dictionary<int, SSDraw>();
|
||||
folders = new Dictionary<string, SSDFolder>();
|
||||
}
|
||||
|
||||
public override SerializableTypeCode SSDTypeCode => SerializableTypeCode.VirtualFolder;
|
||||
public SerializableTypeCode 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 as <see cref="SerializableTypeCode"/></param>
|
||||
/// <returns>Id of the file in its virtual disc</returns>
|
||||
public int FindFileId(string name, SerializableTypeCode type)
|
||||
{
|
||||
int fileid = 0;
|
||||
if (idIndex.TryGetValue($"{name}{Serializer.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, SerializableTypeCode type)
|
||||
{
|
||||
SSDraw ret = new SSDraw(name + Serializer.GetNameTail(type));
|
||||
ret.ID = local.NewID();
|
||||
return ret;
|
||||
}
|
||||
|
||||
//TODO 虚拟文件夹的GetBytes好难写
|
||||
public override byte[] GetBytes()
|
||||
{
|
||||
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.Value.ID);
|
||||
}
|
||||
return rawBytes.ToArray();
|
||||
}
|
||||
|
||||
public override byte[] GetBytes(out int length)
|
||||
public byte[] GetBytes()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
internal override void BuildFromByteStream(byte[] stream)
|
||||
public void FromBytes(byte[] stream)
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
+12
-234
@@ -47,250 +47,28 @@ namespace StgSharp.Data.FileIO
|
||||
{
|
||||
public class SSDraw
|
||||
{
|
||||
private byte[] data;
|
||||
|
||||
public const string FileType = "ssdraw";
|
||||
public const int SegmentCapacity = 16 * (StgSharp.ssdSegmentLength - 1);
|
||||
|
||||
private Area home;
|
||||
private int previousHash;
|
||||
private List<SSDSegment> segments;
|
||||
|
||||
private SSDrawHead _head;
|
||||
private SSDSegment currentSegment;
|
||||
private string _name;
|
||||
|
||||
internal SSDraw()
|
||||
public SSDraw(string name, Stream streamSource)
|
||||
{
|
||||
segments = new List<SSDSegment>();
|
||||
}
|
||||
|
||||
public SSDraw(string name)
|
||||
{
|
||||
_name = name;
|
||||
segments = new List<SSDSegment>();
|
||||
}
|
||||
|
||||
public int ID
|
||||
{
|
||||
get => _head._head.globalID;
|
||||
set => _head._head.globalID = value;
|
||||
}
|
||||
|
||||
internal SSDrawHead Head => _head;
|
||||
|
||||
public SSDSegment[] GetSegments()
|
||||
{
|
||||
SSDSegment[] ret = segments.ToArray();
|
||||
Array.Resize(ref ret, ret.Length + 1);
|
||||
ret[ret.Length - 1] = Head.Data;
|
||||
return ret;
|
||||
}
|
||||
|
||||
public void CreateRawFile(string outputFolder, bool rewrite)
|
||||
{
|
||||
string route = (outputFolder == string.Empty) ? ($"{_name}.{FileType}") :
|
||||
($"{outputFolder} \\ {_name}.{FileType}");
|
||||
if (File.Exists(route) && !rewrite)
|
||||
if ((streamSource == null) || (streamSource.Length == 0))
|
||||
{
|
||||
throw new Exception("This SSDRAW has already existed.");
|
||||
throw new ArgumentNullException(nameof(streamSource));
|
||||
}
|
||||
using (File.Create(route)) { }
|
||||
using (File.Open(route, FileMode.Truncate)) { }
|
||||
FileStream f = File.Open(route, FileMode.Open);
|
||||
foreach (SSDSegment _head in segments)
|
||||
{
|
||||
f.Write(_head.GetBytes(), 0, 18 * 16);
|
||||
}
|
||||
f.Close();
|
||||
}
|
||||
|
||||
|
||||
public static SSDraw readRaw(string route)
|
||||
{
|
||||
string[] routeFolders = route.Split('\\');
|
||||
if (!routeFolders.Last().Contains($".{FileType}"))
|
||||
{
|
||||
throw new Exception($"File to open is not a {FileType} file.");
|
||||
}
|
||||
byte[] dataStream= new byte[16 * StgSharp.ssdSegmentLength];
|
||||
SSDraw ret = new SSDraw();
|
||||
using (FileStream stream = File.Open(route, FileMode.Open))
|
||||
{
|
||||
int offset = 0;
|
||||
offset += stream.Read(dataStream,offset,16);
|
||||
ret._head = new SSDrawHead(dataStream);
|
||||
while (offset < stream.Length)
|
||||
{
|
||||
offset += stream.Read(dataStream, offset, 16);
|
||||
ret.segments.Add(SSDSegment.FromStream(dataStream));
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
public void WriteRaw(byte[] dataStream)
|
||||
{
|
||||
previousHash = BuildRawHead(0);
|
||||
currentSegment = new SSDSegment(previousHash);
|
||||
int t = 0;
|
||||
for (int i = 0; i < dataStream.Length; i++, t++)
|
||||
{
|
||||
if (t == SegmentCapacity)
|
||||
{
|
||||
currentSegment.GetDataHash();
|
||||
currentSegment.Head.Size = 256;
|
||||
currentSegment.Head.globalID = i;
|
||||
segments.Add(currentSegment);
|
||||
previousHash = currentSegment.GetHashCode();
|
||||
currentSegment = new SSDSegment(previousHash);
|
||||
t = 0;
|
||||
}
|
||||
currentSegment[i] = dataStream[i];
|
||||
}
|
||||
currentSegment.FillRandomBytes(t);
|
||||
currentSegment.GetDataHash();
|
||||
currentSegment.Head.Size = t;
|
||||
segments.Add(currentSegment);
|
||||
|
||||
}
|
||||
|
||||
private int BuildRawHead(int beginID)
|
||||
{
|
||||
SSDrawHead rawHead = new SSDrawHead(_name);
|
||||
_head = rawHead;
|
||||
return rawHead.GetHashCode();
|
||||
Name = name;
|
||||
data = new byte[streamSource.Length];
|
||||
streamSource.Read(data.AsSpan());
|
||||
}
|
||||
|
||||
public SSDrawHead Head { get; set; }
|
||||
public string Name { get; set; }
|
||||
}
|
||||
|
||||
|
||||
public unsafe class SSDrawHead : IEnumerable<(int, int)>
|
||||
[StructLayout(LayoutKind.Explicit, Size = 32 * 16)]
|
||||
public unsafe struct SSDrawHead
|
||||
{
|
||||
|
||||
private SSDSegment segmentData;
|
||||
private string name;
|
||||
|
||||
internal SSDrawHead(byte[] data)
|
||||
{
|
||||
if (data.Length != 16* StgSharp.ssdSegmentLength)
|
||||
{
|
||||
throw new ArgumentException();
|
||||
}
|
||||
segmentData = new SSDSegment();
|
||||
fixed (byte* bptr = data)
|
||||
{
|
||||
segmentData.Head.data = *(M128*)bptr;
|
||||
for (int i = 1; i < StgSharp.ssdSegmentLength; i++)
|
||||
{
|
||||
segmentData.WriteData<M128>(i - 1,*(M128*)(bptr + 16 * i));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public SSDrawHead()
|
||||
{
|
||||
}
|
||||
|
||||
internal SSDSegment Data
|
||||
{
|
||||
get => segmentData;
|
||||
}
|
||||
|
||||
public (int begin, int end) this[byte index]
|
||||
{
|
||||
get => segmentData.ReadData<(int, int)>(index);
|
||||
set => segmentData.WriteData<(int, int)>(index, value);
|
||||
}
|
||||
|
||||
public unsafe string Name
|
||||
{
|
||||
get
|
||||
{
|
||||
byte[] bytes = new byte[(StgSharp.ssdSegmentLength - 1)* 16];
|
||||
fixed (byte* bptr = segmentData.Data)
|
||||
{
|
||||
Marshal.Copy(
|
||||
source: (IntPtr)bptr,
|
||||
destination: bytes,
|
||||
(StgSharp.ssdSegmentLength) * 16, (StgSharp.ssdSegmentLength - 1) * 16);
|
||||
}
|
||||
return Encoding.UTF8.GetString(bytes);
|
||||
}
|
||||
set
|
||||
{
|
||||
byte[] namecode = Encoding.UTF8.GetBytes(value);
|
||||
fixed (byte* bptr = segmentData.Data)
|
||||
{
|
||||
Marshal.Copy(
|
||||
source: namecode,
|
||||
startIndex: 0,
|
||||
destination: (IntPtr)(bptr + (StgSharp.ssdSegmentLength) * 16),
|
||||
length: (StgSharp.ssdSegmentLength - 1) * 16);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public byte TypeCode
|
||||
{
|
||||
get => _head.selfDefine_1;
|
||||
internal set => _head.selfDefine_1 = value;
|
||||
}
|
||||
|
||||
internal ref SSDSegmentHead _head
|
||||
{
|
||||
get => ref segmentData.Head;
|
||||
}
|
||||
|
||||
internal byte pairCount
|
||||
{
|
||||
get => (byte)(_head.size / 8);
|
||||
set
|
||||
{
|
||||
if (value > 32)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
_head.size = (byte)(value * 8);
|
||||
}
|
||||
}
|
||||
|
||||
public unsafe byte[] GetBytes()
|
||||
{
|
||||
return segmentData.GetBytes();
|
||||
}
|
||||
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
for (int i = 0; i < pairCount; i++)
|
||||
{
|
||||
yield return segmentData.ReadData<(int, int)>(i);
|
||||
}
|
||||
}
|
||||
|
||||
IEnumerator<(int, int)> IEnumerable<(int, int)>.GetEnumerator()
|
||||
{
|
||||
for (int i = 0; i < pairCount; i++)
|
||||
{
|
||||
yield return segmentData.ReadData<(int, int)>(i);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -7,15 +7,13 @@ using System.Threading.Tasks;
|
||||
namespace StgSharp.Data
|
||||
{
|
||||
|
||||
public abstract class SSDSerializable
|
||||
public interface ISSDSerializable
|
||||
{
|
||||
public abstract SerializableTypeCode SSDTypeCode { get; }
|
||||
public SerializableTypeCode SSDTypeCode { get; }
|
||||
|
||||
public abstract byte[] GetBytes();
|
||||
public byte[] GetBytes();
|
||||
|
||||
public abstract byte[] GetBytes(out int length);
|
||||
|
||||
internal abstract void BuildFromByteStream(byte[] stream);
|
||||
internal void FromBytes(byte[] stream);
|
||||
}
|
||||
|
||||
public enum SerializableTypeCode
|
||||
@@ -28,21 +26,20 @@ namespace StgSharp.Data
|
||||
Shader = 5,
|
||||
Text = 6,
|
||||
RawData = 7,
|
||||
VirtualFolder = 8,
|
||||
|
||||
VirtualFolder = int.MaxValue,
|
||||
}
|
||||
|
||||
public static partial class Serializer
|
||||
{
|
||||
public static byte[] Serialize(SSDSerializable serializableObject)
|
||||
public static byte[] Serialize(ISSDSerializable serializableObject)
|
||||
{
|
||||
return serializableObject.GetBytes();
|
||||
}
|
||||
|
||||
public static T Deserialize<T>(byte[] stream)where T : SSDSerializable
|
||||
public static T Deserialize<T>(byte[] stream)where T : ISSDSerializable
|
||||
{
|
||||
T serializableObject = (T)Activator.CreateInstance<T>();
|
||||
serializableObject.BuildFromByteStream(stream);
|
||||
serializableObject.FromBytes(stream);
|
||||
return serializableObject;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user