|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Text; |
| 5 | +using System.Text.RegularExpressions; |
| 6 | +using TagTool.Cache; |
| 7 | +using TagTool.Commands.Common; |
| 8 | +using TagTool.Tags.Definitions; |
| 9 | + |
| 10 | +namespace TagTool.Commands.Scenarios |
| 11 | +{ |
| 12 | + class PatchScriptsCommand : Command |
| 13 | + { |
| 14 | + private GameCache Cache { get; } |
| 15 | + private Scenario Definition { get; } |
| 16 | + |
| 17 | + public PatchScriptsCommand(GameCache cache, Scenario definition) : |
| 18 | + base(true, |
| 19 | + |
| 20 | + "PatchScripts", |
| 21 | + "Patch a script file by substituting function names for compatibility.", |
| 22 | + |
| 23 | + "PatchScripts <input_file> <output_file> [flags]", |
| 24 | + |
| 25 | + "Performs string-level substitutions on an uncompiled script file to make it\n" + |
| 26 | + "compatible with this version of the game. Optionally accepts an integer flags\n" + |
| 27 | + "value to enable or disable specific substitution groups.") |
| 28 | + { |
| 29 | + Cache = cache; |
| 30 | + Definition = definition; |
| 31 | + } |
| 32 | + |
| 33 | + public override object Execute(List<string> args) |
| 34 | + { |
| 35 | + if (args.Count < 2 || args.Count > 3) |
| 36 | + return new TagToolError(CommandError.ArgCount); |
| 37 | + |
| 38 | + string inputPath = args[0]; |
| 39 | + string outputPath = args[1]; |
| 40 | + |
| 41 | + int flags = ~0; // all flags set by default |
| 42 | + if (args.Count == 3) |
| 43 | + { |
| 44 | + if (!int.TryParse(args[2], out flags)) |
| 45 | + return new TagToolError(CommandError.ArgInvalid, $"Flags must be an integer, got '{args[2]}'."); |
| 46 | + } |
| 47 | + |
| 48 | + var inputFile = new FileInfo(inputPath); |
| 49 | + if (!inputFile.Exists) |
| 50 | + return new TagToolError(CommandError.FileNotFound, $"\"{inputPath}\""); |
| 51 | + |
| 52 | + string source = File.ReadAllText(inputPath); |
| 53 | + string patched = ApplyPatches(source, flags); |
| 54 | + File.WriteAllText(outputPath, patched, new UTF8Encoding(false)); |
| 55 | + |
| 56 | + Console.WriteLine($"Done. Patched script written to \"{outputPath}\"."); |
| 57 | + return true; |
| 58 | + } |
| 59 | + |
| 60 | + // Each entry: (requiredFlags, pattern, replacement) |
| 61 | + // requiredFlags - bitmask checked against the flags arg; 0 means always apply |
| 62 | + // pattern - the exact function name (or multi-line block) to find |
| 63 | + // replacement - what to replace it with |
| 64 | + // |
| 65 | + // For simple one-to-one function renames, pattern and replacement are just the names. |
| 66 | + // For block substitutions, write them out in full including parens/whitespace. |
| 67 | + // |
| 68 | + // Simple rename example: |
| 69 | + // (0, "old_function_name", "new_function_name"), |
| 70 | + // |
| 71 | + // Block substitution example: |
| 72 | + // (0, "(old_function_x)", "(new_function_x)\n (new_function_y)"), |
| 73 | + private static readonly (int flags, string pattern, string replacement)[] Patches = |
| 74 | + [ |
| 75 | + // h3 -> ed (odst) change |
| 76 | + (1, "cinematic_scripting_start_animation", |
| 77 | + "cinematic_scripting_start_animation_legacy"), |
| 78 | + |
| 79 | + (1, "cinematic_scripting_create_object", |
| 80 | + "cinematic_scripting_create_object_legacy"), |
| 81 | + |
| 82 | + (1, "cinematic_scripting_create_and_animate_object", |
| 83 | + "cinematic_scripting_create_and_animate_object_legacy"), |
| 84 | + |
| 85 | + (1, "cinematic_scripting_create_and_animate_object_no_animation", |
| 86 | + "cinematic_scripting_create_and_animate_object_no_animation_legacy"), |
| 87 | + |
| 88 | + (1, "cinematic_scripting_create_and_animate_cinematic_object", |
| 89 | + "cinematic_scripting_create_and_animate_cinematic_object_legacy"), |
| 90 | + |
| 91 | + (1, "cinematic_scripting_destroy_object", |
| 92 | + "cinematic_scripting_destroy_object_legacy"), |
| 93 | + |
| 94 | + // h3 -> odst change |
| 95 | + (1, "cinematic_object_get_unit", |
| 96 | + "cinematic_object_get"), |
| 97 | + |
| 98 | + (1, "cinematic_object_get_scenery", |
| 99 | + "cinematic_object_get"), |
| 100 | + |
| 101 | + (1, "cinematic_object_get_effect_scenery", |
| 102 | + "cinematic_object_get"), |
| 103 | + |
| 104 | + // h3 -> odst change |
| 105 | + (1, "vehicle_test_seat_list", |
| 106 | + "vehicle_test_seat_unit_list"), |
| 107 | + |
| 108 | + (1, "vehicle_test_seat", |
| 109 | + "vehicle_test_seat_unit"), |
| 110 | + |
| 111 | + // h3 -> ed (odst) change |
| 112 | + (4, "vehicle_test_seat_list", |
| 113 | + "vehicle_test_seat_list_legacy"), |
| 114 | + |
| 115 | + (4, "vehicle_test_seat", |
| 116 | + "vehicle_test_seat_legacy"), |
| 117 | + |
| 118 | + // h3/osdt -> ms23 change |
| 119 | + (0, "player_action_test_cinematic_skip", |
| 120 | + "player_action_test_accept"), |
| 121 | + |
| 122 | + (0, "player_action_test_start", |
| 123 | + "player_action_test_accept"), |
| 124 | + |
| 125 | + ]; |
| 126 | + |
| 127 | + private static string ApplyPatches(string source, int flags) |
| 128 | + { |
| 129 | + foreach (var (patchFlags, pattern, replacement) in Patches) |
| 130 | + { |
| 131 | + // skip if the required flags aren't set (0 means always apply) |
| 132 | + if (patchFlags != 0 && (flags & patchFlags) == 0) |
| 133 | + continue; |
| 134 | + |
| 135 | + // For simple identifiers (no parens/newlines in the pattern), use a |
| 136 | + // word-boundary match so we don't clip substrings of longer names. |
| 137 | + // For block patterns, do a literal string replace. |
| 138 | + if (IsIdentifier(pattern)) |
| 139 | + source = ReplaceIdentifier(source, pattern, replacement); |
| 140 | + else |
| 141 | + source = source.Replace(pattern, replacement); |
| 142 | + } |
| 143 | + |
| 144 | + return source; |
| 145 | + } |
| 146 | + |
| 147 | + // Replaces whole identifier occurrences only - won't match if the name is |
| 148 | + // immediately preceded or followed by another identifier character. |
| 149 | + private static string ReplaceIdentifier(string source, string name, string replacement) |
| 150 | + { |
| 151 | + // Identifier chars: letters, digits, underscore |
| 152 | + var sb = new StringBuilder(source.Length); |
| 153 | + int i = 0; |
| 154 | + while (i < source.Length) |
| 155 | + { |
| 156 | + if (source[i] == name[0] && source.Length - i >= name.Length && |
| 157 | + source.Substring(i, name.Length) == name) |
| 158 | + { |
| 159 | + bool prevOk = i == 0 || !IsIdentifierChar(source[i - 1]); |
| 160 | + bool nextOk = i + name.Length == source.Length || !IsIdentifierChar(source[i + name.Length]); |
| 161 | + |
| 162 | + if (prevOk && nextOk) |
| 163 | + { |
| 164 | + sb.Append(replacement); |
| 165 | + i += name.Length; |
| 166 | + continue; |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + sb.Append(source[i++]); |
| 171 | + } |
| 172 | + |
| 173 | + return sb.ToString(); |
| 174 | + } |
| 175 | + |
| 176 | + private static bool IsIdentifier(string s) |
| 177 | + { |
| 178 | + foreach (char c in s) |
| 179 | + if (!IsIdentifierChar(c)) return false; |
| 180 | + return s.Length > 0; |
| 181 | + } |
| 182 | + |
| 183 | + private static bool IsIdentifierChar(char c) => |
| 184 | + char.IsLetterOrDigit(c) || c == '_'; |
| 185 | + } |
| 186 | +} |
0 commit comments