258 lines
7.5 KiB
C#
258 lines
7.5 KiB
C#
//-----------------------------------------------------------------------
|
|
//-----------------------------------------------------------------------
|
|
// file="SSDraw.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.Graphics.ShaderEdit;
|
|
|
|
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Numerics;
|
|
using System.Runtime.CompilerServices;
|
|
using System.Runtime.InteropServices;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using System.Xml.Serialization;
|
|
|
|
namespace StgSharp.Data.FileIO
|
|
{
|
|
public class SSDraw
|
|
{
|
|
|
|
public const string FileType = "ssdraw";
|
|
public const int SegmentCapacity = 16 * 16;
|
|
|
|
private Area home;
|
|
private int previousHash;
|
|
private List<SSDSegment> segments;
|
|
|
|
private SSDrawHead _head;
|
|
private SSDSegment currentSegment;
|
|
private string _name;
|
|
|
|
internal SSDraw()
|
|
{
|
|
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;
|
|
|
|
//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 ((int, int) item in head)
|
|
{
|
|
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
public void CreateRawFile(string outputFolder, bool rewrite)
|
|
{
|
|
string route = (outputFolder == string.Empty) ? ($"{_name}.{FileType}") :
|
|
($"{outputFolder} \\ {_name}.{FileType}");
|
|
if (File.Exists(route) && !rewrite)
|
|
{
|
|
throw new Exception("This SSDRAW has already existed.");
|
|
}
|
|
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();
|
|
}
|
|
|
|
//TODO read raw 没写完
|
|
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.");
|
|
}
|
|
FileStream stream = File.Open(route, FileMode.Open);
|
|
return new SSDraw(string.Empty);
|
|
|
|
}
|
|
|
|
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();
|
|
}
|
|
|
|
}
|
|
|
|
|
|
public unsafe class SSDrawHead : IEnumerable<(int, int)>
|
|
{
|
|
|
|
private SSDSegment segmentRange;
|
|
private string name;
|
|
|
|
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()
|
|
{
|
|
}
|
|
|
|
public (int begin, int end) this[byte index]
|
|
{
|
|
get => segmentRange.ReadData<(int, int)>(index);
|
|
set => segmentRange.WriteData<(int, int)>(index, value);
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
internal ref SSDSegmentHead _head
|
|
{
|
|
get => ref segmentRange.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 segmentRange.GetBytes();
|
|
}
|
|
|
|
|
|
IEnumerator IEnumerable.GetEnumerator()
|
|
{
|
|
for (int i = 0; i < pairCount; i++)
|
|
{
|
|
yield return segmentRange.ReadData<(int, int)>(i);
|
|
}
|
|
}
|
|
|
|
IEnumerator<(int, int)> IEnumerable<(int, int)>.GetEnumerator()
|
|
{
|
|
for (int i = 0; i < pairCount; i++)
|
|
{
|
|
yield return segmentRange.ReadData<(int, int)>(i);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|