Add plumbing for trait-defined lobby options.
This commit is contained in:
@@ -173,6 +173,17 @@ namespace OpenRA.Network
|
||||
}
|
||||
}
|
||||
|
||||
public class LobbyOptionState
|
||||
{
|
||||
public bool Locked;
|
||||
public string Value;
|
||||
public string PreferredValue;
|
||||
|
||||
public LobbyOptionState() { }
|
||||
|
||||
public bool Enabled { get { return Value == "True"; } }
|
||||
}
|
||||
|
||||
public class Global
|
||||
{
|
||||
public string ServerName;
|
||||
@@ -198,14 +209,45 @@ namespace OpenRA.Network
|
||||
public string GameUid;
|
||||
public bool DisableSingleplayer;
|
||||
|
||||
[FieldLoader.Ignore]
|
||||
public Dictionary<string, LobbyOptionState> LobbyOptions = new Dictionary<string, LobbyOptionState>();
|
||||
|
||||
public static Global Deserialize(MiniYaml data)
|
||||
{
|
||||
return FieldLoader.Load<Global>(data);
|
||||
var gs = FieldLoader.Load<Global>(data);
|
||||
|
||||
var optionsNode = data.Nodes.FirstOrDefault(n => n.Key == "Options");
|
||||
if (optionsNode != null)
|
||||
foreach (var n in optionsNode.Value.Nodes)
|
||||
gs.LobbyOptions[n.Key] = FieldLoader.Load<LobbyOptionState>(n.Value);
|
||||
|
||||
return gs;
|
||||
}
|
||||
|
||||
public MiniYamlNode Serialize()
|
||||
{
|
||||
return new MiniYamlNode("GlobalSettings", FieldSaver.Save(this));
|
||||
var data = new MiniYamlNode("GlobalSettings", FieldSaver.Save(this));
|
||||
var options = LobbyOptions.Select(kv => new MiniYamlNode(kv.Key, FieldSaver.Save(kv.Value))).ToList();
|
||||
data.Value.Nodes.Add(new MiniYamlNode("Options", new MiniYaml(null, options)));
|
||||
return data;
|
||||
}
|
||||
|
||||
public bool OptionOrDefault(string id, bool def)
|
||||
{
|
||||
LobbyOptionState option;
|
||||
if (LobbyOptions.TryGetValue(id, out option))
|
||||
return option.Enabled;
|
||||
|
||||
return def;
|
||||
}
|
||||
|
||||
public string OptionOrDefault(string id, string def)
|
||||
{
|
||||
LobbyOptionState option;
|
||||
if (LobbyOptions.TryGetValue(id, out option))
|
||||
return option.Value;
|
||||
|
||||
return def;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -412,4 +412,50 @@ namespace OpenRA.Traits
|
||||
|
||||
public interface IRulesetLoaded<TInfo> { void RulesetLoaded(Ruleset rules, TInfo info); }
|
||||
public interface IRulesetLoaded : IRulesetLoaded<ActorInfo>, ITraitInfoInterface { }
|
||||
|
||||
[RequireExplicitImplementation]
|
||||
public interface ILobbyOptions : ITraitInfoInterface
|
||||
{
|
||||
IEnumerable<LobbyOption> LobbyOptions(Ruleset rules);
|
||||
}
|
||||
|
||||
public class LobbyOption
|
||||
{
|
||||
public readonly string Id;
|
||||
public readonly string Name;
|
||||
public readonly IReadOnlyDictionary<string, string> Values;
|
||||
public readonly string DefaultValue;
|
||||
public readonly bool Locked;
|
||||
|
||||
public LobbyOption(string id, string name, IReadOnlyDictionary<string, string> values, string defaultValue, bool locked)
|
||||
{
|
||||
Id = id;
|
||||
Name = name;
|
||||
Values = values;
|
||||
DefaultValue = defaultValue;
|
||||
Locked = locked;
|
||||
}
|
||||
|
||||
public virtual string ValueChangedMessage(string playerName, string newValue)
|
||||
{
|
||||
return playerName + " changed " + Name + " to " + Values[newValue] + ".";
|
||||
}
|
||||
}
|
||||
|
||||
public class LobbyBooleanOption : LobbyOption
|
||||
{
|
||||
static readonly Dictionary<string, string> BoolValues = new Dictionary<string, string>()
|
||||
{
|
||||
{ true.ToString(), "enabled" },
|
||||
{ false.ToString(), "disabled" }
|
||||
};
|
||||
|
||||
public LobbyBooleanOption(string id, string name, bool defaultValue, bool locked)
|
||||
: base(id, name, new ReadOnlyDictionary<string, string>(BoolValues), defaultValue.ToString(), locked) { }
|
||||
|
||||
public override string ValueChangedMessage(string playerName, string newValue)
|
||||
{
|
||||
return playerName + " " + BoolValues[newValue] + " " + Name + ".";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user