diff --git a/source/ps/GameSetup/GameSetup.cpp b/source/ps/GameSetup/GameSetup.cpp index f91201f3d6..70ac83541a 100644 --- a/source/ps/GameSetup/GameSetup.cpp +++ b/source/ps/GameSetup/GameSetup.cpp @@ -934,6 +934,7 @@ bool Autostart(const CmdLineArgs& args) /* * Handle various command-line options, for quick testing of various features: * -autostart=name -- map name for scenario, or rms name for random map + * -autostart-ai=1:dummybot -- adds the dummybot AI to player 1 * -autostart-playername=name -- multiplayer player name * -autostart-host -- multiplayer host mode * -autostart-players=2 -- number of players @@ -1038,18 +1039,17 @@ bool Autostart(const CmdLineArgs& args) // Set player data for AIs // attrs.settings = { PlayerData: [ { AI: ... }, ... ] }: - - /* - * Handle command-line options for AI: - * -autostart-ai=1:dummybot -autostart-ai=2:dummybot -- adds the dummybot AI to players 1 and 2 - */ if (args.Has("autostart-ai")) { std::vector aiArgs = args.GetMultiple("autostart-ai"); for (size_t i = 0; i < aiArgs.size(); ++i) { + // Instead of overwriting existing player data, modify the array CScriptVal player; - scriptInterface.Eval("({})", player); + if (!scriptInterface.GetPropertyInt(playerData.get(), i, player) || player.undefined()) + { + scriptInterface.Eval("({})", player); + } int playerID = aiArgs[i].BeforeFirst(":").ToInt(); CStr name = aiArgs[i].AfterFirst(":"); diff --git a/source/scriptinterface/ScriptInterface.cpp b/source/scriptinterface/ScriptInterface.cpp index 258173c381..b56114c4f8 100644 --- a/source/scriptinterface/ScriptInterface.cpp +++ b/source/scriptinterface/ScriptInterface.cpp @@ -716,6 +716,17 @@ bool ScriptInterface::GetProperty_(jsval obj, const char* name, jsval& out) return true; } +bool ScriptInterface::GetPropertyInt_(jsval obj, int name, jsval& out) +{ + if (! JSVAL_IS_OBJECT(obj)) + return false; + JSObject* object = JSVAL_TO_OBJECT(obj); + + if (!JS_GetPropertyById(m->m_cx, object, INT_TO_JSID(name), &out)) + return false; + return true; +} + bool ScriptInterface::HasProperty(jsval obj, const char* name) { if (! JSVAL_IS_OBJECT(obj)) diff --git a/source/scriptinterface/ScriptInterface.h b/source/scriptinterface/ScriptInterface.h index 6213ec666d..69f7e9dba1 100644 --- a/source/scriptinterface/ScriptInterface.h +++ b/source/scriptinterface/ScriptInterface.h @@ -190,9 +190,21 @@ public: template bool SetPropertyInt(jsval obj, int name, const T& value, bool constant = false, bool enumerate = true); + /** + * Get the named property on the given object. + */ template bool GetProperty(jsval obj, const char* name, T& out); + /** + * Get the integer-named property on the given object. + */ + template + bool GetPropertyInt(jsval obj, int name, T& out); + + /** + * Check the named property has been defined on the given object. + */ bool HasProperty(jsval obj, const char* name); bool EnumeratePropertyNamesWithPrefix(jsval obj, const char* prefix, std::vector& out); @@ -311,6 +323,7 @@ private: bool SetProperty_(jsval obj, const char* name, jsval value, bool readonly, bool enumerate); bool SetPropertyInt_(jsval obj, int name, jsval value, bool readonly, bool enumerate); bool GetProperty_(jsval obj, const char* name, jsval& value); + bool GetPropertyInt_(jsval obj, int name, jsval& value); static bool IsExceptionPending(JSContext* cx); static JSClass* GetClass(JSContext* cx, JSObject* obj); static void* GetPrivate(JSContext* cx, JSObject* obj); @@ -460,6 +473,15 @@ bool ScriptInterface::GetProperty(jsval obj, const char* name, T& out) return FromJSVal(GetContext(), val, out); } +template +bool ScriptInterface::GetPropertyInt(jsval obj, int name, T& out) +{ + jsval val; + if (! GetPropertyInt_(obj, name, val)) + return false; + return FromJSVal(GetContext(), val, out); +} + template bool ScriptInterface::Eval(const CHAR* code, T& ret) {