Ticket #3806: 3806_player_gamesetup_v2.4_rebased.patch
File 3806_player_gamesetup_v2.4_rebased.patch, 11.4 KB (added by , 8 years ago) |
---|
-
source/gui/scripting/ScriptFunctions.cpp
328 328 LOGERROR("Failed to save game"); 329 329 } 330 330 331 void NetworkChangeSetting(ScriptInterface::CxPrivate* pCxPrivate, JS::HandleValue keyValuePair1) 332 { 333 ENSURE(g_NetClient); 334 //TODO: This is a workaround because we need to pass a MutableHandle to a JSAPI functions somewhere 335 // (with no obvious reason). 336 JSContext* cx = pCxPrivate->pScriptInterface->GetContext(); 337 JSAutoRequest rq(cx); 338 JS::RootedValue keyValuePair(cx, keyValuePair1); 339 340 g_NetClient->SendChangeSettingMessage(&keyValuePair, *(pCxPrivate->pScriptInterface)); 341 } 342 331 343 void SetNetworkGameAttributes(ScriptInterface::CxPrivate* pCxPrivate, JS::HandleValue attribs1) 332 344 { 333 345 ENSURE(g_NetClient); … … 1033 1045 scriptInterface.RegisterFunction<std::string, &GetPlayerGUID>("GetPlayerGUID"); 1034 1046 scriptInterface.RegisterFunction<void, CStrW, bool, &KickPlayer>("KickPlayer"); 1035 1047 scriptInterface.RegisterFunction<JS::Value, &PollNetworkClient>("PollNetworkClient"); 1048 scriptInterface.RegisterFunction<void, JS::HandleValue, &NetworkChangeSetting>("NetworkChangeSetting"); 1036 1049 scriptInterface.RegisterFunction<void, JS::HandleValue, &SetNetworkGameAttributes>("SetNetworkGameAttributes"); 1037 1050 scriptInterface.RegisterFunction<void, int, std::string, &AssignNetworkPlayer>("AssignNetworkPlayer"); 1038 1051 scriptInterface.RegisterFunction<void, &ClearAllPlayerReady>("ClearAllPlayerReady"); -
source/network/NetClient.cpp
96 96 AddTransition(NCS_PREGAME, (uint)NMT_READY, NCS_PREGAME, (void*)&OnReady, context); 97 97 AddTransition(NCS_PREGAME, (uint)NMT_GAME_SETUP, NCS_PREGAME, (void*)&OnGameSetup, context); 98 98 AddTransition(NCS_PREGAME, (uint)NMT_PLAYER_ASSIGNMENT, NCS_PREGAME, (void*)&OnPlayerAssignment, context); 99 AddTransition(NCS_PREGAME, (uint)NMT_CHANGE_SETTING, NCS_PREGAME, (void*)&OnChangeSetting, context); 99 100 AddTransition(NCS_PREGAME, (uint)NMT_KICKED, NCS_PREGAME, (void*)&OnKicked, context); 100 101 AddTransition(NCS_PREGAME, (uint)NMT_CLIENT_TIMEOUT, NCS_PREGAME, (void*)&OnClientTimeout, context); 101 102 AddTransition(NCS_PREGAME, (uint)NMT_CLIENT_PERFORMANCE, NCS_PREGAME, (void*)&OnClientPerformance, context); … … 338 339 SendMessage(&assignPlayer); 339 340 } 340 341 342 void CNetClient::SendChangeSettingMessage(JS::MutableHandleValue keyValuePair, ScriptInterface& scriptInterface) 343 { 344 JSContext* cx = scriptInterface.GetContext(); 345 JS::RootedValue keyValuePairRooted(cx); 346 keyValuePairRooted = keyValuePair; 347 348 CChangeSettingMessage changeSetting(GetScriptInterface()); 349 changeSetting.m_Data.set(keyValuePairRooted); 350 SendMessage(&changeSetting); 351 } 352 341 353 void CNetClient::SendChatMessage(const std::wstring& text) 342 354 { 343 355 CChatMessage chat; … … 627 639 return true; 628 640 } 629 641 642 bool CNetClient::OnChangeSetting(void* context, CFsmEvent* event) 643 { 644 ENSURE(event->GetType() == (uint)NMT_CHANGE_SETTING); 645 646 CNetClient* client = (CNetClient*)context; 647 JSContext* cx = client->GetScriptInterface().GetContext(); 648 JSAutoRequest rq(cx); 649 650 CChangeSettingMessage* message = (CChangeSettingMessage*)event->GetParamRef(); 651 652 JS::RootedValue msg(cx); 653 client->GetScriptInterface().Eval("({'type':'changesetting'})", &msg); 654 client->GetScriptInterface().SetProperty(msg, "guid", std::string(message->m_GUID), false); 655 client->GetScriptInterface().SetProperty(msg, "data", message->m_Data, false); 656 client->PushGuiMessage(msg); 657 658 return true; 659 } 660 630 661 bool CNetClient::OnGameStart(void* context, CFsmEvent* event) 631 662 { 632 663 ENSURE(event->GetType() == (uint)NMT_GAME_START); -
source/network/NetClient.h
191 191 192 192 void SendAssignPlayerMessage(const int playerID, const CStr& guid); 193 193 194 void SendChangeSettingMessage(JS::MutableHandleValue keyValuePair, ScriptInterface& scriptInterface); 195 194 196 void SendChatMessage(const std::wstring& text); 195 197 196 198 void SendReadyMessage(const int status); … … 225 227 static bool OnReady(void* context, CFsmEvent* event); 226 228 static bool OnGameSetup(void* context, CFsmEvent* event); 227 229 static bool OnPlayerAssignment(void* context, CFsmEvent* event); 230 static bool OnChangeSetting(void* context, CFsmEvent* event); 228 231 static bool OnInGame(void* context, CFsmEvent* event); 229 232 static bool OnGameStart(void* context, CFsmEvent* event); 230 233 static bool OnJoinSyncStart(void* context, CFsmEvent* event); -
source/network/NetMessage.cpp
103 103 104 104 switch (header.GetType()) 105 105 { 106 case NMT_CHANGE_SETTING: 107 pNewMessage = new CChangeSettingMessage(scriptInterface); 108 break; 109 106 110 case NMT_GAME_SETUP: 107 111 pNewMessage = new CGameSetupMessage(scriptInterface); 108 112 break; -
source/network/NetMessage.h
1 /* Copyright (C) 201 5Wildfire Games.1 /* Copyright (C) 2016 Wildfire Games. 2 2 * This file is part of 0 A.D. 3 3 * 4 4 * 0 A.D. is free software: you can redistribute it and/or modify … … 154 154 ScriptInterface& m_ScriptInterface; 155 155 }; 156 156 157 /** 158 * Special message type to send single setting changes. 159 */ 160 class CChangeSettingMessage : public CNetMessage 161 { 162 NONCOPYABLE(CChangeSettingMessage); 163 public: 164 CChangeSettingMessage(ScriptInterface& scriptInterface); 165 CChangeSettingMessage(ScriptInterface& scriptInterface, CStr& guid, JS::HandleValue data); 166 virtual u8* Serialize(u8* pBuffer) const; 167 virtual const u8* Deserialize(const u8* pStart, const u8* pEnd); 168 virtual size_t GetSerializedLength() const; 169 virtual CStr ToString() const; 170 171 CStr m_GUID; 172 JS::PersistentRootedValue m_Data; 173 private: 174 ScriptInterface& m_ScriptInterface; 175 }; 176 157 177 // This time, the classes are created 158 178 #include "NetMessages.h" 159 179 -
source/network/NetMessages.h
53 53 NMT_CLEAR_ALL_READY, 54 54 NMT_GAME_SETUP, 55 55 NMT_ASSIGN_PLAYER, 56 NMT_CHANGE_SETTING, 56 57 NMT_PLAYER_ASSIGNMENT, 57 58 58 59 NMT_FILE_TRANSFER_REQUEST, -
source/network/NetMessageSim.cpp
1 /* Copyright (C) 201 1Wildfire Games.1 /* Copyright (C) 2016 Wildfire Games. 2 2 * This file is part of 0 A.D. 3 3 * 4 4 * 0 A.D. is free software: you can redistribute it and/or modify … … 229 229 stream << "CGameSetupMessage { m_Data: " << source << " }"; 230 230 return CStr(stream.str()); 231 231 } 232 233 234 CChangeSettingMessage::CChangeSettingMessage(ScriptInterface& scriptInterface) : 235 CNetMessage(NMT_CHANGE_SETTING), m_ScriptInterface(scriptInterface), m_Data(scriptInterface.GetJSRuntime()) 236 { 237 } 238 239 CChangeSettingMessage::CChangeSettingMessage(ScriptInterface& scriptInterface, CStr& guid, JS::HandleValue data) : 240 CNetMessage(NMT_CHANGE_SETTING), m_ScriptInterface(scriptInterface), 241 m_GUID(guid), m_Data(scriptInterface.GetJSRuntime(), data) 242 { 243 } 244 245 u8* CChangeSettingMessage::Serialize(u8* pBuffer) const 246 { 247 // TODO: ought to handle serialization exceptions 248 u8* pos = CNetMessage::Serialize(pBuffer); 249 CBufferBinarySerializer serializer(m_ScriptInterface, pos); 250 serializer.StringASCII("guid", m_GUID, 0, UINT32_MAX); 251 serializer.ScriptVal("command", const_cast<JS::PersistentRootedValue*>(&m_Data)); 252 return serializer.GetBuffer(); 253 } 254 255 const u8* CChangeSettingMessage::Deserialize(const u8* pStart, const u8* pEnd) 256 { 257 // TODO: ought to handle serialization exceptions 258 const u8* pos = CNetMessage::Deserialize(pStart, pEnd); 259 std::istringstream stream(std::string(pos, pEnd)); 260 CStdDeserializer deserializer(m_ScriptInterface, stream); 261 deserializer.StringASCII("guid", m_GUID, 0, UINT32_MAX); 262 deserializer.ScriptVal("command", const_cast<JS::PersistentRootedValue*>(&m_Data)); 263 return pEnd; 264 } 265 266 size_t CChangeSettingMessage::GetSerializedLength() const 267 { 268 CLengthBinarySerializer serializer(m_ScriptInterface); 269 serializer.StringASCII("guid", m_GUID, 0, UINT32_MAX); 270 serializer.ScriptVal("command", const_cast<JS::PersistentRootedValue*>(&m_Data)); 271 return CNetMessage::GetSerializedLength() + serializer.GetLength(); 272 } 273 274 CStr CChangeSettingMessage::ToString() const 275 { 276 std::string source = m_ScriptInterface.ToString(const_cast<JS::PersistentRootedValue*>(&m_Data)); 277 278 std::stringstream stream; 279 stream << "CChangeSettingMessage { m_GUID: " << m_GUID << ", m_Data: " << source << " }"; 280 return CStr(stream.str()); 281 } -
source/network/NetServer.cpp
634 634 session->AddTransition(NSS_PREGAME, (uint)NMT_CONNECTION_LOST, NSS_UNCONNECTED, (void*)&OnDisconnect, context); 635 635 session->AddTransition(NSS_PREGAME, (uint)NMT_CHAT, NSS_PREGAME, (void*)&OnChat, context); 636 636 session->AddTransition(NSS_PREGAME, (uint)NMT_READY, NSS_PREGAME, (void*)&OnReady, context); 637 session->AddTransition(NSS_PREGAME, (uint)NMT_CHANGE_SETTING, NSS_PREGAME, (void*)&OnChangeSetting, context); 637 638 session->AddTransition(NSS_PREGAME, (uint)NMT_CLEAR_ALL_READY, NSS_PREGAME, (void*)&OnClearAllReady, context); 638 639 session->AddTransition(NSS_PREGAME, (uint)NMT_GAME_SETUP, NSS_PREGAME, (void*)&OnGameSetup, context); 639 640 session->AddTransition(NSS_PREGAME, (uint)NMT_ASSIGN_PLAYER, NSS_PREGAME, (void*)&OnAssignPlayer, context); … … 1157 1158 return true; 1158 1159 } 1159 1160 1161 bool CNetServerWorker::OnChangeSetting(void* context, CFsmEvent* event) 1162 { 1163 ENSURE(event->GetType() == (uint)NMT_CHANGE_SETTING); 1164 1165 CNetServerSession* session = (CNetServerSession*)context; 1166 CNetServerWorker& server = session->GetServer(); 1167 1168 CChangeSettingMessage* message = (CChangeSettingMessage*)event->GetParamRef(); 1169 message->m_GUID = session->GetGUID(); 1170 // Forward message to host 1171 for (CNetServerSession* session : server.m_Sessions) 1172 if (session->GetGUID() == server.m_HostGUID) 1173 return session->SendMessage(message); 1174 LOGERROR("No host found to send the ChaneSettingMessage to."); 1175 } 1176 1160 1177 bool CNetServerWorker::OnLoadedGame(void* context, CFsmEvent* event) 1161 1178 { 1162 1179 ENSURE(event->GetType() == (uint)NMT_LOADED_GAME); -
source/network/NetServer.h
246 246 static bool OnChat(void* context, CFsmEvent* event); 247 247 static bool OnReady(void* context, CFsmEvent* event); 248 248 static bool OnClearAllReady(void* context, CFsmEvent* event); 249 static bool OnChangeSetting(void* context, CFsmEvent* event); 249 250 static bool OnGameSetup(void* context, CFsmEvent* event); 250 251 static bool OnAssignPlayer(void* context, CFsmEvent* event); 251 252 static bool OnStartGame(void* context, CFsmEvent* event);