Page MenuHomeWildfire Games

Remove unnecessary argument from CSimulation2::InitGame
ClosedPublic

Authored by s0600204 on Feb 8 2018, 9:13 PM.

Details

Summary

As mentioned in D1276, CSimulation2's InitGame function requires an argument that contains data that it could fetch itself (as the content of the argument is stored in an instance of CSimulation2Impl that is a member of CSimulation2).

As it does not really make sense to request data only to pass it back to a function of the class it came from unchanged, this revision removes that need, changing the function to get the data it needs internally.


Looking into the commit history, the InitGame function first came into existence (in both c++ and js) in rP7281. When called by CGame, the argument that was passed was essentially empty, having been created the line immediately before the call.

Almost a year later in rP8865, a member object (m_RegisteredAttribs) was added to CGame that could then be used to store the attributes that would be passed to InitGame. (The call was also relocated from CGame's constructor to the current location inside CGame::ReallyStartGame())

A further nine months, and this attribute-holding object was relocated to CSimulation2Impl (and renamed to m_InitAttributes) in rP10426. Functions to get and set this object via CSimulation2 were added at the same time.

Subsequent changes (rP10787, rP15568, rP16214) have maintained the status quo across support for save games, stack rooting, and a SpiderMonkey update respectively.

Test Plan

Check code sanity.

Apply patch and Compile. Confirm it compiles.

Confirm that both starting a new game and beginning a replay of an old one do not throw errors, and that things are initialised correctly.

  • Tip: if they are not, then the StatisticsTracker will complain about an unset key in one of its member objects.
  • Save games are handled differently and do not call InitGame.

Confirm that Atlas starts without error.

Diff Detail

Repository
rP 0 A.D. Public Repository
Lint
Automatic diff as part of commit; lint not applicable.
Unit
Automatic diff as part of commit; unit tests not applicable.

Event Timeline

s0600204 created this revision.Feb 8 2018, 9:13 PM
Owners added a subscriber: Restricted Owners Package.Feb 8 2018, 9:13 PM
Vulcan added a subscriber: Vulcan.Feb 8 2018, 11:29 PM
Executing section Default...
Executing section Source...
Executing section JS...

Successful build - Chance fights ever on the side of the prudent.

Updating workspaces...
Build (release)...
Build (debug)...
Running release tests...
Running cxxtest tests (309 tests).....................................................................................................................................................................................................................................................................................................................OK!
Running debug tests...
Running cxxtest tests (309 tests).....................................................................................................................................................................................................................................................................................................................OK!
Checking XML files...
Itms added a reviewer: Itms.Feb 9 2018, 10:12 AM
elexis added a subscriber: elexis.Feb 9 2018, 10:23 AM
elexis added inline comments.
source/ps/Game.cpp
213 ↗(On Diff #5719)

(Given that SetInitAttributes makes one part of the code run with one kind of attributes and the JS InitGame function is called with the other variable, that sounds like an invitation to bugs.)

Itms added a comment.Feb 9 2018, 12:27 PM

This patch looks very good, and it works well (tested with a few non-default game settings). I'd like to make sure I'm not wrong about what elexis said before accepting it, but I believe it does the right thing.

source/ps/Game.cpp
213 ↗(On Diff #5719)

I don't see why, on the contrary, what we currently do is against the principles of encapsulation: we set some internal data with SetInitAttributes, then we query it out to pass it back to the object itself (!?). It makes much more sense and it is far less error-prone to set some internal data, then let the object use that data to perform whatever we ask it to perform though public methods.

But maybe I missed your point, do you have a specific idea what kind of bug this could cause?

elexis added inline comments.Feb 9 2018, 12:36 PM
source/ps/Game.cpp
213 ↗(On Diff #5719)

Our code assumes the same gamesettings in all places, so the possibility of calling `InitGame and SetInitAttributes with different settings would be wrong by definition, thus removing that is not only removing unnecessary code but fixing a software architecture flaw I'd say. (It's an argument for the patch being even more important than claimed)

Itms accepted this revision as: Itms.Feb 9 2018, 12:47 PM
Itms removed a reviewer: Restricted Owners Package.
Itms added inline comments.
source/ps/Game.cpp
213 ↗(On Diff #5719)

Ah! Then we totally agree, this is a very good patch ? Sorry for the misunderstanding.

This revision is now accepted and ready to land.Feb 9 2018, 12:47 PM
This revision was automatically updated to reflect the committed changes.