From 793fe5e86eeb1a0abaedbf1d9b209e17a4094e9c Mon Sep 17 00:00:00 2001 From: Arneth Date: Wed, 28 May 2025 20:59:34 -0400 Subject: [PATCH] working version --- KgK _alr regenerator/Program.cs | 130 +++++++++++++++---------------- 1 file changed, 63 insertions(+), 67 deletions(-) diff --git a/KgK _alr regenerator/Program.cs b/KgK _alr regenerator/Program.cs index 9c2ddba..01d2e80 100644 --- a/KgK _alr regenerator/Program.cs +++ b/KgK _alr regenerator/Program.cs @@ -1,17 +1,11 @@ -using System; -using System.Drawing; -using System.IO; -using System.Text; -using static Program; -using static System.Net.Mime.MediaTypeNames; -using static System.Runtime.InteropServices.JavaScript.JSType; +using System.Text; class Program { static void Main() { //TEST REMOVE LATER - string[] args = new string[] { "--all", "C:\\Users\\Lauren\\Desktop\\Ken Ga Kimi\\TEST", "C:\\Users\\Lauren\\Desktop\\Ken Ga Kimi\\TEST\\OUTPUT" }; + string[] args = new string[] { "--all", "C:\\Users\\Lauren\\Desktop\\Ken Ga Kimi\\PATCH\\EN Files", "C:\\Users\\Lauren\\Desktop\\Ken Ga Kimi\\PATCH\\EN Files\\OUTPUT" }; if (args.Length < 1) { @@ -187,7 +181,7 @@ class Program offset = RoundUpToNextMultipleOf4(offset); int size = BitConverter.ToInt32(data, offset); offset += 4; - if (data[offset+1] == 255 && data[offset + 2] == 254) { + if (data[offset] == 255 && data[offset + 1] == 254) { offset += 2; } //offset = RoundUpToNextMultipleOf4(offset); @@ -233,84 +227,86 @@ class Program } } List searchStrings = new List() { - "\n@", - "\n", - "\n", + "@", + "", + "", + "", + "", }; - List<(string, int)> stringPositions = FindStringPositions(script.ScriptText, searchStrings); + List<(string, int)> stringPositions = FindStringPositions(script.ScriptText); + List<(string, int)> filteredStringPositions = stringPositions + .Where(pos => searchStrings.Any(s => pos.Item1.Contains(s))) + .ToList(); //Add length of data - newAlr.AddRange(BitConverter.GetBytes(stringPositions.Count * 8)); + newAlr.AddRange(BitConverter.GetBytes(filteredStringPositions.Count * 8)); + if (alr.ScriptTitle == "KEN_00_00_00_alr") + { + Console.WriteLine("Found KEN_00_00_00"); + } //position += 1; for (int i = 0; i < stringPositions.Count; i++) { - newAlr.AddRange(BitConverter.GetBytes(stringPositions[i].Item2)); - newAlr.AddRange(BitConverter.GetBytes(position)); - if (stringPositions[i].Item1 != "\n@") - { - position += 1; - } - else if (stringPositions[i].Item1 == "\n@" && i > 0 && stringPositions[i - 1].Item1 != "\n@") + if (searchStrings.Any(s => stringPositions[i].Item1.Contains(s))) { - //Don't add to position if the previous string was a tag - } - else if (stringPositions.Count == 1) - { - position += 1; + newAlr.AddRange(BitConverter.GetBytes(stringPositions[i].Item2)); + newAlr.AddRange(BitConverter.GetBytes(position)); + var isNormalTag = searchStrings.Any(t => stringPositions[i].Item1.Contains(t)); + if (stringPositions[i].Item1.Contains(" FindStringPositions(byte[] scriptBytes, IEnumerable searchStrings) + public static List<(string SearchString, int Position)> FindStringPositions(byte[] scriptBytes) { var results = new List<(string, int)>(); - // Decode the script bytes to string for line analysis string scriptText = Encoding.Unicode.GetString(scriptBytes); - foreach (var search in searchStrings) + // 1. Find all <...> tags not commented out + var tagRegex = new System.Text.RegularExpressions.Regex(@"<[^>\r\n]+>"); + foreach (System.Text.RegularExpressions.Match match in tagRegex.Matches(scriptText)) { - byte[] searchBytes = Encoding.Unicode.GetBytes(search); - int index = 0; - while (index <= scriptBytes.Length - searchBytes.Length) + if (match.Value == "") continue; // Skip tags + + int lineStart = scriptText.LastIndexOf('\n', match.Index); + if (lineStart == -1) lineStart = 0; else lineStart++; + int lineEnd = scriptText.IndexOf('\n', match.Index); + if (lineEnd == -1) lineEnd = scriptText.Length; + string line = scriptText.Substring(lineStart, lineEnd - lineStart).TrimStart(); + + if (!line.StartsWith("//")) { - bool match = true; - for (int j = 0; j < searchBytes.Length; j++) - { - if (scriptBytes[index + j] != searchBytes[j]) - { - match = false; - break; - } - } - if (match) - { - // Find the character index in the string corresponding to this byte index - int charIndex = Encoding.Unicode.GetString(scriptBytes, 0, index).Length; - // Find the start of the line - int lineStart = scriptText.LastIndexOf('\n', charIndex - 1); - if (lineStart == -1) lineStart = 0; else lineStart++; - int lineEnd = scriptText.IndexOf('\n', charIndex); - if (lineEnd == -1) lineEnd = scriptText.Length; - string line = scriptText.Substring(lineStart, lineEnd - lineStart).TrimStart(); - - // Only add if the line does NOT start with // - if (!line.StartsWith("//")) - { - results.Add((search, index)); - } - index += searchBytes.Length; // Move past this match - } - else - { - index++; - } + int byteIndex = Encoding.Unicode.GetByteCount(scriptText.Substring(0, match.Index)); + results.Add((match.Value, byteIndex)); + } + } + + // 2. Find all lines that start with @ and are not commented out + int currentIndex = 0; + foreach (var line in scriptText.Split('\n')) + { + string trimmedLine = line.TrimStart(); + if (!trimmedLine.StartsWith("//") && trimmedLine.StartsWith("@")) + { + int byteIndex = Encoding.Unicode.GetByteCount(scriptText.Substring(0, currentIndex)); + results.Add((line, byteIndex)); } + currentIndex += line.Length + 1; // +1 for '\n' } - // Sort by position in order of appearance return results.OrderBy(r => r.Item2).ToList(); }