private void TryLoadingGMCM() { //See if we can find GMCM, quit if not. var api = Helper.ModRegistry.GetApi <GenericModConfigMenu.GenericModConfigMenuAPI>("spacechase0.GenericModConfigMenu"); if (api == null) { Monitor.Log("Unable to load GMCM API.", LogLevel.Info); return; } WateringPattern dp = new WateringPattern(); //Just need this to refer to a thing string[] pattypes = dp.GetPatternTypes(); api.RegisterModConfig(ModManifest, () => myConfig = new BasicSprinklerConfig(), () => Helper.WriteConfig(myConfig)); //Pattern types api.RegisterChoiceOption(ModManifest, "Watering Pattern", "Which watering pattern to use. Set to 'custom' (it's in the dropdown, but might not be immediately visible) to set a custom area using the controls below. Otherwise they are ignored.", () => myConfig.patternType, (string val) => myConfig.patternType = val, pattypes); api.RegisterLabel(ModManifest, "Sum of custom values must not exceed 4.", "The improved basic sprinkler will have the same watering area as the default. Values entered here when custom pattern is selected will throw an error if they add up to more than 4. The game will then use the default pattern."); api.RegisterClampedOption(ModManifest, "Custom North Area", "How far north the sprinkler should water.", () => myConfig.northArea, (int val) => myConfig.northArea = val, 0, 4); api.RegisterClampedOption(ModManifest, "Custom South Area", "How far south the sprinkler should water.", () => myConfig.southArea, (int val) => myConfig.southArea = val, 0, 4); api.RegisterClampedOption(ModManifest, "Custom East Area", "How far east the sprinkler should water.", () => myConfig.eastArea, (int val) => myConfig.eastArea = val, 0, 4); api.RegisterClampedOption(ModManifest, "Custom West Area", "How far west the sprinkler should water.", () => myConfig.westArea, (int val) => myConfig.westArea = val, 0, 4); }
WateringPattern LoadPatternFromConfig(BasicSprinklerConfig config) { WateringPattern result; if (config == null) { Monitor.Log($"Tried to load nonexistent config...", LogLevel.Error); noProb = false; return(null); } string type = config.patternType; int[] dims = new int[4] { config.northArea, config.southArea, config.eastArea, config.westArea }; result = new WateringPattern(type, dims); if (result.errorMsg != "") { Monitor.Log($"Error in current pattern: {result.errorMsg}", LogLevel.Warn); noProb = false; } return(result); }
void LoadConfigsFromFile(string fileName = "", WateringPattern toSet = null) { BasicSprinklerConfig tempConfig; string loadedName; //default config - what we're actually using. if (fileName == "") { Monitor.Log("Loading pattern from default config file."); myConfig = this.Helper.ReadConfig <BasicSprinklerConfig>(); } //custom file else { Monitor.Log($"Loading pattern from config file: '{fileName}'"); tempConfig = this.Helper.Data.ReadJsonFile <BasicSprinklerConfig>(fileName); loadedName = fileName; if (tempConfig != null && toSet != null) { Monitor.Log("Configuration loaded correctly."); toSet = LoadPatternFromConfig(tempConfig); if (toSet == null) { Monitor.Log($"No pattern could be loaded from file: '{loadedName}'; may be saved & loaded later."); } else if (toSet.errorMsg != "") { Monitor.Log($"Error in setting pattern: {toSet.errorMsg}"); } else { Monitor.Log($"Result = {toSet}"); } } else { Monitor.Log($"Could not load from file '{loadedName}', did you pass a valid pattern to set?"); } } }
//Every day, activate all sprinklers if not raining. void WaterEachMorning(object sender, EventArgs e) { //Just give up if we're stuck with a default sprinkler due to pattern def error if (!noProb) { Monitor.Log("Had to quit watering due to a pattern definition issue, default pattern will be applied.", LogLevel.Warn); return; } toWater = LoadPatternFromConfig(myConfig); if (toWater == null) { Monitor.Log("Had to quit watering due inablity to load pattern to apply from config, default pattern will be applied.", LogLevel.Error); return; } DoDiagnostics(); //Check to see if we should update the pattern: If the pattern has changed in the congfig, undo the last pattern. if (lastUsed != null && lastUsed.ToString() != toWater.ToString()) { Monitor.Log("Updating pattern."); Monitor.Log("Prior = " + lastUsed.ToString()); Monitor.Log("New = " + toWater.ToString()); LocateSprinklers(UnSprinkleOldPattern); } else { Monitor.Log("No pattern update needed. Current pattern = " + toWater.ToString()); } //Suppress default pattern LocateSprinklers(UnwaterDefault); //Activate user-defined pattern LocateSprinklers(ActivateSprinkler); //myHelper.Events.Player.Warped -= Event_DoWatering; }
//Save last used pattern. The point here being that if the user changes the desired pattern, we need to undo the old pattern. void SavePatterns(object sender, EventArgs e) { Monitor.Log("Saving current pattern."); //Helper.Data.WriteJsonFile<BasicSprinklerConfig>(configFile, new BasicSprinklerConfig(toWater.myType, toWater.myPattern)); if (lastUsed == null) { Monitor.Log("First time save."); Monitor.Log($"Current = {toWater}"); } else { Monitor.Log($"Previous = {lastUsed}"); Monitor.Log($"New = {toWater}"); } lastUsed = toWater; Helper.Data.WriteJsonFile <BasicSprinklerConfig>(backupFile, new BasicSprinklerConfig(lastUsed.myType, lastUsed.myPattern)); Monitor.Log($"New pattern saved - {lastUsed}"); }