reduce redundancy in scripting.cpp
This commit is contained in:
parent
bcbec562ac
commit
99094d2299
@ -281,11 +281,15 @@ int scripting::get_values_on_stack() {
|
|||||||
return lua::gettop(lua::get_main_thread());
|
return lua::gettop(lua::get_main_thread());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void load_script(int env, const std::string& type, const fs::path& file) {
|
||||||
|
std::string src = files::read_string(file);
|
||||||
|
logger.info() << "script (" << type << ") " << file.u8string();
|
||||||
|
lua::execute(lua::get_main_thread(), env, src, file.u8string());
|
||||||
|
}
|
||||||
|
|
||||||
void scripting::load_block_script(const scriptenv& senv, const std::string& prefix, const fs::path& file, block_funcs_set& funcsset) {
|
void scripting::load_block_script(const scriptenv& senv, const std::string& prefix, const fs::path& file, block_funcs_set& funcsset) {
|
||||||
int env = *senv;
|
int env = *senv;
|
||||||
std::string src = files::read_string(file);
|
load_script(env, "block", file);
|
||||||
logger.info() << "script (block) " << file.u8string();
|
|
||||||
lua::execute(lua::get_main_thread(), env, src, file.u8string());
|
|
||||||
funcsset.init = register_event(env, "init", prefix+".init");
|
funcsset.init = register_event(env, "init", prefix+".init");
|
||||||
funcsset.update = register_event(env, "on_update", prefix+".update");
|
funcsset.update = register_event(env, "on_update", prefix+".update");
|
||||||
funcsset.randupdate = register_event(env, "on_random_update", prefix+".randupdate");
|
funcsset.randupdate = register_event(env, "on_random_update", prefix+".randupdate");
|
||||||
@ -297,10 +301,7 @@ void scripting::load_block_script(const scriptenv& senv, const std::string& pref
|
|||||||
|
|
||||||
void scripting::load_item_script(const scriptenv& senv, const std::string& prefix, const fs::path& file, item_funcs_set& funcsset) {
|
void scripting::load_item_script(const scriptenv& senv, const std::string& prefix, const fs::path& file, item_funcs_set& funcsset) {
|
||||||
int env = *senv;
|
int env = *senv;
|
||||||
std::string src = files::read_string(file);
|
load_script(env, "item", file);
|
||||||
logger.info() << "script (item) " << file.u8string();
|
|
||||||
lua::execute(lua::get_main_thread(), env, src, file.u8string());
|
|
||||||
|
|
||||||
funcsset.init = register_event(env, "init", prefix+".init");
|
funcsset.init = register_event(env, "init", prefix+".init");
|
||||||
funcsset.on_use = register_event(env, "on_use", prefix+".use");
|
funcsset.on_use = register_event(env, "on_use", prefix+".use");
|
||||||
funcsset.on_use_on_block = register_event(env, "on_use_on_block", prefix+".useon");
|
funcsset.on_use_on_block = register_event(env, "on_use_on_block", prefix+".useon");
|
||||||
@ -309,11 +310,7 @@ void scripting::load_item_script(const scriptenv& senv, const std::string& prefi
|
|||||||
|
|
||||||
void scripting::load_world_script(const scriptenv& senv, const std::string& prefix, const fs::path& file) {
|
void scripting::load_world_script(const scriptenv& senv, const std::string& prefix, const fs::path& file) {
|
||||||
int env = *senv;
|
int env = *senv;
|
||||||
|
load_script(env, "world", file);
|
||||||
std::string src = files::read_string(file);
|
|
||||||
logger.info() << "loading world script for " << prefix;
|
|
||||||
lua::execute(lua::get_main_thread(), env, src, file.u8string());
|
|
||||||
|
|
||||||
register_event(env, "init", prefix+".init");
|
register_event(env, "init", prefix+".init");
|
||||||
register_event(env, "on_world_open", prefix+".worldopen");
|
register_event(env, "on_world_open", prefix+".worldopen");
|
||||||
register_event(env, "on_world_tick", prefix+".worldtick");
|
register_event(env, "on_world_tick", prefix+".worldtick");
|
||||||
@ -323,11 +320,7 @@ void scripting::load_world_script(const scriptenv& senv, const std::string& pref
|
|||||||
|
|
||||||
void scripting::load_layout_script(const scriptenv& senv, const std::string& prefix, const fs::path& file, uidocscript& script) {
|
void scripting::load_layout_script(const scriptenv& senv, const std::string& prefix, const fs::path& file, uidocscript& script) {
|
||||||
int env = *senv;
|
int env = *senv;
|
||||||
|
load_script(env, "layout", file);
|
||||||
std::string src = files::read_string(file);
|
|
||||||
logger.info() << "loading script " << file.u8string();
|
|
||||||
|
|
||||||
lua::execute(lua::get_main_thread(), env, src, file.u8string());
|
|
||||||
script.onopen = register_event(env, "on_open", prefix+".open");
|
script.onopen = register_event(env, "on_open", prefix+".open");
|
||||||
script.onprogress = register_event(env, "on_progress", prefix+".progress");
|
script.onprogress = register_event(env, "on_progress", prefix+".progress");
|
||||||
script.onclose = register_event(env, "on_close", prefix+".close");
|
script.onclose = register_event(env, "on_close", prefix+".close");
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user