mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-10-29 17:46:56 +00:00
chore: implement gtk handler for custom files
This commit is contained in:
@@ -18,7 +18,9 @@
|
||||
|
||||
namespace
|
||||
{
|
||||
constexpr auto MOD_MAN_URL_PREFIX = "http://modman/";
|
||||
constexpr auto MOD_MAN_URL_PREFIX = "modman://localhost/";
|
||||
|
||||
std::unordered_map<std::string, UiFile> assetLookup;
|
||||
|
||||
std::string wide_string_to_string(const std::wstring& wide_string)
|
||||
{
|
||||
@@ -65,6 +67,8 @@ namespace edge
|
||||
{
|
||||
void InstallCustomProtocolHandler(webview::webview& wv)
|
||||
{
|
||||
assetLookup = BuildUiFileLookup();
|
||||
|
||||
const auto controller = static_cast<ICoreWebView2Controller*>(wv.browser_controller().value());
|
||||
Microsoft::WRL::ComPtr<ICoreWebView2> core;
|
||||
if (!SUCCEEDED(controller->get_CoreWebView2(&core)))
|
||||
@@ -131,8 +135,8 @@ namespace edge
|
||||
{
|
||||
const auto asset = uri.substr(std::char_traits<char>::length(MOD_MAN_URL_PREFIX));
|
||||
|
||||
const auto foundUiFile = MOD_MAN_UI_FILES.find(asset);
|
||||
if (foundUiFile != MOD_MAN_UI_FILES.end())
|
||||
const auto foundUiFile = assetLookup.find(asset);
|
||||
if (foundUiFile != assetLookup.end())
|
||||
{
|
||||
Microsoft::WRL::ComPtr<IStream> response_stream = SHCreateMemStream(
|
||||
static_cast<const BYTE*>(foundUiFile->second.data), foundUiFile->second.dataSize);
|
||||
106
src/ModMan/Web/Gtk/CustomProtocolHandlerGtk.cpp
Normal file
106
src/ModMan/Web/Gtk/CustomProtocolHandlerGtk.cpp
Normal file
@@ -0,0 +1,106 @@
|
||||
#include "CustomProtocolHandlerGtk.h"
|
||||
|
||||
#pragma warning(push, 0)
|
||||
#include <webview/macros.h>
|
||||
#include <webview/webview.h>
|
||||
#pragma warning(pop)
|
||||
|
||||
#if defined(WEBVIEW_PLATFORM_LINUX) && defined(WEBVIEW_GTK)
|
||||
|
||||
#include "Web/UiAssets.h"
|
||||
|
||||
#include <format>
|
||||
#include <iostream>
|
||||
|
||||
#define G_SPAWN_ERROR g_spawn_error_quark()
|
||||
|
||||
G_DEFINE_QUARK(g - spawn - error - quark, g_spawn_error)
|
||||
|
||||
namespace
|
||||
{
|
||||
std::unordered_map<std::string, UiFile> assetLookup;
|
||||
|
||||
const char* ContentTypeForAssetName(const std::string& assetName)
|
||||
{
|
||||
const char* mimeType;
|
||||
|
||||
if (assetName.ends_with(".html"))
|
||||
{
|
||||
mimeType = "text/html";
|
||||
}
|
||||
else if (assetName.ends_with(".js"))
|
||||
{
|
||||
mimeType = "text/javascript";
|
||||
}
|
||||
else if (assetName.ends_with(".css"))
|
||||
{
|
||||
mimeType = "text/css";
|
||||
}
|
||||
else
|
||||
{
|
||||
mimeType = "application/octet-stream";
|
||||
}
|
||||
|
||||
return mimeType;
|
||||
}
|
||||
|
||||
void ModManUriSchemeRequestCb(WebKitURISchemeRequest* request, gpointer user_data)
|
||||
{
|
||||
const gchar* asset = webkit_uri_scheme_request_get_path(request);
|
||||
|
||||
std::cout << std::format("Modman request: {}\n", asset);
|
||||
|
||||
const auto foundUiFile = assetLookup.find(asset);
|
||||
if (foundUiFile != assetLookup.end())
|
||||
{
|
||||
gsize stream_length = foundUiFile->second.dataSize;
|
||||
GInputStream* stream = g_memory_input_stream_new_from_data(foundUiFile->second.data, foundUiFile->second.dataSize, nullptr);
|
||||
|
||||
webkit_uri_scheme_request_finish(request, stream, stream_length, ContentTypeForAssetName(foundUiFile->second.filename));
|
||||
g_object_unref(stream);
|
||||
}
|
||||
else
|
||||
{
|
||||
GError* error = g_error_new(G_SPAWN_ERROR, 123, "Could not find %s.", asset);
|
||||
webkit_uri_scheme_request_finish_error(request, error);
|
||||
g_error_free(error);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void OnResourceLoadStarted(WebKitWebView* self, WebKitWebResource* resource, WebKitURIRequest* request, gpointer user_data)
|
||||
{
|
||||
std::cout << webkit_uri_request_get_http_method(request) << " " << webkit_uri_request_get_uri(request) << "\n";
|
||||
|
||||
std::string uri(webkit_uri_request_get_uri(request));
|
||||
if (uri.starts_with("http://"))
|
||||
{
|
||||
uri = std::format("modman://{}", uri.substr(7));
|
||||
}
|
||||
else if (uri.starts_with("https://"))
|
||||
{
|
||||
uri = std::format("modman://{}", uri.substr(8));
|
||||
}
|
||||
|
||||
std::cout << std::format("Setting uri: {}\n", uri);
|
||||
webkit_uri_request_set_uri(request, uri.c_str());
|
||||
}
|
||||
} // namespace
|
||||
|
||||
namespace gtk
|
||||
{
|
||||
void InstallCustomProtocolHandler(webview::webview& wv)
|
||||
{
|
||||
const auto widget = static_cast<GtkWidget*>(wv.browser_controller().value());
|
||||
const auto webView = WEBKIT_WEB_VIEW(widget);
|
||||
const auto context = webkit_web_view_get_context(webView);
|
||||
|
||||
assetLookup = BuildUiFileLookup();
|
||||
|
||||
g_signal_connect(webView, "resource-load-started", G_CALLBACK(OnResourceLoadStarted), NULL);
|
||||
|
||||
webkit_web_context_register_uri_scheme(context, "modman", ModManUriSchemeRequestCb, NULL, nullptr);
|
||||
}
|
||||
} // namespace gtk
|
||||
|
||||
#endif
|
||||
15
src/ModMan/Web/UiAssets.cpp
Normal file
15
src/ModMan/Web/UiAssets.cpp
Normal file
@@ -0,0 +1,15 @@
|
||||
#include "UiAssets.h"
|
||||
|
||||
#include <format>
|
||||
|
||||
std::unordered_map<std::string, UiFile> BuildUiFileLookup()
|
||||
{
|
||||
std::unordered_map<std::string, UiFile> result;
|
||||
|
||||
for (const auto& asset : MOD_MAN_UI_FILES)
|
||||
{
|
||||
result.emplace(std::format("/{}", asset.filename), asset);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
8
src/ModMan/Web/UiAssets.h
Normal file
8
src/ModMan/Web/UiAssets.h
Normal file
@@ -0,0 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#include "ui/modmanui.h"
|
||||
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
std::unordered_map<std::string, UiFile> BuildUiFileLookup();
|
||||
@@ -2,8 +2,8 @@
|
||||
#include "webview/webview.h"
|
||||
#pragma warning(pop)
|
||||
|
||||
#include "webview/edge/CustomProtocolHandlerEdge.h"
|
||||
#include "webview/gtk/CustomProtocolHandlerGtk.h"
|
||||
#include "Web/Edge/CustomProtocolHandlerEdge.h"
|
||||
#include "Web/Gtk/CustomProtocolHandlerGtk.h"
|
||||
|
||||
#include <chrono>
|
||||
#include <format>
|
||||
@@ -57,7 +57,11 @@ int main()
|
||||
edge::InstallCustomProtocolHandler(w);
|
||||
#endif
|
||||
|
||||
w.navigate("http://modman/index.html");
|
||||
#if defined(WEBVIEW_PLATFORM_LINUX) && defined(WEBVIEW_GTK)
|
||||
gtk::InstallCustomProtocolHandler(w);
|
||||
#endif
|
||||
|
||||
w.navigate("modman://localhost/index.html");
|
||||
w.run();
|
||||
}
|
||||
catch (const webview::exception& e)
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
namespace webkitgtk
|
||||
{
|
||||
void InstallCustomProtocolHandler();
|
||||
}
|
||||
Reference in New Issue
Block a user