public static string[] GetModList() { if (modList == null) { if (BattleTechGame == null) { throw new InvalidOperationException("Mod List is not known until GameStartsOnce."); } modList = new HashSet <string>(); try { foreach (MethodBase method in PatchProcessor.AllPatchedMethods()) { modList.UnionWith(PatchProcessor.GetPatchInfo(method).Owners); } // Some mods may not leave a harmony trace and can only be parsed from log Regex regx = new Regex(" in type \"([^\"]+)\"", RegexOptions.Compiled); foreach (string line in File.ReadAllLines("Mods/BTModLoader.log")) { Match match = regx.Match(line); if (match.Success) { modList.Add(match.Groups[1].Value); } } } catch (Exception ex) { BattleMod.BTML_LOG.Error(ex); } } return(modList.ToArray()); }
public bool TryGetErrorMods(string logString, string stackString, out Dictionary <UnityModManager.ModInfo, List <string> > result) { Dictionary <UnityModManager.ModInfo, List <string> > errorMods = new Dictionary <UnityModManager.ModInfo, List <string> >(); try { List <string> modStr = new List <string>(); foreach (var mod in UnityModManager.modEntries) { modStr.Clear(); modStr = GetModStr(mod); if (modStr.Count <= 0) { break; } foreach (var name in modStr) { if (logString.Contains(name)) { ExceptionHelper.Instance.AddErrorMod(errorMods, mod.Info, name); } if (stackString.Contains(name)) { ExceptionHelper.Instance.AddErrorMod(errorMods, mod.Info, name); } } } string pattern = @"(?<= )\S+?_Patch\d+"; foreach (Match match in Regex.Matches(logString + stackString, pattern)) { string matchString = match.Groups[0].Value; string fullName = matchString.Substring(0, matchString.LastIndexOf('_')); int num = fullName.LastIndexOf('.'); string methodName = fullName.Substring(num + 1, fullName.Length - fullName.LastIndexOf('.') - 1); string typeName = fullName.Substring(0, fullName.LastIndexOf('.')); string index = matchString.Substring(matchString.LastIndexOf("_Patch") + 6, matchString.Length - (matchString.LastIndexOf("_Patch") + 6)); Type classtyp = AccessTools.TypeByName(typeName); if (classtyp == null) { Main.Logger.Log($"无法获取到{fullName}的类型"); continue; } MethodInfo methodInfo = classtyp.GetMethod(methodName, AccessTools.all); if (methodInfo == null) { Main.Logger.Log($"无法获取到{fullName}的方法"); continue; } var info = PatchProcessor.GetPatchInfo(methodInfo); if (info == null) { Main.Logger.Log($"无法获取到对{fullName}的补丁"); continue; } int patchIndex = int.Parse(index); foreach (var patch in info.Prefixes) { if (patch.index == patchIndex) { UnityModManager.ModInfo modInfo = UnityModManager.FindMod(patch.owner).Info; ExceptionHelper.Instance.AddErrorMod(errorMods, modInfo, matchString + ".Prefix()"); } } foreach (var patch in info.Postfixes) { if (patch.index == patchIndex) { UnityModManager.ModInfo modInfo = UnityModManager.FindMod(patch.owner).Info; ExceptionHelper.Instance.AddErrorMod(errorMods, modInfo, matchString + ".Postfix()"); } } } result = errorMods; return(true); } catch (Exception e) { errorMods.Clear(); errorMods.Add(Main.modEntry.Info, new List <string>() { e.Message, e.StackTrace }); result = errorMods; return(false); } }