Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ContentPatcher/Framework/Commands/CommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ private static ICommand[] BuildCommands(PerScreen<ScreenManager> screenManager,
monitor: monitor,
getPatchLoader: () => screenManager.Value.PatchLoader,
getPatchManager: () => screenManager.Value.PatchManager,
getScreenManager: () => screenManager.Value,
contentPacks: contentPacks,
updateContext: updateContext
),
Expand Down
28 changes: 17 additions & 11 deletions ContentPatcher/Framework/Commands/Commands/ReloadCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ internal class ReloadCommand : BaseCommand
/// <summary>Manages loaded patches.</summary>
private readonly Func<PatchManager> GetPatchManager;

/// <summary>Manages content assets for a screen.</summary>
private readonly Func<ScreenManager> GetScreenManager;

/// <summary>The loaded content packs.</summary>
private readonly LoadedContentPack[] ContentPacks;

Expand All @@ -37,11 +40,12 @@ internal class ReloadCommand : BaseCommand
/// <param name="getPatchManager">Manages loaded patches.</param>
/// <param name="contentPacks">The loaded content packs.</param>
/// <param name="updateContext">A callback which immediately updates the current condition context.</param>
public ReloadCommand(IMonitor monitor, Func<PatchLoader> getPatchLoader, Func<PatchManager> getPatchManager, LoadedContentPack[] contentPacks, Action updateContext)
public ReloadCommand(IMonitor monitor, Func<PatchLoader> getPatchLoader, Func<PatchManager> getPatchManager, Func<ScreenManager> getScreenManager, LoadedContentPack[] contentPacks, Action updateContext)
: base(monitor, "reload")
{
this.GetPatchLoader = getPatchLoader;
this.GetPatchManager = getPatchManager;
this.GetScreenManager = getScreenManager;
this.ContentPacks = contentPacks;
this.UpdateContext = updateContext;
}
Expand All @@ -65,6 +69,7 @@ public override void Handle(string[] args)
{
var patchLoader = this.GetPatchLoader();
var patchManager = this.GetPatchManager();
var screenManager = this.GetScreenManager();

// get args
string packId = ArgUtility.Get(args, 0, allowBlank: false);
Expand All @@ -76,7 +81,7 @@ public override void Handle(string[] args)
}

// get pack
RawContentPack? pack = this.ContentPacks.SingleOrDefault(p => p.Manifest.UniqueID == packId);
LoadedContentPack? pack = this.ContentPacks.SingleOrDefault(p => p.Manifest.UniqueID == packId);
if (pack == null)
{
this.Monitor.Log($"No Content Patcher content pack with the unique ID \"{packId}\".", LogLevel.Error);
Expand Down Expand Up @@ -121,15 +126,16 @@ public override void Handle(string[] args)
return;
}

// reload patches
patchLoader.LoadPatches(
contentPack: pack,
rawPatches: pack.Content.Changes,
inheritLocalTokens: null,
rootIndexPath: [pack.Index],
path: new LogPathBuilder(pack.Manifest.Name),
parentPatch: null
);
try
{
pack.ReloadConfig();
}
catch (Exception ex)
{
this.Monitor.Log($"Error reloading configuration for content pack '{pack.ContentPack.Manifest.Name}'. Technical details:\n{ex}", LogLevel.Error);
}

screenManager.ReloadContentPack(pack);
}

