Compare commits

..

No commits in common. "ea52c2128b4dc21c38d93aa061166fc0182808dd" and "12a544b6df5f638e26b5f18831d055dd8b261621" have entirely different histories.

8 changed files with 557 additions and 976 deletions

View File

@ -170,7 +170,7 @@ namespace EscudeTools
// Add columns to the create table query
foreach (var column in sheet.col)
{
createTableQuery.Append($"{column.name} {Utils.GetSQLiteColumnType(column.type)}, ");
createTableQuery.Append($"{column.name} {Utils.GetSQLiteColumnType(column.type, column.size)}, ");
}
createTableQuery.Remove(createTableQuery.Length - 2, 2); // Remove the last comma and space

View File

@ -1,71 +0,0 @@
using EscudeEditor;
namespace EscudeTools.EscudeEditor
{
internal class BitWriter : Stream
{
Stream Base;
int Buffer = 0;
int BufferedBits = 0;
internal BitWriter(Stream Output)
{
Base = Output;
}
public override bool CanRead => false;
public override bool CanSeek => false;
public override bool CanWrite => true;
public override long Length => Base.Length;
public override long Position { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
public override void Flush()
{
Base.WriteByte((byte)(Buffer & 0xFF));
Buffer = 0;
BufferedBits = 0;
}
public override int Read(byte[] buffer, int offset, int count)
{
throw new NotImplementedException();
}
public override long Seek(long offset, SeekOrigin origin)
{
throw new NotImplementedException();
}
public override void SetLength(long value)
{
throw new NotImplementedException();
}
public override void Write(byte[] buffer, int offset, int count)
{
throw new NotImplementedException();
}
public void PutBits(byte Byte)
{
for (int i = 0; i < 8; i++)
PutBit(Byte.GetBit(i));
}
public void PutBit(bool Bit)
{
Buffer <<= 1;
Buffer |= (byte)(Bit ? 1 : 0);
BufferedBits++;
if (BufferedBits == 8)
{
Base.WriteByte((byte)(Buffer & 0xFF));
BufferedBits -= 8;
}
}
}
}

View File

@ -1,78 +0,0 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace EscudeEditor {
internal static class Extensions {
public static string GetString(this byte[] Binary, uint At, uint Len) {
EncodingProvider provider = CodePagesEncodingProvider.Instance;
Encoding? shiftJis = provider.GetEncoding("shift-jis");
byte[] Buffer = new byte[Len];
for (int i = 0; i < Len; i++) {
Buffer[i] = Binary[i + At];
}
return shiftJis.GetString(Buffer);
}
public static uint GetUInt32(this byte[] Binary, uint Position) {
byte[] Buffer = new byte[4];
for (int i = 0; i < 4; i++)
Buffer[i] = Binary[i+Position];
return BitConverter.ToUInt32(Buffer, 0);
}
public static void WriteTo(this uint Value, byte[] Binary, uint At) {
BitConverter.GetBytes(Value).CopyTo(Binary, At);
}
public static bool GetBit(this byte Byte, int Bit) {
return (Byte & (1 << (7 - Bit))) != 0;
}
public static byte[] ToArray(this Stream Stream) {
Stream.Seek(0,0);
byte[] Data = new byte[Stream.Length];
Stream.Read(Data, 0, Data.Length);
return Data;
}
public static void WriteString(this Stream Stream, string String) {
EncodingProvider provider = CodePagesEncodingProvider.Instance;
Encoding? shiftJis = provider.GetEncoding("shift-jis");
byte[] Buffer = shiftJis.GetBytes(String);
Stream.Write(Buffer, 0, Buffer.Length);
}
public static void WriteCString(this Stream Stream, string String) {
Stream.WriteString(String);
Stream.WriteByte(0x00);
}
public static string PeekCString(this Stream Stream, uint At) {
long Pos = Stream.Position;
List<byte> Buffer = new List<byte>();
Stream.Position = At;
int b = 0;
while ((b = Stream.ReadByte()) >= 1)
Buffer.Add((byte)b);
Stream.Position = Pos;
return Encoding.GetEncoding(932).GetString(Buffer.ToArray());
}
public static uint PeekUInt32(this Stream Stream, uint At) {
long Pos = Stream.Position;
byte[] Buffer = new byte[4];
Stream.Position = At;
Stream.Read(Buffer, 0, Buffer.Length);
Stream.Position = Pos;
return Buffer.GetUInt32(0);
}
}
}

View File

@ -114,8 +114,9 @@ namespace EscudeTools
{
static readonly byte[] lsfFileSignature = [0x4C, 0x53, 0x46, 0x00];
static readonly byte[] lsfLayerSkipSignature = [0x00, 0x75, 0x6C, 0x00]; //flowchat部分的lsf块
private static string WorkPath = string.Empty;
private LsfData lsfData = new();
private Dictionary<string, LsfData> lsfDataLookup = [];
private Dictionary<string, LsfData> lsfDataLookup = new();
private bool preFetchInfo;
@ -157,7 +158,7 @@ namespace EscudeTools
return lsfData; // 如果未找到,则返回 null
}
private static LsfFileHeader LoadLsfHeader(string path)
private LsfFileHeader LoadLsfHeader(string path)
{
using var fs = new FileStream(path, FileMode.Open, FileAccess.Read);
if (fs.Length < 0x1C)

View File

@ -1,9 +1,5 @@
//这里的部分Unpack代码参考了Garbro中关于ESCUDE BIN封包的实现
//这里的部分Pack代码参考了marcussacana/EscudeEditor中的假压缩实现
using EscudeEditor;
using EscudeTools.EscudeEditor;
//这里的提取代码参考(Ctrl+C, Ctrl+V)了Garbro中关于ESCUDE BIN封包的实现
using EscudeTools.Garbro;
using System.Drawing;
using System.Text;
using System.Text.Json;
@ -58,10 +54,6 @@ namespace EscudeTools
if (item == null)
return false;
pItem = item;
if (!Directory.Exists(Path.Combine(Path.GetDirectoryName(path), "output")))
Directory.CreateDirectory(Path.Combine(Path.GetDirectoryName(path), "output"));
File.WriteAllText(Path.Combine(Path.GetDirectoryName(path), "output", Path.GetFileNameWithoutExtension(path) + ".json"), JsonSerializer.Serialize(pItem));
}
isLoaded = true;
pFile = path;
@ -129,7 +121,7 @@ namespace EscudeTools
return null;
Decrypt(ref index);
int index_offset = 0;
List<Entry> dir = new((int)m_count);
var dir = new List<Entry>((int)m_count);
for (uint i = 0; i < m_count; ++i)
{
int filename_offset = (int)Utils.ToUInt32(index, index_offset);
@ -178,7 +170,7 @@ namespace EscudeTools
if (!Directory.Exists(output))
Directory.CreateDirectory(output);
var lzwManifest = new List<LzwEntry>();
string jsonPath = Path.Combine(output, "lzwManifest.json");
//string jsonPath = Path.Combine(output, "lzwManifest.json");
using FileStream inputStream = new(pFile, FileMode.Open, FileAccess.Read);
using BinaryReader br = new(inputStream);
foreach (Entry entry in pItem)
@ -215,18 +207,18 @@ namespace EscudeTools
if (lzwManifest.Count > 0)
{
using (FileStream fs = File.Create(jsonPath))
{
byte[] jsonBytes = Encoding.UTF8.GetBytes(JsonSerializer.Serialize(lzwManifest));
fs.Write(jsonBytes, 0, jsonBytes.Length);
}
//using (FileStream fs = File.Create(jsonPath))
//{
// byte[] jsonBytes = Encoding.UTF8.GetBytes(JsonSerializer.Serialize(lzwManifest));
// fs.Write(jsonBytes, 0, jsonBytes.Length);
//}
LzwDecode(lzwManifest, output);
};
return true;
}
private static void LzwDecode(List<LzwEntry> lzwManifest, string output)
private void LzwDecode(List<LzwEntry> lzwManifest, string output)
{
foreach (var i in lzwManifest)
{
@ -244,13 +236,19 @@ namespace EscudeTools
}
}
internal sealed class LzwDecoder(Stream input, int unpacked_size) : IDisposable
internal sealed class LzwDecoder : IDisposable
{
private MsbBitStream m_input = new(input, true);
private byte[] m_output = new byte[unpacked_size];
private MsbBitStream m_input;
private byte[] m_output;
public byte[] Output { get { return m_output; } }
public LzwDecoder(Stream input, int unpacked_size)
{
m_input = new MsbBitStream(input, true);
m_output = new byte[unpacked_size];
}
public void Unpack()
{
int dst = 0;
@ -314,142 +312,96 @@ namespace EscudeTools
#endregion
}
public bool Repack(string path, int version, string customKeyProviderPath = "") //目前支持v2v1
public bool Repack(string path, int version, bool useCustomKey = false, string customKeyProviderPath = "") //目前支持v2v1
{
if (customKeyProviderPath!="")
if (useCustomKey)
LoadKey(customKeyProviderPath);
ReadPItem(path);
List<LzwEntry> lzwManifest = [];
if (File.Exists(Path.Combine(path, "lzwManifest.json")))
lzwManifest = JsonSerializer.Deserialize<List<LzwEntry>>(File.ReadAllText(Path.Combine(path, "lzwManifest.json"))) ?? [];
GeneratePItem(path);
//if(File.Exists(Path.Combine(path, "lzwManifest.json")))
// return false;
//Q:为什么不支持LZW打包 //A:因为我实在不想研究lzw算法欢迎PR
m_seed = isLoaded ? LoadedKey : 2210579460;
string outputPath = Path.Combine(Path.GetDirectoryName(path), Path.GetFileName(path) + ".bin");
using FileStream fs = new(outputPath, FileMode.Create);
using BinaryWriter bw = new(fs);
bw.Write(fileSignature);
bw.Write(supportPackVersion[version - 1]);
bw.Write(m_seed);
m_count = (uint)pItem.Count;
bw.Write(m_count ^ NextKey());
EncodingProvider provider = CodePagesEncodingProvider.Instance;
Encoding? shiftJis = provider.GetEncoding("shift-jis");
if (version == 1) //未经测试
using (FileStream fs = new(outputPath, FileMode.Create))
using (BinaryWriter bw = new(fs))
{
long storeOffset = 0x10 + m_count * 0x88;
for (int i = 0; i < m_count; i++)
bw.Write(fileSignature);
bw.Write(supportPackVersion[version - 1]);
bw.Write(m_seed);
m_count = (uint)pItem.Count;
bw.Write(m_count ^ NextKey());
EncodingProvider provider = CodePagesEncodingProvider.Instance;
Encoding? shiftJis = provider.GetEncoding("shift-jis");
if (version == 1) //未经测试
{
byte[] strbytes = shiftJis.GetBytes(pItem[i].Name);
byte[] result = new byte[80];
int lengthToCopy = Math.Min(strbytes.Length, 78);
Array.Copy(strbytes, result, lengthToCopy);
bw.Write(result);
bw.Write(storeOffset);
uint size = pItem[i].Size;
if (Utils.SearchLzwEntryList(lzwManifest, pItem[i].Name) != -1)
size = (uint)FakeCompress(File.ReadAllBytes(Path.Combine(path, pItem[i].Name))).Length;
bw.Write(size);
storeOffset += size;
long storeOffset = 0x10 + m_count * 0x88;
for (int i = 0; i < m_count; i++)
{
byte[] strbytes = shiftJis.GetBytes(pItem[i].Name);
byte[] result = new byte[80];
int lengthToCopy = Math.Min(strbytes.Length, 78);
Array.Copy(strbytes, result, lengthToCopy);
bw.Write(result);
bw.Write(storeOffset);
bw.Write(pItem[i].Size);
storeOffset += pItem[i].Size;
}
}
}
else
{
uint namesSize = (uint)pItem.Sum(e => e.Name.Length + 1);
bw.Write(namesSize ^ NextKey());
uint filenameOffset = 0;
long storeOffset = 0x14 + m_count * 12 + namesSize;
byte[] index = new byte[m_count * 12];
int indexOffset = 0;
for (int i = 0; i < m_count; i++)
else
{
BitConverter.GetBytes(filenameOffset).CopyTo(index, indexOffset);
indexOffset += 4;
BitConverter.GetBytes(storeOffset).CopyTo(index, indexOffset);
indexOffset += 4;
uint size = pItem[i].Size;
if (Utils.SearchLzwEntryList(lzwManifest, pItem[i].Name) != -1)
size = (uint)FakeCompress(File.ReadAllBytes(Path.Combine(path, pItem[i].Name))).Length;
BitConverter.GetBytes(size).CopyTo(index, indexOffset);
indexOffset += 4;
filenameOffset += (uint)pItem[i].Name.Length + 1;
storeOffset += size;
}
Decrypt(ref index);
bw.Write(index);
uint namesSize = (uint)pItem.Sum(e => e.Name.Length + 1);
bw.Write(namesSize ^ NextKey());
uint filenameOffset = 0;
long storeOffset = 0x14 + m_count * 12 + namesSize;
byte[] index = new byte[m_count * 12];
int indexOffset = 0;
for (int i = 0; i < m_count; i++)
{
BitConverter.GetBytes(filenameOffset).CopyTo(index, indexOffset);
indexOffset += 4;
BitConverter.GetBytes(storeOffset).CopyTo(index, indexOffset);
indexOffset += 4;
BitConverter.GetBytes(pItem[i].Size).CopyTo(index, indexOffset);
indexOffset += 4;
filenameOffset += (uint)pItem[i].Name.Length + 1;
storeOffset += pItem[i].Size;
}
Decrypt(ref index);
bw.Write(index);
foreach (Entry entry in pItem)
{
byte[] nameBytes = shiftJis.GetBytes(entry.Name);
bw.Write(nameBytes);
bw.Write((byte)0);
}
}
foreach (Entry entry in pItem)
{
byte[] nameBytes = shiftJis.GetBytes(entry.Name);
bw.Write(nameBytes);
bw.Write((byte)0);
}
}
foreach (Entry entry in pItem)
{
byte[] data;
if (Utils.SearchLzwEntryList(lzwManifest, entry.Name) != -1)
data = FakeCompress(File.ReadAllBytes(Path.Combine(path, entry.Name)));
else
data = File.ReadAllBytes(Path.Combine(path, entry.Name));
bw.Write(data);
}
byte[] data = File.ReadAllBytes(Path.Combine(path, entry.Name));
bw.Write(data);
}
}
return true;
}
private void ReadPItem(string path)
private void GeneratePItem(string path)
{
pItem.Clear();
//反序列化path上级的json文件
string jsonPath = Path.Combine(Path.GetDirectoryName(path), Path.GetFileNameWithoutExtension(path) + ".json");
if (File.Exists(jsonPath))
pItem = JsonSerializer.Deserialize<List<Entry>>(File.ReadAllText(jsonPath)) ?? [];
else
throw new Exception("No json file found.");
for (int i = 0; i < pItem.Count; i++)
{
pItem[i].Size = (uint)new FileInfo(Path.Combine(path, pItem[i].Name)).Length;
}
//var files = Directory.GetFiles(path, "*", SearchOption.AllDirectories)
// .Where(file => !file.EndsWith("lzwManifest.json", StringComparison.OrdinalIgnoreCase))
// .ToArray();
//foreach (var file in files)
//{
// var relativePath = Path.GetRelativePath(path, file);
// var fileInfo = new FileInfo(file);
// pItem.Add(new Entry
// {
// Name = relativePath,
// Size = (uint)fileInfo.Length
// });
//}
m_count = (uint)pItem.Count;
}
//不压其实也没事
public static byte[] FakeCompress(byte[] Data)
{
using MemoryStream Stream = new();
byte[] Buffer = new byte[4];
((uint)Utils.Reverse((uint)Data.Length)).WriteTo(Buffer, 0);
Stream.WriteCString("acp");
Stream.Write(Buffer, 0, Buffer.Length);
BitWriter Writer = new(Stream);
for (int i = 0; i < Data.Length; i++)
{
if (i > 0 && (i % 0x4000) == 0)
var files = Directory.GetFiles(path, "*", SearchOption.AllDirectories)
.Where(file => !file.EndsWith("lzwManifest.json", StringComparison.OrdinalIgnoreCase))
.ToArray();
foreach (var file in files)
{
Writer.PutBit(true);
Writer.PutBits(2);
var relativePath = Path.GetRelativePath(path, file);
var fileInfo = new FileInfo(file);
pItem.Add(new Entry
{
Name = relativePath,
Size = (uint)fileInfo.Length
});
}
Writer.PutBit(false);
Writer.PutBits(Data[i]);
}
Writer.PutBit(true);
Writer.PutBits(0);
Writer.Flush();
return Stream.ToArray();
m_count = (uint)pItem.Count;
}
}
}

