/********* ** Public methods *********/ /// <summary>The mod entry point, called after the mod is first loaded.</summary> /// <param name="helper">Provides methods for interacting with the mod directory, such as read/writing a config file or custom JSON files.</param> public override void Entry(IModHelper helper) { // initialise this.Config = helper.ReadConfig <ModConfig>(); this.Data = helper.ReadJsonFile <ModData>("data.json") ?? new ModData(); this.ChestFactory = new ChestFactory(helper.Translation, this.Config.EnableShippingBin); // hook UI GraphicsEvents.OnPostRenderHudEvent += this.GraphicsEvents_OnPostRenderHudEvent; MenuEvents.MenuChanged += this.MenuEvents_MenuChanged; MenuEvents.MenuClosed += this.MenuEvents_MenuClosed; // hook input InputEvents.ButtonPressed += this.InputEvents_ButtonPressed; // hook game events SaveEvents.AfterLoad += this.SaveEvents_AfterLoad; // validate translations if (!helper.Translation.GetTranslations().Any()) { this.Monitor.Log("The translation files in this mod's i18n folder seem to be missing. The mod will still work, but you'll see 'missing translation' messages. Try reinstalling the mod to fix this.", LogLevel.Warn); } }
public void addContentPack(string folderName, string fileName, IModHelper helper = null, Dictionary <string, string> options = null) { if (helper == null) { helper = Helper; } string baseFolder = CustomFarmingReduxMod.folder; if (options != null && options.ContainsKey("baseFolder")) { baseFolder = options["baseFolder"]; } string path = Path.Combine(baseFolder, folderName, fileName); CustomFarmingPack pack = helper.ReadJsonFile <CustomFarmingPack>(path); pack.baseFolder = baseFolder; Dictionary <string, string> toCrafting = new Dictionary <string, string>(); pack.folderName = folderName; pack.fileName = fileName; if (pack is CustomFarmingPack) { foreach (CustomMachineBlueprint blueprint in pack.machines) { blueprint.pack = pack; blueprint.texture2d = blueprint.getTexture(helper); CustomFarmingReduxMod.machines.AddOrReplace(blueprint); if (blueprint.production != null) { foreach (RecipeBlueprint recipe in blueprint.production) { if (recipe.texture != null && recipe.texture != "") { recipe.texture2d = recipe.getTexture(helper); } recipe.mBlueprint = blueprint; } } else if (blueprint.asdisplay) { blueprint.pulsate = false; blueprint.production = new List <RecipeBlueprint>(); blueprint.production.Add(new RecipeBlueprint()); blueprint.production[0].index = 0; blueprint.production[0].time = 0; } if (blueprint.crafting != null) { toCrafting.AddOrReplace(blueprint.fullid, $"{blueprint.crafting}/Home/130/true/null/{blueprint.fullid}"); CustomFarmingReduxMod.craftingrecipes.AddOrReplace(blueprint.fullid, 0); } if (blueprint.forsale && (blueprint.condition == null || PyTK.PyUtils.CheckEventConditions(blueprint.condition))) { new InventoryItem(new CustomMachine(blueprint), blueprint.price).addToNPCShop(blueprint.shop); } } } if (toCrafting.Count > 0) { toCrafting.injectInto($"Data/CraftingRecipes"); } }
public static SaveConfig Load(IModHelper helper, string filename) { return(helper.ReadJsonFile <SaveConfig>(filename)); }
public override void Entry(IModHelper helper) { Config = new TimeMultiplierConfig(); LastTimeInterval = 0; SaveEvents.AfterLoad += (sender, e) => { LastTimeInterval = 0; string configLocation = Path.Combine("data", Constants.SaveFolderName + ".json"); Config = helper.ReadJsonFile <TimeMultiplierConfig>(configLocation) ?? new TimeMultiplierConfig(); if (Config.Enabled) { timeMultiplierToggled(Config.Enabled); } }; SaveEvents.AfterReturnToTitle += (sender, e) => { LastTimeInterval = 0; string configLocation = Path.Combine("data", Constants.SaveFolderName + ".json"); helper.WriteJsonFile <TimeMultiplierConfig>(configLocation, Config); Config = new TimeMultiplierConfig(); timeMultiplierToggled(false); }; helper.ConsoleCommands.Add("time_multiplier_change", "Updates time multiplier on the fly", (string command, string[] args) => { float multiplierArg; if (args.Length != 1) { Monitor.Log("Usage: time_multiplier_change 1.00 ", LogLevel.Error); return; } else if (!float.TryParse(args[0], out multiplierArg)) { Monitor.Log("Error: '" + args[0] + "' is not a valid decimal. Usage: time_multiplier_change 1.00 ", LogLevel.Error); return; } Config.TimeMultiplier = multiplierArg; string configLocation = Path.Combine("data", Constants.SaveFolderName + ".json"); helper.WriteJsonFile <TimeMultiplierConfig>(configLocation, Config); Monitor.Log("Time now multiplied by " + multiplierArg, LogLevel.Info); }); helper.ConsoleCommands.Add("time_multiplier_toggle", "Updates time multiplier on the fly", (string command, string[] args) => { if (!timeMultiplierToggled(!Config.Enabled)) { return; } LastTimeInterval = 0; Config.Enabled = !Config.Enabled; string configLocation = Path.Combine("data", Constants.SaveFolderName + ".json"); helper.WriteJsonFile <TimeMultiplierConfig>(configLocation, Config); Monitor.Log("Time multiplier enabled: " + Config.Enabled, LogLevel.Info); }); Monitor.Log("Initialized"); }
/// <summary>Edit a matched asset.</summary> /// <param name="asset">A helper which encapsulates metadata about an asset and enables changes to it.</param> public void Edit <T>(IAssetData asset) { if (!Context.IsWorldReady) { return; // If we aren't in-game, don't edit anything. } ModData modData = Helper.ReadJsonFile <ModData>("saves.json") ?? new ModData(); // Load save file, or create new one. ModData.ModDataSave dataSave = Array.Find <ModData.ModDataSave>(modData.saves, x => x.saveName == Constants.SaveFolderName); // Load data for specific save. bool elemNotFound = false; bool shouldUpdate = false; if (dataSave == null) // If the savefile element was not found { dataSave = new ModData.ModDataSave(); dataSave.saveName = Constants.SaveFolderName; // if element wasn't found, prepare one. elemNotFound = true; } if (!dataSave.enabled) { return; // If RandomTastes isn't enabled for this save. } asset .AsDictionary <string, string>() .Set((id, data) => { if (id.StartsWith("Universal_")) { return(data); // leave universal tastes alone } ModData.ModDataSave.ModDataEntry entry = Array.Find <ModData.ModDataSave.ModDataEntry>(dataSave.entries, x => x.id == id); // find entry bool entryNotFound = false; if (entry == null) // If the entry was not found { // generate new entry entry = GenerateTastes(id); entryNotFound = true; } string[] fields = data.Split('/'); for (int i = 0; i < 5; i++) { List <int> selected = new List <int>(); int fieldIndex = 1; switch (i) { case 0: selected = new List <int>(entry.love); fieldIndex = 1; break; case 1: selected = new List <int>(entry.like); fieldIndex = 3; break; case 2: selected = new List <int>(entry.neutral); fieldIndex = 9; break; case 3: selected = new List <int>(entry.dislike); fieldIndex = 5; break; case 4: selected = new List <int>(entry.hate); fieldIndex = 7; break; default: this.Monitor.Log($"Uhh... This doesn't seem right... ({i})", LogLevel.Error); break; } this.Monitor.Log($"{i} : {string.Join(" ", selected)}", LogLevel.Trace); fields[fieldIndex] = string.Join(" ", selected); } if (entryNotFound) // If the entry was not found { // add entry to savefile List <ModData.ModDataSave.ModDataEntry> entryList = new List <ModData.ModDataSave.ModDataEntry>(dataSave.entries); entryList.Add(entry); dataSave.entries = entryList.ToArray(); shouldUpdate = true; } return(string.Join("/", fields)); }); if (elemNotFound) // If the savefile element was not found { // add entry to savefile List <ModData.ModDataSave> modSaves = new List <ModData.ModDataSave>(modData.saves); modSaves.Add(dataSave); modData.saves = modSaves.ToArray(); } shouldUpdate = shouldUpdate || elemNotFound; // Calculate elemNotFound into shouldUpdate if (shouldUpdate) { this.Helper.WriteJsonFile("saves.json", modData); // Update mod data file } }
public override void Entry(IModHelper helper) { instance = this; MenuEvents.MenuChanged += menuChanged; var savedIds = helper.ReadJsonFile <Dictionary <string, CropData.Ids> >(Path.Combine(Helper.DirectoryPath, "saved-ids.json")); if (savedIds != null) { CropData.savedIds = savedIds; foreach (var ids in savedIds) { CropData.Ids.MostRecentObject = Math.Max(CropData.Ids.MostRecentObject, Math.Max(ids.Value.Product, ids.Value.Seeds)); CropData.Ids.MostRecentCrop = Math.Max(CropData.Ids.MostRecentCrop, ids.Value.Crop); } } CropData.crops.Clear(); Log.info("Registering custom crops..."); foreach (var file in Directory.EnumerateDirectories(Path.Combine(helper.DirectoryPath, "Crops"))) { try { var data = helper.ReadJsonFile <CropData>(Path.Combine(file, "crop.json")); if (data == null) { Log.warn("\tFailed to load crop data for " + file); continue; } else if (!File.Exists(Path.Combine(file, "crop.png"))) { Log.warn("\tCrop " + file + " has no crop image, skipping"); continue; } else if (!File.Exists(Path.Combine(file, "product.png"))) { Log.warn("\tCrop " + file + " has no product image, skipping"); continue; } else if (!File.Exists(Path.Combine(file, "seeds.png"))) { Log.warn("\tCrop " + file + " has no seeds image, skipping"); continue; } Log.info("\tCrop: " + data.Id); CropData.Register(data); } catch (Exception e) { Log.warn("\tFailed to load crop data for " + file + ": " + e); continue; } } helper.WriteJsonFile(Path.Combine(Helper.DirectoryPath, "saved-ids.json"), CropData.savedIds); var editors = ((IList <IAssetEditor>)helper.Content.GetType().GetProperty("AssetEditors", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance).GetValue(Helper.Content)); editors.Add(new ContentInjector()); }