/// <summary>The mod entry point, called after the mod is first loaded.</summary> /// <param name="helper">Provides simplified APIs for writing mods.</param> public override void Entry(IModHelper helper) { instance = this; helper.Events.Display.MenuChanged += OnMenuChanged; var savedIds = helper.Data.ReadJsonFile <Dictionary <string, CropData.Ids> >("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..."); DirectoryInfo cropsFolder = new DirectoryInfo(Path.Combine(helper.DirectoryPath, "Crops")); if (!cropsFolder.Exists) { cropsFolder.Create(); } foreach (var folderPath in Directory.EnumerateDirectories(cropsFolder.FullName)) { IContentPack contentPack = this.Helper.ContentPacks.CreateFake(folderPath); try { var data = contentPack.ReadJsonFile <CropData>("crop.json"); if (data == null) { Log.warn($"\tFailed to load crop data for {folderPath}"); continue; } else if (!File.Exists(Path.Combine(folderPath, "crop.png"))) { Log.warn($"\tCrop {folderPath} has no crop image, skipping"); continue; } else if (!File.Exists(Path.Combine(folderPath, "product.png"))) { Log.warn($"\tCrop {folderPath} has no product image, skipping"); continue; } else if (!File.Exists(Path.Combine(folderPath, "seeds.png"))) { Log.warn($"\tCrop {folderPath} 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 {folderPath}: {e}"); continue; } } helper.Data.WriteJsonFile("saved-ids.json", CropData.savedIds); helper.Content.AssetEditors.Add(new ContentInjector()); }
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()); }