Changeset View
Changeset View
Standalone View
Standalone View
source/network/scripting/JSInterface_Network.cpp
Show All 17 Lines | |||||
#include "precompiled.h" | #include "precompiled.h" | ||||
#include "JSInterface_Network.h" | #include "JSInterface_Network.h" | ||||
#include "lib/external_libraries/enet.h" | #include "lib/external_libraries/enet.h" | ||||
#include "lib/external_libraries/libsdl.h" | #include "lib/external_libraries/libsdl.h" | ||||
#include "lib/types.h" | #include "lib/types.h" | ||||
#include "lobby/IXmppClient.h" | #include "lobby/IXmppClient.h" | ||||
#include "network/IPTools.h" | |||||
#include "network/NetClient.h" | #include "network/NetClient.h" | ||||
#include "network/NetMessage.h" | #include "network/NetMessage.h" | ||||
#include "network/NetServer.h" | #include "network/NetServer.h" | ||||
#include "network/StunClient.h" | #include "network/StunClient.h" | ||||
#include "ps/CLogger.h" | #include "ps/CLogger.h" | ||||
#include "ps/Game.h" | #include "ps/Game.h" | ||||
#include "simulation2/system/TurnManager.h" | |||||
#include "scriptinterface/ScriptInterface.h" | #include "scriptinterface/ScriptInterface.h" | ||||
u16 JSI_Network::GetDefaultPort(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) | u16 JSI_Network::GetDefaultPort(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) | ||||
{ | { | ||||
return PS_DEFAULT_PORT; | return PS_DEFAULT_PORT; | ||||
} | } | ||||
bool JSI_Network::HasNetServer(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) | bool JSI_Network::HasNetServer(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) | ||||
▲ Show 20 Lines • Show All 118 Lines • ▼ Show 20 Lines | JS::Value JSI_Network::PollNetworkClient(ScriptInterface::CxPrivate* pCxPrivate) | ||||
// Convert from net client context to GUI script context | // Convert from net client context to GUI script context | ||||
JSContext* cxNet = g_NetClient->GetScriptInterface().GetContext(); | JSContext* cxNet = g_NetClient->GetScriptInterface().GetContext(); | ||||
JSAutoRequest rqNet(cxNet); | JSAutoRequest rqNet(cxNet); | ||||
JS::RootedValue pollNet(cxNet); | JS::RootedValue pollNet(cxNet); | ||||
g_NetClient->GuiPoll(&pollNet); | g_NetClient->GuiPoll(&pollNet); | ||||
return pCxPrivate->pScriptInterface->CloneValueFromOtherContext(g_NetClient->GetScriptInterface(), pollNet); | return pCxPrivate->pScriptInterface->CloneValueFromOtherContext(g_NetClient->GetScriptInterface(), pollNet); | ||||
} | } | ||||
JS::Value JSI_Network::GetNetworkClientPerformance(ScriptInterface::CxPrivate* pCxPrivate) | |||||
{ | |||||
if (!g_NetClient) | |||||
return JS::UndefinedValue(); | |||||
// TODO: Too many questionmarks in the dialog | |||||
elexis: (obsolete TODO) | |||||
// Convert from net client context to GUI script context | |||||
JSContext* cxNet = g_NetClient->GetScriptInterface().GetContext(); | |||||
JSAutoRequest rqNet(cxNet); | |||||
JS::RootedValue pollNet(cxNet, g_NetClient->GetClientPerformance()); | |||||
return pCxPrivate->pScriptInterface->CloneValueFromOtherContext(g_NetClient->GetScriptInterface(), pollNet); | |||||
} | |||||
void JSI_Network::SetNetworkGameAttributes(ScriptInterface::CxPrivate* pCxPrivate, JS::HandleValue attribs1) | void JSI_Network::SetNetworkGameAttributes(ScriptInterface::CxPrivate* pCxPrivate, JS::HandleValue attribs1) | ||||
{ | { | ||||
ENSURE(g_NetClient); | ENSURE(g_NetClient); | ||||
// TODO: This is a workaround because we need to pass a MutableHandle to a JSAPI functions somewhere (with no obvious reason). | // TODO: This is a workaround because we need to pass a MutableHandle to a JSAPI functions somewhere (with no obvious reason). | ||||
JSContext* cx = pCxPrivate->pScriptInterface->GetContext(); | JSContext* cx = pCxPrivate->pScriptInterface->GetContext(); | ||||
JSAutoRequest rq(cx); | JSAutoRequest rq(cx); | ||||
JS::RootedValue attribs(cx, attribs1); | JS::RootedValue attribs(cx, attribs1); | ||||
Show All 37 Lines | |||||
} | } | ||||
void JSI_Network::StartNetworkGame(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) | void JSI_Network::StartNetworkGame(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) | ||||
{ | { | ||||
ENSURE(g_NetClient); | ENSURE(g_NetClient); | ||||
g_NetClient->SendStartGameMessage(); | g_NetClient->SendStartGameMessage(); | ||||
} | } | ||||
u32 JSI_Network::GetTurnLength(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) | |||||
{ | |||||
return DEFAULT_TURN_LENGTH_MP; | |||||
} | |||||
void JSI_Network::SetTurnLength(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), int length) | void JSI_Network::SetTurnLength(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), int length) | ||||
{ | { | ||||
if (g_NetServer) | if (g_NetServer) | ||||
g_NetServer->SetTurnLength(length); | g_NetServer->SetTurnLength(length); | ||||
else | else | ||||
LOGERROR("Only network host can change turn length"); | LOGERROR("Only network host can change turn length"); | ||||
} | } | ||||
std::string JSI_Network::GetClientIPAddress(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const std::string& guid) | |||||
{ | |||||
if (!g_NetServer) | |||||
return std::string(); | |||||
return g_NetServer->GetClientIPAddress(guid); | |||||
} | |||||
std::string JSI_Network::LookupClientHostname(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const std::string& guid) | |||||
{ | |||||
if (!g_NetServer) | |||||
return std::string(); | |||||
return g_NetServer->GetHostname(guid); | |||||
} | |||||
u32 JSI_Network::IPv4ToNumber(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const std::string& ipAddress) | |||||
{ | |||||
u32 ipAddressNum = 0; | |||||
if (!IPTools::ParseIPv4Address(ipAddress, ipAddressNum)) | |||||
return 0; | |||||
return ipAddressNum; | |||||
} | |||||
void JSI_Network::RegisterScriptFunctions(const ScriptInterface& scriptInterface) | void JSI_Network::RegisterScriptFunctions(const ScriptInterface& scriptInterface) | ||||
{ | { | ||||
scriptInterface.RegisterFunction<u16, &GetDefaultPort>("GetDefaultPort"); | scriptInterface.RegisterFunction<u16, &GetDefaultPort>("GetDefaultPort"); | ||||
scriptInterface.RegisterFunction<bool, &HasNetServer>("HasNetServer"); | scriptInterface.RegisterFunction<bool, &HasNetServer>("HasNetServer"); | ||||
scriptInterface.RegisterFunction<bool, &HasNetClient>("HasNetClient"); | scriptInterface.RegisterFunction<bool, &HasNetClient>("HasNetClient"); | ||||
scriptInterface.RegisterFunction<JS::Value, int, &FindStunEndpoint>("FindStunEndpoint"); | scriptInterface.RegisterFunction<JS::Value, int, &FindStunEndpoint>("FindStunEndpoint"); | ||||
scriptInterface.RegisterFunction<void, CStrW, u16, CStr, &StartNetworkHost>("StartNetworkHost"); | scriptInterface.RegisterFunction<void, CStrW, u16, CStr, &StartNetworkHost>("StartNetworkHost"); | ||||
scriptInterface.RegisterFunction<void, CStrW, CStr, u16, bool, CStr, &StartNetworkJoin>("StartNetworkJoin"); | scriptInterface.RegisterFunction<void, CStrW, CStr, u16, bool, CStr, &StartNetworkJoin>("StartNetworkJoin"); | ||||
scriptInterface.RegisterFunction<void, &DisconnectNetworkGame>("DisconnectNetworkGame"); | scriptInterface.RegisterFunction<void, &DisconnectNetworkGame>("DisconnectNetworkGame"); | ||||
scriptInterface.RegisterFunction<CStr, &GetPlayerGUID>("GetPlayerGUID"); | scriptInterface.RegisterFunction<CStr, &GetPlayerGUID>("GetPlayerGUID"); | ||||
scriptInterface.RegisterFunction<JS::Value, &PollNetworkClient>("PollNetworkClient"); | scriptInterface.RegisterFunction<JS::Value, &PollNetworkClient>("PollNetworkClient"); | ||||
scriptInterface.RegisterFunction<JS::Value, &GetNetworkClientPerformance>("GetNetworkClientPerformance"); | |||||
scriptInterface.RegisterFunction<void, JS::HandleValue, &SetNetworkGameAttributes>("SetNetworkGameAttributes"); | scriptInterface.RegisterFunction<void, JS::HandleValue, &SetNetworkGameAttributes>("SetNetworkGameAttributes"); | ||||
scriptInterface.RegisterFunction<void, int, CStr, &AssignNetworkPlayer>("AssignNetworkPlayer"); | scriptInterface.RegisterFunction<void, int, CStr, &AssignNetworkPlayer>("AssignNetworkPlayer"); | ||||
scriptInterface.RegisterFunction<void, CStrW, bool, &KickPlayer>("KickPlayer"); | scriptInterface.RegisterFunction<void, CStrW, bool, &KickPlayer>("KickPlayer"); | ||||
scriptInterface.RegisterFunction<void, CStrW, &SendNetworkChat>("SendNetworkChat"); | scriptInterface.RegisterFunction<void, CStrW, &SendNetworkChat>("SendNetworkChat"); | ||||
scriptInterface.RegisterFunction<void, int, &SendNetworkReady>("SendNetworkReady"); | scriptInterface.RegisterFunction<void, int, &SendNetworkReady>("SendNetworkReady"); | ||||
scriptInterface.RegisterFunction<void, &ClearAllPlayerReady>("ClearAllPlayerReady"); | scriptInterface.RegisterFunction<void, &ClearAllPlayerReady>("ClearAllPlayerReady"); | ||||
scriptInterface.RegisterFunction<void, &StartNetworkGame>("StartNetworkGame"); | scriptInterface.RegisterFunction<void, &StartNetworkGame>("StartNetworkGame"); | ||||
scriptInterface.RegisterFunction<u32, &GetTurnLength>("GetTurnLength"); | |||||
scriptInterface.RegisterFunction<void, int, &SetTurnLength>("SetTurnLength"); | scriptInterface.RegisterFunction<void, int, &SetTurnLength>("SetTurnLength"); | ||||
scriptInterface.RegisterFunction<std::string, std::string, &GetClientIPAddress>("GetClientIPAddress"); | |||||
scriptInterface.RegisterFunction<u32, std::string, &IPv4ToNumber>("IPv4ToNumber"); | |||||
scriptInterface.RegisterFunction<std::string, std::string, &LookupClientHostname>("LookupClientHostname"); | |||||
} | } |
Wildfire Games · Phabricator
(obsolete TODO)