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

Improve delay between load complete and game starting #21207

Merged
merged 2 commits into from
Nov 12, 2023
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
13 changes: 12 additions & 1 deletion OpenRA.Game/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,10 @@ internal static void StartGame(string mapUID, WorldType type)
// Much better to clean up now then to drop frames during gameplay for GC pauses.
GCSettings.LargeObjectHeapCompactionMode = GCLargeObjectHeapCompactionMode.CompactOnce;
GC.Collect();

// PostLoadComplete is designed for anything that should trigger at the very end of loading.
// e.g. audio notifications that the game is starting.
OrderManager.World.PostLoadComplete(worldRenderer);
}

public static void RestartGame()
Expand Down Expand Up @@ -602,7 +606,10 @@ static void InnerLogicTick(OrderManager orderManager)

if (orderManager.LastTickTime.ShouldAdvance(tick))
{
using (new PerfSample("tick_time"))
if (orderManager.GameStarted && orderManager.LocalFrameNumber == 0)
PerfHistory.Reset(); // Remove history that occurred whilst the new game was loading.

using (var sample = new PerfSample("tick_time"))
{
orderManager.LastTickTime.AdvanceTickTime(tick);

Expand All @@ -611,7 +618,11 @@ static void InnerLogicTick(OrderManager orderManager)
Sync.RunUnsynced(world, orderManager.TickImmediate);

if (world == null)
{
if (orderManager.GameStarted)
PerfHistory.Reset(); // Remove old history when a new game starts.
return;
}

if (orderManager.TryTick())
{
Expand Down
6 changes: 6 additions & 0 deletions OpenRA.Game/Support/PerfHistory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,11 @@ public static void Tick()
if (item.HasNormalTick)
item.Tick();
}

public static void Reset()
{
foreach (var item in Items.Values)
item.ResetSamples();
}
}
}
7 changes: 7 additions & 0 deletions OpenRA.Game/Support/PerfItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,12 @@ public double LastValue
return samples[n];
}
}

public void ResetSamples()
{
head = 1;
tail = 0;
Val = 0.0;
}
}
}
1 change: 1 addition & 0 deletions OpenRA.Game/Traits/TraitsInterfaces.cs
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@ public interface INotifySelected { void Selected(Actor self); }
public interface INotifySelection { void SelectionChanged(); }

public interface IWorldLoaded { void WorldLoaded(World w, WorldRenderer wr); }
public interface IPostWorldLoaded { void PostWorldLoaded(World w, WorldRenderer wr); }
public interface INotifyGameLoading { void GameLoading(World w); }
public interface INotifyGameLoaded { void GameLoaded(World w); }
public interface INotifyGameSaved { void GameSaved(World w); }
Expand Down
15 changes: 14 additions & 1 deletion OpenRA.Game/World.cs
Original file line number Diff line number Diff line change
Expand Up @@ -296,16 +296,29 @@ public void LoadComplete(WorldRenderer wr)
using (new PerfTimer(iwl.GetType().Name + ".WorldLoaded"))
iwl.WorldLoaded(this, wr);

gameInfo.StartTimeUtc = DateTime.UtcNow;
foreach (var player in Players)
gameInfo.AddPlayer(player, OrderManager.LobbyInfo);

gameInfo.DisabledSpawnPoints = OrderManager.LobbyInfo.DisabledSpawnPoints;

gameInfo.StartTimeUtc = DateTime.UtcNow;

if (OrderManager.Connection is NetworkConnection nc && nc.Recorder != null)
nc.Recorder.Metadata = new ReplayMetadata(gameInfo);
}

public void PostLoadComplete(WorldRenderer wr)
{
foreach (var iwl in WorldActor.TraitsImplementing<IPostWorldLoaded>())
using (new PerfTimer(iwl.GetType().Name + ".PostWorldLoaded"))
iwl.PostWorldLoaded(this, wr);

foreach (var p in Players)
foreach (var iwl in p.PlayerActor.TraitsImplementing<IPostWorldLoaded>())
using (new PerfTimer(iwl.GetType().Name + ".PostWorldLoaded"))
iwl.PostWorldLoaded(this, wr);
}

public void SetWorldOwner(Player p)
{
WorldActor.Owner = p;
Expand Down
4 changes: 2 additions & 2 deletions OpenRA.Mods.Common/Traits/World/MusicPlaylist.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class MusicPlaylistInfo : TraitInfo
public override object Create(ActorInitializer init) { return new MusicPlaylist(init.World, this); }
}

public class MusicPlaylist : INotifyActorDisposing, IGameOver, IWorldLoaded, INotifyGameLoaded
public class MusicPlaylist : INotifyActorDisposing, IGameOver, IPostWorldLoaded, INotifyGameLoaded
{
readonly MusicPlaylistInfo info;
readonly World world;
Expand Down Expand Up @@ -98,7 +98,7 @@ public MusicPlaylist(World world, MusicPlaylistInfo info)
}
}

void IWorldLoaded.WorldLoaded(World world, WorldRenderer wr)
void IPostWorldLoaded.PostWorldLoaded(World world, WorldRenderer wr)
{
// Reset any bogus pre-existing state
Game.Sound.DisableWorldSounds = info.DisableWorldSounds;
Expand Down
4 changes: 2 additions & 2 deletions OpenRA.Mods.Common/Traits/World/StartGameNotification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ sealed class StartGameNotificationInfo : TraitInfo
public override object Create(ActorInitializer init) { return new StartGameNotification(this); }
}

sealed class StartGameNotification : IWorldLoaded, INotifyGameLoaded, INotifyGameSaved
sealed class StartGameNotification : IPostWorldLoaded, INotifyGameLoaded, INotifyGameSaved
{
readonly StartGameNotificationInfo info;
public StartGameNotification(StartGameNotificationInfo info)
{
this.info = info;
}

void IWorldLoaded.WorldLoaded(World world, WorldRenderer wr)
void IPostWorldLoaded.PostWorldLoaded(World world, WorldRenderer wr)
{
if (!world.IsLoadingGameSave)
{
Expand Down