upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/shims/package_splash.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/shims/package_splash.cpp')
-rw-r--r--runtime/shims/package_splash.cpp159
1 files changed, 159 insertions, 0 deletions
diff --git a/runtime/shims/package_splash.cpp b/runtime/shims/package_splash.cpp
new file mode 100644
index 000000000..f21967519
--- /dev/null
+++ b/runtime/shims/package_splash.cpp
@@ -0,0 +1,159 @@
1#include "package_splash.h"
2#include "title_window.h"
3
4#include <Shlwapi.h>
5#include <wincodec.h>
6#include <wrl/client.h>
7#include <xmllite.h>
8
9#include <algorithm>
10#include <atomic>
11#include <cstdint>
12#include <string_view>
13#include <vector>
14
15namespace rt::package_splash {
16namespace {
17using Microsoft::WRL::ComPtr;
18std::filesystem::path image_path;
19
20std::filesystem::path declared_image(const std::filesystem::path& package, const wchar_t* manifest,
21 const wchar_t* element, const wchar_t* attribute) {
22 ComPtr<IStream> stream;
23 ComPtr<IXmlReader> reader;
24 const auto config = package / manifest;
25 if (FAILED(SHCreateStreamOnFileEx(config.c_str(), STGM_READ | STGM_SHARE_DENY_NONE, 0, FALSE, nullptr, &stream)) ||
26 FAILED(CreateXmlReader(IID_PPV_ARGS(&reader), nullptr)) || FAILED(reader->SetInput(stream.Get())))
27 return {};
28 XmlNodeType type{};
29 while (reader->Read(&type) == S_OK) {
30 const wchar_t* name = nullptr;
31 const wchar_t* value = nullptr;
32 if (type != XmlNodeType_Element || FAILED(reader->GetLocalName(&name, nullptr)) ||
33 std::wstring_view(name) != element ||
34 reader->MoveToAttributeByName(attribute, nullptr) != S_OK ||
35 FAILED(reader->GetValue(&value, nullptr)) || !*value)
36 continue;
37 const std::filesystem::path asset(value);
38 if (asset.has_root_path() || asset.native().find(L':') != std::wstring::npos) return {};
39 for (const auto& part : asset)
40 if (part == L"..") return {};
41 std::error_code ec;
42 const auto root = std::filesystem::canonical(package, ec);
43 if (ec) return {};
44 const auto file = std::filesystem::canonical(root / asset, ec);
45 if (ec) return {};
46 const auto relative = file.lexically_relative(root);
47 if (relative.empty() || *relative.begin() == L".." || !std::filesystem::is_regular_file(file, ec)) return {};
48 return file;
49 }
50 return {};
51}
52
53std::vector<BYTE> decode(UINT width, UINT height) {
54 ComPtr<IWICImagingFactory> factory;
55 ComPtr<IWICBitmapDecoder> decoder;
56 ComPtr<IWICBitmapFrameDecode> frame;
57 UINT w = 0, h = 0;
58 if (FAILED(CoCreateInstance(CLSID_WICImagingFactory, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&factory))) ||
59 FAILED(factory->CreateDecoderFromFilename(image_path.c_str(), nullptr, GENERIC_READ, WICDecodeMetadataCacheOnDemand, &decoder)) ||
60 FAILED(decoder->GetFrame(0, &frame)) || FAILED(frame->GetSize(&w, &h)) || !w || !h)
61 return {};
62
63 // Centre the package art at its original aspect ratio in the actual client area.
64 const UINT draw_w = UINT(std::clamp<std::uint64_t>(std::uint64_t(height) * w / h, 1, width));
65 const UINT draw_h = UINT(std::clamp<std::uint64_t>(std::uint64_t(width) * h / w, 1, height));
66 ComPtr<IWICBitmapScaler> scaler;
67 ComPtr<IWICFormatConverter> converter;
68 if (FAILED(factory->CreateBitmapScaler(&scaler)) ||
69 FAILED(scaler->Initialize(frame.Get(), draw_w, draw_h, WICBitmapInterpolationModeFant)) ||
70 FAILED(factory->CreateFormatConverter(&converter)) ||
71 FAILED(converter->Initialize(scaler.Get(), GUID_WICPixelFormat32bppPBGRA, WICBitmapDitherTypeNone, nullptr, 0,
72 WICBitmapPaletteTypeCustom)))
73 return {};
74 const UINT pitch = width * 4;
75 const UINT bytes = pitch * height;
76 const UINT offset = (height - draw_h) / 2 * pitch + (width - draw_w) / 2 * 4;
77 std::vector<BYTE> pixels(bytes);
78 if (FAILED(converter->CopyPixels(nullptr, pitch, bytes - offset, pixels.data() + offset))) return {};
79
80 return pixels;
81}
82
83std::vector<BYTE> load_pixels(UINT width, UINT height) {
84 if (image_path.empty() || !width || !height || std::uint64_t(width) * height > (64u << 20)) return {};
85 const HRESULT apartment = CoInitializeEx(nullptr, COINIT_MULTITHREADED);
86 if (FAILED(apartment) && apartment != RPC_E_CHANGED_MODE) return {};
87 std::vector<BYTE> pixels;
88 try {
89 pixels = decode(width, height);
90 } catch (const std::bad_alloc&) {
91 // Optional startup art must leave the first title frame available on low-memory hosts.
92 }
93 if (SUCCEEDED(apartment)) CoUninitialize();
94 return pixels;
95}
96
97} // namespace
98
99void configure(const std::filesystem::path& package) {
100 image_path.clear();
101 wchar_t value[4]{};
102 if (GetEnvironmentVariableW(L"LEONIDA_PACKAGE_SPLASH", value, 4) == 1 && value[0] == L'0') return;
103 wchar_t window_mode[16]{};
104 const DWORD length = GetEnvironmentVariableW(L"LEONIDA_PRESENT_WINDOW", window_mode, 16);
105 if (length && length < 16 && (std::wstring_view(window_mode) == L"own" || std::wstring_view(window_mode) == L"hidden"))
106 return;
107 image_path = declared_image(package, L"MicrosoftGame.Config", L"ShellVisuals", L"SplashScreenImage");
108 if (image_path.empty()) image_path = declared_image(package, L"appxmanifest.xml", L"SplashScreen", L"Image");
109}
110
111bool configured() { return !image_path.empty(); }
112
113bool paint_window(HWND window) {
114 static std::atomic<bool> painted{false};
115 if (painted.load() || !configured() || !window || rt::d3d12::find_title_window(GetCurrentProcessId()) != window)
116 return false;
117 RECT client{};
118 if (!GetClientRect(window, &client) || client.right <= 0 || client.bottom <= 0 || painted.exchange(true))
119 return false;
120 auto pixels = load_pixels(UINT(client.right), UINT(client.bottom));
121 if (pixels.empty()) return false;
122 BITMAPINFO bitmap{};
123 bitmap.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
124 bitmap.bmiHeader.biWidth = client.right;
125 bitmap.bmiHeader.biHeight = -client.bottom;
126 bitmap.bmiHeader.biPlanes = 1;
127 bitmap.bmiHeader.biBitCount = 32;
128 bitmap.bmiHeader.biCompression = BI_RGB;
129 if (GetWindowThreadProcessId(window, nullptr) == GetCurrentThreadId()) UpdateWindow(window);
130 HDC dc = GetDC(window);
131 if (!dc) return false;
132 int lines = SetDIBitsToDevice(dc, 0, 0, client.right, client.bottom, 0, 0, 0, client.bottom,
133 pixels.data(), &bitmap, DIB_RGB_COLORS);
134 ReleaseDC(window, dc);
135 return lines > 0;
136}
137
138ID3D12Resource* create_texture(ID3D12Device* device, UINT width, UINT height) {
139 if (!device) return nullptr;
140 const auto pixels = load_pixels(width, height);
141 if (pixels.empty()) return nullptr;
142 const UINT pitch = width * 4;
143 const UINT bytes = pitch * height;
144 D3D12_RESOURCE_DESC desc{};
145 desc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
146 desc.Width = width;
147 desc.Height = height;
148 desc.DepthOrArraySize = desc.MipLevels = 1;
149 desc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
150 desc.SampleDesc.Count = 1;
151 const D3D12_HEAP_PROPERTIES heap{D3D12_HEAP_TYPE_CUSTOM, D3D12_CPU_PAGE_PROPERTY_WRITE_BACK, D3D12_MEMORY_POOL_L0, 1, 1};
152 ComPtr<ID3D12Resource> texture;
153 if (FAILED(device->CreateCommittedResource(&heap, D3D12_HEAP_FLAG_NONE, &desc, D3D12_RESOURCE_STATE_COMMON,
154 nullptr, IID_PPV_ARGS(&texture))) ||
155 FAILED(texture->WriteToSubresource(0, nullptr, pixels.data(), pitch, bytes)))
156 return nullptr;
157 return texture.Detach();
158}
159} // namespace rt::package_splash