From b0981ba522f8bd5dfc66976aac29bb5ce2f92368 Mon Sep 17 00:00:00 2001 From: Carsten Teibes Date: Sun, 29 Dec 2024 02:38:25 +0100 Subject: [PATCH] bugfix: restore 3DS build Cleanup network code Add 3dslink debug code --- builds/cmake/OJ-misc.cmake | 2 +- src/io/controls.cpp | 2 +- src/io/file.cpp | 2 +- src/io/gfx/video.cpp | 2 +- src/io/gfx/video.h | 2 +- src/io/network.cpp | 34 +++++++-------------------- src/io/sound.cpp | 4 ++-- src/menu/menu.cpp | 4 ++-- src/menu/setupmenu.cpp | 2 +- src/platforms/3ds.cpp | 48 +++++++++++++++++++++++++++++++++++++- src/platforms/3ds.h | 6 ++++- src/platforms/platforms.h | 8 +++---- 12 files changed, 74 insertions(+), 42 deletions(-) diff --git a/builds/cmake/OJ-misc.cmake b/builds/cmake/OJ-misc.cmake index a2d4c25b..e0eea10b 100644 --- a/builds/cmake/OJ-misc.cmake +++ b/builds/cmake/OJ-misc.cmake @@ -98,7 +98,7 @@ if(CPPCHECK_EXECUTABLE) -I${CMAKE_CURRENT_SOURCE_DIR}/src -U__SYMBIAN32__ -UUIQ3 -UENABLE_JJ2 # unmaintained -UGP2X -UWIZ -UDINGOO -UCAANOO -UGAMESHELL # contributed - -UPSP -U__vita__ -U_3DS -U__wii__ # homebrew + -UPSP -U__vita__ -U__3DS__ -U__wii__ # homebrew -U__riscos__ #--enable=information ) diff --git a/src/io/controls.cpp b/src/io/controls.cpp index 78f8c564..c99f2379 100644 --- a/src/io/controls.cpp +++ b/src/io/controls.cpp @@ -156,7 +156,7 @@ #define DEFAULT_BUTTON_PAUSE (5) #define DEFAULT_BUTTON_YES (-1) #define DEFAULT_BUTTON_NO (-1) -#elif defined (_3DS) +#elif defined (__3DS__) #define DEFAULT_BUTTON_UP (-1) #define DEFAULT_BUTTON_DOWN (-1) #define DEFAULT_BUTTON_LEFT (-1) diff --git a/src/io/file.cpp b/src/io/file.cpp index 42c6aed8..0fc57c24 100644 --- a/src/io/file.cpp +++ b/src/io/file.cpp @@ -33,7 +33,7 @@ #include #include -#if !(defined(_WIN32) || defined(WII) || defined(PSP) || defined(_3DS)) +#if !(defined(_WIN32) || defined(WII) || defined(PSP)) #define UPPERCASE_FILENAMES #define LOWERCASE_FILENAMES #endif diff --git a/src/io/gfx/video.cpp b/src/io/gfx/video.cpp index deca46f9..f96df4e3 100644 --- a/src/io/gfx/video.cpp +++ b/src/io/gfx/video.cpp @@ -711,7 +711,7 @@ void Video::clearScreen (int index) { #if defined(CAANOO) || defined(WIZ) || defined(GP2X) || defined(GAMESHELL) // always 240 lines cleared to black memset(video.screen->pixels, index, 320*240); -#elif defined (_3DS) +#elif defined (__3DS__) memset(video.screen->pixels, index, 400*240); #else SDL_FillRect(canvas, NULL, index); diff --git a/src/io/gfx/video.h b/src/io/gfx/video.h index 513f0131..d9e6cca1 100644 --- a/src/io/gfx/video.h +++ b/src/io/gfx/video.h @@ -76,7 +76,7 @@ #define NO_RESIZE #define FULLSCREEN_FLAGS (SDL_FULLSCREEN | SDL_SWSURFACE | SDL_HWPALETTE) -#elif defined(_3DS) +#elif defined(__3DS__) #define DEFAULT_SCREEN_WIDTH 400 #define DEFAULT_SCREEN_HEIGHT 240 diff --git a/src/io/network.cpp b/src/io/network.cpp index 4c540382..1d138e64 100644 --- a/src/io/network.cpp +++ b/src/io/network.cpp @@ -56,12 +56,8 @@ #ifndef MSG_NOSIGNAL #define MSG_NOSIGNAL 0 #endif - #ifdef _3DS - #include <3ds.h> - #include - #include - static u32* socBuffer = NULL; - #define SOC_BUFFERSIZE 0x100000 // maybe 0x80000 is enough + #ifdef __3DS__ + #include "platforms/3ds.h" #endif #elif defined(__wii__) #include @@ -81,9 +77,9 @@ Network::Network () { // Start Windows Sockets WSAStartup(MAKEWORD(1, 0), &WSAData); - #elif defined(_3DS) - socBuffer = static_cast(memalign(0x1000, SOC_BUFFERSIZE)); - socInit(socBuffer, SOC_BUFFERSIZE); + #elif defined(__3DS__) + if (!N3DS_NetHasConsole()) + N3DS_NetInit(); #endif #elif defined USE_SDL_NET # ifdef __wii__ @@ -107,9 +103,9 @@ Network::~Network () { #ifdef _WIN32 // Shut down Windows Sockets WSACleanup(); - #elif defined(_3DS) - socExit(); - free(socBuffer); + #elif defined(__3DS_) + if (!N3DS_NetHasConsole()) + N3DS_NetExit(); #endif #elif defined USE_SDL_NET SDLNet_Quit(); @@ -136,12 +132,8 @@ int Network::host () { // Make the socket non-blocking -#ifdef _3DS - fcntl(sock, F_SETFL, fcntl(sock, F_GETFL, 0) | O_NONBLOCK); -#else int nonblock = 1; ioctl(sock, FIONBIO, (u_long *)&nonblock); -#endif memset(&sockAddr, 0, sizeof(sockaddr_in)); sockAddr.sin_family = AF_INET; @@ -198,21 +190,15 @@ int Network::join (char *address) { int sock, con; // Create socket - sock = socket(AF_INET, SOCK_STREAM, 0); if (sock == -1) return E_N_SOCKET; // Make socket non-blocking -#ifdef _3DS - fcntl(sock, F_SETFL, fcntl(sock, F_GETFL, 0) | O_NONBLOCK); -#else con = 1; ioctl(sock, FIONBIO, (u_long *)&con); -#endif // Connect to server - memset(&sockAddr, 0, sizeof(sockaddr_in)); sockAddr.sin_family = AF_INET; sockAddr.sin_port = htons(NET_PORT); @@ -321,12 +307,8 @@ int Network::accept (int sock) { if (clientSocket != -1) { // Make the socket non-blocking -#ifdef _3DS - fcntl(clientSocket, F_SETFL, fcntl(sock, F_GETFL, 0) | O_NONBLOCK); -#else length = 1; ioctl(clientSocket, FIONBIO, (u_long *)&length); -#endif } diff --git a/src/io/sound.cpp b/src/io/sound.cpp index 8e19478d..8f9fb694 100644 --- a/src/io/sound.cpp +++ b/src/io/sound.cpp @@ -36,13 +36,13 @@ #define OJ_SDL2 0 #endif -#if defined(__SYMBIAN32__) || defined(_3DS) || defined(PSP) || defined(__vita__) +#if defined(__SYMBIAN32__) || defined(__3DS__) || defined(PSP) || defined(__vita__) #define SOUND_FREQ 22050 #else #define SOUND_FREQ 44100 #endif -#if defined(GP2X) || defined(PSP) || defined(_3DS) || defined(__vita__) +#if defined(GP2X) || defined(PSP) || defined(__3DS__) || defined(__vita__) #define SOUND_SAMPLES 512 #else #define SOUND_SAMPLES 2048 diff --git a/src/menu/menu.cpp b/src/menu/menu.cpp index 5e1e6251..b1b00909 100644 --- a/src/menu/menu.cpp +++ b/src/menu/menu.cpp @@ -35,7 +35,7 @@ #include "io/sound.h" #include "loop.h" #include "util.h" -#ifdef _3DS +#ifdef __3DS__ #include "platforms/3ds.h" #elif defined(__vita__) #include "platforms/psvita.h" @@ -176,7 +176,7 @@ int Menu::textInput (const char* request, char*& text, bool ip) { char *input; -#ifdef _3DS +#ifdef __3DS__ int res; diff --git a/src/menu/setupmenu.cpp b/src/menu/setupmenu.cpp index f30de8a3..06bb6f3b 100644 --- a/src/menu/setupmenu.cpp +++ b/src/menu/setupmenu.cpp @@ -695,7 +695,7 @@ int SetupMenu::setupMain () { case 1: -#if !(defined(CAANOO) || defined(WIZ) || defined(GP2X) || defined(PSP) || defined(_3DS) || defined(__vita__)) +#if !(defined(CAANOO) || defined(WIZ) || defined(GP2X) || defined(PSP) || defined(__3DS__) || defined(__vita__)) if (setupKeyboard() == E_QUIT) return E_QUIT; #else if (message("FEATURE NOT AVAILABLE") == E_QUIT) return E_QUIT; diff --git a/src/platforms/3ds.cpp b/src/platforms/3ds.cpp index c4154f8c..038b17d8 100644 --- a/src/platforms/3ds.cpp +++ b/src/platforms/3ds.cpp @@ -15,19 +15,65 @@ #include "3ds.h" -#ifdef _3DS +#ifdef __3DS__ #include <3ds.h> +#include #include "util.h" #include "io/file.h" +#include "io/log.h" + +namespace { + constexpr int SOC_BUFFERSIZE = 0x100000; + + u32 *socBuffer = nullptr; + int sock = -1; +} void N3DS_Init() { // file system romfsInit(); + +#ifndef NDEBUG + // network console + N3DS_NetInit(); + sock = link3dsStdio(); +#endif } void N3DS_Exit() { romfsExit(); + +#ifndef NDEBUG + N3DS_NetExit(); +#endif +} + +void N3DS_NetInit() { + socBuffer = static_cast(memalign(0x1000, SOC_BUFFERSIZE)); + if(!socBuffer) { + LOG_WARN("Failed to allocate soc:u buffer"); + return; + } + + int ret = socInit(socBuffer, SOC_BUFFERSIZE); + if (ret != 0) { + LOG_WARN("Failed to initialize soc:u: 0x%08X\n", (unsigned int)ret); + } +} + +void N3DS_NetExit() { + if(sock > 0) + close(sock); + + socExit(); + + if(socBuffer) + free(socBuffer); +} + +bool N3DS_NetHasConsole() { + return sock > 0; } void N3DS_AddGamePaths() { diff --git a/src/platforms/3ds.h b/src/platforms/3ds.h index 6db8556e..da744a02 100644 --- a/src/platforms/3ds.h +++ b/src/platforms/3ds.h @@ -17,11 +17,15 @@ #ifndef _3DS_H #define _3DS_H -#ifdef _3DS +#ifdef __3DS__ void N3DS_Init(); void N3DS_Exit(); +void N3DS_NetInit(); +void N3DS_NetExit(); +bool N3DS_NetHasConsole(); + void N3DS_AddGamePaths(); void N3DS_ErrorNoDatafiles(); diff --git a/src/platforms/platforms.h b/src/platforms/platforms.h index b69d4b0c..ce48bfe2 100644 --- a/src/platforms/platforms.h +++ b/src/platforms/platforms.h @@ -36,7 +36,7 @@ inline void PLATFORM_Init() { WII_Init(); #endif -#ifdef _3DS +#ifdef __3DS__ N3DS_Init(); #endif @@ -46,7 +46,7 @@ inline void PLATFORM_Init() { } inline void PLATFORM_Exit() { -#ifdef _3DS +#ifdef __3DS__ N3DS_Exit(); #endif } @@ -60,7 +60,7 @@ inline void PLATFORM_AddGamePaths() { SYMBIAN_AddGamePaths(); #endif -#ifdef _3DS +#ifdef __3DS__ N3DS_AddGamePaths(); #endif @@ -98,7 +98,7 @@ inline void PLATFORM_ErrorNoDatafiles() { PSP_ErrorNoDatafiles(); #endif -#ifdef _3DS +#ifdef __3DS__ N3DS_ErrorNoDatafiles(); #endif }