Got it working on Windows using Mingw

This commit is contained in:
Mike Nolan 2024-12-29 21:44:33 -06:00
parent 5b89d8c5de
commit f1bae988c4
15 changed files with 139 additions and 33 deletions

View File

@ -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()

View File

@ -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);

View File

@ -1,5 +1,7 @@
#pragma once
#include "VFS.hpp"
#include "VFSFix.hpp"
namespace Tesses::Framework::Filesystem
{
class MountableDirectory {

View File

@ -1,5 +1,7 @@
#pragma once
#include "VFS.hpp"
#include "VFSFix.hpp"
namespace Tesses::Framework::Filesystem
{
class NullFilesystem : public VFS

View File

@ -1,5 +1,7 @@
#pragma once
#include "VFS.hpp"
#include "VFSFix.hpp"
namespace Tesses::Framework::Filesystem
{
class SubdirFilesystem : public VFS

View File

@ -3,6 +3,8 @@
#include "../Streams/Stream.hpp"
#include <functional>
#include <memory>
#include "VFSFix.hpp"
namespace Tesses::Framework::Filesystem
{
class VFSPath {

View File

@ -1,5 +1,7 @@
#pragma once
#if defined(GEKKO)
#if defined(_WIN32)
#include <windows.h>
#elif defined(GEKKO)
#include <ogc/mutex.h>
#else
#include <threads.h>
@ -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;

View File

@ -1,6 +1,8 @@
#pragma once
#include <functional>
#if defined(GEKKO)
#if defined(_WIN32)
#include <windows.h>
#elif defined(GEKKO)
#include <ogc/lwp.h>
#else
#include <threads.h>
@ -10,8 +12,13 @@ namespace Tesses::Framework::Threading
{
class Thread
{
std::atomic<bool> 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<void()> fn;
std::atomic<bool> hasInvoked;
public:
Thread(std::function<void()> fn);
void Join();

View File

@ -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;

View File

@ -15,8 +15,8 @@ namespace Tesses::Framework::Http
}
FileServer::FileServer(std::filesystem::path path,bool allowListing, bool spa, std::vector<std::string> 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;

View File

@ -1,5 +1,7 @@
#include "TessesFramework/Streams/FileStream.hpp"
#if defined(_WIN32)
#include <windows.h>
#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()
{

View File

@ -14,7 +14,12 @@ using HttpUtils = Tesses::Framework::Http::HttpUtils;
#else
#if defined(_WIN32)
#include <ws2tcpip.h>
#include <winsock2.h>
#include <windows.h>
#pragma comment(lib, "ws2_32.lib")
#else
extern "C" {
#include <netinet/in.h>
#include <sys/socket.h>
@ -23,10 +28,11 @@ extern "C" {
#include <fcntl.h>
#include <unistd.h>
}
#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 <ifaddrs.h>
#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<std::string,std::string>("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;
}

View File

@ -2,7 +2,10 @@
#include "TessesFramework/Streams/NetworkStream.hpp"
#include <atomic>
#include <csignal>
#if defined(GEKKO)
#if defined(_WIN32)
#include <windows.h>
#include <cstdio>
#elif defined(GEKKO)
#include <stdio.h>
#include <stdlib.h>
#include <gccore.h>
@ -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();

View File

@ -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);

View File

@ -2,6 +2,22 @@
#include <iostream>
namespace Tesses::Framework::Threading
{
#if defined(_WIN32)
static DWORD __stdcall cb(LPVOID data)
{
auto thrd = static_cast<Thread*>(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<void()> fn)
{
this->hasInvoked=false;
this->fn = fn;
#if defined(GEKKO)
#if defined(_WIN32)
this->thrd = CreateThread(NULL,0,cb,static_cast<LPVOID>(this), 0, &this->thrdId);
#elif defined(GEKKO)
thrd = LWP_THREAD_NULL;
LWP_CreateThread(&thrd, cb, static_cast<void*>(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