public override void Entry(IModHelper helper) { this.helper = helper; monitor = Monitor; logger = new Logger(monitor); teleporters = new TeleporterList(logger); HarmonyPatches.Patches.Initialise(logger, teleporters); Harmony harmony = new Harmony(ModManifest.UniqueID); // harmony.Patch( // original: AccessTools.Method(typeof(SObject), nameof(SObject.checkForAction)), // prefix: new HarmonyMethod(typeof(HarmonyPatches.Patches), nameof(HarmonyPatches.Patches.SObject_CheckForAction_Prefix))); // This is where we check for interactions with our teleporter. harmony.Patch( original: AccessTools.Method(typeof(GameLocation), nameof(GameLocation.checkAction)), prefix: new HarmonyMethod(typeof(HarmonyPatches.Patches), nameof(HarmonyPatches.Patches.GameLocation_CheckAction_Prefix))); // This is where we detect a teleporter has been placed. harmony.Patch( original: AccessTools.Method(typeof(SObject), nameof(SObject.placementAction)), prefix: new HarmonyMethod(typeof(HarmonyPatches.Patches), nameof(HarmonyPatches.Patches.SObject_PlacementAction_Prefix))); this.helper.Events.GameLoop.OneSecondUpdateTicked += OneSecondUpdateTicked; this.helper.Events.World.ObjectListChanged += WorldOnObjectListChanged; this.helper.Events.GameLoop.SaveLoaded += OnSaveLoaded; this.helper.Events.Input.ButtonsChanged += ButtonsChanged; logger.Log("Stardew Teleporter Network initialised."); }
/********* ** Private methods *********/ private TeleportationResolver(string list) { this.List = ModEntry.PatchData.Teleporters.FirstOrDefault(e => e.ListName == list); this.Destinations = new Dictionary <string, Response> { { "", new Response("cancel", ModEntry.Strings.Get("cancel")) } }; for (int c = 0; c < this.List.Destinations.Count; c++) { if (this.List.Destinations[c].MapName != Game1.currentLocation.Name) { this.Destinations.Add(this.List.Destinations[c].MapName, new Response(c.ToString(), this.List.Destinations[c].ItemText)); } } }
/// <summary>Load data from a version 1.1 manifest.</summary> /// <param name="contentPack">The content pack to load.</param> /// <param name="configPath">The content pack's relative config file path.</param> private LocationConfig ReadConfig_1_1(IContentPack contentPack, string configPath) { // read raw data LocationConfig_1_1 raw; try { raw = contentPack.ReadJsonFile <LocationConfig_1_1>(configPath); } catch (Exception err) { this.Monitor.Log(" Skipped: can't parse config file (version 1.1).", LogLevel.Warn, err); return(null); } // parse LocationConfig config = new LocationConfig(); { // convert locations if (raw.Locations != null) { foreach (IDictionary <string, string> location in raw.Locations) { config.Locations.Add(new Location { Farmable = location.ContainsKey("farmable") && Convert.ToBoolean(location["farmable"]), Outdoor = location.ContainsKey("outdoor") && Convert.ToBoolean(location["outdoor"]), FileName = location["file"], MapName = location["name"], Type = "Default" }); } } // convert overrides if (raw.Overrides != null) { foreach (IDictionary <string, string> @override in raw.Overrides) { config.Overrides.Add(new Override { FileName = @override["file"], MapName = @override["name"] }); } } // convert tilesheets if (raw.Tilesheets != null) { foreach (IDictionary <string, string> sheet in raw.Tilesheets) { config.Tilesheets.Add(new Tilesheet { FileName = sheet["file"], MapName = sheet["map"], SheetId = sheet["sheet"], Seasonal = sheet.ContainsKey("seasonal") && Convert.ToBoolean(sheet["seasonal"]) }); } } // convert tiles if (raw.Tiles != null) { foreach (IDictionary <string, string> tile in raw.Tiles) { Tile newTile = new Tile { TileX = Convert.ToInt32(tile["x"]), TileY = Convert.ToInt32(tile["y"]), MapName = tile["map"], LayerId = tile["layer"], SheetId = tile.ContainsKey("sheet") ? tile["sheet"] : null }; if (tile.ContainsKey("interval")) { newTile.Interval = Convert.ToInt32(tile["interval"]); newTile.TileIndexes = tile["tileIndex"].Split(',').Select(p => Convert.ToInt32(p)).ToArray(); } else { newTile.TileIndex = Convert.ToInt32(tile["tileIndex"]); } newTile.Conditions = tile.ContainsKey("conditions") ? tile["conditions"] : null; config.Tiles.Add(newTile); } } // convert properties if (raw.Properties != null) { foreach (IList <string> property in raw.Properties) { config.Properties.Add(new Property { MapName = property[0], LayerId = property[1], TileX = Convert.ToInt32(property[2]), TileY = Convert.ToInt32(property[3]), Key = property[4], Value = property[5] }); } } // convert warps if (raw.Warps != null) { foreach (IList <string> warp in raw.Warps) { config.Warps.Add(new Warp { MapName = warp[0], TileX = Convert.ToInt32(warp[1]), TileY = Convert.ToInt32(warp[2]), TargetName = warp[3], TargetX = Convert.ToInt32(warp[4]), TargetY = Convert.ToInt32(warp[5]) }); } } // convert conditions if (raw.Conditions != null) { foreach (KeyValuePair <string, IDictionary <string, string> > condition in raw.Conditions) { config.Conditionals.Add(new Conditional { Name = condition.Key, Item = Convert.ToInt32(condition.Value["item"]), Amount = Convert.ToInt32(condition.Value["amount"]), Question = condition.Value["question"] }); } } // convert minecarts if (raw.Minecarts != null) { foreach (KeyValuePair <string, IDictionary <string, IList <string> > > set in raw.Minecarts) { TeleporterList newSet = new TeleporterList { ListName = set.Key }; foreach (KeyValuePair <string, IList <string> > destination in set.Value) { newSet.Destinations.Add(new TeleporterDestination { ItemText = destination.Key, MapName = destination.Value[0], TileX = Convert.ToInt32(destination.Value[1]), TileY = Convert.ToInt32(destination.Value[2]), Direction = Convert.ToInt32(destination.Value[3]) }); } config.Teleporters.Add(newSet); } } // convert shops config.Shops = raw.Shops; } return(config); }
public static void Load(string filepath) { AdvancedLocationLoaderMod.Logger.Log("Converting legacy 1.1 manifest to new 1.2 format...", LogLevel.Debug); LocationConfig1_1 Config; try { Config = JsonConvert.DeserializeObject <LocationConfig1_1>(File.ReadAllText(filepath)); } catch (Exception err) { AdvancedLocationLoaderMod.Logger.Log(LogLevel.Error, "Unable to load legacy manifest, json cannot be parsed: " + filepath, err); return; } MainLocationManifest1_2 Updated = new MainLocationManifest1_2(); // Prepare the 1.2 properties Updated.LoaderVersion = "1.2.0"; AdvancedLocationLoaderMod.Logger.Log("Converting the `about` section", LogLevel.Trace); // Convert the `about` section Updated.About = new About(); if (Config.about.ContainsKey("author")) { Updated.About.Author = Config.about["author"]; } if (Config.about.ContainsKey("description")) { Updated.About.Description = Config.about["description"]; } if (Config.about.ContainsKey("version")) { Updated.About.Version = Config.about["version"]; } AdvancedLocationLoaderMod.Logger.Log("Converting the `locations` section", LogLevel.Trace); // Convert the `locations` section if (Config.locations != null) { Updated.Locations = new List <Location>(); foreach (Dictionary <string, string> loc in Config.locations) { Location newLoc = new Location(); newLoc.Farmable = loc.ContainsKey("farmable") ? Convert.ToBoolean(loc["farmable"]) : false; newLoc.Outdoor = loc.ContainsKey("outdoor") ? Convert.ToBoolean(loc["outdoor"]) : false; newLoc.FileName = loc["file"]; newLoc.MapName = loc["name"]; newLoc.Type = "Default"; Updated.Locations.Add(newLoc); } } AdvancedLocationLoaderMod.Logger.Log("Converting the `overrides` section", LogLevel.Trace); // Convert the `overrides` section if (Config.overrides != null) { Updated.Overrides = new List <Override>(); foreach (Dictionary <string, string> ovr in Config.overrides) { Override newOvr = new Override(); newOvr.FileName = ovr["file"]; newOvr.MapName = ovr["name"]; Updated.Overrides.Add(newOvr); } } AdvancedLocationLoaderMod.Logger.Log("Converting the `tilesheets` section", LogLevel.Trace); // Convert the `tilesheets` section if (Config.tilesheets != null) { Updated.Tilesheets = new List <Tilesheet>(); foreach (Dictionary <string, string> sheet in Config.tilesheets) { Tilesheet newSheet = new Tilesheet(); newSheet.FileName = sheet["file"]; newSheet.MapName = sheet["map"]; newSheet.SheetId = sheet["sheet"]; newSheet.Seasonal = sheet.ContainsKey("seasonal") ? Convert.ToBoolean(sheet["seasonal"]) : false; Updated.Tilesheets.Add(newSheet); } } AdvancedLocationLoaderMod.Logger.Log("Converting the `tiles` section", LogLevel.Trace); // Convert the `tiles` section if (Config.tiles != null) { Updated.Tiles = new List <Tile>(); foreach (Dictionary <string, string> tile in Config.tiles) { Tile newTile = new Tile(); newTile.TileX = Convert.ToInt32(tile["x"]); newTile.TileY = Convert.ToInt32(tile["y"]); newTile.MapName = tile["map"]; newTile.LayerId = tile["layer"]; newTile.SheetId = tile.ContainsKey("sheet") ? tile["sheet"] : null; if (tile.ContainsKey("interval")) { newTile.Interval = Convert.ToInt32(tile["interval"]); newTile.TileIndexes = new List <string>(tile["tileIndex"].Split(',')).ConvertAll(Convert.ToInt32).ToArray(); } else { newTile.TileIndex = Convert.ToInt32(tile["tileIndex"]); } newTile.Conditions = tile.ContainsKey("conditions") ? tile["conditions"] : null; Updated.Tiles.Add(newTile); } } AdvancedLocationLoaderMod.Logger.Log("Converting the `properties` section", LogLevel.Trace); // Convert the `properties` section if (Config.properties != null) { Updated.Properties = new List <Property>(); foreach (List <string> prop in Config.properties) { Property newProp = new Property(); newProp.MapName = prop[0]; newProp.LayerId = prop[1]; newProp.TileX = Convert.ToInt32(prop[2]); newProp.TileY = Convert.ToInt32(prop[3]); newProp.Key = prop[4]; newProp.Value = prop[5]; Updated.Properties.Add(newProp); } } AdvancedLocationLoaderMod.Logger.Log("Converting the `warps` section", LogLevel.Trace); // Convert the `warps` section if (Config.warps != null) { Updated.Warps = new List <Warp>(); foreach (List <string> warp in Config.warps) { Warp newWarp = new Warp(); newWarp.MapName = warp[0]; newWarp.TileX = Convert.ToInt32(warp[1]); newWarp.TileY = Convert.ToInt32(warp[2]); newWarp.TargetName = warp[3]; newWarp.TargetX = Convert.ToInt32(warp[4]); newWarp.TargetY = Convert.ToInt32(warp[5]); Updated.Warps.Add(newWarp); } } AdvancedLocationLoaderMod.Logger.Log("Converting the `conditions` section", LogLevel.Trace); // Convert the `conditions` into the new `Conditionals` section if (Config.conditions != null) { Updated.Conditionals = new List <Conditional>(); foreach (KeyValuePair <string, Dictionary <string, string> > cond in Config.conditions) { Conditional newCond = new Conditional(); newCond.Name = cond.Key; newCond.Item = Convert.ToInt32(cond.Value["item"]); newCond.Amount = Convert.ToInt32(cond.Value["amount"]); newCond.Question = cond.Value["question"]; Updated.Conditionals.Add(newCond); } } AdvancedLocationLoaderMod.Logger.Log("Converting the `minecarts` section", LogLevel.Trace); // Convert the `minecarts` section if (Config.minecarts != null) { Updated.Teleporters = new List <TeleporterList>(); foreach (KeyValuePair <string, Dictionary <string, List <string> > > set in Config.minecarts) { TeleporterList newSet = new TeleporterList(); newSet.ListName = set.Key; foreach (KeyValuePair <string, List <string> > dest in set.Value) { TeleporterDestination newDest = new TeleporterDestination(); newDest.ItemText = dest.Key; newDest.MapName = dest.Value[0]; newDest.TileX = Convert.ToInt32(dest.Value[1]); newDest.TileY = Convert.ToInt32(dest.Value[2]); newDest.Direction = Convert.ToInt32(dest.Value[3]); newSet.Destinations.Add(newDest); } Updated.Teleporters.Add(newSet); } } AdvancedLocationLoaderMod.Logger.Log("Converting the `shops` section", LogLevel.Trace); // Convert the `shops` section Updated.Shops = Config.shops; // Remove empty fields if (Updated.Conditionals.Count == 0) { Updated.Conditionals = null; } if (Updated.Locations.Count == 0) { Updated.Locations = null; } if (Updated.Overrides.Count == 0) { Updated.Overrides = null; } if (Updated.Properties.Count == 0) { Updated.Properties = null; } if (Updated.Redirects.Count == 0) { Updated.Redirects = null; } if (Updated.Shops.Count == 0) { Updated.Shops = null; } if (Updated.Teleporters.Count == 0) { Updated.Teleporters = null; } if (Updated.Tiles.Count == 0) { Updated.Tiles = null; } if (Updated.Tilesheets.Count == 0) { Updated.Tilesheets = null; } if (Updated.Warps.Count == 0) { Updated.Warps = null; } AdvancedLocationLoaderMod.Logger.Log("Saving converted manifest to file...", LogLevel.Trace); // Save and then parse the updated config File.WriteAllText(filepath, JsonConvert.SerializeObject(Updated, new JsonSerializerSettings() { Formatting = Formatting.Indented, NullValueHandling = NullValueHandling.Ignore, DefaultValueHandling = DefaultValueHandling.Ignore })); AdvancedLocationLoaderMod.Logger.Log("Loading the converted manifest", LogLevel.Trace); Loader1_2.Load(filepath); }