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

Do not keep map packages loaded on Game start to reduce memory impact #21408

Merged
merged 1 commit into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion OpenRA.Game/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -980,7 +980,7 @@ public static void LoadMap(string launchMap)
Order.Command($"state {Session.ClientState.Ready}")
};

var map = ModData.MapCache.SingleOrDefault(m => m.Uid == launchMap || Path.GetFileName(m.Package.Name) == launchMap);
var map = ModData.MapCache.SingleOrDefault(m => m.Uid == launchMap || Path.GetFileName(m.PackageName) == launchMap);
if (map == null)
throw new ArgumentException($"Could not find map '{launchMap}'.");

Expand Down
3 changes: 3 additions & 0 deletions OpenRA.Game/Map/MapCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ void LoadMapInternal(string map, IReadOnlyPackage package, MapClassification cla
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();

if (oldMap != uid)
{
LastModifiedMap = uid;
Expand Down
2 changes: 1 addition & 1 deletion OpenRA.Game/Map/MapDirectoryTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public void UpdateMaps(MapCache mapcache)
dirty = false;
foreach (var mapAction in mapActionQueue)
{
var map = mapcache.FirstOrDefault(x => x.Package?.Name == mapAction.Key && x.Status == MapStatus.Available);
var map = mapcache.FirstOrDefault(x => x.PackageName == mapAction.Key && x.Status == MapStatus.Available);
if (map != null)
{
if (mapAction.Value == MapAction.Delete)
Expand Down
29 changes: 23 additions & 6 deletions OpenRA.Game/Map/MapPreview.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,19 @@ public InnerData Clone()
readonly ModData modData;

public readonly string Uid;
public IReadOnlyPackage Package { get; private set; }
public string PackageName { get; private set; }
IReadOnlyPackage package;
public IReadOnlyPackage Package
{
get
{
package ??= parentPackage.OpenPackage(PackageName, modData.ModFiles);
return package;
}

private set => package = value;
}
PunkPun marked this conversation as resolved.
Show resolved Hide resolved

IReadOnlyPackage parentPackage;

volatile InnerData innerData;
Expand Down Expand Up @@ -286,7 +298,7 @@ public MapPreview(Map map, ModData modData)
cache = modData.MapCache;

Uid = map.Uid;
Package = map.Package;
PackageName = map.Package.Name;

var mapPlayers = new MapPlayers(map.PlayerDefinitions);
var spawns = new List<CPos>();
Expand Down Expand Up @@ -338,7 +350,7 @@ public void UpdateFromMap(IReadOnlyPackage p, IReadOnlyPackage parent, MapClassi
yaml = new MiniYaml(null, MiniYaml.FromStream(yamlStream, $"{p.Name}:map.yaml", stringPool: cache.StringPool)).ToDictionary();
}

Package = p;
PackageName = p.Name;
parentPackage = parent;

var newData = innerData.Clone();
Expand Down Expand Up @@ -587,10 +599,15 @@ public void Invalidate()

public void Dispose()
{
if (Package != null)
DisposePackage();
}

public void DisposePackage()
{
if (package != null)
{
Package.Dispose();
Package = null;
package.Dispose();
package = null;
}
}

Expand Down
2 changes: 1 addition & 1 deletion OpenRA.Mods.Common/Widgets/Logic/Editor/SaveMapLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ public static void SaveMap(ModData modData, World world, Map map, string combine
if (map.Package?.Name != combinedPath)
{
// When creating a new map or when file paths don't match
if (modData.MapCache.Any(m => m.Status == MapStatus.Available && m.Package?.Name == combinedPath))
if (modData.MapCache.Any(m => m.Status == MapStatus.Available && m.PackageName == combinedPath))
{
ConfirmationDialogs.ButtonPrompt(modData,
title: OverwriteMapFailedTitle,
Expand Down
2 changes: 1 addition & 1 deletion OpenRA.Mods.Common/Widgets/Logic/MissionBrowserLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public MissionBrowserLogic(Widget widget, ModData modData, World world, Action o
.Select(p => new
{
Preview = p,
Index = missionMapPaths.IndexOf(Path.GetFileName(p.Package.Name))
Index = missionMapPaths.IndexOf(Path.GetFileName(p.PackageName))
})
.Where(x => x.Index != -1)
.OrderBy(x => x.Index)
Expand Down