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

Move Text handling to its own class #19436

Merged
merged 1 commit into from Jun 4, 2021
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
30 changes: 1 addition & 29 deletions OpenRA.Game/Game.cs
Expand Up @@ -474,11 +474,6 @@ public static void InitializeMod(string mod, Arguments args)

JoinLocal();

ChromeMetrics.TryGet("ChatMessageColor", out chatMessageColor);
ChromeMetrics.TryGet("SystemMessageColor", out systemMessageColor);
if (!ChromeMetrics.TryGet("SystemMessageLabel", out systemMessageLabel))
systemMessageLabel = "Battlefield Control";

ModData.LoadScreen.StartGame(args);
}

Expand Down Expand Up @@ -559,9 +554,6 @@ public static void SwitchToExternalMod(ExternalMod mod, string[] launchArguments
// Note: These delayed actions should only be used by widgets or disposing objects
// - things that depend on a particular world should be queuing them on the world actor.
static volatile ActionQueue delayedActions = new ActionQueue();
static Color systemMessageColor = Color.White;
static Color chatMessageColor = Color.White;
static string systemMessageLabel;

public static void RunAfterTick(Action a) { delayedActions.Add(a, RunTime); }
public static void RunAfterDelay(int delayMilliseconds, Action a) { delayedActions.Add(a, RunTime + delayMilliseconds); }
Expand All @@ -579,7 +571,7 @@ static void TakeScreenshotInner()
Log.Write("debug", "Taking screenshot " + path);

Renderer.SaveScreenshot(path);
Debug("Saved screenshot " + filename);
TextNotificationsManager.Debug("Saved screenshot " + filename);
}
}

Expand Down Expand Up @@ -911,26 +903,6 @@ public static void Exit()
state = RunStatus.Success;
}

public static void AddSystemLine(string text)
{
AddSystemLine(systemMessageLabel, text);
}

public static void AddSystemLine(string name, string text)
{
OrderManager.AddChatLine(name, systemMessageColor, text, systemMessageColor);
}

public static void AddChatLine(string name, Color nameColor, string text)
{
OrderManager.AddChatLine(name, nameColor, text, chatMessageColor);
}

public static void Debug(string s, params object[] args)
{
AddSystemLine("Debug", string.Format(s, args));
}

