mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-11-30 00:07:47 +00:00
feat: add bind for loading fastfiles to ModMan
This commit is contained in:
9
src/ModMan/Context/FastFileContext.cpp
Normal file
9
src/ModMan/Context/FastFileContext.cpp
Normal file
@@ -0,0 +1,9 @@
|
||||
#include "FastFileContext.h"
|
||||
|
||||
#include "ZoneLoading.h"
|
||||
|
||||
bool FastFileContext::LoadFastFile(const std::string& path)
|
||||
{
|
||||
m_loaded_zones.emplace_back(ZoneLoading::LoadZone(path));
|
||||
return true;
|
||||
}
|
||||
13
src/ModMan/Context/FastFileContext.h
Normal file
13
src/ModMan/Context/FastFileContext.h
Normal file
@@ -0,0 +1,13 @@
|
||||
#pragma once
|
||||
#include "Zone/Zone.h"
|
||||
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
class FastFileContext
|
||||
{
|
||||
public:
|
||||
bool LoadFastFile(const std::string& path);
|
||||
|
||||
std::vector<std::unique_ptr<Zone>> m_loaded_zones;
|
||||
};
|
||||
@@ -5,3 +5,13 @@ ModManContext& ModManContext::Get()
|
||||
static ModManContext context;
|
||||
return context;
|
||||
}
|
||||
|
||||
void ModManContext::Startup()
|
||||
{
|
||||
m_db_thread.Start();
|
||||
}
|
||||
|
||||
void ModManContext::Destroy()
|
||||
{
|
||||
m_db_thread.Terminate();
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "FastFileContext.h"
|
||||
#include "Utils/DispatchableThread.h"
|
||||
#include "Web/WebViewLib.h"
|
||||
|
||||
#include <memory>
|
||||
@@ -9,6 +11,12 @@ class ModManContext
|
||||
public:
|
||||
static ModManContext& Get();
|
||||
|
||||
void Startup();
|
||||
void Destroy();
|
||||
|
||||
std::unique_ptr<webview::webview> m_main_webview;
|
||||
std::unique_ptr<webview::webview> m_dev_tools_webview;
|
||||
FastFileContext m_fast_file;
|
||||
|
||||
DispatchableThread m_db_thread;
|
||||
};
|
||||
|
||||
77
src/ModMan/Utils/DispatchableThread.cpp
Normal file
77
src/ModMan/Utils/DispatchableThread.cpp
Normal file
@@ -0,0 +1,77 @@
|
||||
#include "DispatchableThread.h"
|
||||
|
||||
DispatchableThread::DispatchableThread()
|
||||
: m_terminate(false)
|
||||
{
|
||||
}
|
||||
|
||||
DispatchableThread::~DispatchableThread()
|
||||
{
|
||||
Terminate();
|
||||
}
|
||||
|
||||
void DispatchableThread::Start()
|
||||
{
|
||||
m_terminate = false;
|
||||
m_thread = std::thread(
|
||||
[&]
|
||||
{
|
||||
ThreadLoop();
|
||||
});
|
||||
}
|
||||
|
||||
void DispatchableThread::Terminate()
|
||||
{
|
||||
std::unique_lock lock(m_cb_mutex);
|
||||
|
||||
if (!m_terminate)
|
||||
{
|
||||
m_terminate = true;
|
||||
m_cv.notify_all();
|
||||
lock.unlock();
|
||||
m_thread.join();
|
||||
}
|
||||
else
|
||||
{
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
void DispatchableThread::Dispatch(cb_t cb)
|
||||
{
|
||||
std::lock_guard lock(m_cb_mutex);
|
||||
|
||||
m_cb_list.emplace_back(std::move(cb));
|
||||
m_cv.notify_one();
|
||||
}
|
||||
|
||||
std::optional<DispatchableThread::cb_t> DispatchableThread::NextCallback()
|
||||
{
|
||||
if (m_terminate || m_cb_list.empty())
|
||||
return std::nullopt;
|
||||
|
||||
auto cb = std::move(m_cb_list.front());
|
||||
m_cb_list.pop_front();
|
||||
|
||||
return cb;
|
||||
}
|
||||
|
||||
void DispatchableThread::ThreadLoop()
|
||||
{
|
||||
while (!m_terminate)
|
||||
{
|
||||
std::unique_lock lock(m_cb_mutex);
|
||||
m_cv.wait(lock,
|
||||
[&]
|
||||
{
|
||||
return !m_cb_list.empty() || m_terminate;
|
||||
});
|
||||
|
||||
auto maybeCb = NextCallback();
|
||||
|
||||
lock.unlock();
|
||||
|
||||
if (maybeCb)
|
||||
(*maybeCb)();
|
||||
}
|
||||
}
|
||||
37
src/ModMan/Utils/DispatchableThread.h
Normal file
37
src/ModMan/Utils/DispatchableThread.h
Normal file
@@ -0,0 +1,37 @@
|
||||
#pragma once
|
||||
|
||||
#include <condition_variable>
|
||||
#include <deque>
|
||||
#include <functional>
|
||||
#include <mutex>
|
||||
#include <optional>
|
||||
#include <thread>
|
||||
|
||||
class DispatchableThread
|
||||
{
|
||||
public:
|
||||
using cb_t = std::function<void()>;
|
||||
|
||||
DispatchableThread();
|
||||
~DispatchableThread();
|
||||
DispatchableThread(const DispatchableThread& other) = delete;
|
||||
DispatchableThread(DispatchableThread&& other) noexcept = default;
|
||||
DispatchableThread& operator=(const DispatchableThread& other) = delete;
|
||||
DispatchableThread& operator=(DispatchableThread&& other) noexcept = default;
|
||||
|
||||
void Start();
|
||||
void Terminate();
|
||||
|
||||
void Dispatch(cb_t cb);
|
||||
|
||||
private:
|
||||
std::optional<cb_t> NextCallback();
|
||||
void ThreadLoop();
|
||||
|
||||
std::mutex m_cb_mutex;
|
||||
std::deque<cb_t> m_cb_list;
|
||||
|
||||
std::condition_variable m_cv;
|
||||
std::thread m_thread;
|
||||
bool m_terminate;
|
||||
};
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "Binds.h"
|
||||
|
||||
#include "FastFileBinds.h"
|
||||
#include "Web/Binds/DialogBinds.h"
|
||||
|
||||
namespace ui
|
||||
@@ -7,5 +8,6 @@ namespace ui
|
||||
void RegisterAllBinds(webview::webview& wv)
|
||||
{
|
||||
RegisterDialogHandlerBinds(wv);
|
||||
RegisterFastFileBinds(wv);
|
||||
}
|
||||
} // namespace ui
|
||||
|
||||
28
src/ModMan/Web/Binds/FastFileBinds.cpp
Normal file
28
src/ModMan/Web/Binds/FastFileBinds.cpp
Normal file
@@ -0,0 +1,28 @@
|
||||
#include "FastFileBinds.h"
|
||||
|
||||
#include "Context/ModManContext.h"
|
||||
#include "Web/UiCommunication.h"
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
namespace ui
|
||||
{
|
||||
void RegisterFastFileBinds(webview::webview& wv)
|
||||
{
|
||||
BindAsync<std::string>(wv,
|
||||
"loadFastFile",
|
||||
[&wv](const std::string& id, std::string path)
|
||||
{
|
||||
std::string idMove(id);
|
||||
ModManContext::Get().m_db_thread.Dispatch(
|
||||
[&wv, idMove, path]
|
||||
{
|
||||
ModManContext::Get().m_fast_file.LoadFastFile(path);
|
||||
PromiseResolve(wv, idMove, true);
|
||||
});
|
||||
});
|
||||
}
|
||||
} // namespace ui
|
||||
8
src/ModMan/Web/Binds/FastFileBinds.h
Normal file
8
src/ModMan/Web/Binds/FastFileBinds.h
Normal file
@@ -0,0 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#include "Web/WebViewLib.h"
|
||||
|
||||
namespace ui
|
||||
{
|
||||
void RegisterFastFileBinds(webview::webview& wv);
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#define NOMINMAX
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(push, 0)
|
||||
#else
|
||||
|
||||
@@ -116,7 +116,11 @@ int main()
|
||||
|
||||
con::info("Starting ModMan " GIT_VERSION);
|
||||
|
||||
ModManContext::Get().Startup();
|
||||
|
||||
const auto result = SpawnMainWindow();
|
||||
|
||||
ModManContext::Get().Destroy();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user