Skip to content

Extra bootstrap files #265

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 6 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions client/js/bootstrap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const extraBootstrapFile = __getExtraBootstrapFile();
if (extraBootstrapFile.length !== 0) new Function("alt", "native", extraBootstrapFile)(__alt, __native);
5 changes: 5 additions & 0 deletions client/src/CJavaScriptResource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ bool CJavaScriptResource::Start()
std::vector<uint8_t> fileBuffer = js::ReadFile(altResource->GetPackage(), main);
std::string source{ (char*)fileBuffer.data(), fileBuffer.size() };

const js::Binding& bootstrapper = js::Binding::Get("client/bootstrap.js");
if (!bootstrapper.IsValid()) return false;

CompileAndRun("<bootstrapper>", bootstrapper.GetSource());

// Run it
v8::Local<v8::Module> mod = CompileAndRun(main, source);
if(mod.IsEmpty()) return false;
Expand Down
3 changes: 3 additions & 0 deletions server/js/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ function setup() {
dns.setDefaultResultOrder("ipv4first");

setupImports();

const extraBootstrapFile = __getExtraBootstrapFile();
if (extraBootstrapFile.length !== 0) new Function("alt", extraBootstrapFile)(alt);
}

// Sets up our custom way of importing alt:V resources
Expand Down
5 changes: 5 additions & 0 deletions server/src/CNodeResource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ bool CNodeResource::Start()
js::TemporaryGlobalExtension altModuleExtension(_context, "__altModule", js::Module::Get("@altv/server").GetNamespace(this));
js::TemporaryGlobalExtension cppBindingsExtension(_context, "__cppBindings", js::Module::Get("cppBindings").GetNamespace(this));
js::TemporaryGlobalExtension altServerModuleExtension(_context, "__resourceStarted", ResourceStarted);

// TODO: fix this in shared
js::TemporaryGlobalExtension getExtraBootstrapExtension(_context, "__getExtraBootstrapFile", js::GetExtraBootstrapFile);
js::TemporaryGlobalExtension registerExtraBootstrapExtension(_context, "__registerExtraBootstrapFile", js::RegisterExtraBootstrapFile);

node::LoadEnvironment(env, bootstrapper.GetSource());

asyncResource.Reset(isolate, v8::Object::New(isolate));
Expand Down
58 changes: 58 additions & 0 deletions shared/src/interfaces/IResource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,57 @@ void js::IResource::RequireBindingNamespaceWrapper(js::FunctionContext& ctx)
ctx.Return(bindingModule->GetModuleNamespace());
}

void js::RegisterExtraBootstrapFile(js::FunctionContext& ctx)
{
if (!ctx.CheckArgCount(1)) return;

std::string filePath;
if (!ctx.GetArg(0, filePath)) return;

IResource* resource = ctx.GetResource();

auto path = alt::ICore::Instance().Resolve(resource->GetResource(), filePath, "");
if (!ctx.Check(path.pkg, "Invalid file")) return;

alt::IPackage::File* file = path.pkg->OpenFile(path.fileName);
if (!ctx.Check(file, "File does not exist")) return;

std::string data(path.pkg->GetFileSize(file), 0);
path.pkg->ReadFile(file, data.data(), data.size());
path.pkg->CloseFile(file);

js::extraBootstrapFile = data;
}

void js::GetExtraBootstrapFile(js::FunctionContext& ctx)
{
ctx.Return(js::extraBootstrapFile);
}

#ifdef ALT_CLIENT_API
void js::RegisterCefExtraBootstrapFile(FunctionContext& ctx)
{
if (!ctx.CheckArgCount(1)) return;

std::string filePath;
if (!ctx.GetArg(0, filePath)) return;

IResource* resource = ctx.GetResource();

auto path = alt::ICore::Instance().Resolve(resource->GetResource(), filePath, "");
if (!ctx.Check(path.pkg, "Invalid file")) return;

alt::IPackage::File* file = path.pkg->OpenFile(path.fileName);
if (!ctx.Check(file, "File does not exist")) return;

std::string data(path.pkg->GetFileSize(file), 0);
path.pkg->ReadFile(file, data.data(), data.size());
path.pkg->CloseFile(file);

alt::ICore::Instance().InternalAddCefBootstrap(data);
}
#endif

void js::IResource::InitializeBinding(js::Binding* binding)
{
if(binding->IsBootstrapBinding()) return; // Skip bootstrap bindings, those are handled separately
Expand Down Expand Up @@ -56,6 +107,13 @@ void js::IResource::InitializeBindings(Binding::Scope scope, Module& altModule)
TemporaryGlobalExtension altExtension(ctx, "__alt", altModule.GetNamespace(this));
TemporaryGlobalExtension cppBindingsExtension(ctx, "__cppBindings", Module::Get("cppBindings").GetNamespace(this));
TemporaryGlobalExtension requireBindingExtension(ctx, "requireBinding", RequireBindingNamespaceWrapper);
TemporaryGlobalExtension registerExtraBootstrapExtension(ctx, "__registerExtraBootstrapFile", js::RegisterExtraBootstrapFile);
TemporaryGlobalExtension getExtraBootstrapExtension(ctx, "__getExtraBootstrapFile", js::GetExtraBootstrapFile);

#ifdef ALT_CLIENT_API
js::TemporaryGlobalExtension nativeModuleExtension(ctx, "__native", js::Module::Get("@altv/natives").GetNamespace(this));
TemporaryGlobalExtension registerCefExtraBootstrapExtension(ctx, "__registerCefExtraBootstrapFile", js::RegisterCefExtraBootstrapFile);
#endif

for(Binding* binding : bindings) InitializeBinding(binding);
}
Expand Down
11 changes: 9 additions & 2 deletions shared/src/interfaces/IResource.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,20 @@
#include "ICompatibilityHandler.h"
#include "IBindingExportHandler.h"
#include "Event.h"
#include "Logger.h"
#include "helpers/Hash.h"
#include "helpers/ClassInstanceCache.h"
#include "helpers/Buffer.h"

namespace js
{
static std::string extraBootstrapFile;

void RegisterExtraBootstrapFile(FunctionContext& ctx);
void GetExtraBootstrapFile(FunctionContext& ctx);

#ifdef ALT_CLIENT_API
void RegisterCefExtraBootstrapFile(FunctionContext& ctx);
#endif

class IResource : public IScriptObjectHandler, public ICompatibilityHandler, public IBindingExportHandler
{
public:
Expand Down