mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-10-24 07:15:51 +00:00
chore: implement gtk handler for custom files
This commit is contained in:
166
src/ModMan/Web/Edge/CustomProtocolHandlerEdge.cpp
Normal file
166
src/ModMan/Web/Edge/CustomProtocolHandlerEdge.cpp
Normal file
@@ -0,0 +1,166 @@
|
||||
#include "CustomProtocolHandlerEdge.h"
|
||||
|
||||
#pragma warning(push, 0)
|
||||
#include <webview/macros.h>
|
||||
#include <webview/webview.h>
|
||||
#pragma warning(pop)
|
||||
|
||||
#if defined(WEBVIEW_PLATFORM_WINDOWS) && defined(WEBVIEW_EDGE)
|
||||
|
||||
#include "ui/modmanui.h"
|
||||
|
||||
#include <Windows.h>
|
||||
#include <format>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <webview/detail/backends/win32_edge.hh>
|
||||
#include <wrl/event.h>
|
||||
|
||||
namespace
|
||||
{
|
||||
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)
|
||||
{
|
||||
if (wide_string.empty())
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
const auto size_needed = WideCharToMultiByte(CP_UTF8, 0, wide_string.data(), (int)wide_string.size(), nullptr, 0, nullptr, nullptr);
|
||||
if (size_needed <= 0)
|
||||
{
|
||||
throw std::runtime_error("WideCharToMultiByte() failed: " + std::to_string(size_needed));
|
||||
}
|
||||
|
||||
std::string result(size_needed, 0);
|
||||
WideCharToMultiByte(CP_UTF8, 0, wide_string.data(), (int)wide_string.size(), result.data(), size_needed, nullptr, nullptr);
|
||||
return result;
|
||||
}
|
||||
|
||||
std::wstring HeadersForAssetName(const std::string& assetName, const size_t contentLength)
|
||||
{
|
||||
std::wstringstream wss;
|
||||
|
||||
wss << std::format(L"Content-Length: {}\n", contentLength);
|
||||
|
||||
if (assetName.ends_with(".html"))
|
||||
{
|
||||
wss << L"Content-Type: text/html\n";
|
||||
}
|
||||
else if (assetName.ends_with(".js"))
|
||||
{
|
||||
wss << L"Content-Type: text/javascript\n";
|
||||
}
|
||||
else if (assetName.ends_with(".css"))
|
||||
{
|
||||
wss << L"Content-Type: text/css\n";
|
||||
}
|
||||
|
||||
return wss.str();
|
||||
}
|
||||
} // namespace
|
||||
|
||||
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)))
|
||||
{
|
||||
std::cerr << "Failed to get webview\n";
|
||||
return;
|
||||
}
|
||||
|
||||
Microsoft::WRL::ComPtr<ICoreWebView2_22> core22;
|
||||
if (!SUCCEEDED(core->QueryInterface(IID_PPV_ARGS(&core22))))
|
||||
{
|
||||
std::cerr << "Failed to get core22\n";
|
||||
return;
|
||||
}
|
||||
|
||||
if (!SUCCEEDED(core22->AddWebResourceRequestedFilterWithRequestSourceKinds(
|
||||
L"*", COREWEBVIEW2_WEB_RESOURCE_CONTEXT_ALL, COREWEBVIEW2_WEB_RESOURCE_REQUEST_SOURCE_KINDS_ALL)))
|
||||
{
|
||||
std::cerr << "Failed to install request filter\n";
|
||||
return;
|
||||
}
|
||||
|
||||
EventRegistrationToken token;
|
||||
core->add_WebResourceRequested(Microsoft::WRL::Callback<ICoreWebView2WebResourceRequestedEventHandler>(
|
||||
[core22](ICoreWebView2* sender, IUnknown* args) -> HRESULT
|
||||
{
|
||||
Microsoft::WRL::ComPtr<ICoreWebView2WebResourceRequestedEventArgs2> webResourceRequestArgs;
|
||||
if (SUCCEEDED(args->QueryInterface(IID_PPV_ARGS(&webResourceRequestArgs))))
|
||||
{
|
||||
COREWEBVIEW2_WEB_RESOURCE_REQUEST_SOURCE_KINDS requestSourceKind =
|
||||
COREWEBVIEW2_WEB_RESOURCE_REQUEST_SOURCE_KINDS_ALL;
|
||||
if (!SUCCEEDED(webResourceRequestArgs->get_RequestedSourceKind(&requestSourceKind)))
|
||||
{
|
||||
std::cerr << "Failed to get requested source kind\n";
|
||||
return S_FALSE;
|
||||
}
|
||||
|
||||
Microsoft::WRL::ComPtr<ICoreWebView2WebResourceRequest> request;
|
||||
if (!SUCCEEDED(webResourceRequestArgs->get_Request(&request)))
|
||||
{
|
||||
std::cerr << "Failed to get request\n";
|
||||
return S_FALSE;
|
||||
}
|
||||
|
||||
LPWSTR wUri;
|
||||
if (!SUCCEEDED(request->get_Uri(&wUri)))
|
||||
{
|
||||
std::cerr << "Failed to get uri\n";
|
||||
return S_FALSE;
|
||||
}
|
||||
|
||||
Microsoft::WRL::ComPtr<ICoreWebView2Environment> environment;
|
||||
if (!SUCCEEDED(core22->get_Environment(&environment)))
|
||||
{
|
||||
std::cerr << "Failed to get environment\n";
|
||||
return S_FALSE;
|
||||
}
|
||||
|
||||
Microsoft::WRL::ComPtr<ICoreWebView2WebResourceResponse> response;
|
||||
|
||||
const auto uri = wide_string_to_string(wUri);
|
||||
bool fileFound = false;
|
||||
if (uri.starts_with(MOD_MAN_URL_PREFIX))
|
||||
{
|
||||
const auto asset = uri.substr(std::char_traits<char>::length(MOD_MAN_URL_PREFIX));
|
||||
|
||||
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);
|
||||
|
||||
const auto headers = HeadersForAssetName(asset, foundUiFile->second.dataSize);
|
||||
environment->CreateWebResourceResponse(response_stream.Get(), 200, L"OK", headers.data(), &response);
|
||||
fileFound = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!fileFound)
|
||||
{
|
||||
environment->CreateWebResourceResponse(nullptr, 404, L"Not found", L"", &response);
|
||||
}
|
||||
|
||||
webResourceRequestArgs->put_Response(response.Get());
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
return S_FALSE;
|
||||
})
|
||||
.Get(),
|
||||
&token);
|
||||
}
|
||||
} // namespace edge
|
||||
|
||||
#endif
|
||||
18
src/ModMan/Web/Edge/CustomProtocolHandlerEdge.h
Normal file
18
src/ModMan/Web/Edge/CustomProtocolHandlerEdge.h
Normal file
@@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
#pragma warning(push, 0)
|
||||
#include <webview/macros.h>
|
||||
#pragma warning(pop)
|
||||
|
||||
#if defined(WEBVIEW_PLATFORM_WINDOWS) && defined(WEBVIEW_EDGE)
|
||||
|
||||
#pragma warning(push, 0)
|
||||
#include <webview/webview.h>
|
||||
#pragma warning(pop)
|
||||
|
||||
namespace edge
|
||||
{
|
||||
void InstallCustomProtocolHandler(webview::webview& wv);
|
||||
}
|
||||
|
||||
#endif
|
||||
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
|
||||
18
src/ModMan/Web/Gtk/CustomProtocolHandlerGtk.h
Normal file
18
src/ModMan/Web/Gtk/CustomProtocolHandlerGtk.h
Normal file
@@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
#pragma warning(push, 0)
|
||||
#include <webview/macros.h>
|
||||
#pragma warning(pop)
|
||||
|
||||
#if defined(WEBVIEW_PLATFORM_LINUX) && defined(WEBVIEW_GTK)
|
||||
|
||||
#pragma warning(push, 0)
|
||||
#include <webview/webview.h>
|
||||
#pragma warning(pop)
|
||||
|
||||
namespace gtk
|
||||
{
|
||||
void InstallCustomProtocolHandler(webview::webview& wv);
|
||||
}
|
||||
|
||||
#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();
|
||||
Reference in New Issue
Block a user