chore: Introduce common directory to deduplicate shared code

This commit is contained in:
Ash Logan 2024-11-21 13:19:06 +11:00
parent 68bc066c7b
commit ea71879bb1
22 changed files with 110 additions and 278 deletions

View File

@ -21,9 +21,9 @@ WUT_ROOT := $(DEVKITPRO)/wut
#-------------------------------------------------------------------------------
TARGET := Inkay-pretendo
BUILD := build
SOURCES := src src/patches src/utils src/ext/inih
SOURCES := src src/patches src/utils src/ext/inih common
DATA := data
INCLUDES := src src/ext/inih
INCLUDES := src src/ext/inih src/lang common
#-------------------------------------------------------------------------------
# options for code generation
@ -103,7 +103,7 @@ all: $(BUILD)
$(BUILD):
@[ -d $@ ] || mkdir -p $@
@$(shell [ ! -d $(BUILD) ] && mkdir -p $(BUILD))
@$(MAKE) --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile
@$(MAKE) --no-print-directory -C $(CURDIR)/plugin -f $(CURDIR)/plugin/Makefile
mkdir -p dist/

44
common/lang.cpp Normal file
View File

@ -0,0 +1,44 @@
//
// Created by ash on 21/11/24.
//
#include "lang.h"
config_strings get_config_strings(nn::swkbd::LanguageType language) {
switch (language) {
case nn::swkbd::LanguageType::English:
default: return {
#include "en_US.lang"
};
case nn::swkbd::LanguageType::Spanish: return {
#include "es_ES.lang"
};
case nn::swkbd::LanguageType::French: return {
#include "fr_FR.lang"
};
case nn::swkbd::LanguageType::Italian: return {
#include "it_IT.lang"
};
case nn::swkbd::LanguageType::German: return {
#include "de_DE.lang"
};
case nn::swkbd::LanguageType::SimplifiedChinese: return {
#include "zh_CN.lang"
};
case nn::swkbd::LanguageType::TraditionalChinese: return {
#include "zh_Hant.lang"
};
case nn::swkbd::LanguageType::Portuguese: return {
#include "pt_BR.lang"
};
case nn::swkbd::LanguageType::Japanese: return {
#include "ja_JP.lang"
};
case nn::swkbd::LanguageType::Dutch: return {
#include "nl_NL.lang"
};
case nn::swkbd::LanguageType::Russian: return {
#include "ru_RU.lang"
};
}
}

22
common/lang.h Normal file
View File

@ -0,0 +1,22 @@
#pragma once
#include <string_view>
#include <nn/swkbd.h>
struct config_strings {
const char *plugin_name;
std::string_view network_category;
std::string_view connect_to_network_setting;
std::string_view other_category;
std::string_view reset_wwp_setting;
std::string_view press_a_action;
std::string_view restart_to_apply_action;
std::string_view need_menu_action;
std::string_view using_nintendo_network;
std::string_view using_pretendo_network;
std::string_view multiplayer_port_display;
std::string_view module_not_found;
std::string_view module_init_not_found;
};
config_strings get_config_strings(nn::swkbd::LanguageType language);

View File