public static void Disconnect()
{
OrderManager.World?.TraitDict.PrintReport();
Expand Down
4 changes: 2 additions & 2 deletions OpenRA.Game/Network/Order.cs
Expand Up @@ -212,8 +212,8 @@ public static Order Deserialize(World world, BinaryReader r)
Log.Write("debug", e.ToString());

// HACK: this can hopefully go away in the future
Game.Debug("Ignoring malformed order that would have crashed the game");
Game.Debug("Please file a bug report and include the replay from this match");
TextNotificationsManager.Debug("Ignoring malformed order that would have crashed the game");
TextNotificationsManager.Debug("Please file a bug report and include the replay from this match");

return null;
}
Expand Down
16 changes: 8 additions & 8 deletions OpenRA.Game/Network/UnitOrders.cs
Expand Up @@ -31,7 +31,7 @@ internal static void ProcessOrder(OrderManager orderManager, World world, int cl
{
// Server message
case "Message":
Game.AddSystemLine(order.TargetString);
TextNotificationsManager.AddSystemLine(order.TargetString);
break;

// Reports that the target player disconnected
Expand Down Expand Up @@ -70,7 +70,7 @@ internal static void ProcessOrder(OrderManager orderManager, World world, int cl
if (orderManager.LocalClient != null && client != orderManager.LocalClient && client.Team > 0 && client.Team == orderManager.LocalClient.Team)
suffix += " (Ally)";

Game.AddChatLine(client.Name + suffix, client.Color, message);
TextNotificationsManager.AddChatLine(client.Name + suffix, client.Color, message);
break;
}

Expand All @@ -79,7 +79,7 @@ internal static void ProcessOrder(OrderManager orderManager, World world, int cl
{
var prefix = order.ExtraData == uint.MaxValue ? "[Spectators] " : "[Team] ";
if (orderManager.LocalClient != null && client.Team == orderManager.LocalClient.Team)
Game.AddChatLine(prefix + client.Name, client.Color, message);
TextNotificationsManager.AddChatLine(prefix + client.Name, client.Color, message);

break;
}
Expand All @@ -93,7 +93,7 @@ internal static void ProcessOrder(OrderManager orderManager, World world, int cl
{
// Validate before adding the line
if (client.IsObserver || (player != null && player.WinState != WinState.Undefined))
Game.AddChatLine("[Spectators] " + client.Name, client.Color, message);
TextNotificationsManager.AddChatLine("[Spectators] " + client.Name, client.Color, message);

break;
}
Expand All @@ -103,7 +103,7 @@ internal static void ProcessOrder(OrderManager orderManager, World world, int cl
&& world.LocalPlayer != null && world.LocalPlayer.WinState == WinState.Undefined;

if (valid && (isSameTeam || world.IsReplay))
Game.AddChatLine("[Team" + (world.IsReplay ? " " + order.ExtraData : "") + "] " + client.Name, client.Color, message);
TextNotificationsManager.AddChatLine("[Team" + (world.IsReplay ? " " + order.ExtraData : "") + "] " + client.Name, client.Color, message);

break;
}
Expand Down Expand Up @@ -133,7 +133,7 @@ internal static void ProcessOrder(OrderManager orderManager, World world, int cl
FieldLoader.GetValue<int>("SaveSyncFrame", saveSyncFrame.Value.Value);
}
else
Game.AddSystemLine("The game has started.");
TextNotificationsManager.AddSystemLine("The game has started.");

Game.StartGame(orderManager.LobbyInfo.GlobalSettings.Map, WorldType.Regular);
break;
Expand All @@ -151,7 +151,7 @@ internal static void ProcessOrder(OrderManager orderManager, World world, int cl

case "GameSaved":
if (!orderManager.World.IsReplay)
Game.AddSystemLine("Game saved");
TextNotificationsManager.AddSystemLine("Game saved");

foreach (var nsr in orderManager.World.WorldActor.TraitsImplementing<INotifyGameSaved>())
nsr.GameSaved(orderManager.World);
Expand All @@ -171,7 +171,7 @@ internal static void ProcessOrder(OrderManager orderManager, World world, int cl
if (orderManager.World.Paused != pause && world != null && world.LobbyInfo.NonBotClients.Count() > 1)
{
var pausetext = $"The game is {(pause ? "paused" : "un-paused")} by {client.Name}";
Game.AddSystemLine(pausetext);
TextNotificationsManager.AddSystemLine(pausetext);
}

orderManager.World.Paused = pause;
Expand Down
4 changes: 2 additions & 2 deletions OpenRA.Game/Orders/UnitOrderGenerator.cs
Expand Up @@ -188,9 +188,9 @@ static UnitOrderResult OrderForUnit(Actor self, Target target, List<Actor> actor
static Order CheckSameOrder(IOrderTargeter iot, Order order)
{
if (order == null && iot.OrderID != null)
Game.Debug("BUG: in order targeter - decided on {0} but then didn't order", iot.OrderID);
TextNotificationsManager.Debug("BUG: in order targeter - decided on {0} but then didn't order", iot.OrderID);
else if (order != null && iot.OrderID != order.OrderString)
Game.Debug("BUG: in order targeter - decided on {0} but ordered {1}", iot.OrderID, order.OrderString);
TextNotificationsManager.Debug("BUG: in order targeter - decided on {0} but ordered {1}", iot.OrderID, order.OrderString);
return order;
}

Expand Down
51 changes: 51 additions & 0 deletions OpenRA.Game/TextNotificationsManager.cs
@@ -0,0 +1,51 @@
#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 OpenRA.Primitives;
using OpenRA.Widgets;

namespace OpenRA
{
public static class TextNotificationsManager
{
static Color systemMessageColor = Color.White;
static Color chatMessageColor = Color.White;
static string systemMessageLabel;

static TextNotificationsManager()
{
ChromeMetrics.TryGet("ChatMessageColor", out chatMessageColor);
ChromeMetrics.TryGet("SystemMessageColor", out systemMessageColor);
if (!ChromeMetrics.TryGet("SystemMessageLabel", out systemMessageLabel))
systemMessageLabel = "Battlefield Control";
}

public static void AddSystemLine(string text)
{
AddSystemLine(systemMessageLabel, text);
}

public static void AddSystemLine(string name, string text)
{
Game.OrderManager.AddChatLine(name, systemMessageColor, text, systemMessageColor);
}

public static void AddChatLine(string name, Color nameColor, string text)
{
Game.OrderManager.AddChatLine(name, nameColor, text, chatMessageColor);
}

public static void Debug(string s, params object[] args)
{
AddSystemLine("Debug", string.Format(s, args));
}
}
}
4 changes: 2 additions & 2 deletions OpenRA.Game/WAngle.cs
Expand Up @@ -226,7 +226,7 @@ public LuaValue Add(LuaRuntime runtime, LuaValue left, LuaValue right)

if (right.TryGetClrValue(out int c))
{
Game.Debug("Support for facing calculations mixing Angle with integers is deprecated. Make sure all facing calculations use Angle");
TextNotificationsManager.Debug("Support for facing calculations mixing Angle with integers is deprecated. Make sure all facing calculations use Angle");
return new LuaCustomClrObject(a + FromFacing(c));
}

Expand All @@ -243,7 +243,7 @@ public LuaValue Subtract(LuaRuntime runtime, LuaValue left, LuaValue right)

if (right.TryGetClrValue(out int c))
{
Game.Debug("Support for facing calculations mixing Angle with integers is deprecated. Make sure all facing calculations use Angle");
TextNotificationsManager.Debug("Support for facing calculations mixing Angle with integers is deprecated. Make sure all facing calculations use Angle");
return new LuaCustomClrObject(a - FromFacing(c));
}

Expand Down
2 changes: 1 addition & 1 deletion OpenRA.Game/WDist.cs
Expand Up @@ -143,7 +143,7 @@ public LuaValue Equals(LuaRuntime runtime, LuaValue left, LuaValue right)
switch (key.ToString())
{
case "Length": return Length;
case "Range": Game.Debug("WDist.Range is deprecated. Use WDist.Length instead"); return Length;
case "Range": TextNotificationsManager.Debug("WDist.Range is deprecated. Use WDist.Length instead"); return Length;
default: throw new LuaException($"WDist does not define a member '{key}'");
}
}
Expand Down
2 changes: 1 addition & 1 deletion OpenRA.Mods.Common/AIUtils.cs
Expand Up @@ -77,7 +77,7 @@ public static ActorInfo GetInfoByCommonName(HashSet<string> names, Player owner)
public static void BotDebug(string s, params object[] args)
{
if (Game.Settings.Debug.BotDebug)
Game.Debug(s, args);
TextNotificationsManager.Debug(s, args);
}
}
}
2 changes: 1 addition & 1 deletion OpenRA.Mods.Common/Commands/ChatCommands.cs
Expand Up @@ -39,7 +39,7 @@ public bool OnChat(string playername, string message)
if (command.Value != null)
command.Value.InvokeCommand(name.ToLowerInvariant(), message.Substring(1 + name.Length).Trim());
else
Game.Debug("{0} is not a valid command.", name);
TextNotificationsManager.Debug("{0} is not a valid command.", name);

return false;
}
Expand Down
2 changes: 1 addition & 1 deletion OpenRA.Mods.Common/Commands/DevCommands.cs
Expand Up @@ -66,7 +66,7 @@ public void InvokeCommand(string name, string arg)

if (!developerMode.Enabled)
{
Game.Debug("Cheats are disabled.");
TextNotificationsManager.Debug("Cheats are disabled.");
return;
}

Expand Down
4 changes: 2 additions & 2 deletions OpenRA.Mods.Common/Commands/HelpCommand.cs
Expand Up @@ -42,14 +42,14 @@ public void WorldLoaded(World w, WorldRenderer wr)

public void InvokeCommand(string name, string arg)
{
Game.Debug("Here are the available commands:");
TextNotificationsManager.Debug("Here are the available commands:");

foreach (var key in console.Commands.Keys)
{
if (!helpDescriptions.TryGetValue(key, out var description))
description = "no description available.";

Game.Debug("{0}: {1}", key, description);
TextNotificationsManager.Debug("{0}: {1}", key, description);
}
}

Expand Down
2 changes: 1 addition & 1 deletion OpenRA.Mods.Common/Scripting/Global/ActorGlobal.cs
Expand Up @@ -77,7 +77,7 @@ ActorInit CreateInit(string initName, LuaValue value)
if (value.TryGetClrValue(out int facing))
{
facingInit.Initialize(WAngle.FromFacing(facing));
Game.Debug("Initializing Facing with integers is deprecated. Use Angle instead.");
TextNotificationsManager.Debug("Initializing Facing with integers is deprecated. Use Angle instead.");
return facingInit;
}
}
Expand Down
2 changes: 1 addition & 1 deletion OpenRA.Mods.Common/Scripting/Global/FacingGlobal.cs
Expand Up @@ -21,7 +21,7 @@ public FacingGlobal(ScriptContext context)

void Deprecated()
{
Game.Debug("The Facing table is deprecated. Use Angle instead.");
TextNotificationsManager.Debug("The Facing table is deprecated. Use Angle instead.");
}

public int North { get { Deprecated(); return 0; } }
Expand Down
8 changes: 4 additions & 4 deletions OpenRA.Mods.Common/Scripting/Global/MediaGlobal.cs
Expand Up @@ -194,7 +194,7 @@ public void DisplayMessage(string text, string prefix = "Mission", Color? color
return;

var c = color.HasValue ? color.Value : Color.White;
Game.AddChatLine(prefix, c, text);
TextNotificationsManager.AddChatLine(prefix, c, text);
}

[Desc("Display a system message to the player. If 'prefix' is nil the default system prefix is used.")]
Expand All @@ -204,9 +204,9 @@ public void DisplaySystemMessage(string text, string prefix = null)
return;

if (string.IsNullOrEmpty(prefix))
Game.AddSystemLine(text);
TextNotificationsManager.AddSystemLine(text);
else
Game.AddSystemLine(prefix, text);
TextNotificationsManager.AddSystemLine(prefix, text);
}

[Desc("Displays a debug message to the player, if \"Show Map Debug Messages\" is checked in the settings.")]
Expand All @@ -215,7 +215,7 @@ public void Debug(string text)
if (string.IsNullOrEmpty(text) || !Game.Settings.Debug.LuaDebug)
return;

Game.Debug(text);
TextNotificationsManager.Debug(text);
}

