解析&导出功能优化

This commit is contained in:
Chenx221 2024-10-16 01:42:42 +08:00
parent 767fce3dd9
commit cd926e361e
422 changed files with 104 additions and 86 deletions

View File

@ -146,6 +146,8 @@ namespace EscudeTools
public static string SetCommandStr(Command c, ScriptFile sf, ScriptMessage sm, ref int messIndex)
{
//__cdecl
//todo
//可考虑对pop进行进一步处理
switch (c.Instruction)
{
case INST_POP:
@ -161,7 +163,7 @@ namespace EscudeTools
case INST_PUSH_RET:
return $"Push the return value";
case INST_PUSH_TEXT:
return $"Push a string: {sf.TextString[(uint)c.Parameter[0]]}";
return $"Push a string: {sf.TextString[(uint)c.Parameter]}";
case INST_PUSH_MESS:
{
messIndex++;
@ -184,7 +186,7 @@ namespace EscudeTools
case INST_JMPZ:
return $"Conditional jump";
case INST_CALL:
return $"Call function offset: {(uint)c.Parameter[0] + 1}";
return $"Call function offset: {(uint)c.Parameter + 1}";
case INST_RET:
return $"Return";
case INST_LOG_OR:
@ -238,7 +240,7 @@ namespace EscudeTools
case INST_LINE:
return $"File line number";
case INST_PROC:
uint index = (uint)c.Parameter[0];
uint index = (uint)c.Parameter;
return $"Execute built-in function: {ProcNames[index]} {SetExtStr(c, sf)}";
case INST_TEXT:
messIndex++;
@ -250,7 +252,7 @@ namespace EscudeTools
private static string SetExtStr(Command c, ScriptFile sf)
{
switch ((uint)c.Parameter[0])
switch ((uint)c.Parameter)
{
case 0:
{

View File

@ -4,10 +4,50 @@
{
static void Main(string[] args)
{
ScriptManager smr = new();
smr.LoadScriptFile(args[0]); //加载.bin文件
smr.ExportDatabase(Path.GetDirectoryName(args[0]));
return;
if (Directory.Exists(args[0]))
{
string[] files = Directory.GetFiles(args[0], "*.bin");
foreach (string file in files)
{
ScriptManager smr = new();
//目前不支持二次加载
//Todo
//修复
if (smr.LoadScriptFile(file))
{
Console.WriteLine($"Load {file} Success");
}
else
{
Console.WriteLine($"Load {file} Failed");
return;
}
if (smr.ExportDatabase(Path.GetDirectoryName(args[0])))
Console.WriteLine("Export Database Success");
else
{
Console.WriteLine("Export Database Failed");
return;
}
if (smr.ExportMessDatabase(Path.GetDirectoryName(args[0])))
Console.WriteLine("Export Mess Database Success");
else
{
Console.WriteLine("Export Mess Database Failed");
return;
}
}
}
//ScriptManager smr = new();
//smr.LoadScriptFile(args[0]); //加载.bin文件
//smr.ExportDatabase(Path.GetDirectoryName(args[0]));
//smr.ExportMessDatabase(Path.GetDirectoryName(args[0]));
//return;
//if (Directory.Exists(args[0]))

View File

@ -2,7 +2,7 @@
"profiles": {
"EscudeTools": {
"commandName": "Project",
"commandLineArgs": "G:\\x221.local\\lab\\a_prologue_01.bin \r\nG:\\x221.local\\lab\\a_prologue_01.001"
"commandLineArgs": "G:\\x221.local\\lab\\script"
}
}
}

View File

@ -34,7 +34,7 @@ namespace EscudeTools
public uint Offset { get; set; }
public byte Instruction { get; set; }
public string InstructionString { get; set; }
public List<Object> Parameter { get; set; }
public Object Parameter { get; set; }
public String Helper { get; set; }
public bool IsProcSet { get; set; }
}
@ -70,7 +70,10 @@ namespace EscudeTools
{
if (!File.Exists(path))
return false;
sf ??= new ScriptFile();
sf = new ScriptFile();
smEncrypted = false;
name = string.Empty;
messIndex = 0;
name = Path.GetFileNameWithoutExtension(path);
using FileStream fs = new(path, FileMode.Open);
using BinaryReader br = new(fs);
@ -106,7 +109,6 @@ namespace EscudeTools
{
Command c = new()
{
Parameter = [],
Instruction = sf.Code[i++],
Offset = (uint)i,
IsProcSet = false
@ -116,7 +118,7 @@ namespace EscudeTools
c.InstructionString = Define.GetInstructionString(c.Instruction, out int paramNum);
for (int j = 0; j < paramNum; j++)
{
c.Parameter.Add(Define.TyperHelper(c.Instruction, sf.Code, i));
c.Parameter = Define.TyperHelper(c.Instruction, sf.Code, i);
i += 4;
}
if (sm != null)
@ -139,7 +141,7 @@ namespace EscudeTools
{
if (!File.Exists(path))
return false;
sm ??= new ScriptMessage();
sm = new ScriptMessage();
using FileStream fs = new(path, FileMode.Open);
using BinaryReader br = new(fs);
byte[] head = br.ReadBytes(8);
@ -245,16 +247,17 @@ namespace EscudeTools
public bool ExportMessDatabase(string? storePath)
{
if (sm.Data == null)
if (sf == null)
return false;
if (sm == null)
return true;
storePath ??= Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? throw new InvalidOperationException("Unable to determine the directory."); //导出位置
if (string.IsNullOrEmpty(name))
return false;
string targetPath = Path.Combine(storePath, name + "script_sm.db");
string targetPath = Path.Combine(storePath, "script_sm.db");
if (!File.Exists(targetPath))
ExtractEmbeddedDatabase(targetPath);
//return SqliteProcess(sm, targetPath);
throw new NotImplementedException();
return SqliteProcess(sm, targetPath);
}
private bool SqliteProcess(ScriptFile sf, string path)
@ -268,7 +271,7 @@ namespace EscudeTools
{
var result = checkTableCmd.ExecuteScalar();
if (result != null)
return false;
return true;
}
string createTableQuery = $@"
@ -276,6 +279,7 @@ namespace EscudeTools
Offset INTEGER,
Instruction INTEGER,
InstructionString TEXT,
Parameter TEXT,
Helper TEXT
);";
@ -284,88 +288,60 @@ namespace EscudeTools
createTableCmd.ExecuteNonQuery();
}
string insertQuery = $"INSERT INTO {name} (Offset, Instruction, InstructionString, Helper) VALUES (@Offset, @Instruction, @InstructionString, @Helper);";
string insertQuery = $"INSERT INTO {name} (Offset, Instruction, InstructionString,Parameter, Helper) VALUES (@Offset, @Instruction, @InstructionString,@Parameter, @Helper);";
using (var insertCmd = new SqliteCommand(insertQuery, connection))
using var insertCmd = new SqliteCommand(insertQuery, connection);
foreach (var command in sf.Commands)
{
foreach (var command in sf.Commands)
{
insertCmd.Parameters.Clear();
insertCmd.Parameters.AddWithValue("@Offset", command.Offset);
insertCmd.Parameters.AddWithValue("@Instruction", command.Instruction);
insertCmd.Parameters.AddWithValue("@InstructionString", command.InstructionString);
insertCmd.Parameters.AddWithValue("@Helper", command.Helper);
insertCmd.Parameters.Clear();
insertCmd.Parameters.AddWithValue("@Offset", command.Offset);
insertCmd.Parameters.AddWithValue("@Instruction", command.Instruction);
insertCmd.Parameters.AddWithValue("@InstructionString", command.InstructionString);
insertCmd.Parameters.AddWithValue("@Parameter", command.Parameter == null ? "" : command.Parameter.ToString());
insertCmd.Parameters.AddWithValue("@Helper", command.Helper ?? "");
insertCmd.ExecuteNonQuery();
}
insertCmd.ExecuteNonQuery();
}
return true;
}
//下面的没法用
//private bool SqliteProcess(ScriptMessage sm, string path)
//{
// using SqliteConnection connection = new($"Data Source={path};");
// connection.Open();
private bool SqliteProcess(ScriptMessage sm, string path)
{
using SqliteConnection connection = new($"Data Source={path};");
connection.Open();
// using (SqliteCommand createTableCommand = connection.CreateCommand())
// {
// StringBuilder createTableQuery = new();
// createTableQuery.Append($"CREATE TABLE IF NOT EXISTS {smName} (");
string checkTableExistsQuery = $"SELECT name FROM sqlite_master WHERE type='table' AND name='{name}';";
// // Add columns to the create table query
// foreach (var column in sheet.col)
// {
// createTableQuery.Append($"{column.name} {GetSQLiteColumnType(column.type)}, ");
// }
using (var checkTableCmd = new SqliteCommand(checkTableExistsQuery, connection))
{
var result = checkTableCmd.ExecuteScalar();
if (result != null)
return true;
}
// createTableQuery.Remove(createTableQuery.Length - 2, 2); // Remove the last comma and space
// createTableQuery.Append(");");
string createTableQuery = $@"
CREATE TABLE {name} (
DataString TEXT
);";
// createTableCommand.CommandText = createTableQuery.ToString();
// createTableCommand.ExecuteNonQuery();
// }
using (var createTableCmd = new SqliteCommand(createTableQuery, connection))
{
createTableCmd.ExecuteNonQuery();
}
// using SqliteCommand insertDataCommand = connection.CreateCommand();
// StringBuilder insertDataQuery = new();
// insertDataQuery.Append($"INSERT INTO {sheet.name} (");
string insertQuery = $"INSERT INTO {name} (DataString) VALUES (@DataString);";
// // Add column names to the insert data query
// foreach (var column in sheet.col)
// {
// insertDataQuery.Append($"{column.name}, ");
// }
using var insertCmd = new SqliteCommand(insertQuery, connection);
foreach (var ds in sm.DataString)
{
insertCmd.Parameters.Clear();
insertCmd.Parameters.AddWithValue("@DataString", ds ?? "");
insertCmd.ExecuteNonQuery();
}
// insertDataQuery.Remove(insertDataQuery.Length - 2, 2); // Remove the last comma and space
// insertDataQuery.Append(") VALUES (");
// // Add parameter placeholders to the insert data query
// for (int i = 0; i < sheet.cols; i++)
// {
// insertDataQuery.Append($"@param{i}, ");
// }
// insertDataQuery.Remove(insertDataQuery.Length - 2, 2); // Remove the last comma and space
// insertDataQuery.Append(");");
// insertDataCommand.CommandText = insertDataQuery.ToString();
// // Add data parameters to the insert data command
// for (int i = 0; i < sheet.records.values.Length; i++)
// {
// var record = (Record)sheet.records.values[i];
// for (int j = 0; j < sheet.cols; j++)
// {
// var parameter = new SqliteParameter($"@param{j}", record.values[j]);
// insertDataCommand.Parameters.Add(parameter);
// }
// insertDataCommand.ExecuteNonQuery();
// insertDataCommand.Parameters.Clear();
// }
// return true;
//}
return true;
}
private static string GetSQLiteColumnType(ushort type)
{

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Some files were not shown because too many files have changed in this diff Show More