Compare commits

..

3 Commits

Author SHA1 Message Date
ea52c2128b
update 2024-10-27 13:37:38 +08:00
3adf8c207a
重新支持repack ESC-ARC bin
这里lzw FakeCompress来自marcussacana的EscudeEditor
*不用lzw压缩也没事,问题出在文件顺序上
2024-10-27 11:59:46 +08:00
390a4614b3
update 2024-10-26 22:58:01 +08:00
8 changed files with 992 additions and 573 deletions

View File

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

View File

@ -0,0 +1,71 @@
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

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

View File

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

File diff suppressed because it is too large Load Diff

View File

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

View File

@ -4,6 +4,34 @@ using System.Text;
namespace EscudeTools 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 class Utils
{ {
public static string ReadStringFromTextData(byte[] sheet_text, int offset) public static string ReadStringFromTextData(byte[] sheet_text, int offset)
@ -77,7 +105,7 @@ namespace EscudeTools
stream.CopyTo(fileStream); stream.CopyTo(fileStream);
} }
public static string GetSQLiteColumnType(ushort type, uint size) public static string GetSQLiteColumnType(ushort type)
{ {
if (type == 1) if (type == 1)
return "INTEGER"; return "INTEGER";
@ -164,5 +192,38 @@ namespace EscudeTools
Buffer.BlockCopy(data, src, data, dst, count); 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;
}
} }
} }