Switch: Use a few more native APIs

This commit is contained in:
UnknownShadow200 2024-04-01 22:13:32 +11:00
parent 0ff916fbfc
commit 5b7800fe2b
2 changed files with 26 additions and 32 deletions

View File

@ -21,9 +21,9 @@ ClassiCube is not trying to replicate modern Minecraft versions. It will never s
You can **download ClassiCube** [from here](https://www.classicube.net/download/) and the very latest builds [from here](https://www.classicube.net/nightlies/). You can **download ClassiCube** [from here](https://www.classicube.net/download/) and the very latest builds [from here](https://www.classicube.net/nightlies/).
![classic](https://github.com/ClassiCube/actions-testing-cc/assets/7892772/a233cb4c-296a-4d08-87fc-49874c230d4f) ![classic](https://github.com/ClassiCube/ClassiCube/assets/6509348/eedee53f-f53e-456f-b51c-92c62079eee0)
![enhanced](https://github.com/ClassiCube/actions-testing-cc/assets/7892772/61a064bd-cfaa-4a91-bedf-a16c3dd7e8a2) ![enhanced](https://github.com/ClassiCube/ClassiCube/assets/6509348/b2fe0e2b-5d76-41ab-909f-048d0ad15f37)
# We need your help # We need your help

View File

@ -23,13 +23,10 @@
#include <arpa/inet.h> #include <arpa/inet.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/types.h> #include <sys/types.h>
#include <netdb.h> #include <netdb.h>
#include "_PlatformConsole.h" #include "_PlatformConsole.h"
const cc_result ReturnCode_FileShareViolation = 1000000000; // not used const cc_result ReturnCode_FileShareViolation = 1000000000; // not used
const cc_result ReturnCode_FileNotFound = ENOENT; const cc_result ReturnCode_FileNotFound = ENOENT;
const cc_result ReturnCode_SocketInProgess = EINPROGRESS; const cc_result ReturnCode_SocketInProgess = EINPROGRESS;
@ -37,7 +34,6 @@ const cc_result ReturnCode_SocketWouldBlock = EWOULDBLOCK;
const cc_result ReturnCode_DirectoryExists = EEXIST; const cc_result ReturnCode_DirectoryExists = EEXIST;
const char* Platform_AppNameSuffix = " Switch"; const char* Platform_AppNameSuffix = " Switch";
alignas(16) u8 __nx_exception_stack[0x1000]; alignas(16) u8 __nx_exception_stack[0x1000];
u64 __nx_exception_stack_size = sizeof(__nx_exception_stack); u64 __nx_exception_stack_size = sizeof(__nx_exception_stack);
@ -74,13 +70,13 @@ void __libnx_exception_handler(ThreadExceptionDump *ctx)
cc_uint64 Stopwatch_ElapsedMicroseconds(cc_uint64 beg, cc_uint64 end) { cc_uint64 Stopwatch_ElapsedMicroseconds(cc_uint64 beg, cc_uint64 end) {
if (end < beg) return 0; if (end < beg) return 0;
return (end - beg) / 1000; // See include/switch/arm/counter.h
// static inline u64 armTicksToNs(u64 tick) { return (tick * 625) / 12; }
return ((end - beg) * 625) / 12000;
} }
cc_uint64 Stopwatch_Measure(void) { cc_uint64 Stopwatch_Measure(void) {
struct timespec t; return armGetSystemTick();
clock_gettime(CLOCK_MONOTONIC, &t);
return (cc_uint64)t.tv_sec * 1e9 + t.tv_nsec;
} }
void Platform_Log(const char* msg, int len) { void Platform_Log(const char* msg, int len) {
@ -88,23 +84,23 @@ void Platform_Log(const char* msg, int len) {
} }
TimeMS DateTime_CurrentUTC(void) { TimeMS DateTime_CurrentUTC(void) {
struct timeval cur; u64 timestamp = 0;
gettimeofday(&cur, NULL); timeGetCurrentTime(TimeType_Default, &timestamp);
return (cc_uint64)cur.tv_sec + UNIX_EPOCH_SECONDS; return timestamp + UNIX_EPOCH_SECONDS;
} }
void DateTime_CurrentLocal(struct DateTime* t) { void DateTime_CurrentLocal(struct DateTime* t) {
struct timeval cur; u64 timestamp = 0;
struct tm loc_time; TimeCalendarTime calTime = { 0 };
gettimeofday(&cur, NULL); timeGetCurrentTime(TimeType_Default, &timestamp);
localtime_r(&cur.tv_sec, &loc_time); timeToCalendarTimeWithMyRule(timestamp, &calTime, NULL);
t->year = loc_time.tm_year + 1900; t->year = calTime.year;
t->month = loc_time.tm_mon + 1; t->month = calTime.month;
t->day = loc_time.tm_mday; t->day = calTime.day;
t->hour = loc_time.tm_hour; t->hour = calTime.hour;
t->minute = loc_time.tm_min; t->minute = calTime.minute;
t->second = loc_time.tm_sec; t->second = calTime.second;
} }
@ -227,7 +223,7 @@ cc_result File_Length(cc_file file, cc_uint32* len) {
*--------------------------------------------------------Threading--------------------------------------------------------* *--------------------------------------------------------Threading--------------------------------------------------------*
*#########################################################################################################################*/ *#########################################################################################################################*/
void Thread_Sleep(cc_uint32 milliseconds) { void Thread_Sleep(cc_uint32 milliseconds) {
cc_uint64 timeout_ns = milliseconds * (1000 * 1000); // to nanoseconds cc_uint64 timeout_ns = (cc_uint64)milliseconds * (1000 * 1000); // to nanoseconds
svcSleepThread(timeout_ns); svcSleepThread(timeout_ns);
} }
@ -243,7 +239,11 @@ void Thread_Run(void** handle, Thread_StartFunc func, int stackSize, const char*
threadStart(thread); threadStart(thread);
} }
void Thread_Detach(void* handle) { } void Thread_Detach(void* handle) {
// threadClose frees up resources, **including the stack of the thread**
// Which obviously completely breaks the thread - so instead just accept
// that there will be a small memory leak when non-joined threads exit
}
void Thread_Join(void* handle) { void Thread_Join(void* handle) {
Thread* thread = (Thread*)handle; Thread* thread = (Thread*)handle;
@ -315,7 +315,6 @@ void Waitable_Wait(void* handle) {
void Waitable_WaitFor(void* handle, cc_uint32 milliseconds) { void Waitable_WaitFor(void* handle, cc_uint32 milliseconds) {
struct WaitData* ptr = (struct WaitData*)handle; struct WaitData* ptr = (struct WaitData*)handle;
cc_uint64 timeout_ns = (cc_uint64)milliseconds * (1000 * 1000); // to nanoseconds cc_uint64 timeout_ns = (cc_uint64)milliseconds * (1000 * 1000); // to nanoseconds
Mutex_Lock(&ptr->mutex); Mutex_Lock(&ptr->mutex);
@ -359,10 +358,8 @@ void Waitable_WaitFor(void* handle, cc_uint32 milliseconds) {
union SocketAddress { union SocketAddress {
struct sockaddr raw; struct sockaddr raw;
struct sockaddr_in v4; struct sockaddr_in v4;
#ifdef AF_INET6
struct sockaddr_in6 v6; struct sockaddr_in6 v6;
struct sockaddr_storage total; struct sockaddr_storage total;
#endif
}; };
static cc_result ParseHost(const char* host, int port, cc_sockaddr* addrs, int* numValidAddrs) { static cc_result ParseHost(const char* host, int port, cc_sockaddr* addrs, int* numValidAddrs) {
@ -419,7 +416,6 @@ cc_result Socket_ParseAddress(const cc_string* address, int port, cc_sockaddr* a
return 0; return 0;
} }
#ifdef AF_INET6
if (inet_pton(AF_INET6, str, &addr->v6.sin6_addr) > 0) { if (inet_pton(AF_INET6, str, &addr->v6.sin6_addr) > 0) {
addr->v6.sin6_family = AF_INET6; addr->v6.sin6_family = AF_INET6;
addr->v6.sin6_port = htons(port); addr->v6.sin6_port = htons(port);
@ -428,7 +424,6 @@ cc_result Socket_ParseAddress(const cc_string* address, int port, cc_sockaddr* a
*numValidAddrs = 1; *numValidAddrs = 1;
return 0; return 0;
} }
#endif
return ParseHost(str, port, addrs, numValidAddrs); return ParseHost(str, port, addrs, numValidAddrs);
} }
@ -441,8 +436,7 @@ cc_result Socket_Connect(cc_socket* s, cc_sockaddr* addr, cc_bool nonblocking) {
if (*s == -1) return errno; if (*s == -1) return errno;
if (nonblocking) { if (nonblocking) {
int blocking_raw = -1; /* non-blocking mode */ fcntl(*s, F_SETFL, O_NONBLOCK);
ioctl(*s, FIONBIO, &blocking_raw);
} }
res = connect(*s, raw, addr->size); res = connect(*s, raw, addr->size);