Static tiles uses HashSet to lookup entries. More nullchecks changed to use `is`.

master
Katie Durden 2023-03-01 17:20:44 -08:00
parent a88172c0cf
commit 541fd42133
1 changed files with 106 additions and 95 deletions

View File

@ -7,6 +7,8 @@ namespace stardew_access.Features
{
private JObject? staticTilesData = null;
private JObject? customTilesData = null;
HashSet<KeyValuePair<string, JToken?>>? staticTilesDataSet = null;
HashSet<KeyValuePair<string, JToken?>>? 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<KeyValuePair<string, JToken>>(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<KeyValuePair<string, JToken>>(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<JObject> allData = new List<JObject>();
if (customTilesData != null) allData.Add(customTilesData);
if (staticTilesData != null) allData.Add(staticTilesData);
foreach (JObject data in allData) {
foreach (KeyValuePair<string, JToken?> location in data)
public (string? name, CATEGORY category) GetTileFromSet(int x, int y, HashSet<KeyValuePair<string, JToken>> data) {
foreach (KeyValuePair<string, JToken?> 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: <location name>||<Mod's unique id, look into the mod's manifest.json for unique id>
// Example: THe following tile will only be detected if Stardew Valley Expanded mod is installed
// {
// .
// .
// .
// "Town||FlashShifter.StardewValleyExpandedCP":{
// "<Tile Name>":{
// "x": [<x location(s)>],
// "y": [<y location(s)>],
// "type": "<Category name>"
// }
// },
// .
// .
// .
// }
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: <location name>||<Mod's unique id, look into the mod's manifest.json for unique id>
// Example: THe following tile will only be detected if Stardew Valley Expanded mod is installed
// {
// .
// .
// .
// "Town||FlashShifter.StardewValleyExpandedCP":{
// "<Tile Name>":{
// "x": [<x location(s)>],
// "y": [<y location(s)>],
// "type": "<Category name>"
// }
// },
// .
// .
// .
// }
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);
}