Added custom-tiles.json support

- users can add their own tiles in this file just asin static-tiles.json
- the file needs to be placed just where the static-tiles.json is
master
Mohammad Shoaib Khan 2022-10-27 20:05:12 +05:30
parent 1187ba30e7
commit b4560bc9e1
No known key found for this signature in database
GPG Key ID: 3EE32C9DFF520699
2 changed files with 123 additions and 88 deletions

1
.gitignore vendored
View File

@ -4,6 +4,7 @@
## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
*.dll
stardew-access/assets/custom-tiles.json
.vscode/*
.git-old/

View File

@ -5,38 +5,68 @@ namespace stardew_access.Features
{
public class StaticTiles
{
private JObject? data = null;
private JObject? staticTilesData = null;
private JObject? customTilesData = null;
public StaticTiles()
{
if (MainClass.ModHelper == null)
return;
using (StreamReader file = new StreamReader(Path.Combine(MainClass.ModHelper.DirectoryPath, "assets", "static-tiles.json")))
try
{
string json = file.ReadToEnd();
data = JObject.Parse(json);
using (StreamReader file = new StreamReader(Path.Combine(MainClass.ModHelper.DirectoryPath, "assets", "static-tiles.json")))
{
string json = file.ReadToEnd();
staticTilesData = JObject.Parse(json);
}
MainClass.InfoLog($"Loaded static-tile.json");
}
catch (System.Exception)
{
MainClass.ErrorLog($"static-tiles.json file not found or an error occured while initializing static-tiles.json\nThe path of the file should be:\n\t{Path.Combine(MainClass.ModHelper.DirectoryPath, "assets", "static-tiles.json")}");
}
try
{
using (StreamReader file = new StreamReader(Path.Combine(MainClass.ModHelper.DirectoryPath, "assets", "custom-tiles.json")))
{
string json = file.ReadToEnd();
customTilesData = JObject.Parse(json);
}
MainClass.InfoLog($"Loaded custom-tile.json");
}
catch (System.Exception)
{
MainClass.ErrorLog($"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")}");
}
}
public bool isAvailable(string locationName)
{
if (data == null)
return false;
List<JObject> allData = new List<JObject>();
foreach (var location in data)
if (staticTilesData != null) allData.Add(staticTilesData);
if (customTilesData != null) allData.Add(customTilesData);
foreach (JObject data in allData)
{
if (location.Key.Contains("||") && MainClass.ModHelper != null)
foreach (KeyValuePair<string, JToken?> location in data)
{
string uniqueModID = location.Key.Substring(location.Key.LastIndexOf("||") + 2);
string locationNameInJson = location.Key.Remove(location.Key.LastIndexOf("||"));
bool isLoaded = MainClass.ModHelper.ModRegistry.IsLoaded(uniqueModID);
if (location.Key.Contains("||") && MainClass.ModHelper != null)
{
string uniqueModID = location.Key.Substring(location.Key.LastIndexOf("||") + 2);
string locationNameInJson = location.Key.Remove(location.Key.LastIndexOf("||"));
bool isLoaded = MainClass.ModHelper.ModRegistry.IsLoaded(uniqueModID);
if (!isLoaded) continue; // Skip if the specified mod is not loaded
if (locationName.ToLower().Equals(locationNameInJson.ToLower())) return true;
if (!isLoaded) continue; // Skip if the specified mod is not loaded
if (locationName.ToLower().Equals(locationNameInJson.ToLower())) return true;
}
else if (locationName.ToLower().Equals(location.Key.ToLower()))
return true;
}
else if (locationName.ToLower().Equals(location.Key.ToLower()))
return true;
}
return false;
@ -49,94 +79,98 @@ namespace stardew_access.Features
public (string? name, CATEGORY category) getStaticTileInfoAtWithCategory(int x, int y)
{
if (data == null)
return (null, CATEGORY.Others);
List<JObject> allData = new List<JObject>();
foreach (var location in data)
if (staticTilesData != null) allData.Add(staticTilesData);
if (customTilesData != null) allData.Add(customTilesData);
foreach (JObject data in allData)
{
if (location.Key.Contains("||") && MainClass.ModHelper != null)
foreach (KeyValuePair<string, JToken?> location in data)
{
// 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 = location.Key.Substring(location.Key.LastIndexOf("||") + 2);
string locationName = location.Key.Remove(location.Key.LastIndexOf("||"));
bool isLoaded = MainClass.ModHelper.ModRegistry.IsLoaded(uniqueModID);
if (!isLoaded) continue; // Skip if the specified mod is not loaded
if (!Game1.currentLocation.Name.ToLower().Equals(locationName.ToLower())) continue;
}
else if (!Game1.currentLocation.Name.ToLower().Equals(location.Key.ToLower())) continue;
if (location.Value != null)
foreach (var tile in ((JObject)location.Value))
if (location.Key.Contains("||") && MainClass.ModHelper != null)
{
if (tile.Value == null)
continue;
// 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 = location.Key.Substring(location.Key.LastIndexOf("||") + 2);
string locationName = location.Key.Remove(location.Key.LastIndexOf("||"));
bool isLoaded = MainClass.ModHelper.ModRegistry.IsLoaded(uniqueModID);
JToken? tileXArray = tile.Value["x"];
JToken? tileYArray = tile.Value["y"];
JToken? tileType = tile.Value["type"];
if (!isLoaded) continue; // Skip if the specified mod is not loaded
if (!Game1.currentLocation.Name.ToLower().Equals(locationName.ToLower())) continue;
}
else if (!Game1.currentLocation.Name.ToLower().Equals(location.Key.ToLower())) continue;
if (tileXArray == null || tileYArray == null || tileType == null)
continue;
bool isXPresent = false;
bool isYPresent = false;
foreach (var item in tileXArray)
if (location.Value != null)
foreach (var tile in ((JObject)location.Value))
{
if (short.Parse(item.ToString()) == x)
{
isXPresent = true;
break;
}
}
if (tile.Value == null)
continue;
foreach (var item in tileYArray)
{
if (short.Parse(item.ToString()) == y)
{
isYPresent = true;
break;
}
}
JToken? tileXArray = tile.Value["x"];
JToken? tileYArray = tile.Value["y"];
JToken? tileType = tile.Value["type"];
if (isXPresent && isYPresent)
{
string key = tile.Key;
if (key.Contains('[') && key.Contains(']'))
{
int i1 = key.IndexOf('[');
int i2 = key.LastIndexOf(']');
if (tileXArray == null || tileYArray == null || tileType == null)
continue;
if (i1 < i2)
bool isXPresent = false;
bool isYPresent = false;
foreach (var item in tileXArray)
{
if (short.Parse(item.ToString()) == x)
{
key = key.Remove(i1, ++i2 - i1);
isXPresent = true;
break;
}
}
return (key.Trim(), CATEGORY.FromString(tileType.ToString().ToLower()));
}
}
}
foreach (var item in tileYArray)
{
if (short.Parse(item.ToString()) == y)
{
isYPresent = true;
break;
}
}
if (isXPresent && isYPresent)
{
string key = tile.Key;
if (key.Contains('[') && key.Contains(']'))
{
int i1 = key.IndexOf('[');
int i2 = key.LastIndexOf(']');
if (i1 < i2)
{
key = key.Remove(i1, ++i2 - i1);
}
}
return (key.Trim(), CATEGORY.FromString(tileType.ToString().ToLower()));
}
}
}
}
return (null, CATEGORY.Others);
}
}