Rework MapPreview custom rule handling.
The previous asynchronous approach did not work particularly well, leading to large janks when switching to custom maps or opening the mission browser. This commit introduces two key changes: * Rule loading for WorldActorInfo and PlayerActorInfo is made synchronous, in preparation for the next commit which will significantly optimize this path. * The full ruleset loading, which is required for map validation, is moved to the server-side and managed by a new ServerMapStatusCache. The previous syntax check is expanded to include the ability to run lint tests.
This commit is contained in:
@@ -38,9 +38,6 @@ namespace OpenRA
|
||||
Remote = 4
|
||||
}
|
||||
|
||||
// Used for verifying map availability in the lobby
|
||||
public enum MapRuleStatus { Unknown, Cached, Invalid }
|
||||
|
||||
[SuppressMessage("StyleCop.CSharp.NamingRules",
|
||||
"SA1307:AccessibleFieldsMustBeginWithUpperCaseLetter",
|
||||
Justification = "Fields names must match the with the remote API.")]
|
||||
@@ -85,45 +82,50 @@ namespace OpenRA
|
||||
public MapClassification Class;
|
||||
public MapVisibility Visibility;
|
||||
|
||||
Lazy<Ruleset> rules;
|
||||
public bool InvalidCustomRules { get; private set; }
|
||||
public bool DefinesUnsafeCustomRules { get; private set; }
|
||||
public bool RulesLoaded { get; private set; }
|
||||
public MiniYaml RuleDefinitions;
|
||||
public MiniYaml WeaponDefinitions;
|
||||
public MiniYaml VoiceDefinitions;
|
||||
public MiniYaml MusicDefinitions;
|
||||
public MiniYaml NotificationDefinitions;
|
||||
public MiniYaml SequenceDefinitions;
|
||||
public MiniYaml ModelSequenceDefinitions;
|
||||
|
||||
public ActorInfo WorldActorInfo => rules?.Value.Actors[SystemActors.World];
|
||||
public ActorInfo PlayerActorInfo => rules?.Value.Actors[SystemActors.Player];
|
||||
public ActorInfo WorldActorInfo { get; private set; }
|
||||
public ActorInfo PlayerActorInfo { get; private set; }
|
||||
|
||||
public void SetRulesetGenerator(ModData modData, Func<(Ruleset Ruleset, bool DefinesUnsafeCustomRules)> generator)
|
||||
static MiniYaml LoadRuleSection(Dictionary<string, MiniYaml> yaml, string section)
|
||||
{
|
||||
InvalidCustomRules = false;
|
||||
RulesLoaded = false;
|
||||
DefinesUnsafeCustomRules = false;
|
||||
if (!yaml.TryGetValue(section, out var node))
|
||||
return null;
|
||||
|
||||
// Note: multiple threads may try to access the value at the same time
|
||||
// We rely on the thread-safety guarantees given by Lazy<T> to prevent race conitions.
|
||||
// If you're thinking about replacing this, then you must be careful to keep this safe.
|
||||
rules = Exts.Lazy(() =>
|
||||
return node;
|
||||
}
|
||||
|
||||
public void SetCustomRules(ModData modData, IReadOnlyFileSystem fileSystem, Dictionary<string, MiniYaml> yaml)
|
||||
{
|
||||
RuleDefinitions = LoadRuleSection(yaml, "Rules");
|
||||
WeaponDefinitions = LoadRuleSection(yaml, "Weapons");
|
||||
VoiceDefinitions = LoadRuleSection(yaml, "Voices");
|
||||
MusicDefinitions = LoadRuleSection(yaml, "Music");
|
||||
NotificationDefinitions = LoadRuleSection(yaml, "Notifications");
|
||||
SequenceDefinitions = LoadRuleSection(yaml, "Sequences");
|
||||
ModelSequenceDefinitions = LoadRuleSection(yaml, "ModelSequences");
|
||||
|
||||
try
|
||||
{
|
||||
if (generator == null)
|
||||
return Ruleset.LoadDefaultsForTileSet(modData, TileSet);
|
||||
var rules = Ruleset.Load(modData, fileSystem, TileSet, RuleDefinitions,
|
||||
WeaponDefinitions, VoiceDefinitions, NotificationDefinitions,
|
||||
MusicDefinitions, SequenceDefinitions, ModelSequenceDefinitions);
|
||||
|
||||
try
|
||||
{
|
||||
var ret = generator();
|
||||
DefinesUnsafeCustomRules = ret.DefinesUnsafeCustomRules;
|
||||
return ret.Ruleset;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Write("debug", "Failed to load rules for `{0}` with error :{1}", Title, e.Message);
|
||||
InvalidCustomRules = true;
|
||||
return Ruleset.LoadDefaultsForTileSet(modData, TileSet);
|
||||
}
|
||||
finally
|
||||
{
|
||||
RulesLoaded = true;
|
||||
}
|
||||
});
|
||||
WorldActorInfo = rules.Actors[SystemActors.World];
|
||||
PlayerActorInfo = rules.Actors[SystemActors.Player];
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Write("debug", "Failed to load rules for `{0}` with error :{1}", Title, e.Message);
|
||||
WorldActorInfo = modData.DefaultRules.Actors[SystemActors.World];
|
||||
PlayerActorInfo = modData.DefaultRules.Actors[SystemActors.Player];
|
||||
}
|
||||
}
|
||||
|
||||
public InnerData Clone()
|
||||
@@ -132,7 +134,7 @@ namespace OpenRA
|
||||
}
|
||||
}
|
||||
|
||||
static readonly CPos[] NoSpawns = new CPos[] { };
|
||||
static readonly CPos[] NoSpawns = { };
|
||||
readonly MapCache cache;
|
||||
readonly ModData modData;
|
||||
|
||||
@@ -156,22 +158,9 @@ namespace OpenRA
|
||||
public MapClassification Class => innerData.Class;
|
||||
public MapVisibility Visibility => innerData.Visibility;
|
||||
|
||||
public bool InvalidCustomRules => innerData.InvalidCustomRules;
|
||||
public bool RulesLoaded => innerData.RulesLoaded;
|
||||
|
||||
public ActorInfo WorldActorInfo => innerData.WorldActorInfo;
|
||||
public ActorInfo PlayerActorInfo => innerData.PlayerActorInfo;
|
||||
|
||||
public bool DefinesUnsafeCustomRules
|
||||
{
|
||||
get
|
||||
{
|
||||
// Force lazy rules to be evaluated
|
||||
var force = innerData.WorldActorInfo;
|
||||
return innerData.DefinesUnsafeCustomRules;
|
||||
}
|
||||
}
|
||||
|
||||
public long DownloadBytes { get; private set; }
|
||||
public int DownloadPercentage { get; private set; }
|
||||
|
||||
@@ -197,6 +186,20 @@ namespace OpenRA
|
||||
generatingMinimap = false;
|
||||
}
|
||||
|
||||
public bool DefinesUnsafeCustomRules()
|
||||
{
|
||||
return Ruleset.DefinesUnsafeCustomRules(modData, this, innerData.RuleDefinitions,
|
||||
innerData.WeaponDefinitions, innerData.VoiceDefinitions,
|
||||
innerData.NotificationDefinitions, innerData.SequenceDefinitions);
|
||||
}
|
||||
|
||||
public Ruleset LoadRuleset()
|
||||
{
|
||||
return Ruleset.Load(modData, this, TileSet, innerData.RuleDefinitions,
|
||||
innerData.WeaponDefinitions, innerData.VoiceDefinitions, innerData.NotificationDefinitions,
|
||||
innerData.MusicDefinitions, innerData.SequenceDefinitions, innerData.ModelSequenceDefinitions);
|
||||
}
|
||||
|
||||
public MapPreview(ModData modData, string uid, MapGridType gridType, MapCache cache)
|
||||
{
|
||||
this.cache = cache;
|
||||
@@ -308,21 +311,7 @@ namespace OpenRA
|
||||
newData.Status = MapStatus.Unavailable;
|
||||
}
|
||||
|
||||
newData.SetRulesetGenerator(modData, () =>
|
||||
{
|
||||
var ruleDefinitions = LoadRuleSection(yaml, "Rules");
|
||||
var weaponDefinitions = LoadRuleSection(yaml, "Weapons");
|
||||
var voiceDefinitions = LoadRuleSection(yaml, "Voices");
|
||||
var musicDefinitions = LoadRuleSection(yaml, "Music");
|
||||
var notificationDefinitions = LoadRuleSection(yaml, "Notifications");
|
||||
var sequenceDefinitions = LoadRuleSection(yaml, "Sequences");
|
||||
var modelSequenceDefinitions = LoadRuleSection(yaml, "ModelSequences");
|
||||
var rules = Ruleset.Load(modData, this, TileSet, ruleDefinitions, weaponDefinitions,
|
||||
voiceDefinitions, notificationDefinitions, musicDefinitions, sequenceDefinitions, modelSequenceDefinitions);
|
||||
var flagged = Ruleset.DefinesUnsafeCustomRules(modData, this, ruleDefinitions,
|
||||
weaponDefinitions, voiceDefinitions, notificationDefinitions, sequenceDefinitions);
|
||||
return (rules, flagged);
|
||||
});
|
||||
newData.SetCustomRules(modData, this, yaml);
|
||||
|
||||
if (p.Contains("map.png"))
|
||||
using (var dataStream = p.GetStream("map.png"))
|
||||
@@ -332,19 +321,6 @@ namespace OpenRA
|
||||
innerData = newData;
|
||||
}
|
||||
|
||||
MiniYaml LoadRuleSection(Dictionary<string, MiniYaml> yaml, string section)
|
||||
{
|
||||
if (!yaml.TryGetValue(section, out var node))
|
||||
return null;
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
public void PreloadRules()
|
||||
{
|
||||
var unused = WorldActorInfo;
|
||||
}
|
||||
|
||||
public void UpdateRemoteSearch(MapStatus status, MiniYaml yaml, Action<MapPreview> parseMetadata = null)
|
||||
{
|
||||
var newData = innerData.Clone();
|
||||
@@ -389,23 +365,9 @@ namespace OpenRA
|
||||
var playersString = Encoding.UTF8.GetString(Convert.FromBase64String(r.players_block));
|
||||
newData.Players = new MapPlayers(MiniYaml.FromString(playersString));
|
||||
|
||||
newData.SetRulesetGenerator(modData, () =>
|
||||
{
|
||||
var rulesString = Encoding.UTF8.GetString(Convert.FromBase64String(r.rules));
|
||||
var rulesYaml = new MiniYaml("", MiniYaml.FromString(rulesString)).ToDictionary();
|
||||
var ruleDefinitions = LoadRuleSection(rulesYaml, "Rules");
|
||||
var weaponDefinitions = LoadRuleSection(rulesYaml, "Weapons");
|
||||
var voiceDefinitions = LoadRuleSection(rulesYaml, "Voices");
|
||||
var musicDefinitions = LoadRuleSection(rulesYaml, "Music");
|
||||
var notificationDefinitions = LoadRuleSection(rulesYaml, "Notifications");
|
||||
var sequenceDefinitions = LoadRuleSection(rulesYaml, "Sequences");
|
||||
var modelSequenceDefinitions = LoadRuleSection(rulesYaml, "ModelSequences");
|
||||
var rules = Ruleset.Load(modData, this, TileSet, ruleDefinitions, weaponDefinitions,
|
||||
voiceDefinitions, notificationDefinitions, musicDefinitions, sequenceDefinitions, modelSequenceDefinitions);
|
||||
var flagged = Ruleset.DefinesUnsafeCustomRules(modData, this, ruleDefinitions,
|
||||
weaponDefinitions, voiceDefinitions, notificationDefinitions, sequenceDefinitions);
|
||||
return (rules, flagged);
|
||||
});
|
||||
var rulesString = Encoding.UTF8.GetString(Convert.FromBase64String(r.rules));
|
||||
var rulesYaml = new MiniYaml("", MiniYaml.FromString(rulesString)).ToDictionary();
|
||||
newData.SetCustomRules(modData, this, rulesYaml);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
@@ -473,21 +435,19 @@ namespace OpenRA
|
||||
|
||||
mapInstallPackage.Update(mapFilename, fileStream.ToArray());
|
||||
Log.Write("debug", "Downloaded map to '{0}'", mapFilename);
|
||||
Game.RunAfterTick(() =>
|
||||
|
||||
var package = mapInstallPackage.OpenPackage(mapFilename, modData.ModFiles);
|
||||
if (package == null)
|
||||
innerData.Status = MapStatus.DownloadError;
|
||||
else
|
||||
{
|
||||
var package = mapInstallPackage.OpenPackage(mapFilename, modData.ModFiles);
|
||||
if (package == null)
|
||||
innerData.Status = MapStatus.DownloadError;
|
||||
else
|
||||
{
|
||||
UpdateFromMap(package, mapInstallPackage, MapClassification.User, null, GridType);
|
||||
onSuccess();
|
||||
}
|
||||
});
|
||||
UpdateFromMap(package, mapInstallPackage, MapClassification.User, null, GridType);
|
||||
Game.RunAfterTick(onSuccess);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e.Message);
|
||||
Log.Write("debug", "Map installation failed with error: {0}", e);
|
||||
innerData.Status = MapStatus.DownloadError;
|
||||
}
|
||||
});
|
||||
|
||||
@@ -15,6 +15,7 @@ using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using OpenRA.Primitives;
|
||||
using OpenRA.Server;
|
||||
|
||||
namespace OpenRA.Network
|
||||
{
|
||||
@@ -218,10 +219,21 @@ namespace OpenRA.Network
|
||||
public bool IsEnabled => Value == "True";
|
||||
}
|
||||
|
||||
[Flags]
|
||||
public enum MapStatus
|
||||
{
|
||||
Unknown = 0,
|
||||
Validating = 1,
|
||||
Playable = 2,
|
||||
Incompatible = 4,
|
||||
UnsafeCustomRules = 8,
|
||||
}
|
||||
|
||||
public class Global
|
||||
{
|
||||
public string ServerName;
|
||||
public string Map;
|
||||
public MapStatus MapStatus;
|
||||
public int RandomSeed = 0;
|
||||
public bool AllowSpectators = true;
|
||||
public string GameUid;
|
||||
|
||||
100
OpenRA.Game/Server/MapStatusCache.cs
Normal file
100
OpenRA.Game/Server/MapStatusCache.cs
Normal file
@@ -0,0 +1,100 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2021 The OpenRA Developers (see AUTHORS)
|
||||
* This file is part of OpenRA, which is free software. It is made
|
||||
* available to you under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation, either version 3 of
|
||||
* the License, or (at your option) any later version. For more
|
||||
* information, see COPYING.
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using OpenRA.Network;
|
||||
|
||||
namespace OpenRA.Server
|
||||
{
|
||||
public interface ILintServerMapPass { void Run(Action<string> emitError, Action<string> emitWarning, ModData modData, MapPreview map, Ruleset mapRules); }
|
||||
|
||||
public class MapStatusCache
|
||||
{
|
||||
readonly Dictionary<MapPreview, Session.MapStatus> cache = new Dictionary<MapPreview, Session.MapStatus>();
|
||||
readonly Action<string, Session.MapStatus> onStatusChanged;
|
||||
readonly bool enableRemoteLinting;
|
||||
readonly ModData modData;
|
||||
|
||||
public MapStatusCache(ModData modData, Action<string, Session.MapStatus> onStatusChanged, bool enableRemoteLinting)
|
||||
{
|
||||
this.modData = modData;
|
||||
this.enableRemoteLinting = enableRemoteLinting;
|
||||
this.onStatusChanged = onStatusChanged;
|
||||
}
|
||||
|
||||
void RunLintTests(MapPreview map, Ruleset rules)
|
||||
{
|
||||
var status = cache[map];
|
||||
var failed = false;
|
||||
|
||||
Action<string> onLintFailure = message =>
|
||||
{
|
||||
Log.Write("server", "Map {0} failed lint with error: {1}", map.Title, message);
|
||||
failed = true;
|
||||
};
|
||||
|
||||
Action<string> onLintWarning = _ => { };
|
||||
|
||||
foreach (var customMapPassType in modData.ObjectCreator.GetTypesImplementing<ILintServerMapPass>())
|
||||
{
|
||||
try
|
||||
{
|
||||
var customMapPass = (ILintServerMapPass)modData.ObjectCreator.CreateBasic(customMapPassType);
|
||||
customMapPass.Run(onLintFailure, onLintWarning, modData, map, rules);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
onLintFailure(e.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
status &= ~Session.MapStatus.Validating;
|
||||
status |= failed ? Session.MapStatus.Incompatible : Session.MapStatus.Playable;
|
||||
|
||||
cache[map] = status;
|
||||
onStatusChanged?.Invoke(map.Uid, status);
|
||||
}
|
||||
|
||||
public Session.MapStatus this[MapPreview map]
|
||||
{
|
||||
get
|
||||
{
|
||||
if (cache.TryGetValue(map, out var status))
|
||||
return status;
|
||||
|
||||
Ruleset rules = null;
|
||||
try
|
||||
{
|
||||
rules = map.LoadRuleset();
|
||||
|
||||
// Locally installed maps are assumed to not require linting
|
||||
status = enableRemoteLinting && map.Status != MapStatus.Available ? Session.MapStatus.Validating : Session.MapStatus.Playable;
|
||||
if (map.DefinesUnsafeCustomRules())
|
||||
status |= Session.MapStatus.UnsafeCustomRules;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Write("server", "Failed to load rules for `{0}` with error :{1}", map.Title, e.Message);
|
||||
status = Session.MapStatus.Incompatible;
|
||||
}
|
||||
|
||||
cache[map] = status;
|
||||
|
||||
if ((status & Session.MapStatus.Validating) != 0)
|
||||
ThreadPool.QueueUserWorkItem(_ => RunLintTests(map, rules));
|
||||
|
||||
return status;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -68,6 +68,6 @@ namespace OpenRA.Server
|
||||
// The protocol for server and world orders
|
||||
// This applies after the handshake has completed, and is provided to support
|
||||
// alternative server implementations that wish to support multiple versions in parallel
|
||||
public const int Orders = 11;
|
||||
public const int Orders = 12;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -61,6 +61,7 @@ namespace OpenRA.Server
|
||||
|
||||
// Managed by LobbyCommands
|
||||
public MapPreview Map;
|
||||
public readonly MapStatusCache MapStatusCache;
|
||||
public GameSave GameSave = null;
|
||||
|
||||
readonly int randomSeed;
|
||||
@@ -170,6 +171,17 @@ namespace OpenRA.Server
|
||||
}.Serialize());
|
||||
}
|
||||
|
||||
void MapStatusChanged(string uid, Session.MapStatus status)
|
||||
{
|
||||
lock (LobbyInfo)
|
||||
{
|
||||
if (LobbyInfo.GlobalSettings.Map == uid)
|
||||
LobbyInfo.GlobalSettings.MapStatus = status;
|
||||
|
||||
SyncLobbyInfo();
|
||||
}
|
||||
}
|
||||
|
||||
public Server(List<IPEndPoint> endpoints, ServerSettings settings, ModData modData, ServerType type)
|
||||
{
|
||||
Log.AddChannel("server", "server.log", true);
|
||||
@@ -229,12 +241,16 @@ namespace OpenRA.Server
|
||||
|
||||
serverTraits.TrimExcess();
|
||||
|
||||
Map = ModData.MapCache[settings.Map];
|
||||
MapStatusCache = new MapStatusCache(modData, MapStatusChanged, type == ServerType.Dedicated && settings.EnableLintChecks);
|
||||
|
||||
LobbyInfo = new Session
|
||||
{
|
||||
GlobalSettings =
|
||||
{
|
||||
RandomSeed = randomSeed,
|
||||
Map = settings.Map,
|
||||
Map = Map.Uid,
|
||||
MapStatus = Session.MapStatus.Unknown,
|
||||
ServerName = settings.Name,
|
||||
EnableSingleplayer = settings.EnableSingleplayer || Type != ServerType.Dedicated,
|
||||
EnableSyncReports = settings.EnableSyncReports,
|
||||
@@ -254,6 +270,8 @@ namespace OpenRA.Server
|
||||
|
||||
new Thread(_ =>
|
||||
{
|
||||
// Initial status is set off the main thread to avoid triggering a load screen when joining a skirmish game
|
||||
LobbyInfo.GlobalSettings.MapStatus = MapStatusCache[Map];
|
||||
foreach (var t in serverTraits.WithInterface<INotifyServerStart>())
|
||||
t.ServerStarted(this);
|
||||
|
||||
@@ -541,7 +559,7 @@ namespace OpenRA.Server
|
||||
SendOrderTo(newConn, "Message", motd);
|
||||
}
|
||||
|
||||
if (Map.DefinesUnsafeCustomRules)
|
||||
if ((LobbyInfo.GlobalSettings.MapStatus & Session.MapStatus.UnsafeCustomRules) != 0)
|
||||
SendOrderTo(newConn, "Message", "This map contains custom rules. Game experience may change.");
|
||||
|
||||
if (!LobbyInfo.GlobalSettings.EnableSingleplayer)
|
||||
|
||||
@@ -91,6 +91,9 @@ namespace OpenRA
|
||||
[Desc("For dedicated servers only, save replays for all games played.")]
|
||||
public bool RecordReplays = false;
|
||||
|
||||
[Desc("For dedicated servers only, treat maps that fail the lint checks as invalid.")]
|
||||
public bool EnableLintChecks = true;
|
||||
|
||||
public ServerSettings Clone()
|
||||
{
|
||||
return (ServerSettings)MemberwiseClone();
|
||||
|
||||
Reference in New Issue
Block a user