Skip to content

Commit

Permalink
Lua Async Done!
Browse files Browse the repository at this point in the history
  • Loading branch information
megumumpkin committed Aug 13, 2022
1 parent d012afa commit 85376b7
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 47 deletions.
19 changes: 1 addition & 18 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -106,21 +106,4 @@ add_custom_command(
COMMAND ${CMAKE_COMMAND} -E copy_if_different "${WickedEngine_DIR}/../../WickedEngine/shaders/*" "${CMAKE_CURRENT_BINARY_DIR}/Data/Shader/"
COMMAND ${CMAKE_COMMAND} -E copy_if_different "${WickedEngine_DIR}/../../WickedEngine/shaders/ffx-fsr/*" "${CMAKE_CURRENT_BINARY_DIR}/Data/Shader/ffx-fsr"
COMMAND ${CMAKE_COMMAND} -E copy_if_different "${WickedEngine_DIR}/../../WickedEngine/shaders/ffx-shadows-dnsr/*" "${CMAKE_CURRENT_BINARY_DIR}/Data/Shader/ffx-shadows-dnsr"
)

# Sync back and forth scripts
if(EXISTS "build/startup.lua")
add_custom_command(
TARGET Game POST_BUILD
# Copy back scripts
COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_CURRENT_BINARY_DIR}/startup.lua" "${CMAKE_CURRENT_SOURCE_DIR}/Source/Scripts/Root/startup.lua"
COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_CURRENT_BINARY_DIR}/editor.lua" "${CMAKE_CURRENT_SOURCE_DIR}/Source/Scripts/Root/editor.lua"
)
else()
add_custom_command(
TARGET Game POST_BUILD
# Copy forth scripts
COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_CURRENT_SOURCE_DIR}/Source/Scripts/Root/startup.lua" "${CMAKE_CURRENT_BINARY_DIR}/startup.lua"
COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_CURRENT_SOURCE_DIR}/Source/Scripts/Root/editor.lua" "${CMAKE_CURRENT_BINARY_DIR}/editor.lua"
)
endif()
)
31 changes: 17 additions & 14 deletions Source/BindLua.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
#include "WickedEngine.h"
#include <LUA/lua.h>
#include <filesystem>
#include <memory>
#include <string>
#include <wiArchive.h>

