From b78bed90c5a437020ed3ec50c80408d9a41fa95d Mon Sep 17 00:00:00 2001 From: AnyOldName3 Date: Wed, 5 May 2021 23:51:07 +0100 Subject: [PATCH 1/9] Add CMake-based base64 port --- cmake/base64.cmake | 74 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 cmake/base64.cmake diff --git a/cmake/base64.cmake b/cmake/base64.cmake new file mode 100644 index 000000000..7931758bb --- /dev/null +++ b/cmake/base64.cmake @@ -0,0 +1,74 @@ +# math(EXPR "...") can't parse hex until 3.13 +cmake_minimum_required(VERSION 3.13) + +if (NOT DEFINED INPUT_FILE) + message(STATUS "Usage: cmake -DINPUT_FILE=\"infile.ext\" -DOUTPUT_FILE=\"out.txt\" -P base64.cmake") + message(FATAL_ERROR "INPUT_FILE not specified") +endif() + +if (NOT DEFINED OUTPUT_FILE) + message(STATUS "Usage: cmake -DINPUT_FILE=\"infile.ext\" -DOUTPUT_FILE=\"out.txt\" -P base64.cmake") + message(FATAL_ERROR "OUTPUT_FILE not specified") +endif() + +if (NOT EXISTS ${INPUT_FILE}) + message(FATAL_ERROR "INPUT_FILE ${INPUT_FILE} does not exist") +endif() + +set(lut "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/") + +file(READ "${INPUT_FILE}" hexContent HEX) + +set(base64Content "") +while(TRUE) + string(LENGTH "${hexContent}" tailLength) + if (tailLength LESS 1) + break() + endif() + + string(SUBSTRING "${hexContent}" 0 6 head) + # base64 works on three-byte chunks. Pad. + string(LENGTH "${head}" headLength) + if (headLength LESS 6) + set(hexContent "") + math(EXPR padSize "6 - ${headLength}") + set(pad "") + foreach(i RANGE 1 ${padSize}) + string(APPEND pad "0") + endforeach() + string(APPEND head "${pad}") + else() + string(SUBSTRING "${hexContent}" 6 -1 hexContent) + set(padSize 0) + endif() + + # get six-bit slices + math(EXPR first "0x${head} >> 18") + math(EXPR second "(0x${head} & 0x3F000) >> 12") + math(EXPR third "(0x${head} & 0xFC0) >> 6") + math(EXPR fourth "0x${head} & 0x3F") + + # first two characters are always needed to represent the first byte + string(SUBSTRING "${lut}" ${first} 1 char) + string(APPEND base64Content "${char}") + string(SUBSTRING "${lut}" ${second} 1 char) + string(APPEND base64Content "${char}") + + # if there's no second byte, pad with = + if (NOT padSize EQUAL 4) + string(SUBSTRING "${lut}" ${third} 1 char) + string(APPEND base64Content "${char}") + else() + string(APPEND base64Content "=") + endif() + + # if there's no third byte, pad with = + if (padSize EQUAL 0) + string(SUBSTRING "${lut}" ${fourth} 1 char) + string(APPEND base64Content "${char}") + else() + string(APPEND base64Content "=") + endif() +endwhile() + +file(WRITE "${OUTPUT_FILE}" "${base64Content}") \ No newline at end of file From 0d737a3501f3f7e97be5ae906235597d69fbc982 Mon Sep 17 00:00:00 2001 From: AnyOldName3 Date: Sun, 9 May 2021 21:00:49 +0100 Subject: [PATCH 2/9] Create defaults.bin at configure time --- CMakeLists.txt | 8 ++++---- apps/openmw/CMakeLists.txt | 2 +- cmake/OpenMWMacros.cmake | 12 ++++++++++++ 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 104140107..b97657ffd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -438,8 +438,8 @@ endif (APPLE) # Other files -configure_resource_file(${OpenMW_SOURCE_DIR}/files/settings-default.cfg - "${OpenMW_BINARY_DIR}" "settings-default.cfg") +pack_resource_file(${OpenMW_SOURCE_DIR}/files/settings-default.cfg + "${OpenMW_BINARY_DIR}" "defaults.bin") configure_resource_file(${OpenMW_SOURCE_DIR}/files/openmw.appdata.xml "${OpenMW_BINARY_DIR}" "openmw.appdata.xml") @@ -808,7 +808,7 @@ elseif(NOT APPLE) INSTALL(FILES "${OpenMW_SOURCE_DIR}/README.md" DESTINATION "." RENAME "README.txt") INSTALL(FILES "${OpenMW_SOURCE_DIR}/LICENSE" DESTINATION "." RENAME "LICENSE.txt") INSTALL(FILES "${OpenMW_SOURCE_DIR}/files/mygui/DejaVuFontLicense.txt" DESTINATION ".") - INSTALL(FILES "${INSTALL_SOURCE}/settings-default.cfg" DESTINATION ".") + INSTALL(FILES "${INSTALL_SOURCE}/defaults.bin" DESTINATION ".") INSTALL(FILES "${INSTALL_SOURCE}/gamecontrollerdb.txt" DESTINATION ".") INSTALL(DIRECTORY "${INSTALL_SOURCE}/resources" DESTINATION ".") @@ -916,7 +916,7 @@ elseif(NOT APPLE) ENDIF(BUILD_OPENCS) # Install global configuration files - INSTALL(FILES "${INSTALL_SOURCE}/settings-default.cfg" DESTINATION "${SYSCONFDIR}" COMPONENT "openmw") + INSTALL(FILES "${INSTALL_SOURCE}/defaults.bin" DESTINATION "${SYSCONFDIR}" COMPONENT "openmw") INSTALL(FILES "${INSTALL_SOURCE}/openmw.cfg.install" DESTINATION "${SYSCONFDIR}" RENAME "openmw.cfg" COMPONENT "openmw") INSTALL(FILES "${INSTALL_SOURCE}/resources/version" DESTINATION "${SYSCONFDIR}" COMPONENT "openmw") INSTALL(FILES "${INSTALL_SOURCE}/gamecontrollerdb.txt" DESTINATION "${SYSCONFDIR}" COMPONENT "openmw") diff --git a/apps/openmw/CMakeLists.txt b/apps/openmw/CMakeLists.txt index 5e5bb8a9f..3fb762d30 100644 --- a/apps/openmw/CMakeLists.txt +++ b/apps/openmw/CMakeLists.txt @@ -198,7 +198,7 @@ if(APPLE) add_subdirectory(../../files/ ${CMAKE_CURRENT_BINARY_DIR}/files) - configure_file("${OpenMW_BINARY_DIR}/settings-default.cfg" ${BUNDLE_RESOURCES_DIR} COPYONLY) + configure_file("${OpenMW_BINARY_DIR}/defaults.bin" ${BUNDLE_RESOURCES_DIR} COPYONLY) configure_file("${OpenMW_BINARY_DIR}/openmw.cfg" ${BUNDLE_RESOURCES_DIR} COPYONLY) configure_file("${OpenMW_BINARY_DIR}/gamecontrollerdb.txt" ${BUNDLE_RESOURCES_DIR} COPYONLY) diff --git a/cmake/OpenMWMacros.cmake b/cmake/OpenMWMacros.cmake index 2408cae2b..1621a08cf 100644 --- a/cmake/OpenMWMacros.cmake +++ b/cmake/OpenMWMacros.cmake @@ -199,6 +199,18 @@ macro (configure_resource_file source_path destination_dir_base dest_path_relati endif (multi_config) endmacro (configure_resource_file) +macro (pack_resource_file source_path destination_dir_base dest_path_relative) + get_generator_is_multi_config(multi_config) + if (multi_config) + foreach(cfgtype ${CMAKE_CONFIGURATION_TYPES}) + execute_process(COMMAND ${CMAKE_COMMAND} "-DINPUT_FILE=${source_path}" "-DOUTPUT_FILE=${destination_dir_base}/${cfgtype}/${dest_path_relative}" -P "${CMAKE_SOURCE_DIR}/cmake/base64.cmake") + endforeach(cfgtype) + else (multi_config) + execute_process(COMMAND ${CMAKE_COMMAND} "-DINPUT_FILE=${source_path}" "-DOUTPUT_FILE=${destination_dir_base}/${dest_path_relative}" -P "${CMAKE_SOURCE_DIR}/cmake/base64.cmake") + endif (multi_config) + set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${source_path}") +endmacro (pack_resource_file) + macro (copy_all_resource_files source_dir destination_dir_base destination_dir_relative files) foreach (f ${files}) get_filename_component(filename ${f} NAME) From 92325976e94c0440181bbbe1b365404dfa8d1d3b Mon Sep 17 00:00:00 2001 From: AnyOldName3 Date: Sun, 9 May 2021 21:13:34 +0100 Subject: [PATCH 3/9] Update documentation to refer to defaults.bin --- apps/launcher/maindialog.cpp | 4 ++-- apps/openmw/engine.cpp | 2 +- components/settings/parser.cpp | 2 +- components/settings/settings.cpp | 2 +- docs/source/reference/modding/settings/index.rst | 14 ++++++++++++-- files/settings-default.cfg | 7 ++++--- 6 files changed, 21 insertions(+), 10 deletions(-) diff --git a/apps/launcher/maindialog.cpp b/apps/launcher/maindialog.cpp index 692415309..9d497e879 100644 --- a/apps/launcher/maindialog.cpp +++ b/apps/launcher/maindialog.cpp @@ -436,7 +436,7 @@ bool Launcher::MainDialog::setupGraphicsSettings() // Something's very wrong if we can't find the file at all. else { cfgError(tr("Error reading OpenMW configuration file"), - tr("
Could not find settings-default.cfg

