Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache source maps for a major performance optimization #1085

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
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
16 changes: 15 additions & 1 deletion ContentPatcher/Framework/PatchLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ internal class PatchLoader
nameof(ConditionType.TargetWithoutPath)
);

private readonly Dictionary<string, xTile.Map> 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
Expand Down Expand Up @@ -820,7 +832,9 @@ bool TryParseFields(IContext context, PatchConfig rawFields, out List<EditDataPa
migrator: rawContentPack.Migrator,
parentPatch: parentPatch,
monitor: this.Monitor,
parseAssetName: this.ParseAssetName
parseAssetName: this.ParseAssetName,
populateMapCache: this.PopulateMapCache,
tryGetMapCache: this.TryGetMapCache
);
}
break;
Expand Down
14 changes: 12 additions & 2 deletions ContentPatcher/Framework/Patches/EditMapPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ internal class EditMapPatch : Patch
/// <summary>Encapsulates monitoring and logging.</summary>
private readonly IMonitor Monitor;

private readonly Func<string, Map?> TryGetMapCache;
private readonly Action<string, Map> PopulateMapCache;

/// <summary>The map area from which to read tiles.</summary>
private readonly TokenRectangle? FromArea;

Expand Down Expand Up @@ -85,7 +88,7 @@ internal class EditMapPatch : Patch
/// <param name="parentPatch">The parent patch for which this patch was loaded, if any.</param>
/// <param name="monitor">Encapsulates monitoring and logging.</param>
/// <param name="parseAssetName">Parse an asset name.</param>
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>? addWarps, IEnumerable<ITextOperation>? textOperations, UpdateRate updateRate, InvariantDictionary<IManagedTokenString>? inheritedLocalTokens, InvariantDictionary<IManagedTokenString>? localTokens, IContentPack contentPack, IRuntimeMigration migrator, IPatch? parentPatch, IMonitor monitor, Func<string, IAssetName> parseAssetName)
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>? 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)
: base(
indexPath: indexPath,
path: path,
Expand All @@ -112,6 +115,8 @@ public EditMapPatch(int[] indexPath, LogPathBuilder path, IManagedTokenString as
this.AddWarps = addWarps?.Reverse().ToArray() ?? []; // reversing the warps allows later ones to 'overwrite' earlier ones, since the game checks them in the listed order
this.TextOperations = textOperations?.ToArray() ?? [];
this.Monitor = monitor;
this.TryGetMapCache = tryGetMapCache;
this.PopulateMapCache = populateMapCache;

this.Contextuals
.Add(this.FromArea)
Expand Down Expand Up @@ -144,7 +149,12 @@ public override void Edit<T>(IAssetData asset)
// apply map area patch
if (this.AppliesMapPatch)
{
Map source = this.ContentPack.ModContent.Load<Map>(this.FromAsset!);
Map? source = this.TryGetMapCache(this.ContentPack.Manifest.UniqueID + "/" + this.FromAsset!);
if (source == null)
{
source = this.ContentPack.ModContent.Load<Map>(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}");
}
Expand Down