2021-09-10 17:06:07 -04:00
|
|
|
|
|
|
|
|
#include "world.hpp"
|
|
|
|
|
#include "idalloc.hpp"
|
|
|
|
|
#include "animqueue.hpp"
|
|
|
|
|
#include "gui.hpp"
|
|
|
|
|
#include "traceback.hpp"
|
2021-10-21 13:15:04 -04:00
|
|
|
#include "pprint.hpp"
|
2022-04-16 02:26:32 -04:00
|
|
|
#include "util.hpp"
|
2023-04-10 16:00:47 -04:00
|
|
|
#include "serializelua.hpp"
|
2022-04-16 02:26:32 -04:00
|
|
|
|
2021-09-10 17:06:07 -04:00
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
|
|
void World::store_global_pointer(lua_State *L, World *v) {
|
|
|
|
|
lua_pushstring(L, "world");
|
|
|
|
|
lua_pushlightuserdata(L, v);
|
|
|
|
|
lua_rawset(L, LUA_REGISTRYINDEX);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
World *World::fetch_global_pointer(lua_State *L) {
|
|
|
|
|
lua_pushstring(L, "world");
|
|
|
|
|
lua_rawget(L, LUA_REGISTRYINDEX);
|
|
|
|
|
World *result = (World *)lua_touserdata(L, -1);
|
|
|
|
|
if (result == nullptr) {
|
|
|
|
|
luaL_error(L, "No world pointer stored.");
|
|
|
|
|
}
|
|
|
|
|
lua_pop(L, 1);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
World::~World() {
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-01 16:07:13 -05:00
|
|
|
World::World(WorldType wt) {
|
2021-09-10 17:06:07 -04:00
|
|
|
// Master world model by default.
|
|
|
|
|
world_type_ = wt;
|
|
|
|
|
|
|
|
|
|
// Initialize the ID allocator in master mode.
|
2023-03-01 16:07:13 -05:00
|
|
|
if (is_authoritative()) {
|
2021-09-10 17:06:07 -04:00
|
|
|
id_global_pool_.init_master();
|
|
|
|
|
} else {
|
|
|
|
|
id_global_pool_.init_synch();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Prepare to manipulate the lua state.
|
|
|
|
|
LuaVar world, globtab;
|
2023-04-14 15:33:58 -04:00
|
|
|
LuaExtStack LS(state(), world, globtab);
|
2021-09-10 17:06:07 -04:00
|
|
|
|
|
|
|
|
// Put the world pointer into the lua registry.
|
|
|
|
|
World::store_global_pointer(state(), this);
|
|
|
|
|
|
|
|
|
|
// Clear the global GUI pointer.
|
|
|
|
|
Gui::store_global_pointer(state(), nullptr);
|
|
|
|
|
|
2021-10-14 11:43:16 -04:00
|
|
|
// Clear the lthread state.
|
2021-10-21 13:15:04 -04:00
|
|
|
clear_lthread_state();
|
2021-10-14 11:43:16 -04:00
|
|
|
|
2021-09-10 17:06:07 -04:00
|
|
|
// Set the tabletype of the registry.
|
|
|
|
|
LS.settabletype(LuaRegistry, LUA_TT_REGISTRY);
|
|
|
|
|
|
|
|
|
|
// Set the tabletype of the global environment.
|
|
|
|
|
LS.getglobaltable(globtab);
|
|
|
|
|
LS.settabletype(globtab, LUA_TT_GLOBALENV);
|
|
|
|
|
|
2023-03-01 16:07:13 -05:00
|
|
|
// Store the world type in the registry.
|
|
|
|
|
LS.rawset(LuaRegistry, "worldtype", wt);
|
|
|
|
|
|
2023-03-01 17:35:06 -05:00
|
|
|
// Create the globaldb in the registry.
|
2023-03-01 16:07:13 -05:00
|
|
|
LS.rawset(LuaRegistry, "globaldb", LuaNewTable);
|
2021-09-10 17:06:07 -04:00
|
|
|
|
2023-04-10 17:58:44 -04:00
|
|
|
// Initialize the SourceDB.
|
2021-09-10 17:06:07 -04:00
|
|
|
source_db_.init(state());
|
2023-04-10 17:58:44 -04:00
|
|
|
|
2021-11-26 15:45:36 -05:00
|
|
|
// Clear the clock.
|
|
|
|
|
clock_ = 0;
|
|
|
|
|
|
2023-04-05 18:41:03 -04:00
|
|
|
// Initialize global variable state.
|
2023-04-10 16:00:47 -04:00
|
|
|
assign_seqno_ = 1;
|
2021-09-10 17:06:07 -04:00
|
|
|
}
|
|
|
|
|
|
2023-07-24 17:19:25 -04:00
|
|
|
Tangible::Tangible(World *w, int64_t id) : world_(w), anim_queue_(), id_player_pool_(&w->id_global_pool_) {
|
2021-09-10 17:06:07 -04:00
|
|
|
plane_item_.set_id(id);
|
|
|
|
|
plane_item_.track(&w->plane_map_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Tangible::update_plane_item() {
|
2023-07-24 17:19:25 -04:00
|
|
|
AnimCoreState pos = anim_queue_.get_final_core_state();
|
|
|
|
|
plane_item_.set_pos(pos.plane, pos.xyz.x, pos.xyz.y, pos.xyz.z);
|
2021-09-10 17:06:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Tangible::serialize(StreamBuffer *sb) {
|
|
|
|
|
anim_queue_.serialize(sb);
|
|
|
|
|
id_player_pool_.serialize(sb);
|
2021-11-14 15:57:18 -05:00
|
|
|
print_buffer_.serialize(sb);
|
2021-09-10 17:06:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Tangible::deserialize(StreamBuffer *sb) {
|
|
|
|
|
anim_queue_.deserialize(sb);
|
|
|
|
|
id_player_pool_.deserialize(sb);
|
2021-11-14 15:57:18 -05:00
|
|
|
print_buffer_.deserialize(sb);
|
2021-09-10 17:06:07 -04:00
|
|
|
update_plane_item();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Tangible *World::tangible_get(int64_t id) {
|
|
|
|
|
auto iter = tangibles_.find(id);
|
|
|
|
|
if (iter == tangibles_.end()) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
} else {
|
|
|
|
|
return iter->second.get();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const Tangible *World::tangible_get(int64_t id) const {
|
|
|
|
|
auto iter = tangibles_.find(id);
|
|
|
|
|
if (iter == tangibles_.end()) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
} else {
|
|
|
|
|
return iter->second.get();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
World::TanVector World::tangible_get_all(const IdVector &ids) const {
|
|
|
|
|
TanVector result(ids.size());
|
|
|
|
|
for (int i = 0; i < int(ids.size()); i++) {
|
|
|
|
|
result[i] = tangible_get(ids[i]);
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-06 20:12:03 -04:00
|
|
|
Tangible *World::tangible_get(const LuaCoreStack &LS, LuaSlot tab, bool allowdel) {
|
2021-11-26 12:28:59 -05:00
|
|
|
int64_t id = LS.tanid(tab);
|
2021-09-10 17:06:07 -04:00
|
|
|
if (id == 0) {
|
|
|
|
|
luaL_error(LS.state(), "parameter is not a tangible");
|
|
|
|
|
}
|
|
|
|
|
Tangible *result = tangible_get(id);
|
2023-03-16 23:31:29 -04:00
|
|
|
if (!allowdel) {
|
|
|
|
|
if (result == nullptr) {
|
|
|
|
|
luaL_error(LS.state(), "argument is a deleted tangible, which is not allowed here");
|
|
|
|
|
}
|
2021-09-10 17:06:07 -04:00
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-24 17:19:25 -04:00
|
|
|
Tangible *World::tangible_make(const LuaCoreStack &LS0, LuaSlot database, int64_t id) {
|
2021-10-14 11:43:16 -04:00
|
|
|
assert(id != 0);
|
2023-03-16 23:31:29 -04:00
|
|
|
LuaVar metatab;
|
2023-04-14 15:33:58 -04:00
|
|
|
LuaExtStack LS(LS0.state(), metatab);
|
2021-09-10 17:06:07 -04:00
|
|
|
|
|
|
|
|
// Create the C++ part of the structure.
|
2021-10-15 13:51:32 -04:00
|
|
|
UniqueTangible &t = tangibles_[id];
|
2021-09-10 17:06:07 -04:00
|
|
|
assert (t == nullptr);
|
|
|
|
|
t.reset(new Tangible(this, id));
|
|
|
|
|
|
2023-07-24 17:19:25 -04:00
|
|
|
// AnimQueue initializes itself to a valid default state.
|
|
|
|
|
AnimState state;
|
|
|
|
|
state.add_defaults(nullptr);
|
|
|
|
|
t->anim_queue_.clear(state);
|
2021-09-10 17:06:07 -04:00
|
|
|
t->update_plane_item();
|
|
|
|
|
|
2023-03-16 23:31:29 -04:00
|
|
|
// Fetch the tangible's Lua database and metatable.
|
|
|
|
|
LS.maketan(database, id);
|
|
|
|
|
LS.getmetatable(metatab, database);
|
2021-09-10 17:06:07 -04:00
|
|
|
|
2023-03-16 23:31:29 -04:00
|
|
|
// Set up the inventory and thread table.
|
2021-09-10 17:06:07 -04:00
|
|
|
LS.rawset(database, "inventory", LuaNewTable);
|
|
|
|
|
LS.rawset(metatab, "threads", LuaNewTable);
|
2023-04-14 15:33:58 -04:00
|
|
|
|
2021-09-10 17:06:07 -04:00
|
|
|
return t.get();
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-24 17:19:25 -04:00
|
|
|
Tangible *World::tangible_make(int64_t id) {
|
2023-04-14 15:33:58 -04:00
|
|
|
LuaVar database;
|
|
|
|
|
LuaExtStack LS(state(), database);
|
2023-07-24 17:19:25 -04:00
|
|
|
return tangible_make(LS, database, id);
|
2023-04-14 15:33:58 -04:00
|
|
|
}
|
|
|
|
|
|
2021-09-10 17:06:07 -04:00
|
|
|
void World::tangible_delete(int64_t id) {
|
|
|
|
|
lua_State *L = state();
|
2023-03-16 23:31:29 -04:00
|
|
|
LuaVar tangibles, database, metatab;
|
2023-04-14 15:33:58 -04:00
|
|
|
LuaExtStack LS(L, tangibles, database, metatab);
|
2021-09-10 17:06:07 -04:00
|
|
|
|
|
|
|
|
// Fetch the C++ side of the tangible.
|
|
|
|
|
auto iter = tangibles_.find(id);
|
|
|
|
|
if (iter == tangibles_.end()) {
|
|
|
|
|
return; // Nothing to delete.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Fetch the lua side of the tangible.
|
2023-03-16 23:31:29 -04:00
|
|
|
LS.maketan(database, id);
|
2021-09-10 17:06:07 -04:00
|
|
|
assert(LS.istable(database));
|
2023-03-16 23:31:29 -04:00
|
|
|
LS.getmetatable(metatab, database);
|
|
|
|
|
|
|
|
|
|
// Clear out the database and the metatable.
|
|
|
|
|
LS.cleartable(database, false);
|
|
|
|
|
LS.cleartable(metatab, true);
|
2021-09-10 17:06:07 -04:00
|
|
|
|
2023-03-16 23:31:29 -04:00
|
|
|
// Now put the bare minimum info back into the metatable.
|
|
|
|
|
LS.rawset(metatab, "id", id);
|
|
|
|
|
LS.rawset(metatab, "__metatable", false);
|
2021-09-10 17:06:07 -04:00
|
|
|
|
|
|
|
|
// Remove the C++ portion from the tangibles table.
|
|
|
|
|
tangibles_.erase(iter);
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-24 17:19:25 -04:00
|
|
|
void World::get_near(PlaneScan &scan, util::IdVector *into) const {
|
|
|
|
|
into->clear();
|
|
|
|
|
// If 'near' is set, update the plane and center.
|
|
|
|
|
int64_t actor_id = scan.near();
|
|
|
|
|
if (actor_id != 0) {
|
|
|
|
|
const Tangible *player = tangible_get(actor_id);
|
|
|
|
|
if (player == nullptr) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const PlaneItem &pi = player->plane_item_;
|
|
|
|
|
scan.set_plane(pi.plane());
|
|
|
|
|
scan.set_center(util::XYZ(pi.x(), pi.y(), pi.z()));
|
2021-09-10 17:06:07 -04:00
|
|
|
}
|
2023-07-24 17:19:25 -04:00
|
|
|
plane_map_.scan(scan, into);
|
|
|
|
|
}
|
2021-09-10 17:06:07 -04:00
|
|
|
|
2023-07-24 17:19:25 -04:00
|
|
|
void World::get_near(int64_t player_id, float radius, bool exclude_nowhere, bool omit_player, bool sorted, util::IdVector *into) const {
|
2022-07-11 02:32:12 -04:00
|
|
|
PlaneScan scan;
|
2023-07-24 17:19:25 -04:00
|
|
|
scan.set_radius(radius);
|
2022-07-13 01:08:54 -04:00
|
|
|
scan.set_shape(PlaneScan::SPHERE);
|
2022-07-11 02:32:12 -04:00
|
|
|
scan.set_sorted(sorted);
|
|
|
|
|
scan.set_omit_nowhere(exclude_nowhere);
|
2022-07-14 00:57:11 -04:00
|
|
|
scan.set_near(player_id, !omit_player);
|
2023-07-24 17:19:25 -04:00
|
|
|
get_near(scan, into);
|
2021-09-10 17:06:07 -04:00
|
|
|
}
|
|
|
|
|
|
2023-07-26 17:40:20 -04:00
|
|
|
void World::get_encoded_animation_queues(uint32_t count, const int64_t *ids, util::SharedStdStringVec &into) {
|
|
|
|
|
util::SharedStdString empty = std::make_shared<std::string>("");
|
|
|
|
|
into.resize(count);
|
2023-07-24 17:20:45 -04:00
|
|
|
for (int i = 0; i < int(count); i++) {
|
|
|
|
|
Tangible *tan = tangible_get(ids[i]);
|
2023-07-26 17:40:20 -04:00
|
|
|
if (tan == nullptr) {
|
|
|
|
|
into[i] = empty;
|
2023-07-24 17:20:45 -04:00
|
|
|
} else {
|
2023-07-26 17:40:20 -04:00
|
|
|
into[i] = tan->anim_queue_.get_encoded_queue();
|
2023-07-24 17:20:45 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-10 17:06:07 -04:00
|
|
|
World::Redirects World::fetch_redirects() {
|
|
|
|
|
World::Redirects result = std::move(redirects_);
|
|
|
|
|
redirects_.clear();
|
2021-11-23 14:38:08 -05:00
|
|
|
return result;
|
2021-09-10 17:06:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int64_t World::create_login_actor() {
|
2021-10-25 14:47:37 -04:00
|
|
|
assert(stack_is_clear());
|
2021-10-14 11:43:16 -04:00
|
|
|
int64_t id = id_global_pool_.get_one();
|
2023-04-14 15:33:58 -04:00
|
|
|
LuaVar database, classtab, mt;
|
|
|
|
|
LuaExtStack LS(state(), database, classtab, mt);
|
2023-07-24 17:19:25 -04:00
|
|
|
Tangible *tan = tangible_make(LS, database, id);
|
2021-09-10 17:06:07 -04:00
|
|
|
LS.makeclass(classtab, "login");
|
|
|
|
|
LS.getmetatable(mt, database);
|
|
|
|
|
LS.rawset(mt, "__index", classtab);
|
|
|
|
|
tan->configure_id_pool_for_actor();
|
2021-11-14 15:57:18 -05:00
|
|
|
tan->print_buffer_.clear();
|
2021-09-10 17:06:07 -04:00
|
|
|
return tan->id();
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-24 02:17:41 -05:00
|
|
|
eng::string World::probe_lua(int64_t actor_id, const eng::string &lua) {
|
2021-11-26 13:56:24 -05:00
|
|
|
assert(stack_is_clear());
|
|
|
|
|
lua_State *L = state();
|
|
|
|
|
|
|
|
|
|
Tangible *actor = tangible_get(actor_id);
|
|
|
|
|
if (actor == nullptr) {
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LuaVar closure;
|
2023-04-14 17:50:06 -04:00
|
|
|
LuaExtStack LS(L, closure);
|
2021-11-26 13:56:24 -05:00
|
|
|
|
|
|
|
|
// create the compiled closure.
|
|
|
|
|
int status = luaL_loadbuffer(L, lua.c_str(), lua.size(), "=probe");
|
|
|
|
|
lua_replace(L, closure.index());
|
|
|
|
|
if (status != LUA_OK) {
|
|
|
|
|
// The closure is actually an error message. Do nothing.
|
|
|
|
|
// This should normally not happen: LuaConsole should filter
|
|
|
|
|
// out syntax errors.
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Call the closure.
|
|
|
|
|
int top = lua_gettop(L);
|
|
|
|
|
lua_pushvalue(L, closure.index());
|
2022-04-25 17:17:41 -04:00
|
|
|
open_lthread_state(actor_id, actor_id, 0, false, true);
|
|
|
|
|
eng::string msg = traceback_pcall(L, 0, LUA_MULTRET);
|
2021-11-26 13:56:24 -05:00
|
|
|
|
|
|
|
|
// If there's an error message, print it.
|
|
|
|
|
// Otherwise, pretty-print the results.
|
2022-02-25 19:57:23 -05:00
|
|
|
std::ostream *ostream = lthread_print_stream();
|
2022-04-25 17:17:41 -04:00
|
|
|
if (msg.empty()) {
|
2021-11-26 13:56:24 -05:00
|
|
|
for (int i = top + 1; i <= lua_gettop(L); i++) {
|
|
|
|
|
LuaSpecial root(i);
|
2023-03-05 01:51:25 -05:00
|
|
|
pprint(LS, root, PrettyPrintOptions(), ostream);
|
2022-06-07 01:54:08 -04:00
|
|
|
// TODO: this endl is unnecessary if we just printed a newline.
|
2021-11-26 13:56:24 -05:00
|
|
|
(*ostream) << std::endl;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
(*ostream) << msg << std::endl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Collect the lthread_prints (and also make sure they
|
|
|
|
|
// don't go into the printbuffer).
|
2022-02-24 02:17:41 -05:00
|
|
|
eng::string result = lthread_prints_->str();
|
2021-11-26 13:56:24 -05:00
|
|
|
lthread_prints_.reset();
|
|
|
|
|
|
|
|
|
|
close_lthread_state();
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-10 17:06:07 -04:00
|
|
|
void World::update_gui(int64_t actor_id, int64_t place_id, Gui *gui) {
|
|
|
|
|
assert(stack_is_clear());
|
2021-11-16 13:14:59 -05:00
|
|
|
gui->clear(place_id);
|
2021-09-10 17:06:07 -04:00
|
|
|
lua_State *L = state();
|
|
|
|
|
|
|
|
|
|
LuaVar actor, place, ugui, func, tangibles, mt, index;
|
2023-04-14 17:50:06 -04:00
|
|
|
LuaExtStack LS(L, actor, place, ugui, func, tangibles, mt, index);
|
2021-09-10 17:06:07 -04:00
|
|
|
|
|
|
|
|
// Get the actor and place.
|
|
|
|
|
LS.rawget(tangibles, LuaRegistry, "tangibles");
|
|
|
|
|
LS.rawget(actor, tangibles, actor_id);
|
|
|
|
|
LS.rawget(place, tangibles, place_id);
|
|
|
|
|
if (!LS.istable(actor) || !LS.istable(place)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get the interface closure.
|
|
|
|
|
LS.getmetatable(mt, place);
|
|
|
|
|
if (!LS.istable(mt)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
LS.rawget(index, mt, "__index");
|
|
|
|
|
if (!LS.istable(index)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
LS.rawget(func, index, "interface");
|
|
|
|
|
if (!LS.isfunction(func)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Call the interface function.
|
|
|
|
|
lua_pushvalue(L, func.index());
|
|
|
|
|
lua_pushvalue(L, actor.index());
|
|
|
|
|
lua_pushvalue(L, place.index());
|
|
|
|
|
Gui::store_global_pointer(L, gui);
|
2022-04-25 17:17:41 -04:00
|
|
|
open_lthread_state(actor_id, place_id, 0, false, false);
|
|
|
|
|
eng::string msg = traceback_pcall(L, 2, 0);
|
2021-10-21 13:15:04 -04:00
|
|
|
close_lthread_state();
|
2021-09-10 17:06:07 -04:00
|
|
|
Gui::store_global_pointer(L, nullptr);
|
2022-04-25 17:17:41 -04:00
|
|
|
if (!msg.empty()) {
|
2021-11-16 13:14:59 -05:00
|
|
|
gui->clear(0);
|
2023-04-07 16:00:52 -04:00
|
|
|
util::dprint(msg);
|
2021-09-10 17:06:07 -04:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-10 17:58:44 -04:00
|
|
|
// This is called from World::update_source, and also
|
|
|
|
|
// from World::patch_source in the difference transmitter.
|
|
|
|
|
//
|
|
|
|
|
// 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
|
|
|
|
|
// up error handling.
|
|
|
|
|
//
|
|
|
|
|
// We also need to figure out a solution for what happens if
|
|
|
|
|
// some lua source file tries to modify, say, tangible state
|
|
|
|
|
// in top-level code.
|
|
|
|
|
//
|
|
|
|
|
void World::rebuild_sourcedb() {
|
|
|
|
|
for (const eng::string &mod: source_db_.modules()) {
|
2023-04-11 16:36:30 -04:00
|
|
|
open_lthread_state(0, 0, 0, false, true);
|
2023-04-10 17:58:44 -04:00
|
|
|
eng::string err = source_db_.rebuild_module(mod);
|
2023-04-11 16:36:30 -04:00
|
|
|
eng::string prints = lthread_prints_->str();
|
|
|
|
|
lthread_prints_.reset();
|
|
|
|
|
close_lthread_state();
|
|
|
|
|
if (!err.empty() || !prints.empty()) {
|
2023-04-14 15:13:59 -04:00
|
|
|
util::dprint("Loading Module ", mod);
|
2023-04-11 16:36:30 -04:00
|
|
|
if (!err.empty()) util::dprint(err);
|
|
|
|
|
if (!prints.empty()) util::dprint(prints);
|
2023-04-10 17:58:44 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-15 14:18:19 -05:00
|
|
|
void World::update_source(const util::LuaSourceVec &source) {
|
|
|
|
|
assert(stack_is_clear());
|
|
|
|
|
source_db_.update(source);
|
2023-04-10 17:58:44 -04:00
|
|
|
rebuild_sourcedb();
|
2021-10-25 14:47:37 -04:00
|
|
|
assert(stack_is_clear());
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-19 19:42:33 -04:00
|
|
|
void World::update_source(const util::LuaSourcePtr &source) {
|
|
|
|
|
if (source != nullptr) {
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-16 17:16:42 -04:00
|
|
|
void World::http_response(const HttpParser &response) {
|
2022-05-03 00:36:11 -04:00
|
|
|
// Find the request.
|
2022-05-06 13:16:27 -04:00
|
|
|
auto iter = http_requests_.find(response.request_id());
|
2022-05-03 00:36:11 -04:00
|
|
|
if (iter == http_requests_.end()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-05-06 13:16:27 -04:00
|
|
|
HttpClientRequest request = iter->second;
|
2022-05-03 00:36:11 -04:00
|
|
|
http_requests_.erase(iter);
|
|
|
|
|
|
2023-04-14 17:50:06 -04:00
|
|
|
lua_State *CO;
|
|
|
|
|
{
|
|
|
|
|
// Get the place and thread as lua objects.
|
|
|
|
|
LuaVar tangibles, place, mt, threads, thinfo, thread;
|
|
|
|
|
LuaExtStack LS(state(), tangibles, place, mt, threads, thinfo, thread);
|
|
|
|
|
LS.rawget(tangibles, LuaRegistry, "tangibles");
|
|
|
|
|
LS.rawget(place, tangibles, request.place_id());
|
|
|
|
|
if (!LS.istable(place)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
LS.getmetatable(mt, place);
|
|
|
|
|
if (!LS.istable(mt)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
LS.rawget(threads, mt, "threads");
|
|
|
|
|
if (!LS.istable(threads)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
LS.rawget(thinfo, threads, request.thread_id());
|
|
|
|
|
if (!LS.istable(thinfo)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
LS.rawget(thread, thinfo, "thread");
|
|
|
|
|
if (!LS.isthread(thread)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
CO = LS.ckthread(thread);
|
2022-05-03 00:36:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Push the response onto the awakening thread.
|
2023-04-14 17:50:06 -04:00
|
|
|
lua_pushnil(CO);
|
|
|
|
|
LuaSpecial responsetable(lua_gettop(CO));
|
|
|
|
|
LuaCoreStack LSCO(CO);
|
2022-05-03 00:36:11 -04:00
|
|
|
response.store(LSCO, responsetable);
|
|
|
|
|
|
|
|
|
|
// Awaken the thread, with its new return value.
|
|
|
|
|
schedule(0, request.thread_id(), request.place_id());
|
|
|
|
|
run_scheduled_threads();
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-16 17:16:42 -04:00
|
|
|
void World::http_responses(const HttpParserVec &responses) {
|
|
|
|
|
for (const HttpParser &response : responses) {
|
2022-05-06 13:16:27 -04:00
|
|
|
http_response(response);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-03 00:36:11 -04:00
|
|
|
void World::abort_all_http_requests(int status_code, std::string_view error) {
|
2022-05-16 17:16:42 -04:00
|
|
|
HttpParser abortresponse;
|
2022-05-03 00:36:11 -04:00
|
|
|
abortresponse.fail(status_code, error);
|
2022-05-06 13:16:27 -04:00
|
|
|
while (!http_requests_.empty()) {
|
|
|
|
|
abortresponse.set_request_id(http_requests_.begin()->first);
|
|
|
|
|
http_response(abortresponse);
|
2022-05-03 00:36:11 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-20 17:12:58 -04:00
|
|
|
HttpServerResponse World::http_serve(const HttpParser &request) {
|
|
|
|
|
assert(stack_is_clear());
|
|
|
|
|
HttpServerResponse response;
|
|
|
|
|
|
|
|
|
|
// We're only supposed to be passed complete requests.
|
|
|
|
|
assert(request.complete());
|
|
|
|
|
|
|
|
|
|
// If the request is HTTP/1.1, then the response should be HTTP/1.1
|
|
|
|
|
response.set_http11(request.http11());
|
|
|
|
|
|
|
|
|
|
// If the incoming request has already been detected to be
|
|
|
|
|
// invalid by the HTTP parser, then just send the error
|
|
|
|
|
// message back to the client without involving lua at all.
|
|
|
|
|
if (request.errstatus()) {
|
|
|
|
|
response.fail(request.status(), request.error());
|
|
|
|
|
return response;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lua_State *L = state();
|
|
|
|
|
LuaVar www, func, reqtab;
|
2023-04-14 17:50:06 -04:00
|
|
|
LuaExtStack LS(L, www, func, reqtab);
|
2022-05-20 17:12:58 -04:00
|
|
|
|
|
|
|
|
// Get the www class. If there's no such class,
|
|
|
|
|
// return a 503 Service Unavailable to the client.
|
|
|
|
|
eng::string err = LS.getclass(www, "www");
|
|
|
|
|
if (!err.empty()) {
|
|
|
|
|
response.fail(503, "class www doesn't exist");
|
|
|
|
|
return response;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-03 15:18:21 -04:00
|
|
|
// Get the name of the desired function.
|
|
|
|
|
std::string_view orig_fn = request.first_path_component("index");
|
|
|
|
|
eng::string lua_fn = HttpParser::to_lua_identifier(orig_fn);
|
|
|
|
|
if (lua_fn.empty()) {
|
|
|
|
|
response.fail(404, util::ss("cannot convert to lua function name: ", orig_fn));
|
|
|
|
|
return response;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-20 17:12:58 -04:00
|
|
|
// Get the closure. If there's no such closure,
|
|
|
|
|
// return a 404 Not Found to the client.
|
2023-07-03 15:18:21 -04:00
|
|
|
LS.rawget(func, www, lua_fn);
|
2022-05-20 17:12:58 -04:00
|
|
|
if (!LS.isfunction(func)) {
|
2023-07-03 15:18:21 -04:00
|
|
|
response.fail(404, util::ss("no such lua function: www.", lua_fn));
|
2022-05-20 17:12:58 -04:00
|
|
|
return response;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Store the request into a lua table.
|
|
|
|
|
request.store(LS, reqtab);
|
|
|
|
|
|
|
|
|
|
// Call the function.
|
|
|
|
|
int oldtop = lua_gettop(L);
|
|
|
|
|
lua_pushvalue(L, func.index());
|
|
|
|
|
lua_pushvalue(L, reqtab.index());
|
|
|
|
|
Gui::store_global_pointer(L, nullptr);
|
|
|
|
|
open_lthread_state(0, 0, 0, false, false);
|
|
|
|
|
eng::string msg = traceback_pcall(L, 1, LUA_MULTRET);
|
|
|
|
|
close_lthread_state();
|
|
|
|
|
|
|
|
|
|
// If the call threw an error, return
|
|
|
|
|
// a 500 Internal Server Error to the client.
|
|
|
|
|
if (!msg.empty()) {
|
|
|
|
|
response.fail(500, msg);
|
|
|
|
|
return response;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If the call didn't return a single table, return
|
|
|
|
|
// a 500 Internal Server Error to the client.
|
|
|
|
|
int newtop = lua_gettop(L);
|
|
|
|
|
if ((newtop != oldtop + 1) || (!lua_istable(L, newtop))) {
|
2023-07-03 15:18:21 -04:00
|
|
|
response.fail(500, util::ss("lua function www.", lua_fn, " didn't return a table"));
|
2022-05-20 17:12:58 -04:00
|
|
|
return response;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Try to convert the table into a response.
|
2022-07-22 17:07:40 -04:00
|
|
|
LuaKeywordParser kp(LS, LuaSpecial(newtop));
|
|
|
|
|
response.configure(kp);
|
2022-05-20 17:12:58 -04:00
|
|
|
response.set_defaults();
|
2022-07-22 17:07:40 -04:00
|
|
|
eng::string kperr = kp.final_check();
|
|
|
|
|
if (!kperr.empty()) {
|
|
|
|
|
response.fail(500, kperr);
|
|
|
|
|
}
|
2022-05-20 17:12:58 -04:00
|
|
|
return response;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-25 14:47:37 -04:00
|
|
|
void World::run_unittests() {
|
|
|
|
|
assert(stack_is_clear());
|
|
|
|
|
source_db_.run_unittests();
|
|
|
|
|
assert(stack_is_clear());
|
2021-10-15 14:47:12 -04:00
|
|
|
}
|
|
|
|
|
|
2021-09-10 17:06:07 -04:00
|
|
|
void World::invoke(const Invocation &inv) {
|
|
|
|
|
switch (inv.kind()) {
|
2023-10-24 00:49:15 -04:00
|
|
|
case Invocation::KIND_CHOOSE:
|
|
|
|
|
invoke_choose(inv.actor(), inv.place(), inv.datapack());
|
|
|
|
|
break;
|
|
|
|
|
case Invocation::KIND_PLAYER:
|
|
|
|
|
invoke_player(inv.actor(), inv.place(), inv.datapack());
|
2021-09-10 17:06:07 -04:00
|
|
|
break;
|
2021-10-15 14:47:12 -04:00
|
|
|
case Invocation::KIND_LUA:
|
2023-10-19 19:42:33 -04:00
|
|
|
invoke_lua(inv.actor(), inv.place(), inv.datapack());
|
2021-10-15 14:47:12 -04:00
|
|
|
break;
|
2021-10-25 14:47:37 -04:00
|
|
|
case Invocation::KIND_FLUSH_PRINTS:
|
2023-10-19 19:42:33 -04:00
|
|
|
invoke_flush_prints(inv.actor(), inv.place(), inv.datapack());
|
2021-10-25 14:47:37 -04:00
|
|
|
break;
|
2021-11-26 15:45:36 -05:00
|
|
|
case Invocation::KIND_TICK:
|
2023-10-19 19:42:33 -04:00
|
|
|
invoke_tick(inv.actor(), inv.place(), inv.datapack());
|
2021-12-15 14:18:19 -05:00
|
|
|
break;
|
|
|
|
|
case Invocation::KIND_LUA_SOURCE:
|
2023-10-19 19:42:33 -04:00
|
|
|
invoke_lua_source(inv.actor(), inv.place(), inv.datapack());
|
2021-12-15 14:18:19 -05:00
|
|
|
break;
|
2021-09-10 17:06:07 -04:00
|
|
|
default:
|
|
|
|
|
// Do nothing. Standard behavior for any invalid command is to
|
|
|
|
|
// simply do nothing at all. Perhaps eventually we may add a flag
|
|
|
|
|
// to the world model to indicate that we've detected an invalid
|
|
|
|
|
// command, to allow us to close the connection to a client that
|
|
|
|
|
// is misbehaving.
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-19 19:42:33 -04:00
|
|
|
void World::invoke_flush_prints(int64_t actor_id, int64_t place_id, std::string_view datapack) {
|
2021-10-25 14:47:37 -04:00
|
|
|
assert(stack_is_clear());
|
|
|
|
|
// Check argument sanity.
|
|
|
|
|
if (actor_id != place_id) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-10-19 19:42:33 -04:00
|
|
|
int64_t line = sv::to_int64(datapack, -1);
|
2022-04-15 17:24:07 -04:00
|
|
|
if ((line < 0)||(line > INT_MAX)) {
|
2021-10-25 14:47:37 -04:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
Tangible *tactor = tangible_get(actor_id);
|
|
|
|
|
if (tactor == nullptr) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-11-14 15:57:18 -05:00
|
|
|
tactor->print_buffer_.discard_upto(line);
|
2021-10-25 14:47:37 -04:00
|
|
|
assert(stack_is_clear());
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-19 19:42:33 -04:00
|
|
|
void World::invoke_lua(int64_t actor_id, int64_t place_id, std::string_view datapack) {
|
2021-10-15 14:47:12 -04:00
|
|
|
assert(stack_is_clear());
|
|
|
|
|
|
2023-03-17 13:31:10 -04:00
|
|
|
// Make sure that actor and place exist and are not stubs.
|
2021-10-15 14:47:12 -04:00
|
|
|
Tangible *tactor = tangible_get(actor_id);
|
2021-11-30 12:39:09 -05:00
|
|
|
Tangible *tplace = tangible_get(place_id);
|
|
|
|
|
if ((tactor == nullptr) || (tplace == nullptr)) {
|
2021-10-15 14:47:12 -04:00
|
|
|
return;
|
2021-10-05 12:54:37 -04:00
|
|
|
}
|
2021-10-15 14:47:12 -04:00
|
|
|
|
2021-11-30 12:39:09 -05:00
|
|
|
// Get a thread ID for the new thread.
|
|
|
|
|
int64_t tid = tplace->id_player_pool_.get_one();
|
|
|
|
|
|
|
|
|
|
// Set up for lua manipulation.
|
2023-04-14 17:50:06 -04:00
|
|
|
{
|
|
|
|
|
lua_State *L = state();
|
|
|
|
|
LuaVar func, tangibles, place, mt, thread, thinfo, threads;
|
|
|
|
|
LuaExtStack LS(L, func, tangibles, place, mt, thread, thinfo, threads);
|
|
|
|
|
|
|
|
|
|
// create the compiled closure.
|
2023-10-19 19:42:33 -04:00
|
|
|
int status = luaL_loadbuffer(L, datapack.data(), datapack.size(), "=invoke");
|
2023-04-14 17:50:06 -04:00
|
|
|
lua_replace(L, func.index());
|
|
|
|
|
if (status != LUA_OK) {
|
|
|
|
|
// The closure is actually an error message. Do nothing.
|
|
|
|
|
// This should normally not happen: LuaConsole should filter
|
|
|
|
|
// out syntax errors.
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-10-15 14:47:12 -04:00
|
|
|
|
2023-04-14 17:50:06 -04:00
|
|
|
// Get the place.
|
|
|
|
|
LS.rawget(tangibles, LuaRegistry, "tangibles");
|
|
|
|
|
LS.rawget(place, tangibles, place_id);
|
|
|
|
|
if (!LS.istable(place)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-10-21 13:15:04 -04:00
|
|
|
|
2023-04-14 17:50:06 -04:00
|
|
|
// Get the place's metatable.
|
|
|
|
|
LS.getmetatable(mt, place);
|
|
|
|
|
if (!LS.istable(mt)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-11-30 12:39:09 -05:00
|
|
|
|
2023-04-14 17:50:06 -04:00
|
|
|
// Create a new thread, set up function and parameters.
|
|
|
|
|
lua_State *CO = LS.newthread(thread);
|
|
|
|
|
lua_pushvalue(L, func.index());
|
|
|
|
|
lua_xmove(L, CO, 1);
|
|
|
|
|
|
|
|
|
|
// Create the thread info table.
|
|
|
|
|
LS.newtable(thinfo);
|
|
|
|
|
LS.rawset(thinfo, "thread", thread);
|
|
|
|
|
LS.rawset(thinfo, "actorid", actor_id);
|
|
|
|
|
LS.rawset(thinfo, "isnew", true);
|
|
|
|
|
LS.rawset(thinfo, "useppool", true);
|
|
|
|
|
LS.rawset(thinfo, "print", true);
|
|
|
|
|
|
|
|
|
|
// Store the thread into place's thread table.
|
|
|
|
|
LS.rawget(threads, mt, "threads");
|
|
|
|
|
if (!LS.istable(threads)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
LS.rawset(threads, tid, thinfo);
|
2021-11-30 12:39:09 -05:00
|
|
|
}
|
|
|
|
|
|
2022-04-27 16:05:45 -04:00
|
|
|
schedule(0, tid, place_id);
|
2021-11-30 12:39:09 -05:00
|
|
|
run_scheduled_threads();
|
2021-10-15 14:47:12 -04:00
|
|
|
assert(stack_is_clear());
|
2021-10-05 12:54:37 -04:00
|
|
|
}
|
|
|
|
|
|
2023-10-24 00:49:15 -04:00
|
|
|
void World::invoke_choose(int64_t actor_id, int64_t place_id, std::string_view datapack) {
|
2021-09-10 17:06:07 -04:00
|
|
|
assert(stack_is_clear());
|
|
|
|
|
|
|
|
|
|
// Validate that the action is legal.
|
|
|
|
|
Gui validation_gui;
|
|
|
|
|
update_gui(actor_id, place_id, &validation_gui);
|
2023-10-19 19:42:33 -04:00
|
|
|
if (!validation_gui.has_action(datapack)) {
|
2021-09-10 17:06:07 -04:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-28 14:07:15 -05:00
|
|
|
// Make sure the action starts with "cb_"
|
2023-10-19 19:42:33 -04:00
|
|
|
if (!sv::has_prefix(datapack, "cb_")) {
|
2021-12-28 14:07:15 -05:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-10 17:06:07 -04:00
|
|
|
// Get the actor and place. Make sure both exist.
|
|
|
|
|
Tangible *tactor = tangible_get(actor_id);
|
|
|
|
|
Tangible *tplace = tangible_get(place_id);
|
|
|
|
|
if ((tactor == nullptr) || (tplace == nullptr)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-14 11:43:16 -04:00
|
|
|
// Get an ID for the thread. We always use the player
|
|
|
|
|
// pool in this case.
|
|
|
|
|
int64_t tid = tactor->id_player_pool_.get_one();
|
2021-09-10 17:06:07 -04:00
|
|
|
|
2023-04-14 17:50:06 -04:00
|
|
|
{
|
|
|
|
|
// Set up for Lua manipulation.
|
|
|
|
|
lua_State *L = state();
|
|
|
|
|
LuaVar actor, place, func, tangibles, mt, index, thread, threads, thinfo, message, invdata;
|
|
|
|
|
LuaExtStack LS(L, actor, place, func, tangibles, mt, index, thread, threads, thinfo, message, invdata);
|
|
|
|
|
|
|
|
|
|
// Get the actor and place.
|
|
|
|
|
LS.rawget(tangibles, LuaRegistry, "tangibles");
|
|
|
|
|
LS.rawget(actor, tangibles, actor_id);
|
|
|
|
|
LS.rawget(place, tangibles, place_id);
|
|
|
|
|
if (!LS.istable(actor) || !LS.istable(place)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-09-10 17:06:07 -04:00
|
|
|
|
2023-04-14 17:50:06 -04:00
|
|
|
// Get the action closure.
|
|
|
|
|
LS.getmetatable(mt, place);
|
|
|
|
|
if (!LS.istable(mt)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
LS.rawget(index, mt, "__index");
|
|
|
|
|
if (!LS.istable(index)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-10-19 19:42:33 -04:00
|
|
|
LS.rawget(func, index, datapack);
|
2023-04-14 17:50:06 -04:00
|
|
|
if (!LS.isfunction(func)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-09-10 17:06:07 -04:00
|
|
|
|
2023-10-19 19:42:33 -04:00
|
|
|
// TODO: maybe add the ability to pass data in as a table?
|
|
|
|
|
// For now the table is always empty.
|
2023-04-14 17:50:06 -04:00
|
|
|
LS.newtable(invdata);
|
2021-09-10 17:06:07 -04:00
|
|
|
|
2023-04-14 17:50:06 -04:00
|
|
|
// Create a new thread, set up function and parameters.
|
|
|
|
|
lua_State *CO = LS.newthread(thread);
|
|
|
|
|
lua_pushvalue(L, func.index());
|
|
|
|
|
lua_pushvalue(L, actor.index());
|
|
|
|
|
lua_pushvalue(L, place.index());
|
|
|
|
|
lua_pushvalue(L, invdata.index());
|
|
|
|
|
lua_xmove(L, CO, 4);
|
|
|
|
|
|
|
|
|
|
// Create the thread info table.
|
|
|
|
|
LS.newtable(thinfo);
|
|
|
|
|
LS.rawset(thinfo, "thread", thread);
|
|
|
|
|
LS.rawset(thinfo, "actorid", actor_id);
|
|
|
|
|
LS.rawset(thinfo, "isnew", true);
|
|
|
|
|
LS.rawset(thinfo, "useppool", true);
|
|
|
|
|
LS.rawset(thinfo, "print", false);
|
|
|
|
|
|
|
|
|
|
// Store the thread into place's thread table.
|
|
|
|
|
LS.rawget(threads, mt, "threads");
|
|
|
|
|
if (!LS.istable(threads)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
LS.rawset(threads, tid, thinfo);
|
2021-09-10 17:06:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Push the thread's ID into the runnable thread queue,
|
|
|
|
|
// then run the thread queue.
|
2022-04-27 16:05:45 -04:00
|
|
|
schedule(0, tid, place_id);
|
2021-11-26 15:45:36 -05:00
|
|
|
run_scheduled_threads();
|
2021-09-10 17:06:07 -04:00
|
|
|
assert(stack_is_clear());
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-24 00:49:15 -04:00
|
|
|
void World::invoke_player(int64_t actor_id, int64_t place_id, std::string_view datapack) {
|
|
|
|
|
assert(stack_is_clear());
|
|
|
|
|
|
|
|
|
|
// Get the actor and place. Make sure both exist.
|
|
|
|
|
Tangible *tactor = tangible_get(actor_id);
|
|
|
|
|
Tangible *tplace = tangible_get(place_id);
|
|
|
|
|
if ((tactor == nullptr) || (tplace == nullptr)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get an ID for the thread. We always use the player
|
|
|
|
|
// pool in this case.
|
|
|
|
|
int64_t tid = tactor->id_player_pool_.get_one();
|
|
|
|
|
|
|
|
|
|
// Use a streambuffer to parse the datapack.
|
|
|
|
|
StreamBuffer datasb(datapack);
|
|
|
|
|
|
|
|
|
|
// Extract the function name from the datapack.
|
|
|
|
|
eng::string funcname;
|
|
|
|
|
try {
|
|
|
|
|
funcname = datasb.read_string_limit(100);
|
|
|
|
|
} catch (const StreamException &ex) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (sv::is_lua_id(funcname)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
// Set up for Lua manipulation.
|
|
|
|
|
lua_State *L = state();
|
|
|
|
|
LuaVar actor, place, func, mt, tangibles, playercb, thread, threads, thinfo, message;
|
|
|
|
|
LuaExtStack LS(L, actor, place, func, mt, tangibles, playercb, thread, threads, thinfo, message);
|
|
|
|
|
|
|
|
|
|
// Get the actor and place.
|
|
|
|
|
LS.rawget(tangibles, LuaRegistry, "tangibles");
|
|
|
|
|
LS.rawget(actor, tangibles, actor_id);
|
|
|
|
|
LS.rawget(place, tangibles, place_id);
|
|
|
|
|
if (!LS.istable(actor) || !LS.istable(place)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get the closure (playerinvoke.funcname).
|
|
|
|
|
eng::string err = LS.getclass(playercb, "playerinvoke");
|
|
|
|
|
if ((!err.empty()) || (!LS.istable(playercb))) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
LS.rawget(func, playercb, funcname);
|
|
|
|
|
if (!LS.isfunction(func)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create a new thread, push func, actor, place.
|
|
|
|
|
int nargs = 3;
|
|
|
|
|
lua_State *CO = LS.newthread(thread);
|
|
|
|
|
lua_pushvalue(L, func.index());
|
|
|
|
|
lua_pushvalue(L, actor.index());
|
|
|
|
|
lua_pushvalue(L, place.index());
|
|
|
|
|
|
|
|
|
|
// Push any additional args from the datapack.
|
|
|
|
|
// TODO: IMPLEMENT ME.
|
|
|
|
|
|
|
|
|
|
// Transfer all arguments to the new thread.
|
|
|
|
|
lua_xmove(L, CO, nargs);
|
|
|
|
|
|
|
|
|
|
// Create the thread info table.
|
|
|
|
|
LS.newtable(thinfo);
|
|
|
|
|
LS.rawset(thinfo, "thread", thread);
|
|
|
|
|
LS.rawset(thinfo, "actorid", actor_id);
|
|
|
|
|
LS.rawset(thinfo, "isnew", true);
|
|
|
|
|
LS.rawset(thinfo, "useppool", true);
|
|
|
|
|
LS.rawset(thinfo, "print", false);
|
|
|
|
|
|
|
|
|
|
// Store the thread into place's thread table.
|
|
|
|
|
LS.getmetatable(mt, place);
|
|
|
|
|
if (!LS.istable(mt)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
LS.rawget(threads, mt, "threads");
|
|
|
|
|
if (!LS.istable(threads)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
LS.rawset(threads, tid, thinfo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Push the thread's ID into the runnable thread queue,
|
|
|
|
|
// then run the thread queue.
|
|
|
|
|
schedule(0, tid, place_id);
|
|
|
|
|
run_scheduled_threads();
|
|
|
|
|
assert(stack_is_clear());
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-19 19:42:33 -04:00
|
|
|
void World::invoke_tick(int64_t actor_id, int64_t place_id, std::string_view datapack) {
|
2022-04-25 17:17:41 -04:00
|
|
|
if (!is_authoritative()) {
|
2021-12-15 14:18:19 -05:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
clock_ += 1;
|
|
|
|
|
run_scheduled_threads();
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-19 19:42:33 -04:00
|
|
|
void World::invoke_lua_source(int64_t actor_id, int64_t place_id, std::string_view datapack) {
|
2022-04-25 17:17:41 -04:00
|
|
|
if (!is_authoritative()) {
|
2021-12-15 14:18:19 -05:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// We need some kind of authentication here.
|
2023-10-19 19:42:33 -04:00
|
|
|
update_source(datapack);
|
2021-11-26 15:45:36 -05:00
|
|
|
}
|
|
|
|
|
|
2022-05-03 00:36:11 -04:00
|
|
|
void World::guard_blockable(lua_State *L, const char *fn) {
|
|
|
|
|
if (lthread_thread_id_ == 0) {
|
2023-03-01 16:07:13 -05:00
|
|
|
// in a probe, blocking functions like http.get throw an error.
|
2022-05-03 00:36:11 -04:00
|
|
|
luaL_error(L, "cannot %s in a probe", fn);
|
|
|
|
|
assert(false);
|
|
|
|
|
}
|
|
|
|
|
if (!is_authoritative()) {
|
2023-03-01 16:07:13 -05:00
|
|
|
// in a nonauth model, blocking functions like http.get are converted to nopredict.
|
2022-05-03 00:36:11 -04:00
|
|
|
lua_yield(L, 0);
|
|
|
|
|
luaL_error(L, "unexplained nopredict failure in %s", fn);
|
|
|
|
|
assert(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void World::guard_nopredict(lua_State *L, const char *fn) {
|
2023-03-01 16:07:13 -05:00
|
|
|
// Caution: this code must be equivalent to the
|
2023-04-14 17:50:06 -04:00
|
|
|
// code in LuaCoreStack::guard_nopredict.
|
2022-05-03 00:36:11 -04:00
|
|
|
if (lthread_thread_id_ == 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!is_authoritative()) {
|
|
|
|
|
lua_yield(L, 0);
|
|
|
|
|
luaL_error(L, "unexplained nopredict failure in %s", fn);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-27 16:05:45 -04:00
|
|
|
void World::schedule(int64_t clk, int64_t thid, int64_t plid) {
|
2022-05-06 15:50:07 -04:00
|
|
|
if (clk > 0) {
|
|
|
|
|
assert(is_authoritative());
|
|
|
|
|
}
|
2022-04-27 16:05:45 -04:00
|
|
|
thread_sched_.add(clk, thid, plid);
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-26 15:45:36 -05:00
|
|
|
void World::run_scheduled_threads() {
|
2021-09-10 17:06:07 -04:00
|
|
|
assert(stack_is_clear());
|
|
|
|
|
lua_State *L = state();
|
2022-04-25 17:17:41 -04:00
|
|
|
LuaVar tangibles, place, mt, threads, thinfo, actorid, isnew, useppool, thread, print;
|
2023-04-14 17:50:06 -04:00
|
|
|
LuaExtStack LS(L, tangibles, place, mt, threads, thinfo, actorid, isnew, useppool, thread, print);
|
2021-09-10 17:06:07 -04:00
|
|
|
|
2021-11-30 12:39:09 -05:00
|
|
|
LS.rawget(tangibles, LuaRegistry, "tangibles");
|
2021-11-26 15:45:36 -05:00
|
|
|
while (thread_sched_.ready(clock_)) {
|
2021-09-10 17:06:07 -04:00
|
|
|
SchedEntry sched = thread_sched_.pop();
|
|
|
|
|
LS.rawget(place, tangibles, sched.place_id());
|
|
|
|
|
if (!LS.istable(place)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
LS.getmetatable(mt, place);
|
|
|
|
|
if (!LS.istable(mt)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
LS.rawget(threads, mt, "threads");
|
|
|
|
|
if (!LS.istable(threads)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2021-10-13 20:32:43 -04:00
|
|
|
LS.rawget(thinfo, threads, sched.thread_id());
|
|
|
|
|
if (!LS.istable(thinfo)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2021-10-14 11:43:16 -04:00
|
|
|
LS.rawget(actorid, thinfo, "actorid");
|
|
|
|
|
if (!LS.isnumber(actorid)) {
|
2021-10-13 20:32:43 -04:00
|
|
|
continue;
|
|
|
|
|
}
|
2022-04-25 17:17:41 -04:00
|
|
|
LS.rawget(isnew, thinfo, "isnew");
|
|
|
|
|
if (!LS.isboolean(isnew)) {
|
2021-10-13 20:32:43 -04:00
|
|
|
continue;
|
|
|
|
|
}
|
2021-10-14 11:43:16 -04:00
|
|
|
LS.rawget(useppool, thinfo, "useppool");
|
|
|
|
|
if (!LS.isboolean(useppool)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2021-10-13 20:32:43 -04:00
|
|
|
LS.rawget(thread, thinfo, "thread");
|
2021-09-10 17:06:07 -04:00
|
|
|
if (!LS.isthread(thread)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Resume the coroutine.
|
|
|
|
|
lua_State *CO = LS.ckthread(thread);
|
2022-04-25 17:17:41 -04:00
|
|
|
open_lthread_state(LS.ckinteger(actorid), sched.place_id(), sched.thread_id(), LS.ckboolean(useppool), true);
|
|
|
|
|
int nargs = LS.ckboolean(isnew) ? (lua_gettop(CO) - 1) : lua_gettop(CO);
|
|
|
|
|
int status = lua_resume(CO, nullptr, nargs);
|
2022-02-25 19:57:23 -05:00
|
|
|
std::ostream *ostream = lthread_print_stream();
|
2021-11-30 12:39:09 -05:00
|
|
|
|
2022-04-25 17:17:41 -04:00
|
|
|
if (status == LUA_OK) {
|
2021-12-14 12:24:25 -05:00
|
|
|
// Successfully ran to completion. Print any return values.
|
|
|
|
|
// Remove from thread table.
|
|
|
|
|
LS.rawget(print, thinfo, "print");
|
2022-04-25 17:17:41 -04:00
|
|
|
LS.rawset(threads, sched.thread_id(), LuaNil);
|
2023-04-14 17:50:06 -04:00
|
|
|
LuaCoreStack LSCO(CO);
|
2021-12-14 12:24:25 -05:00
|
|
|
if (LS.ckboolean(print)) {
|
|
|
|
|
for (int i = 1; i <= lua_gettop(CO); i++) {
|
2023-03-05 01:51:25 -05:00
|
|
|
pprint(LSCO, LuaSpecial(i), PrettyPrintOptions(), ostream);
|
2021-12-15 23:03:43 -05:00
|
|
|
(*ostream) << std::endl;
|
2021-12-14 12:24:25 -05:00
|
|
|
}
|
|
|
|
|
}
|
2022-04-25 17:17:41 -04:00
|
|
|
} else if (status == LUA_YIELD) {
|
|
|
|
|
if (is_authoritative()) {
|
|
|
|
|
LS.rawset(thinfo, "isnew", false);
|
|
|
|
|
LS.rawset(thinfo, "useppool", false);
|
|
|
|
|
} else {
|
2023-03-01 16:07:13 -05:00
|
|
|
// When a nonauthoritative model yields, for any reason,
|
|
|
|
|
// the thread is discarded. This is also used as a way to implement
|
|
|
|
|
// nopredict: the thread that wants to 'nopredict' just yields,
|
|
|
|
|
// knowing that this will cause it to be killed.
|
2022-04-25 17:17:41 -04:00
|
|
|
LS.rawset(threads, sched.thread_id(), LuaNil);
|
|
|
|
|
}
|
2021-09-10 17:06:07 -04:00
|
|
|
} else {
|
|
|
|
|
// Generated an error. Add a traceback, print, and kill the coroutine.
|
2021-11-30 12:39:09 -05:00
|
|
|
// Currently, the error is sent to the actor. That seems... not right in the long run.
|
2022-04-25 17:17:41 -04:00
|
|
|
if (is_authoritative()) {
|
|
|
|
|
traceback_coroutine(CO);
|
|
|
|
|
(*ostream) << lua_tostring(CO, -1);
|
|
|
|
|
}
|
2021-09-10 17:06:07 -04:00
|
|
|
LS.rawset(threads, sched.thread_id(), LuaNil);
|
|
|
|
|
}
|
2021-11-30 12:39:09 -05:00
|
|
|
close_lthread_state();
|
2021-09-10 17:06:07 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-14 11:43:16 -04:00
|
|
|
int64_t World::alloc_id_predictable() {
|
|
|
|
|
if (!lthread_use_ppool_) {
|
|
|
|
|
return id_global_pool_.get_one();
|
|
|
|
|
}
|
|
|
|
|
Tangible *t = tangible_get(lthread_actor_id_);
|
|
|
|
|
if (t == nullptr) {
|
|
|
|
|
return id_global_pool_.get_one();
|
|
|
|
|
}
|
|
|
|
|
return t->id_player_pool_.get_one();
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-21 13:15:04 -04:00
|
|
|
const PrintBuffer *World::get_printbuffer(int64_t actor_id) {
|
|
|
|
|
Tangible *actor = tangible_get(actor_id);
|
|
|
|
|
if (actor != nullptr) {
|
2021-11-14 15:57:18 -05:00
|
|
|
return &actor->print_buffer_;
|
2021-10-21 13:15:04 -04:00
|
|
|
}
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void World::clear_lthread_state() {
|
|
|
|
|
lthread_prints_.reset();
|
|
|
|
|
lthread_actor_id_ = 0;
|
|
|
|
|
lthread_place_id_ = 0;
|
2022-04-25 17:17:41 -04:00
|
|
|
lthread_thread_id_ = 0;
|
2021-10-21 13:15:04 -04:00
|
|
|
lthread_use_ppool_ = false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-25 17:17:41 -04:00
|
|
|
void World::open_lthread_state(int64_t actor, int64_t place, int64_t thread, bool ppool, bool prints) {
|
2021-10-14 11:43:16 -04:00
|
|
|
lthread_actor_id_ = actor;
|
|
|
|
|
lthread_place_id_ = place;
|
2022-04-25 17:17:41 -04:00
|
|
|
lthread_thread_id_ = thread;
|
2021-10-14 11:43:16 -04:00
|
|
|
lthread_use_ppool_ = ppool;
|
2021-10-21 13:15:04 -04:00
|
|
|
if (prints) {
|
2022-02-24 02:17:41 -05:00
|
|
|
lthread_prints_.reset(new eng::ostringstream);
|
2021-10-21 13:15:04 -04:00
|
|
|
} else {
|
|
|
|
|
lthread_prints_.reset();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void World::close_lthread_state() {
|
|
|
|
|
// Copy prints from lthread_prints_ stringstream into
|
|
|
|
|
// the appropriate actor's PrintBuffer. If for some reason
|
|
|
|
|
// there isn't an actor, or if the actor doesn't have a PrintBuffer,
|
|
|
|
|
// send the output to std::cerr.
|
|
|
|
|
if (lthread_prints_ != nullptr) {
|
2022-02-24 02:17:41 -05:00
|
|
|
const eng::string &output = lthread_prints_->str();
|
2021-10-21 13:15:04 -04:00
|
|
|
Tangible *actor = tangible_get(lthread_actor_id_);
|
|
|
|
|
if (actor != nullptr) {
|
2022-04-25 17:17:41 -04:00
|
|
|
actor->print_buffer_.add_string(output, is_authoritative());
|
2021-10-21 13:15:04 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Now clean up everything.
|
|
|
|
|
clear_lthread_state();
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-25 19:57:23 -05:00
|
|
|
std::ostream *World::lthread_print_stream() const {
|
2021-10-21 13:15:04 -04:00
|
|
|
if (lthread_prints_ != nullptr) {
|
|
|
|
|
return lthread_prints_.get();
|
|
|
|
|
} else {
|
|
|
|
|
return &std::cerr;
|
|
|
|
|
}
|
2021-10-14 11:43:16 -04:00
|
|
|
}
|
|
|
|
|
|
2023-04-10 16:00:47 -04:00
|
|
|
void World::set_global(LuaCoreStack &LS0, const eng::string &gvar, LuaSlot value) {
|
|
|
|
|
lua_State *L = LS0.state();
|
|
|
|
|
LuaVar globaldb, copy;
|
|
|
|
|
LuaExtStack LS(L, globaldb, copy);
|
|
|
|
|
|
|
|
|
|
// Serialize then deserialize the data, to produce a copy.
|
|
|
|
|
StreamBuffer sb;
|
|
|
|
|
eng::string error = serialize_lua(LS, value, &sb);
|
|
|
|
|
if (!error.empty()) {
|
|
|
|
|
luaL_error(L, "%s", error.c_str());
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
eng::string serialized(sb.view());
|
|
|
|
|
error = deserialize_lua(LS, copy, &sb);
|
|
|
|
|
if (!error.empty()) {
|
|
|
|
|
luaL_error(L, "%s", error.c_str());
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Store the copy in the globalDB.
|
|
|
|
|
LS.rawget(globaldb, LuaRegistry, "globaldb");
|
|
|
|
|
LS.rawset(globaldb, gvar, copy);
|
|
|
|
|
|
|
|
|
|
// Store the serialized blob.
|
|
|
|
|
gvname_to_serial_[gvar] = serialized;
|
|
|
|
|
|
|
|
|
|
// Implement the tracking so that we can rapidly determine which global
|
|
|
|
|
// variables need to be difference transmitted.
|
|
|
|
|
//
|
|
|
|
|
// In the master model, we generate a sequence number for the assignment.
|
|
|
|
|
// We store the mapping from global variable name to that sequence number
|
|
|
|
|
// and vice versa.
|
|
|
|
|
//
|
|
|
|
|
// On the client side, we just record the global variable in a list
|
|
|
|
|
// of recently modified globals.
|
|
|
|
|
//
|
|
|
|
|
if (is_authoritative()) {
|
|
|
|
|
int64_t &seqno = gvname_to_seqno_[gvar];
|
|
|
|
|
seqno_to_gvname_.erase(seqno);
|
|
|
|
|
seqno = assign_seqno_++;
|
|
|
|
|
seqno_to_gvname_[seqno] = gvar;
|
|
|
|
|
} else {
|
|
|
|
|
gvname_modified_.insert(gvar);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-09-10 17:06:07 -04:00
|
|
|
void World::serialize(StreamBuffer *sb) {
|
|
|
|
|
assert(stack_is_clear());
|
|
|
|
|
assert(redirects_.empty());
|
|
|
|
|
// int64_t wc0 = sb->total_writes();
|
|
|
|
|
lua_snap_.serialize(sb);
|
|
|
|
|
id_global_pool_.serialize(sb);
|
2021-11-26 15:45:36 -05:00
|
|
|
sb->write_int64(clock_);
|
2021-09-10 17:06:07 -04:00
|
|
|
thread_sched_.serialize(sb);
|
2022-05-03 00:36:11 -04:00
|
|
|
http_requests_.serialize(sb);
|
2021-09-10 17:06:07 -04:00
|
|
|
sb->write_uint32(tangibles_.size());
|
|
|
|
|
for (const auto &p : tangibles_) {
|
|
|
|
|
sb->write_int64(p.first);
|
|
|
|
|
p.second->serialize(sb);
|
|
|
|
|
}
|
|
|
|
|
assert(stack_is_clear());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void World::deserialize(StreamBuffer *sb) {
|
|
|
|
|
assert(stack_is_clear());
|
|
|
|
|
redirects_.clear();
|
|
|
|
|
lua_snap_.deserialize(sb);
|
|
|
|
|
id_global_pool_.deserialize(sb);
|
2021-11-26 15:45:36 -05:00
|
|
|
clock_ = sb->read_int64();
|
2021-09-10 17:06:07 -04:00
|
|
|
thread_sched_.deserialize(sb);
|
2022-05-03 00:36:11 -04:00
|
|
|
http_requests_.deserialize(sb);
|
2021-09-10 17:06:07 -04:00
|
|
|
// Mark all tangibles for deletion by setting ID to zero.
|
|
|
|
|
for (const auto &p : tangibles_) {
|
|
|
|
|
p.second->plane_item_.set_id(0);
|
|
|
|
|
}
|
|
|
|
|
// Deserialize tangibles.
|
|
|
|
|
size_t ntan = sb->read_uint32();
|
|
|
|
|
for (size_t i = 0; i < ntan; i++) {
|
|
|
|
|
int64_t id = sb->read_int64();
|
2021-10-15 13:51:32 -04:00
|
|
|
UniqueTangible &t = tangibles_[id];
|
2021-09-10 17:06:07 -04:00
|
|
|
if (t == nullptr) {
|
|
|
|
|
t.reset(new Tangible(this, id));
|
|
|
|
|
} else {
|
|
|
|
|
t->plane_item_.set_id(id);
|
|
|
|
|
}
|
|
|
|
|
t->deserialize(sb);
|
|
|
|
|
}
|
|
|
|
|
// Delete tangibles that didn't get deserialized.
|
|
|
|
|
for (auto iter = tangibles_.begin(); iter != tangibles_.end(); ) {
|
|
|
|
|
if (iter->second->plane_item_.id() == 0) {
|
|
|
|
|
tangibles_.erase(iter++);
|
|
|
|
|
} else {
|
|
|
|
|
++iter;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-05-03 00:36:11 -04:00
|
|
|
// After a save and load, http requests no longer should exist
|
|
|
|
|
abort_all_http_requests(425, "http requests aborted by loading a save game");
|
2021-09-10 17:06:07 -04:00
|
|
|
assert(stack_is_clear());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void World::snapshot() {
|
2021-11-09 16:27:39 -05:00
|
|
|
assert(snapshot_.empty());
|
2021-09-10 17:06:07 -04:00
|
|
|
serialize(&snapshot_);
|
2021-11-09 16:27:39 -05:00
|
|
|
assert(!snapshot_.empty());
|
2021-09-10 17:06:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void World::rollback() {
|
2021-10-07 14:58:20 -04:00
|
|
|
assert(!snapshot_.empty());
|
2021-09-10 17:06:07 -04:00
|
|
|
deserialize(&snapshot_);
|
2021-11-09 16:27:39 -05:00
|
|
|
assert(snapshot_.empty());
|
2021-09-10 17:06:07 -04:00
|
|
|
}
|
|
|
|
|
|
2023-02-14 13:14:18 -05:00
|
|
|
// This is the main routine for the DLL. We have to use a registration device
|
|
|
|
|
// to register this main routine with DrivenEngine. DrivenEngine will then call
|
|
|
|
|
// it exactly once the first time that the driver initializes an EngineWrapper.
|
|
|
|
|
//
|
|
|
|
|
void engine_initialization() {
|
|
|
|
|
SourceDB::register_lua_builtins();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static DrivenEngineInitializerReg eireg(engine_initialization);
|