From 541fd4213381443086fd4235cbc1f24c7b6d75d7 Mon Sep 17 00:00:00 2001 From: Katie Durden Date: Wed, 1 Mar 2023 17:20:44 -0800 Subject: [PATCH] Static tiles uses HashSet to lookup entries. More nullchecks changed to use `is`. --- stardew-access/Features/StaticTiles.cs | 201 +++++++++++++------------ 1 file changed, 106 insertions(+), 95 deletions(-) diff --git a/stardew-access/Features/StaticTiles.cs b/stardew-access/Features/StaticTiles.cs index 9b36631..570c24e 100644 --- a/stardew-access/Features/StaticTiles.cs +++ b/stardew-access/Features/StaticTiles.cs @@ -7,6 +7,8 @@ namespace stardew_access.Features { private JObject? staticTilesData = null; private JObject? customTilesData = null; + HashSet>? staticTilesDataSet = null; + HashSet>? customTilesDataSet = null; public StaticTiles() { @@ -20,6 +22,10 @@ namespace stardew_access.Features string json = file.ReadToEnd(); staticTilesData = JObject.Parse(json); } + if (staticTilesData is not null) + { + staticTilesDataSet = new HashSet>(staticTilesData); + } MainClass.InfoLog($"Loaded static-tile.json"); } @@ -35,6 +41,10 @@ namespace stardew_access.Features string json = file.ReadToEnd(); customTilesData = JObject.Parse(json); } + if (customTilesData is not null) + { + customTilesDataSet = new HashSet>(customTilesData); + } MainClass.InfoLog($"Loaded custom-tile.json"); } @@ -77,114 +87,115 @@ namespace stardew_access.Features return getStaticTileInfoAtWithCategory(x, y).name; } - public (string? name, CATEGORY category) getStaticTileInfoAtWithCategory(int x, int y) { - List allData = new List(); - - if (customTilesData != null) allData.Add(customTilesData); - if (staticTilesData != null) allData.Add(staticTilesData); - - foreach (JObject data in allData) { - foreach (KeyValuePair location in data) + public (string? name, CATEGORY category) GetTileFromSet(int x, int y, HashSet> data) { + foreach (KeyValuePair location in data) + { + string locationName = location.Key; + if (locationName.Contains("||") && MainClass.ModHelper != null) { - string locationName = location.Key; - if (locationName.Contains("||") && MainClass.ModHelper != 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.Substring(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.Contains("_") && locationName.ToLower().StartsWith("farm_")) + { + 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 != null) MainClass.DebugLog($"{farmType} {Game1.whichModFarm.MapName}"); + if (farmTypeIndex != 7 || Game1.whichModFarm == null || !farmType.ToLower().Equals(Game1.whichModFarm.MapName.ToLower())) continue; // Not tested but should work + } + + if (locationName.ToLower().Equals("town_joja") && Game1.MasterPlayer.mailReceived.Contains("JojaMember")) + { + locationName = "town"; + } + + if (!Game1.currentLocation.Name.ToLower().Equals(locationName.ToLower())) continue; + if (location.Value == null) continue; + + foreach (var tile in ((JObject)location.Value)) + { + if (tile.Value == null) continue; + + JToken? tileXArray = tile.Value["x"]; + JToken? tileYArray = tile.Value["y"]; + JToken? tileType = tile.Value["type"]; + + if (tileXArray == null || tileYArray == null || tileType == null) + continue; + + bool isXPresent = false; + bool isYPresent = false; + + foreach (var item in tileXArray) { - // 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); - - if (!isLoaded) continue; // Skip if the specified mod is not loaded - } - - if (locationName.Contains("_") && locationName.ToLower().StartsWith("farm_")) - { - 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 != null) MainClass.DebugLog($"{farmType} {Game1.whichModFarm.MapName}"); - if (farmTypeIndex != 7 || Game1.whichModFarm == null || !farmType.ToLower().Equals(Game1.whichModFarm.MapName.ToLower())) continue; // Not tested but should work - } - - if (locationName.ToLower().Equals("town_joja") && Game1.MasterPlayer.mailReceived.Contains("JojaMember")) - { - locationName = "town"; - } - - if (!Game1.currentLocation.Name.ToLower().Equals(locationName.ToLower())) continue; - if (location.Value == null) continue; - - foreach (var tile in ((JObject)location.Value)) - { - if (tile.Value == null) continue; - - JToken? tileXArray = tile.Value["x"]; - JToken? tileYArray = tile.Value["y"]; - JToken? tileType = tile.Value["type"]; - - if (tileXArray == null || tileYArray == null || tileType == null) + if (short.Parse(item.ToString()) != x) continue; - bool isXPresent = false; - bool isYPresent = false; + isXPresent = true; + break; + } - foreach (var item in tileXArray) + foreach (var item in tileYArray) + { + if (short.Parse(item.ToString()) != y) + continue; + + isYPresent = true; + break; + } + + if (isXPresent && isYPresent) + { + string key = tile.Key; + if (key.Contains('[') && key.Contains(']')) { - if (short.Parse(item.ToString()) != x) - continue; + int i1 = key.IndexOf('['); + int i2 = key.LastIndexOf(']'); - isXPresent = true; - break; - } - - foreach (var item in tileYArray) - { - if (short.Parse(item.ToString()) != y) - continue; - - isYPresent = true; - break; - } - - if (isXPresent && isYPresent) - { - string key = tile.Key; - if (key.Contains('[') && key.Contains(']')) + if (i1 < i2) { - int i1 = key.IndexOf('['); - int i2 = key.LastIndexOf(']'); - - if (i1 < i2) - { - key = key.Remove(i1, ++i2 - i1); - } + key = key.Remove(i1, ++i2 - i1); } - - return (key.Trim(), CATEGORY.FromString(tileType.ToString().ToLower())); } + + return (key.Trim(), CATEGORY.FromString(tileType.ToString().ToLower())); } } } + + return (null, CATEGORY.Others); + } + + public (string? name, CATEGORY category) getStaticTileInfoAtWithCategory(int x, int y) { + if (customTilesDataSet != null) return GetTileFromSet(x, y, customTilesDataSet); + if (staticTilesDataSet != null) return GetTileFromSet(x, y, staticTilesDataSet); + return (null, CATEGORY.Others); }