1
0
forked from mirrors/0ad

Load the PlayerNames dependant on their origin

`PlayerData` didn't contain the playerNames when serialized because they
should not be loaded when a new game is made. This lead to the issue
that client didn't receive the initial player names of saved games.

Now `fromInitAttributes` gets as a second parameter whether the
initAttributus are from persistent settings.

Defect introduced in b90280855f.
This commit is contained in:
phosit
2024-12-28 13:31:36 +01:00
committed by phosit
parent 199aa8ac35
commit b52c1c137f
4 changed files with 10 additions and 16 deletions
@@ -51,7 +51,7 @@ class GameSettings
// Remove the gaia entry.
initAttributes.settings.PlayerData.splice(0, 1);
this.fromInitAttributes(initAttributes);
this.fromInitAttributes(initAttributes, false);
return this;
}
@@ -79,7 +79,7 @@ class GameSettings
* TODO: this could/should maybe support partial deserialization,
* which means MP might actually send only the bits that change.
*/
fromInitAttributes(attribs)
fromInitAttributes(attribs, fromPersistentSettings)
{
// Settings may depend on eachother. Some selections of settings
// might be illegal. So keep looping through all settings until
@@ -100,7 +100,7 @@ class GameSettings
{
const oldSettings = clone(getComponentData(comp));
if (this[comp].fromInitAttributes)
this[comp].fromInitAttributes(attribs);
this[comp].fromInitAttributes(attribs, fromPersistentSettings);
reInit = reInit || !deepCompare(oldSettings, getComponentData(comp));
}
if (!reInit)
@@ -7,8 +7,6 @@
*/
GameSettings.prototype.Attributes.PlayerName = class PlayerName extends GameSetting
{
randomPicked = false;
init()
{
// NB: watchers aren't auto-triggered when modifying array elements.
@@ -23,17 +21,15 @@ GameSettings.prototype.Attributes.PlayerName = class PlayerName extends GameSett
attribs.settings.PlayerData = [];
while (attribs.settings.PlayerData.length < this.values.length)
attribs.settings.PlayerData.push({});
if (this.isSavedGame && !this.randomPicked)
return;
for (let i in this.values)
if (this.values[i])
attribs.settings.PlayerData[i].Name = this.values[i];
}
fromInitAttributes(attribs)
fromInitAttributes(attribs, fromPersistentSettings)
{
if (!this.getLegacySetting(attribs, "PlayerData"))
if (fromPersistentSettings || !this.getLegacySetting(attribs, "PlayerData"))
return;
const pData = this.getLegacySetting(attribs, "PlayerData");
for (let i = 0; i < this.values.length; ++i)
@@ -114,7 +110,6 @@ GameSettings.prototype.Attributes.PlayerName = class PlayerName extends GameSett
}
if (picked)
{
this.randomPicked = true;
this.trigger("values");
}
return picked;
@@ -85,7 +85,7 @@ class GameSettingsController
// Also include hotloaded data because that can also fail and having to restart isn't very useful.
try {
if (hotloadData)
this.parseSettings(hotloadData.initAttributes);
this.parseSettings(hotloadData.initAttributes, false);
else if (g_IsController && (initData?.gameSettings || this.persistentMatchSettings.enabled))
{
// Allow opting-in to persistence when sending initial data (though default off)
@@ -93,7 +93,7 @@ class GameSettingsController
this.persistentMatchSettings.enabled = !!initData.gameSettings?.usePersistence;
const settings = initData?.gameSettings || this.persistentMatchSettings.loadFile();
if (settings)
this.parseSettings(settings);
this.parseSettings(settings, true);
}
} catch(err) {
error("There was an error loading game settings. You may need to disable persistent match settings.");
@@ -177,7 +177,7 @@ class GameSettingsController
this.setLoading(false);
}
this.parseSettings(message.data.initAttribs);
this.parseSettings(message.data.initAttribs, false);
// This assumes that messages aren't sent spuriously without changes
// (which is generally fair), but technically it would be good
@@ -199,11 +199,11 @@ class GameSettingsController
/**
* Parse the following settings.
*/
parseSettings(settings)
parseSettings(settings, fromPersistentSettings)
{
if (settings.guiData)
this.guiData.Deserialize(settings.guiData);
g_GameSettings.fromInitAttributes(settings);
g_GameSettings.fromInitAttributes(settings, fromPersistentSettings);
}
setLoading(loading)
@@ -5,7 +5,6 @@ PlayerSettingControls.PlayerName = class PlayerName extends GameSettingControl
constructor(...args)
{
super(...args);
g_GameSettings.playerName.isSavedGame = this.isSavedGame;
this.playerName = Engine.GetGUIObjectByName("playerName[" + this.playerIndex + "]");
g_GameSettings.playerCount.watch(() => this.render(), ["nbPlayers"]);