Skip to content

Commit

Permalink
Fix selected map in server creation panel not updating
Browse files Browse the repository at this point in the history
  • Loading branch information
PunkPun committed Apr 22, 2022
1 parent 6c81590 commit 6a57a50
Showing 1 changed file with 59 additions and 3 deletions.
62 changes: 59 additions & 3 deletions OpenRA.Mods.Common/Widgets/Logic/ServerCreationLogic.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#region Copyright & License Information
/*
* Copyright 2007-2021 The OpenRA Developers (see AUTHORS)
* Copyright 2007-2022 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
Expand All @@ -23,17 +23,24 @@ public class ServerCreationLogic : ChromeLogic
readonly LabelWidget noticesLabelA, noticesLabelB, noticesLabelC;
readonly Action onCreate;
readonly Action onExit;
readonly ModData modData;
MapPreview preview = MapCache.UnknownMap;
bool advertiseOnline;

string oldMapUid;
string newMapUid;
string lastUpdatedUid;

[ObjectCreator.UseCtor]
public ServerCreationLogic(Widget widget, ModData modData, Action onExit, Action openLobby)
{
this.modData = modData;
panel = widget;
onCreate = openLobby;
this.onExit = onExit;

var settings = Game.Settings;
modData.MapCache.MapUpdated += TrackRelevantMapUpdates;
preview = modData.MapCache[modData.MapCache.ChooseInitialMap(Game.Settings.Server.Map, Game.CosmeticRandom)];

panel.Get<ButtonWidget>("BACK_BUTTON").OnClick = () => { Ui.CloseWindow(); onExit(); };
Expand All @@ -44,12 +51,16 @@ public ServerCreationLogic(Widget widget, ModData modData, Action onExit, Action
{
panel.Get<ButtonWidget>("MAP_BUTTON").OnClick = () =>
{
// Technically unnecesary
// Makes sure that the updated map is already pre-selected in MapChooser
UpdatePreview(preview.Uid);
Ui.OpenWindow("MAPCHOOSER_PANEL", new WidgetArgs()
{
{ "initialMap", preview.Uid },
{ "initialTab", MapClassification.System },
{ "onExit", () => { } },
{ "onSelect", (Action<string>)(uid => preview = modData.MapCache[uid]) },
{ "onExit", () => { UpdatePreview(preview.Uid); } },
{ "onSelect", (Action<string>)UpdatePreview },
{ "filter", MapVisibility.Lobby },
{ "onStart", () => { } }
});
Expand Down Expand Up @@ -147,6 +158,32 @@ public ServerCreationLogic(Widget widget, ModData modData, Action onExit, Action
BuildNotices();
}

bool disposed;
protected override void Dispose(bool disposing)
{
if (disposing && !disposed)
{
disposed = true;
modData.MapCache.MapUpdated -= TrackRelevantMapUpdates;
}

base.Dispose(disposing);
}

void TrackRelevantMapUpdates(string oldUid, string newUid)
{
// We need to handle map being updated multiple times without a refresh
if (preview.Uid == oldUid || oldUid == newMapUid)
{
if (oldMapUid == null)
oldMapUid = oldUid;
newMapUid = newUid;
}

if (newUid != null)
lastUpdatedUid = newUid;
}

void BuildNotices()
{
if (noticesLabelA == null || noticesLabelB == null || noticesLabelC == null)
Expand Down Expand Up @@ -182,8 +219,27 @@ void BuildNotices()
}
}

void UpdatePreview(string uid)
{
if (modData.MapCache[uid].Status != MapStatus.Available)
preview = modData.MapCache[lastUpdatedUid ?? modData.MapCache.ChooseInitialMap(Game.Settings.Server.Map, Game.CosmeticRandom)];
else
preview = modData.MapCache[uid];

newMapUid = null;
oldMapUid = null;
lastUpdatedUid = null;
}

void CreateAndJoin()
{
// Refresh MapCache
if (modData.MapCache[preview.Uid].Status != MapStatus.Available)
{
UpdatePreview(preview.Uid);
return;
}

var name = Game.Settings.SanitizedServerName(panel.Get<TextFieldWidget>("SERVER_NAME").Text);
if (!Exts.TryParseIntegerInvariant(panel.Get<TextFieldWidget>("LISTEN_PORT").Text, out var listenPort))
listenPort = 1234;
Expand Down

0 comments on commit 6a57a50

Please sign in to comment.