修正command help显示,初步支持sqlite导出command表

但别指望完全准确
This commit is contained in:
Chenx221 2024-10-15 22:49:43 +08:00
parent 4e9f6ad2a9
commit 767fce3dd9
3 changed files with 86 additions and 111 deletions

View File

@ -165,7 +165,7 @@ namespace EscudeTools
case INST_PUSH_MESS: case INST_PUSH_MESS:
{ {
messIndex++; messIndex++;
return $"Push a string: {sm.DataString[messIndex - 1]}"; return $"{sm.DataString[messIndex - 1]}";
} }
case INST_PUSH_GVAR: case INST_PUSH_GVAR:
return $"Push a global variable"; return $"Push a global variable";
@ -807,10 +807,19 @@ namespace EscudeTools
private static void SetExtStr1(string[] ps, ScriptFile sf) private static void SetExtStr1(string[] ps, ScriptFile sf)
{ {
int index = sf.Commands.Count - 2; int i = sf.Commands.Count - 2;
for (int k = 0; k < ps.Length; k++) for (int k = 0; k < ps.Length; k++)
{ {
sf.Commands[index--].Helper += $": {ps[k]}"; if (sf.Commands[i].IsProcSet || sf.Commands[i].Instruction < 4 || sf.Commands[i].Instruction > 10)
{
k--;
i--;
continue;
}
sf.Commands[i].Helper += $": {ps[k]}";
sf.Commands[i].IsProcSet = true;
i--;
} }
} }
} }

View File

