Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
allow connectTo from conhost natives
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=c6f260f69cf077755fbaa0a3945e801ea3065ce3
  • Loading branch information
cuprum authored and blattersturm committed Apr 17, 2017
1 parent da759bc commit ab84947
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 21 deletions.
3 changes: 2 additions & 1 deletion components/glue/component.json
Expand Up @@ -5,7 +5,8 @@
"fx[2]",
"net",
"nui:core",
"gta:core"
"gta:core",
"conhost"
],
"provides": []
}
54 changes: 34 additions & 20 deletions components/glue/src/ConnectToNative.cpp
Expand Up @@ -11,6 +11,7 @@
#include <strsafe.h>
#include <GlobalEvents.h>
#include <nutsnbolts.h>
#include <ConsoleHost.h>
//New libs needed for saveSettings
#include <fstream>
#include <sstream>
Expand Down Expand Up @@ -110,10 +111,32 @@ inline bool HasDefaultName()
return false;
}

static InitFunction initFunction([] ()
static NetLibrary* netLibrary;

static void ConnectTo(const std::string& hostnameStr)
{
static NetLibrary* netLibrary;
static char hostname[256];

StringCbCopyA(hostname, sizeof(hostname), hostnameStr.c_str());

std::string port = std::string(hostname);
std::string ip = std::string(hostname);
ip = ip.substr(0, ip.find_last_of(":"));
port = port.substr(port.find_last_of(":") + 1);
const char* portnum = port.c_str();

if (port.empty())
{
portnum = "30120";
}

netLibrary->ConnectToServer(ip.c_str(), atoi(portnum));

nui::ExecuteRootScript("citFrames[\"mpMenu\"].contentWindow.postMessage({ type: 'connecting' }, '*');");
}

static InitFunction initFunction([] ()
{
NetLibrary::OnNetLibraryCreate.Connect([] (NetLibrary* lib)
{
netLibrary = lib;
Expand Down Expand Up @@ -148,31 +171,22 @@ static InitFunction initFunction([] ()
});
});

ConHost::OnInvokeNative.Connect([](const char* type, const char* arg)
{
if (!_stricmp(type, "connectTo"))
{
ConnectTo(arg);
}
});

nui::OnInvokeNative.Connect([](const wchar_t* type, const wchar_t* arg)
{
if (!_wcsicmp(type, L"connectTo"))
{
std::wstring hostnameStrW = arg;
std::string hostnameStr(hostnameStrW.begin(), hostnameStrW.end());

static char hostname[256];

StringCbCopyA(hostname, sizeof(hostname), hostnameStr.c_str());

std::string port = std::string(hostname);
std::string ip = std::string(hostname);
ip = ip.substr(0, ip.find_last_of(":"));
port = port.substr(port.find_last_of(":") + 1);
const char* portnum = port.c_str();

if (port.empty())
{
portnum = "30120";
}

netLibrary->ConnectToServer(ip.c_str(), atoi(portnum));

nui::ExecuteRootScript("citFrames[\"mpMenu\"].contentWindow.postMessage({ type: 'connecting' }, '*');");
ConnectTo(hostnameStr);
}
else if (!_wcsicmp(type, L"changeName"))
{
Expand Down

0 comments on commit ab84947

Please sign in to comment.