Skip to content

Commit

Permalink
Cleanup, detailing, moving code to shared libray
Browse files Browse the repository at this point in the history
  • Loading branch information
atravita-mods committed Mar 1, 2022
1 parent 877d527 commit 637d777
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 31 deletions.
26 changes: 14 additions & 12 deletions FarmCaveSpawn/AssetManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,39 @@ namespace FarmCaveSpawn;
/// </summary>
internal class AssetManager : IAssetLoader
{
#pragma warning disable SA1401 // Fields should be private. This is intentional.
private AssetManager()
{
}

/// <summary>
/// Fake asset location for the denylist.
/// Gets the instance of the assetmanager for this mod.
/// </summary>
public readonly string DENYLIST_LOCATION = PathUtilities.NormalizeAssetName("Mods/atravita_FarmCaveSpawn_denylist");
public static AssetManager Instance { get; } = new();

/// <summary>
/// Fake asset location for more locations that can spawn in fruit.
/// Gets fake asset location for the denylist.
/// </summary>
public readonly string ADDITIONAL_LOCATIONS_LOCATION = PathUtilities.NormalizeAssetName("Mods/atravita_FarmCaveSpawn_additionalLocations");
#pragma warning restore SA1401 // Fields should be private

public static string DENYLIST_LOCATION { get; } = PathUtilities.NormalizeAssetName("Mods/atravita_FarmCaveSpawn_denylist");

/// <summary>
/// Gets fake asset location for more locations that can spawn in fruit.
/// </summary>
public static string ADDITIONAL_LOCATIONS_LOCATION { get; } = PathUtilities.NormalizeAssetName("Mods/atravita_FarmCaveSpawn_additionalLocations");

/// <inheritdoc/>
public bool CanLoad<T>(IAssetInfo asset)
{
return asset.AssetNameEquals(this.DENYLIST_LOCATION) || asset.AssetNameEquals(this.ADDITIONAL_LOCATIONS_LOCATION);
}
=> asset.AssetNameEquals(DENYLIST_LOCATION) || asset.AssetNameEquals(ADDITIONAL_LOCATIONS_LOCATION);

/// <inheritdoc/>
public T Load<T>(IAssetInfo asset)
{
if (asset.AssetNameEquals(this.DENYLIST_LOCATION))
if (asset.AssetNameEquals(DENYLIST_LOCATION))
{
return (T)(object)new Dictionary<string, string>
{
};
}
else if (asset.AssetNameEquals(this.ADDITIONAL_LOCATIONS_LOCATION))
else if (asset.AssetNameEquals(ADDITIONAL_LOCATIONS_LOCATION))
{
return (T)(object)new Dictionary<string, string>
{
Expand Down
2 changes: 1 addition & 1 deletion FarmCaveSpawn/FarmCaveSpawn.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<EnableHarmony>True</EnableHarmony>
<DefineConstants>REGEX;</DefineConstants>
</PropertyGroup>

<PropertyGroup>
Expand Down
28 changes: 10 additions & 18 deletions FarmCaveSpawn/ModEntry.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Reflection;
using System.Text.RegularExpressions;
using AtraBase.Toolkit.Extensions;
using AtraShared.Integrations;
using AtraShared.Utils.Extensions;
using Microsoft.Xna.Framework;
Expand All @@ -12,8 +13,6 @@ namespace FarmCaveSpawn;
/// <inheritdoc />
public class ModEntry : Mod
{
private readonly AssetManager assetManager = new();

/// <summary>
/// Sublocation-parsing regex.
/// </summary>
Expand Down Expand Up @@ -93,7 +92,7 @@ public override void Entry(IModHelper helper)
documentation: I18n.ListFruits_Description(),
callback: this.ListFruits);

helper.Content.AssetLoaders.Add(this.assetManager);
helper.Content.AssetLoaders.Add(AssetManager.Instance);
}

/// <summary>
Expand Down Expand Up @@ -172,7 +171,7 @@ private bool ShouldSpawnFruit()
if (Game1.CustomData.TryGetValue("smapi/mod-data/aedenthorn.farmcaveframework/farm-cave-framework-choice", out string? farmcavechoice))
{
// Crosscheck this = probably better to just use the actual value, maybe...
hasFCFbatcave = (farmcavechoice is not null) && (farmcavechoice.ToLowerInvariant().Contains("bat") || farmcavechoice.ToLowerInvariant().Contains("fruit"));
hasFCFbatcave = (farmcavechoice is not null) && (farmcavechoice.Contains("bat", StringComparison.OrdinalIgnoreCase) || farmcavechoice.Contains("fruit", StringComparison.OrdinalIgnoreCase));
this.Monitor.DebugLog(hasFCFbatcave ? "FarmCaveFramework fruit bat cave detected." : "FarmCaveFramework fruit bat cave not detected.");
}

Expand Down Expand Up @@ -237,7 +236,7 @@ private void SpawnFruit(object? sender, DayStartedEventArgs e)

if (this.config.UseModCaves)
{
foreach (string location in this.GetData(this.assetManager.ADDITIONAL_LOCATIONS_LOCATION))
foreach (string location in this.GetData(AssetManager.ADDITIONAL_LOCATIONS_LOCATION))
{
string parseloc = location;
// initialize default limits
Expand All @@ -255,13 +254,7 @@ private void SpawnFruit(object? sender, DayStartedEventArgs e)
{
Match match = matches[0];
parseloc = location[..^match.Value.Length];
foreach (Group group in match.Groups)
{
if (int.TryParse(group.Value, out int result))
{
locLimits[group.Name] = result;
}
}
locLimits.Update(match);
this.Monitor.DebugLog($"Found and parsed sublocation: {parseloc} + ({locLimits["x1"]};{locLimits["y1"]});({locLimits["x2"]};{locLimits["y2"]})");
}
else if (matches.Count >= 2)
Expand All @@ -277,7 +270,7 @@ private void SpawnFruit(object? sender, DayStartedEventArgs e)

if (Game1.getLocationFromName(parseloc) is GameLocation gameLocation)
{
this.Monitor.Log($"Found {gameLocation}");
this.Monitor.DebugLog($"Found {gameLocation}");
foreach (Vector2 v in this.IterateTiles(gameLocation, xstart: locLimits["x1"], xend: locLimits["x2"], ystart: locLimits["y1"], yend: locLimits["y2"]))
{
this.PlaceFruit(gameLocation, v);
Expand All @@ -290,7 +283,7 @@ private void SpawnFruit(object? sender, DayStartedEventArgs e)
}
else
{
this.Monitor.Log(I18n.LocationMissing(loc: location), LogLevel.Debug);
this.Monitor.Log(I18n.LocationMissing(loc: location), LogLevel.Info);
}
}
}
Expand Down Expand Up @@ -405,7 +398,7 @@ private List<int> GetTreeFruits()
return this.VANILLA_FRUIT;
}

List<string> denylist = this.GetData(this.assetManager.DENYLIST_LOCATION);
List<string> denylist = this.GetData(AssetManager.DENYLIST_LOCATION);
List<int> treeFruits = new();

Dictionary<int, string> fruittrees = this.Helper.Content.Load<Dictionary<int, string>>("Data/fruitTrees", ContentSource.GameContent);
Expand All @@ -414,10 +407,9 @@ private List<int> GetTreeFruits()
{
string[] treedata = tree.Split('/', StringSplitOptions.TrimEntries);

if ((this.config.SeasonalOnly == SeasonalBehavior.SeasonalOnly
|| (this.config.SeasonalOnly == SeasonalBehavior.SeasonalExceptWinter && !currentseason.Contains("winter")))
if ((this.config.SeasonalOnly == SeasonalBehavior.SeasonalOnly || (this.config.SeasonalOnly == SeasonalBehavior.SeasonalExceptWinter && !Game1.IsWinter))
&& !treedata[1].Contains(currentseason)
&& (!currentseason.Contains("summer") || !treedata[1].Contains("island")))
&& (!Game1.IsSummer || !treedata[1].Contains("island")))
{
continue;
}
Expand Down

0 comments on commit 637d777

Please sign in to comment.