-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRuntimeSettings.cpp
More file actions
149 lines (131 loc) · 4.72 KB
/
Copy pathRuntimeSettings.cpp
File metadata and controls
149 lines (131 loc) · 4.72 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
#include "RuntimeSettings.h"
#include "LogSystem.h"
#include "PathFinder.h"
#include "ReflectionTypedYml.h"
#include "AuthoringParsedDocument.h"
#include <cstdio>
#include <exception>
#include <filesystem>
#include <memory>
#include <string>
#include <utility>
namespace
{
std::unique_ptr<RuntimeSettings> g_runtimeSettings;
}
bool RuntimeSettings::Initialize() noexcept
{
if (g_runtimeSettings) return true;
std::unique_ptr<RuntimeSettings> candidate(new RuntimeSettings());
if (!candidate->Load()) return false;
g_runtimeSettings = std::move(candidate);
return true;
}
void RuntimeSettings::Shutdown() noexcept
{
g_runtimeSettings.reset();
}
RuntimeSettings& RuntimeSettings::Get() noexcept
{
if (!g_runtimeSettings) std::terminate();
return *g_runtimeSettings;
}
RuntimeSettings* RuntimeSettings::TryGet() noexcept
{
return g_runtimeSettings.get();
}
bool RuntimeSettings::Load() noexcept
{
const std::filesystem::path settingsPath =
PathFinder::ProjectSettingPath("EngineSettings.asset");
try
{
if (!std::filesystem::exists(settingsPath))
{
if (!PathFinder::IsAssetAuthoringEnabled())
{
Debug->LogError("Packaged EngineSettings.asset is missing.");
return false;
}
Debug->LogWarning("EngineSettings.asset is missing; using runtime defaults until the Editor saves it.");
return true;
}
std::string parseError;
const Authoring::ParsedDocument document =
Authoring::ParsedDocument::ParseFile(settingsPath.string(), parseError);
if (!document)
{
Debug->LogError("Unable to load EngineSettings.asset: " + parseError);
return false;
}
const Authoring::ReadNode root = document.Root();
RenderPassSettings renderPassSettings = m_renderPassSettings;
if (root["renderPassSettings"])
{
Meta::Typed::DeserializeThunk<RenderPassSettings>(
&renderPassSettings, root["renderPassSettings"]);
}
RenderBackend renderBackend = RenderBackend::DX12;
bool hasCanonicalBackend = false;
const Authoring::ReadNode renderNode = root["render"];
if (renderNode)
{
if (!renderNode.IsMap())
{
Debug->LogError("EngineSettings render must be a map.");
return false;
}
const Authoring::ReadNode backendNode = renderNode["backend"];
if (backendNode)
{
if (!backendNode.IsScalar())
{
Debug->LogError("EngineSettings render.backend must be dx12 or vulkan.");
return false;
}
const std::string backendName = backendNode.AsString();
if (!TryParseRenderBackend(backendName, renderBackend))
{
const std::string message = "Unsupported EngineSettings render.backend '" +
backendName + "' (expected dx12 or vulkan).";
std::fprintf(stderr, "[RenderBackend] %s\n", message.c_str());
Debug->LogError(message);
return false;
}
hasCanonicalBackend = true;
}
}
if (!hasCanonicalBackend)
{
bool legacyDx12 = true;
if (root["renderBackendDx12"])
legacyDx12 = root["renderBackendDx12"].As<bool>();
if (root["imguiBackendDx12"])
legacyDx12 = legacyDx12 && root["imguiBackendDx12"].As<bool>();
renderBackend = legacyDx12 ? RenderBackend::DX12 : RenderBackend::Vulkan;
}
const Authoring::ReadNode startupSceneNode = root["startupSceneName"];
const std::filesystem::path startupScene = startupSceneNode
? startupSceneNode.AsString() : "SampleScene";
m_renderPassSettings = std::move(renderPassSettings);
m_renderBackend = renderBackend;
m_startupSceneName = startupScene.wstring();
Debug->LogDebug(std::string("[RenderBackend] runtime=") +
RenderBackendName(m_renderBackend));
return true;
}
catch (const std::exception& exception)
{
const std::string message = "Runtime settings initialization failed: " +
std::string(exception.what());
std::fprintf(stderr, "[RuntimeSettings] %s\n", message.c_str());
Debug->LogError(message);
return false;
}
catch (...)
{
std::fputs("[RuntimeSettings] unknown initialization failure\n", stderr);
Debug->LogError("Runtime settings initialization failed with an unknown error.");
return false;
}
}