Merge GuiInterface calls in OnSimulationUpdate

This patch merges the four GuiInterface calls in OnSimulationUpdate in
the GUI into a single one. This makes both sides shorter, is better
in terms of performance and decouples the simulation more from the GUI.
This commit is contained in:
Vantha 2026-08-12 11:14:05 +02:00
parent 15f150341a
commit 705207d1bb
No known key found for this signature in database
GPG key ID: 3F5D02FA4D3E8E74
2 changed files with 37 additions and 44 deletions

View file

@ -662,20 +662,19 @@ function onSimulationUpdate()
g_EntityStates = {};
g_SimState = undefined;
Engine.ProfileStart("sdf");
const info = Engine.GuiInterfaceCall("GetAndClearTemplateChanges");
// Templates change depending on technologies and auras, so they have to be reloaded after such a change.
// g_TechnologyData data never changes, so it shouldn't be deleted.
const players = Engine.GuiInterfaceCall("GetPlayersWithModifiedTemplates");
for (const player of players)
g_TemplateData[player] = {};
if (players.size)
Engine.GuiInterfaceCall("ResetPlayersWithModifiedTemplates");
// g_TechnologyData data never changes, so it shouldn't be cleared.
if (info.playersWithModifiedTemplates)
for (const player of info.playersWithModifiedTemplates)
g_TemplateData[player] = {};
// Some changes may require re-rendering the selection.
if (Engine.GuiInterfaceCall("IsSelectionDirty"))
{
// Some changes require reloading some cached values even if the selection itself didn't change.
if (info.selectionDirty)
g_Selection.onChange();
Engine.GuiInterfaceCall("ResetSelectionDirty");
}
Engine.ProfileStop("sdf");
if (!GetSimState())
return;

View file

@ -37,7 +37,7 @@ GuiInterface.prototype.Init = function()
this.entsWithAuraAndStatusBars = new Set();
this.enabledVisualRangeOverlayTypes = {};
this.playersWithModifiedTemplates = new Set();
this.selectionDirty = {};
this.playersWithDirtySelection = new Set();
this.obstructionSnap = new ObstructionSnap();
};
@ -707,53 +707,50 @@ GuiInterface.prototype.GetNeededResources = function(player, data)
return cmpPlayer ? cmpPlayer.GetNeededResources(data.cost) : {};
};
/**
* State of the templateData (player dependent): true when some template values have been modified
* and need to be reloaded by the gui.
*/
GuiInterface.prototype.OnTemplateModification = function(msg)
{
// The GUI caches templates, which could become out-of-date now, so tell it
// to reload all of them for that player.
// TODO: Allow the GUI to do that more selectively by passing which values
// of which components have changed and letting it only reload the affected
// ones.
this.playersWithModifiedTemplates.add(msg.player);
this.selectionDirty[msg.player] = true;
this.playersWithDirtySelection.add(msg.player);
};
GuiInterface.prototype.GetPlayersWithModifiedTemplates = function(player)
{
return this.playersWithModifiedTemplates;
};
GuiInterface.prototype.ResetPlayersWithModifiedTemplates = function()
{
this.playersWithModifiedTemplates.clear();
};
/**
* Some changes may require an update to the selection panel,
* which is cached for efficiency. Inform the GUI it needs reloading.
*/
GuiInterface.prototype.OnDisabledTemplatesChanged = function(msg)
{
this.selectionDirty[msg.player] = true;
this.playersWithDirtySelection.add(msg.player);
};
GuiInterface.prototype.OnDisabledTechnologiesChanged = function(msg)
{
this.selectionDirty[msg.player] = true;
this.playersWithDirtySelection.add(msg.player);
};
/**
* Some values about the selection (trainable entities for example) are cached
* by the GUI. Inform the GUI that it needs to reload them.
*/
GuiInterface.prototype.SetSelectionDirty = function(player)
{
this.selectionDirty[player] = true;
this.playersWithDirtySelection.add(player);
};
GuiInterface.prototype.IsSelectionDirty = function(player)
GuiInterface.prototype.GetAndClearTemplateChanges = function(player)
{
return this.selectionDirty[player] || false;
};
const ret = {};
if (this.playersWithModifiedTemplates.size)
ret.playersWithModifiedTemplates = new Set(this.playersWithModifiedTemplates);
GuiInterface.prototype.ResetSelectionDirty = function()
{
this.selectionDirty = {};
if (this.playersWithDirtySelection.has(player))
ret.selectionDirty = true;
this.playersWithModifiedTemplates.clear();
this.playersWithDirtySelection.clear();
return ret;
};
/**
@ -2132,10 +2129,7 @@ GuiInterface.prototype.exposedFunctions = {
"GetTraderNumber": 1,
"GetTradingGoods": 1,
"GetPlayersWithModifiedTemplates": 1,
"ResetPlayersWithModifiedTemplates": 1,
"IsSelectionDirty": 1,
"ResetSelectionDirty": 1
"GetAndClearTemplateChanges": 1
};
GuiInterface.prototype.ScriptCall = function(player, name, args)