Skip to content

Commit

Permalink
Merge branch 'rc' into release
Browse files Browse the repository at this point in the history
  • Loading branch information
C0kkie committed Dec 7, 2022
2 parents e0d04b6 + ba54984 commit df1c35f
Show file tree
Hide file tree
Showing 15 changed files with 76 additions and 60 deletions.
24 changes: 9 additions & 15 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
runs-on: windows-2019
steps:
- name: Checkout repository
uses: actions/checkout@v2
uses: actions/checkout@v3
with:
submodules: recursive

Expand All @@ -23,39 +23,33 @@ jobs:
run: build.bat

- name: Upload module dll
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v3
with:
name: bytecode-module-windows-dll
path: ./BUILD/bin/module/js-bytecode-module.dll

- name: Upload module pdb
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v3
with:
name: bytecode-module-windows-pdb
path: ./BUILD/bin/module/js-bytecode-module.pdb


build-linux:
name: Build linux
runs-on: ubuntu-18.04
runs-on: ubuntu-22.04
steps:
- name: Checkout repository
uses: actions/checkout@v2
uses: actions/checkout@v3
with:
submodules: recursive

- name: Install Clang
run: |
sudo apt update
sudo apt install clang-6.0
- name: Build
run: |
export CXX=/usr/bin/clang++-6.0
./build.sh
- name: Upload module so
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v3
with:
name: bytecode-module-linux-so
path: ./BUILD/module/libjs-bytecode-module.so
Expand All @@ -66,19 +60,19 @@ jobs:
needs: [build-linux, build-windows]
steps:
- name: Download windows module dll
uses: actions/download-artifact@v2
uses: actions/download-artifact@v3
with:
name: bytecode-module-windows-dll
path: dist-windows/modules

- name: Download windows module pdb
uses: actions/download-artifact@v2
uses: actions/download-artifact@v3
with:
name: bytecode-module-windows-pdb
path: dist

- name: Download linux module so
uses: actions/download-artifact@v2
uses: actions/download-artifact@v3
with:
name: bytecode-module-linux-so
path: dist-linux/modules
Expand Down
5 changes: 1 addition & 4 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
[submodule "module/deps/cpp-sdk"]
path = module/deps/cpp-sdk
url = https://github.com/altmp/cpp-sdk.git
[submodule "executable/deps/alt-config"]
path = executable/deps/alt-config
url = [email protected]:altmp/alt-config.git
url = https://github.com/altmp/cpp-sdk.git
2 changes: 1 addition & 1 deletion .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
],
"windowsSdkVersion": "10.0.19041.0",
"cStandard": "c11",
"cppStandard": "c++17",
"cppStandard": "c++20",
"intelliSenseMode": "msvc-x64",
"configurationProvider": "ms-vscode.cmake-tools"
}
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cmake_minimum_required (VERSION 3.10)

project("js-bytecode")
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
Expand Down
2 changes: 0 additions & 2 deletions compiler/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ add_library(${PROJECT_NAME} STATIC "compiler.cpp")

if(UNIX)
target_compile_options(${PROJECT_NAME} PRIVATE
-std=c++17
-stdlib=libstdc++
-fPIC
)
endif()
Expand Down
26 changes: 15 additions & 11 deletions compiler/compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ bool Compiler::CompileModule(const std::string& fileName, bool compileDependenci
}

// Write the bytecode to file
std::vector<uint8_t> bytecodeResult = CreateBytecodeBuffer(cache->data, cache->length);
std::vector<uint8_t> bytecodeResult = CreateBytecodeBuffer(cache->data, cache->length, sourceCode.size());
bool writeResult = package->WriteFile(fileName, (void*)bytecodeResult.data(), bytecodeResult.size());
if(!writeResult)
{
Expand Down Expand Up @@ -85,8 +85,13 @@ bool Compiler::CompileModule(const std::string& fileName, bool compileDependenci

// Compile the dependency file
std::string fullFileName = package->ResolveFile(depPath, fileName);

// Check if the file has already been compiled
if(std::find(compiledFiles.begin(), compiledFiles.end(), fullFileName) != compiledFiles.end()) continue;

// Dont compile if the module is ignored
if(std::find(ignoredModules.begin(), ignoredModules.end(), fullFileName) != ignoredModules.end()) continue;

if(!CompileModule(fullFileName, true)) return false;
}
}
Expand All @@ -101,35 +106,34 @@ bool Compiler::IsBytecodeFile(void* buffer, size_t size)
return true;
}

