//Say patch - replace dialogue lines just-in-time public static void Say_PrefixPatch(Agent __instance, ref string myMessage) { float chance = UnityEngine.Random.Range(0.0f, 1.0f); NPCName npcName = null; if (npcNamesDict.ContainsKey(__instance.agentRealName) && chance < regularDialogueReplacementChance) { npcName = npcNamesDict[__instance.agentRealName]; } else if (__instance.agentRealName == priorityNPCName.Name && chance < priorityDialogueReplacementChance) { npcName = priorityNPCName; } if (npcName != null) { string message = npcName.GetRandomDialogue(); if (message != "") { myMessage = message; } } }
public void Awake() { Logger.LogInfo(string.Format("{0} v{1} ({2}) started.", pluginName, pluginVersion, pluginGuid)); string priorityName = ""; try { // Read config data foreach (string line in File.ReadLines(@"BepInEx\plugins\NamedNPCs\config.cfg")) { if (string.IsNullOrWhiteSpace(line) || line.StartsWith("#")) { continue; } string[] operands = line.Split('='); string paramName = operands[0].Trim(); string paramValue = operands[1].Trim(); paramValue = paramValue.Replace("\"", ""); if (bool.TryParse(paramValue, out bool paramBool)) // Booleans { switch (paramName) { case "EnableRenaming": enableRenaming = paramBool; break; case "EnableDialogueReplacement": enableDialogueReplacement = paramBool; break; } } else if (float.TryParse(paramValue, out float paramNumber)) // Numbers { switch (paramName) { case "LevelSpawnsMin": levelSpawnsMin = Math.Max(0, (int)paramNumber); break; case "LevelSpawnsMax": levelSpawnsMax = Math.Max(0, (int)paramNumber); break; case "NameOccurancePerLevelMax": nameOccurancePerLevelMax = Math.Max(0, (int)paramNumber); break; case "NameOccurancePerRunMax": nameOccurancePerRunMax = Math.Max(0, (int)paramNumber); break; case "RegularDialogueReplacementChance": regularDialogueReplacementChance = Mathf.Clamp(paramNumber / 100.0f, 0.0f, 1.0f); break; case "PriorityDialogueReplacementChance": priorityDialogueReplacementChance = Mathf.Clamp(paramNumber / 100.0f, 0.0f, 1.0f); break; } } else // Strings { switch (paramName) { case "PlayerName": if (paramValue != "") { playerName = paramValue; } break; case "PriorityName": if (paramValue != "") { priorityName = paramValue; } break; } } } } catch { Logger.LogError("NamedNPCs: Error opening/parsing configuration file"); return; } try { // Read name data foreach (string line in File.ReadLines(@"BepInEx\plugins\NamedNPCs\names.txt")) { if (string.IsNullOrWhiteSpace(line) || line.StartsWith("#")) { continue; } string[] entries = line.Trim().Split('|'); string npcName = entries[0]; List <string> dialogues = entries.Skip(1).ToList(); if (priorityNPCName == null && npcName.ToLower() == priorityName.ToLower()) { priorityNPCName = new NPCName(npcName, dialogues); continue; } if (npcNamesDict.ContainsKey(npcName)) { continue; } npcNamesDict.Add(npcName, new NPCName(npcName, dialogues)); } } catch { Logger.LogError("NamedNPCs: Error opening/parsing names file"); return; } // Link in patch methods Harmony harmony = new Harmony(pluginGuid); MethodInfo original; MethodInfo patch; if (!enableRenaming) { return; } original = AccessTools.Method(typeof(Fader), "FadedIn"); patch = AccessTools.Method(typeof(Renamer), "FadedIn_PrefixPatch"); harmony.Patch(original, new HarmonyMethod(patch)); if (!enableDialogueReplacement) { return; } original = AccessTools.Method(typeof(Agent), "Say", new Type[] { typeof(string), typeof(bool) }); patch = AccessTools.Method(typeof(Renamer), "Say_PrefixPatch"); harmony.Patch(original, new HarmonyMethod(patch)); }