aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.Client/Settings.cpp
diff options
context:
space:
mode:
authordaoge_cmd <3523206925@qq.com>2026-03-04 16:18:47 +0800
committerdaoge_cmd <3523206925@qq.com>2026-03-04 17:29:43 +0800
commitd112090fde200c545a70ec5dc33fe91cca0f26ec (patch)
tree9adf5fea35a3e1bccb40d94638fdf63f45baedd5 /Minecraft.Client/Settings.cpp
parent8ecfc525471720012f36a0016d88a4f0f4cfaa1d (diff)
feat: headless server
Diffstat (limited to 'Minecraft.Client/Settings.cpp')
-rw-r--r--Minecraft.Client/Settings.cpp80
1 files changed, 76 insertions, 4 deletions
diff --git a/Minecraft.Client/Settings.cpp b/Minecraft.Client/Settings.cpp
index 89946773..4223b001 100644
--- a/Minecraft.Client/Settings.cpp
+++ b/Minecraft.Client/Settings.cpp
@@ -1,18 +1,90 @@
#include "stdafx.h"
#include "Settings.h"
+#include "..\Minecraft.World\File.h"
#include "..\Minecraft.World\StringHelpers.h"
+#include <fstream>
+
+static wstring ParsePropertyText(const string &text)
+{
+ return trimString(convStringToWstring(text));
+}
+
+static bool TryParseBoolean(const wstring &text, bool defaultValue)
+{
+ wstring lowered = toLower(trimString(text));
+ if (lowered == L"true" || lowered == L"1" || lowered == L"yes" || lowered == L"on")
+ return true;
+ if (lowered == L"false" || lowered == L"0" || lowered == L"no" || lowered == L"off")
+ return false;
+ return defaultValue;
+}
-// 4J - TODO - serialise/deserialise from file
Settings::Settings(File *file)
{
+ if (file != NULL)
+ {
+ filePath = file->getPath();
+ }
+
+ if (filePath.empty())
+ return;
+
+ std::ifstream stream(wstringtofilename(filePath), std::ios::in | std::ios::binary);
+ if (!stream.is_open())
+ return;
+
+ string line;
+ while (std::getline(stream, line))
+ {
+ if (!line.empty() && line[line.size() - 1] == '\r')
+ line.erase(line.size() - 1);
+
+ if (line.size() >= 3 &&
+ (unsigned char)line[0] == 0xEF &&
+ (unsigned char)line[1] == 0xBB &&
+ (unsigned char)line[2] == 0xBF)
+ {
+ line.erase(0, 3);
+ }
+
+ size_t commentPos = line.find_first_of("#;");
+ if (commentPos != string::npos && line.find_first_not_of(" \t") == commentPos)
+ continue;
+
+ size_t separatorPos = line.find('=');
+ if (separatorPos == string::npos)
+ continue;
+
+ wstring key = ParsePropertyText(line.substr(0, separatorPos));
+ if (key.empty())
+ continue;
+
+ wstring value = ParsePropertyText(line.substr(separatorPos + 1));
+ properties[key] = value;
+ }
}
void Settings::generateNewProperties()
{
+ saveProperties();
}
void Settings::saveProperties()
{
+ if (filePath.empty())
+ return;
+
+ std::ofstream stream(wstringtofilename(filePath), std::ios::out | std::ios::binary | std::ios::trunc);
+ if (!stream.is_open())
+ return;
+
+ stream << "# MinecraftConsoles dedicated server properties\r\n";
+ for (unordered_map<wstring, wstring>::const_iterator it = properties.begin(); it != properties.end(); ++it)
+ {
+ string key = string(wstringtochararray(it->first));
+ string value = string(wstringtochararray(it->second));
+ stream << key << "=" << value << "\r\n";
+ }
}
wstring Settings::getString(const wstring& key, const wstring& defaultValue)
@@ -39,17 +111,17 @@ bool Settings::getBoolean(const wstring& key, bool defaultValue)
{
if(properties.find(key) == properties.end())
{
- properties[key] = _toString<bool>(defaultValue);
+ properties[key] = defaultValue ? L"true" : L"false";
saveProperties();
}
MemSect(35);
- bool retval = _fromString<bool>(properties[key]);
+ bool retval = TryParseBoolean(properties[key], defaultValue);
MemSect(0);
return retval;
}
void Settings::setBooleanAndSave(const wstring& key, bool value)
{
- properties[key] = _toString<bool>(value);
+ properties[key] = value ? L"true" : L"false";
saveProperties();
} \ No newline at end of file