From dd812851b4d663978a2acf78f95d6ea46a4e4a08 Mon Sep 17 00:00:00 2001 From: Katie Durden Date: Wed, 8 Mar 2023 14:09:13 -0800 Subject: [PATCH] Add base for improved tile coordinate lookup functionality; typo fixes. * Build tile dictionary at game launch to speed up tile lookups during gameplay. * Add GameLaunched event to setup new dictionary after other mods loaded. * Change `StaticTiles.getStaticTileInfoAtWithCategory` to use new dictionary for lookup. * Various typo fixes and other code cleanup --- stardew-access/CustomCommands.cs | 60 ++--- stardew-access/Features/StaticTiles.cs | 284 +++++++++++++++--------- stardew-access/Features/TileInfo.cs | 2 +- stardew-access/ModEntry.cs | 7 + stardew-access/assets/static-tiles.json | 1 + 5 files changed, 218 insertions(+), 136 deletions(-) diff --git a/stardew-access/CustomCommands.cs b/stardew-access/CustomCommands.cs index 8d10fc2..60c9482 100644 --- a/stardew-access/CustomCommands.cs +++ b/stardew-access/CustomCommands.cs @@ -18,7 +18,7 @@ namespace stardew_access return; #region Read Tile - helper.ConsoleCommands.Add("readtile", "Toggle read tile feature.", (string commmand, string[] args) => + helper.ConsoleCommands.Add("readtile", "Toggle read tile feature.", (string command, string[] args) => { MainClass.Config.ReadTile = !MainClass.Config.ReadTile; helper.WriteConfig(MainClass.Config); @@ -26,7 +26,7 @@ namespace stardew_access MainClass.InfoLog("Read Tile is " + (MainClass.Config.ReadTile ? "on" : "off")); }); - helper.ConsoleCommands.Add("flooring", "Toggle flooring in read tile.", (string commmand, string[] args) => + helper.ConsoleCommands.Add("flooring", "Toggle flooring in read tile.", (string command, string[] args) => { MainClass.Config.ReadFlooring = !MainClass.Config.ReadFlooring; helper.WriteConfig(MainClass.Config); @@ -34,7 +34,7 @@ namespace stardew_access MainClass.InfoLog("Flooring is " + (MainClass.Config.ReadFlooring ? "on" : "off")); }); - helper.ConsoleCommands.Add("watered", "Toggle speaking watered or unwatered for crops.", (string commmand, string[] args) => + helper.ConsoleCommands.Add("watered", "Toggle speaking watered or unwatered for crops.", (string command, string[] args) => { MainClass.Config.WateredToggle = !MainClass.Config.WateredToggle; helper.WriteConfig(MainClass.Config); @@ -44,7 +44,7 @@ namespace stardew_access #endregion #region Radar Feature - helper.ConsoleCommands.Add("radar", "Toggle radar feature.", (string commmand, string[] args) => + helper.ConsoleCommands.Add("radar", "Toggle radar feature.", (string command, string[] args) => { MainClass.Config.Radar = !MainClass.Config.Radar; helper.WriteConfig(MainClass.Config); @@ -52,14 +52,14 @@ namespace stardew_access MainClass.InfoLog("Radar " + (MainClass.Config.Radar ? "on" : "off")); }); - helper.ConsoleCommands.Add("rdebug", "Toggle debugging in radar feature.", (string commmand, string[] args) => + helper.ConsoleCommands.Add("rdebug", "Toggle debugging in radar feature.", (string command, string[] args) => { MainClass.radarDebug = !MainClass.radarDebug; MainClass.InfoLog("Radar debugging " + (MainClass.radarDebug ? "on" : "off")); }); - helper.ConsoleCommands.Add("rstereo", "Toggle stereo sound in radar feature.", (string commmand, string[] args) => + helper.ConsoleCommands.Add("rstereo", "Toggle stereo sound in radar feature.", (string command, string[] args) => { MainClass.Config.RadarStereoSound = !MainClass.Config.RadarStereoSound; helper.WriteConfig(MainClass.Config); @@ -67,14 +67,14 @@ namespace stardew_access MainClass.InfoLog("Stereo sound is " + (MainClass.Config.RadarStereoSound ? "on" : "off")); }); - helper.ConsoleCommands.Add("rfocus", "Toggle focus mode in radar feature.", (string commmand, string[] args) => + helper.ConsoleCommands.Add("rfocus", "Toggle focus mode in radar feature.", (string command, string[] args) => { bool focus = MainClass.RadarFeature.ToggleFocus(); MainClass.InfoLog("Focus mode is " + (focus ? "on" : "off")); }); - helper.ConsoleCommands.Add("rdelay", "Set the delay of radar feature in milliseconds.", (string commmand, string[] args) => + helper.ConsoleCommands.Add("rdelay", "Set the delay of radar feature in milliseconds.", (string command, string[] args) => { string? delayInString = null; @@ -107,7 +107,7 @@ namespace stardew_access }); - helper.ConsoleCommands.Add("rrange", "Set the range of radar feature.", (string commmand, string[] args) => + helper.ConsoleCommands.Add("rrange", "Set the range of radar feature.", (string command, string[] args) => { string? rangeInString = null; @@ -142,7 +142,7 @@ namespace stardew_access #region Exclusions - helper.ConsoleCommands.Add("readd", "Add an object key to the exclusions list of radar feature.", (string commmand, string[] args) => + helper.ConsoleCommands.Add("readd", "Add an object key to the exclusions list of radar feature.", (string command, string[] args) => { string? keyToAdd = null; @@ -167,7 +167,7 @@ namespace stardew_access } }); - helper.ConsoleCommands.Add("reremove", "Remove an object key from the exclusions list of radar feature.", (string commmand, string[] args) => + helper.ConsoleCommands.Add("reremove", "Remove an object key from the exclusions list of radar feature.", (string command, string[] args) => { string? keyToAdd = null; @@ -192,7 +192,7 @@ namespace stardew_access } }); - helper.ConsoleCommands.Add("relist", "List all the exclusions in the radar feature.", (string commmand, string[] args) => + helper.ConsoleCommands.Add("relist", "List all the exclusions in the radar feature.", (string command, string[] args) => { if (MainClass.RadarFeature.exclusions.Count > 0) { @@ -209,20 +209,20 @@ namespace stardew_access } }); - helper.ConsoleCommands.Add("reclear", "Clear the focus exclusions in the radar featrure.", (string commmand, string[] args) => + helper.ConsoleCommands.Add("reclear", "Clear the focus exclusions in the radar featrure.", (string command, string[] args) => { MainClass.RadarFeature.exclusions.Clear(); MainClass.InfoLog($"Cleared the focus list in the exclusions feature."); }); - helper.ConsoleCommands.Add("recount", "Number of exclusions in the radar feature.", (string commmand, string[] args) => + helper.ConsoleCommands.Add("recount", "Number of exclusions in the radar feature.", (string command, string[] args) => { MainClass.InfoLog($"There are {MainClass.RadarFeature.exclusions.Count} exclusiond in the radar feature."); }); #endregion #region Focus - helper.ConsoleCommands.Add("rfadd", "Add an object key to the focus list of radar feature.", (string commmand, string[] args) => + helper.ConsoleCommands.Add("rfadd", "Add an object key to the focus list of radar feature.", (string command, string[] args) => { string? keyToAdd = null; @@ -247,7 +247,7 @@ namespace stardew_access } }); - helper.ConsoleCommands.Add("rfremove", "Remove an object key from the focus list of radar feature.", (string commmand, string[] args) => + helper.ConsoleCommands.Add("rfremove", "Remove an object key from the focus list of radar feature.", (string command, string[] args) => { string? keyToAdd = null; @@ -272,7 +272,7 @@ namespace stardew_access } }); - helper.ConsoleCommands.Add("rflist", "List all the exclusions in the radar feature.", (string commmand, string[] args) => + helper.ConsoleCommands.Add("rflist", "List all the exclusions in the radar feature.", (string command, string[] args) => { if (MainClass.RadarFeature.focus.Count > 0) { @@ -289,13 +289,13 @@ namespace stardew_access } }); - helper.ConsoleCommands.Add("rfclear", "Clear the focus list in the radar featrure.", (string commmand, string[] args) => + helper.ConsoleCommands.Add("rfclear", "Clear the focus list in the radar featrure.", (string command, string[] args) => { MainClass.RadarFeature.focus.Clear(); MainClass.InfoLog($"Cleared the focus list in the radar feature."); }); - helper.ConsoleCommands.Add("rfcount", "Number of list in the radar feature.", (string commmand, string[] args) => + helper.ConsoleCommands.Add("rfcount", "Number of list in the radar feature.", (string command, string[] args) => { MainClass.InfoLog($"There are {MainClass.RadarFeature.focus.Count} objects in the focus list in the radar feature."); }); @@ -304,7 +304,7 @@ namespace stardew_access #endregion #region Tile marking - helper.ConsoleCommands.Add("mark", "Marks the player's position for use in building construction in Carpenter Menu.", (string commmand, string[] args) => + helper.ConsoleCommands.Add("mark", "Marks the player's position for use in building construction in Carpenter Menu.", (string command, string[] args) => { if (Game1.currentLocation is not Farm) { @@ -332,7 +332,7 @@ namespace stardew_access MainClass.InfoLog($"Location {(int)Game1.player.getTileX()}x {(int)Game1.player.getTileY()}y added at {index} index."); }); - helper.ConsoleCommands.Add("marklist", "List all marked positions.", (string commmand, string[] args) => + helper.ConsoleCommands.Add("marklist", "List all marked positions.", (string command, string[] args) => { string toPrint = ""; for (int i = 0; i < BuildingOperations.marked.Length; i++) @@ -349,12 +349,12 @@ namespace stardew_access MainClass.InfoLog($"Marked positions:{toPrint}\nOpen command menu and use pageup and pagedown to check the list"); }); - helper.ConsoleCommands.Add("buildlist", "List all buildings for selection for upgrading/demolishing/painting", (string commmand, string[] args) => + helper.ConsoleCommands.Add("buildlist", "List all buildings for selection for upgrading/demolishing/painting", (string command, string[] args) => { onBuildListCalled(); }); - helper.ConsoleCommands.Add("buildsel", "Select the building index which you want to upgrade/demolish/paint", (string commmand, string[] args) => + helper.ConsoleCommands.Add("buildsel", "Select the building index which you want to upgrade/demolish/paint", (string command, string[] args) => { if ((Game1.activeClickableMenu is not CarpenterMenu && Game1.activeClickableMenu is not PurchaseAnimalsMenu && Game1.activeClickableMenu is not AnimalQueryMenu) || !CarpenterMenuPatch.isOnFarm) { @@ -451,28 +451,28 @@ namespace stardew_access #endregion #region Other - helper.ConsoleCommands.Add("refsr", "Refresh screen reader", (string commmand, string[] args) => + helper.ConsoleCommands.Add("refsr", "Refresh screen reader", (string command, string[] args) => { MainClass.ScreenReader.InitializeScreenReader(); MainClass.InfoLog("Screen Reader refreshed!"); }); - helper.ConsoleCommands.Add("refmc", "Refresh mod config", (string commmand, string[] args) => + helper.ConsoleCommands.Add("refmc", "Refresh mod config", (string command, string[] args) => { MainClass.Config = helper.ReadConfig(); MainClass.InfoLog("Mod Config refreshed!"); }); - helper.ConsoleCommands.Add("refst", "Refresh static tiles", (string commmand, string[] args) => + helper.ConsoleCommands.Add("refst", "Refresh static tiles", (string command, string[] args) => { MainClass.STiles = new Features.StaticTiles(); MainClass.InfoLog("Static tiles refreshed!"); }); - helper.ConsoleCommands.Add("hnspercent", "Toggle between speaking in percentage or full health and stamina.", (string commmand, string[] args) => + helper.ConsoleCommands.Add("hnspercent", "Toggle between speaking in percentage or full health and stamina.", (string command, string[] args) => { MainClass.Config.HealthNStaminaInPercentage = !MainClass.Config.HealthNStaminaInPercentage; helper.WriteConfig(MainClass.Config); @@ -480,7 +480,7 @@ namespace stardew_access MainClass.InfoLog("Speaking in percentage is " + (MainClass.Config.HealthNStaminaInPercentage ? "on" : "off")); }); - helper.ConsoleCommands.Add("snapmouse", "Toggle snap mouse feature.", (string commmand, string[] args) => + helper.ConsoleCommands.Add("snapmouse", "Toggle snap mouse feature.", (string command, string[] args) => { MainClass.Config.SnapMouse = !MainClass.Config.SnapMouse; helper.WriteConfig(MainClass.Config); @@ -488,7 +488,7 @@ namespace stardew_access MainClass.InfoLog("Snap Mouse is " + (MainClass.Config.SnapMouse ? "on" : "off")); }); - helper.ConsoleCommands.Add("warning", "Toggle warnings feature.", (string commmand, string[] args) => + helper.ConsoleCommands.Add("warning", "Toggle warnings feature.", (string command, string[] args) => { MainClass.Config.Warning = !MainClass.Config.Warning; helper.WriteConfig(MainClass.Config); @@ -496,7 +496,7 @@ namespace stardew_access MainClass.InfoLog("Warnings is " + (MainClass.Config.Warning ? "on" : "off")); }); - helper.ConsoleCommands.Add("tts", "Toggles the screen reader/tts", (string commmand, string[] args) => + helper.ConsoleCommands.Add("tts", "Toggles the screen reader/tts", (string command, string[] args) => { MainClass.Config.TTS = !MainClass.Config.TTS; helper.WriteConfig(MainClass.Config); diff --git a/stardew-access/Features/StaticTiles.cs b/stardew-access/Features/StaticTiles.cs index 34a36b5..dbd441d 100644 --- a/stardew-access/Features/StaticTiles.cs +++ b/stardew-access/Features/StaticTiles.cs @@ -1,15 +1,16 @@ using Newtonsoft.Json.Linq; using StardewValley; +using System.Linq; namespace stardew_access.Features { public class StaticTiles { - private JObject? staticTilesData = null; - private JObject? customTilesData = null; - HashSet>? staticTilesDataSet = null; - HashSet>? customTilesDataSet = null; - + private static JObject? staticTilesData = null; + private static JObject? customTilesData = null; + private static Dictionary?>? staticTilesDataDict = null; + private static Dictionary?>? customTilesDataDict = null; + public StaticTiles() { if (MainClass.ModHelper is null) @@ -17,14 +18,13 @@ namespace stardew_access.Features try { - using (StreamReader file = new StreamReader(Path.Combine(MainClass.ModHelper.DirectoryPath, "assets", "static-tiles.json"))) + using (StreamReader file = new(Path.Combine(MainClass.ModHelper.DirectoryPath, "assets", "static-tiles.json"))) { string json = file.ReadToEnd(); staticTilesData = JObject.Parse(json); } if (staticTilesData is not null) { - staticTilesDataSet = new HashSet>(staticTilesData); } MainClass.InfoLog($"Loaded static-tile.json"); @@ -36,14 +36,13 @@ namespace stardew_access.Features try { - using (StreamReader file = new StreamReader(Path.Combine(MainClass.ModHelper.DirectoryPath, "assets", "custom-tiles.json"))) + using (StreamReader file = new(Path.Combine(MainClass.ModHelper.DirectoryPath, "assets", "custom-tiles.json"))) { string json = file.ReadToEnd(); customTilesData = JObject.Parse(json); } if (customTilesData is not null) { - customTilesDataSet = new HashSet>(customTilesData); } MainClass.InfoLog($"Loaded custom-tile.json"); @@ -52,11 +51,12 @@ namespace stardew_access.Features { MainClass.InfoLog($"custom-tiles.json file not found or an error occured while initializing custom-tiles.json\nThe path of the file should be:\n\t{Path.Combine(MainClass.ModHelper.DirectoryPath, "assets", "custom-tiles.json")}"); } + this.SetupTilesDicts(); } - public bool isAvailable(string locationName) + public static bool IsAvailable(string locationName) { - List allData = new List(); + List allData = new(); if (customTilesData != null) allData.Add(customTilesData); if (staticTilesData != null) allData.Add(staticTilesData); @@ -67,7 +67,7 @@ namespace stardew_access.Features { if (location.Key.Contains("||") && MainClass.ModHelper != null) { - string uniqueModID = location.Key.Substring(location.Key.LastIndexOf("||") + 2); + string uniqueModID = location.Key[(location.Key.LastIndexOf("||") + 2)..]; string locationNameInJson = location.Key.Remove(location.Key.LastIndexOf("||")); bool isLoaded = MainClass.ModHelper.ModRegistry.IsLoaded(uniqueModID); @@ -82,127 +82,201 @@ namespace stardew_access.Features return false; } - public (string? name, CATEGORY category) GetTileFromSet(int x, int y, GameLocation currentLocation, HashSet> data) + public static (string? name, CATEGORY category) GetTileFromDict(int x, int y) { + if (staticTilesDataDict is not null && staticTilesDataDict.TryGetValue(Game1.currentLocation.Name, out var locationDict)) + { + if (locationDict is not null && locationDict.TryGetValue(((short)x, (short)y), out var tile)) + { + //MainClass.DebugLog($"Tile ({x}, {y}) is in the dict as {tile.name}."); + return tile; + } + } /*else if (locationDict is null) { + //MainClass.DebugLog($"Skipping null entry for location {Game1.currentLocation.Name}."); + } + else { + MainClass.InfoLog($"Location {Game1.currentLocation.Name} not found in static tiles."); + }*/ + return (null, CATEGORY.Others); + } + + private static Dictionary?>? BuildTilesDict(JObject? data) + { + if (data is null) return null; + //MainClass.DebugLog("Loading dict data"); + var comparer = StringComparer.OrdinalIgnoreCase; + Dictionary?> tilesDict = new(comparer); foreach (KeyValuePair location in data) { - string locationName = location.Key; - if (locationName.Contains("||") && MainClass.ModHelper is not null) + try { - // Mod Specific Tiles - // We can add tiles that only get detected when the specified mod is loaded. - // Syntax: || - // Example: THe following tile will only be detected if Stardew Valley Expanded mod is installed - // { - // . - // . - // . - // "Town||FlashShifter.StardewValleyExpandedCP":{ - // "":{ - // "x": [], - // "y": [], - // "type": "" - // } - // }, - // . - // . - // . - // } - string uniqueModID = locationName.Substring(locationName.LastIndexOf("||") + 2); - locationName = locationName.Remove(locationName.LastIndexOf("||")); - bool isLoaded = MainClass.ModHelper.ModRegistry.IsLoaded(uniqueModID); + //MainClass.DebugLog($"Entering loop for location {location}."); + if (location.Value is null) continue; + string locationName = location.Key; + if (locationName.Contains("||") && MainClass.ModHelper is not null) + { + /* Mod Specific Tiles + * We can add tiles that only get detected when the specified mod is loaded. + * Syntax: || + * Example: The following tile will only be detected if Stardew Valley Expanded mod is installed + * { + * . + * . + * . + * "Town||FlashShifter.StardewValleyExpandedCP":{ + * "":{ + * "x": [], + * "y": [], + * "type": "" + * } + * }, + * . + * . + * . + * } + */ + string uniqueModID = locationName[(locationName.LastIndexOf("||") + 2)..]; + locationName = locationName.Remove(locationName.LastIndexOf("||")); + bool isLoaded = MainClass.ModHelper.ModRegistry.IsLoaded(uniqueModID); - if (!isLoaded) continue; // Skip if the specified mod is not loaded - } - - if (locationName.StartsWith("farm_", StringComparison.OrdinalIgnoreCase)) - { - string farmType = locationName.Substring(locationName.LastIndexOf("_") + 1); - int farmTypeIndex = getFarmTypeIndex(farmType); - locationName = locationName.Remove(locationName.LastIndexOf("_")); - - if (farmTypeIndex != Game1.whichFarm) continue; // Skip if current farm type does not matches - // if (Game1.whichModFarm is not null) MainClass.DebugLog($"{farmType} {Game1.whichModFarm.MapName}"); - if (farmTypeIndex != 7 || Game1.whichModFarm is null || !farmType.Equals(Game1.whichModFarm.MapName, StringComparison.OrdinalIgnoreCase)) continue; // Not tested but should work - } - - if (locationName.Equals("town_joja", StringComparison.OrdinalIgnoreCase) && Game1.MasterPlayer.mailReceived.Contains("JojaMember")) - { - locationName = "town"; - } - - if (!currentLocation.Name.Equals(locationName, StringComparison.OrdinalIgnoreCase)) continue; - if (location.Value is null) continue; - - foreach (var tile in ((JObject)location.Value)) - { - if (tile.Value is null) continue; - - JToken? tileXArray = tile.Value["x"]; - JToken? tileYArray = tile.Value["y"]; - JToken? tileType = tile.Value["type"]; - - if (tileXArray is null || tileYArray is null || tileType is null) + if (!isLoaded) continue; // Skip if the specified mod is not loaded + } + //MainClass.DebugLog($"Loading tiles for {locationName}."); + if (location.Value.Type == JTokenType.Null) + { + tilesDict.Add(location.Key, null); + //MainClass.DebugLog($"Created null entry for location {location.Key}."); + //MainClass.DebugLog("SPAM!!!"); continue; - - bool isXPresent = false; - bool isYPresent = false; - - foreach (var item in tileXArray) - { - if (short.Parse(item.ToString()) != x) - continue; - - isXPresent = true; - break; } + - foreach (var item in tileYArray) + Dictionary<(short x, short y), (string name, CATEGORY category)>? locationDict = new(); + //MainClass.DebugLog($"Entering tiles loop for {locationName}."); + foreach (var tileInfo in ((JObject)location.Value)) { - if (short.Parse(item.ToString()) != y) - continue; - - isYPresent = true; - break; - } - - if (isXPresent && isYPresent) - { - string key = tile.Key; - if (key.Contains('[') && key.Contains(']')) + if (tileInfo.Value == null) continue; + string key = tileInfo.Key; + var tile = tileInfo.Value; + if (tile.Type == JTokenType.Object ) { - int i1 = key.IndexOf('['); - int i2 = key.LastIndexOf(']'); + JToken? tileXArray = tile["x"]; + JToken? tileYArray = tile["y"]; + JToken? tileType = tile["type"]; - if (i1 < i2) + if (tileXArray is null || tileYArray is null || tileType is null) + continue; + + //MainClass.DebugLog($"Adding tile {key} to location {locationName}."); + if (key.Contains('[') && key.Contains(']')) { - key = key.Remove(i1, ++i2 - i1); + int i1 = key.IndexOf('['); + int i2 = key.LastIndexOf(']'); + + if (i1 < i2) + { + key = key.Remove(i1, ++i2 - i1); + } + } + (string key, CATEGORY category) tileData = (key.Trim(), CATEGORY.FromString(tileType.ToString().ToLower())); + + foreach (var item_x in tileXArray) + { + short x = short.Parse(item_x.ToString()); + foreach (var item_y in tileYArray) + { + short y = short.Parse(item_y.ToString()); + (short x, short y) coords = (x, y); + try + { + locationDict.Add(coords, tileData); + } + catch (System.Exception e) + { + MainClass.ErrorLog($"Failed setting tile {key} for location {locationName}. Reason:\n\t{e}"); + } + } } } - - return (key.Trim(), CATEGORY.FromString(tileType.ToString().ToLower())); + } + //MainClass.DebugLog($"Location Dict has {locationDict.Count} members."); + if (locationDict.Count > 0) + { + //MainClass.DebugLog($"Adding locationDict for {locationName}"); + tilesDict.Add(locationName, locationDict); + //MainClass.DebugLog($"Added locationDict for {locationName}"); + } + } catch (System.Exception e) { + if (location.Value is null || location.Value.Type == JTokenType.Null) + { + tilesDict.Add(location.Key, null); + //MainClass.DebugLog($"Created null entry for location {location.Key}."); + } else { + MainClass.ErrorLog($"Unable to build tiles dict; failed on location {location.Key} with value ({location.Value.GetType()}){location.Value}. Reason:\n\t{e}"); + throw; } } } - return (null, CATEGORY.Others); + if (tilesDict.Count > 0) + { + //MainClass.DebugLog("Dict loaded, returning."); + return tilesDict; + } else { + //MainClass.DebugLog("Dict not loaded, returning null"); + return null; + } } - public string? getStaticTileInfoAt(int x, int y) + public void SetupTilesDicts() { - return getStaticTileInfoAtWithCategory(x, y).name; + //MainClass.DebugLog("Attempting to set dicts"); + try + { + staticTilesDataDict = BuildTilesDict(staticTilesData); + if (staticTilesDataDict is not null) + { + //MainClass.DebugLog($"staticTilesDataDict has {staticTilesDataDict.Count} entries."); + //MainClass.DebugLog($"Keys: {staticTilesDataDict.Keys}"); + } else { + //MainClass.DebugLog("Static tiles not loaded."); + } + } + catch (System.Exception e) + { + MainClass.ErrorLog($"Failed to set static tiles dict. Reason: \n\t{e}"); + } + try + { + customTilesDataDict = BuildTilesDict(customTilesData); + if (customTilesDataDict is not null) + { + //MainClass.DebugLog($"customTilesDataDict has {customTilesDataDict.Count} entries."); + } else { + //MainClass.DebugLog("Custom tiles not loaded."); + } + } + catch (System.Exception e) + { + MainClass.ErrorLog($"Faild to set custom tiles dict. Reason:\n\t{e}"); + } + //MainClass.DebugLog("Successfully created tiles dicts."); } - public (string? name, CATEGORY category) getStaticTileInfoAtWithCategory(int x, int y) + public static string? GetStaticTileInfoAt(int x, int y) { - GameLocation currentLocation = Game1.currentLocation; - if (customTilesDataSet is not null) return GetTileFromSet(x, y, currentLocation, customTilesDataSet); - if (staticTilesDataSet is not null) return GetTileFromSet(x, y, currentLocation, staticTilesDataSet); + return GetStaticTileInfoAtWithCategory(x, y).name; + } + + public static (string? name, CATEGORY category) GetStaticTileInfoAtWithCategory(int x, int y) + { + if (customTilesDataDict is not null) return GetTileFromDict(x, y); + if (staticTilesDataDict is not null) return GetTileFromDict(x, y); return (null, CATEGORY.Others); } - private int getFarmTypeIndex(string farmType) + private static int GetFarmTypeIndex(string farmType) { return farmType.ToLower() switch { diff --git a/stardew-access/Features/TileInfo.cs b/stardew-access/Features/TileInfo.cs index 270c960..9fe824b 100644 --- a/stardew-access/Features/TileInfo.cs +++ b/stardew-access/Features/TileInfo.cs @@ -46,7 +46,7 @@ namespace stardew_access.Features string? resourceClump = getResourceClumpAtTile(x, y, lessInfo); string? farmAnimal = getFarmAnimalAt(Game1.currentLocation, x, y); string? parrot = getParrotPerchAtTile(x, y); - (string? name, CATEGORY category) staticTile = MainClass.STiles.getStaticTileInfoAtWithCategory(x, y); + (string? name, CATEGORY category) staticTile = StaticTiles.GetStaticTileInfoAtWithCategory(x, y); string? bush = getBushAtTile(x, y, lessInfo); if (Game1.currentLocation.isCharacterAtTile(tile) is NPC npc) diff --git a/stardew-access/ModEntry.cs b/stardew-access/ModEntry.cs index 1830c50..8fb43c2 100644 --- a/stardew-access/ModEntry.cs +++ b/stardew-access/ModEntry.cs @@ -141,6 +141,7 @@ namespace stardew_access helper.Events.Input.ButtonPressed += this.OnButtonPressed; helper.Events.GameLoop.UpdateTicked += this.onUpdateTicked; + helper.Events.GameLoop.GameLaunched += this.onGameLaunched; AppDomain.CurrentDomain.DomainUnload += OnExit; AppDomain.CurrentDomain.ProcessExit += OnExit; } @@ -159,6 +160,12 @@ namespace stardew_access return new API(); } + private void onGameLaunched(object? sender, GameLaunchedEventArgs? e) + { + if (sTiles is not null) + sTiles.SetupTilesDicts(); + } + private void onUpdateTicked(object? sender, UpdateTickedEventArgs? e) { if (!Context.IsPlayerFree) diff --git a/stardew-access/assets/static-tiles.json b/stardew-access/assets/static-tiles.json index 7ac8f23..3bce903 100644 --- a/stardew-access/assets/static-tiles.json +++ b/stardew-access/assets/static-tiles.json @@ -674,6 +674,7 @@ "type": "decoration" } }, + "farmhouse": null, "fishshop": { "Shop Counter": { "x": [4, 5, 6],