public static void PatchAll(AnimalsDie animalsDie) { mod = animalsDie; var harmony = HarmonyInstance.Create(mod.ModManifest.UniqueID); try { harmony.Patch( original: AccessTools.Method(typeof(FarmAnimal), "dayUpdate"), prefix: new HarmonyMethod(typeof(Patcher), nameof(PatchDayUpdate)) ); harmony.Patch( original: AccessTools.Method(typeof(QuestionEvent), "setUp"), postfix: new HarmonyMethod(typeof(Patcher), nameof(DetectPregnancy)) ); harmony.Patch( original: AccessTools.Method(typeof(SoundInTheNightEvent), "makeChangesToLocation"), prefix: new HarmonyMethod(typeof(Patcher), nameof(DetectAnimalAttack)) ); } catch (Exception e) { mod.ErrorLog("Error while trying to setup required patches:", e); } }
public static void VerifyConfigValues(AnimalsDieConfig config, AnimalsDie mod) { bool invalidConfig = false; foreach (var prop in typeof(AnimalsDieConfig).GetProperties()) { if (prop.Name.StartsWith("Min")) { if (prop.PropertyType == typeof(int)) { int minValue = (int)prop.GetValue(config); if (minValue < 0) { invalidConfig = true; prop.SetValue(config, 0); } var maxProp = typeof(AnimalsDieConfig).GetProperty("Max" + prop.Name.Substring(3)); if ((int)maxProp.GetValue(config) < minValue) { invalidConfig = true; maxProp.SetValue(config, minValue); } } } if (prop.Name == nameof(config.DaysToDieDueToStarvation) || prop.Name == nameof(config.DaysToDieDueToDehydrationWithAnimalsNeedWaterMod) || prop.Name == nameof(config.IllnessScoreToDie)) { int value = (int)prop.GetValue(config); if (value < 1) { invalidConfig = true; prop.SetValue(config, 1); } } } if (invalidConfig) { mod.DebugLog("At least one config value was out of range and was reset."); mod.Helper.WriteConfig(config); } }
public static void VerifyConfigValues(AnimalsDieConfig config, AnimalsDie mod) { bool invalidConfig = false; foreach (var prop in typeof(AnimalsDieConfig).GetProperties()) { if (prop.Name.StartsWith("Min")) { if (prop.PropertyType == typeof(int)) { int minValue = (int)prop.GetValue(config); if (minValue < 0) { invalidConfig = true; prop.SetValue(config, 0); } var maxProp = typeof(AnimalsDieConfig).GetProperty("Max" + prop.Name[3..]);
public static void SetUpModConfigMenu(AnimalsDieConfig config, AnimalsDie mod, bool isWaterModInstalled) { GenericModConfigMenuAPI api = mod.Helper.ModRegistry.GetApi <GenericModConfigMenuAPI>("spacechase0.GenericModConfigMenu"); if (api == null) { return; } var manifest = mod.ModManifest; api.RegisterModConfig(manifest, () => config = new AnimalsDieConfig(), delegate { mod.Helper.WriteConfig(config); VerifyConfigValues(config, mod); }); api.RegisterLabel(manifest, "Features", null); foreach (var prop in typeof(AnimalsDieConfig).GetProperties()) { if (prop.PropertyType == typeof(bool)) { string betterName = Regex.Replace(prop.Name, "([a-z](?=[A-Z])|[A-Z](?=[A-Z][a-z]))", "$1 "); if (prop.Name == nameof(config.DeathByDehydrationWithAnimalsNeedWaterMod)) { if (!isWaterModInstalled) { continue; } else { betterName = Regex.Replace(prop.Name.Remove(prop.Name.Length - "WithAnimalsNeedWaterMod".Length), "([a-z](?=[A-Z])|[A-Z](?=[A-Z][a-z]))", "$1 "); } } api.RegisterSimpleOption(manifest, betterName, null, () => (bool)prop.GetValue(config), (bool b) => prop.SetValue(config, b)); } else { if (prop.Name == nameof(config.DaysToDieDueToStarvation) || prop.Name == nameof(config.IllnessScoreToDie)) { string betterName = Regex.Replace(prop.Name, "([a-z](?=[A-Z])|[A-Z](?=[A-Z][a-z]))", "$1 "); api.RegisterSimpleOption(manifest, betterName, null, () => (int)prop.GetValue(config), (int b) => prop.SetValue(config, b)); } if (prop.Name == nameof(config.DaysToDieDueToDehydrationWithAnimalsNeedWaterMod)) { string betterName = string.Empty; if (!isWaterModInstalled) { continue; } else { betterName = Regex.Replace(prop.Name.Remove(prop.Name.Length - "WithAnimalsNeedWaterMod".Length), "([a-z](?=[A-Z])|[A-Z](?=[A-Z][a-z]))", "$1 "); } api.RegisterSimpleOption(manifest, betterName, null, () => (int)prop.GetValue(config), (int b) => prop.SetValue(config, b)); } } } api.RegisterLabel(manifest, "Ages (in years)", null); foreach (var prop in typeof(AnimalsDieConfig).GetProperties()) { if (prop.Name.StartsWith("Min")) { if (prop.PropertyType == typeof(int)) { string betterName = Regex.Replace(prop.Name, "([a-z](?=[A-Z])|[A-Z](?=[A-Z][a-z]))", "$1 "); api.RegisterSimpleOption(manifest, betterName, "must at least 0 and smaller or equal to maximum age, otherwise it's reset", () => (int)prop.GetValue(config), (int i) => prop.SetValue(config, i)); var maxProp = typeof(AnimalsDieConfig).GetProperty("Max" + prop.Name.Substring(3)); betterName = Regex.Replace(maxProp.Name, "([a-z](?=[A-Z])|[A-Z](?=[A-Z][a-z]))", "$1 "); api.RegisterSimpleOption(manifest, betterName, "must be larger or equal to the minimum age, otherwise it's reset", () => (int)maxProp.GetValue(config), (int i) => maxProp.SetValue(config, i)); } } } }