\ + tr("
Could not find defaults.bin

\ The problem may be due to an incomplete installation of OpenMW.
\ Reinstalling OpenMW may resolve the problem.")); return false; @@ -447,7 +447,7 @@ bool Launcher::MainDialog::setupGraphicsSettings() mEngineSettings.loadDefault(defaultPath); } catch (std::exception& e) { - std::string msg = std::string("
Error reading settings-default.cfg

") + e.what(); + std::string msg = std::string("
Error reading defaults.bin

") + e.what(); cfgError(tr("Error reading OpenMW configuration file"), tr(msg.c_str())); return false; } diff --git a/apps/openmw/engine.cpp b/apps/openmw/engine.cpp index d7c315323..b52bfc7ef 100644 --- a/apps/openmw/engine.cpp +++ b/apps/openmw/engine.cpp @@ -487,7 +487,7 @@ std::string OMW::Engine::loadSettings (Settings::Manager & settings) else if (boost::filesystem::exists(globaldefault)) settings.loadDefault(globaldefault); else - throw std::runtime_error ("No default settings file found! Make sure the file \"settings-default.cfg\" was properly installed."); + throw std::runtime_error ("No default settings file found! Make sure the file \"defaults.bin\" was properly installed."); // load user settings if they exist const std::string settingspath = (mCfgMgr.getUserConfigPath() / "settings.cfg").string(); diff --git a/components/settings/parser.cpp b/components/settings/parser.cpp index 9693bf511..72ff3aac4 100644 --- a/components/settings/parser.cpp +++ b/components/settings/parser.cpp @@ -255,7 +255,7 @@ void Settings::SettingsFileParser::saveSettingsFile(const std::string& file, con ostream << "# This is the OpenMW user 'settings.cfg' file. This file only contains" << std::endl; ostream << "# explicitly changed settings. If you would like to revert a setting" << std::endl; ostream << "# to its default, simply remove it from this file. For available" << std::endl; - ostream << "# settings, see the file 'settings-default.cfg' or the documentation at:" << std::endl; + ostream << "# settings, see the file 'files/settings-default.cfg' in our source repo or the documentation at:" << std::endl; ostream << "#" << std::endl; ostream << "# https://openmw.readthedocs.io/en/master/reference/modding/settings/index.html" << std::endl; } diff --git a/components/settings/settings.cpp b/components/settings/settings.cpp index b29dadcdc..52dbb6e21 100644 --- a/components/settings/settings.cpp +++ b/components/settings/settings.cpp @@ -49,7 +49,7 @@ std::string Manager::getString(const std::string &setting, const std::string &ca return it->second; throw std::runtime_error(std::string("Trying to retrieve a non-existing setting: ") + setting - + ".\nMake sure the settings-default.cfg file was properly installed."); + + ".\nMake sure the defaults.bin file was properly installed."); } float Manager::getFloat (const std::string& setting, const std::string& category) diff --git a/docs/source/reference/modding/settings/index.rst b/docs/source/reference/modding/settings/index.rst index 220ee88c4..e9607fd9d 100644 --- a/docs/source/reference/modding/settings/index.rst +++ b/docs/source/reference/modding/settings/index.rst @@ -8,6 +8,14 @@ If you are familiar with ``.ini`` tweaks in Morrowind or the other games, this w All settings described in this section are changed in ``settings.cfg``, located in your OpenMW user directory. See :doc:`../paths` for this location. +When creating a new game based on the OpenMW engine, it may be desirable to change the default settings - the defaults are chosen for compatibility with Morrowind. +This can be done by editing ``defaults.bin`` in the OpenMW installation directory without rebuilding OpenMW itself. +If you're using a custom fork of OpenMW, ``files/settings-default.cfg`` in the source repository should be edited instead. +To edit ``defaults.bin``, base64 decode it, make any changes, and then base64 encode it again. + +If you feel a need to edit the default settings for any other reason than when creating a new OpenMW-based game, you should not. +We may be able to accommodate your desired workflow some other way if you make a feature request. + Changing Settings ################# @@ -25,8 +33,10 @@ Changing Settings Then to the line above, type ``[GUI]``, as the tooltip delay setting comes from the "GUI Settings" section. Although this guide attempts to be comprehensive and up to date, -you will always be able to find the full list of settings available and their default values in ``settings-default.cfg`` -in your main OpenMW installation directory. +you will always be able to find the full list of settings available and their default values in ``settings-default.cfg``, +available in the ``files`` directory of our source repo, or by base64 decoding ``defaults.bin`` in your main OpenMW installation directory. +This has changed compared to previous versions of OpenMW as more users were confused by the existence of a file they weren't supposed to edit +than were helped by the existence of a file listing settings they could edit in a different file. The ranges included with each setting are the physically possible ranges, not recommendations. .. warning:: diff --git a/files/settings-default.cfg b/files/settings-default.cfg index ea678c70f..d94c43b01 100644 --- a/files/settings-default.cfg +++ b/files/settings-default.cfg @@ -1,6 +1,7 @@ -# WARNING: If this file is named settings-default.cfg, then editing -# this file might have no effect, as these settings may be overwritten -# by your user settings.cfg file (see documentation for its location). +# WARNING: If this file is named settings-default.cfg or was generated +# from defaults.bin, then editing this file might have no effect, as +# these settings may be overwritten by your user settings.cfg file +# (see documentation for its location). # # This file provides minimal documentation for each setting, and # ranges of recommended values. For detailed explanations of the From 09f39b29f0019a90219549dfa2c0af4cc0a66e0f Mon Sep 17 00:00:00 2001 From: AnyOldName3 Date: Sun, 9 May 2021 21:14:06 +0100 Subject: [PATCH 4/9] Load defaults.bin instead of settings-default.cfg. Do not decode yet. --- apps/launcher/maindialog.cpp | 6 +++--- apps/openmw/engine.cpp | 4 ++-- components/settings/parser.cpp | 2 +- components/settings/parser.hpp | 2 +- components/settings/settings.cpp | 2 +- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/apps/launcher/maindialog.cpp b/apps/launcher/maindialog.cpp index 9d497e879..0a7d495ab 100644 --- a/apps/launcher/maindialog.cpp +++ b/apps/launcher/maindialog.cpp @@ -424,11 +424,11 @@ bool Launcher::MainDialog::setupGraphicsSettings() mEngineSettings.clear(); // Create the settings manager and load default settings file - const std::string localDefault = (mCfgMgr.getLocalPath() / "settings-default.cfg").string(); - const std::string globalDefault = (mCfgMgr.getGlobalPath() / "settings-default.cfg").string(); + const std::string localDefault = (mCfgMgr.getLocalPath() / "defaults.bin").string(); + const std::string globalDefault = (mCfgMgr.getGlobalPath() / "defaults.bin").string(); std::string defaultPath; - // Prefer the settings-default.cfg in the current directory. + // Prefer the defaults.bin in the current directory. if (boost::filesystem::exists(localDefault)) defaultPath = localDefault; else if (boost::filesystem::exists(globalDefault)) diff --git a/apps/openmw/engine.cpp b/apps/openmw/engine.cpp index b52bfc7ef..b227ae04e 100644 --- a/apps/openmw/engine.cpp +++ b/apps/openmw/engine.cpp @@ -478,8 +478,8 @@ void OMW::Engine::setSkipMenu (bool skipMenu, bool newGame) std::string OMW::Engine::loadSettings (Settings::Manager & settings) { // Create the settings manager and load default settings file - const std::string localdefault = (mCfgMgr.getLocalPath() / "settings-default.cfg").string(); - const std::string globaldefault = (mCfgMgr.getGlobalPath() / "settings-default.cfg").string(); + const std::string localdefault = (mCfgMgr.getLocalPath() / "defaults.bin").string(); + const std::string globaldefault = (mCfgMgr.getGlobalPath() / "defaults.bin").string(); // prefer local if (boost::filesystem::exists(localdefault)) diff --git a/components/settings/parser.cpp b/components/settings/parser.cpp index 72ff3aac4..ee14eb5de 100644 --- a/components/settings/parser.cpp +++ b/components/settings/parser.cpp @@ -7,7 +7,7 @@ #include -void Settings::SettingsFileParser::loadSettingsFile(const std::string& file, CategorySettingValueMap& settings) +void Settings::SettingsFileParser::loadSettingsFile(const std::string& file, CategorySettingValueMap& settings, bool base64Encoded) { mFile = file; boost::filesystem::ifstream stream; diff --git a/components/settings/parser.hpp b/components/settings/parser.hpp index 449e54223..69e9cdaa4 100644 --- a/components/settings/parser.hpp +++ b/components/settings/parser.hpp @@ -10,7 +10,7 @@ namespace Settings class SettingsFileParser { public: - void loadSettingsFile(const std::string& file, CategorySettingValueMap& settings); + void loadSettingsFile(const std::string& file, CategorySettingValueMap& settings, bool base64encoded = false); void saveSettingsFile(const std::string& file, const CategorySettingValueMap& settings); diff --git a/components/settings/settings.cpp b/components/settings/settings.cpp index 52dbb6e21..13501ac8c 100644 --- a/components/settings/settings.cpp +++ b/components/settings/settings.cpp @@ -22,7 +22,7 @@ void Manager::clear() void Manager::loadDefault(const std::string &file) { SettingsFileParser parser; - parser.loadSettingsFile(file, mDefaultSettings); + parser.loadSettingsFile(file, mDefaultSettings, true); } void Manager::loadUser(const std::string &file) From aba735e6158f5b0bae9ca9f12ff0092ab2df1476 Mon Sep 17 00:00:00 2001 From: AnyOldName3 Date: Mon, 17 May 2021 22:45:10 +0100 Subject: [PATCH 5/9] Check in external Base64 implementation Taken from https://gist.github.com/tomykaira/f0fd86b6c73063283afe550bc5d77594 MIT licenced --- extern/Base64/Base64.h | 124 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 extern/Base64/Base64.h diff --git a/extern/Base64/Base64.h b/extern/Base64/Base64.h new file mode 100644 index 000000000..cdfdc04d0 --- /dev/null +++ b/extern/Base64/Base64.h @@ -0,0 +1,124 @@ +#ifndef _MACARON_BASE64_H_ +#define _MACARON_BASE64_H_ + +/** + * The MIT License (MIT) + * Copyright (c) 2016 tomykaira + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include + +namespace macaron { + +class Base64 { + public: + + static std::string Encode(const std::string data) { + static constexpr char sEncodingTable[] = { + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', + 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', + 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', + 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', + 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', + 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', + 'w', 'x', 'y', 'z', '0', '1', '2', '3', + '4', '5', '6', '7', '8', '9', '+', '/' + }; + + size_t in_len = data.size(); + size_t out_len = 4 * ((in_len + 2) / 3); + std::string ret(out_len, '\0'); + size_t i; + char *p = const_cast(ret.c_str()); + + for (i = 0; i < in_len - 2; i += 3) { + *p++ = sEncodingTable[(data[i] >> 2) & 0x3F]; + *p++ = sEncodingTable[((data[i] & 0x3) << 4) | ((int) (data[i + 1] & 0xF0) >> 4)]; + *p++ = sEncodingTable[((data[i + 1] & 0xF) << 2) | ((int) (data[i + 2] & 0xC0) >> 6)]; + *p++ = sEncodingTable[data[i + 2] & 0x3F]; + } + if (i < in_len) { + *p++ = sEncodingTable[(data[i] >> 2) & 0x3F]; + if (i == (in_len - 1)) { + *p++ = sEncodingTable[((data[i] & 0x3) << 4)]; + *p++ = '='; + } + else { + *p++ = sEncodingTable[((data[i] & 0x3) << 4) | ((int) (data[i + 1] & 0xF0) >> 4)]; + *p++ = sEncodingTable[((data[i + 1] & 0xF) << 2)]; + } + *p++ = '='; + } + + return ret; + } + + static std::string Decode(const std::string& input, std::string& out) { + static constexpr unsigned char kDecodingTable[] = { + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63, + 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64, + 64, 0, 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, 64, 64, 64, 64, 64, + 64, 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, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 + }; + + size_t in_len = input.size(); + if (in_len % 4 != 0) return "Input data size is not a multiple of 4"; + + size_t out_len = in_len / 4 * 3; + if (input[in_len - 1] == '=') out_len--; + if (input[in_len - 2] == '=') out_len--; + + out.resize(out_len); + + for (size_t i = 0, j = 0; i < in_len;) { + uint32_t a = input[i] == '=' ? 0 & i++ : kDecodingTable[static_cast(input[i++])]; + uint32_t b = input[i] == '=' ? 0 & i++ : kDecodingTable[static_cast(input[i++])]; + uint32_t c = input[i] == '=' ? 0 & i++ : kDecodingTable[static_cast(input[i++])]; + uint32_t d = input[i] == '=' ? 0 & i++ : kDecodingTable[static_cast(input[i++])]; + + uint32_t triple = (a << 3 * 6) + (b << 2 * 6) + (c << 1 * 6) + (d << 0 * 6); + + if (j < out_len) out[j++] = (triple >> 2 * 8) & 0xFF; + if (j < out_len) out[j++] = (triple >> 1 * 8) & 0xFF; + if (j < out_len) out[j++] = (triple >> 0 * 8) & 0xFF; + } + + return ""; + } + +}; + +} + +#endif /* _MACARON_BASE64_H_ */ From d66cc3b7aeaf2ca5c0a5bff1cffe9e464907bea6 Mon Sep 17 00:00:00 2001 From: AnyOldName3 Date: Mon, 17 May 2021 22:47:08 +0100 Subject: [PATCH 6/9] Fix undefined behaviour --- extern/Base64/Base64.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extern/Base64/Base64.h b/extern/Base64/Base64.h index cdfdc04d0..498d2be12 100644 --- a/extern/Base64/Base64.h +++ b/extern/Base64/Base64.h @@ -48,7 +48,7 @@ class Base64 { size_t out_len = 4 * ((in_len + 2) / 3); std::string ret(out_len, '\0'); size_t i; - char *p = const_cast(ret.c_str()); + char *p = ret.data(); for (i = 0; i < in_len - 2; i += 3) { *p++ = sEncodingTable[(data[i] >> 2) & 0x3F]; From 2b1326cb745406215c17f574ee844a70505d14e0 Mon Sep 17 00:00:00 2001 From: AnyOldName3 Date: Mon, 17 May 2021 22:50:32 +0100 Subject: [PATCH 7/9] Change namespace to Base64 The functions do Base64 encoding and decoding and do not feed me delicious almond and meringue based confectionary. --- extern/Base64/Base64.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extern/Base64/Base64.h b/extern/Base64/Base64.h index 498d2be12..4e9f51747 100644 --- a/extern/Base64/Base64.h +++ b/extern/Base64/Base64.h @@ -27,7 +27,7 @@ #include -namespace macaron { +namespace Base64 { class Base64 { public: From 081650a2e536c1a6a79cedadd3e0f10bf166d8ab Mon Sep 17 00:00:00 2001 From: AnyOldName3 Date: Mon, 17 May 2021 23:00:23 +0100 Subject: [PATCH 8/9] Integrate Base64 library with build --- CMakeLists.txt | 1 + components/CMakeLists.txt | 2 ++ components/settings/parser.cpp | 2 ++ extern/Base64/CMakeLists.txt | 2 ++ 4 files changed, 7 insertions(+) create mode 100644 extern/Base64/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index b97657ffd..2ed4eafd7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -516,6 +516,7 @@ endif (CMAKE_CXX_COMPILER_ID STREQUAL GNU OR CMAKE_CXX_COMPILER_ID STREQUAL Clan add_subdirectory (extern/osg-ffmpeg-videoplayer) add_subdirectory (extern/oics) +add_subdirectory (extern/Base64) if (BUILD_OPENCS) add_subdirectory (extern/osgQt) endif() diff --git a/components/CMakeLists.txt b/components/CMakeLists.txt index 9041ee533..06ae0791d 100644 --- a/components/CMakeLists.txt +++ b/components/CMakeLists.txt @@ -251,6 +251,8 @@ target_link_libraries(components RecastNavigation::DebugUtils RecastNavigation::Detour RecastNavigation::Recast + + Base64 ) target_link_libraries(components ${BULLET_LIBRARIES}) diff --git a/components/settings/parser.cpp b/components/settings/parser.cpp index ee14eb5de..f375bf85b 100644 --- a/components/settings/parser.cpp +++ b/components/settings/parser.cpp @@ -7,6 +7,8 @@ #include +#include + void Settings::SettingsFileParser::loadSettingsFile(const std::string& file, CategorySettingValueMap& settings, bool base64Encoded) { mFile = file; diff --git a/extern/Base64/CMakeLists.txt b/extern/Base64/CMakeLists.txt new file mode 100644 index 000000000..d1adf91bc --- /dev/null +++ b/extern/Base64/CMakeLists.txt @@ -0,0 +1,2 @@ +add_library(Base64 INTERFACE) +target_include_directories(Base64 INTERFACE .) \ No newline at end of file From 4cedb3549b53c54b31d581ce6538846aede2e136 Mon Sep 17 00:00:00 2001 From: AnyOldName3 Date: Mon, 17 May 2021 23:39:56 +0100 Subject: [PATCH 9/9] Decode base64-packed settings files --- components/settings/parser.cpp | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/components/settings/parser.cpp b/components/settings/parser.cpp index f375bf85b..24f359b31 100644 --- a/components/settings/parser.cpp +++ b/components/settings/parser.cpp @@ -12,16 +12,31 @@ void Settings::SettingsFileParser::loadSettingsFile(const std::string& file, CategorySettingValueMap& settings, bool base64Encoded) { mFile = file; - boost::filesystem::ifstream stream; - stream.open(boost::filesystem::path(file)); + boost::filesystem::ifstream fstream; + fstream.open(boost::filesystem::path(file)); + auto stream = std::ref(fstream); + + std::istringstream decodedStream; + if (base64Encoded) + { + std::string base64String(std::istreambuf_iterator(fstream), {}); + std::string decodedString; + auto result = Base64::Base64::Decode(base64String, decodedString); + if (!result.empty()) + fail("Could not decode Base64 file: " + result); + // Move won't do anything until C++20, but won't hurt to do it anyway. + decodedStream.str(std::move(decodedString)); + stream = std::ref(decodedStream); + } + Log(Debug::Info) << "Loading settings file: " << file; std::string currentCategory; mLine = 0; - while (!stream.eof() && !stream.fail()) + while (!stream.get().eof() && !stream.get().fail()) { ++mLine; std::string line; - std::getline( stream, line ); + std::getline( stream.get(), line ); size_t i = 0; if (!skipWhiteSpace(i, line))