1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
| using System.Collections.Generic; using System.IO; using System.Linq; using UnityEditor; using UnityEngine; using LuaInterface; using LuaFramework; using System;
public class LanguageFilesBatchComparer : EditorWindow { private Dictionary<string, List<LanguageFileInfo>> fileGroups = new Dictionary<string, List<LanguageFileInfo>>(); private Vector2 scrollPosition; private string comparisonResult = ""; private static string folderPath, defaultPath = "Assets/_GameCenter/ClientLua/Model/Language";
private static LuaState lua { get; set; } private static bool isStarted;
private class LanguageFileInfo { public string FileName; public string FullPath; public List<string> Keys; }
private static void Initialize() { if (isStarted && lua != null) return; var loader = ROYAL_EMPIRE_GAMES_LuaLoader.inst; lua = new LuaState(); OpenCJson(); lua.LuaSetTop(0); LuaBinder.Bind(lua); lua.AddSearchPath(ROYAL_EMPIRE_GAMES_AppConst.ClientLuaRoot); lua.AddSearchPath(ROYAL_EMPIRE_GAMES_AppConst.FrameworkRoot + "/ToLua/Lua"); lua.Start(); isStarted = true; }
private static void OpenCJson() { lua.LuaGetField(LuaIndexes.LUA_REGISTRYINDEX, "_LOADED"); lua.OpenLibs(LuaDLL.luaopen_cjson); lua.LuaSetField(-2, "cjson"); lua.OpenLibs(LuaDLL.luaopen_cjson_safe); lua.LuaSetField(-2, "cjson.safe"); }
[MenuItem("Tools/多语言文件批量对比")] public static void ShowWindow() { GetWindow<LanguageFilesBatchComparer>("Language Files Batch Comparer"); }
private void OnGUI() { GUILayout.Label("Language Files Comparison Tool", EditorStyles.boldLabel); EditorGUILayout.Space(); folderPath = string.IsNullOrEmpty(folderPath) ? defaultPath : folderPath; EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("Languages Folder Path:", folderPath); if (GUILayout.Button("...", GUILayout.Width(30))) { var path = EditorUtility.OpenFolderPanel("Select Languages Folder", defaultPath, ""); if (string.IsNullOrEmpty(path)) return; if (path.StartsWith(Application.dataPath)) folderPath = path.Substring(path.IndexOf("Assets")); } EditorGUILayout.EndHorizontal(); EditorGUILayout.Space(); if (GUILayout.Button("Scan and Compare All Language Files")) { if (string.IsNullOrEmpty(folderPath) || !Directory.Exists(folderPath)) Debug.Log("Please select a valid folder path first!"); else ScanAndCompareFiles(); } EditorGUILayout.Space(); GUILayout.Label("Comparison Results", EditorStyles.boldLabel); scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition, GUILayout.ExpandHeight(true)); EditorGUILayout.TextArea(comparisonResult, GUILayout.ExpandHeight(true)); EditorGUILayout.EndScrollView(); }
private void ScanAndCompareFiles() { Initialize(); fileGroups.Clear(); comparisonResult = "";
var path = Path.GetFullPath(Path.Combine(Application.dataPath, folderPath.Replace("Assets/", ""))); var files = Directory.GetFiles(path, "*.lua", SearchOption.AllDirectories); if (files.Length == 0) { comparisonResult = "No .lua files found in the specified folder!"; return; }
foreach (var filePath in files) { var fileName = Path.GetFileNameWithoutExtension(filePath); try { var keys = ExtractLuaTableKeys(filePath); var fileInfo = new LanguageFileInfo { FileName = fileName, FullPath = filePath, Keys = keys }; if (!fileGroups.ContainsKey(fileName)) fileGroups.Add(fileName, new List<LanguageFileInfo>()); fileGroups[fileName].Add(fileInfo); } catch (Exception e) { Debug.LogError($"Error parsing file {filePath}: {e.Message}"); } } lua?.Dispose();
var totalIssues = 0; foreach (var group in fileGroups) { if (group.Value.Count < 2) { comparisonResult += $"File '{group.Key}' has no counterparts to compare with.\n"; continue; }
var referenceFile = group.Value[0]; var hasIssues = false; var groupResult = ""; for (var i = 1; i < group.Value.Count; i++) { var currentFile = group.Value[i]; var missingKeys = referenceFile.Keys.Except(currentFile.Keys).ToList(); var extraKeys = currentFile.Keys.Except(referenceFile.Keys).ToList(); if (missingKeys.Count > 0 || extraKeys.Count > 0) { hasIssues = true; groupResult += $"\nCompared to {referenceFile.FileName}:\n"; if (missingKeys.Count > 0) groupResult += $"Missing keys in {currentFile.FullPath}:\n{string.Join("\n", missingKeys)}\n"; if (extraKeys.Count > 0) groupResult += $"Extra keys in {currentFile.FullPath}:\n{string.Join("\n", extraKeys)}\n"; } } if (hasIssues) { totalIssues++; comparisonResult += $"\n=== Differences found in '{group.Key}' group ===\n"; comparisonResult += $"Files in group: {string.Join(", ", group.Value.Select(f => f.FileName))}\n"; comparisonResult += groupResult; } } comparisonResult = $"Scanned {files.Length} files, found {fileGroups.Count} groups, {totalIssues} groups with issues.\n" + comparisonResult; if (totalIssues == 0) comparisonResult += "\nAll language file groups are consistent!"; }
private List<string> ExtractLuaTableKeys(string filePath) { var keys = new List<string>(); try { var lanTable = lua.DoFile<LuaTable>(filePath); if (lanTable == null) throw new Exception($"无法加载 Lua 文件:{filePath}");
ExtractKeysRecursively(lanTable, "", keys);
lanTable.Dispose(); } catch (Exception ex) { Debug.LogError($"解析文件 {filePath} 失败: {ex.Message}"); throw; } return keys; }
private void ExtractKeysRecursively(LuaTable table, string parentKey, List<string> keys) { var dictTable = table.ToDictTable(); foreach (var entry in dictTable) { var key = entry.Key?.ToString(); if (string.IsNullOrEmpty(key)) continue;
var fullKey = string.IsNullOrEmpty(parentKey) ? key : $"{parentKey}.{key}";
if (entry.Value is LuaTable nestedTable) ExtractKeysRecursively(nestedTable, fullKey, keys); else keys.Add(fullKey); } } }
|