std::vector<uint8_t> Compiler::CreateBytecodeBuffer(const uint8_t* buffer, int length)
std::vector<uint8_t> Compiler::CreateBytecodeBuffer(const uint8_t* buffer, int length, int sourceLength)
{
// Make necessary changes to the bytecode
FixBytecode(buffer);
FixBytecode(buffer, sourceLength);

// Create our own custom bytecode buffer by appending our magic bytes
// at the front, and then the bytecode itself at the end
std::vector<uint8_t> buf;
size_t bufSize = magicBytes.size() + length;
size_t bufSize = magicBytes.size() + sizeof(int) + length;
buf.resize(bufSize);

memcpy(buf.data(), magicBytes.data(), magicBytes.size());
memcpy(buf.data() + magicBytes.size(), buffer, length);
memcpy(buf.data() + magicBytes.size(), &sourceLength, sizeof(int));
memcpy(buf.data() + magicBytes.size() + sizeof(int), buffer, length);

return std::move(buf);
return buf;
}

// Hash for empty module ("")
static constexpr uint32_t srcHash = 2147483648;
static constexpr int srcHashOffset = 8;

static constexpr uint32_t flagsHash = 3901848073;
static constexpr int flagsHashOffset = 12;

void Compiler::FixBytecode(const uint8_t* buffer)
void Compiler::FixBytecode(const uint8_t* buffer, int sourceLength)
{
// Copy hash of empty source file into bytecode source hash section
// Copy hash of source into bytecode source hash section
// Needed because V8 compares the bytecode code hash to provided source hash
Helpers::CopyValueToBuffer(buffer, srcHashOffset, srcHash);
Helpers::CopyValueToBuffer(buffer, srcHashOffset, Helpers::CreateV8SourceHash(sourceLength));

// Overwrite flags hash with the hash used in client js
// !!! Make sure to update the hash if flags in client js change !!!
Expand Down
4 changes: 2 additions & 2 deletions compiler/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ namespace BytecodeCompiler
bool IsBytecodeFile(void* buffer, size_t size);

private:
std::vector<uint8_t> CreateBytecodeBuffer(const uint8_t* buffer, int length);
std::vector<uint8_t> CreateBytecodeBuffer(const uint8_t* buffer, int length, int sourceLength);

static void FixBytecode(const uint8_t* buffer);
static void FixBytecode(const uint8_t* buffer, int sourceLength);
};
} // namespace BytecodeCompiler
7 changes: 7 additions & 0 deletions compiler/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ namespace Helpers
}
}
}
// Copies 'SerializedCodeData::SourceHash' behaviour
inline uint32_t CreateV8SourceHash(uint32_t sourceSize)
{
// We always use modules, so this flag is always used
static constexpr uint32_t moduleFlagMask = (1 << 31);
return sourceSize | moduleFlagMask;
}
inline void CheckTryCatch(const std::string& fileName, BytecodeCompiler::ILogger* logger, v8::TryCatch& tryCatch, v8::Local<v8::Context> ctx)
{
if(tryCatch.HasCaught())
Expand Down
4 changes: 3 additions & 1 deletion docs/internal.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,7 @@ so that the module is working again.
## Format
The bytecode we send to the client has exactly *5 bytes* of magic bytes at the front, these bytes are `ALTBC` to identify the data as alt:V bytecode, when we write it to file.
To read this bytecode, we need to check for these 5 magic bytes at the front, if they match, remove them from the buffer,
After that there are *4 bytes* that form a 4-byte integer that corresponds to the size of the original source code (this is needed because V8 sometimes uses the source code buffer,
when compiling functions, and if the buffer is too small, it crashes) that will be used to create a string to pass to the script compiler with the length of the original source code.
To read this bytecode, we need to check for these 5 magic bytes at the front, if they match, remove them and the 4 bytes from the source code length from the buffer,
and the remaining buffer is the full bytecode generated by V8, which we can then use to instantiate the script.
10 changes: 6 additions & 4 deletions executable/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ add_executable(${PROJECT_NAME}
${PROJECT_SOURCE_FILES}
)

if(UNIX)
target_compile_options(${PROJECT_NAME} PRIVATE
-fPIC
)
endif()

include(Shared)
SetupProject("executable")

Expand All @@ -27,10 +33,6 @@ if(WIN32)
${GLOBAL_DEPS_FOLDER}/v8/lib/$<IF:$<CONFIG:Debug>,Debug,Release>/v8_monolith.lib
)
elseif(UNIX)
target_compile_options(${PROJECT_NAME} PRIVATE
-std=c++17
-stdlib=libstdc++
)
set(LINK_LIBS
${GLOBAL_DEPS_FOLDER}/v8/lib/$<IF:$<CONFIG:Debug>,Debug,Release>/libv8_monolith.a
)
Expand Down
13 changes: 6 additions & 7 deletions module/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ add_library(${PROJECT_NAME} SHARED
${PROJECT_SOURCE_FILES}
)

