Simplified module state serialization: uses now a templated function for both reading and writing

master
Martin Felis 2017-01-09 21:24:20 +01:00
parent 84efe9b9aa
commit 546fe64c6f
5 changed files with 48 additions and 33 deletions

View File

@ -18,12 +18,12 @@ struct module_api {
/** /**
* Called exactly once when the module code is reloaded. * Called exactly once when the module code is reloaded.
*/ */
void (*reload)(struct module_state *state); void (*reload)(struct module_state *state, void* read_serializer);
/** /**
* Called exactly once when the module code is about to be reloaded. * Called exactly once when the module code is about to be reloaded.
*/ */
void (*unload)(struct module_state *state); void (*unload)(struct module_state *state, void* write_serializer);
/** /**
* Called at a regular interval by the main program. * Called at a regular interval by the main program.

View File

@ -65,7 +65,7 @@ void RuntimeModuleManager::LoadModule(RuntimeModule* module) {
module->state = module->api.init(); module->state = module->api.init();
} }
std::cout << "Reloading module " << module->name << std::endl; std::cout << "Reloading module " << module->name << std::endl;
module->api.reload(module->state); module->api.reload(module->state, gReadSerializer);
} else { } else {
std::cerr << "Error: could not find API for module " << module->name << std::endl; std::cerr << "Error: could not find API for module " << module->name << std::endl;
dlclose(module->handle); dlclose(module->handle);
@ -123,7 +123,7 @@ void RuntimeModuleManager::UnloadModules() {
for (int i = mModules.size() - 1; i >= 0 ; i--) { for (int i = mModules.size() - 1; i >= 0 ; i--) {
if (mModules[i]->handle) { if (mModules[i]->handle) {
mModules[i]->api.unload(mModules[i]->state); mModules[i]->api.unload(mModules[i]->state, gWriteSerializer);
mModules[i]->state = nullptr; mModules[i]->state = nullptr;
dlclose(mModules[i]->handle); dlclose(mModules[i]->handle);
mModules[i]->handle = 0; mModules[i]->handle = 0;

View File

@ -79,7 +79,6 @@ struct ReadSerializer {
std::ifstream stream(filename, std::ios::binary); std::ifstream stream(filename, std::ios::binary);
size_t key_size; size_t key_size;
size_t block_size; size_t block_size;

View File

@ -59,6 +59,21 @@ static struct module_state *module_init() {
return state; return state;
} }
template <typename Serializer>
static void module_serialize (
struct module_state *state,
Serializer* serializer) {
// get the state from the serializer
Camera* camera = &gRenderer->cameras[gRenderer->activeCameraIndex];
assert (camera != nullptr);
SerializeBool (*serializer, "protot.RenderModule.draw_floor", gRenderer->drawFloor);
SerializeBool (*serializer, "protot.RenderModule.draw_skybox", gRenderer->drawSkybox);
SerializeBool (*serializer, "protot.RenderModule.debug_enabled", gRenderer->drawDebug);
SerializeVec3 (*serializer, "protot.RenderModule.camera.eye", camera->eye);
SerializeVec3 (*serializer, "protot.RenderModule.camera.poi", camera->poi);
}
static void module_finalize(struct module_state *state) { static void module_finalize(struct module_state *state) {
std::cout << "RenderModule finalize called" << std::endl; std::cout << "RenderModule finalize called" << std::endl;
@ -68,7 +83,7 @@ static void module_finalize(struct module_state *state) {
free(state); free(state);
} }
static void module_reload(struct module_state *state) { static void module_reload(struct module_state *state, void *read_serializer) {
std::cout << "RenderModule reload called" << std::endl; std::cout << "RenderModule reload called" << std::endl;
assert (gWindow != nullptr); assert (gWindow != nullptr);
int width, height; int width, height;
@ -79,30 +94,23 @@ static void module_reload(struct module_state *state) {
state->renderer->initialize(width, height); state->renderer->initialize(width, height);
gRenderer = state->renderer; gRenderer = state->renderer;
// load the state of the module
if (read_serializer != nullptr) {
module_serialize(state, static_cast<ReadSerializer*>(read_serializer));
}
// get the state from the serializer // get the state from the serializer
Camera* camera = &gRenderer->cameras[gRenderer->activeCameraIndex]; Camera* camera = &gRenderer->cameras[gRenderer->activeCameraIndex];
assert (camera != nullptr); assert (camera != nullptr);
SerializeBool (*gReadSerializer, "protot.RenderModule.draw_floor", gRenderer->drawFloor);
SerializeBool (*gReadSerializer, "protot.RenderModule.draw_skybox", gRenderer->drawSkybox);
SerializeBool (*gReadSerializer, "protot.RenderModule.debug_enabled", gRenderer->drawDebug);
SerializeVec3 (*gReadSerializer, "protot.RenderModule.camera.eye", camera->eye);
SerializeVec3 (*gReadSerializer, "protot.RenderModule.camera.poi", camera->poi);
camera->updateMatrices(); camera->updateMatrices();
} }
static void module_unload(struct module_state *state) { static void module_unload(struct module_state *state, void* write_serializer) {
Camera* camera = &gRenderer->cameras[gRenderer->activeCameraIndex]; // serialize the state of the module
if (write_serializer != nullptr) {
//(*gSerializer)["protot"]["RenderModule"]["active_camera"] = (double)gRenderer->activeCameraIndex; module_serialize(state, static_cast<WriteSerializer*>(write_serializer));
}
SerializeBool (*gWriteSerializer, "protot.RenderModule.draw_floor", gRenderer->drawFloor);
SerializeBool (*gWriteSerializer, "protot.RenderModule.draw_skybox", gRenderer->drawSkybox);
SerializeBool (*gWriteSerializer, "protot.RenderModule.debug_enabled", gRenderer->drawDebug);
SerializeVec3 (*gWriteSerializer, "protot.RenderModule.camera.eye", camera->eye);
SerializeVec3 (*gWriteSerializer, "protot.RenderModule.camera.poi", camera->poi);
gRenderer = nullptr; gRenderer = nullptr;
state->renderer->shutdown(); state->renderer->shutdown();

View File

@ -280,12 +280,22 @@ static struct module_state *module_init() {
return state; return state;
} }
template <typename Serializer>
static void module_serialize (
struct module_state *state,
Serializer* serializer) {
SerializeVec3(*serializer, "protot.TestModule.entity.position", state->character->position);
SerializeBool(*serializer, "protot.TestModule.character_window.visible", state->character_properties_window_visible);
SerializeBool(*serializer, "protot.TestModule.modules_window.visible", state->modules_window_visible);
SerializeInt(*serializer, "protot.TestModule.modules_window.selection_index", state->modules_window_selected_index);
}
static void module_finalize(struct module_state *state) { static void module_finalize(struct module_state *state) {
std::cout << "Module finalize called" << std::endl; std::cout << "Module finalize called" << std::endl;
free(state); free(state);
} }
static void module_reload(struct module_state *state) { static void module_reload(struct module_state *state, void* read_serializer) {
std::cout << "Module reload called. State: " << state << std::endl; std::cout << "Module reload called. State: " << state << std::endl;
// reset mouse scrolling state // reset mouse scrolling state
@ -323,22 +333,20 @@ static void module_reload(struct module_state *state) {
cout << "Creating render entity mesh ... success!" << endl; cout << "Creating render entity mesh ... success!" << endl;
// load the state of the entity // load the state of the entity
SerializeVec3(*gReadSerializer, "protot.TestModule.entity.position", state->character->position); if (read_serializer != nullptr) {
SerializeBool(*gReadSerializer, "protot.TestModule.character_window.visible", state->character_properties_window_visible); module_serialize(state, static_cast<ReadSerializer*>(read_serializer));
SerializeBool(*gReadSerializer, "protot.TestModule.modules_window.visible", state->modules_window_visible); }
SerializeInt(*gReadSerializer, "protot.TestModule.modules_window.selection_index", state->modules_window_selected_index);
glfwSetScrollCallback (gWindow, mouse_scroll_callback); glfwSetScrollCallback (gWindow, mouse_scroll_callback);
} }
static void module_unload(struct module_state *state) { static void module_unload(struct module_state *state, void* write_serializer) {
glfwSetScrollCallback (gWindow, nullptr); glfwSetScrollCallback (gWindow, nullptr);
// serialize the state of the entity // serialize the state of the entity
SerializeVec3(*gWriteSerializer, "protot.TestModule.entity.position", state->character->position); if (write_serializer != nullptr) {
SerializeBool(*gWriteSerializer, "protot.TestModule.character_window.visible", state->character_properties_window_visible); module_serialize(state, static_cast<WriteSerializer*>(write_serializer));
SerializeBool(*gWriteSerializer, "protot.TestModule.modules_window.visible", state->modules_window_visible); }
SerializeInt(*gWriteSerializer, "protot.TestModule.modules_window.selection_index", state->modules_window_selected_index);
// clean up // clean up
cout << "destroying render entity " << state->character->entity << endl; cout << "destroying render entity " << state->character->entity << endl;