Ticket #3409: t3409_getPlayerGUID_v1.patch

File t3409_getPlayerGUID_v1.patch, 4.5 KB (added by elexis, 9 years ago)

This patch introduces the Engine function GetPlayerGUID, which returns the guid of the current player. One of the cleaner ways to check if a chat message was sent by a different player and to find out the name of the local player. Required for #3270 too.

  • source/gui/scripting/ScriptFunctions.cpp

     
    191191    if (g_Game)
    192192        return g_Game->GetPlayerID();
    193193    return -1;
    194194}
    195195
     196CStr GetPlayerGUID(ScriptInterface::CxPrivate* UNUSED(pCxPrivate))
     197{
     198    if (g_NetServer)
     199        return g_NetServer->GetHostGUID();
     200    else if (g_NetClient)
     201        return g_NetClient->GetGUID();
     202
     203    return "";
     204}
     205
    196206void SetPlayerID(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), int id)
    197207{
    198208    if (g_Game)
    199209        g_Game->SetPlayerID(id);
    200210}
     
    963973    scriptInterface.RegisterFunction<void, &DisconnectNetworkGame>("DisconnectNetworkGame");
    964974    scriptInterface.RegisterFunction<JS::Value, &PollNetworkClient>("PollNetworkClient");
    965975    scriptInterface.RegisterFunction<void, JS::HandleValue, &SetNetworkGameAttributes>("SetNetworkGameAttributes");
    966976    scriptInterface.RegisterFunction<void, int, std::string, &AssignNetworkPlayer>("AssignNetworkPlayer");
    967977    scriptInterface.RegisterFunction<void, std::string, int, &SetNetworkPlayerStatus>("SetNetworkPlayerStatus");
     978    scriptInterface.RegisterFunction<CStr, &GetPlayerGUID>("GetPlayerGUID");
    968979    scriptInterface.RegisterFunction<void, &ClearAllPlayerReady>("ClearAllPlayerReady");
    969980    scriptInterface.RegisterFunction<void, std::wstring, &SendNetworkChat>("SendNetworkChat");
    970981    scriptInterface.RegisterFunction<void, int, &SendNetworkReady>("SendNetworkReady");
    971982    scriptInterface.RegisterFunction<void, &SendNetworkRejoined>("SendNetworkRejoined");
    972983    scriptInterface.RegisterFunction<JS::Value, &GetAIs>("GetAIs");
  • source/network/NetClient.h

     
    8787     * This must not be called after the connection setup.
    8888     */
    8989    void SetUserName(const CStrW& username);
    9090
    9191    /**
     92     * Returns the GUID of the client.
     93     */
     94    CStr GetGUID() const { return m_GUID; }
     95
     96    /**
    9297     * Set up a connection to the remote networked server.
    9398     * @param server IP address or host name to connect to
    9499     * @return true on success, false on connection failure
    95100     */
    96101    bool SetupConnection(const CStr& server);
  • source/network/NetServer.cpp

     
    615615
    616616void CNetServerWorker::OnUserJoin(CNetServerSession* session)
    617617{
    618618    AddPlayer(session->GetGUID(), session->GetUserName());
    619619
     620    if (m_HostGUID.empty())
     621        m_HostGUID = session->GetGUID();
     622
    620623    CGameSetupMessage gameSetupMessage(GetScriptInterface());
    621624    gameSetupMessage.m_Data = m_GameAttributes.get();
    622625    session->SendMessage(&gameSetupMessage);
    623626
    624627    CPlayerAssignmentMessage assignMessage;
     
    11591162{
    11601163    CScopeLock lock(m_Worker->m_WorkerMutex);
    11611164    m_Worker->m_PlayerReadyQueue.emplace_back(guid, ready);
    11621165}
    11631166
     1167CStr CNetServer::GetHostGUID()
     1168{
     1169    CScopeLock lock(m_Worker->m_WorkerMutex);
     1170    return m_Worker->GetHostGUID();
     1171}
     1172
    11641173void CNetServer::ClearAllPlayerReady()
    11651174{
    11661175    CScopeLock lock(m_Worker->m_WorkerMutex);
    11671176    m_Worker->m_PlayerResetReadyQueue.push_back(false);
    11681177}
  • source/network/NetServer.h

     
    132132    /**
    133133     * Call from the GUI to set the all player readiness to 0.
    134134     * The changes will be asynchronously propagated to all clients.
    135135     */
    136136    void ClearAllPlayerReady();
    137    
     137
     138    /**
     139     * Returns the GUID of the host.
     140    */
     141    CStr GetHostGUID();
     142
    138143    /**
    139144     * Call from the GUI to asynchronously notify all clients that they should start loading the game.
    140145     */
    141146    void StartGame();
    142147
     
    186191     * Send a message to all clients who have completed the full connection process
    187192     * (i.e. are in the pre-game or in-game states).
    188193     */
    189194    bool Broadcast(const CNetMessage* message);
    190195
     196    CStr GetHostGUID() const { return m_HostGUID; }
     197
    191198private:
    192199    friend class CNetServer;
    193200    friend class CNetFileReceiveTask_ServerRejoin;
    194201
    195202    CNetServerWorker(int autostartPlayers);
     
    301308
    302309    u32 m_NextHostID;
    303310
    304311    CNetServerTurnManager* m_ServerTurnManager;
    305312
     313    CStr m_HostGUID;
     314
    306315    /**
    307316     * A copy of all simulation commands received so far, indexed by
    308317     * turn number, to simplify support for rejoining etc.
    309318     * TODO: verify this doesn't use too much RAM.
    310319     */