Skip to content

Commit

Permalink
executable: Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
LeonMrBonnie committed Feb 22, 2023
1 parent d096d87 commit 781eea9
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 83 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@
"xlocmes": "cpp",
"filesystem": "cpp",
"set": "cpp",
"deque": "cpp"
"deque": "cpp",
"variant": "cpp"
},
"editor.defaultFormatter": "xaver.clang-format",
"editor.formatOnSave": true
Expand Down
5 changes: 5 additions & 0 deletions compiler/compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
71 changes: 17 additions & 54 deletions executable/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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);
Expand All @@ -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
Expand All @@ -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;
}
33 changes: 5 additions & 28 deletions executable/src/package.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
};

0 comments on commit 781eea9

Please sign in to comment.