Code cleanup

This commit is contained in:
Matthias Mailänder
2022-05-28 16:09:52 +02:00
committed by teinarss
parent 10ac07bf9f
commit 2c8c6e50da
20 changed files with 42 additions and 35 deletions

View File

@@ -160,7 +160,7 @@ namespace OpenRA.Mods.Common.Traits
{
TextNotificationsManager.AddSystemLine(Notification.F(m, m > 1 ? "s" : null));
var faction = self.World.LocalPlayer == null ? null : self.World.LocalPlayer.Faction.InternalName;
var faction = self.World.LocalPlayer?.Faction.InternalName;
Game.Sound.PlayNotification(self.World.Map.Rules, self.World.LocalPlayer, "Speech", info.TimeLimitWarnings[m], faction);
}
}

View File

@@ -97,7 +97,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
var spriteWidget = panel.GetOrNull<SpriteWidget>("SPRITE");
if (spriteWidget != null)
{
spriteWidget.GetSprite = () => currentSprites != null ? currentSprites[currentFrame] : null;
spriteWidget.GetSprite = () => currentSprites?[currentFrame];
currentPalette = spriteWidget.Palette;
spriteScale = spriteWidget.Scale;
spriteWidget.GetPalette = () => currentPalette;
@@ -394,7 +394,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
if (string.IsNullOrWhiteSpace(filter))
return true;
if (filename.IndexOf(filter, StringComparison.OrdinalIgnoreCase) >= 0)
if (filename.Contains(filter, StringComparison.OrdinalIgnoreCase))
return true;
return false;
@@ -524,11 +524,12 @@ namespace OpenRA.Mods.Common.Widgets.Logic
else
return false;
}
catch (Exception ex)
catch (Exception exception)
{
isLoadError = true;
Log.AddChannel("assetbrowser", "assetbrowser.log");
Log.Write("assetbrowser", "Error reading {0}:{3} {1}{3}{2}", filename, ex.Message, ex.StackTrace, Environment.NewLine);
Log.Write("assetbrowser", $"Error reading {filename}");
Log.Write("assetbrowser", exception);
return false;
}

View File

@@ -90,7 +90,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
}
}
IEnumerable<string> ParseLines(Stream file)
static IEnumerable<string> ParseLines(Stream file)
{
return file.ReadAllLines().Select(l => l.Replace("\t", " ").Replace("*", "\u2022")).ToList();
}

View File

@@ -190,15 +190,15 @@ namespace OpenRA.Mods.Common.Widgets.Logic
map.Save(package);
Console.WriteLine("Saved current map at {0}", combinedPath);
Console.WriteLine($"Saved current map at {combinedPath}");
Ui.CloseWindow();
onSave(map.Uid);
}
catch (Exception e)
{
Log.Write("debug", "Failed to save map at {0}: {1}", combinedPath, e.Message);
Log.Write("debug", "{0}", e.StackTrace);
Log.Write("debug", $"Failed to save map at {combinedPath}");
Log.Write("debug", e);
ConfirmationDialogs.ButtonPrompt(
title: "Failed to save map",

View File

@@ -153,7 +153,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
}
}
public void Order(World world, string order)
public static void Order(World world, string order)
{
world.IssueOrder(new Order(order, world.LocalPlayer.PlayerActor, false));
}

View File

