From ab18a25be5ae8f953aeec7fdb2a4ade405f299fe Mon Sep 17 00:00:00 2001 From: xshady <54737754+xxshady@users.noreply.github.com> Date: Sat, 2 Nov 2024 23:20:16 +0300 Subject: [PATCH 1/6] feat(shared): starting point for extra bootstrap files thought it would be simple, but it turns out that bootstrappers in v1 module are completely different --- shared/src/interfaces/IResource.cpp | 59 +++++++++++++++++++++++++++++ shared/src/interfaces/IResource.h | 8 ++++ 2 files changed, 67 insertions(+) diff --git a/shared/src/interfaces/IResource.cpp b/shared/src/interfaces/IResource.cpp index 2545ad460..1019e5c2d 100644 --- a/shared/src/interfaces/IResource.cpp +++ b/shared/src/interfaces/IResource.cpp @@ -17,6 +17,59 @@ void js::IResource::RequireBindingNamespaceWrapper(js::FunctionContext& ctx) ctx.Return(bindingModule->GetModuleNamespace()); } +void js::IResource::RegisterExtraBootstrapFile(js::FunctionContext& ctx) +{ + if(!ctx.CheckArgCount(1)) return; + + std::string filePath; + if(!ctx.GetArg(0, filePath)) return; + + IResource* resource = ctx.GetResource(); + if(!resource) return; + + auto path = alt::ICore::Instance().Resolve(resource->GetResource(), filePath, ""); + if (!ctx.Check(path.pkg), "Invalid file"); + + alt::IPackage::File* file = path.pkg->OpenFile(path.fileName); + if (!ctx.Check(file), "File does not exist"); + + std::string data(path.pkg->GetFileSize(file), 0); + path.pkg->ReadFile(file, data.data(), data.size()); + path.pkg->CloseFile(file); + + extraBootstrapFile = data; +} + +void js::IResource::GetExtraBootstrapFile(js::FunctionContext& ctx) +{ + ctx.Return(extraBootstrapFile); +} + +#ifdef ALT_CLIENT_API +static void js::IResource::RegisterCefExtraBootstrapFile(FunctionContext& ctx) +{ + if(!ctx.CheckArgCount(1)) return; + + std::string filePath; + if(!ctx.GetArg(0, filePath)) return; + + IResource* resource = ctx.GetResource(); + if(!resource) return; + + auto path = alt::ICore::Instance().Resolve(resource->GetResource(), filePath, ""); + if (!ctx.Check(path.pkg), "Invalid file"); + + alt::IPackage::File* file = path.pkg->OpenFile(path.fileName); + if (!ctx.Check(file), "File does not exist"); + + 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 @@ -56,6 +109,12 @@ 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", RegisterExtraBootstrapFile); + TemporaryGlobalExtension getExtraBootstrapExtension(ctx, "__getExtraBootstrapFile", GetExtraBootstrapFile); + +#ifdef ALT_CLIENT_API + TemporaryGlobalExtension registerCefExtraBootstrapExtension(ctx, "__registerCefExtraBootstrapFile", RegisterCefExtraBootstrapFile); +#endif for(Binding* binding : bindings) InitializeBinding(binding); } diff --git a/shared/src/interfaces/IResource.h b/shared/src/interfaces/IResource.h index b9534bda6..d00d95a53 100644 --- a/shared/src/interfaces/IResource.h +++ b/shared/src/interfaces/IResource.h @@ -30,6 +30,12 @@ namespace js static constexpr int ContextInternalFieldIdx = 1; static void RequireBindingNamespaceWrapper(FunctionContext& ctx); + static void RegisterExtraBootstrapFile(FunctionContext& ctx); + static void GetExtraBootstrapFile(FunctionContext& ctx); + +#ifdef ALT_CLIENT_API + static void RegisterCefExtraBootstrapFile(FunctionContext& ctx); +#endif v8::Isolate* isolate; Persistent context; @@ -39,6 +45,8 @@ namespace js std::vector promises; + std::string extraBootstrapFile; + void Initialize() { context.Get(isolate)->SetAlignedPointerInEmbedderData(ContextInternalFieldIdx, this); From 9532f65f2686039a681a2246ea042886c4c8b8b9 Mon Sep 17 00:00:00 2001 From: xLuxy <67131061+xLuxy@users.noreply.github.com> Date: Mon, 4 Nov 2024 14:14:11 +0100 Subject: [PATCH 2/6] fix build --- shared/src/interfaces/IResource.cpp | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/shared/src/interfaces/IResource.cpp b/shared/src/interfaces/IResource.cpp index 1019e5c2d..6aaf87764 100644 --- a/shared/src/interfaces/IResource.cpp +++ b/shared/src/interfaces/IResource.cpp @@ -19,48 +19,46 @@ void js::IResource::RequireBindingNamespaceWrapper(js::FunctionContext& ctx) void js::IResource::RegisterExtraBootstrapFile(js::FunctionContext& ctx) { - if(!ctx.CheckArgCount(1)) return; + if (!ctx.CheckArgCount(1)) return; std::string filePath; - if(!ctx.GetArg(0, filePath)) return; + if (!ctx.GetArg(0, filePath)) return; IResource* resource = ctx.GetResource(); - if(!resource) return; auto path = alt::ICore::Instance().Resolve(resource->GetResource(), filePath, ""); - if (!ctx.Check(path.pkg), "Invalid file"); + 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"); + 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); - extraBootstrapFile = data; + resource->extraBootstrapFile = data; } void js::IResource::GetExtraBootstrapFile(js::FunctionContext& ctx) { - ctx.Return(extraBootstrapFile); + ctx.Return(ctx.GetResource()->extraBootstrapFile); } #ifdef ALT_CLIENT_API -static void js::IResource::RegisterCefExtraBootstrapFile(FunctionContext& ctx) +void js::IResource::RegisterCefExtraBootstrapFile(FunctionContext& ctx) { - if(!ctx.CheckArgCount(1)) return; + if (!ctx.CheckArgCount(1)) return; std::string filePath; - if(!ctx.GetArg(0, filePath)) return; + if (!ctx.GetArg(0, filePath)) return; IResource* resource = ctx.GetResource(); - if(!resource) return; auto path = alt::ICore::Instance().Resolve(resource->GetResource(), filePath, ""); - if (!ctx.Check(path.pkg), "Invalid file"); + 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"); + 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()); From 51dedf89e5b03e5d442fadc3b4d68b2e2655defe Mon Sep 17 00:00:00 2001 From: xLuxy <67131061+xLuxy@users.noreply.github.com> Date: Mon, 4 Nov 2024 18:32:33 +0100 Subject: [PATCH 3/6] server: Add bootstrapper file --- server/js/bootstrap.js | 3 +++ shared/src/interfaces/IResource.h | 3 --- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/server/js/bootstrap.js b/server/js/bootstrap.js index 40b3dfc87..a8aed4997 100644 --- a/server/js/bootstrap.js +++ b/server/js/bootstrap.js @@ -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 diff --git a/shared/src/interfaces/IResource.h b/shared/src/interfaces/IResource.h index d00d95a53..71ee89fc2 100644 --- a/shared/src/interfaces/IResource.h +++ b/shared/src/interfaces/IResource.h @@ -13,9 +13,6 @@ #include "IScriptObjectHandler.h" #include "ICompatibilityHandler.h" #include "IBindingExportHandler.h" -#include "Event.h" -#include "Logger.h" -#include "helpers/Hash.h" #include "helpers/ClassInstanceCache.h" #include "helpers/Buffer.h" From bc80b9c34b8dba54b6f4e676d2e4fac110dba89e Mon Sep 17 00:00:00 2001 From: xLuxy <67131061+xLuxy@users.noreply.github.com> Date: Mon, 4 Nov 2024 18:49:17 +0100 Subject: [PATCH 4/6] WIP: add client bootstrapper --- client/js/bootstrap.js | 2 ++ client/src/CJavaScriptResource.cpp | 5 +++++ shared/src/interfaces/IResource.cpp | 1 + 3 files changed, 8 insertions(+) create mode 100644 client/js/bootstrap.js diff --git a/client/js/bootstrap.js b/client/js/bootstrap.js new file mode 100644 index 000000000..846c51161 --- /dev/null +++ b/client/js/bootstrap.js @@ -0,0 +1,2 @@ +const extraBootstrapFile = __getExtraBootstrapFile(); +if (extraBootstrapFile.length !== 0) new Function("alt", "native", extraBootstrapFile)(__alt, __native); diff --git a/client/src/CJavaScriptResource.cpp b/client/src/CJavaScriptResource.cpp index bf000ed09..14c17fcfc 100644 --- a/client/src/CJavaScriptResource.cpp +++ b/client/src/CJavaScriptResource.cpp @@ -96,6 +96,11 @@ bool CJavaScriptResource::Start() std::vector 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.GetSource()); + // Run it v8::Local mod = CompileAndRun(main, source); if(mod.IsEmpty()) return false; diff --git a/shared/src/interfaces/IResource.cpp b/shared/src/interfaces/IResource.cpp index 6aaf87764..126d89a4c 100644 --- a/shared/src/interfaces/IResource.cpp +++ b/shared/src/interfaces/IResource.cpp @@ -111,6 +111,7 @@ void js::IResource::InitializeBindings(Binding::Scope scope, Module& altModule) TemporaryGlobalExtension getExtraBootstrapExtension(ctx, "__getExtraBootstrapFile", GetExtraBootstrapFile); #ifdef ALT_CLIENT_API + js::TemporaryGlobalExtension nativeModuleExtension(ctx, "__native", js::Module::Get("@altv/natives").GetNamespace(this)); TemporaryGlobalExtension registerCefExtraBootstrapExtension(ctx, "__registerCefExtraBootstrapFile", RegisterCefExtraBootstrapFile); #endif From 0949159829e369e1bf2f04a7cc48b5fcbe0bf58c Mon Sep 17 00:00:00 2001 From: xshady <54737754+xxshady@users.noreply.github.com> Date: Tue, 5 Nov 2024 01:57:07 +0300 Subject: [PATCH 5/6] fix(shared): add required include back --- shared/src/interfaces/IResource.h | 1 + 1 file changed, 1 insertion(+) diff --git a/shared/src/interfaces/IResource.h b/shared/src/interfaces/IResource.h index 71ee89fc2..04caa04e1 100644 --- a/shared/src/interfaces/IResource.h +++ b/shared/src/interfaces/IResource.h @@ -13,6 +13,7 @@ #include "IScriptObjectHandler.h" #include "ICompatibilityHandler.h" #include "IBindingExportHandler.h" +#include "Event.h" #include "helpers/ClassInstanceCache.h" #include "helpers/Buffer.h" From 67da2f4199948592ef7fbe60cbd4d9ec16d24500 Mon Sep 17 00:00:00 2001 From: xshady <54737754+xxshady@users.noreply.github.com> Date: Tue, 5 Nov 2024 02:40:23 +0300 Subject: [PATCH 6/6] fix(shared, server): extra bootstrap file added temporary hack + made bootstrap file variable shared for all resources --- server/src/CNodeResource.cpp | 5 +++++ shared/src/interfaces/IResource.cpp | 16 ++++++++-------- shared/src/interfaces/IResource.h | 17 +++++++++-------- 3 files changed, 22 insertions(+), 16 deletions(-) diff --git a/server/src/CNodeResource.cpp b/server/src/CNodeResource.cpp index af7c08edc..88bbf24e9 100644 --- a/server/src/CNodeResource.cpp +++ b/server/src/CNodeResource.cpp @@ -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)); diff --git a/shared/src/interfaces/IResource.cpp b/shared/src/interfaces/IResource.cpp index 126d89a4c..d0a1ab9f2 100644 --- a/shared/src/interfaces/IResource.cpp +++ b/shared/src/interfaces/IResource.cpp @@ -17,7 +17,7 @@ void js::IResource::RequireBindingNamespaceWrapper(js::FunctionContext& ctx) ctx.Return(bindingModule->GetModuleNamespace()); } -void js::IResource::RegisterExtraBootstrapFile(js::FunctionContext& ctx) +void js::RegisterExtraBootstrapFile(js::FunctionContext& ctx) { if (!ctx.CheckArgCount(1)) return; @@ -36,16 +36,16 @@ void js::IResource::RegisterExtraBootstrapFile(js::FunctionContext& ctx) path.pkg->ReadFile(file, data.data(), data.size()); path.pkg->CloseFile(file); - resource->extraBootstrapFile = data; + js::extraBootstrapFile = data; } -void js::IResource::GetExtraBootstrapFile(js::FunctionContext& ctx) +void js::GetExtraBootstrapFile(js::FunctionContext& ctx) { - ctx.Return(ctx.GetResource()->extraBootstrapFile); + ctx.Return(js::extraBootstrapFile); } #ifdef ALT_CLIENT_API -void js::IResource::RegisterCefExtraBootstrapFile(FunctionContext& ctx) +void js::RegisterCefExtraBootstrapFile(FunctionContext& ctx) { if (!ctx.CheckArgCount(1)) return; @@ -107,12 +107,12 @@ 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", RegisterExtraBootstrapFile); - TemporaryGlobalExtension getExtraBootstrapExtension(ctx, "__getExtraBootstrapFile", GetExtraBootstrapFile); + 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", RegisterCefExtraBootstrapFile); + TemporaryGlobalExtension registerCefExtraBootstrapExtension(ctx, "__registerCefExtraBootstrapFile", js::RegisterCefExtraBootstrapFile); #endif for(Binding* binding : bindings) InitializeBinding(binding); diff --git a/shared/src/interfaces/IResource.h b/shared/src/interfaces/IResource.h index 04caa04e1..54a31975b 100644 --- a/shared/src/interfaces/IResource.h +++ b/shared/src/interfaces/IResource.h @@ -19,6 +19,15 @@ 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: @@ -28,12 +37,6 @@ namespace js static constexpr int ContextInternalFieldIdx = 1; static void RequireBindingNamespaceWrapper(FunctionContext& ctx); - static void RegisterExtraBootstrapFile(FunctionContext& ctx); - static void GetExtraBootstrapFile(FunctionContext& ctx); - -#ifdef ALT_CLIENT_API - static void RegisterCefExtraBootstrapFile(FunctionContext& ctx); -#endif v8::Isolate* isolate; Persistent context; @@ -43,8 +46,6 @@ namespace js std::vector promises; - std::string extraBootstrapFile; - void Initialize() { context.Get(isolate)->SetAlignedPointerInEmbedderData(ContextInternalFieldIdx, this);