Skip to content

Commit

Permalink
Merge pull request OpenBrickProtocolFoundation#111 from mgerhold/smal…
Browse files Browse the repository at this point in the history
…l-fixes

Small fixes
  • Loading branch information
Totto16 authored Jan 2, 2024
2 parents 33e1a6f + 789ac50 commit 0a09252
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 37 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/meson.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,22 @@ jobs:
os: windows-latest
msvc: true
buildtype: release
library_type: static
# - name: Windows MingGW Release
# os: windows-latest
# msvc: false
# buildtype: release
# library_type: static
- name: Linux Release
os: ubuntu-22.04
buildtype: release
use-clang: false
library_type: shared
- name: Linux Clang Release
os: ubuntu-22.04
buildtype: release
use-clang: true
library_type: shared
steps:
- uses: actions/checkout@v3
with:
Expand Down Expand Up @@ -73,7 +77,7 @@ jobs:
sudo apt-get install ninja-build libsdl2-2.0-0 libsdl2-dev libsdl2-net* libsdl2-ttf* -y
- name: Configure
run: meson setup build -Dbuildtype=${{ matrix.config.buildtype }}
run: meson setup build -Dbuildtype=${{ matrix.config.buildtype }} -Ddefault_library=${{ matrix.config.library_type }}

- name: Build
run: meson compile -C build
Expand Down
10 changes: 5 additions & 5 deletions assets/icon/OOPetris.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions com.github.mgerhold.OOPetris.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ modules:
- bash ../platforms/flatpak-icon-helper.sh
- mkdir -p /app/share/applications/
- cp ../assets/OOPetris.desktop /app/share/applications/com.github.mgerhold.OOPetris.desktop
- rm -rf /app/include
- rm -rf /app/lib/pkgconfig
run-tests: true

finish-args:
Expand Down
6 changes: 6 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ else
add_project_arguments('-std=c++20', language: 'cpp')
endif

if get_option('buildtype') == 'debug' or get_option('buildtype') == 'debugoptimized'
add_project_arguments('-DDEBUG_BUILD', language: 'cpp')
endif


deps = []
inc_dirs = []

Expand Down Expand Up @@ -240,6 +245,7 @@ src_files = []

subdir('src')

## TODO: only istall needed ones, since sometimes we only need e.g. flacs or mp3 and no icons etc.
## install assets
install_subdir('assets', install_dir: 'share/oopetris')

Expand Down
2 changes: 2 additions & 0 deletions platforms/android/app/src/main/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
/assets

/res/mipmap-*
2 changes: 1 addition & 1 deletion platforms/flatpak-icon-helper.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bash

APP_NAME="com.github.mgerhold.OOPetris"
export APP_NAME="com.github.mgerhold.OOPetris"

