2
0
mirror of https://github.com/Laupetin/OpenAssetTools.git synced 2025-10-19 04:55:19 +00:00

feat: add bind for loading fastfiles to ModMan

This commit is contained in:
Jan Laupetin
2025-10-11 12:51:21 +01:00
parent 42473a7320
commit 4911cfa4c6
15 changed files with 215 additions and 1 deletions

View File

@@ -47,10 +47,16 @@ function ModMan:project()
self:include(includes)
Utils:include(includes)
ZoneLoading:include(includes)
ObjLoading:include(includes)
ObjWriting:include(includes)
json:include(includes)
webview:include(includes)
links:linkto(Utils)
links:linkto(ZoneLoading)
links:linkto(ObjLoading)
links:linkto(ObjWriting)
links:linkto(webview)
links:linkall()
end

View 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;
}

View 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;
};

View File

@@ -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();
}

View File

@@ -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;
};

View 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)();
}
}

View 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;
};

View File

@@ -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

View 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

View File

@@ -0,0 +1,8 @@
#pragma once
#include "Web/WebViewLib.h"
namespace ui
{
void RegisterFastFileBinds(webview::webview& wv);
}

View File

@@ -1,5 +1,7 @@
#pragma once
#define NOMINMAX
#ifdef _MSC_VER
#pragma warning(push, 0)
#else

View File

@@ -116,7 +116,11 @@ int main()
con::info("Starting ModMan " GIT_VERSION);
ModManContext::Get().Startup();
const auto result = SpawnMainWindow();
ModManContext::Get().Destroy();
return result;
}

View File

@@ -3,10 +3,15 @@ import { ref } from "vue";
import { webviewBinds } from "./native";
const lastPath = ref("");
const loadingFastFile = ref(false);
async function onOpenFastfileClick() {
lastPath.value =
(await webviewBinds.openFileDialog({ filters: [{ name: "Fastfiles", filter: "*.ff" }] })) ?? "";
loadingFastFile.value = true;
await webviewBinds.loadFastFile(lastPath.value);
loadingFastFile.value = false;
}
</script>
@@ -18,6 +23,7 @@ async function onOpenFastfileClick() {
<p>
<button @click="onOpenFastfileClick">Open fastfile</button>
<span>The last path: {{ lastPath }}</span>
<span>Loading: {{ loadingFastFile }}</span>
</p>
</main>
</template>

View File

@@ -0,0 +1,3 @@
export interface FastFileBinds {
loadFastFile(path: string): Promise<void>;
}

View File

@@ -1,7 +1,8 @@
import type { DialogBinds } from "./DialogBinds";
import type { FastFileBinds } from "./FastFileBinds";
export type NativeMethods = DialogBinds;
export type NativeMethods = DialogBinds & FastFileBinds;
interface NativeEventMap {