Skip to content

Commit

Permalink
Refactor pass (#21)
Browse files Browse the repository at this point in the history
* move to c++20 and rework c-style image merge

* moved to c++23, partial working

* removed generic-toolbox dependency

* save before coroutines integration

* revert unique_ptr change on img; more error handling

* working

* increment version
  • Loading branch information
dslatt authored Oct 2, 2024
1 parent 98f9848 commit fafcb08
Show file tree
Hide file tree
Showing 15 changed files with 284 additions and 254 deletions.
4 changes: 0 additions & 4 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
[submodule "library/cpp-generic-toolbox"]
path = library/cpp-generic-toolbox
url = https://github.com/nadrino/cpp-generic-toolbox.git
branch = main
[submodule "library/borealis"]
path = library/borealis
url = https://github.com/dslatt/borealis.git
Expand Down
8 changes: 5 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ include(${BOREALIS_LIBRARY}/cmake/toolchain.cmake)
project(nso-icon-tool)
set(VERSION_MAJOR "0")
set(VERSION_MINOR "4")
set(VERSION_ALTER "1")
set(VERSION_ALTER "2")
set(VERSION_BUILD "0")
set(PROJECT_AUTHOR "dslatt")
set(PROJECT_ICON ${CMAKE_CURRENT_SOURCE_DIR}/resources/img/dev.jpg)
Expand Down Expand Up @@ -63,7 +63,9 @@ endif ()

# building target
program_target(${PROJECT_NAME} "${MAIN_SRC}")
set_target_properties(${PROJECT_NAME} PROPERTIES CXX_STANDARD 17)

# c++23 not supported by current toolchain cmake version; use compiler flags instead
#set_target_properties(${PROJECT_NAME} PROPERTIES CXX_STANDARD 20)


# building release file
Expand All @@ -87,5 +89,5 @@ CheckGit("${GIT_WORKING_DIR}" changed)
GitStateChangedAction()

target_include_directories(${PROJECT_NAME} PRIVATE demo ${APP_PLATFORM_INCLUDE})
target_compile_options(${PROJECT_NAME} PRIVATE -ffunction-sections -fdata-sections ${APP_PLATFORM_OPTION})
target_compile_options(${PROJECT_NAME} PRIVATE -ffunction-sections -fdata-sections -std=c++2b ${APP_PLATFORM_OPTION})
target_link_libraries(${PROJECT_NAME} PRIVATE borealis ${APP_PLATFORM_LIB})
7 changes: 4 additions & 3 deletions include/util/image.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#pragma once

#include <string>
#include <filesystem>

struct Image
{
unsigned char *img = nullptr;
unsigned char* img = nullptr;
int size = 0; // raw size in bytes
int pixels = 0; // pixel count
int x = 0, y = 0, n = 0;
Expand All @@ -24,8 +25,8 @@ struct Image
Image(unsigned char *buffer, size_t size);
Image(std::string file);
void resize(int x, int y);
bool writeJpg(std::string path);
bool writePng(std::string path);
bool writeJpg(std::filesystem::path path);
bool writePng(std::filesystem::path path);
void applyAlpha(float alpha);

std::string hash();
Expand Down
13 changes: 6 additions & 7 deletions include/util/paths.hpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
#pragma once

#include <string>

namespace paths
{
const std::string BasePath = "sdmc:/avatars/";
const std::string BaseAppPath = "sdmc:/avatars/nso-icon-tool";
const std::string_view BasePath = "sdmc:/avatars/";
const std::string_view BaseAppPath = "sdmc:/avatars/nso-icon-tool/";

const std::string IconCachePath = "sdmc:/avatars/nso-icons-main/";
const std::string CacheFilePath = "sdmc:/avatars/nso-icon-tool/cache.json";
const std::string LogFilePath = "sdmc:/avatars/nso-icon-tool/log.log";
const std::string CollectionPath = "sdmc:/avatars/nso-icon-tool/collection";
const std::string_view IconCachePath = "sdmc:/avatars/nso-icons-main/";
const std::string_view CacheFilePath = "sdmc:/avatars/nso-icon-tool/cache.json";
const std::string_view LogFilePath = "sdmc:/avatars/nso-icon-tool/log.log";
const std::string_view CollectionPath = "sdmc:/avatars/nso-icon-tool/collection";
}
1 change: 0 additions & 1 deletion library/cpp-generic-toolbox
Submodule cpp-generic-toolbox deleted from 420dd6
29 changes: 20 additions & 9 deletions source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <borealis.hpp>
#include <cstdlib>
#include <string>
#include <filesystem>

#include <switch/services/acc.h>

Expand All @@ -30,16 +31,12 @@
#include "util/paths.hpp"

#include "view/recycling_grid.hpp"
#include "GenericToolbox.Fs.h"

using namespace brls::literals; // for _i18n
namespace fs = std::filesystem;

int main(int argc, char *argv[])
{
GenericToolbox::mkdir(paths::BasePath);
GenericToolbox::mkdir(paths::BaseAppPath);
GenericToolbox::mkdir(paths::CollectionPath);

// We recommend to use INFO for real apps
for (int i = 1; i < argc; i++)
{
Expand All @@ -58,8 +55,16 @@ int main(int argc, char *argv[])
}
}

try {
// create expected directories; these need to exist for the app to work
fs::create_directories(paths::CollectionPath);
} catch(const std::exception &e) {
brls::Logger::error("Error creating expected directories; Cant continue: {}", e.what());
return EXIT_FAILURE;
}

#ifdef NDEBUG
brls::Logger::setLogOutput(fopen(paths::LogFilePath.c_str(), "w"));
brls::Logger::setLogOutput(fopen(std::string(paths::LogFilePath).c_str(), "w"));
#endif

// Init the app and i18n
Expand All @@ -69,6 +74,7 @@ int main(int argc, char *argv[])
return EXIT_FAILURE;
}

brls::Logger::setLogLevel(brls::LogLevel::LOG_DEBUG);
brls::Application::createWindow("demo/title"_i18n);

brls::Application::getPlatform()->setThemeVariant(brls::ThemeVariant::DARK);
Expand All @@ -94,9 +100,14 @@ int main(int argc, char *argv[])
// Create and push the main activity to the stack
brls::Application::pushActivity(new MainActivity());

// Run the app
while (brls::Application::mainLoop())
;
try {
// Run the app
while (brls::Application::mainLoop())
;
} catch(const std::exception &e) {
brls::Logger::error("Top level exception: {}", e.what());
return EXIT_FAILURE;
}

// Exit
return EXIT_SUCCESS;
Expand Down
17 changes: 9 additions & 8 deletions source/util/account.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
#include <functional>
#include <string>
#include <switch.h>
#include <GenericToolbox.Fs.h>
#include <fstream>
#include "util/paths.hpp"
#include "borealis.hpp"
#include <filesystem>

namespace fs = std::filesystem;

namespace account
{
Expand Down Expand Up @@ -52,7 +55,7 @@ namespace account
if (!R_SUCCEEDED(res))
return image;

std::unique_ptr<char[]> buffer(new char[imageSize]);
auto buffer = std::make_unique<char[]>(imageSize);
res = accountProfileLoadImage(&user.profile, (void *)buffer.get(), imageSize, &tmpSize);
if (!R_SUCCEEDED(res))
return image;
Expand All @@ -63,11 +66,9 @@ namespace account

bool setUserIcon(UserInfo &user, Image &image)
{
auto path = GenericToolbox::joinAsString(paths::BaseAppPath, "tmpicon.jpg");
auto path = fs::path(paths::BaseAppPath) / "tmpicon.jpg";
image.writeJpg(path);

brls::Logger::info("");

auto *service = accountGetServiceSession();
if (!service)
return false;
Expand All @@ -94,8 +95,8 @@ namespace account
return false;

{
auto size = GenericToolbox::getFileSize(path);
std::unique_ptr<char[]> buffer(new char[size]);
auto size = fs::file_size(path);
auto buffer = std::make_unique<char[]>(size);
std::fstream stream(path, std::ios::in);
stream.read(buffer.get(), size);

Expand All @@ -110,7 +111,7 @@ namespace account
});
}

GenericToolbox::rm(path);
fs::remove(path);
return true;
}
}
9 changes: 5 additions & 4 deletions source/util/extract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
#include <vector>
#include <borealis.hpp>
#include <strings.h>
#include <filesystem>

#include <switch.h>
#include <GenericToolbox.Fs.h>

#include "util/progress_event.hpp"

using namespace brls::literals; // for _i18n
namespace fs = std::filesystem;

constexpr size_t WRITE_BUFFER_SIZE = 0x10000;

Expand Down Expand Up @@ -66,12 +67,12 @@ namespace extract
{
if (filename.back() == '/')
{
GenericToolbox::mkdir(filename);
fs::create_directories(filename);
return;
}
if (forceCreateTree)
{
GenericToolbox::mkdir(filename);
fs::create_directories(filename);
}
void *buf = malloc(WRITE_BUFFER_SIZE);
FILE *outfile;
Expand Down Expand Up @@ -109,7 +110,7 @@ namespace extract
break;
}

if (overwriteExisting || !GenericToolbox::isPathValid(filename))
if (overwriteExisting || !fs::exists(filename))
{
ProgressEvent::instance().setMsg(filename);
extractEntry(filename, zfile);
Expand Down
Loading

0 comments on commit fafcb08

Please sign in to comment.