From 781eea9bc8ef87f82f6b86bd600a7d5e588e6f99 Mon Sep 17 00:00:00 2001 From: LeonMrBonnie Date: Wed, 22 Feb 2023 20:22:33 +0100 Subject: [PATCH] executable: Fixes --- .vscode/settings.json | 3 +- compiler/compiler.cpp | 5 +++ executable/src/main.cpp | 71 ++++++++++------------------------------ executable/src/package.h | 33 +++---------------- 4 files changed, 29 insertions(+), 83 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index b40a10c..de5b315 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -72,7 +72,8 @@ "xlocmes": "cpp", "filesystem": "cpp", "set": "cpp", - "deque": "cpp" + "deque": "cpp", + "variant": "cpp" }, "editor.defaultFormatter": "xaver.clang-format", "editor.formatOnSave": true diff --git a/compiler/compiler.cpp b/compiler/compiler.cpp index 846dbec..bb69c92 100644 --- a/compiler/compiler.cpp +++ b/compiler/compiler.cpp @@ -85,6 +85,11 @@ bool Compiler::CompileModule(const std::string& fileName, bool compileDependenci // Compile the dependency file std::string fullFileName = package->ResolveFile(depPath, fileName); + if(!package->FileExists(fullFileName)) + { + logger->LogError("File not found: " + depPath); + return false; + } // Check if the file has already been compiled if(std::find(compiledFiles.begin(), compiledFiles.end(), fullFileName) != compiledFiles.end()) continue; diff --git a/executable/src/main.cpp b/executable/src/main.cpp index 37853c9..291891f 100644 --- a/executable/src/main.cpp +++ b/executable/src/main.cpp @@ -9,7 +9,8 @@ namespace fs = std::filesystem; -static void EvaluateArgs(CLI::Parser& parser) { +static void EvaluateArgs(CLI::Parser& parser) +{ Logger logger = Logger::Instance(); if(parser.IsEmpty()) @@ -22,7 +23,7 @@ static void EvaluateArgs(CLI::Parser& parser) { { logger.Log("Available arguments:"); logger.Log(" --help: Show this help"); - logger.Log(" --input: Specify the resource directory"); + logger.Log(" --input: Specify the input file"); logger.Log(" --output: Specify the output directory"); logger.Log(" --debug: Enables debug info"); exit(0); @@ -31,76 +32,39 @@ static void EvaluateArgs(CLI::Parser& parser) { logger.ToggleDebugLogs(parser.HasArgument("debug")); } -static fs::path GetResourceDir(CLI::Parser& parser) { - fs::path resourceDir = parser.GetArgument("input"); +static fs::path GetInputFile(CLI::Parser& parser) +{ + fs::path inputFile = parser.GetArgument("input"); - if(resourceDir.empty() || !fs::exists(resourceDir) || !fs::is_directory(resourceDir)) + if(inputFile.empty() || !fs::exists(inputFile) || !fs::is_regular_file(inputFile)) { - Logger::Instance().LogError("Invalid input directory specified"); + Logger::Instance().LogError("Invalid input file specified"); exit(1); } - return resourceDir; + return inputFile; } -static fs::path GetOutputDir(CLI::Parser& parser) { +static fs::path GetOutputDir(CLI::Parser& parser) +{ fs::path outputDir = parser.GetArgument("output"); - if(outputDir.empty() || !fs::exists(outputDir) || !fs::is_directory(outputDir)) + if(outputDir.empty() || (fs::exists(outputDir) && !fs::is_directory(outputDir))) { Logger::Instance().LogError("Invalid output directory specified"); exit(1); } + if(!fs::exists(outputDir)) fs::create_directory(outputDir); return outputDir; } -static Config::Value::ValuePtr GetResourceConfig(Package& package, const fs::path& path) -{ - // Check if the input directory has a resource.toml - if(!fs::exists(path.string())) - { - Logger::Instance().LogError("Input directory does not contain a resource.toml"); - exit(1); - } - - // Read the resource.toml - std::string resourceCfgSource; - resourceCfgSource.resize(package.GetFileSize(path.string())); - - if(!package.ReadFile(path.string(), resourceCfgSource.data(), resourceCfgSource.size())) - { - Logger::Instance().LogError("Failed to read resource.toml"); - exit(1); - } - - // Parse the resource.toml - std::string error = "Failed to parse resource.toml"; - return TomlConfig::Parse(resourceCfgSource, error); -} - -static fs::path GetClientMain(Package& package, const fs::path& resourceDir) { - // Read the resource.cfg - Config::Value::ValuePtr config = GetResourceConfig(package, resourceDir / "resource.toml"); - std::string clientMainFile = config->Get("client-main")->AsString(); - - // Get the resource client-main file - if(clientMainFile.empty()) - { - Logger::Instance().LogError("Failed to find client-main in resource.toml"); - exit(1); - } - - // no need to check is the clientMainPath exists as its checked in the compiler - return resourceDir / clientMainFile; -} - int main(int argc, char* argv[]) { CLI::Parser parser(argc, argv); EvaluateArgs(parser); - fs::path resourceDir = GetResourceDir(parser); + fs::path inputFile = GetInputFile(parser); fs::path outputDir = GetOutputDir(parser); // Set up v8 @@ -118,13 +82,12 @@ int main(int argc, char* argv[]) v8::HandleScope handleScope(isolate); // Set up the file package, to read and write files - Package package(outputDir, resourceDir); - fs::path clientMain = GetClientMain(package, resourceDir); + Package package(outputDir); // Set up the compiler BytecodeCompiler::Compiler compiler(isolate, &package, &Logger::Instance()); - compiler.SetIgnoredModules({ "alt", "alt-client", "natives", "alt-worker", "alt-shared" }); + compiler.SetIgnoredModules({ "alt", "alt-server", "alt-shared" }); // Compile the main file - return compiler.CompileModule(clientMain.string()) ? 0 : 1; + return compiler.CompileModule(inputFile.string()) ? 0 : 1; } diff --git a/executable/src/package.h b/executable/src/package.h index f471fe9..ba7cfaa 100644 --- a/executable/src/package.h +++ b/executable/src/package.h @@ -10,15 +10,14 @@ namespace fs = std::filesystem; class Package : public BytecodeCompiler::IPackage { fs::path outPath; - fs::path inPath; public: - Package(fs::path _outPath, fs::path _inPath) : outPath(_outPath), inPath(_inPath) {} + Package(fs::path _outPath) : outPath(_outPath) {} bool WriteFile(const std::string& fileName, void* data, size_t size) override { - fs::path p (fileName); - fs::path filePath = p.replace_extension(".bin").filename(); + fs::path p(fileName); + fs::path filePath = p.replace_extension(".jsb").filename(); std::ofstream file(outPath / filePath, std::ios::out | std::ios::binary); if(!file.good()) return false; @@ -49,30 +48,8 @@ class Package : public BytecodeCompiler::IPackage } std::string ResolveFile(const std::string& file, const std::string& basePath) override { - fs::path path = fs::path(basePath) / file; + fs::path path = (fs::path(basePath).remove_filename() / file).lexically_normal(); if(!fs::exists(path)) return std::string(); - fs::path rootPath = path.root_path().string(); - fs::path fileName = path.filename().string(); - if(fileName.empty()) - { - if(FileExists(rootPath / "index.js")) fileName = "index.js"; - else if(FileExists(rootPath / "index.mjs")) - fileName = "index.mjs"; - else - return std::string(); - } - else - { - if(FileExists(rootPath / fs::path(fileName.string() + ".js"))) fileName += ".js"; - else if(FileExists(rootPath / fs::path(fileName.string() + ".mjs"))) - fileName += ".mjs"; - else if(FileExists(rootPath / fs::path(fileName.string() + "/index.js"))) - fileName += "/index.js"; - else if(FileExists(rootPath / fs::path(fileName.string() + "/index.mjs"))) - fileName += "/index.mjs"; - else if(!FileExists(fileName)) - return std::string(); - } - return (rootPath / fileName).string(); + return path.string(); } };