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
178
179
180
|
// Copyright Epic Games, Inc. All Rights Reserved.
#include "config.h"
#include "diag/logging.h"
#include <zencore/fmtutils.h>
#include <zencore/iobuffer.h>
#include <zencore/string.h>
#pragma warning(push)
#pragma warning(disable : 4267) // warning C4267: '=': conversion from 'size_t' to 'US', possible loss of data
#include <cxxopts.hpp>
#pragma warning(pop)
#include <fmt/format.h>
#include <spdlog/spdlog.h>
#include <sol/sol.hpp>
#if ZEN_PLATFORM_WINDOWS
// Used for getting My Documents for default data directory
# include <ShlObj.h>
# pragma comment(lib, "shell32.lib")
std::filesystem::path
PickDefaultStateDirectory()
{
// Pick sensible default
WCHAR myDocumentsDir[MAX_PATH];
HRESULT hRes = SHGetFolderPathW(NULL,
CSIDL_PERSONAL /* My Documents */,
NULL,
SHGFP_TYPE_CURRENT,
/* out */ myDocumentsDir);
if (SUCCEEDED(hRes))
{
wcscat_s(myDocumentsDir, L"\\zen");
return myDocumentsDir;
}
return L"";
}
#else
std::filesystem::path
PickDefaultStateDirectory()
{
return std::filesystem::path("~/.zen");
}
#endif
void
ParseGlobalCliOptions(int argc, char* argv[], ZenServerOptions& GlobalOptions, ZenServiceConfig& ServiceConfig)
{
cxxopts::Options options("zenserver", "Zen Server");
options.add_options()("d, debug", "Enable debugging", cxxopts::value<bool>(GlobalOptions.IsDebug)->default_value("false"));
options.add_options()("help", "Show command line help");
options.add_options()("t, test", "Enable test mode", cxxopts::value<bool>(GlobalOptions.IsTest)->default_value("false"));
options.add_options()("log-id", "Specify id for adding context to log output", cxxopts::value<std::string>(GlobalOptions.LogId));
options.add_options()("data-dir", "Specify persistence root", cxxopts::value<std::filesystem::path>(GlobalOptions.DataDir));
options
.add_option("lifetime", "", "owner-pid", "Specify owning process id", cxxopts::value<int>(GlobalOptions.OwnerPid), "<identifier>");
options.add_option("lifetime",
"",
"child-id",
"Specify id which can be used to signal parent",
cxxopts::value<std::string>(GlobalOptions.ChildId),
"<identifier>");
options.add_option("network",
"p",
"port",
"Select HTTP port",
cxxopts::value<int>(GlobalOptions.BasePort)->default_value("1337"),
"<port number>");
options.add_option("network",
"m",
"mesh",
"Enable mesh network",
cxxopts::value<bool>(ServiceConfig.MeshEnabled)->default_value("true"),
"");
options.add_option("diagnostics",
"",
"crash",
"Simulate a crash",
cxxopts::value<bool>(ServiceConfig.ShouldCrash)->default_value("false"),
"");
options.add_option("cache",
"",
"enable-upstream-cache",
"Whether upstream caching is enabled",
cxxopts::value<bool>(ServiceConfig.UpstreamCacheEnabled)->default_value("false"),
"");
try
{
auto result = options.parse(argc, argv);
if (result.count("help"))
{
ConsoleLog().info("{}", options.help());
exit(0);
}
}
catch (cxxopts::OptionParseException& e)
{
ConsoleLog().error("Error parsing zenserver arguments: {}\n\n{}", e.what(), options.help());
throw;
}
if (GlobalOptions.DataDir.empty())
{
GlobalOptions.DataDir = PickDefaultStateDirectory();
}
}
void
ParseServiceConfig(const std::filesystem::path& DataRoot, ZenServiceConfig& ServiceConfig)
{
using namespace fmt::literals;
std::filesystem::path ConfigScript = DataRoot / "zen_cfg.lua";
zen::IoBuffer LuaScript = zen::IoBufferBuilder::MakeFromFile(ConfigScript.native().c_str());
if (LuaScript)
{
sol::state lua;
// Provide some context to help derive defaults
lua.set("dataroot", DataRoot.native());
lua.open_libraries(sol::lib::base);
// We probably want to limit the scope of this so the script won't see
// any more than it needs to
lua.set_function("getenv", [&](const std::string env) -> sol::object {
std::wstring EnvVarValue;
size_t RequiredSize = 0;
std::wstring EnvWide = zen::Utf8ToWide(env);
_wgetenv_s(&RequiredSize, nullptr, 0, EnvWide.c_str());
if (RequiredSize == 0)
return sol::make_object(lua, sol::lua_nil);
EnvVarValue.resize(RequiredSize);
_wgetenv_s(&RequiredSize, EnvVarValue.data(), RequiredSize, EnvWide.c_str());
return sol::make_object(lua, zen::WideToUtf8(EnvVarValue.c_str()));
});
try
{
sol::load_result config = lua.load(std::string_view((const char*)LuaScript.Data(), LuaScript.Size()), "zencfg");
config();
}
catch (std::exception& e)
{
spdlog::error("config script failure: {}", e.what());
throw std::exception("fatal zen global config script ({}) failure: {}"_format(ConfigScript, e.what()).c_str());
}
ServiceConfig.LegacyCacheEnabled = lua["legacycache"]["enable"].get_or(ServiceConfig.LegacyCacheEnabled);
const std::string path = lua["legacycache"]["readpath"].get_or(std::string());
ServiceConfig.StructuredCacheEnabled = lua["structuredcache"]["enable"].get_or(ServiceConfig.StructuredCacheEnabled);
ServiceConfig.MeshEnabled = lua["mesh"]["enable"].get_or(ServiceConfig.MeshEnabled);
ServiceConfig.UpstreamCacheEnabled = lua["structuredcache"]["upstream"]["enable"].get_or(ServiceConfig.UpstreamCacheEnabled);
}
}
|