@ -6,8 +6,7 @@
{ {
ScriptManager smr = new(); ScriptManager smr = new();
smr.LoadScriptFile(args[0]); //加载.bin文件 smr.LoadScriptFile(args[0]); //加载.bin文件
//if(args.Length == 2) //不再需要,因为会根据需求自动加载配套.001文件 smr.ExportDatabase(Path.GetDirectoryName(args[0]));
// smr.LoadScriptMess(args[1]);
return; return;

View File

@ -1,5 +1,7 @@
using System.Reflection; using Microsoft.Data.Sqlite;
using System.Reflection;
using System.Text; using System.Text;
using System.Xml.Linq;
namespace EscudeTools namespace EscudeTools
{ {
@ -34,6 +36,7 @@ namespace EscudeTools
public string InstructionString { get; set; } public string InstructionString { get; set; }
public List<Object> Parameter { get; set; } public List<Object> Parameter { get; set; }
public String Helper { get; set; } public String Helper { get; set; }
public bool IsProcSet { get; set; }
} }
public class ScriptManager public class ScriptManager
@ -42,9 +45,8 @@ namespace EscudeTools
static readonly byte[] FileHeader = [0x40, 0x63, 0x6F, 0x64, 0x65, 0x3A, 0x5F, 0x5F]; //@code:__ static readonly byte[] FileHeader = [0x40, 0x63, 0x6F, 0x64, 0x65, 0x3A, 0x5F, 0x5F]; //@code:__
private ScriptMessage sm; private ScriptMessage sm;
private bool smEncrypted; private bool smEncrypted;
private string smName = string.Empty; private string name = string.Empty;
private ScriptFile sf; private ScriptFile sf;
private string sfName = string.Empty;
private int messIndex = 0; private int messIndex = 0;
private bool enableCommandHelper = true; private bool enableCommandHelper = true;
@ -69,7 +71,7 @@ namespace EscudeTools
if (!File.Exists(path)) if (!File.Exists(path))
return false; return false;
sf ??= new ScriptFile(); sf ??= new ScriptFile();
sfName = Path.GetFileNameWithoutExtension(path); name = Path.GetFileNameWithoutExtension(path);
using FileStream fs = new(path, FileMode.Open); using FileStream fs = new(path, FileMode.Open);
using BinaryReader br = new(fs); using BinaryReader br = new(fs);
byte[] head = br.ReadBytes(8); byte[] head = br.ReadBytes(8);
@ -106,7 +108,8 @@ namespace EscudeTools
{ {
Parameter = [], Parameter = [],
Instruction = sf.Code[i++], Instruction = sf.Code[i++],
Offset = (uint)i Offset = (uint)i,
IsProcSet = false
}; };
if (enableCommandHelper) if (enableCommandHelper)
{ {
@ -137,7 +140,6 @@ namespace EscudeTools
if (!File.Exists(path)) if (!File.Exists(path))
return false; return false;
sm ??= new ScriptMessage(); sm ??= new ScriptMessage();
smName = Path.GetFileNameWithoutExtension(path);
using FileStream fs = new(path, FileMode.Open); using FileStream fs = new(path, FileMode.Open);
using BinaryReader br = new(fs); using BinaryReader br = new(fs);
byte[] head = br.ReadBytes(8); byte[] head = br.ReadBytes(8);
@ -228,48 +230,80 @@ namespace EscudeTools
stream.CopyTo(fileStream); stream.CopyTo(fileStream);
} }
public bool ExportDatabase(int outputType, string? storePath) public bool ExportDatabase(string? storePath)
{ {
if (sf.Code == null)
return false;
storePath ??= Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? throw new InvalidOperationException("Unable to determine the directory."); //导出位置 storePath ??= Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? throw new InvalidOperationException("Unable to determine the directory."); //导出位置
throw new NotImplementedException(); if (string.IsNullOrEmpty(name))
//switch (outputType) return false;
//{ string targetPath = Path.Combine(storePath, "script.db");
// case 0: //sf if (!File.Exists(targetPath))
// if (string.IsNullOrEmpty(sfName)) ExtractEmbeddedDatabase(targetPath);
// return false; return SqliteProcess(sf, targetPath);
// ExtractEmbeddedDatabase(Path.Combine(storePath, sfName + ".db"));
// return SqliteProcess(sf, Path.Combine(storePath, sfName + ".db"));
// case 1: //sm
// if (string.IsNullOrEmpty(smName))
// return false;
// ExtractEmbeddedDatabase(Path.Combine(storePath, smName + ".db"));
// return SqliteProcess(sm, Path.Combine(storePath, smName + ".db"));
// default:
// throw new NotSupportedException("Unsupported output type.");
//}
} }
public bool ExportMessDatabase(int outputType, string? storePath) public bool ExportMessDatabase(string? storePath)
{ {
if (sm.Data == null)
return false;
storePath ??= Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? throw new InvalidOperationException("Unable to determine the directory."); //导出位置 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");
if (!File.Exists(targetPath))
ExtractEmbeddedDatabase(targetPath);
//return SqliteProcess(sm, targetPath);
throw new NotImplementedException(); throw new NotImplementedException();
//switch (outputType)
//{
// case 0: //sf
// if (string.IsNullOrEmpty(sfName))
// return false;
// ExtractEmbeddedDatabase(Path.Combine(storePath, sfName + ".db"));
// return SqliteProcess(sf, Path.Combine(storePath, sfName + ".db"));
// case 1: //sm
// if (string.IsNullOrEmpty(smName))
// return false;
// ExtractEmbeddedDatabase(Path.Combine(storePath, smName + ".db"));
// return SqliteProcess(sm, Path.Combine(storePath, smName + ".db"));
// default:
// throw new NotSupportedException("Unsupported output type.");
//}
} }
private bool SqliteProcess(ScriptFile sf, string path)
{
using SqliteConnection connection = new($"Data Source={path};");
connection.Open();
string checkTableExistsQuery = $"SELECT name FROM sqlite_master WHERE type='table' AND name='{name}';";
using (var checkTableCmd = new SqliteCommand(checkTableExistsQuery, connection))
{
var result = checkTableCmd.ExecuteScalar();
if (result != null)
return false;
}
string createTableQuery = $@"
CREATE TABLE {name} (
Offset INTEGER,
Instruction INTEGER,
InstructionString TEXT,
Helper TEXT
);";
using (var createTableCmd = new SqliteCommand(createTableQuery, connection))
{
createTableCmd.ExecuteNonQuery();
}
string insertQuery = $"INSERT INTO {name} (Offset, Instruction, InstructionString, Helper) VALUES (@Offset, @Instruction, @InstructionString, @Helper);";
using (var insertCmd = new SqliteCommand(insertQuery, connection))
{
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.ExecuteNonQuery();
}
}
return true;
}
//下面的没法用
//private bool SqliteProcess(ScriptMessage sm, string path) //private bool SqliteProcess(ScriptMessage sm, string path)
//{ //{
// using SqliteConnection connection = new($"Data Source={path};"); // using SqliteConnection connection = new($"Data Source={path};");
@ -333,73 +367,6 @@ namespace EscudeTools
// return true; // return true;
//} //}
//private bool SqliteProcess(ScriptFile sf, string path)
//{
// //db含有多个sheet每个sheet中col存放标题对应数据库中应该是字段records存放数据对应数据库中应该是记录
// using SqliteConnection connection = new($"Data Source={path};");
// connection.Open();
// foreach (var sheet in db)
// {
// using (SqliteCommand createTableCommand = connection.CreateCommand())
// {
// StringBuilder createTableQuery = new();
// createTableQuery.Append($"CREATE TABLE IF NOT EXISTS {sheet.name} (");
// // Add columns to the create table query
// foreach (var column in sheet.col)
// {
// createTableQuery.Append($"{column.name} {GetSQLiteColumnType(column.type)}, ");
// }
// createTableQuery.Remove(createTableQuery.Length - 2, 2); // Remove the last comma and space
// createTableQuery.Append(");");
// createTableCommand.CommandText = createTableQuery.ToString();
// createTableCommand.ExecuteNonQuery();
// }
// using SqliteCommand insertDataCommand = connection.CreateCommand();
// StringBuilder insertDataQuery = new();
// insertDataQuery.Append($"INSERT INTO {sheet.name} (");
// // Add column names to the insert data query
// foreach (var column in sheet.col)
// {
// insertDataQuery.Append($"{column.name}, ");
// }
// 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;
//}
private static string GetSQLiteColumnType(ushort type) private static string GetSQLiteColumnType(ushort type)
{ {
return type switch return type switch