add project args & add vc.get_project_args

This commit is contained in:
MihailRis 2026-05-31 21:04:51 +03:00
parent 4565c6a3bd
commit aa5bd51206
5 changed files with 48 additions and 3 deletions

View file

@ -1,5 +1,7 @@
local _vc_headless = __VC_HEADLESS
local _vc_project_args = __VC_PROJECT_ARGS
__VC_HEADLESS = nil
__VC_PROJECT_ARGS = nil
vc = {
is_headless = function()
@ -300,6 +302,8 @@ require "core:internal/extensions/file"
require "core:internal/extensions/table"
require "core:internal/extensions/string"
vc.get_project_args = table.copy(_vc_project_args)
local bytearray = require "core:internal/bytearray"
Bytearray = bytearray.FFIBytearray
Bytearray_as_string = bytearray.FFIBytearray_as_string

View file

@ -2,6 +2,7 @@
#include <string>
#include <filesystem>
#include <unordered_map>
struct CoreParameters {
bool headless = false;
@ -13,4 +14,5 @@ struct CoreParameters {
std::filesystem::path projectFolder;
std::string debugServerString;
int tps = 20;
std::unordered_map<std::string, std::string> projectArgs;
};

View file

@ -15,9 +15,12 @@
#include "usertypes/lua_type_pcmstream.hpp"
#include "engine/Engine.hpp"
static debug::Logger logger("lua-state");
static lua::State* main_thread = nullptr;
static bool headless_mode = false;
namespace {
debug::Logger logger("lua-state");
lua::State* main_thread = nullptr;
bool headless_mode = false;
const std::unordered_map<std::string, std::string>* project_args;
}
using namespace lua;
@ -151,6 +154,13 @@ void lua::init_state(State* L, StateType stateType) {
pushboolean(L, headless_mode);
setglobal(L, "__VC_HEADLESS");
createtable(L, 0, project_args->size());
for (const auto& [key, value] : *project_args) {
pushstring(L, value);
setfield(L, key);
}
setglobal(L, "__VC_PROJECT_ARGS");
auto file = "res:scripts/stdmin.lua";
auto src = io::read_string(file);
lua::pop(L, lua::execute(L, 0, src, "core:scripts/stdmin.lua"));
@ -181,6 +191,7 @@ void lua::initialize(const EnginePaths& paths, const CoreParameters& params) {
logger.info() << LUAJIT_VERSION;
headless_mode = params.headless;
project_args = &params.projectArgs;
main_thread = create_state(
paths, params.headless ? StateType::SCRIPT : StateType::BASE
);

View file

@ -26,6 +26,13 @@ namespace util {
return last[0] == '-';
}
bool isNextKeywordArg() const {
if (pos >= argc) {
throw std::runtime_error("unexpected end");
}
return argv[pos][0] == '-';
}
std::string next() {
if (pos >= argc) {
throw std::runtime_error("unexpected end");

View file

@ -97,11 +97,32 @@ static bool perform_keyword(
throw std::runtime_error("unknown argument " + keyword);
}
static void parse_project_args(util::ArgsReader& reader, CoreParameters& params) {
while (reader.hasNext()) {
std::string key = reader.next();
if (!reader.isKeywordArg()) {
std::cerr << "--<keyword> argument expected" << std::endl;
return;
}
key = key.substr(2);
if (!reader.hasNext() || reader.isNextKeywordArg()) {
params.projectArgs[std::move(key)] = "";
continue;
}
std::string value = reader.next();
params.projectArgs[std::move(key)] = std::move(value);
}
}
bool parse_cmdline(int argc, char** argv, CoreParameters& params) {
util::ArgsReader reader(argc, argv);
reader.skip();
while (reader.hasNext()) {
std::string token = reader.next();
if (token == "--") {
parse_project_args(reader, params);
return true;
}
if (reader.isKeywordArg()) {
if (!perform_keyword(reader, token, params)) {
return false;