function process_icon() {
SRC=$1
Expand Down
33 changes: 27 additions & 6 deletions src/application.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "application.hpp"
#include "scenes/scene.hpp"
#include <fmt/format.h>
#include <fstream>
#include <stdexcept>

#if defined(__SWITCH__)
#include <switch.h>
Expand Down Expand Up @@ -109,12 +111,31 @@ void Application::initialize() {
push_scene(scenes::create_scene(*this, &m_window, SceneId::MainMenu));
}

void Application::try_load_settings() try {
std::ifstream settings_file{ settings_filename };
m_settings = nlohmann::json::parse(settings_file);
spdlog::info("settings loaded");
} catch (...) {
spdlog::error("unable to load settings from \"{}\"", settings_filename);
void Application::try_load_settings() {
const std::filesystem::path settings_file = utils::get_root_folder() / settings_filename;
std::string reason{};
if (not std::filesystem::exists(settings_file)) {
reason = "file doesn't exist";
} else {
try {
std::ifstream settings_file_stream{ settings_file };

m_settings = nlohmann::json::parse(settings_file_stream);
spdlog::info("settings loaded");
return;

} catch (nlohmann::json::parse_error& parse_error) {
reason = fmt::format("parse error: {}", parse_error.what());
} catch (nlohmann::json::type_error& type_error) {
reason = fmt::format("type error: {}", type_error.what());
} catch (nlohmann::json::exception& exception) {
reason = fmt::format("unknown json exception: {}", exception.what());
} catch (std::exception& exception) {
reason = fmt::format("unknown exception: {}", exception.what());
}
}

spdlog::error("unable to load settings from \"{}\": {}", settings_filename, reason);
spdlog::warn("applying default settings");
}

Expand Down
2 changes: 1 addition & 1 deletion src/scenes/ingame/ingame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ namespace scenes {
[[nodiscard]] std::unique_ptr<RecordingWriter> Ingame::create_recording_writer(TetrionHeaders tetrion_headers) {

static constexpr auto recordings_directory = "recordings";
const auto recording_directory_path = utils::get_subfolder_to_root(recordings_directory);
const auto recording_directory_path = utils::get_root_folder() / recordings_directory;

if (not std::filesystem::exists(recording_directory_path)) {
std::filesystem::create_directory(recording_directory_path);
Expand Down
48 changes: 33 additions & 15 deletions src/settings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@
#include "controls.hpp"
#include "magic_enum_wrapper.hpp"
#include <array>

#include <fmt/format.h>

#ifdef DEBUG_BUILD
// better json error messages, see https://json.nlohmann.me/api/macros/json_diagnostics/
#define JSON_DIAGNOSTICS 1
#endif
#include <nlohmann/json.hpp>

#include <string>
#include <variant>

Expand All @@ -21,22 +29,32 @@ inline void to_json(nlohmann::json& j, const KeyboardControls& controls) {
};
}


inline KeyCode get_key_code_safe(const nlohmann::json& j, const std::string& name) {

auto context = j.at(name);

std::string input;
context.get_to(input);

const auto& value = magic_enum::enum_cast<KeyCode>(input);
if (not value.has_value()) {
throw nlohmann::json::type_error::create(
302, fmt::format("Expected a valid KeyCode in key '{}', but got '{}'", name, input), &context
);
}
return value.value();
}

inline void from_json(const nlohmann::json& j, KeyboardControls& controls) {
std::string str;
j.at("rotate_left").get_to(str);
controls.rotate_left = magic_enum::enum_cast<KeyCode>(str).value();
j.at("rotate_right").get_to(str);
controls.rotate_right = magic_enum::enum_cast<KeyCode>(str).value();
j.at("move_left").get_to(str);
controls.move_left = magic_enum::enum_cast<KeyCode>(str).value();
j.at("move_right").get_to(str);
controls.move_right = magic_enum::enum_cast<KeyCode>(str).value();
j.at("move_down").get_to(str);
controls.move_down = magic_enum::enum_cast<KeyCode>(str).value();
j.at("drop").get_to(str);
controls.drop = magic_enum::enum_cast<KeyCode>(str).value();
j.at("hold").get_to(str);
controls.hold = magic_enum::enum_cast<KeyCode>(str).value();

controls.rotate_left = get_key_code_safe(j, "rotate_left");
controls.rotate_right = get_key_code_safe(j, "rotate_right");
controls.move_left = get_key_code_safe(j, "move_left");
controls.move_right = get_key_code_safe(j, "move_right");
controls.move_down = get_key_code_safe(j, "move_down");
controls.drop = get_key_code_safe(j, "drop");
controls.hold = get_key_code_safe(j, "hold");
}

using Controls = std::variant<KeyboardControls>;
Expand Down
17 changes: 11 additions & 6 deletions src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <SDL.h>
#include <array>
#include <chrono>
#include <cstdlib>
#include <ctime>
#include <filesystem>
#include <spdlog/spdlog.h>
Expand Down Expand Up @@ -48,8 +49,17 @@ namespace utils {
#elif defined(__SWITCH__)
// this is in the sdcard of the switch, since internal storage is read-only for applications!
return std::filesystem::path{ "." };

#elif defined(FLATPAK_BUILD)
// this is a read write location in the flatpak build, see https://docs.flatpak.org/en/latest/conventions.html
const char* data_home = std::getenv("XDG_DATA_HOME");
if (data_home == = nullptr) {
throw std::runtime_error{ "Failed to get flatpak data directory (XDG_DATA_HOME)" };
}

return std::filesystem::path{ data_home };
#else
// this is only used in local build for debugging, when compiling in release mode the path is real path where the app can store many things without interfering with other things (eg. AppData\Roaming\... onw Windows or .local/share/... on Linux )
// this is only used in local build for debugging, when compiling in release mode the path is real path where the app can store many things without interfering with other things (eg. AppData\Roaming\... on Windows or .local/share/... on Linux )
return std::filesystem::path{ "." };
#endif
}
Expand Down Expand Up @@ -77,11 +87,6 @@ namespace utils {
#endif
}

[[nodiscard]] std::filesystem::path get_subfolder_to_root(const std::string_view folder) {
return get_root_folder() / folder;
}


tl::optional<bool> log_error(const std::string& error) {
spdlog::error(error);
return tl::nullopt;
Expand Down
2 changes: 0 additions & 2 deletions src/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ namespace utils {

[[nodiscard]] std::filesystem::path get_root_folder();

[[nodiscard]] std::filesystem::path get_subfolder_to_root(std::string_view folder);

tl::optional<bool> log_error(const std::string& error);

template<usize data_size>
Expand Down

0 comments on commit 0a09252

Please sign in to comment.