mirror of
https://github.com/celisej567/mcpe.git
synced 2026-09-23 20:09:46 +03:00
Options Logic Cleanup (#71)
* Output/Logging Overhaul * Added StandardOut class * Renamed LOGX macros to LOG_X * Removed LogMsg macros in favor of LOG_X * Added console window for debug Windows builds * Options Refactor * Moved options loading code from AppPlatform classes to Options class * Added AppPlatform::singleton() * Minecraft::m_options is now only accessible via Minecraft::getOptions() (as it should be) * Making this work with SDL2 next * Options Cleanup for SDL2 * Added AppPlatform::hasFileSystemAccess() * Options won't try to load if hasFileSystemAccess returns false. Emscripten build will be happy. --------- Co-authored-by: Brent Da Mage <BrentDaMage@users.noreply.github.com>
This commit is contained in:
@@ -6,6 +6,8 @@
|
||||
SPDX-License-Identifier: BSD-1-Clause
|
||||
********************************************************************/
|
||||
|
||||
#include <fstream>
|
||||
|
||||
#include "Options.hpp"
|
||||
#include "Util.hpp"
|
||||
#include "compat/KeyCodes.hpp"
|
||||
@@ -25,7 +27,7 @@ Options::Option
|
||||
Options::Option::AMBIENT_OCCLUSION(10, "options.ao", false, true),
|
||||
Options::Option::GUI_SCALE (11, "options.guiScale", false, false);
|
||||
|
||||
void Options::initDefaultValues()
|
||||
void Options::_initDefaultValues()
|
||||
{
|
||||
field_238 = 2;
|
||||
field_244 = 1.0f;
|
||||
@@ -201,7 +203,14 @@ void Options::initDefaultValues()
|
||||
|
||||
Options::Options()
|
||||
{
|
||||
initDefaultValues();
|
||||
_initDefaultValues();
|
||||
}
|
||||
|
||||
Options::Options(const std::string& folderPath)
|
||||
{
|
||||
m_filePath = folderPath + "/options.txt";
|
||||
_initDefaultValues();
|
||||
_load();
|
||||
}
|
||||
|
||||
std::string getMessage(const Options::Option& option)
|
||||
@@ -209,14 +218,34 @@ std::string getMessage(const Options::Option& option)
|
||||
return "Options::getMessage - Not implemented";
|
||||
}
|
||||
|
||||
void Options::load()
|
||||
void Options::_load()
|
||||
{
|
||||
// stub
|
||||
std::vector<std::string> strings = readPropertiesFromFile(m_filePath);
|
||||
|
||||
for (int i = 0; i < strings.size(); i += 2)
|
||||
{
|
||||
std::string key = strings[i], value = strings[i + 1];
|
||||
|
||||
if (key == "mp_username")
|
||||
m_playerName = value;
|
||||
else if (key == "ctrl_invertmouse")
|
||||
m_bInvertMouse = readBool(value);
|
||||
else if (key == "ctrl_autojump")
|
||||
m_bAutoJump = readBool(value);
|
||||
else if (key == "gfx_fancygraphics")
|
||||
m_bFancyGraphics = readBool(value);
|
||||
else if (key == "mp_server_visible_default")
|
||||
m_bServerVisibleDefault = readBool(value);
|
||||
else if (key == "gfx_smoothlighting")
|
||||
Minecraft::useAmbientOcclusion = m_bAmbientOcclusion = readBool(value);
|
||||
else if (key == "gfx_viewdistance")
|
||||
m_iViewDistance = readInt(value);
|
||||
}
|
||||
}
|
||||
|
||||
void Options::save()
|
||||
{
|
||||
// stub
|
||||
savePropertiesToFile(m_filePath, getOptionStrings());
|
||||
}
|
||||
|
||||
std::string Options::getMessage(const Options::Option& option)
|
||||
@@ -257,27 +286,59 @@ std::string Options::saveInt(int i)
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
void Options::update(const std::vector<std::string>& strings)
|
||||
std::vector<std::string> Options::readPropertiesFromFile(const std::string& filePath)
|
||||
{
|
||||
for (int i = 0; i<int(strings.size()); i += 2)
|
||||
{
|
||||
std::string key = strings[i], value = strings[i + 1];
|
||||
std::vector<std::string> o;
|
||||
|
||||
if (key == "mp_username")
|
||||
m_playerName = value;
|
||||
else if (key == "ctrl_invertmouse")
|
||||
m_bInvertMouse = readBool(value);
|
||||
else if (key == "ctrl_autojump")
|
||||
m_bAutoJump = readBool(value);
|
||||
else if (key == "gfx_fancygraphics")
|
||||
m_bFancyGraphics = readBool(value);
|
||||
else if (key == "mp_server_visible_default")
|
||||
m_bServerVisibleDefault = readBool(value);
|
||||
else if (key == "gfx_smoothlighting")
|
||||
Minecraft::useAmbientOcclusion = m_bAmbientOcclusion = readBool(value);
|
||||
else if (key == "gfx_viewdistance")
|
||||
m_iViewDistance = readInt(value);
|
||||
const char* const path = filePath.c_str();
|
||||
LOG_I("Loading options from %s", path);
|
||||
|
||||
std::ifstream ifs(path);
|
||||
if (!ifs.is_open())
|
||||
{
|
||||
LOG_W("%s doesn't exist, resetting to defaults", path);
|
||||
return o;
|
||||
}
|
||||
|
||||
std::string str;
|
||||
while (true)
|
||||
{
|
||||
if (!std::getline(ifs, str, '\n'))
|
||||
break;
|
||||
|
||||
if (str.empty() || str[0] == '#')
|
||||
continue;
|
||||
|
||||
std::stringstream ss;
|
||||
ss << str;
|
||||
|
||||
std::string key, value;
|
||||
if (std::getline(ss, key, ':') && std::getline(ss, value))
|
||||
{
|
||||
o.push_back(key);
|
||||
o.push_back(value);
|
||||
}
|
||||
}
|
||||
|
||||
return o;
|
||||
}
|
||||
|
||||
void Options::savePropertiesToFile(const std::string& filePath, std::vector<std::string> properties)
|
||||
{
|
||||
assert(properties.size() % 2 == 0);
|
||||
|
||||
std::ofstream os;
|
||||
os.open(filePath.c_str());
|
||||
if (!os.is_open())
|
||||
{
|
||||
LOG_E("Failed to read %s", filePath);
|
||||
return;
|
||||
}
|
||||
|
||||
os << "#Config file for Minecraft PE. The # at the start denotes a comment, removing it makes it a command.\n\n";
|
||||
|
||||
for (int i = 0; i < properties.size(); i += 2)
|
||||
os << properties[i] << ':' << properties[i + 1] << '\n';
|
||||
}
|
||||
|
||||
std::vector<std::string> Options::getOptionStrings()
|
||||
|
||||
@@ -67,44 +67,23 @@ class Options
|
||||
public:
|
||||
struct Option;
|
||||
struct KeyBind;
|
||||
public:
|
||||
Options();
|
||||
void initDefaultValues();
|
||||
void load();
|
||||
void save();
|
||||
std::string getMessage(const Options::Option&);
|
||||
void update(const std::vector<std::string>& string);
|
||||
std::vector<std::string> getOptionStrings();
|
||||
|
||||
public:
|
||||
private:
|
||||
static bool readBool(const std::string& str);
|
||||
static int readInt(const std::string& str);
|
||||
static std::string saveBool(bool b);
|
||||
static std::string saveInt(int i);
|
||||
static std::vector<std::string> readPropertiesFromFile(const std::string& filePath);
|
||||
static void savePropertiesToFile(const std::string& filePath, std::vector<std::string> properties);
|
||||
|
||||
private:
|
||||
void _initDefaultValues();
|
||||
void _load();
|
||||
public:
|
||||
struct Option
|
||||
{
|
||||
bool field_0;
|
||||
bool field_1;
|
||||
std::string str;
|
||||
int field_1C;
|
||||
|
||||
Option(int i, const std::string& str, bool b1, bool b2) : field_0(b1), field_1(b2), str(str), field_1C(i) {}
|
||||
|
||||
static Option MUSIC;
|
||||
static Option SOUND;
|
||||
static Option INVERT_MOUSE;
|
||||
static Option SENSITIVITY;
|
||||
static Option RENDER_DISTANCE;
|
||||
static Option VIEW_BOBBING;
|
||||
static Option ANAGLYPH;
|
||||
static Option LIMIT_FRAMERATE;
|
||||
static Option DIFFICULTY;
|
||||
static Option GRAPHICS;
|
||||
static Option AMBIENT_OCCLUSION;
|
||||
static Option GUI_SCALE;
|
||||
};
|
||||
Options();
|
||||
Options(const std::string& folderPath);
|
||||
void save();
|
||||
std::string getMessage(const Options::Option&);
|
||||
std::vector<std::string> getOptionStrings();
|
||||
|
||||
int getKey(eKeyMappingIndex idx)
|
||||
{
|
||||
@@ -115,6 +94,9 @@ public:
|
||||
return getKey(idx) == keyCode;
|
||||
}
|
||||
|
||||
private:
|
||||
std::string m_filePath;
|
||||
|
||||
public:
|
||||
float field_0;
|
||||
float m_fMasterVolume;
|
||||
@@ -143,5 +125,29 @@ public:
|
||||
bool m_bServerVisibleDefault;
|
||||
bool m_bAutoJump;
|
||||
bool m_bDebugText;
|
||||
|
||||
public:
|
||||
struct Option
|
||||
{
|
||||
bool field_0;
|
||||
bool field_1;
|
||||
std::string str;
|
||||
int field_1C;
|
||||
|
||||
Option(int i, const std::string& str, bool b1, bool b2) : field_0(b1), field_1(b2), str(str), field_1C(i) {}
|
||||
|
||||
static Option MUSIC;
|
||||
static Option SOUND;
|
||||
static Option INVERT_MOUSE;
|
||||
static Option SENSITIVITY;
|
||||
static Option RENDER_DISTANCE;
|
||||
static Option VIEW_BOBBING;
|
||||
static Option ANAGLYPH;
|
||||
static Option LIMIT_FRAMERATE;
|
||||
static Option DIFFICULTY;
|
||||
static Option GRAPHICS;
|
||||
static Option AMBIENT_OCCLUSION;
|
||||
static Option GUI_SCALE;
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
46
source/common/StandardOut.cpp
Normal file
46
source/common/StandardOut.cpp
Normal file
@@ -0,0 +1,46 @@
|
||||
#include <iostream>
|
||||
#include <stdarg.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#endif
|
||||
|
||||
#include "StandardOut.hpp"
|
||||
#include "Util.hpp"
|
||||
|
||||
StandardOut* const StandardOut::singleton()
|
||||
{
|
||||
// This is automatically allocated when accessed for the first time,
|
||||
// and automatically deallocated when runtime concludes.
|
||||
static StandardOut standardOut = StandardOut();
|
||||
return &standardOut;
|
||||
}
|
||||
|
||||
void StandardOut::print(const char* const str)
|
||||
{
|
||||
std::cout << str << std::endl;
|
||||
#ifdef _WIN32
|
||||
OutputDebugStringA(str);
|
||||
OutputDebugStringA("\n");
|
||||
#endif
|
||||
}
|
||||
|
||||
void StandardOut::print(std::string str)
|
||||
{
|
||||
print(str.c_str());
|
||||
}
|
||||
|
||||
void StandardOut::vprintf(const char* const fmt, va_list argPtr)
|
||||
{
|
||||
print(Util::vformat(fmt, argPtr));
|
||||
}
|
||||
|
||||
void StandardOut::printf(const char* const fmt, ...)
|
||||
{
|
||||
va_list argList;
|
||||
va_start(argList, fmt);
|
||||
|
||||
vprintf(fmt, argList);
|
||||
|
||||
va_end(argList);
|
||||
}
|
||||
37
source/common/StandardOut.hpp
Normal file
37
source/common/StandardOut.hpp
Normal file
@@ -0,0 +1,37 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
class StandardOut
|
||||
{
|
||||
public:
|
||||
static StandardOut* const singleton();
|
||||
|
||||
void print(const char* const str);
|
||||
void print(std::string str);
|
||||
void vprintf(const char* const fmt, va_list argPtr);
|
||||
void printf(const char* const fmt, ...);
|
||||
};
|
||||
|
||||
#ifdef _DEBUG
|
||||
|
||||
#define LOG(...) StandardOut::singleton()->printf(__VA_ARGS__)
|
||||
|
||||
#ifdef PLATFORM_ANDROID
|
||||
#define LOG_I(...) __android_log_print(ANDROID_LOG_INFO, "MinecraftPE", __VA_ARGS__)
|
||||
#define LOG_W(...) __android_log_print(ANDROID_LOG_WARN, "MinecraftPE", __VA_ARGS__)
|
||||
#define LOG_E(...) __android_log_print(ANDROID_LOG_ERROR, "MinecraftPE", __VA_ARGS__)
|
||||
#else
|
||||
#define LOG_I(...) LOG("[Info]: " __VA_ARGS__)
|
||||
#define LOG_W(...) LOG("[WARN]: " __VA_ARGS__)
|
||||
#define LOG_E(...) LOG("[ERROR]: " __VA_ARGS__)
|
||||
#endif
|
||||
|
||||
#else
|
||||
|
||||
#define LOG(...)
|
||||
#define LOG_I(...)
|
||||
#define LOG_W(...)
|
||||
#define LOG_E(...)
|
||||
|
||||
#endif
|
||||
@@ -46,7 +46,7 @@ std::string Util::stringTrim(const std::string& str)
|
||||
|
||||
std::string Util::vformat(const char *fmt, va_list argPtr)
|
||||
{
|
||||
char str[8192];
|
||||
char str[1024];
|
||||
|
||||
vsnprintf(str, sizeof(str), fmt, argPtr);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user