File diff suppressed because it is too large Load Diff

View File

@ -2,7 +2,7 @@
"profiles": {
"EscudeTools": {
"commandName": "Project",
"commandLineArgs": "G:\\x221.local\\archived\\femme_fatale\\output\r\n\"G:\\x221.local\\lab3\\Haison\\output\\ev\\db_graphics.db\""
"commandLineArgs": "G:\\x221.local\\lab3\\Haison\\output\\data"
}
}
}

View File

@ -4,34 +4,6 @@ using System.Text;
namespace EscudeTools
{
public class Const
{
//Types
public const string INT8 = "System.SByte";
public const string UINT8 = "System.Byte";
public const string INT16 = "System.Int16";
public const string UINT16 = "System.UInt16";
public const string INT32 = "System.Int32";
public const string UINT32 = "System.UInt32";
public const string DOUBLE = "System.Double";
public const string FLOAT = "System.Single";
public const string INT64 = "System.Int64";
public const string UINT64 = "System.UInt64";
public const string STRING = "System.String";
public const string DELEGATE = "System.MulticastDelegate";
public const string CHAR = "System.Char";
//Attributes
public const string PSTRING = "PString";
public const string CSTRING = "CString";
public const string UCSTRING = "UCString";
public const string FSTRING = "FString";
public const string STRUCT = "StructField";
public const string IGNORE = "Ignore";
public const string FARRAY = "FArray";
public const string PARRAY = "PArray";
public const string RARRAY = "RArray";
}
public class Utils
{
public static string ReadStringFromTextData(byte[] sheet_text, int offset)
@ -105,7 +77,7 @@ namespace EscudeTools
stream.CopyTo(fileStream);
}
public static string GetSQLiteColumnType(ushort type)
public static string GetSQLiteColumnType(ushort type, uint size)
{
if (type == 1)
return "INTEGER";
@ -192,38 +164,5 @@ namespace EscudeTools
Buffer.BlockCopy(data, src, data, dst, count);
}
}
public static dynamic Reverse(dynamic Data)
{
byte[] Arr = BitConverter.GetBytes(Data);
Array.Reverse(Arr, 0, Arr.Length);
string type = Data.GetType().FullName;
return type switch
{
Const.INT8 or Const.UINT8 => Data,
Const.INT16 => BitConverter.ToInt16(Arr, 0),
Const.UINT16 => BitConverter.ToUInt16(Arr, 0),
Const.INT32 => BitConverter.ToInt32(Arr, 0),
Const.UINT32 => BitConverter.ToUInt32(Arr, 0),
Const.INT64 => BitConverter.ToInt64(Arr, 0),
Const.UINT64 => BitConverter.ToUInt64(Arr, 0),
Const.DOUBLE => BitConverter.ToDouble(Arr, 0),
Const.FLOAT => BitConverter.ToSingle(Arr, 0),
_ => throw new Exception("Unk Data Type."),
};
}
public static void WriteTo(uint Value, byte[] Binary, uint At)
{
BitConverter.GetBytes(Value).CopyTo(Binary, At);
}
public static int SearchLzwEntryList(List<LzwEntry> lle, string keyword)
{
foreach (LzwEntry le in lle)
{
if (le.Name == keyword)
return lle.IndexOf(le);
}
return -1;
}
}
}