From ca7722bb7e365e3ee646b21c3b07f1f5deecf632 Mon Sep 17 00:00:00 2001 From: Chenx221 Date: Sat, 19 Oct 2024 21:49:57 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=AE=8C=E6=95=B4script?= =?UTF-8?q?=E5=AF=BC=E5=87=BA=E6=97=B6=E7=9A=84=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- EscudeTools/Define.cs | 10 +-- EscudeTools/Program.cs | 30 +++---- EscudeTools/Properties/launchSettings.json | 2 +- EscudeTools/ScriptManager.cs | 93 +++++++++++++++------- EscudeTools/Utils.cs | 12 +++ 5 files changed, 98 insertions(+), 49 deletions(-) diff --git a/EscudeTools/Define.cs b/EscudeTools/Define.cs index a7e0570..3ee344a 100644 --- a/EscudeTools/Define.cs +++ b/EscudeTools/Define.cs @@ -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 switch (c.Instruction) @@ -162,12 +162,12 @@ namespace EscudeTools return $"Push a floating-point value"; case INST_PUSH_RET: return $"Push the return value"; - case INST_PUSH_TEXT: + case INST_PUSH_TEXT://并非所有的TEXT都会使用 return $"Push a string: {sf.TextString[BitConverter.ToUInt32(c.Parameter)]}"; - case INST_PUSH_MESS: + case INST_PUSH_MESS://并非所有的MESS都会使用 { messIndex++; - return $"{sm.DataString[messIndex - 1]}"; + return $"{((sm == null) ? "意外的指令,此表无Mess" : sm.DataString[messIndex - 1])}"; } case INST_PUSH_GVAR: return $"Push a global variable"; @@ -244,7 +244,7 @@ namespace EscudeTools return $"Execute built-in function: {ProcNames[index]} {SetExtStr(c, sf)}"; case INST_TEXT: messIndex++; - return sm.DataString[messIndex - 1]; + return (sm == null) ? "意外的指令,此表无Mess" : sm.DataString[messIndex - 1]; default: return "UNKNOWN"; } diff --git a/EscudeTools/Program.cs b/EscudeTools/Program.cs index 1973e6a..8c296ba 100644 --- a/EscudeTools/Program.cs +++ b/EscudeTools/Program.cs @@ -92,23 +92,23 @@ // } //} - ////Export Script Type 1 - //if (File.Exists(args[0])) //fail //lost 1 //something diff - //{ - // ScriptManager.Repackv1(args[0], true); - //} + //Export Full Script + if (File.Exists(args[0])) //fail //lost 1 //something diff + { + ScriptManager.Repackv1(args[0], true); + } - ////Export Script Type 2 - //if (File.Exists(args[0])) //pass - //{ - // ScriptManager.Repackv2(args[0], true); - //} + //Export ScriptMessage + if (File.Exists(args[1])) //pass + { + ScriptManager.Repackv2(args[1], true); + } - ////Export Script Type 3 - //if (File.Exists(args[0])) //pass - //{ - // ScriptManager.Repackv3(args[0]); - //} + ////Export ScriptFile + if (File.Exists(args[2])) //pass + { + ScriptManager.Repackv3(args[2]); + } diff --git a/EscudeTools/Properties/launchSettings.json b/EscudeTools/Properties/launchSettings.json index f32ea79..f48662c 100644 --- a/EscudeTools/Properties/launchSettings.json +++ b/EscudeTools/Properties/launchSettings.json @@ -2,7 +2,7 @@ "profiles": { "EscudeTools": { "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\"" } } } \ No newline at end of file diff --git a/EscudeTools/ScriptManager.cs b/EscudeTools/ScriptManager.cs index 026718e..adb50f2 100644 --- a/EscudeTools/ScriptManager.cs +++ b/EscudeTools/ScriptManager.cs @@ -1,10 +1,6 @@ using Microsoft.Data.Sqlite; -using System.ComponentModel; -using System.Linq; using System.Reflection; -using System.Reflection.Metadata; using System.Text; -using static System.Runtime.InteropServices.JavaScript.JSType; namespace EscudeTools { @@ -115,6 +111,8 @@ namespace EscudeTools } if (sm != null) c.Helper = Define.SetCommandStr(c, sf, sm, ref messIndex); + else + c.Helper = Define.SetCommandStr(c, sf, null, ref messIndex); sf.Commands.Add(c); } return true; @@ -287,10 +285,25 @@ namespace EscudeTools CREATE TABLE {name}__text ( Text TEXT );"; - using (var createTextTableCmd = new SqliteCommand(createTextTableQuery, connection)) - { - createTextTableCmd.ExecuteNonQuery(); - } + using var createTextTableCmd = new SqliteCommand(createTextTableQuery, connection); + 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 return false; @@ -326,6 +339,19 @@ namespace EscudeTools 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(); return true; } @@ -417,7 +443,7 @@ namespace EscudeTools { 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)); } @@ -441,10 +467,6 @@ namespace EscudeTools MessCount = 0, Commands = [] }; - uint Offset = 0; - List messOffset = new(); - List messString = new(); - bool flag = false; // need .001? using (var command = new SqliteCommand($"SELECT * FROM {tableName};", connection)) using (var reader = command.ExecuteReader()) { @@ -457,19 +479,10 @@ namespace EscudeTools }; sf.Commands.Add(c); 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 textString = new(); - List textOffset = new(); + List textString = []; + List textOffset = []; using (var command = new SqliteCommand($"SELECT * FROM {tableName}__text;", connection)) using (var reader = command.ExecuteReader()) { @@ -482,6 +495,30 @@ namespace EscudeTools sf.TextSize += (uint)(shiftJis.GetBytes(s).Length + 1); } } + uint Offset = 0; + List messOffset = []; + List 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(sf.CodeSize);//代码区大小 @@ -618,8 +655,8 @@ namespace EscudeTools Count = 0, Size = 0 }; - List messOffset = new(); - List messString = new(); + List messOffset = []; + List messString = []; using (var command = new SqliteCommand($"SELECT * FROM {tableName};", connection)) using (var reader = command.ExecuteReader()) { @@ -731,8 +768,8 @@ namespace EscudeTools string trunkPath = Path.Combine(Path.GetDirectoryName(sqlitePath), tableName + ".dat"); byte[] bytes = File.ReadAllBytes(trunkPath); uint textSizeOffset = 0x10; - List textOffset = new(); - List textString = new(); + List textOffset = []; + List textString = []; uint Offset = 0; using (var command = new SqliteCommand($"SELECT * FROM {tableName};", connection)) using (var reader = command.ExecuteReader()) diff --git a/EscudeTools/Utils.cs b/EscudeTools/Utils.cs index 811f0fb..ebab346 100644 --- a/EscudeTools/Utils.cs +++ b/EscudeTools/Utils.cs @@ -122,5 +122,17 @@ namespace EscudeTools _ => 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)); + } } }