修复完整script导出时的错误

This commit is contained in:
Chenx221 2024-10-19 21:49:57 +08:00
parent 4ad356f145
commit ca7722bb7e
Signed by: chenx221
GPG Key ID: D7A9EC07024C3021
5 changed files with 98 additions and 49 deletions

View File

@ -138,7 +138,7 @@ namespace EscudeTools
}; };
} }
public static string SetCommandStr(Command c, ScriptFile sf, ScriptMessage sm, ref int messIndex) public static string SetCommandStr(Command c, ScriptFile sf, ScriptMessage? sm, ref int messIndex)
{ {
//__cdecl //__cdecl
switch (c.Instruction) switch (c.Instruction)
@ -162,12 +162,12 @@ namespace EscudeTools
return $"Push a floating-point value"; return $"Push a floating-point value";
case INST_PUSH_RET: case INST_PUSH_RET:
return $"Push the return value"; return $"Push the return value";
case INST_PUSH_TEXT: case INST_PUSH_TEXT://并非所有的TEXT都会使用
return $"Push a string: {sf.TextString[BitConverter.ToUInt32(c.Parameter)]}"; return $"Push a string: {sf.TextString[BitConverter.ToUInt32(c.Parameter)]}";
case INST_PUSH_MESS: case INST_PUSH_MESS://并非所有的MESS都会使用
{ {
messIndex++; messIndex++;
return $"{sm.DataString[messIndex - 1]}"; return $"{((sm == null) ? ",Mess" : sm.DataString[messIndex - 1])}";
} }
case INST_PUSH_GVAR: case INST_PUSH_GVAR:
return $"Push a global variable"; return $"Push a global variable";
@ -244,7 +244,7 @@ namespace EscudeTools
return $"Execute built-in function: {ProcNames[index]} {SetExtStr(c, sf)}"; return $"Execute built-in function: {ProcNames[index]} {SetExtStr(c, sf)}";
case INST_TEXT: case INST_TEXT:
messIndex++; messIndex++;
return sm.DataString[messIndex - 1]; return (sm == null) ? "意外的指令此表无Mess" : sm.DataString[messIndex - 1];
default: default:
return "UNKNOWN"; return "UNKNOWN";
} }

View File

@ -92,23 +92,23 @@
// } // }
//} //}
////Export Script Type 1 //Export Full Script
//if (File.Exists(args[0])) //fail //lost 1 //something diff if (File.Exists(args[0])) //fail //lost 1 //something diff
//{ {
// ScriptManager.Repackv1(args[0], true); ScriptManager.Repackv1(args[0], true);
//} }
////Export Script Type 2 //Export ScriptMessage
//if (File.Exists(args[0])) //pass if (File.Exists(args[1])) //pass
//{ {
// ScriptManager.Repackv2(args[0], true); ScriptManager.Repackv2(args[1], true);
//} }
////Export Script Type 3 ////Export ScriptFile
//if (File.Exists(args[0])) //pass if (File.Exists(args[2])) //pass
//{ {
// ScriptManager.Repackv3(args[0]); ScriptManager.Repackv3(args[2]);
//} }

View File

@ -2,7 +2,7 @@
"profiles": { "profiles": {
"EscudeTools": { "EscudeTools": {
"commandName": "Project", "commandName": "Project",
"commandLineArgs": "\"G:\\x221.local\\lab\\test1\\type3\\script_text.db\"" "commandLineArgs": "\"G:\\x221.local\\lab\\test1\\type1\\script.db\"\r\n\"G:\\x221.local\\lab\\test1\\type2\\script_sm.db\"\r\n\"G:\\x221.local\\lab\\test1\\type3\\script_text.db\""
} }
} }
} }

View File