[Desc("Display a text message at the specified location.")]
Expand Down
Expand Up @@ -36,14 +36,14 @@ public Actor[] TargetAirstrike(WPos target, WAngle? facing = null)
[Desc("Activate the actor's Airstrike Power. DEPRECATED! Will be removed.")]
public void SendAirstrike(WPos target, bool randomize = true, int facing = 0)
{
Game.Debug("SendAirstrike is deprecated. Use TargetAirstrike instead.");
TextNotificationsManager.Debug("SendAirstrike is deprecated. Use TargetAirstrike instead.");
ap.SendAirstrike(Self, target, randomize ? (WAngle?)null : WAngle.FromFacing(facing));
}

[Desc("Activate the actor's Airstrike Power. DEPRECATED! Will be removed.")]
public void SendAirstrikeFrom(CPos from, CPos to)
{
Game.Debug("SendAirstrikeFrom is deprecated. Use TargetAirstrike instead.");
TextNotificationsManager.Debug("SendAirstrikeFrom is deprecated. Use TargetAirstrike instead.");
var i = Self.World.Map.CenterOfCell(from);
var j = Self.World.Map.CenterOfCell(to);

Expand Down
Expand Up @@ -37,7 +37,7 @@ public Actor[] TargetParatroopers(WPos target, WAngle? facing = null)
[Desc("Activate the actor's Paratroopers Power. Returns the aircraft that will drop the reinforcements. DEPRECATED! Will be removed.")]
public Actor[] ActivateParatroopers(WPos target, int facing = -1)
{
Game.Debug("SendParatroopersFrom is deprecated. Use TargetParatroopers instead.");
TextNotificationsManager.Debug("SendParatroopersFrom is deprecated. Use TargetParatroopers instead.");
var actors = pp.SendParatroopers(Self, target, facing == -1 ? (WAngle?)null : WAngle.FromFacing(facing));
return actors.Aircraft;
}
Expand Down
4 changes: 2 additions & 2 deletions OpenRA.Mods.Common/Traits/Player/ConquestVictoryConditions.cs
Expand Up @@ -99,7 +99,7 @@ void INotifyWinStateChanged.OnPlayerLost(Player player)
if (info.SuppressNotifications)
return;

Game.AddSystemLine(player.PlayerName + " is defeated.");
TextNotificationsManager.AddSystemLine(player.PlayerName + " is defeated.");
Game.RunAfterDelay(info.NotificationDelay, () =>
{
if (Game.IsCurrentWorld(player.World) && player == player.World.LocalPlayer)
Expand All @@ -112,7 +112,7 @@ void INotifyWinStateChanged.OnPlayerWon(Player player)
if (info.SuppressNotifications)
return;

Game.AddSystemLine(player.PlayerName + " is victorious.");
TextNotificationsManager.AddSystemLine(player.PlayerName + " is victorious.");
Game.RunAfterDelay(info.NotificationDelay, () =>
{
if (Game.IsCurrentWorld(player.World) && player == player.World.LocalPlayer)
Expand Down
2 changes: 1 addition & 1 deletion OpenRA.Mods.Common/Traits/Player/DeveloperMode.cs
Expand Up @@ -273,7 +273,7 @@ public void ResolveOrder(Actor self, Order order)
return;
}

Game.Debug("Cheat used: {0} by {1}{2}", order.OrderString, self.Owner.PlayerName, debugSuffix);
TextNotificationsManager.Debug("Cheat used: {0} by {1}{2}", order.OrderString, self.Owner.PlayerName, debugSuffix);
}

bool IUnlocksRenderPlayer.RenderPlayerUnlocked => Enabled;
Expand Down
Expand Up @@ -137,7 +137,7 @@ void INotifyWinStateChanged.OnPlayerLost(Player player)
if (info.SuppressNotifications)
return;

Game.AddSystemLine(player.PlayerName + " is defeated.");
TextNotificationsManager.AddSystemLine(player.PlayerName + " is defeated.");
Game.RunAfterDelay(info.NotificationDelay, () =>
{
if (Game.IsCurrentWorld(player.World) && player == player.World.LocalPlayer)
Expand All @@ -150,7 +150,7 @@ void INotifyWinStateChanged.OnPlayerWon(Player player)
if (info.SuppressNotifications)
return;

Game.AddSystemLine(player.PlayerName + " is victorious.");
TextNotificationsManager.AddSystemLine(player.PlayerName + " is victorious.");
Game.RunAfterDelay(info.NotificationDelay, () =>
{
if (Game.IsCurrentWorld(player.World) && player == player.World.LocalPlayer)
Expand Down