Веб-сайт самохостера Lotigara

summaryrefslogtreecommitdiff
path: root/source/game/StarRootLoader.cpp
blob: b2be4e9d8266db3e8383ae6816e519758a876ab1 (plain)
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include "StarRootLoader.hpp"
#include "StarLexicalCast.hpp"
#include "StarJsonExtra.hpp"

namespace Star {

Json const BaseAssetsSettings = Json::parseJson(R"JSON(
    {
      "assetTimeToLive" : 30,

      // In seconds, audio less than this long will be decompressed in memory.
      "audioDecompressLimit" : 4.0,

      "workerPoolSize" : 2,

      "pathIgnore" : [
        "/\\.",
        "/~",
        "thumbs\\.db$",
        "\\.bak$",
        "\\.tmp$",
        "\\.zip$",
        "\\.orig$",
        "\\.fail$",
        "\\.psd$",
        "\\.tmx$"
      ],

      "digestIgnore" : [
        "\\.ogg$",
        "\\.wav$",
        "\\.abc$"
      ]
    }
  )JSON");

Json const BaseDefaultConfiguration = Json::parseJson(R"JSON(
    {
      "configurationVersion" : {
        "basic" : 2
      },

      "gameServerPort" : 21025,
)JSON"
#ifdef STAR_SYSTEM_WINDOWS
                                                      R"JSON(
      "gameServerBind" : "*",
      "queryServerBind" : "*",
      "rconServerBind" : "*",
)JSON"
#else
                                                      R"JSON(
      "gameServerBind" : "::",
)JSON"
#endif
R"JSON(
      "serverUsers" : {},
      "allowAnonymousConnections" : true,

      "bannedUuids" : [],
      "bannedIPs" : [],

      "serverName" : "A Starbound Server",
      "maxPlayers" : 8,
      "maxTeamSize" : 4,
      "serverFidelity" : "automatic",

      "checkAssetsDigest" : false,

      "safeScripts" : true,
      "scriptRecursionLimit" : 100,
      "scriptInstructionLimit" : 10000000,
      "scriptProfilingEnabled" : false,
      "scriptInstructionMeasureInterval" : 10000,

      "allowAdminCommands" : true,
      "allowAdminCommandsFromAnyone" : false,
      "anonymousConnectionsAreAdmin" : false,
      "connectionSettings" : {
        "compression" : "Zstd"
      },

      "clientP2PJoinable" : true,
      "clientIPJoinable" : false,

      "clearUniverseFiles" : false,
      "clearPlayerFiles" : false,
      "playerBackupFileCount" : 3,

      "tutorialMessages" : true,

      "interactiveHighlight" : true,

      "monochromeLighting" : false,

      "crafting" : {
        "filterHaveMaterials" : false
      },

      "inventory" : {
        "pickupToActionBar" : true
      },

      "discord" : {
        "activityDetails" : "<playerName> | <worldName>"
      }
    }
  )JSON");

RootLoader::RootLoader(Defaults defaults) {
  String baseConfigFile;
  Maybe<String> userConfigFile;

  addParameter("bootconfig", "bootconfig", Optional,
      strf("Boot time configuration file, defaults to sbinit.config"));
  addParameter("logfile", "logfile", Optional,
      strf("Log to the given logfile relative to the root directory, defaults to {}",
        defaults.logFile ? *defaults.logFile : "no log file"));
  addParameter("loglevel", "level", Optional,
      strf("Sets the logging level (debug|info|warn|error), defaults to {}",
        LogLevelNames.getRight(defaults.logLevel)));
  addSwitch("quiet", strf("Do not log to stdout, defaults to {}", defaults.quiet));
  addSwitch("verbose", strf("Log to stdout, defaults to {}", !defaults.quiet));
  addSwitch("runtimeconfig",
      strf("Sets the path to the runtime configuration storage file relative to root directory, defauts to {}",
        defaults.runtimeConfigFile ? *defaults.runtimeConfigFile : "no storage file"));
  m_defaults = std::move(defaults);
}