if(UNIX)
target_compile_options(${PROJECT_NAME} PRIVATE
-fPIC
)
endif()

add_dependencies(${PROJECT_NAME} alt-sdk)

include(Shared)
Expand All @@ -42,13 +48,6 @@ target_compile_definitions(${PROJECT_NAME} PRIVATE
${ALTV_JS_DEFS}
)

if(UNIX)
target_compile_options(${PROJECT_NAME} PRIVATE
-std=c++17
-stdlib=libstdc++
)
endif()

if(WIN32)
set(ALTV_JS_LINKS
# Platform binaries
Expand Down
4 changes: 2 additions & 2 deletions module/deps/Log.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ class Log
buf << val;
return *this;
}
#if _HAS_CXX20
#if __cplusplus >= 202002L
Log& Put(const char8_t* val)
{
buf << (const char*)val;
return *this;
}
#endif // _HAS_CXX20
#endif // __cplusplus
Log& Put(LogFn val)
{
return val(*this);
Expand Down
2 changes: 1 addition & 1 deletion module/deps/cpp-sdk
Submodule cpp-sdk updated 79 files
+1 −2 IAudioFilter.h
+57 −45 ICore.h
+1 −1 IDiscordManager.h
+9 −6 IResource.h
+5 −0 IScriptRuntime.h
+3 −1 SDK.h
+307 −0 deps/ConfigBase.h
+1 −1 deps/alt-config
+156 −31 deps/alt-math/alt-math.h
+3 −4 events/CAudioEvent.h
+3 −4 events/CClientScriptEvent.h
+5 −6 events/CColShapeEvent.h
+3 −4 events/CConnectionQueueAddEvent.h
+3 −4 events/CConnectionQueueRemoveEvent.h
+3 −4 events/CCreateBaseObjectEvent.h
+5 −1 events/CEvent.h
+5 −6 events/CExplosionEvent.h
+3 −3 events/CFireEvent.h
+3 −4 events/CGameEntityCreateEvent.h
+3 −4 events/CGameEntityDestroyEvent.h
+3 −4 events/CLocalMetaDataChangeEvent.h
+3 −4 events/CMetaDataChangeEvent.h
+7 −8 events/CNetOwnerChangeEvent.h
+3 −4 events/CPlayerBeforeConnectEvent.h
+3 −4 events/CPlayerChangeAnimationEvent.h
+3 −4 events/CPlayerChangeInteriorEvent.h
+5 −7 events/CPlayerChangeVehicleSeatEvent.h
+45 −0 events/CPlayerConnectDeniedEvent.h
+3 −5 events/CPlayerConnectEvent.h
+5 −6 events/CPlayerDamageEvent.h
+5 −6 events/CPlayerDeathEvent.h
+29 −0 events/CPlayerDimensionChangeEvent.h
+3 −5 events/CPlayerDisconnectEvent.h
+5 −6 events/CPlayerEnterVehicleEvent.h
+5 −6 events/CPlayerEnteringVehicleEvent.h
+5 −6 events/CPlayerLeaveVehicleEvent.h
+5 −6 events/CPlayerRequestControlEvent.h
+17 −7 events/CPlayerWeaponChangeEvent.h
+28 −0 events/CPlayerWeaponShootEvent.h
+3 −4 events/CRemoveBaseObjectEvent.h
+0 −25 events/CRemoveEntityEvent.h
+3 −3 events/CRmlEvent.h
+3 −4 events/CStartProjectileEvent.h
+3 −4 events/CStreamSyncedMetaDataChangeEvent.h
+3 −4 events/CSyncedMetaDataChangeEvent.h
+3 −5 events/CTaskChangeEvent.h
+5 −6 events/CVehicleAttachEvent.h
+5 −6 events/CVehicleDamageEvent.h
+3 −4 events/CVehicleDestroyEvent.h
+5 −6 events/CVehicleDetachEvent.h
+12 −8 events/CWeaponDamageEvent.h
+3 −4 events/CWebSocketClientEvent.h
+3 −4 events/CWebViewEvent.h
+17 −3 objects/IBaseObject.h
+6 −3 objects/IEntity.h
+7 −2 objects/ILocalPlayer.h
+10 −7 objects/IPlayer.h
+46 −15 objects/IVehicle.h
+2 −4 script-objects/IAudio.h
+3 −6 script-objects/IBlip.h
+0 −3 script-objects/ICheckpoint.h
+1 −4 script-objects/IColShape.h
+1 −3 script-objects/IHandlingData.h
+0 −2 script-objects/IHttpClient.h
+1 −3 script-objects/IMapData.h
+4 −7 script-objects/INative.h
+45 −0 script-objects/IObject.h
+23 −28 script-objects/IRml.h
+7 −10 script-objects/IVoiceChannel.h
+3 −5 script-objects/IWeaponData.h
+0 −2 script-objects/IWebSocketClient.h
+0 −2 script-objects/IWebView.h
+13 −0 types/BoneInfo.h
+18 −0 types/ConfigError.h
+15 −0 types/Expected.h
+15 −6 types/IConnectionInfo.h
+3 −1 types/MValue.h
+15 −0 types/PedModelInfo.h
+7 −1 types/VehicleModelInfo.h
6 changes: 4 additions & 2 deletions module/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ static void CommandHandler(const std::vector<std::string>& args)
else if(args[0] == "--version")
{
Log::Colored << "~ly~cpp-sdk: #" << ALT_SDK_VERSION << Log::Endl;
Log::Colored << "~ly~" << u8"Copyright © 2022 altMP team." << Log::Endl;
Log::Colored << "~ly~"
<< "Copyright | 2022 altMP team." << Log::Endl;

Log::Colored << "~ly~v8: " << v8::V8::GetVersion() << Log::Endl;
Log::Colored << "~ly~" << u8"Copyright © 2014 The V8 project authors." << Log::Endl;
Log::Colored << "~ly~"
<< "Copyright | 2014 The V8 project authors." << Log::Endl;
}
else if(args[0] == "--help")
{
Expand Down
25 changes: 18 additions & 7 deletions module/src/runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,35 @@ void JSBytecodeRuntime::ProcessClientFile(alt::IResource* resource, alt::IPackag
Logger compilerLogger;
BytecodeCompiler::Compiler compiler(isolate, &compilerPackage, &compilerLogger);

static std::vector<std::string> ignoredModules = { "alt", "alt-client", "natives", "alt-worker", "alt-shared" };
Config::Value::ValuePtr config = resource->GetConfig();
// Get ignored files
std::vector<std::string> ignoredModules = { "alt", "alt-client", "natives", "alt-worker", "alt-shared" };
Config::Value::ValuePtr ignoredFiles = config->Get("ignored-files");
if(ignoredFiles->IsList())
{
Config::Value::List list = ignoredFiles->As<Config::Value::List>();
ignoredModules.reserve(ignoredModules.size() + list.size());
for(auto& item : list)
{
if(item->IsString()) ignoredModules.push_back(item->As<std::string>());
}
}
compiler.SetIgnoredModules(ignoredModules);

// Compile client main file
bool result = compiler.CompileModule(resource->GetClientMain());
if(!result) return;

// Compile the extra files
alt::config::Node config = resource->GetConfig();
alt::config::Node& node = config["extra-compile-files"];
if(node && node.IsList())
Config::Value::ValuePtr extraCompileFiles = config->Get("extra-compile-files");
if(extraCompileFiles->IsList())
{
alt::config::Node::List& list = node.ToList();
Config::Value::List list = extraCompileFiles->As<Config::Value::List>();
std::vector<std::string> extraFilePatterns;
extraFilePatterns.reserve(list.size());
for(alt::config::Node& item : list)
for(auto& item : list)
{
if(item.IsScalar()) extraFilePatterns.push_back(item.ToString());
if(item->IsString()) extraFilePatterns.push_back(item->As<std::string>());
}

std::set<std::string> files = resource->GetMatchedFiles(extraFilePatterns);
Expand Down

0 comments on commit df1c35f

Please sign in to comment.