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 ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
*.dll *.dll
stardew-access/assets/custom-tiles.json
.vscode/* .vscode/*
.git-old/ .git-old/

View File

@ -5,38 +5,68 @@ namespace stardew_access.Features
{ {
public class StaticTiles public class StaticTiles
{ {
private JObject? data = null; private JObject? staticTilesData = null;
private JObject? customTilesData = null;
public StaticTiles() public StaticTiles()
{ {
if (MainClass.ModHelper == null) if (MainClass.ModHelper == null)
return; return;
using (StreamReader file = new StreamReader(Path.Combine(MainClass.ModHelper.DirectoryPath, "assets", "static-tiles.json"))) try
{ {
string json = file.ReadToEnd(); using (StreamReader file = new StreamReader(Path.Combine(MainClass.ModHelper.DirectoryPath, "assets", "static-tiles.json")))
data = JObject.Parse(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) public bool isAvailable(string locationName)
{ {
if (data == null) List<JObject> allData = new List<JObject>();
return false;
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); if (location.Key.Contains("||") && MainClass.ModHelper != null)
string locationNameInJson = location.Key.Remove(location.Key.LastIndexOf("||")); {
bool isLoaded = MainClass.ModHelper.ModRegistry.IsLoaded(uniqueModID); 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 (!isLoaded) continue; // Skip if the specified mod is not loaded
if (locationName.ToLower().Equals(locationNameInJson.ToLower())) return true; 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; return false;
@ -49,94 +79,98 @@ namespace stardew_access.Features
public (string? name, CATEGORY category) getStaticTileInfoAtWithCategory(int x, int y) public (string? name, CATEGORY category) getStaticTileInfoAtWithCategory(int x, int y)
{ {
if (data == null) List<JObject> allData = new List<JObject>();
return (null, CATEGORY.Others);
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 if (location.Key.Contains("||") && MainClass.ModHelper != null)
// 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 (tile.Value == null) // Mod Specific Tiles
continue; // 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"]; if (!isLoaded) continue; // Skip if the specified mod is not loaded
JToken? tileYArray = tile.Value["y"]; if (!Game1.currentLocation.Name.ToLower().Equals(locationName.ToLower())) continue;
JToken? tileType = tile.Value["type"]; }
else if (!Game1.currentLocation.Name.ToLower().Equals(location.Key.ToLower())) continue;
if (tileXArray == null || tileYArray == null || tileType == null) if (location.Value != null)
continue; foreach (var tile in ((JObject)location.Value))
bool isXPresent = false;
bool isYPresent = false;
foreach (var item in tileXArray)
{ {
if (short.Parse(item.ToString()) == x) if (tile.Value == null)
{ continue;
isXPresent = true;
break;
}
}
foreach (var item in tileYArray) JToken? tileXArray = tile.Value["x"];
{ JToken? tileYArray = tile.Value["y"];
if (short.Parse(item.ToString()) == y) JToken? tileType = tile.Value["type"];
{
isYPresent = true;
break;
}
}
if (isXPresent && isYPresent) if (tileXArray == null || tileYArray == null || tileType == null)
{ continue;
string key = tile.Key;
if (key.Contains('[') && key.Contains(']'))
{
int i1 = key.IndexOf('[');
int i2 = key.LastIndexOf(']');
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); return (null, CATEGORY.Others);
} }
} }