Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
177 lines (169 loc) · 8.93 KB
/
Copy pathmain.cpp
File metadata and controls
177 lines (169 loc) · 8.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include "scarlett.h"
#ifdef RDNA4RAGE_DIRECTSTORAGE
#include "storage_stream.h"
#endif
#include <cerrno>
#include <cstdio>
#include <cwchar>
#include <filesystem>
#include <fstream>
#include <fcntl.h>
#include <io.h>
#include <stdexcept>
#include <string_view>
#include <system_error>
#include <vector>
#include <wrl/wrappers/corewrappers.h>
namespace {
void win_error(const char* operation) {
throw std::system_error(static_cast<int>(GetLastError()), std::system_category(), operation);
}
void hold_title_drive(const std::filesystem::path& path) {
namespace fs = std::filesystem;
const auto local = fs::absolute(path).lexically_normal();
const auto root = local.root_name().wstring();
if (root.size() != 2 || root[1] != L':' || GetDriveTypeW(local.root_path().c_str()) != DRIVE_FIXED)
throw std::runtime_error("Expected a title directory on a local fixed drive");
const auto directory = fs::canonical(local);
const auto archive = directory / L"appdata0.rpf";
if (!fs::is_regular_file(archive)) throw std::runtime_error("Title directory is missing appdata0.rpf");
using Handle = Microsoft::WRL::Wrappers::HandleT<Microsoft::WRL::Wrappers::HandleTraits::HANDLENullTraits>;
Handle lock(CreateMutexW(nullptr, FALSE, L"Local\\rdna4rage-SeriesX-GameDrive"));
if (!lock.IsValid()) win_error("creating game drive lock");
if (GetLastError() == ERROR_ALREADY_EXISTS) throw std::runtime_error("Another launcher owns the G: game drive");
const auto name = directory.wstring();
const auto target = L"\\??\\" + (name.starts_with(L"\\\\?\\") ? name.substr(4) : name);
constexpr DWORD flags = DDD_RAW_TARGET_PATH | DDD_NO_BROADCAST_SYSTEM;
struct Mapping {
const std::wstring& target;
bool owned{};
bool release() {
if (!owned) return true;
if (!DefineDosDeviceW(flags | DDD_REMOVE_DEFINITION | DDD_EXACT_MATCH_ON_REMOVE, L"G:", target.c_str())) return false;
owned = false;
return true;
}
~Mapping() { if (!release()) std::fprintf(stderr, "Could not release G: (Windows error %lu)\n", GetLastError()); }
} mapping{target};
wchar_t current[32768]{};
if (QueryDosDeviceW(L"G:", current, static_cast<DWORD>(std::size(current)))) {
if (_wcsicmp(current, target.c_str()) != 0) throw std::runtime_error("G: already points elsewhere");
} else {
if (GetLastError() != ERROR_FILE_NOT_FOUND) win_error("checking G: drive");
if (!DefineDosDeviceW(flags, L"G:", target.c_str())) win_error("mapping title directory to G:");
mapping.owned = true;
}
if (!fs::equivalent(archive, L"G:\\appdata0.rpf")) throw std::runtime_error("G: resolved a different startup archive");
std::puts("G:\\appdata0.rpf verified. Press Enter or close standard input to release the drive.");
std::fflush(stdout);
std::getchar();
if (!mapping.release()) win_error("releasing G: drive");
}
UINT64 number64(const wchar_t* text) {
wchar_t* end{};
errno = 0;
const bool hex = std::wstring_view(text).starts_with(L"0x") || std::wstring_view(text).starts_with(L"0X");
const auto value = std::wcstoull(text, &end, hex ? 16 : 10);
if (*text < L'0' || *text > L'9' || *end || errno == ERANGE)
throw std::runtime_error("Expected an unsigned decimal or hexadecimal number");
return value;
}
UINT number(const wchar_t* text) {
const auto value = number64(text);
if (value > UINT_MAX) throw std::out_of_range("Number exceeds 32 bits");
return static_cast<UINT>(value);
}
void binary_output(const void* data, size_t size) {
if (_isatty(_fileno(stdout))) throw std::runtime_error("Redirect binary output to a file or pipe");
if (_setmode(_fileno(stdout), _O_BINARY) < 0 || std::fwrite(data, 1, size, stdout) != size || std::fflush(stdout))
throw std::runtime_error("Writing binary output failed");
}
void texture(UINT width, UINT height, UINT format, UINT flags) {
if (!width || !height || width > D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION ||
height > D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION || !format || format > XG_FORMAT_X16_TYPELESS_G8_UINT)
throw std::runtime_error("Invalid 2D texture dimensions or format");
const XG_RESOURCE_DESC xg{XG_RESOURCE_DIMENSION_TEXTURE2D, 0, width, height, 1, 1,
static_cast<XG_FORMAT>(format), {1,0}, XG_TEXTURE_LAYOUT_UNKNOWN,
static_cast<XG12_RESOURCE_MISC_FLAG>(flags)};
auto computer = scarlett::open_texture(xg);
XG_RESOURCE_LAYOUT layout{};
if (FAILED(computer->GetResourceLayout(&layout))) throw std::runtime_error("GetResourceLayout failed");
if (!layout.SizeBytes || !layout.Planes || layout.Planes > std::size(layout.Plane) ||
layout.MipLevels != 1 || !layout.BaseAlignmentBytes)
throw std::runtime_error("Invalid layout returned by GXDK");
std::printf("%ux%u format %u flags 0x%08x: XG %llu bytes aligned to %llu, %u planes\n",
width, height, format, flags, layout.SizeBytes, layout.BaseAlignmentBytes, layout.Planes);
for (UINT i = 0; i < layout.Planes; ++i) {
const auto& p = layout.Plane[i];
std::printf(" plane %u usage %u offset %llu size %llu pitch %u\n",
i, UINT(p.Usage), p.BaseOffsetBytes, p.SizeBytes, p.MipLayout[0].PitchBytes);
}
}
std::vector<char> read_container(const std::filesystem::path& file) {
std::ifstream in(file, std::ios::binary | std::ios::ate);
const auto size = in.tellg();
if (size <= 0 || static_cast<unsigned long long>(size) > UINT_MAX)
throw std::runtime_error("Could not read shader container file or size");
std::vector<char> data(static_cast<size_t>(size));
if (!in.seekg(0).read(data.data(), size)) throw std::runtime_error("Incomplete shader container file");
return data;
}
}
int wmain(int argc, wchar_t** argv) try {
const std::wstring_view command = argc > 1 ? argv[1] : L"";
if (argc == 3 && command == L"hold-title-drive") {
hold_title_drive(argv[2]);
} else if (argc == 6 && command == L"texture") {
const UINT width = number(argv[2]), height = number(argv[3]), format = number(argv[4]), flags = number(argv[5]);
texture(width, height, format, flags);
#ifdef RDNA4RAGE_DIRECTSTORAGE
} else if (argc == 8 && command == L"read-stream") {
const UINT packed = number(argv[4]), inflated = number(argv[5]), decoded = number(argv[6]), format = number(argv[7]);
if (!packed || packed > inflated || inflated > decoded)
throw std::invalid_argument("Expected 0 < packed size <= inflated size <= decoded size");
const auto source = storage::read_local_range(argv[2], number64(argv[3]), packed);
if (packed == decoded) {
binary_output(source.data(), source.size());
} else {
std::vector<char> output(decoded);
scarlett::decode_stream(source, inflated, format, output);
binary_output(output.data(), output.size());
}
#endif
} else if (argc == 5 && command == L"check-rgba8") {
auto device = scarlett::open_device();
auto root = scarlett::open_root_signature(*device.Get(), read_container(argv[2]));
auto pipeline = scarlett::open_rgba8_pipeline(*device.Get(), *root.Get(), read_container(argv[3]), read_container(argv[4]));
std::puts("Scarlett device created the RGBA8 graphics pipeline");
} else if (argc == 3 && (command == L"inspect-root" || command == L"check-compute" || command == L"desktop-root")) {
auto device = scarlett::open_device();
const auto data = read_container(argv[2]);
if (command == L"desktop-root") {
auto blob = scarlett::serialize_root_signature(data, scarlett::RootTarget::desktop);
binary_output(blob->GetBufferPointer(), blob->GetBufferSize());
return 0;
}
auto root = scarlett::open_root_signature(*device.Get(), data);
if (command == L"check-compute") {
auto pipeline = scarlett::open_compute_pipeline(*device.Get(), *root.Get(), data);
std::printf("Scarlett device created a compute pipeline from %zu bytes\n", data.size());
} else {
std::printf("Scarlett device accepted the root signature in %zu bytes\n", data.size());
}
} else {
std::fputs("rdna4rage-gdk texture <width> <height> <format> <flags>\n"
"rdna4rage-gdk hold-title-drive <staged-title-directory>\n"
"rdna4rage-gdk inspect-root <serialized-root-signature>\n"
"rdna4rage-gdk desktop-root <shader-with-root-signature> (binary stdout)\n"
#ifdef RDNA4RAGE_DIRECTSTORAGE
"rdna4rage-gdk read-stream <file> <offset> <packed> <inflated> <decoded> <DXGI-format> (binary stdout)\n"
#endif
"rdna4rage-gdk check-rgba8 <root> <vertex> <pixel>\n"
"rdna4rage-gdk check-compute <shader-with-root-signature>\n", stderr);
return 2;
}
return 0;
} catch (const std::exception& error) {
std::fprintf(stderr, "%s\n", error.what());
return 1;
}