pair<Root::Settings, RootLoader::Options> RootLoader::parseOrDie(
    StringList const& cmdLineArguments) const {
  auto options = VersionOptionParser::parseOrDie(cmdLineArguments);
  return {rootSettingsForOptions(options), options};
}

pair<RootUPtr, RootLoader::Options> RootLoader::initOrDie(StringList const& cmdLineArguments) const {
  auto p = parseOrDie(cmdLineArguments);
  auto root = make_unique<Root>(p.first);
  return {std::move(root), p.second};
}

pair<Root::Settings, RootLoader::Options> RootLoader::commandParseOrDie(int argc, char** argv) {
  auto options = VersionOptionParser::commandParseOrDie(argc, argv);
  return {rootSettingsForOptions(options), options};
}

pair<RootUPtr, RootLoader::Options> RootLoader::commandInitOrDie(int argc, char** argv) {
  auto p = commandParseOrDie(argc, argv);
  auto root = make_unique<Root>(p.first);
  return {std::move(root), p.second};
}

Root::Settings RootLoader::rootSettingsForOptions(Options const& options) const {
  try {
    String bootConfigFile = options.parameters.value("bootconfig").maybeFirst().value("sbinit.config");
    Json bootConfig = Json::parseJson(File::readFileString(bootConfigFile));

    Json assetsSettings = jsonMerge(
        BaseAssetsSettings,
        m_defaults.additionalAssetsSettings,
        bootConfig.get("assetsSettings", {})
      );

    Root::Settings rootSettings;
    rootSettings.assetsSettings.assetTimeToLive = assetsSettings.getInt("assetTimeToLive");
    rootSettings.assetsSettings.audioDecompressLimit = assetsSettings.getFloat("audioDecompressLimit");
    rootSettings.assetsSettings.workerPoolSize = assetsSettings.getUInt("workerPoolSize");
    rootSettings.assetsSettings.missingImage = assetsSettings.optString("missingImage");
    rootSettings.assetsSettings.missingAudio = assetsSettings.optString("missingAudio");
    rootSettings.assetsSettings.pathIgnore = jsonToStringList(assetsSettings.get("pathIgnore"));
    rootSettings.assetsSettings.digestIgnore = jsonToStringList(assetsSettings.get("digestIgnore"));

    rootSettings.assetDirectories = jsonToStringList(bootConfig.get("assetDirectories", JsonArray()));
    rootSettings.assetSources     = jsonToStringList(bootConfig.get("assetSources",     JsonArray()));

    rootSettings.defaultConfiguration = jsonMerge(
        BaseDefaultConfiguration,
        m_defaults.additionalDefaultConfiguration,
        bootConfig.get("defaultConfiguration", {})
      );

    rootSettings.storageDirectory = bootConfig.getString("storageDirectory");
    rootSettings.logDirectory = bootConfig.optString("logDirectory");
    rootSettings.logFile = options.parameters.value("logfile").maybeFirst().orMaybe(m_defaults.logFile);
    rootSettings.logFileBackups = bootConfig.getUInt("logFileBackups", 10);
    rootSettings.includeUGC = bootConfig.getBool("includeUGC", true);

    if (auto ll = options.parameters.value("loglevel").maybeFirst())
      rootSettings.logLevel = LogLevelNames.getLeft(*ll);
    else
      rootSettings.logLevel = m_defaults.logLevel;

    if (options.switches.contains("quiet"))
      rootSettings.quiet = true;
    else if (options.switches.contains("verbose"))
      rootSettings.quiet = false;
    else
      rootSettings.quiet = m_defaults.quiet;

    if (auto rc = options.parameters.value("runtimeconfig").maybeFirst())
      rootSettings.runtimeConfigFile = *rc;
    else
      rootSettings.runtimeConfigFile = m_defaults.runtimeConfigFile;

    return rootSettings;

  } catch (std::exception const& e) {
    throw StarException("Could not perform initial Root load", e);
  }
}

}