Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
LeonMrBonnie committed Dec 15, 2021
1 parent d6bf2be commit a0593fe
Show file tree
Hide file tree
Showing 13 changed files with 214 additions and 61 deletions.
98 changes: 98 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
---
BasedOnStyle: LLVM
IndentWidth: 4
---
Language: Cpp
ColumnLimit: 190
AccessModifierOffset: 0
AlignAfterOpenBracket: true
#AlignConsecutiveAssignments: true
AlignConsecutiveBitFields: true
#AlignConsecutiveDeclarations: true
AlignConsecutiveMacros: true
AlignEscapedNewlines: Left
AlignOperands: true
AlignTrailingComments: true
AllowAllArgumentsOnNextLine: true
AllowAllConstructorInitializersOnNextLine: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: Never
AllowShortCaseLabelsOnASingleLine: true
AllowShortEnumsOnASingleLine: false
AllowShortFunctionsOnASingleLine: Empty
AllowShortIfStatementsOnASingleLine: Always
AllowShortLambdasOnASingleLine: All
AllowShortLoopsOnASingleLine: true
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: Yes
BinPackArguments: false
BinPackParameters: false
#BitFieldColonSpacing: Both
BraceWrapping: Custom
BraceWrapping:
AfterCaseLabel: true
AfterClass: true
AfterControlStatement: MultiLine
AfterEnum: true
AfterFunction: true
AfterNamespace: false
AfterStruct: true
AfterUnion: true
AfterExternBlock: true
BeforeCatch: true
BeforeElse: true
BeforeLambdaBody: false
BeforeWhile: false
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Allman
BreakBeforeTernaryOperators: true
BreakConstructorInitializers: BeforeColon
BreakInheritanceList: BeforeColon
BreakStringLiterals: false
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ContinuationIndentWidth: 2
Cpp11BracedListStyle: false
#EmptyLineAfterAccessModifier: Never
#EmptyLineBeforeAccessModifier: LogicalBlock
FixNamespaceComments: true
#IncludeBlocks: Regroup
#IndentAccessModifiers: false
AccessModifierOffset: -4
IndentCaseBlocks: false
IndentCaseLabels: true
IndentExternBlock: true
IndentGotoLabels: false
IndentPPDirectives: BeforeHash
IndentWrappedFunctionNames: true
InsertTrailingCommas: None
KeepEmptyLinesAtTheStartOfBlocks: false
MaxEmptyLinesToKeep: 1
NamespaceIndentation: All
PointerAlignment: Left
#ShortNamespaceLines: 0
SortIncludes: false
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: false
#SpaceAroundPointerQualifiers: Default
SpaceBeforeAssignmentOperators: true
#SpaceBeforeCaseColon: false
SpaceBeforeCpp11BracedList: false
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: Never
SpaceBeforeRangeBasedForLoopColon: true
SpaceBeforeSquareBrackets: false
SpaceInEmptyBlock: false
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 2
SpacesInAngles: false
SpacesInCStyleCastParentheses: false
SpacesInConditionalStatement: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: c++17
DeriveLineEnding: true
UseTab: Never
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
BUILD/
dist/
dist/
deps/v8/**/
36 changes: 18 additions & 18 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
{
"configurations": [
{
"name": "Win32 Static",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE",
"ALT_SERVER_API"
],
"windowsSdkVersion": "10.0.19041.0",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "msvc-x64"
}
{
"name": "Win32 Static",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE",
"ALT_SERVER_API"
],
"windowsSdkVersion": "10.0.19041.0",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "msvc-x64",
"configurationProvider": "ms-vscode.cmake-tools"
}
],
"version": 4
}

}
4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,7 @@
"thread": "cpp",
"xlocbuf": "cpp",
"xlocmes": "cpp"
}
},
"editor.defaultFormatter": "xaver.clang-format",
"editor.formatOnSave": true
}
18 changes: 9 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
cmake_minimum_required (VERSION 3.10)
include(FetchContent)
include(cmake/DepsDownload.cmake)

