diff --git a/CMakeLists.txt b/CMakeLists.txt index 43e96f1..65e036f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -31,7 +31,7 @@ src/Crypto/ClientTLSStream.cpp src/TF_Init.cpp ) -set(TESSESFRAMEWORK_CERT_BUNDLE_FILE "/etc/ssl/certs/ca-certificates.crt") +set(TESSESFRAMEWORK_CERT_BUNDLE_FILE "/etc/ssl/certs/ca-certificates.crt" CACHE FILEPATH "Path to ca-chain") option(TESSESFRAMEWORK_EMBED_CERT_BUNDLE "Embed the certificate chain bundle" ON) option(TESSESFRAMEWORK_ENABLE_MBED "Enable Tesses Framework mbedtls" ON) option(TESSESFRAMEWORK_ENABLE_EXAMPLES "Enable Tesses Framework examples" ON) @@ -54,7 +54,7 @@ file(APPEND ${CMAKE_CURRENT_BINARY_DIR}/include/CertificateChain.h "\n") endif() endif() -set(MBEDTLS_DIR "") +set(MBEDTLS_DIR "" CACHE PATH "Mbed tls directory") function(link_deps TessesFramework_TARGET) if(TESSESFRAMEWORK_ENABLE_MBED) @@ -83,6 +83,11 @@ endif() if("${CMAKE_SYSTEM_NAME}" STREQUAL "NintendoWii") target_link_libraries(${TessesFramework_TARGET} PUBLIC wiisocket) target_compile_definitions(${TessesFramework_TARGET} PUBLIC TESSESFRAMEWORK_USE_WII_SOCKET) +endif() +if("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows") + +target_link_libraries(${TessesFramework_TARGET} PUBLIC ws2_32) + endif() endfunction() diff --git a/include/TessesFramework/Filesystem/LocalFS.hpp b/include/TessesFramework/Filesystem/LocalFS.hpp index c7d6ec4..b98fcd3 100644 --- a/include/TessesFramework/Filesystem/LocalFS.hpp +++ b/include/TessesFramework/Filesystem/LocalFS.hpp @@ -1,11 +1,15 @@ #pragma once #include "VFS.hpp" +#include "VFSFix.hpp" + namespace Tesses::Framework::Filesystem { class LocalFilesystem : public VFS { public: + Tesses::Framework::Streams::Stream* OpenFile(VFSPath path, std::string mode); + void CreateDirectory(VFSPath path); void DeleteDirectory(VFSPath path); bool RegularFileExists(VFSPath path); @@ -15,11 +19,14 @@ namespace Tesses::Framework::Filesystem bool SocketFileExists(VFSPath path); bool FIFOFileExists(VFSPath path); bool DirectoryExists(VFSPath path); + void DeleteFile(VFSPath path); void CreateSymlink(VFSPath existingFile, VFSPath symlinkFile); VFSPathEnumerator EnumeratePaths(VFSPath path); void CreateHardlink(VFSPath existingFile, VFSPath newName); + void MoveFile(VFSPath src, VFSPath dest); + void MoveDirectory(VFSPath src, VFSPath dest); VFSPath ReadLink(VFSPath path); std::string VFSPathToSystem(VFSPath path); diff --git a/include/TessesFramework/Filesystem/MountableFilesystem.hpp b/include/TessesFramework/Filesystem/MountableFilesystem.hpp index 7d145df..54d1cbb 100644 --- a/include/TessesFramework/Filesystem/MountableFilesystem.hpp +++ b/include/TessesFramework/Filesystem/MountableFilesystem.hpp @@ -1,5 +1,7 @@ #pragma once #include "VFS.hpp" +#include "VFSFix.hpp" + namespace Tesses::Framework::Filesystem { class MountableDirectory { diff --git a/include/TessesFramework/Filesystem/NullFilesystem.hpp b/include/TessesFramework/Filesystem/NullFilesystem.hpp index 49c8cc1..a0bf7ce 100644 --- a/include/TessesFramework/Filesystem/NullFilesystem.hpp +++ b/include/TessesFramework/Filesystem/NullFilesystem.hpp @@ -1,5 +1,7 @@ #pragma once #include "VFS.hpp" +#include "VFSFix.hpp" + namespace Tesses::Framework::Filesystem { class NullFilesystem : public VFS diff --git a/include/TessesFramework/Filesystem/SubdirFilesystem.hpp b/include/TessesFramework/Filesystem/SubdirFilesystem.hpp index 86a6ff8..88c45c9 100644 --- a/include/TessesFramework/Filesystem/SubdirFilesystem.hpp +++ b/include/TessesFramework/Filesystem/SubdirFilesystem.hpp @@ -1,5 +1,7 @@ #pragma once #include "VFS.hpp" +#include "VFSFix.hpp" + namespace Tesses::Framework::Filesystem { class SubdirFilesystem : public VFS diff --git a/include/TessesFramework/Filesystem/VFS.hpp b/include/TessesFramework/Filesystem/VFS.hpp index 5348774..632777f 100644 --- a/include/TessesFramework/Filesystem/VFS.hpp +++ b/include/TessesFramework/Filesystem/VFS.hpp @@ -3,6 +3,8 @@ #include "../Streams/Stream.hpp" #include #include +#include "VFSFix.hpp" + namespace Tesses::Framework::Filesystem { class VFSPath { diff --git a/include/TessesFramework/Threading/Mutex.hpp b/include/TessesFramework/Threading/Mutex.hpp index abfaab9..82bc0f8 100644 --- a/include/TessesFramework/Threading/Mutex.hpp +++ b/include/TessesFramework/Threading/Mutex.hpp @@ -1,5 +1,7 @@ #pragma once -#if defined(GEKKO) +#if defined(_WIN32) +#include +#elif defined(GEKKO) #include #else #include @@ -7,7 +9,9 @@ namespace Tesses::Framework::Threading { class Mutex { - #if defined(GEKKO) + #if defined(_WIN32) + HANDLE mtx; + #elif defined(GEKKO) mutex_t mtx; #else mtx_t mtx; diff --git a/include/TessesFramework/Threading/Thread.hpp b/include/TessesFramework/Threading/Thread.hpp index f1218b8..cdefbfd 100644 --- a/include/TessesFramework/Threading/Thread.hpp +++ b/include/TessesFramework/Threading/Thread.hpp @@ -1,6 +1,8 @@ #pragma once #include -#if defined(GEKKO) +#if defined(_WIN32) +#include +#elif defined(GEKKO) #include #else #include @@ -10,8 +12,13 @@ namespace Tesses::Framework::Threading { class Thread { - std::atomic hasInvoked; - #if defined(GEKKO) + #if defined(_WIN32) + + HANDLE thrd; + DWORD thrdId; + + public: + #elif defined(GEKKO) lwp_t thrd; static void* cb(void* ptr); #else @@ -19,6 +26,8 @@ namespace Tesses::Framework::Threading static int cb(void* ptr); #endif std::function fn; + + std::atomic hasInvoked; public: Thread(std::function fn); void Join(); diff --git a/src/Filesystem/LocalFS.cpp b/src/Filesystem/LocalFS.cpp index bf127a9..6c21a09 100644 --- a/src/Filesystem/LocalFS.cpp +++ b/src/Filesystem/LocalFS.cpp @@ -25,7 +25,8 @@ namespace Tesses::Framework::Filesystem } VFSPath LocalFilesystem::ReadLink(VFSPath path) { - return this->SystemToVFSPath(std::filesystem::read_symlink(this->VFSPathToSystem(path))); + auto res = std::filesystem::read_symlink(this->VFSPathToSystem(path)).string(); + return this->SystemToVFSPath(res.c_str()); } Tesses::Framework::Streams::Stream* LocalFilesystem::OpenFile(VFSPath path, std::string mode) { @@ -97,12 +98,12 @@ namespace Tesses::Framework::Filesystem } std::string LocalFilesystem::VFSPathToSystem(VFSPath path) { - #if defined(WIN32) + #if defined(_WIN32) bool first=true; std::string p = {}; for(auto item : path.path) { - if(!(first && !item.empty() && item.back()==':') && !(first && this->relative)) + if(!(first && !item.empty() && item.back()==':') && !(first && path.relative)) p.push_back('\\'); p.append(item); first=false; diff --git a/src/Http/FileServer.cpp b/src/Http/FileServer.cpp index 6324ed1..546becb 100644 --- a/src/Http/FileServer.cpp +++ b/src/Http/FileServer.cpp @@ -15,8 +15,8 @@ namespace Tesses::Framework::Http } FileServer::FileServer(std::filesystem::path path,bool allowListing, bool spa, std::vector defaultNames) { - LocalFilesystem* lfs=new LocalFilesystem; - SubdirFilesystem* sdfs=new SubdirFilesystem(lfs,lfs->SystemToVFSPath(path),true); + LocalFilesystem* lfs=new LocalFilesystem(); + SubdirFilesystem* sdfs=new SubdirFilesystem(lfs,lfs->SystemToVFSPath(path.string()),true); this->vfs = sdfs; this->spa = spa; diff --git a/src/Streams/FileStream.cpp b/src/Streams/FileStream.cpp index ca52b74..6472a8d 100644 --- a/src/Streams/FileStream.cpp +++ b/src/Streams/FileStream.cpp @@ -1,5 +1,7 @@ #include "TessesFramework/Streams/FileStream.hpp" - +#if defined(_WIN32) +#include +#endif namespace Tesses::Framework::Streams { void FileStream::SetMode(std::string mode) @@ -32,7 +34,8 @@ namespace Tesses::Framework::Streams } FileStream::FileStream(std::filesystem::path p, std::string mode) { - this->f = fopen(p.c_str(),mode.c_str()); + std::string str = p.string(); + this->f = fopen(str.c_str(),mode.c_str()); this->canSeek = true; this->owns=true; this->SetMode(mode); @@ -66,7 +69,11 @@ namespace Tesses::Framework::Streams } int64_t FileStream::GetPosition() { + #if defined(_WIN32) + return (int64_t)_ftelli64(this->f); + #else return (int64_t)ftello(this->f); + #endif } void FileStream::Flush() { @@ -74,7 +81,11 @@ namespace Tesses::Framework::Streams } void FileStream::Seek(int64_t pos, SeekOrigin whence) { + #if defined(_WIN32) + _fseeki64(this->f,pos,whence == SeekOrigin::Begin ? SEEK_SET : whence == SeekOrigin::Current ? SEEK_CUR : SEEK_END); + #else fseeko(this->f,(off_t)pos,whence == SeekOrigin::Begin ? SEEK_SET : whence == SeekOrigin::Current ? SEEK_CUR : SEEK_END); + #endif } FileStream::~FileStream() { diff --git a/src/Streams/NetworkStream.cpp b/src/Streams/NetworkStream.cpp index 1a24b17..f7eb672 100644 --- a/src/Streams/NetworkStream.cpp +++ b/src/Streams/NetworkStream.cpp @@ -14,7 +14,12 @@ using HttpUtils = Tesses::Framework::Http::HttpUtils; #else - +#if defined(_WIN32) +#include +#include +#include +#pragma comment(lib, "ws2_32.lib") +#else extern "C" { #include #include @@ -23,10 +28,11 @@ extern "C" { #include #include } +#endif #if defined(GEKKO) extern "C" uint32_t if_config( char *local_ip, char *netmask, char *gateway,bool use_dhcp, int max_retries); -#else +#elif !defined(_WIN32) #include #endif @@ -42,8 +48,12 @@ extern "C" uint32_t if_config( char *local_ip, char *netmask, char *gateway,bool #define NETWORK_ACCEPT accept #define NETWORK_GETADDRINFO getaddrinfo #define NETWORK_FREEADDRINFO freeaddrinfo +#if defined(_WIN32) +#define NETWORK_CLOSE closesocket +#else #define NETWORK_CLOSE close #endif +#endif #undef AF_INET6 @@ -61,6 +71,8 @@ namespace Tesses::Framework::Streams { char gateway[16]; if_config(localIp,netmask, gateway, true, 1); ipConfig.push_back(std::pair("net",localIp)); + #elif defined(_WIN32) + #else struct ifaddrs *ifAddrStruct = NULL; getifaddrs(&ifAddrStruct); @@ -367,7 +379,7 @@ namespace Tesses::Framework::Streams { if(broadcast) { int broadcast = 1; - if (NETWORK_SETSOCKOPT(sock, SOL_SOCKET, SO_BROADCAST, &broadcast, sizeof(broadcast)) != 0) { + if (NETWORK_SETSOCKOPT(sock, SOL_SOCKET, SO_BROADCAST, (const char*)&broadcast, sizeof(broadcast)) != 0) { this->success=false; NETWORK_CLOSE(this->sock); continue; @@ -434,7 +446,7 @@ namespace Tesses::Framework::Streams { { if(!this->success) return; int broadcast = 1; - if (NETWORK_SETSOCKOPT(sock, SOL_SOCKET, SO_BROADCAST, &broadcast, sizeof(broadcast)) != 0) + if (NETWORK_SETSOCKOPT(sock, SOL_SOCKET, SO_BROADCAST, (const char*)&broadcast, sizeof(broadcast)) != 0) { this->success=false; if(this->owns) @@ -462,7 +474,7 @@ namespace Tesses::Framework::Streams { { if(!this->success) return 0; - ssize_t r = NETWORK_RECV(this->sock,buff,sz,0); + ssize_t r = NETWORK_RECV(this->sock,(char*)buff,sz,0); if(r <= 0) { @@ -477,7 +489,7 @@ namespace Tesses::Framework::Streams { if(!this->success) return 0; - ssize_t sz2 = NETWORK_SEND(this->sock,buff,sz, 0); + ssize_t sz2 = NETWORK_SEND(this->sock,(const char*)buff,sz, 0); if(sz2 < 0) return 0; return (size_t)sz; @@ -487,7 +499,7 @@ namespace Tesses::Framework::Streams { if(!this->success) return 0; struct sockaddr_storage storage; socklen_t addrlen=(socklen_t)sizeof(storage); - ssize_t r = NETWORK_RECVFROM(this->sock,buff,sz,0, (struct sockaddr*)&storage, (socklen_t*)&addrlen); + ssize_t r = NETWORK_RECVFROM(this->sock,(char*)buff,sz,0, (struct sockaddr*)&storage, (socklen_t*)&addrlen); ip = StringifyIP((struct sockaddr*)&storage); port = GetPort((struct sockaddr*)&storage); if(r < 0) return 0; @@ -516,7 +528,7 @@ namespace Tesses::Framework::Streams { } SetPort((struct sockaddr*)&addr, port); - ssize_t sz2 = NETWORK_SENDTO(this->sock,buff,sz, 0, (const sockaddr*)&addr, (socklen_t)sizeof(addr)); + ssize_t sz2 = NETWORK_SENDTO(this->sock,(const char*)buff,sz, 0, (const sockaddr*)&addr, (socklen_t)sizeof(addr)); if(sz2 < 0) return 0; return (size_t)sz2; } diff --git a/src/TF_Init.cpp b/src/TF_Init.cpp index 74b1659..7102e13 100644 --- a/src/TF_Init.cpp +++ b/src/TF_Init.cpp @@ -2,7 +2,10 @@ #include "TessesFramework/Streams/NetworkStream.hpp" #include #include -#if defined(GEKKO) +#if defined(_WIN32) +#include +#include +#elif defined(GEKKO) #include #include #include @@ -63,10 +66,22 @@ namespace Tesses::Framework void TF_Init() { - + #if defined(_WIN32) + system(" "); + #endif isRunning=true; - #if defined(GEKKO) + #if defined(_WIN32) + WSADATA wsaData; + int iResult; + +// Initialize Winsock +iResult = WSAStartup(MAKEWORD(2,2), &wsaData); +if (iResult != 0) { + printf("WSAStartup failed: %d\n", iResult); + return; +} + #elif defined(GEKKO) fatInitDefault(); VIDEO_Init(); PAD_Init(); diff --git a/src/Threading/Mutex.cpp b/src/Threading/Mutex.cpp index b53d804..137bb02 100644 --- a/src/Threading/Mutex.cpp +++ b/src/Threading/Mutex.cpp @@ -4,7 +4,9 @@ namespace Tesses::Framework::Threading { Mutex::Mutex() { - #if defined(GEKKO) + #if defined(_WIN32) + this->mtx = CreateMutex(NULL,false,NULL); + #elif defined(GEKKO) mtx = LWP_MUTEX_NULL; LWP_MutexInit(&mtx, true); #else @@ -15,7 +17,9 @@ namespace Tesses::Framework::Threading } void Mutex::Lock() { - #if defined(GEKKO) + #if defined(_WIN32) + WaitForSingleObject(mtx, INFINITE); + #elif defined(GEKKO) LWP_MutexLock(mtx); #else mtx_lock(&mtx); @@ -23,7 +27,9 @@ namespace Tesses::Framework::Threading } void Mutex::Unlock() { - #if defined(GEKKO) + #if defined(_WIN32) + ReleaseMutex(mtx); + #elif defined(GEKKO) LWP_MutexUnlock(mtx); #else mtx_unlock(&mtx); @@ -31,7 +37,9 @@ namespace Tesses::Framework::Threading } bool Mutex::TryLock() { - #if defined(GEKKO) + #if defined(_WIN32) + return WaitForSingleObject(mtx, 100) == WAIT_OBJECT_0; + #elif defined(GEKKO) return LWP_MutexTryLock(mtx) == 0; #else return mtx_trylock(&mtx) == thrd_success; @@ -39,7 +47,9 @@ namespace Tesses::Framework::Threading } Mutex::~Mutex() { - #if defined(GEKKO) + #if defined(_WIN32) + CloseHandle(mtx); + #elif defined(GEKKO) LWP_MutexDestroy(mtx); #else mtx_destroy(&mtx); diff --git a/src/Threading/Thread.cpp b/src/Threading/Thread.cpp index 30f6438..f6cbf8d 100644 --- a/src/Threading/Thread.cpp +++ b/src/Threading/Thread.cpp @@ -2,6 +2,22 @@ #include namespace Tesses::Framework::Threading { + #if defined(_WIN32) + static DWORD __stdcall cb(LPVOID data) + { + auto thrd = static_cast(data); + + auto fn = thrd->fn; + thrd->hasInvoked=true; + fn(); + #if defined(GEKKO) + return NULL; + #else + return 0; + #endif + } + #else + #if defined(GEKKO) void* Thread::cb(void* data) #else @@ -19,12 +35,14 @@ namespace Tesses::Framework::Threading return 0; #endif } + #endif Thread::Thread(std::function fn) { this->hasInvoked=false; this->fn = fn; - - #if defined(GEKKO) + #if defined(_WIN32) + this->thrd = CreateThread(NULL,0,cb,static_cast(this), 0, &this->thrdId); + #elif defined(GEKKO) thrd = LWP_THREAD_NULL; LWP_CreateThread(&thrd, cb, static_cast(this), NULL, 12000, LWP_PRIO_HIGHEST); #else @@ -35,13 +53,19 @@ namespace Tesses::Framework::Threading void Thread::Detach() { #if !defined(GEKKO) + #if defined(_WIN32) + CloseHandle(thrd); + #else thrd_detach(thrd); + #endif #endif } void Thread::Join() { - #if defined(GEKKO) + #if defined(_WIN32) + WaitForSingleObject(this->thrd, INFINITE); + #elif defined(GEKKO) void* res; LWP_JoinThread(thrd,&res); #else