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:
Brent
2023-09-04 04:11:36 -05:00
committed by GitHub
parent cd4c469571
commit 2e55f99a54
25 changed files with 355 additions and 275 deletions

View File

@@ -289,63 +289,7 @@ Texture AppPlatform_sdl::loadTexture(const std::string& path, bool b)
return out;
}
std::string AppPlatform_sdl::getOptionsFilePath() const
bool AppPlatform_sdl::hasFileSystemAccess()
{
return _storageDir + "/options.txt";
}
std::vector<std::string> AppPlatform_sdl::getOptionStrings()
{
// TODO: This isn't specific to SDL2. Why isn't it in an AppPlatform base class?
std::vector<std::string> o;
LOG_I("Storage dir is %s", _storageDir.c_str());
std::ifstream ifs(getOptionsFilePath().c_str());
if (!ifs.is_open())
{
LOG_W("options.txt doesn't exist, resetting to defaults");
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 AppPlatform_sdl::setOptionStrings(const std::vector<std::string>& str)
{
// TODO: This isn't specific to SDL2. Why isn't it in an AppPlatform base class?
assert(str.size() % 2 == 0);
std::ofstream os;
os.open(getOptionsFilePath().c_str());
if (!os.is_open())
{
LOG_E("Failed to read options.txt");
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 < int(str.size()); i += 2)
os << str[i] << ':' << str[i + 1] << '\n';
return true;
}

View File

@@ -11,10 +11,9 @@ public:
void saveScreenshot(const std::string& fileName, int width, int height) override;
Texture loadTexture(const std::string& path, bool b = false) override;
std::vector<std::string> getOptionStrings() override;
void setOptionStrings(const std::vector<std::string>& str) override;
bool hasFileSystemAccess() override;
protected:
void ensureDirectoryExists(const char* path) override;
std::string getOptionsFilePath() const;
};

View File

@@ -176,47 +176,9 @@ Texture AppPlatform_windows::loadTexture(const std::string& str, bool b)
return Texture(width, height, img2, 1, 0);
}
std::vector<std::string> AppPlatform_windows::getOptionStrings()
bool AppPlatform_windows::hasFileSystemAccess()
{
std::vector<std::string> o;
std::ifstream ifs("options.txt");
if (!ifs.is_open())
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 AppPlatform_windows::setOptionStrings(const std::vector<std::string>& str)
{
assert(str.size() % 2 == 0);
std::ofstream os("options.txt");
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 < int(str.size()); i += 2)
os << str[i] << ':' << str[i + 1] << '\n';
return true;
}
std::string AppPlatform_windows::getPatchData()

View File

@@ -34,7 +34,6 @@ public:
void showDialog(eDialogType) override;
std::string getDateString(int time) override;
Texture loadTexture(const std::string& str, bool b) override;
std::vector<std::string> getOptionStrings() override;
// Also add these to allow proper turning within the game.
void recenterMouse() override;
@@ -46,9 +45,8 @@ public:
// Also add these to allow proper text input within the game.
bool shiftPressed() override { return m_bShiftPressed; }
void setShiftPressed(bool b) { m_bShiftPressed = b; }
// Also add these to allow saving options.
void setOptionStrings(const std::vector <std::string>& str) override;
bool hasFileSystemAccess() override;
// Also add this to allow dynamic texture patching.
std::string getPatchData() override;

View File

@@ -383,7 +383,7 @@
<ClInclude Include="$(MC_ROOT)\thirdparty\zlib\zutil.h" />
<ClInclude Include="$(MC_ROOT)\compat\KeyCodes.hpp" />
<ClInclude Include="$(MC_ROOT)\compat\SDLKeyCodes.h" />
<ClInclude Include="..\..\..\source\common\Logger.hpp" />
<ClInclude Include="$(MC_ROOT)\source\common\Logger.hpp" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="$(MC_ROOT)\compat\GLExt.cpp" />
@@ -391,7 +391,7 @@
<ClCompile Include="$(MC_ROOT)\platforms\windows\AppPlatform_windows.cpp" />
<ClCompile Include="$(MC_ROOT)\platforms\windows\main.cpp" />
<ClCompile Include="$(MC_ROOT)\platforms\windows\SoundSystemWindows.cpp" />
<ClInclude Include="..\LoggerWindows.hpp" />
<ClInclude Include="$(MC_ROOT)\platforms\windows\LoggerWindows.hpp" />
<ClCompile Include="$(MC_ROOT)\source\App.cpp" />
<ClCompile Include="$(MC_ROOT)\source\AppPlatform.cpp" />
<ClCompile Include="$(MC_ROOT)\source\client\gui\components\AvailableGamesList.cpp" />
@@ -729,8 +729,8 @@
<ClCompile Include="$(MC_ROOT)\thirdparty\zlib\trees.c" />
<ClCompile Include="$(MC_ROOT)\thirdparty\zlib\uncompr.c" />
<ClCompile Include="$(MC_ROOT)\thirdparty\zlib\zutil.c" />
<ClCompile Include="..\..\..\source\common\Logger.cpp" />
<ClCompile Include="..\LoggerWindows.cpp" />
<ClCompile Include="$(MC_ROOT)\source\common\Logger.cpp" />
<ClCompile Include="$(MC_ROOT)\platforms\windows\LoggerWindows.cpp" />
</ItemGroup>
<ItemGroup>
<Text Include="$(MC_ROOT)\thirdparty\raknet\CMakeLists.txt" />

View File

@@ -1178,6 +1178,9 @@
<ClInclude Include="..\..\..\source\common\Logger.hpp">
<Filter>source\common</Filter>
</ClInclude>
<ClInclude Include="..\..\..\source\common\StandardOut.hpp">
<Filter>source\common</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="$(MC_ROOT)\thirdparty\raknet\TwoWayAuthentication.cpp">
@@ -2206,10 +2209,10 @@
<ClCompile Include="$(MC_ROOT)\platforms\windows\SoundSystemWindows.cpp">
<Filter>source\platforms\windows</Filter>
</ClCompile>
<ClCompile Include="..\LoggerWindows.cpp">
<ClCompile Include="$(MC_ROOT)\platforms\windows\LoggerWindows.cpp">
<Filter>source\platforms\windows</Filter>
</ClCompile>
<ClCompile Include="..\..\..\source\common\Logger.cpp">
<ClCompile Include="$(MC_ROOT)\source\common\Logger.cpp">
<Filter>source\common</Filter>
</ClCompile>
</ItemGroup>