@@ -58,7 +58,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
mo.ObjectiveAdded += redrawObjectives;
}
void PopulateObjectivesList(MissionObjectives mo, ScrollPanelWidget parent, ContainerWidget template)
static void PopulateObjectivesList(MissionObjectives mo, ScrollPanelWidget parent, ContainerWidget template)
{
parent.RemoveChildren();

View File

@@ -469,7 +469,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
return template;
}
void SetupPlayerColor(Player player, ScrollItemWidget template, ColorBlockWidget colorBlockWidget, GradientColorBlockWidget gradientColorBlockWidget)
static void SetupPlayerColor(Player player, ScrollItemWidget template, ColorBlockWidget colorBlockWidget, GradientColorBlockWidget gradientColorBlockWidget)
{
var color = Color.FromArgb(128, player.Color.R, player.Color.G, player.Color.B);
var hoverColor = Color.FromArgb(192, player.Color.R, player.Color.G, player.Color.B);

View File

@@ -48,7 +48,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
var requiresFormat = requiresLabel.Text;
ActorInfo lastActor = null;
Hotkey lastHotkey = Hotkey.Invalid;
var lastHotkey = Hotkey.Invalid;
var lastPowerState = pm?.PowerState ?? PowerState.Normal;
var descLabelY = descLabel.Bounds.Y;
var descLabelPadding = descLabel.Bounds.Height;

View File

@@ -34,7 +34,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
var timeOffset = timeLabel.Bounds.X;
SupportPowerInstance lastPower = null;
Hotkey lastHotkey = Hotkey.Invalid;
var lastHotkey = Hotkey.Invalid;
var lastRemainingSeconds = 0;
tooltipContainer.BeforeRender = () =>

View File

@@ -137,7 +137,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
var path = FindSourcePath(kv.Value, volumes);
if (path != null)
{
Log.Write("install", "Using installer `{0}: {1}` of type `{2}`:", kv.Key, kv.Value.Title, kv.Value.Type);
Log.Write("install", $"Using installer `{kv.Key}: {kv.Value.Title}` of type `{kv.Value.Type}`:");
var packages = content.Packages.Values
.Where(p => p.Sources.Contains(kv.Key) && !p.IsInstalled())
@@ -271,13 +271,13 @@ namespace OpenRA.Mods.Common.Widgets.Logic
// Yaml path may be specified relative to a named directory (e.g. ^SupportDir) or the detected disc path
var sourcePath = i.Value.Value.StartsWith("^") ? Platform.ResolvePath(i.Value.Value) : Path.Combine(path, i.Value.Value);
Log.Write("debug", "Deleting {0}", sourcePath);
Log.Write("debug", $"Deleting {sourcePath}");
File.Delete(sourcePath);
break;
}
default:
Log.Write("debug", "Unknown installation command {0} - ignoring", i.Key);
Log.Write("debug", $"Unknown installation command {i.Key} - ignoring");
break;
}
}
@@ -499,7 +499,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
return null;
}
bool IsValidSourcePath(string path, ModContent.ModSource source)
static bool IsValidSourcePath(string path, ModContent.ModSource source)
{
try
{

View File

@@ -212,7 +212,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
slotsButton.IsVisible = () => panel != PanelType.Servers;
slotsButton.IsDisabled = () => configurationDisabled() || panel != PanelType.Players ||
(orderManager.LobbyInfo.Slots.Values.All(s => !s.AllowBots) &&
orderManager.LobbyInfo.Slots.Count(s => !s.Value.LockTeam && orderManager.LobbyInfo.ClientInSlot(s.Key) != null) == 0);
!orderManager.LobbyInfo.Slots.Any(s => !s.Value.LockTeam && orderManager.LobbyInfo.ClientInSlot(s.Key) != null));
slotsButton.OnMouseDown = _ =>
{
@@ -269,7 +269,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
{
Title = $"{d} Teams",
IsSelected = () => false,
OnClick = () => orderManager.IssueOrder(Order.Command($"assignteams {d.ToString()}"))
OnClick = () => orderManager.IssueOrder(Order.Command($"assignteams {d}"))
}).ToList();
if (orderManager.LobbyInfo.Slots.Any(s => s.Value.AllowBots))

View File

@@ -45,7 +45,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
{
"Slot", new List<SlotDropDownOption>
{
new SlotDropDownOption("Open", "slot_open " + slot.PlayerReference, () => (!slot.Closed && client == null)),
new SlotDropDownOption("Open", "slot_open " + slot.PlayerReference, () => !slot.Closed && client == null),
new SlotDropDownOption("Closed", "slot_close " + slot.PlayerReference, () => slot.Closed)
}
}

View File

@@ -20,7 +20,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
[ObjectCreator.UseCtor]
public SpawnSelectorTooltipLogic(Widget widget, TooltipContainerWidget tooltipContainer, MapPreviewWidget preview, bool showUnoccupiedSpawnpoints)
{
bool showTooltip = true;
var showTooltip = true;
widget.IsVisible = () => preview.TooltipSpawnIndex != -1 && showTooltip;
var label = widget.Get<LabelWidget>("LABEL");
var flag = widget.Get<ImageWidget>("FLAG");

View File

@@ -346,7 +346,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
});
}
void LoadMapIntoEditor(string uid)
static void LoadMapIntoEditor(string uid)
{
Game.LoadEditor(uid);

View File

@@ -126,7 +126,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
SetupMapTab(MapClassification.User, filter, "USER_MAPS_TAB_BUTTON", "USER_MAPS_TAB", itemTemplate);
SetupMapTab(MapClassification.System, filter, "SYSTEM_MAPS_TAB_BUTTON", "SYSTEM_MAPS_TAB", itemTemplate);
if (initialMap == null && tabMaps.Keys.Contains(initialTab) && tabMaps[initialTab].Length > 0)
if (initialMap == null && tabMaps.ContainsKey(initialTab) && tabMaps[initialTab].Length > 0)
{
selectedUid = Game.ModData.MapCache.ChooseInitialMap(tabMaps[initialTab].Select(mp => mp.Uid).First(),
Game.CosmeticRandom);
@@ -178,8 +178,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
{
foreach (var category in map.Categories)
{
var count = 0;
categoryDict.TryGetValue(category, out count);
categoryDict.TryGetValue(category, out var count);
categoryDict[category] = count + 1;
}
}

View File

@@ -211,9 +211,9 @@ namespace OpenRA.Mods.Common.Widgets.Logic
{
var badges = Ui.LoadWidget("PLAYER_PROFILE_BADGES_INSERT", badgeContainer, new WidgetArgs()
{
{ "worldRenderer", worldRenderer },
{ "profile", profile },
{ "negotiateWidth", negotiateWidth }
{ nameof(worldRenderer), worldRenderer },
{ nameof(profile), profile },
{ nameof(negotiateWidth), negotiateWidth }
});
if (badges.Bounds.Height > 0)
@@ -236,7 +236,8 @@ namespace OpenRA.Mods.Common.Widgets.Logic
}
catch (Exception e)
{
Log.Write("debug", "Failed to parse player data result with exception: {0}", e);
Log.Write("debug", "Failed to parse player data result with exception");
Log.Write("debug", e);
}
finally
{

View File

@@ -445,7 +445,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
};
var deleteAllButton = panel.Get<ButtonWidget>("MNG_DELALL_BUTTON");
deleteAllButton.IsDisabled = () => replayState.Count(kvp => kvp.Value.Visible) == 0;
deleteAllButton.IsDisabled = () => !replayState.Any(kvp => kvp.Value.Visible);
deleteAllButton.OnClick = () =>
{
var list = replayState.Where(kvp => kvp.Value.Visible).Select(kvp => kvp.Key).ToList();
@@ -511,7 +511,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
replayState.Remove(replay);
}
bool EvaluateReplayVisibility(ReplayMetadata replay)
static bool EvaluateReplayVisibility(ReplayMetadata replay)
{
// Game type
if ((filter.Type == GameType.Multiplayer && replay.GameInfo.IsSinglePlayer) || (filter.Type == GameType.Singleplayer && !replay.GameInfo.IsSinglePlayer))
@@ -675,7 +675,8 @@ namespace OpenRA.Mods.Common.Widgets.Logic
}
catch (Exception e)
{
Log.Write("debug", "Exception while parsing replay: {0}", e);
Log.Write("debug", $"Exception while parsing replay: {replay}");
Log.Write("debug", e);
SelectReplay(null);
}
}

View File

@@ -320,7 +320,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
RefreshServerList();
}
string PlayersLabel(GameServer game)
static string PlayersLabel(GameServer game)
{
return $"{(game.Players > 0 ? game.Players.ToString() : "No")} Player{(game.Players != 1 ? "s" : "")}{(game.Bots > 0 ? $", {game.Bots} Bot{(game.Bots != 1 ? "s" : "")}" : "")}{(game.Spectators > 0 ? $", {game.Spectators} Spectator{(game.Spectators != 1 ? "s" : "")}" : "")}";
}

View File

@@ -184,7 +184,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
dropdown.ShowDropDown("LABEL_DROPDOWN_TEMPLATE", 500, options.Keys, setupItem);
}
void MakeMouseFocusSettingsLive()
static void MakeMouseFocusSettingsLive()
{
var gameSettings = Game.Settings.Game;