Ticket #3806: 3806_player_gamesetup_v2.4_rebased.patch

File 3806_player_gamesetup_v2.4_rebased.patch, 11.4 KB (added by Imarok, 3 years ago)

rebased version of the cpp part (introduces CChangeSettingMessage)

  • source/gui/scripting/ScriptFunctions.cpp

     
    328328        LOGERROR("Failed to save game");
    329329}
    330330
     331void 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
    331343void SetNetworkGameAttributes(ScriptInterface::CxPrivate* pCxPrivate, JS::HandleValue attribs1)
    332344{
    333345    ENSURE(g_NetClient);
     
    10331045    scriptInterface.RegisterFunction<std::string, &GetPlayerGUID>("GetPlayerGUID");
    10341046    scriptInterface.RegisterFunction<void, CStrW, bool, &KickPlayer>("KickPlayer");
    10351047    scriptInterface.RegisterFunction<JS::Value, &PollNetworkClient>("PollNetworkClient");
     1048    scriptInterface.RegisterFunction<void, JS::HandleValue, &NetworkChangeSetting>("NetworkChangeSetting");
    10361049    scriptInterface.RegisterFunction<void, JS::HandleValue, &SetNetworkGameAttributes>("SetNetworkGameAttributes");
    10371050    scriptInterface.RegisterFunction<void, int, std::string, &AssignNetworkPlayer>("AssignNetworkPlayer");
    10381051    scriptInterface.RegisterFunction<void, &ClearAllPlayerReady>("ClearAllPlayerReady");
  • source/network/NetClient.cpp

     
    9696    AddTransition(NCS_PREGAME, (uint)NMT_READY, NCS_PREGAME, (void*)&OnReady, context);
    9797    AddTransition(NCS_PREGAME, (uint)NMT_GAME_SETUP, NCS_PREGAME, (void*)&OnGameSetup, context);
    9898    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);
    99100    AddTransition(NCS_PREGAME, (uint)NMT_KICKED, NCS_PREGAME, (void*)&OnKicked, context);
    100101    AddTransition(NCS_PREGAME, (uint)NMT_CLIENT_TIMEOUT, NCS_PREGAME, (void*)&OnClientTimeout, context);
    101102    AddTransition(NCS_PREGAME, (uint)NMT_CLIENT_PERFORMANCE, NCS_PREGAME, (void*)&OnClientPerformance, context);
     
    338339    SendMessage(&assignPlayer);
    339340}
    340341
     342void 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
    341353void CNetClient::SendChatMessage(const std::wstring& text)
    342354{
    343355    CChatMessage chat;
     
    627639    return true;
    628640}
    629641
     642bool 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
    630661bool CNetClient::OnGameStart(void* context, CFsmEvent* event)
    631662{
    632663    ENSURE(event->GetType() == (uint)NMT_GAME_START);
  • source/network/NetClient.h

     
    191191
    192192    void SendAssignPlayerMessage(const int playerID, const CStr& guid);
    193193
     194    void SendChangeSettingMessage(JS::MutableHandleValue keyValuePair, ScriptInterface& scriptInterface);
     195
    194196    void SendChatMessage(const std::wstring& text);
    195197
    196198    void SendReadyMessage(const int status);
     
    225227    static bool OnReady(void* context, CFsmEvent* event);
    226228    static bool OnGameSetup(void* context, CFsmEvent* event);
    227229    static bool OnPlayerAssignment(void* context, CFsmEvent* event);
     230    static bool OnChangeSetting(void* context, CFsmEvent* event);
    228231    static bool OnInGame(void* context, CFsmEvent* event);
    229232    static bool OnGameStart(void* context, CFsmEvent* event);
    230233    static bool OnJoinSyncStart(void* context, CFsmEvent* event);
  • source/network/NetMessage.cpp

     
    103103
    104104    switch (header.GetType())
    105105    {
     106    case NMT_CHANGE_SETTING:
     107        pNewMessage = new CChangeSettingMessage(scriptInterface);
     108        break;
     109
    106110    case NMT_GAME_SETUP:
    107111        pNewMessage = new CGameSetupMessage(scriptInterface);
    108112        break;
  • source/network/NetMessage.h

     
    1 /* Copyright (C) 2015 Wildfire Games.
     1/* Copyright (C) 2016 Wildfire Games.
    22 * This file is part of 0 A.D.
    33 *
    44 * 0 A.D. is free software: you can redistribute it and/or modify
     
    154154    ScriptInterface& m_ScriptInterface;
    155155};
    156156
     157/**
     158 * Special message type to send single setting changes.
     159 */
     160class CChangeSettingMessage : public CNetMessage
     161{
     162    NONCOPYABLE(CChangeSettingMessage);
     163public:
     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;
     173private:
     174    ScriptInterface& m_ScriptInterface;
     175};
     176
    157177// This time, the classes are created
    158178#include "NetMessages.h"
    159179
  • source/network/NetMessages.h

     
    5353    NMT_CLEAR_ALL_READY,
    5454    NMT_GAME_SETUP,
    5555    NMT_ASSIGN_PLAYER,
     56    NMT_CHANGE_SETTING,
    5657    NMT_PLAYER_ASSIGNMENT,
    5758
    5859    NMT_FILE_TRANSFER_REQUEST,
  • source/network/NetMessageSim.cpp

     
    1 /* Copyright (C) 2011 Wildfire Games.
     1/* Copyright (C) 2016 Wildfire Games.
    22 * This file is part of 0 A.D.
    33 *
    44 * 0 A.D. is free software: you can redistribute it and/or modify
     
    229229    stream << "CGameSetupMessage { m_Data: " << source << " }";
    230230    return CStr(stream.str());
    231231}
     232
     233
     234CChangeSettingMessage::CChangeSettingMessage(ScriptInterface& scriptInterface) :
     235CNetMessage(NMT_CHANGE_SETTING), m_ScriptInterface(scriptInterface), m_Data(scriptInterface.GetJSRuntime())
     236{
     237}
     238
     239CChangeSettingMessage::CChangeSettingMessage(ScriptInterface& scriptInterface, CStr& guid, JS::HandleValue data) :
     240CNetMessage(NMT_CHANGE_SETTING), m_ScriptInterface(scriptInterface),
     241m_GUID(guid), m_Data(scriptInterface.GetJSRuntime(), data)
     242{
     243}
     244
     245u8* 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
     255const 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
     266size_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
     274CStr 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

     
    634634    session->AddTransition(NSS_PREGAME, (uint)NMT_CONNECTION_LOST, NSS_UNCONNECTED, (void*)&OnDisconnect, context);
    635635    session->AddTransition(NSS_PREGAME, (uint)NMT_CHAT, NSS_PREGAME, (void*)&OnChat, context);
    636636    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);
    637638    session->AddTransition(NSS_PREGAME, (uint)NMT_CLEAR_ALL_READY, NSS_PREGAME, (void*)&OnClearAllReady, context);
    638639    session->AddTransition(NSS_PREGAME, (uint)NMT_GAME_SETUP, NSS_PREGAME, (void*)&OnGameSetup, context);
    639640    session->AddTransition(NSS_PREGAME, (uint)NMT_ASSIGN_PLAYER, NSS_PREGAME, (void*)&OnAssignPlayer, context);
     
    11571158    return true;
    11581159}
    11591160
     1161bool 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
    11601177bool CNetServerWorker::OnLoadedGame(void* context, CFsmEvent* event)
    11611178{
    11621179    ENSURE(event->GetType() == (uint)NMT_LOADED_GAME);
  • source/network/NetServer.h

     
    246246    static bool OnChat(void* context, CFsmEvent* event);
    247247    static bool OnReady(void* context, CFsmEvent* event);
    248248    static bool OnClearAllReady(void* context, CFsmEvent* event);
     249    static bool OnChangeSetting(void* context, CFsmEvent* event);
    249250    static bool OnGameSetup(void* context, CFsmEvent* event);
    250251    static bool OnAssignPlayer(void* context, CFsmEvent* event);
    251252    static bool OnStartGame(void* context, CFsmEvent* event);