@ -1,10 +1,6 @@
using Microsoft.Data.Sqlite; using Microsoft.Data.Sqlite;
using System.ComponentModel;
using System.Linq;
using System.Reflection; using System.Reflection;
using System.Reflection.Metadata;
using System.Text; using System.Text;
using static System.Runtime.InteropServices.JavaScript.JSType;
namespace EscudeTools namespace EscudeTools
{ {
@ -115,6 +111,8 @@ namespace EscudeTools
} }
if (sm != null) if (sm != null)
c.Helper = Define.SetCommandStr(c, sf, sm, ref messIndex); c.Helper = Define.SetCommandStr(c, sf, sm, ref messIndex);
else
c.Helper = Define.SetCommandStr(c, sf, null, ref messIndex);
sf.Commands.Add(c); sf.Commands.Add(c);
} }
return true; return true;
@ -287,10 +285,25 @@ namespace EscudeTools
CREATE TABLE {name}__text ( CREATE TABLE {name}__text (
Text TEXT Text TEXT
);"; );";
using (var createTextTableCmd = new SqliteCommand(createTextTableQuery, connection)) using var createTextTableCmd = new SqliteCommand(createTextTableQuery, connection);
{ createTextTableCmd.ExecuteNonQuery();
createTextTableCmd.ExecuteNonQuery(); }
} else
return false;
}
if (sm != null)
{
string checkMessTableExistsQuery = $"SELECT name FROM sqlite_master WHERE type='table' AND name='{name}__mess';";
using var checkMessTableCmd = new SqliteCommand(checkMessTableExistsQuery, connection);
var result = checkMessTableCmd.ExecuteScalar();
if (result == null)
{
string createMessTableQuery = $@"
CREATE TABLE {name}__mess (
Mess TEXT
);";
using var createMessTableCmd = new SqliteCommand(createMessTableQuery, connection);
createMessTableCmd.ExecuteNonQuery();
} }
else else
return false; return false;
@ -326,6 +339,19 @@ namespace EscudeTools
insertCmdSub.ExecuteNonQuery(); insertCmdSub.ExecuteNonQuery();
} }
if (sm != null)
{
string insertQuerySub2 = $"INSERT INTO {name}__mess (Mess) VALUES (@Mess);";
using var insertCmdSub2 = new SqliteCommand(insertQuerySub2, connection, transaction);
foreach (string ds in sm.DataString)
{
insertCmdSub2.Parameters.Clear();
insertCmdSub2.Parameters.AddWithValue("@Mess", ds ?? "");
insertCmdSub2.ExecuteNonQuery();
}
}
transaction.Commit(); transaction.Commit();
return true; return true;
} }
@ -417,7 +443,7 @@ namespace EscudeTools
{ {
while (reader.Read()) while (reader.Read())
{ {
if (!reader.GetString(0).EndsWith("__text")) if (!reader.GetString(0).EndsWith("__text") && !reader.GetString(0).EndsWith("__mess"))
{ {
tableNames.Add(reader.GetString(0)); tableNames.Add(reader.GetString(0));
} }
@ -441,10 +467,6 @@ namespace EscudeTools
MessCount = 0, MessCount = 0,
Commands = [] Commands = []
}; };
uint Offset = 0;
List<uint> messOffset = new();
List<string> messString = new();
bool flag = false; // need .001?
using (var command = new SqliteCommand($"SELECT * FROM {tableName};", connection)) using (var command = new SqliteCommand($"SELECT * FROM {tableName};", connection))
using (var reader = command.ExecuteReader()) using (var reader = command.ExecuteReader())
{ {
@ -457,19 +479,10 @@ namespace EscudeTools
}; };
sf.Commands.Add(c); sf.Commands.Add(c);
sf.CodeSize += 1 + (reader.IsDBNull(3) ? 0 : (uint)((byte[])reader[3]).Length); sf.CodeSize += 1 + (reader.IsDBNull(3) ? 0 : (uint)((byte[])reader[3]).Length);
if (c.Instruction == 41) //INST_TEXT //此方法未必可靠
{
flag = true;
string s = reader.GetString(4);
messString.Add(s);
messOffset.Add(Offset);
Offset += (uint)(shiftJis.GetBytes(s).Length + 1);
sf.MessCount++;
}
} }
} }
List<string> textString = new(); List<string> textString = [];
List<uint> textOffset = new(); List<uint> textOffset = [];
using (var command = new SqliteCommand($"SELECT * FROM {tableName}__text;", connection)) using (var command = new SqliteCommand($"SELECT * FROM {tableName}__text;", connection))
using (var reader = command.ExecuteReader()) using (var reader = command.ExecuteReader())
{ {
@ -482,6 +495,30 @@ namespace EscudeTools
sf.TextSize += (uint)(shiftJis.GetBytes(s).Length + 1); sf.TextSize += (uint)(shiftJis.GetBytes(s).Length + 1);
} }
} }
uint Offset = 0;
List<uint> messOffset = [];
List<string> messString = [];
bool flag = false; // need .001?
using (var command = new SqliteCommand($"SELECT name FROM sqlite_master WHERE type='table' AND name='{tableName}__mess';", connection))
{
using var reader = command.ExecuteReader();
flag = reader.Read();
}
if (flag)
{
using var command = new SqliteCommand($"SELECT * FROM {tableName}__mess;", connection);
using var reader = command.ExecuteReader();
while (reader.Read())
{
string s = reader.GetString(0);
messString.Add(s);
messOffset.Add(Offset);
Offset += (uint)(shiftJis.GetBytes(s).Length + 1);
sf.MessCount++;
}
}
//准备写入 //准备写入
bw.Write(FileHeader);//文件头 bw.Write(FileHeader);//文件头
bw.Write(sf.CodeSize);//代码区大小 bw.Write(sf.CodeSize);//代码区大小
@ -618,8 +655,8 @@ namespace EscudeTools
Count = 0, Count = 0,
Size = 0 Size = 0
}; };
List<uint> messOffset = new(); List<uint> messOffset = [];
List<string> messString = new(); List<string> messString = [];
using (var command = new SqliteCommand($"SELECT * FROM {tableName};", connection)) using (var command = new SqliteCommand($"SELECT * FROM {tableName};", connection))
using (var reader = command.ExecuteReader()) using (var reader = command.ExecuteReader())
{ {
@ -731,8 +768,8 @@ namespace EscudeTools
string trunkPath = Path.Combine(Path.GetDirectoryName(sqlitePath), tableName + ".dat"); string trunkPath = Path.Combine(Path.GetDirectoryName(sqlitePath), tableName + ".dat");
byte[] bytes = File.ReadAllBytes(trunkPath); byte[] bytes = File.ReadAllBytes(trunkPath);
uint textSizeOffset = 0x10; uint textSizeOffset = 0x10;
List<uint> textOffset = new(); List<uint> textOffset = [];
List<string> textString = new(); List<string> textString = [];
uint Offset = 0; uint Offset = 0;
using (var command = new SqliteCommand($"SELECT * FROM {tableName};", connection)) using (var command = new SqliteCommand($"SELECT * FROM {tableName};", connection))
using (var reader = command.ExecuteReader()) using (var reader = command.ExecuteReader())

View File

@ -122,5 +122,17 @@ namespace EscudeTools
_ => throw new NotSupportedException($"Unsupported column Size: {v}"), _ => throw new NotSupportedException($"Unsupported column Size: {v}"),
}; };
} }
public static byte RotByteR (byte v, int count)
{
count &= 7;
return (byte)(v >> count | v << (8-count));
}
public static byte RotByteL (byte v, int count)
{
count &= 7;
return (byte)(v << count | v >> (8-count));
}
} }
} }