daily update on 20240508 by nitload youmu, partical added
This commit is contained in:
@@ -8,11 +8,15 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.Data
|
||||
{
|
||||
public partial class Area
|
||||
/// <summary>
|
||||
/// An independent storage area, used as the only folder type in root folder.
|
||||
/// 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
|
||||
{
|
||||
private byte _id;
|
||||
private List<SSD> discList;
|
||||
private List<SSDrawHead> files;
|
||||
|
||||
public Area()
|
||||
{
|
||||
@@ -23,5 +27,56 @@ namespace StgSharp.Data
|
||||
{
|
||||
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];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,34 @@
|
||||
using StgSharp.Math;
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
// file="DataSegment.cs"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload Space
|
||||
// Copyright (c) Nitload Space. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person
|
||||
// obtaining a copy of this software and associated documentation
|
||||
// files (the “Software”), to deal in the Software without restriction,
|
||||
// including without limitation the rights to use, copy, modify, merge,
|
||||
// publish, distribute, sublicense, and/or sell copies of the Software,
|
||||
// and to permit persons to whom the Software is furnished to do so,
|
||||
// subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and
|
||||
// this permission notice shall be included in all copies
|
||||
// or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED “AS IS”,
|
||||
// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH
|
||||
// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
using StgSharp.Math;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
@@ -10,31 +40,43 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.Data
|
||||
{
|
||||
[StructLayout(LayoutKind.Explicit)]
|
||||
public unsafe struct SSDSegment
|
||||
[StructLayout(LayoutKind.Explicit, Size = StgSharp.ssdSegmentLength * 16)]
|
||||
public unsafe struct SSDSegment : IComparable<int>, IComparable<SSDSegment>
|
||||
{
|
||||
|
||||
[FieldOffset(16)] public fixed byte Data[16 * 16];
|
||||
[FieldOffset(16)] public fixed byte Data[16 * (StgSharp.ssdSegmentLength - 1)];
|
||||
[FieldOffset(12)] public int DataHash;
|
||||
[FieldOffset(0 * 16)] public SSDSegmentHead Head;
|
||||
[FieldOffset(8)] public int PreviousHash;
|
||||
[FieldOffset(0 * 16)] public SSDSegmentHead Head;
|
||||
|
||||
public SSDSegment(int previousHash)
|
||||
{
|
||||
this.PreviousHash = previousHash;
|
||||
}
|
||||
|
||||
public int ID
|
||||
public static SSDSegment FromStream(byte[] stream)
|
||||
{
|
||||
get { return Head.globalID; }
|
||||
set { Head.globalID = value; }
|
||||
if (stream.Length != 16 * StgSharp.ssdSegmentLength)
|
||||
{
|
||||
throw new ArgumentException();
|
||||
}
|
||||
SSDSegment ret = new SSDSegment();
|
||||
fixed (byte* bptr = stream)
|
||||
{
|
||||
ret.Head.data = *(M128*)bptr;
|
||||
for (int i = 1; i < StgSharp.ssdSegmentLength; i++)
|
||||
{
|
||||
ret.WriteData<M128>(i - 1, *(M128*)(bptr + 16 * i));
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
public byte this[int index]
|
||||
{
|
||||
get
|
||||
{
|
||||
if (index > (16 * 14) - 1)
|
||||
if (index > (16 * (StgSharp.ssdSegmentLength - 1)) - 1)
|
||||
{
|
||||
throw new IndexOutOfRangeException();
|
||||
}
|
||||
@@ -43,7 +85,7 @@ namespace StgSharp.Data
|
||||
|
||||
set
|
||||
{
|
||||
if (index > (16 * 14) - 1)
|
||||
if (index > (16 * (StgSharp.ssdSegmentLength - 1)) - 1)
|
||||
{
|
||||
throw new IndexOutOfRangeException();
|
||||
}
|
||||
@@ -51,6 +93,12 @@ namespace StgSharp.Data
|
||||
}
|
||||
}
|
||||
|
||||
public int ID
|
||||
{
|
||||
get { return Head.globalID; }
|
||||
set { Head.globalID = value; }
|
||||
}
|
||||
|
||||
public short Size => Head.size;
|
||||
|
||||
public void FillRandomBytes(int begin)
|
||||
@@ -67,7 +115,7 @@ namespace StgSharp.Data
|
||||
|
||||
public byte[] GetBytes()
|
||||
{
|
||||
byte[] ret = new byte[16 * 18];
|
||||
byte[] ret = new byte[16 * StgSharp.ssdSegmentLength];
|
||||
fixed (M128* mptr = &Head.data)
|
||||
{
|
||||
fixed (byte* aptr = ret)
|
||||
@@ -90,7 +138,7 @@ namespace StgSharp.Data
|
||||
fixed (M128* mptr = &Head.data)
|
||||
{
|
||||
Vector4* vptr = (Vector4*)mptr;
|
||||
for (int i = 0; i < 16; i += 1)
|
||||
for (int i = 0; i < StgSharp.ssdSegmentLength - 1; i += 1)
|
||||
{
|
||||
v += (*vptr) * 31;
|
||||
}
|
||||
@@ -106,7 +154,7 @@ namespace StgSharp.Data
|
||||
fixed (M128* mptr = &Head.data)
|
||||
{
|
||||
Vector4* vptr = (Vector4*)mptr;
|
||||
for (int i = 0; i < 18; i++)
|
||||
for (int i = 0; i < StgSharp.ssdSegmentLength; i++)
|
||||
{
|
||||
ret = (ret + (*vptr).GetHashCode()) * 31;
|
||||
}
|
||||
@@ -122,6 +170,7 @@ namespace StgSharp.Data
|
||||
return *(tptr + index);
|
||||
}
|
||||
}
|
||||
|
||||
public unsafe void WriteData<T>(int index, T value) where T : struct
|
||||
{
|
||||
fixed (byte* bptr = Data)
|
||||
@@ -131,6 +180,16 @@ namespace StgSharp.Data
|
||||
}
|
||||
}
|
||||
|
||||
int IComparable<int>.CompareTo(int other)
|
||||
{
|
||||
return ID.CompareTo(other);
|
||||
}
|
||||
|
||||
int IComparable<SSDSegment>.CompareTo(SSDSegment other)
|
||||
{
|
||||
return ID.CompareTo(other.ID);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -72,18 +72,18 @@ namespace StgSharp.Data
|
||||
files = new Dictionary<int, SSDraw>();
|
||||
}
|
||||
|
||||
public override byte SSDTypeCode => SerializableTypeCode.VirtualFolder;
|
||||
public override 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 in <see cref="SerializableTypeCode"/></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, byte type)
|
||||
public int FindFileId(string name, SerializableTypeCode type)
|
||||
{
|
||||
int fileid = 0;
|
||||
if (idIndex.TryGetValue($"{name}{SerializableTypeCode.GetNameTail(type)}"
|
||||
if (idIndex.TryGetValue($"{name}{Serializer.GetNameTail(type)}"
|
||||
, out fileid) && !(type == SerializableTypeCode.VirtualFolder))
|
||||
{
|
||||
return fileid;
|
||||
@@ -92,6 +92,7 @@ namespace StgSharp.Data
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Check out if this virtual folder contains certain file.
|
||||
/// </summary>
|
||||
@@ -107,9 +108,9 @@ namespace StgSharp.Data
|
||||
return 0;
|
||||
}
|
||||
|
||||
public SSDraw CreateFile(string name, byte type)
|
||||
public SSDraw CreateFile(string name, SerializableTypeCode type)
|
||||
{
|
||||
SSDraw ret = new SSDraw(name + SerializableTypeCode.GetNameTail(type));
|
||||
SSDraw ret = new SSDraw(name + Serializer.GetNameTail(type));
|
||||
ret.ID = local.NewID();
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -49,7 +49,7 @@ namespace StgSharp.Data.FileIO
|
||||
{
|
||||
|
||||
public const string FileType = "ssdraw";
|
||||
public const int SegmentCapacity = 16 * 16;
|
||||
public const int SegmentCapacity = 16 * (StgSharp.ssdSegmentLength - 1);
|
||||
|
||||
private Area home;
|
||||
private int previousHash;
|
||||
@@ -62,7 +62,6 @@ namespace StgSharp.Data.FileIO
|
||||
internal SSDraw()
|
||||
{
|
||||
segments = new List<SSDSegment>();
|
||||
|
||||
}
|
||||
|
||||
public SSDraw(string name)
|
||||
@@ -79,24 +78,11 @@ namespace StgSharp.Data.FileIO
|
||||
|
||||
internal SSDrawHead Head => _head;
|
||||
|
||||
//TODO 分析头,加载文件头,进一步加载全部的数据块
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="head"></param>
|
||||
/// <returns></returns>
|
||||
public static SSDraw AnalyzeHead(SSDrawHead head)
|
||||
public SSDSegment[] GetSegments()
|
||||
{
|
||||
if (head._head.globalID == 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
SSDraw ret = new SSDraw();
|
||||
foreach ((int, int) item in head)
|
||||
{
|
||||
|
||||
}
|
||||
SSDSegment[] ret = segments.ToArray();
|
||||
Array.Resize(ref ret, ret.Length + 1);
|
||||
ret[ret.Length - 1] = Head.Data;
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -118,7 +104,7 @@ namespace StgSharp.Data.FileIO
|
||||
f.Close();
|
||||
}
|
||||
|
||||
//TODO read raw 没写完
|
||||
|
||||
public static SSDraw readRaw(string route)
|
||||
{
|
||||
string[] routeFolders = route.Split('\\');
|
||||
@@ -126,9 +112,20 @@ 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);
|
||||
|
||||
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)
|
||||
@@ -170,9 +167,26 @@ namespace StgSharp.Data.FileIO
|
||||
public unsafe class SSDrawHead : IEnumerable<(int, int)>
|
||||
{
|
||||
|
||||
private SSDSegment segmentRange;
|
||||
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;
|
||||
@@ -190,16 +204,43 @@ namespace StgSharp.Data.FileIO
|
||||
{
|
||||
}
|
||||
|
||||
public (int begin, int end) this[byte index]
|
||||
internal SSDSegment Data
|
||||
{
|
||||
get => segmentRange.ReadData<(int, int)>(index);
|
||||
set => segmentRange.WriteData<(int, int)>(index, value);
|
||||
get => segmentData;
|
||||
}
|
||||
|
||||
public byte DiscIndex
|
||||
public (int begin, int end) this[byte index]
|
||||
{
|
||||
get => _head.selfDefine_2;
|
||||
internal set => _head.selfDefine_2 = value;
|
||||
get => segmentData.ReadData<(int, int)>(index);
|
||||
set => segmentData.WriteData<(int, int)>(index, value);
|
||||
}
|
||||
|
||||
public unsafe string Name
|
||||
{
|
||||
get
|
||||
{
|
||||
byte[] bytes = new byte[15 * 16];
|
||||
fixed (byte* bptr = segmentData.Data)
|
||||
{
|
||||
Marshal.Copy(
|
||||
source: (IntPtr)bptr,
|
||||
destination: bytes,
|
||||
256, 15 * 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 + 256),
|
||||
length: 15 * 16);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public byte TypeCode
|
||||
@@ -210,7 +251,7 @@ namespace StgSharp.Data.FileIO
|
||||
|
||||
internal ref SSDSegmentHead _head
|
||||
{
|
||||
get => ref segmentRange.Head;
|
||||
get => ref segmentData.Head;
|
||||
}
|
||||
|
||||
internal byte pairCount
|
||||
@@ -226,10 +267,9 @@ namespace StgSharp.Data.FileIO
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public unsafe byte[] GetBytes()
|
||||
{
|
||||
return segmentRange.GetBytes();
|
||||
return segmentData.GetBytes();
|
||||
}
|
||||
|
||||
|
||||
@@ -237,7 +277,7 @@ namespace StgSharp.Data.FileIO
|
||||
{
|
||||
for (int i = 0; i < pairCount; i++)
|
||||
{
|
||||
yield return segmentRange.ReadData<(int, int)>(i);
|
||||
yield return segmentData.ReadData<(int, int)>(i);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -245,7 +285,7 @@ namespace StgSharp.Data.FileIO
|
||||
{
|
||||
for (int i = 0; i < pairCount; i++)
|
||||
{
|
||||
yield return segmentRange.ReadData<(int, int)>(i);
|
||||
yield return segmentData.ReadData<(int, int)>(i);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -77,6 +77,14 @@ namespace StgSharp.Data
|
||||
}
|
||||
target = new BitArray(boolStream.ToArray());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Serialize a <see cref="BitArray"/>, useful when writing
|
||||
/// a Huffman tree index dictionary to file.
|
||||
/// </summary>
|
||||
/// <param name="bits"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="Exception"></exception>
|
||||
public static byte[] Serialize(BitArray bits)
|
||||
{
|
||||
int size = (bits.Count / 4) + 1;
|
||||
|
||||
@@ -9,7 +9,7 @@ namespace StgSharp.Data
|
||||
|
||||
public abstract class SSDSerializable
|
||||
{
|
||||
public abstract byte SSDTypeCode { get; }
|
||||
public abstract SerializableTypeCode SSDTypeCode { get; }
|
||||
|
||||
public abstract byte[] GetBytes();
|
||||
|
||||
@@ -18,39 +18,17 @@ namespace StgSharp.Data
|
||||
internal abstract void BuildFromByteStream(byte[] stream);
|
||||
}
|
||||
|
||||
public static class SerializableTypeCode
|
||||
public enum SerializableTypeCode:byte
|
||||
{
|
||||
public const byte IndexTable = 1;
|
||||
public const byte Image = 2;
|
||||
public const byte Audio = 3;
|
||||
public const byte Shader = 4;
|
||||
public const byte Text = 5;
|
||||
public const byte RawData = 6;
|
||||
public const byte VirtualFolder = 7;
|
||||
IndexTable = 1,
|
||||
PixelImage = 2,
|
||||
VectorImage = 3,
|
||||
Audio = 4,
|
||||
Shader = 5,
|
||||
Text = 6,
|
||||
RawData = 7,
|
||||
VirtualFolder = 8,
|
||||
|
||||
|
||||
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
|
||||
@@ -66,5 +44,30 @@ namespace StgSharp.Data
|
||||
serializableObject.BuildFromByteStream(stream);
|
||||
return serializableObject;
|
||||
}
|
||||
|
||||
public static string GetNameTail(SerializableTypeCode typeCode)
|
||||
{
|
||||
switch (typeCode)
|
||||
{
|
||||
case SerializableTypeCode.IndexTable:
|
||||
return "_idt";
|
||||
case SerializableTypeCode.PixelImage:
|
||||
return "_pim";
|
||||
case SerializableTypeCode.VectorImage:
|
||||
return "_vim";
|
||||
case SerializableTypeCode.Audio:
|
||||
return "_wav";
|
||||
case SerializableTypeCode.Shader:
|
||||
return "_gls";
|
||||
case SerializableTypeCode.Text:
|
||||
return "_txt";
|
||||
case SerializableTypeCode.RawData:
|
||||
return "_raw";
|
||||
case SerializableTypeCode.VirtualFolder:
|
||||
return "_fod";
|
||||
default:
|
||||
throw new ArgumentException("Unknown file type");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user