CPL command

This commit is contained in:
2026-05-26 15:07:42 -04:00
parent 933c1ac6c3
commit 8dab0d16b7
9 changed files with 64 additions and 47 deletions

View File

@@ -523,6 +523,9 @@ void World::probe_lua_call(int64_t actor_id, int64_t place_id, std::string_view
// This is called from World::update_source, and also
// from World::patch_source in the difference transmitter.
//
// When called from the difference transmitter, we suppress
// error messages.
//
// For the moment, errors are channeled to util::dprint,
// and 'print' statements just go to std::cerr. Neither
// of these is ideal. We need to get serious about setting
@@ -532,7 +535,7 @@ 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.
//
bool World::rebuild_sourcedb() {
bool World::rebuild_sourcedb(int64_t actor_id) {
bool ok = true;
for (const eng::string &mod: source_db_.modules()) {
open_lthread_state(0, 0, 0, false);
@@ -540,30 +543,28 @@ bool World::rebuild_sourcedb() {
eng::string prints = lthread_prints_.str();
clear_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);
if (actor_id >= 0) {
lthread_prints_ << "Compiling " << mod << std::endl;
if (!err.empty()) lthread_prints_ << err << std::endl;
if (!prints.empty()) lthread_prints_ << prints;
lthread_prints_to_actor(actor_id);
}
}
if (actor_id > 0) {
lthread_prints_ << (ok ? "Compilation Successful." : "Compilation Failed.") << std::endl;
lthread_prints_to_actor(actor_id);
}
return ok;
}
bool World::update_source(const util::LuaSourceVec &source) {
bool World::update_source(const util::LuaSourceVec &source, int64_t actor_id) {
assert(stack_is_clear());
source_db_.update(source);
return rebuild_sourcedb();
return rebuild_sourcedb(actor_id);
assert(stack_is_clear());
}
bool World::update_source(const util::LuaSourcePtr &source) {
if (source == nullptr) {
return false;
}
return update_source(*source);
}
bool World::update_source(std::string_view sourcepack) {
bool World::update_source(std::string_view sourcepack, int64_t actor_id) {
if (sourcepack.empty()) {
return false;
}
@@ -571,7 +572,7 @@ bool World::update_source(std::string_view sourcepack) {
StreamBuffer sb(sourcepack);
util::LuaSourceVec sv;
SourceDB::deserialize_source(&sv, &sb);
return update_source(sv);
return update_source(sv, actor_id);
} catch (const StreamException &ex) {
return false;
}
@@ -697,7 +698,7 @@ HttpServerResponse World::http_serve(const HttpParser &request) {
open_lthread_state(0, 0, 0, false);
eng::string msg = traceback_pcall(L, 1, LUA_MULTRET);
if (!msg.empty()) lthread_prints_ << msg << std::endl;
lthread_prints_to_dprint();
lthread_prints_to_actor(0);
clear_lthread_state();
// If the call threw an error, return
@@ -954,7 +955,7 @@ void World::invoke_lua_source(int64_t actor_id, int64_t place_id, std::string_vi
bool brand_new = (source_db_.modules().size() == 1);
// Compile and load the source.
bool success = update_source(datapack);
bool success = update_source(datapack, actor_id);
// Call world.init
if (brand_new) {
@@ -976,7 +977,7 @@ void World::invoke_lua_source(int64_t actor_id, int64_t place_id, std::string_vi
util::dprint("You will need to fix the errors then run it manually.");
}
}
// Run the new thread and return.
assert(stack_is_clear());
}
@@ -1096,7 +1097,7 @@ void World::run_scheduled_threads() {
}
LS.rawset(threads, sched.thread_id(), LuaNil);
}
lthread_prints_to_printbuffer();
lthread_prints_to_actor(lthread_actor_id_);
clear_lthread_state();
}
}
@@ -1162,22 +1163,20 @@ void World::open_lthread_state(int64_t actor, int64_t place, int64_t thread, boo
lthread_prints_.clear();
}
void World::lthread_prints_to_printbuffer()
void World::lthread_prints_to_actor(int64_t actor_id)
{
const eng::string &output = lthread_prints_.str();
if (output.size() > 0) {
Tangible *actor = tangible_get(lthread_actor_id_);
if (actor != nullptr) {
actor->print_buffer_.add_string(output, is_authoritative());
if (actor_id >= 0) {
Tangible *actor = tangible_get(actor_id);
if (actor != nullptr) {
actor->print_buffer_.add_string(output, is_authoritative());
} else {
util::dprintview(output);
}
}
}
}
void World::lthread_prints_to_dprint()
{
const eng::string &output = lthread_prints_.str();
if (output.size() > 0) {
util::dprintview(output);
lthread_prints_.str("");
lthread_prints_.clear();
}
}