@ -1,6 +1,5 @@
/* Copyright 2024 Pretendo Network contributors <pretendo.network>
Copyright 2024 Ash Logan <ash@heyquark.com>
Copyright 2023 Maschell
Copyright 2020-2022 V10lator <v10lator@myway.de>
Copyright 2022 Xpl0itU <DaThinkingChair@protonmail.com>
@ -81,8 +80,8 @@ static void get_mcp_config() {
mcp_os_version = os_version;
DEBUG_FUNCTION_LINE_VERBOSE("Running on %d.%d.%d%c; %s%s",
os_version.major, os_version.minor, os_version.patch, os_version.region
config.code_id, config.serial_id
os_version.major, os_version.minor, os_version.patch, os_version.region
config.code_id, config.serial_id
);
}
@ -97,3 +96,20 @@ MCPSystemVersion get_console_os_version() {
return mcp_os_version.value_or((MCPSystemVersion) { .major = 5, .minor = 5, .patch = 5, .region = 'E' });
}
static inline int digit(char a) {
if (a < '0' || a > '9') return 0;
return a - '0';
}
unsigned short get_console_peertopeer_port() {
const char * serial = get_console_serial();
unsigned short port = 50000 +
(digit(serial[4]) * 1000) +
(digit(serial[5]) * 100 ) +
(digit(serial[6]) * 10 ) +
(digit(serial[7]) * 1 );
return port;
}

View File

@ -11,5 +11,6 @@
nn::swkbd::LanguageType get_system_language();
const char * get_console_serial();
MCPSystemVersion get_console_os_version();
unsigned short get_console_peertopeer_port();
#endif //INKAY_SYSCONFIG_H

View File

@ -21,9 +21,9 @@ WUMS_ROOT := $(DEVKITPRO)/wums
#-------------------------------------------------------------------------------
TARGET := Inkay-pretendo
BUILD := build
SOURCES := src src/utils
SOURCES := src src/utils ../common
DATA := data
INCLUDES := src ../src/lang
INCLUDES := src ../src/lang ../common
#DEBUG := 1
#-------------------------------------------------------------------------------

View File

@ -19,65 +19,29 @@
#include "wut_extra.h"
#include "utils/logger.h"
#include "utils/sysconfig.h"
#include "sysconfig.h"
#include "lang.h"
#include <wups.h>
#include <wups/storage.h>
#include <wups/config_api.h>
#include <wups/config/WUPSConfigItemBoolean.h>
#include <wups/config/WUPSConfigItemStub.h>
#include <coreinit/title.h>
#include <coreinit/launch.h>
#include <sysapp/title.h>
#include <sysapp/launch.h>
#include <nn/act.h>
#include <format>
static config_strings strings;
bool Config::connect_to_network = true;
bool Config::need_relaunch = false;
bool Config::unregister_task_item_pressed = false;
bool Config::is_wiiu_menu = false;
static config_strings strings;
config_strings get_config_strings(nn::swkbd::LanguageType language) {
switch (language) {
case nn::swkbd::LanguageType::English:
default: return {
#include "en_US.lang"
};
case nn::swkbd::LanguageType::Spanish: return {
#include "es_ES.lang"
};
case nn::swkbd::LanguageType::French: return {
#include "fr_FR.lang"
};
case nn::swkbd::LanguageType::Italian: return {
#include "it_IT.lang"
};
case nn::swkbd::LanguageType::German: return {
#include "de_DE.lang"
};
case nn::swkbd::LanguageType::SimplifiedChinese: return {
#include "zh_CN.lang"
};
case nn::swkbd::LanguageType::TraditionalChinese: return {
#include "zh_Hant.lang"
};
case nn::swkbd::LanguageType::Portuguese: return {
#include "pt_BR.lang"
};
case nn::swkbd::LanguageType::Japanese: return {
#include "ja_JP.lang"
};
case nn::swkbd::LanguageType::Dutch: return {
#include "nl_NL.lang"
};
case nn::swkbd::LanguageType::Russian: return {
#include "ru_RU.lang"
};
}
}
static WUPSConfigAPICallbackStatus report_error(WUPSConfigAPIStatus err) {
DEBUG_FUNCTION_LINE_VERBOSE("WUPS config error: %s", WUPSConfigAPI_GetStatusStr(err));
return WUPSCONFIG_API_CALLBACK_RESULT_ERROR;
@ -174,6 +138,12 @@ static WUPSConfigAPICallbackStatus ConfigMenuOpenedCallback(WUPSConfigCategoryHa
res = network_cat->add(std::move(*connect_item), err);
if (!res) return report_error(err);
{
std::string multiplayer_port_text = std::vformat(strings.multiplayer_port_display, std::make_format_args(get_console_peertopeer_port()));
res = network_cat->add(WUPSConfigItemStub::Create(multiplayer_port_text), err);
if (!res) return report_error(err);
}
res = root.add(std::move(*network_cat), err);
if (!res) return report_error(err);

View File

@ -5,9 +5,6 @@
#ifndef INKAY_CONFIG_H
#define INKAY_CONFIG_H
#include <string_view>
#include <nn/swkbd.h>
class Config {
public:
static void Init();
@ -24,21 +21,4 @@ public:
static bool unregister_task_item_pressed;
};
struct config_strings {
const char *plugin_name;
std::string_view network_category;
std::string_view connect_to_network_setting;
std::string_view other_category;
std::string_view reset_wwp_setting;
std::string_view press_a_action;
std::string_view restart_to_apply_action;
std::string_view need_menu_action;
std::string_view using_nintendo_network;
std::string_view using_pretendo_network;
std::string_view module_not_found;
std::string_view module_init_not_found;
};
config_strings get_config_strings(nn::swkbd::LanguageType language);
#endif //INKAY_CONFIG_H

View File

@ -16,33 +16,12 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <wups.h>
#include <optional>
#include <nsysnet/nssl.h>
#include <sysapp/title.h>
#include <coreinit/cache.h>
#include <coreinit/dynload.h>
#include <coreinit/mcp.h>
#include <coreinit/memory.h>
#include <coreinit/memorymap.h>
#include <coreinit/memexpheap.h>
#include <coreinit/title.h>
#include <notifications/notifications.h>
#include <utils/logger.h>
#include "config.h"
#include "module.h"
#include "Notification.h"
#include <coreinit/filesystem.h>
#include <cstring>
#include <string>
#include <nn/erreula/erreula_cpp.h>
#include <nn/act/client_cpp.h>
#include <gx2/surface.h>
#define INKAY_VERSION "v2.6.0"
@ -60,8 +39,6 @@ WUPS_USE_STORAGE("inkay");
WUPS_USE_WUT_DEVOPTAB();
#include "utils/sysconfig.h"
INITIALIZE_PLUGIN() {
WHBLogCafeInit();
WHBLogUdpInit();

View File

@ -20,7 +20,8 @@
#include "config.h"
#include "Notification.h"
#include "utils/logger.h"
#include "utils/sysconfig.h"
#include "sysconfig.h"
#include "lang.h"
static OSDynLoad_Module module;
static void (*moduleInitialize)(bool) = nullptr;

View File

@ -1,57 +0,0 @@
/* Copyright 2024 Pretendo Network contributors <pretendo.network>
Copyright 2024 Ash Logan <ash@heyquark.com>
Copyright 2020-2022 V10lator <v10lator@myway.de>
Copyright 2022 Xpl0itU <DaThinkingChair@protonmail.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "sysconfig.h"
#include "utils/logger.h"
#include <coreinit/userconfig.h>
#include <optional>
nn::swkbd::LanguageType get_system_language() {
static std::optional <nn::swkbd::LanguageType> cached_language{};
if (cached_language) return *cached_language;
UCHandle handle = UCOpen();
if (handle >= 0) {
nn::swkbd::LanguageType language;
UCSysConfig settings __attribute__((__aligned__(0x40))) = {
.name = "cafe.language",
.access = 0,
.dataType = UC_DATATYPE_UNSIGNED_INT,
.error = UC_ERROR_OK,
.dataSize = sizeof(language),
.data = &language,
};
UCError err = UCReadSysConfig(handle, 1, &settings);
UCClose(handle);
if (err != UC_ERROR_OK) {
DEBUG_FUNCTION_LINE("Error reading UC: %d!", err);
return nn::swkbd::LanguageType::English;
} else {
DEBUG_FUNCTION_LINE_VERBOSE("System language found: %d", language);
cached_language = language;
return language;
}
} else {
DEBUG_FUNCTION_LINE("Error opening UC: %d", handle);
return nn::swkbd::LanguageType::English;
}
}

View File

@ -1,12 +0,0 @@
//
// Created by ash on 9/04/24.
//
#ifndef INKAY_SYSCONFIG_H
#define INKAY_SYSCONFIG_H
#include <nn/swkbd.h>
nn::swkbd::LanguageType get_system_language();
#endif //INKAY_SYSCONFIG_H

View File

@ -1,31 +0,0 @@
/* Copyright 2024 Pretendo Network contributors <pretendo.network>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Notification.h"
#include <notifications/notification_defines.h>
#include <notifications/notifications.h>
void ShowNotification(const char* notification) {
auto err1 = NotificationModule_SetDefaultValue(NOTIFICATION_MODULE_NOTIFICATION_TYPE_INFO,
NOTIFICATION_MODULE_DEFAULT_OPTION_KEEP_UNTIL_SHOWN, true);
auto err2 = NotificationModule_SetDefaultValue(NOTIFICATION_MODULE_NOTIFICATION_TYPE_INFO,
NOTIFICATION_MODULE_DEFAULT_OPTION_DURATION_BEFORE_FADE_OUT,
15.0f);
if (err1 != NOTIFICATION_MODULE_RESULT_SUCCESS || err2 != NOTIFICATION_MODULE_RESULT_SUCCESS) return;
NotificationModule_AddInfoNotification(notification);
}

View File

@ -1,3 +0,0 @@
#pragma once
void ShowNotification(const char* notification);

View File

@ -20,44 +20,3 @@
bool Config::connect_to_network = false;
bool Config::initialized = false;
bool Config::shown_uninitialized_warning = false;
static config_strings strings;
config_strings get_config_strings(nn::swkbd::LanguageType language) {
switch (language) {
case nn::swkbd::LanguageType::English:
default: return {
#include "lang/en_US.lang"
};
case nn::swkbd::LanguageType::Spanish: return {
#include "lang/es_ES.lang"
};
case nn::swkbd::LanguageType::French: return {
#include "lang/fr_FR.lang"
};
case nn::swkbd::LanguageType::Italian: return {
#include "lang/it_IT.lang"
};
case nn::swkbd::LanguageType::German: return {
#include "lang/de_DE.lang"
};
case nn::swkbd::LanguageType::SimplifiedChinese: return {
#include "lang/zh_CN.lang"
};
case nn::swkbd::LanguageType::TraditionalChinese: return {
#include "lang/zh_Hant.lang"
};
case nn::swkbd::LanguageType::Portuguese: return {
#include "lang/pt_BR.lang"
};
case nn::swkbd::LanguageType::Japanese: return {
#include "lang/ja_JP.lang"
};
case nn::swkbd::LanguageType::Dutch: return {
#include "lang/nl_NL.lang"
};
case nn::swkbd::LanguageType::Russian: return {
#include "lang/ru_RU.lang"
};
}
}

View File

@ -17,22 +17,4 @@ public:
static bool shown_uninitialized_warning;
};
struct config_strings {
const char *plugin_name;
std::string_view network_category;
std::string_view connect_to_network_setting;
std::string_view other_category;
std::string_view reset_wwp_setting;
std::string_view press_a_action;
std::string_view restart_to_apply_action;
std::string_view need_menu_action;
std::string_view using_nintendo_network;
std::string_view using_pretendo_network;
std::string_view multiplayer_port_display;
std::string_view module_not_found;
std::string_view module_init_not_found;
};
config_strings get_config_strings(nn::swkbd::LanguageType language);
#endif //INKAY_CONFIG_H

View File

@ -75,7 +75,8 @@ WUMS_USE_WUT_DEVOPTAB();
#include "patches/eshop_applet.h"
#include "patches/olv_applet.h"
#include "patches/game_peertopeer.h"
#include "utils/sysconfig.h"
#include "sysconfig.h"
#include "lang.h"
//thanks @Gary#4139 :p
static void write_string(uint32_t addr, const char *str) {

View File

@ -15,30 +15,13 @@
#include <coreinit/dynload.h>
#include "game_peertopeer.h"
#include "utils/sysconfig.h"
#include "sysconfig.h"
#include "utils/logger.h"
#include "utils/rpl_info.h"
#include "utils/replace_mem.h"
#include <optional>
static inline int digit(char a) {
if (a < '0' || a > '9') return 0;
return a - '0';
}
unsigned short peertopeer_port() {
const char * serial = get_console_serial();
unsigned short port = 50000 +
(digit(serial[4]) * 1000) +
(digit(serial[5]) * 100 ) +
(digit(serial[6]) * 10 ) +
(digit(serial[7]) * 1 );
return port;
}
static void minecraft_peertopeer_patch() {
std::optional<OSDynLoad_NotifyData> minecraft = search_for_rpl("Minecraft.Client.rpx");
if (!minecraft) {
@ -46,7 +29,7 @@ static void minecraft_peertopeer_patch() {
return;
}
auto port = peertopeer_port();
auto port = get_console_peertopeer_port();
DEBUG_FUNCTION_LINE_VERBOSE("Will use port %d. %08x", port, minecraft->textAddr);
uint32_t *target_func = rpl_addr(*minecraft, 0x03579530);

View File

@ -14,4 +14,3 @@
#pragma once
void peertopeer_patch();
unsigned short peertopeer_port();