// make the changes apply
Expand Down
2 changes: 1 addition & 1 deletion ContentPatcher/Framework/ConfigFileHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public InvariantDictionary<ConfigField> Read(IContentPack contentPack, Invariant
/// <param name="contentPack">The content pack.</param>
/// <param name="config">The configuration to save.</param>
/// <param name="modHelper">The mod helper through which to save the file.</param>
public void Save(IContentPack contentPack, InvariantDictionary<ConfigField> config, IModHelper modHelper)
public void Save(IContentPack contentPack, InvariantDictionary<ConfigField> config)
{
// save if settings valid
if (config.Any())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internal class GenericModConfigMenuIntegrationForContentPack : IGenericModConfig
private readonly IContentPack ContentPack;

/// <summary>The config model.</summary>
private readonly InvariantDictionary<ConfigField> Config;
public InvariantDictionary<ConfigField> Config { get; set; }

/// <summary>Parse a comma-delimited set of case-insensitive condition values.</summary>
private readonly Func<string, IInvariantSet> ParseCommaDelimitedField;
Expand Down
21 changes: 18 additions & 3 deletions ContentPatcher/Framework/LoadedContentPack.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using ContentPatcher.Framework.ConfigModels;
using Pathoschild.Stardew.Common.Integrations.GenericModConfigMenu;
using Pathoschild.Stardew.Common.Utilities;
using StardewModdingAPI;

namespace ContentPatcher.Framework;

Expand All @@ -13,8 +15,10 @@ internal class LoadedContentPack : RawContentPack
public ConfigFileHandler ConfigFileHandler { get; }

/// <summary>The content pack's configuration.</summary>
public InvariantDictionary<ConfigField> Config { get; }

public InvariantDictionary<ConfigField> Config { get; private set; }
public GenericModConfigMenuIntegrationForContentPack GMCMConfigMenu { get; internal set; }
public GenericModConfigMenuIntegration<InvariantDictionary<ConfigField>> GMCMAPI { get; internal set; }
private IMonitor Monitor { get; }

/*********
** Public methods
Expand All @@ -23,10 +27,21 @@ internal class LoadedContentPack : RawContentPack
/// <param name="contentPack">The raw content pack instance.</param>
/// <param name="configFileHandler">Handles reading, normalizing, and saving the configuration for the content pack.</param>
/// <param name="config">The content pack's configuration.</param>
public LoadedContentPack(RawContentPack contentPack, ConfigFileHandler configFileHandler, InvariantDictionary<ConfigField> config)
public LoadedContentPack(RawContentPack contentPack, ConfigFileHandler configFileHandler, InvariantDictionary<ConfigField> config, IMonitor monitor)
: base(contentPack)
{
this.ConfigFileHandler = configFileHandler;
this.Config = config;
this.Monitor = monitor;
}

public void ReloadConfig()
{
var config = this.ConfigFileHandler.Read(this.ContentPack, this.Content.ConfigSchema, this.Content.Format!);
this.ConfigFileHandler.Save(this.ContentPack, config);
this.Config = config;

this.GMCMConfigMenu.Config = config;
this.GMCMConfigMenu.Register(this.GMCMAPI, this.Monitor);
}
}
6 changes: 6 additions & 0 deletions ContentPatcher/Framework/Locations/CustomLocationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using StardewModdingAPI;
using StardewModdingAPI.Events;
using StardewValley;
using StardewValley.Extensions;
using StardewValley.GameData.Locations;
using xTile;

Expand Down Expand Up @@ -66,6 +67,11 @@ public bool TryAddCustomLocationConfig(CustomLocationConfig? config, IContentPac
return true;
}

public int ClearContentPack(IContentPack contentPack)
{
return this.CustomLocations.RemoveWhere(location => location.ContentPack.Manifest.UniqueID == contentPack.Manifest.UniqueID);
}

/// <summary>Enforce that custom location maps have a unique name.</summary>
public void EnforceUniqueNames()
{
Expand Down
1 change: 1 addition & 0 deletions ContentPatcher/Framework/PatchLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ public void UnloadPatchesLoadedBy(IPatch parentPatch)
public void UnloadPatchesLoadedBy(RawContentPack pack)
{
this.UnloadPatches(patch => patch.ContentPack == pack.ContentPack);
this.PatchManager.ClearPermanentlyDisabledPatchesForPack(pack.ContentPack);
}

/// <summary>Normalize and parse the given condition values.</summary>
Expand Down
5 changes: 5 additions & 0 deletions ContentPatcher/Framework/PatchManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,11 @@ public IEnumerable<DisabledPatch> GetPermanentlyDisabledPatches()
return this.PermanentlyDisabledPatches;
}

public void ClearPermanentlyDisabledPatchesForPack(IContentPack contentPack)
{
this.PermanentlyDisabledPatches.RemoveWhere(patch => patch.ContentPack.Manifest.UniqueID == contentPack.Manifest.UniqueID);
}

/// <summary>Get patches which load the given asset in the current context.</summary>
/// <param name="request">The asset request being intercepted.</param>
public IEnumerable<LoadPatch> GetCurrentLoaders(AssetRequestedEventArgs request)
Expand Down
17 changes: 17 additions & 0 deletions ContentPatcher/Framework/ScreenManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ internal class ScreenManager
/// <summary>Whether <see cref="Initialize"/> has been called for this instance.</summary>
public bool IsInitialized { get; private set; }

private IInvariantSet InstalledMods;


/*********
** Public methods
Expand All @@ -70,6 +72,7 @@ public ScreenManager(IModHelper helper, IMonitor monitor, IInvariantSet installe
{
this.Helper = helper;
this.Monitor = monitor;
this.InstalledMods = installedMods;
this.TokenManager = new TokenManager(helper.GameContent, installedMods, modTokens);
this.PatchManager = new PatchManager(this.Monitor, this.TokenManager, assetValidators, profiler);
this.PatchLoader = new PatchLoader(this.PatchManager, this.TokenManager, this.Monitor, installedMods, helper.GameContent.ParseAssetName);
Expand Down Expand Up @@ -215,6 +218,20 @@ public void UpdateContext(ContextUpdateType updateType)
this.PatchManager.UpdateContext(this.Helper.GameContent, changedGlobalTokens, updateType);
}

public void ReloadContentPack(LoadedContentPack contentPack)
{
this.TokenManager.ClearLocalToken(contentPack.ContentPack);
int oldLocations = this.CustomLocationManager.ClearContentPack(contentPack.ContentPack);

this.LoadContentPacks([contentPack], this.InstalledMods);

int newLocations = this.CustomLocationManager.GetCustomLocationData().Where(location => location.ContentPack.Manifest.UniqueID == contentPack.Manifest.UniqueID).Count();

if (oldLocations > 0 || newLocations > 0)
{
this.Helper.GameContent.InvalidateCache("Data/Locations");
}
}

/*********
** Private methods
Expand Down
6 changes: 6 additions & 0 deletions ContentPatcher/Framework/TokenManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ public TokenManager(IGameContentHelper contentHelper, IInvariantSet installedMod
this.GlobalContext.Save(new Token(valueProvider));
}

public bool ClearLocalToken(IContentPack pack)
{
string scope = pack.Manifest.UniqueID.Trim();
return this.LocalTokens.Remove(scope);
}

/// <summary>Get the tokens which are defined for a specific content pack. This returns a reference to the list, which can be held for a live view of the tokens. If the content pack isn't currently tracked, this will add it.</summary>
/// <param name="pack">The content pack to manage.</param>
public ModTokenContext TrackLocalTokens(IContentPack pack)
Expand Down
8 changes: 5 additions & 3 deletions ContentPatcher/ModEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,8 @@ group token by token.Mod into modGroup
break;

var configMenu = new GenericModConfigMenuIntegrationForContentPack(contentPack.ContentPack, this.ParseCommaDelimitedField, config);
contentPack.GMCMAPI = api;
contentPack.GMCMConfigMenu = configMenu;
configMenu.Register(api, this.Monitor);
}
}
Expand Down Expand Up @@ -371,7 +373,7 @@ private IInvariantSet GetInstalledMods()
private void OnContentPackConfigChanged(LoadedContentPack contentPack)
{
// re-save config.json
contentPack.ConfigFileHandler.Save(contentPack.ContentPack, contentPack.Config, this.Helper);
contentPack.ConfigFileHandler.Save(contentPack.ContentPack, contentPack.Config);

// update tokens
foreach (var screenManager in this.ScreenManager.GetActiveValues())
Expand Down Expand Up @@ -476,7 +478,7 @@ private IEnumerable<LoadedContentPack> GetContentPacks()
{
configFileHandler = new ConfigFileHandler("config.json", this.ParseCommaDelimitedField, (pack, label, reason) => this.Monitor.Log($"Ignored {pack.Manifest.Name} > {label}: {reason}", LogLevel.Warn));
config = configFileHandler.Read(contentPack, rawContentPack.Content.ConfigSchema, rawContentPack.Content.Format!);
configFileHandler.Save(contentPack, config, this.Helper);
configFileHandler.Save(contentPack, config);
}
catch (Exception ex)
{
Expand All @@ -485,7 +487,7 @@ private IEnumerable<LoadedContentPack> GetContentPacks()
}

// build content pack
yield return new LoadedContentPack(rawContentPack, configFileHandler, config);
yield return new LoadedContentPack(rawContentPack, configFileHandler, config, this.Monitor);
}
}

Expand Down