Rework MapCache API.

This clarifies the ownership of the underlying
IReadOnlyPackage objects, and avoids passing
around some unnecessary state.
This commit is contained in:
Paul Chote
2025-03-02 21:19:50 +00:00
committed by Gustas Kažukauskas
parent 5c0b82e65b
commit e186dc428e
7 changed files with 72 additions and 62 deletions

View File

@@ -87,8 +87,6 @@ namespace OpenRA
if (!modData.Manifest.Contains<MapGrid>())
return;
var mapGrid = modData.Manifest.Get<MapGrid>();
// Enumerate map directories
foreach (var kv in modData.Manifest.MapFolders)
{
@@ -120,28 +118,29 @@ namespace OpenRA
}
mapLocations.Add(package, classification);
mapDirectoryTrackers.Add(new MapDirectoryTracker(mapGrid, package, classification));
mapDirectoryTrackers.Add(new MapDirectoryTracker(package, classification));
}
// PERF: Load the mod YAML once outside the loop, and reuse it when resolving each maps custom YAML.
var modDataRules = modData.GetRulesYaml();
var gridType = modData.Manifest.Get<MapGrid>().Type;
foreach (var kv in MapLocations)
{
foreach (var map in kv.Key.Contents)
LoadMapInternal(map, kv.Key, kv.Value, mapGrid, null, modDataRules);
LoadMapInternal(map, kv.Key, kv.Value, null, gridType, modDataRules);
}
// We only want to track maps in runtime, not at loadtime
LastModifiedMap = null;
}
public void LoadMap(string map, IReadOnlyPackage package, MapClassification classification, MapGrid mapGrid, string oldMap)
public void LoadMap(string map, IReadOnlyPackage package, MapClassification classification, string oldMap)
{
LoadMapInternal(map, package, classification, mapGrid, oldMap, null);
LoadMapInternal(map, package, classification, oldMap);
}
void LoadMapInternal(string map, IReadOnlyPackage package, MapClassification classification, MapGrid mapGrid, string oldMap,
IEnumerable<List<MiniYamlNode>> modDataRules)
void LoadMapInternal(string map, IReadOnlyPackage package, MapClassification classification, string oldMap,
MapGridType? gridType = null, IEnumerable<List<MiniYamlNode>> modDataRules = null)
{
IReadOnlyPackage mapPackage = null;
try
@@ -152,10 +151,8 @@ namespace OpenRA
if (mapPackage != null)
{
var uid = Map.ComputeUID(mapPackage);
previews[uid].UpdateFromMap(mapPackage, package, classification, modData.Manifest.MapCompatibility, mapGrid.Type, modDataRules);
// Freeing the package to save memory if there is a lot of Maps
previews[uid].DisposePackage();
previews[uid].UpdateFromMapWithoutOwningPackage(mapPackage, package, classification, gridType, modDataRules);
mapPackage.Dispose();
if (oldMap != uid)
{
@@ -257,7 +254,7 @@ namespace OpenRA
var yaml = MiniYaml.FromStream(result, url, stringPool: stringPool);
foreach (var kv in yaml)
previews[kv.Key].UpdateRemoteSearch(MapStatus.DownloadAvailable, kv.Value, modData.Manifest.MapCompatibility, mapDetailsReceived);
previews[kv.Key].UpdateRemoteSearch(MapStatus.DownloadAvailable, kv.Value, mapDetailsReceived);
foreach (var uid in batchUids)
{