diff --git a/ContentPatcher/Framework/PatchLoader.cs b/ContentPatcher/Framework/PatchLoader.cs index f49dc8272..df063ef05 100644 --- a/ContentPatcher/Framework/PatchLoader.cs +++ b/ContentPatcher/Framework/PatchLoader.cs @@ -54,6 +54,18 @@ internal class PatchLoader nameof(ConditionType.TargetWithoutPath) ); + private readonly Dictionary RawMapCache = []; + + internal xTile.Map? TryGetMapCache(string filename) + { + this.RawMapCache.TryGetValue(filename, out var map); + return map; + } + internal void PopulateMapCache(string filename, xTile.Map map) + { + this.RawMapCache.TryAdd(filename, map); + } + // TODO: Add clear function for Reload command /********* ** Public methods @@ -817,7 +829,9 @@ bool TryParseFields(IContext context, PatchConfig rawFields, out ListEncapsulates monitoring and logging. private readonly IMonitor Monitor; + private readonly Func TryGetMapCache; + private readonly Action PopulateMapCache; + /// The map area from which to read tiles. private readonly TokenRectangle? FromArea; @@ -89,7 +92,7 @@ internal class EditMapPatch : Patch /// The parent patch for which this patch was loaded, if any. /// Encapsulates monitoring and logging. /// Parse an asset name. - public EditMapPatch(int[] indexPath, LogPathBuilder path, IManagedTokenString assetName, IManagedTokenString? assetLocale, AssetEditPriority priority, IEnumerable conditions, IManagedTokenString? fromAsset, TokenRectangle? fromArea, TokenRectangle? toArea, PatchMapMode patchMode, IEnumerable? mapProperties, IEnumerable? mapTiles, IEnumerable? addNpcWarps, IEnumerable? addWarps, IEnumerable? textOperations, UpdateRate updateRate, InvariantDictionary? inheritedLocalTokens, InvariantDictionary? localTokens, IContentPack contentPack, IRuntimeMigration migrator, IPatch? parentPatch, IMonitor monitor, Func parseAssetName) + public EditMapPatch(int[] indexPath, LogPathBuilder path, IManagedTokenString assetName, IManagedTokenString? assetLocale, AssetEditPriority priority, IEnumerable conditions, IManagedTokenString? fromAsset, TokenRectangle? fromArea, TokenRectangle? toArea, PatchMapMode patchMode, IEnumerable? mapProperties, IEnumerable? mapTiles, IEnumerable? addNpcWarps, IEnumerable? addWarps, IEnumerable? textOperations, UpdateRate updateRate, InvariantDictionary? inheritedLocalTokens, InvariantDictionary? localTokens, IContentPack contentPack, IRuntimeMigration migrator, IPatch? parentPatch, IMonitor monitor, Func parseAssetName, Action populateMapCache, Func tryGetMapCache) : base( indexPath: indexPath, path: path, @@ -117,6 +120,8 @@ public EditMapPatch(int[] indexPath, LogPathBuilder path, IManagedTokenString as this.AddWarps = addWarps?.Reverse().ToArray() ?? []; this.TextOperations = textOperations?.ToArray() ?? []; this.Monitor = monitor; + this.TryGetMapCache = tryGetMapCache; + this.PopulateMapCache = populateMapCache; this.Contextuals .Add(this.FromArea) @@ -150,7 +155,12 @@ public override void Edit(IAssetData asset) // apply map area patch if (this.AppliesMapPatch) { - Map source = this.ContentPack.ModContent.Load(this.FromAsset!); + Map? source = this.TryGetMapCache(this.ContentPack.Manifest.UniqueID + "/" + this.FromAsset!); + if (source == null) + { + source = this.ContentPack.ModContent.Load(this.FromAsset!); + this.PopulateMapCache(this.ContentPack.Manifest.UniqueID + "/" + this.FromAsset!, source); + } if (!this.TryApplyMapPatch(source, targetAsset, out string? error)) this.WarnForPatch($"map patch couldn't be applied: {error}"); }