Skip to content

Commit

Permalink
Merge branch 'feature/resource' to master
Browse files Browse the repository at this point in the history
  • Loading branch information
AmyrAhmady committed Apr 20, 2020
2 parents 506e87c + f306bc9 commit 8e2a7d1
Show file tree
Hide file tree
Showing 14 changed files with 363 additions and 304 deletions.
4 changes: 2 additions & 2 deletions src/callbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#include "v8.h"
#include "logger.hpp"
#include "node.hpp"
#include "resource.hpp"
#include "events.hpp"
#include "callbacks.hpp"
#include "amx/amx.h"
Expand Down Expand Up @@ -94,7 +94,7 @@ namespace sampnode
{
for (auto& callback : sampCallbacks)
{
v8val::add_definition(callback.alias, callback.name, global);
sampnode::v8val::add_definition(callback.alias, callback.name, global);
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ namespace sampnode

if (!jsonFile.good())
{
L_INFO << "unable to locate JSON config file at " << path + ".json, trying YAML now";
return false;
}

Expand Down Expand Up @@ -82,10 +83,10 @@ namespace sampnode
{
return Props_t{
get_as<std::string>("entry_file"),
get_as<std::string>("workspace_path"),
get_as<bool>("enable_resources"),
get_as<std::string>("resources_path"),
get_as<std::vector<std::string>>("resources"),
get_as<std::vector<std::string>>("node_flags"),
get_as<std::vector<std::string>>("resources"),
static_cast<LogLevel>(get_as<int>("log_level"))
};
}
Expand Down
2 changes: 1 addition & 1 deletion src/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace sampnode
struct Props_t
{
std::string entry_file;
std::string workspace_path;
bool enable_resources;
std::string resources_path;
std::vector<std::string> node_flags;
std::vector<std::string> resources;
Expand Down
9 changes: 5 additions & 4 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "callbacks.hpp"
#include "events.hpp"
#include "amxhandler.hpp"
#include "node.hpp"
#include "nodeimpl.hpp"
#include <sampgdk.h>
#include "common.hpp"
#include "config.hpp"
Expand All @@ -26,7 +26,7 @@ PLUGIN_EXPORT bool PLUGIN_CALL OnPublicCall(AMX* amx, const char* name, cell* pa
PLUGIN_EXPORT void PLUGIN_CALL ProcessTick()
{
sampgdk::ProcessTick();
sampnode::node_tick();
sampnode::nodeImpl.Tick();
return;
}

Expand Down Expand Up @@ -55,7 +55,8 @@ PLUGIN_EXPORT bool PLUGIN_CALL Load(void** ppData)

sampgdk::Load(ppData);
sampnode::callback::init();
sampnode::node_init(mainConfigData);
sampnode::nodeImpl.Initialize(mainConfigData);
sampnode::nodeImpl.LoadAllResources(mainConfigData.resources, mainConfigData.enable_resources);
return true;
}

Expand All @@ -68,7 +69,7 @@ PLUGIN_EXPORT int PLUGIN_CALL AmxLoad(AMX* amx)
PLUGIN_EXPORT void PLUGIN_CALL Unload()
{
sampgdk::Unload();
sampnode::node_stop();
sampnode::nodeImpl.Stop();
return;
}

Expand Down
1 change: 0 additions & 1 deletion src/natives.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include "amx/amx2.h"
#include "common.hpp"
#include "v8.h"
#include "node.hpp"
#include "natives.hpp"
#include "sampgdk.h"

Expand Down
30 changes: 0 additions & 30 deletions src/node.hpp

This file was deleted.

120 changes: 120 additions & 0 deletions src/nodeimpl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
#include <cstring>
#include <unordered_map>
#include "config.hpp"
#include "resource.hpp"
#include "nodeimpl.hpp"

class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator
{
public:
virtual void* Allocate(size_t length) override
{
void* data = AllocateUninitialized(length);
return data == NULL ? data : memset(data, 0, length);
}
virtual void* AllocateUninitialized(size_t length) override { return malloc(length); }
virtual void Free(void* data, size_t) override { free(data); }
};

namespace sampnode
{
NodeImpl nodeImpl;
std::unordered_map<node::Environment*, std::shared_ptr<Resource>> NodeImpl::resourcesPool;

void NodeImpl::LoadAllResources(const std::vector<std::string>& resources, bool enable_resources)
{
if (enable_resources)
{
for (auto& resource : resources)
{
nodeImpl.LoadResource(resource);
}
}
else
{
nodeImpl.LoadResource("");
}
}

NodeImpl::NodeImpl()
{}

NodeImpl::~NodeImpl()
{}

void NodeImpl::Initialize(const Props_t& config)
{
mainConfig = config;
auto platform = node::InitializeV8Platform(4);
v8Platform = std::unique_ptr<v8::Platform>(platform);

const char* flags = "--expose_gc";
v8::V8::SetFlagsFromString(flags, strlen(flags));

v8::V8::Initialize();

arrayBufferAllocator = std::make_unique<ArrayBufferAllocator>();

v8::Isolate::CreateParams params;
params.array_buffer_allocator = arrayBufferAllocator.get();

v8Isolate = v8::Isolate::New(params);
v8Isolate->SetFatalErrorHandler([](const char* location, const char* message)
{
exit(0);
});

v8Isolate->SetCaptureStackTraceForUncaughtExceptions(true);

//v8::Locker locker(m_isolate);
v8::Isolate::Scope isolateScope(v8Isolate);
v8::HandleScope handle_scope(v8Isolate);

int eac;
const char** eav;

std::vector<const char*> args{
"",
"--expose-internals"
};

int argc = args.size();

node::Init(&argc, args.data(), &eac, &eav);
nodeLoop = std::make_unique<UvLoop>("mainNode");
nodeData = node::CreateIsolateData(v8Isolate, nodeLoop->GetLoop(), platform, (node::ArrayBufferAllocator*)arrayBufferAllocator.get());
}

bool NodeImpl::LoadResource(const std::string& name)
{
if (mainConfig.enable_resources)
{
std::shared_ptr<Resource> resource = std::make_shared<Resource>(name, "./" + mainConfig.resources_path + "/" + name);
resource->Init();
resourcesPool.insert({ resource->GetEnv(), resource });
resourceNamesPool.insert({ name, resource->GetEnv() });
return true;
}
else
{
std::shared_ptr<Resource> resource = std::make_shared<Resource>("main", "no_resource");
resource->Init();
resourcesPool.insert({ resource->GetEnv(), resource });
resourceNamesPool.insert({ name, resource->GetEnv() });
return true;
}
}

bool NodeImpl::UnloadResource(const std::string& name)
{
node::Environment* nodeEnv = resourceNamesPool[name];
resourcesPool.erase(nodeEnv);
resourceNamesPool.erase(name);
return true;
}

bool NodeImpl::ReloadResource(const std::string& name)
{
return true;
}
}
80 changes: 80 additions & 0 deletions src/nodeimpl.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#pragma once
#include <unordered_map>
#include "node.h"
#include "node_api.h"
#include "env.h"
#include "env-inl.h"
#include "v8.h"
#include "uv.h"
#include "libplatform/libplatform.h"
#include "config.hpp"
#include "resource.hpp"
#include "uvloop.hpp"

namespace sampnode
{
class NodeImpl
{
public:
static std::unordered_map<node::Environment*, std::shared_ptr<Resource>> resourcesPool;

static void LoadAllResources(const std::vector<std::string>& resources, bool enable_resources = true);

NodeImpl();
~NodeImpl();

void Initialize(const Props_t& config);
bool LoadResource(const std::string& name);
bool UnloadResource(const std::string& name);
bool ReloadResource(const std::string& name);

inline v8::Platform* GetPlatform()
{
return v8Platform.get();
}

inline v8::Isolate* GetIsolate()
{
return v8Isolate;
}

inline node::IsolateData* GetNodeIsolate()
{
return nodeData;
}

inline UvLoop* GetUVLoop()
{
return nodeLoop.get();
}

inline Props_t& GetMainConfig()
{
return mainConfig;
}

inline void Tick()
{
uv_run(nodeLoop->GetLoop(), UV_RUN_NOWAIT);
}

inline void Stop()
{
for (auto& resource : resourceNamesPool)
{
UnloadResource(resource.first);
}
}

private:
v8::Isolate* v8Isolate;
node::IsolateData* nodeData;
std::unique_ptr<v8::Platform> v8Platform;
std::unique_ptr<v8::ArrayBuffer::Allocator> arrayBufferAllocator;
std::unique_ptr<UvLoop> nodeLoop;
Props_t mainConfig;
std::unordered_map<std::string, node::Environment*> resourceNamesPool;
};

extern NodeImpl nodeImpl;
}
Loading

0 comments on commit 8e2a7d1

Please sign in to comment.