From 3bc268aab08cd3e074e9ee83da3763c12457d6ed Mon Sep 17 00:00:00 2001
From: leper <leper@wildfiregames.com>
Date: Mon, 22 Aug 2016 04:52:23 +0200
Subject: [PATCH 4/5] Prevent leaking internal data in cmpPlayer.
---
.../mods/public/simulation/components/CeasefireManager.js | 3 +--
binaries/data/mods/public/simulation/components/Player.js | 4 ++--
.../mods/public/simulation/components/tests/test_Player.js | 11 +++++++++++
3 files changed, 14 insertions(+), 4 deletions(-)
diff --git a/binaries/data/mods/public/simulation/components/CeasefireManager.js b/binaries/data/mods/public/simulation/components/CeasefireManager.js
index 2f2e37f..3418569 100644
a
|
b
|
CeasefireManager.prototype.StartCeasefire = function(ceasefireTime)
|
71 | 71 | // Save diplomacy |
72 | 72 | let numPlayers = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager).GetNumPlayers(); |
73 | 73 | for (let i = 1; i < numPlayers; ++i) |
74 | | // Copy array with slice(), otherwise it will change |
75 | | this.diplomacyBeforeCeasefire.push(QueryPlayerIDInterface(i).GetDiplomacy().slice()); |
| 74 | this.diplomacyBeforeCeasefire.push(QueryPlayerIDInterface(i).GetDiplomacy()); |
76 | 75 | |
77 | 76 | // Set every enemy (except gaia) to neutral |
78 | 77 | for (let i = 1; i < numPlayers; ++i) |
diff --git a/binaries/data/mods/public/simulation/components/Player.js b/binaries/data/mods/public/simulation/components/Player.js
index cb137dd..7959dd3 100644
a
|
b
|
Player.prototype.GetLockTeams = function()
|
460 | 460 | |
461 | 461 | Player.prototype.GetDiplomacy = function() |
462 | 462 | { |
463 | | return this.diplomacy; |
| 463 | return this.diplomacy.slice(); |
464 | 464 | }; |
465 | 465 | |
466 | 466 | Player.prototype.SetDiplomacy = function(dipl) |
467 | 467 | { |
468 | | this.diplomacy = dipl; |
| 468 | this.diplomacy = dipl.slice(); |
469 | 469 | |
470 | 470 | Engine.BroadcastMessage(MT_DiplomacyChanged, { |
471 | 471 | "player": this.playerID, |
diff --git a/binaries/data/mods/public/simulation/components/tests/test_Player.js b/binaries/data/mods/public/simulation/components/tests/test_Player.js
index fbd19e0..f8650c9 100644
a
|
b
|
var cmpPlayer = ConstructComponent(10, "Player");
|
7 | 7 | |
8 | 8 | TS_ASSERT_EQUALS(cmpPlayer.GetPopulationCount(), 0); |
9 | 9 | TS_ASSERT_EQUALS(cmpPlayer.GetPopulationLimit(), 0); |
| 10 | |
| 11 | cmpPlayer.SetDiplomacy([-1, 1, 0, 1, -1]); |
| 12 | |
| 13 | var diplo = cmpPlayer.GetDiplomacy(); |
| 14 | diplo[0] = 1; |
| 15 | TS_ASSERT(cmpPlayer.IsEnemy(0)); |
| 16 | |
| 17 | diplo = [1, 1, 0]; |
| 18 | cmpPlayer.SetDiplomacy(diplo); |
| 19 | diplo[1] = -1; |
| 20 | TS_ASSERT(cmpPlayer.IsAlly(1)); |