#if IS_DEV
#include "ImGui/imgui_BindLua.h"
Expand Down Expand Up @@ -74,8 +76,8 @@ namespace Game::ScriptBindings{
wi::jobsystem::context script_sys_jobs;
std::mutex script_sys_mutex;

wi::unordered_map<std::string, std::function<void(std::string,wi::Archive)>> async_callback_solvers;
wi::unordered_map<std::string, wi::Archive> async_callbacks;
wi::unordered_map<std::string, std::function<void(std::string,std::shared_ptr<wi::Archive>)>> async_callback_solvers;
wi::unordered_map<std::string, std::shared_ptr<wi::Archive>> async_callbacks;

int Internal_DoFile(lua_State* L)
{
Expand Down Expand Up @@ -141,7 +143,6 @@ namespace Game::ScriptBindings{
std::stringstream ss(types);
while(std::getline(ss, token, ';')){
type_list.push_back(token);
wi::backlog::post(token);
}
}
std::string TID = "FILEDIALOG";
Expand All @@ -155,12 +156,14 @@ namespace Game::ScriptBindings{
wi::eventhandler::Subscribe_Once(wi::eventhandler::EVENT_THREAD_SAFE_POINT, [=](uint64_t userdata){
std::scoped_lock lock (script_sys_mutex);

wi::Archive callback_data;
std::shared_ptr<wi::Archive> callback_data_ptr = std::make_shared<wi::Archive>();
auto& callback_data = *callback_data_ptr;
callback_data.SetReadModeAndResetPos(false);
callback_data << "filedialog";
std::string callback_type = "filedialog";
callback_data << callback_type;
callback_data << fileName;

Push_AsyncCallback(TID, callback_data);
Push_AsyncCallback(TID, callback_data_ptr);
});
});
});
Expand Down Expand Up @@ -192,7 +195,8 @@ namespace Game::ScriptBindings{
wi::lua::RegisterFunc("dofile", Internal_DoFile);
#if IS_DEV
wi::lua::RegisterFunc("filedialog", Internal_FileDialog);
Register_AsyncCallback("filedialog",[=](std::string tid, wi::Archive archive){
Register_AsyncCallback("filedialog",[=](std::string tid, std::shared_ptr<wi::Archive> archive_ptr){
auto& archive = *archive_ptr;
std::string filepath;
archive >> filepath;
auto L = wi::lua::GetLuaState();
Expand All @@ -214,29 +218,28 @@ namespace Game::ScriptBindings{
// Updates stuff which needs synchronization from Lua
void Update(float dt){
// Updates all async callbacks from lua here
/*
for(auto& callback : async_callbacks){
auto& UID = callback.first;
auto& archive = callback.second;
auto& archive = *callback.second;
auto archive_ptr = callback.second;
archive.SetReadModeAndResetPos(true);

std::string callback_solver_type;
archive >> callback_solver_type;

auto find_callback = async_callback_solvers.find(callback_solver_type);
if(find_callback != async_callback_solvers.end()){
find_callback->second(UID,archive);
find_callback->second(UID,archive_ptr);
}
}
async_callbacks.clear();
*/
async_callbacks.clear();
}

void Register_AsyncCallback(std::string callback_type, std::function<void(std::string, wi::Archive)> callback_solver){
void Register_AsyncCallback(std::string callback_type, std::function<void(std::string, std::shared_ptr<wi::Archive>)> callback_solver){
async_callback_solvers[callback_type] = callback_solver;
}

void Push_AsyncCallback(std::string callback_UID, wi::Archive& async_data){
void Push_AsyncCallback(std::string callback_UID, std::shared_ptr<wi::Archive> async_data){
async_callbacks[callback_UID] = async_data;
}
}
Expand Down
4 changes: 2 additions & 2 deletions Source/BindLua.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
namespace Game::ScriptBindings{
void Init();
void Update(float dt);
void Register_AsyncCallback(std::string callback_type, std::function<void(std::string, wi::Archive)>);
void Push_AsyncCallback(std::string callback_UID, wi::Archive& async_data);
void Register_AsyncCallback(std::string callback_type, std::function<void(std::string, std::shared_ptr<wi::Archive>)>);
void Push_AsyncCallback(std::string callback_UID, std::shared_ptr<wi::Archive> async_data);
namespace LiveUpdate{
struct ScriptReloadEvent{
enum MODE{
Expand Down
28 changes: 15 additions & 13 deletions Source/Resources_BindLua.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "BindLua.h"
#include "Resources.h"
#include "Resources_BindLua.h"
#include <memory>
#include <wiArchive.h>
#include <wiLua.h>
#include <wiScene_BindLua.h>
#include <set>
Expand Down Expand Up @@ -30,15 +32,16 @@ namespace Game::ScriptBindings::Resources{
wi::lua::RunText("Resources.LibraryFlags.COMPONENT_FILTER_USE_LAYER_TRANSFORM = "+std::to_string(Game::Resources::Library::COMPONENT_FILTER_USE_LAYER_TRANSFORM));
wi::lua::RunText("Resources.LibraryFlags.COMPONENT_FILTER_REMOVE_CONFIG_AFTER = "+std::to_string(Game::Resources::Library::COMPONENT_FILTER_REMOVE_CONFIG_AFTER));

// Game::ScriptBindings::Register_AsyncCallback("asyncload",[=](std::string tid, wi::Archive archive){
// uint32_t instance_uuid;
// archive >> instance_uuid;
// auto L = wi::lua::GetLuaState();
// lua_getglobal(L, "asyncload_setresult");
// lua_pushstring(L, tid.c_str());
// lua_pushnumber(L, instance_uuid);
// lua_call(L,3,0);
// });
Game::ScriptBindings::Register_AsyncCallback("asyncload",[=](std::string tid, std::shared_ptr<wi::Archive> archive_ptr){
auto& archive = *archive_ptr;
uint32_t instance_uuid;
archive >> instance_uuid;
auto L = wi::lua::GetLuaState();
lua_getglobal(L, "asyncload_setresult");
lua_pushstring(L, tid.c_str());
lua_pushnumber(L, instance_uuid);
lua_call(L,3,0);
});
}

const char Library_BindLua::className[] = "Library";
Expand Down Expand Up @@ -107,13 +110,12 @@ namespace Game::ScriptBindings::Resources{
uint32_t loadingflags = 0;
if(argc >= 6) loadingflags = wi::lua::SGetInt(L, 6);
Game::Resources::Library::Load_Async(filepath, [=](uint32_t instance_uuid){
/*
wi::Archive callback_data;
std::shared_ptr<wi::Archive> callback_data_ptr = std::make_shared<wi::Archive>();
auto& callback_data = *callback_data_ptr;
callback_data.SetReadModeAndResetPos(false);
callback_data << "asyncload";
callback_data << instance_uuid;
Push_AsyncCallback(TID, callback_data);
*/
Push_AsyncCallback(TID, callback_data_ptr);
}, subresource, root, loadingstrategy, loadingflags);
return 1;
}else{
Expand Down

1 comment on commit 85376b7

@megumumpkin
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mentioned Issue: #1 #2 #3 #5

Please sign in to comment.