Skip to content

Commit ea27623

Browse files
committed
Cache source maps as they are not mutated.
TODO: Add ability for patch reload to empty the cache
1 parent e41467c commit ea27623

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

ContentPatcher/Framework/PatchLoader.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,18 @@ internal class PatchLoader
5454
nameof(ConditionType.TargetWithoutPath)
5555
);
5656

57+
private readonly Dictionary<string, xTile.Map> RawMapCache = [];
58+
59+
internal xTile.Map? TryGetMapCache(string filename)
60+
{
61+
this.RawMapCache.TryGetValue(filename, out var map);
62+
return map;
63+
}
64+
internal void PopulateMapCache(string filename, xTile.Map map)
65+
{
66+
this.RawMapCache.TryAdd(filename, map);
67+
}
68+
// TODO: Add clear function for Reload command
5769

5870
/*********
5971
** Public methods
@@ -817,7 +829,9 @@ bool TryParseFields(IContext context, PatchConfig rawFields, out List<EditDataPa
817829
migrator: rawContentPack.Migrator,
818830
parentPatch: parentPatch,
819831
monitor: this.Monitor,
820-
parseAssetName: this.ParseAssetName
832+
parseAssetName: this.ParseAssetName,
833+
populateMapCache: this.PopulateMapCache,
834+
tryGetMapCache: this.TryGetMapCache
821835
);
822836
}
823837
break;

ContentPatcher/Framework/Patches/EditMapPatch.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ internal class EditMapPatch : Patch
3131
/// <summary>Encapsulates monitoring and logging.</summary>
3232
private readonly IMonitor Monitor;
3333

34+
private readonly Func<string, Map?> TryGetMapCache;
35+
private readonly Action<string, Map> PopulateMapCache;
36+
3437
/// <summary>The map area from which to read tiles.</summary>
3538
private readonly TokenRectangle? FromArea;
3639

@@ -89,7 +92,7 @@ internal class EditMapPatch : Patch
8992
/// <param name="parentPatch">The parent patch for which this patch was loaded, if any.</param>
9093
/// <param name="monitor">Encapsulates monitoring and logging.</param>
9194
/// <param name="parseAssetName">Parse an asset name.</param>
92-
public EditMapPatch(int[] indexPath, LogPathBuilder path, IManagedTokenString assetName, IManagedTokenString? assetLocale, AssetEditPriority priority, IEnumerable<Condition> conditions, IManagedTokenString? fromAsset, TokenRectangle? fromArea, TokenRectangle? toArea, PatchMapMode patchMode, IEnumerable<EditMapPatchProperty>? mapProperties, IEnumerable<EditMapPatchTile>? mapTiles, IEnumerable<IManagedTokenString>? addNpcWarps, IEnumerable<IManagedTokenString>? addWarps, IEnumerable<ITextOperation>? textOperations, UpdateRate updateRate, InvariantDictionary<IManagedTokenString>? inheritedLocalTokens, InvariantDictionary<IManagedTokenString>? localTokens, IContentPack contentPack, IRuntimeMigration migrator, IPatch? parentPatch, IMonitor monitor, Func<string, IAssetName> parseAssetName)
95+
public EditMapPatch(int[] indexPath, LogPathBuilder path, IManagedTokenString assetName, IManagedTokenString? assetLocale, AssetEditPriority priority, IEnumerable<Condition> conditions, IManagedTokenString? fromAsset, TokenRectangle? fromArea, TokenRectangle? toArea, PatchMapMode patchMode, IEnumerable<EditMapPatchProperty>? mapProperties, IEnumerable<EditMapPatchTile>? mapTiles, IEnumerable<IManagedTokenString>? addNpcWarps, IEnumerable<IManagedTokenString>? addWarps, IEnumerable<ITextOperation>? textOperations, UpdateRate updateRate, InvariantDictionary<IManagedTokenString>? inheritedLocalTokens, InvariantDictionary<IManagedTokenString>? localTokens, IContentPack contentPack, IRuntimeMigration migrator, IPatch? parentPatch, IMonitor monitor, Func<string, IAssetName> parseAssetName, Action<string, Map> populateMapCache, Func<string, Map?> tryGetMapCache)
9396
: base(
9497
indexPath: indexPath,
9598
path: path,
@@ -117,6 +120,8 @@ public EditMapPatch(int[] indexPath, LogPathBuilder path, IManagedTokenString as
117120
this.AddWarps = addWarps?.Reverse().ToArray() ?? [];
118121
this.TextOperations = textOperations?.ToArray() ?? [];
119122
this.Monitor = monitor;
123+
this.TryGetMapCache = tryGetMapCache;
124+
this.PopulateMapCache = populateMapCache;
120125

121126
this.Contextuals
122127
.Add(this.FromArea)
@@ -150,7 +155,12 @@ public override void Edit<T>(IAssetData asset)
150155
// apply map area patch
151156
if (this.AppliesMapPatch)
152157
{
153-
Map source = this.ContentPack.ModContent.Load<Map>(this.FromAsset!);
158+
Map? source = this.TryGetMapCache(this.ContentPack.Manifest.UniqueID + "/" + this.FromAsset!);
159+
if (source == null)
160+
{
161+
source = this.ContentPack.ModContent.Load<Map>(this.FromAsset!);
162+
this.PopulateMapCache(this.ContentPack.Manifest.UniqueID + "/" + this.FromAsset!, source);
163+
}
154164
if (!this.TryApplyMapPatch(source, targetAsset, out string? error))
155165
this.WarnForPatch($"map patch couldn't be applied: {error}");
156166
}

0 commit comments

Comments
 (0)