daily update on 20240610 by nitload youmu
This commit is contained in:
+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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user