nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
settings.cc
Go to the documentation of this file.
1#include "client/settings.h"
2
3#include <raylib.h>
4
5#if defined(_WIN32)
6#include <windows.h>
7#endif
8
9#include <algorithm>
10#include <cerrno>
11#include <cmath>
12#include <cstdio>
13#include <cstdlib>
14#include <cstring>
15#include <fstream>
16#include <string>
17
18#include "absl/strings/str_cat.h"
19#include "client/settings.pb.h"
20#include "content/text.h"
21#include "google/protobuf/text_format.h"
22
23namespace nanommo {
24
25namespace {
26
27std::string Environment(const char* name, const std::string& otherwise) {
28 const char* value = std::getenv(name);
29 return value != nullptr && *value != '\0' ? value : otherwise;
30}
31
32} // namespace
33
34std::string SettingsPath() {
35 const std::string home = Environment("HOME", ".");
36#if defined(__APPLE__)
37 return home + "/Library/Application Support/nanoMMO/settings.txtpb";
38#else
39 return Environment("XDG_CONFIG_HOME", home + "/.config") +
40 "/nanommo/settings.txtpb";
41#endif
42}
43
44absl::StatusOr<Settings> LoadSettings(const std::string& path) {
45 Settings loaded;
46 if (!FileExists(path.c_str())) {
47 return loaded;
48 }
49 const absl::StatusOr<std::string> text = ReadFile(path);
50 if (!text.ok()) {
51 return text.status();
52 }
53 settings::Settings file;
54 const absl::Status parsed = ParseText(*text, file);
55 if (!parsed.ok()) {
56 return absl::InvalidArgumentError(
57 absl::StrCat(path, ", ", parsed.message()));
58 }
59 // A player can write anything into the file, and a volume past full would
60 // play many times louder, so each stays between nothing and full. The text
61 // format reads nan too, which std::clamp lets through, so a volume that isn't
62 // a number keeps its default.
63 const settings::Sound& sound = file.sound();
64 if (sound.has_master() && !std::isnan(sound.master())) {
65 loaded.sound.master = std::clamp(sound.master(), 0.0f, 1.0f);
66 }
67 if (sound.has_music() && !std::isnan(sound.music())) {
68 loaded.sound.music = std::clamp(sound.music(), 0.0f, 1.0f);
69 }
70 if (sound.has_effects() && !std::isnan(sound.effects())) {
71 loaded.sound.effects = std::clamp(sound.effects(), 0.0f, 1.0f);
72 }
73 if (sound.has_muted()) {
74 loaded.sound.muted = sound.muted();
75 }
76 const settings::Graphics& graphics = file.graphics();
77 if (graphics.has_bloom()) {
78 loaded.graphics.bloom = graphics.bloom();
79 }
80 if (graphics.has_cel_shading()) {
81 loaded.graphics.cel_shading = graphics.cel_shading();
82 }
83 if (graphics.has_antialias()) {
84 loaded.graphics.antialias = graphics.antialias();
85 }
86 if (graphics.has_day_night()) {
87 loaded.graphics.day_night = graphics.day_night();
88 }
89 if (graphics.has_weather()) {
90 loaded.graphics.weather = graphics.weather();
91 }
92 const settings::Interface& ui = file.ui();
93 if (ui.has_scale() && !std::isnan(ui.scale())) {
94 loaded.ui.scale = std::clamp(ui.scale(), 0.5f, 2.0f);
95 }
96 if (ui.has_minimap()) {
97 loaded.ui.minimap = ui.minimap();
98 }
99 if (ui.has_minimap_turns()) {
100 loaded.ui.minimap_turns = ui.minimap_turns();
101 }
102 loaded.ui.hotbar = {ui.hotbar().begin(), ui.hotbar().end()};
103 if (ui.has_language()) {
104 loaded.ui.language = ui.language();
105 }
106 const settings::Nameplates& plates = ui.nameplates();
107 NameplateSettings& shown = loaded.ui.nameplates;
108 shown.monsters = plates.has_monsters() ? plates.monsters() : shown.monsters;
109 shown.players = plates.has_players() ? plates.players() : shown.players;
110 shown.npcs = plates.has_npcs() ? plates.npcs() : shown.npcs;
111 shown.yours = plates.has_yours() ? plates.yours() : shown.yours;
112 shown.health = plates.has_health() ? plates.health() : shown.health;
113 shown.levels = plates.has_levels() ? plates.levels() : shown.levels;
114 shown.guilds = plates.has_guilds() ? plates.guilds() : shown.guilds;
115 return loaded;
116}
117
118absl::Status SaveSettings(const std::string& path, const Settings& player) {
119 settings::Settings file;
120 file.mutable_sound()->set_master(player.sound.master);
121 file.mutable_sound()->set_music(player.sound.music);
122 file.mutable_sound()->set_effects(player.sound.effects);
123 file.mutable_sound()->set_muted(player.sound.muted);
124 file.mutable_graphics()->set_bloom(player.graphics.bloom);
125 file.mutable_graphics()->set_cel_shading(player.graphics.cel_shading);
126 file.mutable_graphics()->set_antialias(player.graphics.antialias);
127 file.mutable_graphics()->set_day_night(player.graphics.day_night);
128 file.mutable_graphics()->set_weather(player.graphics.weather);
129 file.mutable_ui()->set_scale(player.ui.scale);
130 file.mutable_ui()->set_minimap(player.ui.minimap);
131 file.mutable_ui()->set_minimap_turns(player.ui.minimap_turns);
132 for (const uint32_t skill : player.ui.hotbar) {
133 file.mutable_ui()->add_hotbar(skill);
134 }
135 file.mutable_ui()->set_language(player.ui.language);
136 const NameplateSettings& shown = player.ui.nameplates;
137 settings::Nameplates& plates = *file.mutable_ui()->mutable_nameplates();
138 plates.set_monsters(shown.monsters);
139 plates.set_players(shown.players);
140 plates.set_npcs(shown.npcs);
141 plates.set_yours(shown.yours);
142 plates.set_health(shown.health);
143 plates.set_levels(shown.levels);
144 plates.set_guilds(shown.guilds);
145 std::string text;
146 if (!google::protobuf::TextFormat::PrintToString(file, &text)) {
147 return absl::InternalError("couldn't print the settings");
148 }
149
150 // raylib makes the folders on the way too.
151 MakeDirectory(GetDirectoryPath(path.c_str()));
152 const std::string written = path + ".new";
153 {
154 std::ofstream out(written, std::ios::binary | std::ios::trunc);
155 out << text;
156 if (!out.flush()) {
157 return absl::UnavailableError(absl::StrCat("couldn't write ", written));
158 }
159 }
160 // Windows' rename won't replace a file that's there, as after the first
161 // save, and MoveFileEx will.
162#if defined(_WIN32)
163 if (!MoveFileExA(written.c_str(), path.c_str(), MOVEFILE_REPLACE_EXISTING)) {
164 return absl::UnavailableError(absl::StrCat(
165 "couldn't replace ", path, ": Windows error ", GetLastError()));
166 }
167#else
168 if (std::rename(written.c_str(), path.c_str()) != 0) {
169 return absl::UnavailableError(
170 absl::StrCat("couldn't replace ", path, ": ", std::strerror(errno)));
171 }
172#endif
173 return absl::OkStatus();
174}
175
176} // namespace nanommo
Definition chat.cc:9
absl::StatusOr< Settings > LoadSettings(const std::string &path)
Definition settings.cc:44
absl::Status ParseText(std::string_view text, google::protobuf::Message &message)
Definition text.cc:32
absl::Status SaveSettings(const std::string &path, const Settings &player)
Definition settings.cc:118
absl::StatusOr< std::string > ReadFile(const std::string &path)
Definition text.cc:43
std::string SettingsPath()
Definition settings.cc:34
bool antialias
Smooths the world's edges, with FXAA.
Definition graphics.h:14
std::vector< uint32_t > hotbar
Definition settings.h:44
NameplateSettings nameplates
Definition settings.h:45
bool monsters
Whose show always.
Definition settings.h:19
What the player sets, which the client keeps between runs.
Definition settings.h:54
InterfaceSettings ui
Definition settings.h:57
SoundSettings sound
Definition settings.h:55
GraphicsSettings graphics
Definition settings.h:56
float effects
The sound effects, apart from the music.
Definition sound.h:12
float master
Where the sliders are, from nothing to full.
Definition sound.h:9