Refactor code for invoke_lua_source and world.init. Also, add compile_commands.json to luprex
This commit is contained in:
@@ -546,44 +546,49 @@ void World::probe_lua_call(int64_t actor_id, int64_t place_id, std::string_view
|
||||
// some lua source file tries to modify, say, tangible state
|
||||
// in top-level code.
|
||||
//
|
||||
void World::rebuild_sourcedb() {
|
||||
bool World::rebuild_sourcedb() {
|
||||
bool ok = true;
|
||||
for (const eng::string &mod: source_db_.modules()) {
|
||||
open_lthread_state(0, 0, 0, false, true);
|
||||
eng::string err = source_db_.rebuild_module(mod);
|
||||
eng::string prints = lthread_prints_->str();
|
||||
lthread_prints_.reset();
|
||||
close_lthread_state();
|
||||
if (!err.empty()) ok = false;
|
||||
if (!err.empty() || !prints.empty()) {
|
||||
util::dprint("Loading Module ", mod);
|
||||
if (!err.empty()) util::dprint(err);
|
||||
if (!prints.empty()) util::dprint(prints);
|
||||
}
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
void World::update_source(const util::LuaSourceVec &source) {
|
||||
bool World::update_source(const util::LuaSourceVec &source) {
|
||||
assert(stack_is_clear());
|
||||
source_db_.update(source);
|
||||
rebuild_sourcedb();
|
||||
return rebuild_sourcedb();
|
||||
assert(stack_is_clear());
|
||||
}
|
||||
|
||||
void World::update_source(const util::LuaSourcePtr &source) {
|
||||
if (source != nullptr) {
|
||||
update_source(*source);
|
||||
bool World::update_source(const util::LuaSourcePtr &source) {
|
||||
if (source == nullptr) {
|
||||
return false;
|
||||
}
|
||||
return update_source(*source);
|
||||
}
|
||||
|
||||
void World::update_source(std::string_view sourcepack) {
|
||||
if (!sourcepack.empty()) {
|
||||
try {
|
||||
StreamBuffer sb(sourcepack);
|
||||
util::LuaSourceVec sv;
|
||||
SourceDB::deserialize_source(&sv, &sb);
|
||||
update_source(sv);
|
||||
} catch (const StreamException &ex) {
|
||||
return;
|
||||
}
|
||||
bool World::update_source(std::string_view sourcepack) {
|
||||
if (sourcepack.empty()) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
StreamBuffer sb(sourcepack);
|
||||
util::LuaSourceVec sv;
|
||||
SourceDB::deserialize_source(&sv, &sb);
|
||||
return update_source(sv);
|
||||
} catch (const StreamException &ex) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -960,11 +965,40 @@ void World::invoke_tick(int64_t actor_id, int64_t place_id, std::string_view dat
|
||||
}
|
||||
|
||||
void World::invoke_lua_source(int64_t actor_id, int64_t place_id, std::string_view datapack) {
|
||||
if (!is_authoritative()) {
|
||||
return;
|
||||
// Sanity check arguments.
|
||||
// We also need some kind of authentication here.
|
||||
if (!is_authoritative()) return;
|
||||
if (actor_id != place_id) return;
|
||||
|
||||
// Check if this is the first time we're loading the source.
|
||||
bool brand_new = (source_db_.modules().size() == 1);
|
||||
|
||||
// Compile and load the source.
|
||||
bool success = update_source(datapack);
|
||||
|
||||
// Call world.init
|
||||
if (brand_new) {
|
||||
if (success) {
|
||||
{
|
||||
lua_State *L = state();
|
||||
LuaVar lclass, lfunc;
|
||||
LuaExtStack LS(L, lclass, lfunc);
|
||||
|
||||
LS.getclass(lclass, "world");
|
||||
if (LS.classname(lclass) != "") {
|
||||
LS.rawget(lfunc, lclass, "init");
|
||||
spawn(LS, actor_id, place_id, lfunc, 0, false);
|
||||
}
|
||||
}
|
||||
run_scheduled_threads();
|
||||
} else {
|
||||
util::dprint("Did not run world.init because of lua errors.");
|
||||
util::dprint("You will need to fix the errors then run it manually.");
|
||||
}
|
||||
}
|
||||
// We need some kind of authentication here.
|
||||
update_source(datapack);
|
||||
|
||||
// Run the new thread and return.
|
||||
assert(stack_is_clear());
|
||||
}
|
||||
|
||||
void World::guard_blockable(lua_State *L, const char *fn) {
|
||||
|
||||
Reference in New Issue
Block a user