project("altv-js-bytecode")

# Fetch v8 from the Client JS repo
message("Fetching v8 from Client JS...")
FetchContent_Declare(altv-js-deps
URL https://github.com/altmp/altv-client-js/releases/download/deps3/deps.zip
)
FetchContent_Populate(altv-js-deps)
set(ALTV_JS_DL_DEPS ${altv-js-deps_SOURCE_DIR})
set(DEPS_FOLDER ${PROJECT_SOURCE_DIR}/deps)

# Fetch v8
message("alt:V JS - Fetching v8 deps, can take a while")

DownloadDeps(${DEPS_FOLDER})

file(GLOB_RECURSE PROJECT_SOURCE_FILES "src/*.h" "src/*.cpp" "include/*.h")

Expand All @@ -32,7 +32,7 @@ GroupSources(${PROJECT_SOURCE_DIR}/src "Source Files")

include_directories(
deps
${ALTV_JS_DL_DEPS}/include
"include"
)

set(ALTV_JS_LINKS
Expand All @@ -42,7 +42,7 @@ set(ALTV_JS_LINKS
shlwapi.lib

# V8
${ALTV_JS_DL_DEPS}/lib/$<IF:$<CONFIG:Debug>,Debug,Release>/v8_monolith.lib
${DEPS_FOLDER}/v8/lib/$<IF:$<CONFIG:Debug>,Debug,Release>/v8_monolith.lib
)

set(ALTV_JS_DEFS
Expand Down
52 changes: 52 additions & 0 deletions cmake/DepsDownload.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@

# Set this to false, when using a custom v8 build for testing
set(__deps_check_enabled true)

function(DownloadDeps basePath)
set(__deps_release_lib "${basePath}/v8/lib/Release/v8_monolith.lib")
set(__deps_debug_lib "${basePath}/v8/lib/Debug/v8_monolith.lib")
set(__deps_headers "${basePath}/v8/include/headers.zip")
set(__deps_update_file "${basePath}/v8/update.json")

if(__deps_check_enabled)
file(DOWNLOAD "https://cdn.altv.mp/deps/v8/dev/update.json" ${__deps_update_file})
file(READ ${__deps_update_file} __deps_update_json)
string(JSON __deps_hashes GET ${__deps_update_json} hashList)
file(REMOVE ${__deps_update_file})

if(EXISTS ${__deps_release_lib})
file(SHA1 ${__deps_release_lib} __deps_release_checksum)
else()
set(__deps_release_checksum 0)
endif()
string(JSON __deps_release_checksum_cdn GET ${__deps_hashes} v8_monolith.lib)
if(NOT ${__deps_release_checksum} STREQUAL ${__deps_release_checksum_cdn})
message("Downloading release binary...")
file(DOWNLOAD "https://cdn.altv.mp/deps/v8/dev/v8_monolith.lib" ${__deps_release_lib})
endif()

if(EXISTS ${__deps_debug_lib})
file(SHA1 ${__deps_debug_lib} __deps_debug_checksum)
else()
set(__deps_debug_checksum 0)
endif()
string(JSON __deps_debug_checksum_cdn GET ${__deps_hashes} v8_monolithd.lib)
if(NOT ${__deps_debug_checksum} STREQUAL ${__deps_debug_checksum_cdn})
message("Downloading debug binary...")
file(DOWNLOAD "https://cdn.altv.mp/deps/v8/dev/v8_monolithd.lib" ${__deps_debug_lib})
endif()

if(EXISTS ${__deps_headers})
file(SHA1 ${__deps_headers} __deps_headers_checksum)
else()
set(__deps_headers_checksum 0)
endif()
string(JSON __deps_headers_checksum_cdn GET ${__deps_hashes} headers.zip)
if(NOT ${__deps_headers_checksum} STREQUAL ${__deps_headers_checksum_cdn})
message("Downloading headers...")
file(DOWNLOAD "https://cdn.altv.mp/deps/v8/dev/headers.zip" ${__deps_headers})
message("Extracting headers...")
file(ARCHIVE_EXTRACT INPUT ${__deps_headers} DESTINATION "${basePath}")
endif()
endif()
endfunction()
Empty file added deps/v8/.gitkeep
Empty file.
23 changes: 23 additions & 0 deletions include/Bytecode.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once
#include "v8/include/v8.h"

#include <string>

namespace BytecodeCompiler
{
constexpr uint32_t currentVersion = 1;

struct Bytecode
{
uint32_t version = 0;
size_t size = 0;
uint8_t* data = nullptr;

bool IsValid()
{
return version == currentVersion && size != 0 && data != nullptr;
}

Bytecode(uint32_t version, size_t size, uint8_t* data) : version(version), size(size), data(data) {}
};
} // namespace BytecodeCompiler
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ EXPORT bool altMain(alt::ICore* core)

EXPORT uint32_t GetSDKVersion()
{
return alt::ICore::SDK_VERSION;
return alt::ICore::SDK_VERSION;
}
25 changes: 1 addition & 24 deletions src/resource.cpp
Original file line number Diff line number Diff line change
@@ -1,28 +1,5 @@
#include "resource.h"
#include "util/compiler.h"
#include <sstream>
#include <fstream>

bool JSBytecodeResource::MakeClient(alt::IResource::CreationInfo *info, alt::Array<alt::String> files)
{
if(info->type != "jsb" && info->type != "js") return true;

v8::Locker locker(isolate);
v8::Isolate::Scope isolateScope(isolate);
v8::HandleScope handleScope(isolate);

alt::String path = resource->GetPath() + "/" + info->main;
std::ifstream iFile;
iFile.open(path.CStr());
std::string source((std::istreambuf_iterator<char>(iFile)), std::istreambuf_iterator<char>());

auto bytecode = BytecodeCompiler::CompileSourceIntoBytecode(isolate, info->main.CStr(), source.c_str());
auto file = info->pkg->OpenFile(info->main);
info->pkg->WriteFile(file, (void*)bytecode->data, bytecode->length);
info->pkg->CloseFile(file);

std::cout << __FUNCTION__ << " " << "size: " << bytecode->length << std::endl;
std::cout << __FUNCTION__ << " " << "version: " << v8::ScriptCompiler::CachedDataVersionTag() << std::endl;

return true;
}
bool JSBytecodeResource::WriteClientFile(alt::IPackage* package, const std::string& fileName, void* buffer, uint64_t size) {}
2 changes: 1 addition & 1 deletion src/resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ class JSBytecodeResource : public alt::IResource::Impl
public:
JSBytecodeResource(alt::IResource* resource, v8::Isolate* isolate) : resource(resource), isolate(isolate) {}

bool MakeClient(alt::IResource::CreationInfo *info, alt::Array<alt::String> files) override;
bool WriteClientFile(alt::IPackage* package, const std::string& fileName, void* buffer, uint64_t size) override;
};
10 changes: 5 additions & 5 deletions src/runtime.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#include "runtime.h"

JSBytecodeRuntime::JSBytecodeRuntime()
JSBytecodeRuntime::JSBytecodeRuntime()
{
platform = v8::platform::NewDefaultPlatform();
v8::V8::InitializePlatform(platform.get());
v8::V8::Initialize();
v8::V8::InitializePlatform(platform.get());
v8::V8::Initialize();

create_params.array_buffer_allocator = v8::ArrayBuffer::Allocator::NewDefaultAllocator();
create_params.array_buffer_allocator = v8::ArrayBuffer::Allocator::NewDefaultAllocator();

isolate = v8::Isolate::New(create_params);
isolate = v8::Isolate::New(create_params);
}

0 comments on commit a0593fe

Please sign in to comment.