Remove FluentBundle.Arguments helper method.
This commit is contained in:
@@ -112,15 +112,15 @@ namespace OpenRA
|
||||
}
|
||||
}
|
||||
|
||||
public string GetString(string key, IDictionary<string, object> arguments = null)
|
||||
public string GetString(string key, object[] args = null)
|
||||
{
|
||||
if (!TryGetString(key, out var message, arguments))
|
||||
if (!TryGetString(key, out var message, args))
|
||||
message = key;
|
||||
|
||||
return message;
|
||||
}
|
||||
|
||||
public bool TryGetString(string key, out string value, IDictionary<string, object> arguments = null)
|
||||
public bool TryGetString(string key, out string value, object[] args = null)
|
||||
{
|
||||
if (key == null)
|
||||
throw new ArgumentNullException(nameof(key));
|
||||
@@ -133,12 +133,29 @@ namespace OpenRA
|
||||
return false;
|
||||
}
|
||||
|
||||
var fluentArguments = new Dictionary<string, IFluentType>();
|
||||
if (arguments != null)
|
||||
foreach (var (k, v) in arguments)
|
||||
fluentArguments.Add(k, v.ToFluentType());
|
||||
Dictionary<string, IFluentType> fluentArgs = null;
|
||||
if (args != null)
|
||||
{
|
||||
if (args.Length % 2 != 0)
|
||||
throw new ArgumentException("Expected a comma separated list of name, value arguments " +
|
||||
"but the number of arguments is not a multiple of two", nameof(args));
|
||||
|
||||
var result = bundle.TryGetAttrMessage(key, fluentArguments, out var errors, out value);
|
||||
fluentArgs = new Dictionary<string, IFluentType>();
|
||||
for (var i = 0; i < args.Length; i += 2)
|
||||
{
|
||||
var argKey = args[i] as string;
|
||||
if (string.IsNullOrEmpty(argKey))
|
||||
throw new ArgumentException($"Expected the argument at index {i} to be a non-empty string", nameof(args));
|
||||
|
||||
var argValue = args[i + 1];
|
||||
if (argValue == null)
|
||||
throw new ArgumentNullException(nameof(args), $"Expected the argument at index {i + 1} to be a non-null value");
|
||||
|
||||
fluentArgs.Add(argKey, argValue.ToFluentType());
|
||||
}
|
||||
}
|
||||
|
||||
var result = bundle.TryGetAttrMessage(key, fluentArgs, out var errors, out value);
|
||||
foreach (var error in errors)
|
||||
Log.Write("debug", $"FluentBundle of {key}: {error}");
|
||||
|
||||
@@ -157,31 +174,5 @@ namespace OpenRA
|
||||
{
|
||||
return bundle.HasAttrMessage(key);
|
||||
}
|
||||
|
||||
// Adapted from Fluent.Net.SimpleExample.TranslationService by Mark Weaver
|
||||
public static Dictionary<string, object> Arguments(string name, object value, params object[] args)
|
||||
{
|
||||
if (args.Length % 2 != 0)
|
||||
throw new ArgumentException("Expected a comma separated list of name, value arguments"
|
||||
+ " but the number of arguments is not a multiple of two", nameof(args));
|
||||
|
||||
var argumentDictionary = new Dictionary<string, object> { { name, value } };
|
||||
|
||||
for (var i = 0; i < args.Length; i += 2)
|
||||
{
|
||||
name = args[i] as string;
|
||||
if (string.IsNullOrEmpty(name))
|
||||
throw new ArgumentException($"Expected the argument at index {i} to be a non-empty string",
|
||||
nameof(args));
|
||||
|
||||
value = args[i + 1];
|
||||
if (value == null)
|
||||
throw new ArgumentNullException(nameof(args), $"Expected the argument at index {i + 1} to be a non-null value");
|
||||
|
||||
argumentDictionary.Add(name, value);
|
||||
}
|
||||
|
||||
return argumentDictionary;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,6 @@
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System.Collections.Generic;
|
||||
using OpenRA.FileSystem;
|
||||
|
||||
namespace OpenRA
|
||||
@@ -32,7 +31,7 @@ namespace OpenRA
|
||||
}
|
||||
}
|
||||
|
||||
public static string GetString(string key, IDictionary<string, object> args = null)
|
||||
public static string GetString(string key, params object[] args)
|
||||
{
|
||||
lock (SyncObject)
|
||||
{
|
||||
@@ -48,7 +47,7 @@ namespace OpenRA
|
||||
}
|
||||
}
|
||||
|
||||
public static bool TryGetString(string key, out string message, IDictionary<string, object> args = null)
|
||||
public static bool TryGetString(string key, out string message, params object[] args)
|
||||
{
|
||||
lock (SyncObject)
|
||||
{
|
||||
@@ -65,7 +64,7 @@ namespace OpenRA
|
||||
}
|
||||
|
||||
/// <summary>Should only be used by <see cref="MapPreview"/>.</summary>
|
||||
internal static bool TryGetModString(string key, out string message, IDictionary<string, object> args = null)
|
||||
internal static bool TryGetModString(string key, out string message, params object[] args)
|
||||
{
|
||||
lock (SyncObject)
|
||||
{
|
||||
|
||||
@@ -595,7 +595,7 @@ namespace OpenRA
|
||||
Log.Write("debug", "Taking screenshot " + path);
|
||||
|
||||
Renderer.SaveScreenshot(path);
|
||||
TextNotificationsManager.Debug(FluentProvider.GetString(SavedScreenshot, FluentBundle.Arguments("filename", filename)));
|
||||
TextNotificationsManager.Debug(FluentProvider.GetString(SavedScreenshot, "filename", filename));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -153,9 +153,8 @@ namespace OpenRA
|
||||
{
|
||||
var number = Players.Where(p => p.BotType == player.BotType).ToList().IndexOf(player) + 1;
|
||||
return FluentProvider.GetString(EnumeratedBotName,
|
||||
FluentBundle.Arguments(
|
||||
"name", FluentProvider.GetString(player.Name),
|
||||
"number", number));
|
||||
"number", number);
|
||||
}
|
||||
|
||||
return player.Name;
|
||||
|
||||
@@ -227,7 +227,7 @@ namespace OpenRA
|
||||
/// Functionality mirrors <see cref="FluentProvider.GetString"/>, except instead of using
|
||||
/// loaded <see cref="Map"/>'s fluent bundle as backup, we use this <see cref="MapPreview"/>'s.
|
||||
/// </summary>
|
||||
public string GetLocalisedString(string key, IDictionary<string, object> args = null)
|
||||
public string GetLocalisedString(string key, object[] args = null)
|
||||
{
|
||||
// PERF: instead of loading mod level strings per each MapPreview, reuse the already loaded one in FluentProvider.
|
||||
if (FluentProvider.TryGetModString(key, out var message, args))
|
||||
|
||||
@@ -9,8 +9,8 @@
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Linguini.Shared.Types.Bundle;
|
||||
|
||||
namespace OpenRA.Network
|
||||
@@ -55,30 +55,31 @@ namespace OpenRA.Network
|
||||
public readonly string Key = string.Empty;
|
||||
|
||||
[FieldLoader.LoadUsing(nameof(LoadArguments))]
|
||||
public readonly Dictionary<string, object> Arguments;
|
||||
public readonly object[] Arguments;
|
||||
|
||||
static object LoadArguments(MiniYaml yaml)
|
||||
{
|
||||
var arguments = new Dictionary<string, object>();
|
||||
var arguments = new List<object>();
|
||||
var argumentsNode = yaml.NodeWithKeyOrDefault("Arguments");
|
||||
if (argumentsNode != null)
|
||||
{
|
||||
foreach (var argumentNode in argumentsNode.Value.Nodes)
|
||||
{
|
||||
var argument = FieldLoader.Load<FluentArgument>(argumentNode.Value);
|
||||
arguments.Add(argument.Key);
|
||||
if (argument.Type == FluentArgument.FluentArgumentType.Number)
|
||||
{
|
||||
if (!double.TryParse(argument.Value, out var number))
|
||||
Log.Write("debug", $"Failed to parse {argument.Value}");
|
||||
|
||||
arguments.Add(argument.Key, number);
|
||||
arguments.Add(number);
|
||||
}
|
||||
else
|
||||
arguments.Add(argument.Key, argument.Value);
|
||||
arguments.Add(argument.Value);
|
||||
}
|
||||
}
|
||||
|
||||
return arguments;
|
||||
return arguments.ToArray();
|
||||
}
|
||||
|
||||
public FluentMessage(MiniYaml yaml)
|
||||
@@ -87,21 +88,31 @@ namespace OpenRA.Network
|
||||
FieldLoader.Load(this, yaml);
|
||||
}
|
||||
|
||||
public static string Serialize(string key, Dictionary<string, object> arguments = null)
|
||||
public static string Serialize(string key, object[] args)
|
||||
{
|
||||
var root = new List<MiniYamlNode>
|
||||
{
|
||||
new("Protocol", ProtocolVersion.ToStringInvariant()),
|
||||
new("Key", key)
|
||||
new("Key", key),
|
||||
};
|
||||
|
||||
if (arguments != null)
|
||||
if (args != null)
|
||||
{
|
||||
var argumentsNode = new MiniYaml("", arguments
|
||||
.Select(a => new FluentArgument(a.Key, a.Value))
|
||||
.Select((argument, i) => new MiniYamlNode("Argument@" + i, FieldSaver.Save(argument))));
|
||||
var nodes = new List<MiniYamlNode>();
|
||||
for (var i = 0; i < args.Length; i += 2)
|
||||
{
|
||||
var argKey = args[i] as string;
|
||||
if (string.IsNullOrEmpty(argKey))
|
||||
throw new ArgumentException($"Expected the argument at index {i} to be a non-empty string", nameof(args));
|
||||
|
||||
root.Add(new MiniYamlNode("Arguments", argumentsNode));
|
||||
var argValue = args[i + 1];
|
||||
if (argValue == null)
|
||||
throw new ArgumentNullException(nameof(args), $"Expected the argument at index {i + 1} to be a non-null value");
|
||||
|
||||
nodes.Add(new MiniYamlNode($"Argument@{i / 2}", FieldSaver.Save(new FluentArgument(argKey, argValue))));
|
||||
}
|
||||
|
||||
root.Add(new MiniYamlNode("Arguments", new MiniYaml("", nodes)));
|
||||
}
|
||||
|
||||
return new MiniYaml("", root)
|
||||
|
||||
@@ -91,7 +91,7 @@ namespace OpenRA.Network
|
||||
World.OutOfSync();
|
||||
IsOutOfSync = true;
|
||||
|
||||
TextNotificationsManager.AddSystemLine(DesyncCompareLogs, FluentBundle.Arguments("frame", frame));
|
||||
TextNotificationsManager.AddSystemLine(DesyncCompareLogs, "frame", frame);
|
||||
}
|
||||
|
||||
public void StartGame()
|
||||
|
||||
@@ -231,7 +231,7 @@ namespace OpenRA.Network
|
||||
break;
|
||||
|
||||
if (orderManager.World.Paused != pause && world != null && world.LobbyInfo.NonBotClients.Count() > 1)
|
||||
TextNotificationsManager.AddSystemLine(pause ? GamePaused : GameUnpaused, FluentBundle.Arguments("player", client.Name));
|
||||
TextNotificationsManager.AddSystemLine(pause ? GamePaused : GameUnpaused, "player", client.Name);
|
||||
|
||||
orderManager.World.Paused = pause;
|
||||
orderManager.World.PredictedPaused = pause;
|
||||
|
||||
@@ -239,8 +239,8 @@ namespace OpenRA
|
||||
var botInfo = botInfos.First(b => b.Type == BotType);
|
||||
var botsOfSameType = World.Players.Where(c => c.BotType == BotType).ToArray();
|
||||
return FluentProvider.GetString(EnumeratedBotName,
|
||||
FluentBundle.Arguments("name", FluentProvider.GetString(botInfo.Name),
|
||||
"number", botsOfSameType.IndexOf(this) + 1));
|
||||
"name", FluentProvider.GetString(botInfo.Name),
|
||||
"number", botsOfSameType.IndexOf(this) + 1);
|
||||
}
|
||||
|
||||
return PlayerName;
|
||||
|
||||
@@ -22,12 +22,12 @@ namespace OpenRA.Server
|
||||
readonly Dictionary<int, List<long>> messageTracker = new();
|
||||
readonly Server server;
|
||||
readonly Action<Connection, int, int, byte[]> dispatchOrdersToClient;
|
||||
readonly Action<Connection, string, Dictionary<string, object>> sendLocalizedMessageTo;
|
||||
readonly Action<Connection, string, object[]> sendLocalizedMessageTo;
|
||||
|
||||
public PlayerMessageTracker(
|
||||
Server server,
|
||||
Action<Connection, int, int, byte[]> dispatchOrdersToClient,
|
||||
Action<Connection, string, Dictionary<string, object>> sendLocalizedMessageTo)
|
||||
Action<Connection, string, object[]> sendLocalizedMessageTo)
|
||||
{
|
||||
this.server = server;
|
||||
this.dispatchOrdersToClient = dispatchOrdersToClient;
|
||||
@@ -56,7 +56,7 @@ namespace OpenRA.Server
|
||||
if (!isAdmin && time < settings.FloodLimitJoinCooldown)
|
||||
{
|
||||
var remaining = CalculateRemaining(settings.FloodLimitJoinCooldown);
|
||||
sendLocalizedMessageTo(conn, ChatTemporaryDisabled, FluentBundle.Arguments("remaining", remaining));
|
||||
sendLocalizedMessageTo(conn, ChatTemporaryDisabled, new object[] { "remaining", remaining });
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -64,7 +64,7 @@ namespace OpenRA.Server
|
||||
if (tracker.Count >= settings.FloodLimitMessageCount)
|
||||
{
|
||||
var remaining = CalculateRemaining(tracker[0] + settings.FloodLimitInterval);
|
||||
sendLocalizedMessageTo(conn, ChatTemporaryDisabled, FluentBundle.Arguments("remaining", remaining));
|
||||
sendLocalizedMessageTo(conn, ChatTemporaryDisabled, new object[] { "remaining", remaining });
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -580,7 +580,7 @@ namespace OpenRA.Server
|
||||
|
||||
Log.Write("server", $"{client.Name} ({newConn.EndPoint}) has joined the game.");
|
||||
|
||||
SendLocalizedMessage(Joined, FluentBundle.Arguments("player", client.Name));
|
||||
SendLocalizedMessage(Joined, "player", client.Name);
|
||||
|
||||
if (Type == ServerType.Dedicated)
|
||||
{
|
||||
@@ -952,18 +952,18 @@ namespace OpenRA.Server
|
||||
WriteLineWithTimeStamp(text);
|
||||
}
|
||||
|
||||
public void SendLocalizedMessage(string key, Dictionary<string, object> arguments = null)
|
||||
public void SendLocalizedMessage(string key, params object[] args)
|
||||
{
|
||||
var text = FluentMessage.Serialize(key, arguments);
|
||||
var text = FluentMessage.Serialize(key, args);
|
||||
DispatchServerOrdersToClients(Order.FromTargetString("FluentMessage", text, true));
|
||||
|
||||
if (Type == ServerType.Dedicated)
|
||||
WriteLineWithTimeStamp(FluentProvider.GetString(key, arguments));
|
||||
WriteLineWithTimeStamp(FluentProvider.GetString(key, args));
|
||||
}
|
||||
|
||||
public void SendLocalizedMessageTo(Connection conn, string key, Dictionary<string, object> arguments = null)
|
||||
public void SendLocalizedMessageTo(Connection conn, string key, object[] args = null)
|
||||
{
|
||||
var text = FluentMessage.Serialize(key, arguments);
|
||||
var text = FluentMessage.Serialize(key, args);
|
||||
DispatchOrdersToClient(conn, 0, 0, Order.FromTargetString("FluentMessage", text, true).Serialize());
|
||||
}
|
||||
|
||||
@@ -998,7 +998,7 @@ namespace OpenRA.Server
|
||||
if (!InterpretCommand(o.TargetString, conn))
|
||||
{
|
||||
Log.Write("server", $"Unknown server command: {o.TargetString}");
|
||||
SendLocalizedMessageTo(conn, UnknownServerCommand, FluentBundle.Arguments("command", o.TargetString));
|
||||
SendLocalizedMessageTo(conn, UnknownServerCommand, new object[] { "command", o.TargetString });
|
||||
}
|
||||
|
||||
break;
|
||||
@@ -1180,14 +1180,14 @@ namespace OpenRA.Server
|
||||
if (State == ServerState.GameStarted)
|
||||
{
|
||||
if (dropClient.IsObserver)
|
||||
SendLocalizedMessage(ObserverDisconnected, FluentBundle.Arguments("player", dropClient.Name));
|
||||
SendLocalizedMessage(ObserverDisconnected, "player", dropClient.Name);
|
||||
else if (dropClient.Team > 0)
|
||||
SendLocalizedMessage(PlayerTeamDisconnected, FluentBundle.Arguments("player", dropClient.Name, "team", dropClient.Team));
|
||||
SendLocalizedMessage(PlayerTeamDisconnected, "player", dropClient.Name, "team", dropClient.Team);
|
||||
else
|
||||
SendLocalizedMessage(PlayerDisconnected, FluentBundle.Arguments("player", dropClient.Name));
|
||||
SendLocalizedMessage(PlayerDisconnected, "player", dropClient.Name);
|
||||
}
|
||||
else
|
||||
SendLocalizedMessage(LobbyDisconnected, FluentBundle.Arguments("player", dropClient.Name));
|
||||
SendLocalizedMessage(LobbyDisconnected, "player", dropClient.Name);
|
||||
|
||||
LobbyInfo.Clients.RemoveAll(c => c.Index == toDrop.PlayerIndex);
|
||||
|
||||
@@ -1204,7 +1204,7 @@ namespace OpenRA.Server
|
||||
if (nextAdmin != null)
|
||||
{
|
||||
nextAdmin.IsAdmin = true;
|
||||
SendLocalizedMessage(NewAdmin, FluentBundle.Arguments("player", nextAdmin.Name));
|
||||
SendLocalizedMessage(NewAdmin, "player", nextAdmin.Name);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -107,7 +107,7 @@ namespace OpenRA.Server
|
||||
if (!kickee.IsObserver && !server.HasClientWonOrLost(kickee))
|
||||
{
|
||||
// Vote kick cannot be the sole deciding factor for a game.
|
||||
server.SendLocalizedMessageTo(conn, InsufficientVotes, FluentBundle.Arguments("kickee", kickee.Name));
|
||||
server.SendLocalizedMessageTo(conn, InsufficientVotes, new object[] { "kickee", kickee.Name });
|
||||
EndKickVote();
|
||||
return false;
|
||||
}
|
||||
@@ -135,7 +135,7 @@ namespace OpenRA.Server
|
||||
|
||||
Log.Write("server", $"Vote kick started on {kickeeID}.");
|
||||
voteKickTimer = Stopwatch.StartNew();
|
||||
server.SendLocalizedMessage(VoteKickStarted, FluentBundle.Arguments("kicker", kicker.Name, "kickee", kickee.Name));
|
||||
server.SendLocalizedMessage(VoteKickStarted, "kicker", kicker.Name, "kickee", kickee.Name);
|
||||
server.DispatchServerOrdersToClients(new Order("StartKickVote", null, false) { ExtraData = (uint)kickeeID }.Serialize());
|
||||
this.kickee = (kickee, kickeeConn);
|
||||
voteKickerStarter = (kicker, conn);
|
||||
@@ -145,7 +145,7 @@ namespace OpenRA.Server
|
||||
voteTracker[conn.PlayerIndex] = vote;
|
||||
else
|
||||
{
|
||||
server.SendLocalizedMessageTo(conn, AlreadyVoted, null);
|
||||
server.SendLocalizedMessageTo(conn, AlreadyVoted);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -168,9 +168,9 @@ namespace OpenRA.Server
|
||||
}
|
||||
|
||||
var votesNeeded = eligiblePlayers / 2 + 1;
|
||||
server.SendLocalizedMessage(VoteKickProgress, FluentBundle.Arguments(
|
||||
server.SendLocalizedMessage(VoteKickProgress,
|
||||
"kickee", kickee.Name,
|
||||
"percentage", votesFor * 100 / eligiblePlayers));
|
||||
"percentage", votesFor * 100 / eligiblePlayers);
|
||||
|
||||
// If a player or players during a vote lose or disconnect, it is possible that a downvote will
|
||||
// kick a client. Guard against that situation.
|
||||
@@ -210,7 +210,7 @@ namespace OpenRA.Server
|
||||
return;
|
||||
|
||||
if (sendMessage)
|
||||
server.SendLocalizedMessage(VoteKickEnded, FluentBundle.Arguments("kickee", kickee.Client.Name));
|
||||
server.SendLocalizedMessage(VoteKickEnded, "kickee", kickee.Client.Name);
|
||||
|
||||
server.DispatchServerOrdersToClients(new Order("EndKickVote", null, false) { ExtraData = (uint)kickee.Client.Index }.Serialize());
|
||||
|
||||
|
||||
@@ -41,9 +41,9 @@ namespace OpenRA
|
||||
AddTextNotification(TextNotificationPool.Transients, SystemClientId, SystemMessageLabel, FluentProvider.GetString(text));
|
||||
}
|
||||
|
||||
public static void AddFeedbackLine(string text, Dictionary<string, object> arguments = null)
|
||||
public static void AddFeedbackLine(string text, params object[] args)
|
||||
{
|
||||
AddTextNotification(TextNotificationPool.Feedback, SystemClientId, SystemMessageLabel, FluentProvider.GetString(text, arguments));
|
||||
AddTextNotification(TextNotificationPool.Feedback, SystemClientId, SystemMessageLabel, FluentProvider.GetString(text, args));
|
||||
}
|
||||
|
||||
public static void AddMissionLine(string prefix, string text, Color? prefixColor = null)
|
||||
@@ -51,19 +51,19 @@ namespace OpenRA
|
||||
AddTextNotification(TextNotificationPool.Mission, SystemClientId, prefix, text, prefixColor);
|
||||
}
|
||||
|
||||
public static void AddPlayerJoinedLine(string text, Dictionary<string, object> arguments = null)
|
||||
public static void AddPlayerJoinedLine(string text, params object[] args)
|
||||
{
|
||||
AddTextNotification(TextNotificationPool.Join, SystemClientId, SystemMessageLabel, FluentProvider.GetString(text, arguments));
|
||||
AddTextNotification(TextNotificationPool.Join, SystemClientId, SystemMessageLabel, FluentProvider.GetString(text, args));
|
||||
}
|
||||
|
||||
public static void AddPlayerLeftLine(string text, Dictionary<string, object> arguments = null)
|
||||
public static void AddPlayerLeftLine(string text, params object[] args)
|
||||
{
|
||||
AddTextNotification(TextNotificationPool.Leave, SystemClientId, SystemMessageLabel, FluentProvider.GetString(text, arguments));
|
||||
AddTextNotification(TextNotificationPool.Leave, SystemClientId, SystemMessageLabel, FluentProvider.GetString(text, args));
|
||||
}
|
||||
|
||||
public static void AddSystemLine(string text, Dictionary<string, object> arguments = null)
|
||||
public static void AddSystemLine(string text, params object[] args)
|
||||
{
|
||||
AddSystemLine(SystemMessageLabel, FluentProvider.GetString(text, arguments));
|
||||
AddSystemLine(SystemMessageLabel, FluentProvider.GetString(text, args));
|
||||
}
|
||||
|
||||
public static void AddSystemLine(string prefix, string text)
|
||||
|
||||
Reference in New Issue
Block a user