mirror of
https://github.com/Kelsidavis/WoWee.git
synced 2026-03-22 23:30:14 +00:00
Add Windows cross-platform support alongside Linux
Replace POSIX-specific socket and process APIs with portable abstractions so the project builds on both Windows and Linux. - Add include/network/net_platform.hpp: Winsock2/POSIX socket abstraction (socket types, non-blocking, error handling, WSAStartup lifecycle) - Add include/platform/process.hpp: CreateProcess/fork+exec abstraction for spawning ffplay subprocesses - Update network module (tcp_socket, world_socket) to use portable socket helpers instead of raw POSIX calls - Update audio module (music_manager, footstep_manager, activity_sound_manager) to use portable process helpers instead of fork/exec/kill/waitpid - Replace hardcoded /tmp/ paths with std::filesystem::temp_directory_path() - Link ws2_32 and SDL2main on Windows in CMakeLists.txt
This commit is contained in:
parent
dd126c6e4b
commit
6bf3fa4ed4
14 changed files with 416 additions and 186 deletions
|
|
@ -1,6 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
#include "audio/footstep_manager.hpp"
|
||||
#include "platform/process.hpp"
|
||||
#include <array>
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
|
|
@ -51,10 +52,10 @@ private:
|
|||
|
||||
bool swimmingActive = false;
|
||||
bool swimMoving = false;
|
||||
pid_t swimLoopPid = -1;
|
||||
pid_t oneShotPid = -1;
|
||||
std::string loopTempPath = "/tmp/wowee_swim_loop.wav";
|
||||
std::string oneShotTempPath = "/tmp/wowee_activity.wav";
|
||||
ProcessHandle swimLoopPid = INVALID_PROCESS;
|
||||
ProcessHandle oneShotPid = INVALID_PROCESS;
|
||||
std::string loopTempPath = platform::getTempFilePath("wowee_swim_loop.wav");
|
||||
std::string oneShotTempPath = platform::getTempFilePath("wowee_activity.wav");
|
||||
std::mt19937 rng;
|
||||
|
||||
std::chrono::steady_clock::time_point lastJumpAt{};
|
||||
|
|
|
|||
|
|
@ -1,10 +1,10 @@
|
|||
#pragma once
|
||||
|
||||
#include "platform/process.hpp"
|
||||
#include <cstdint>
|
||||
#include <random>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <sys/types.h>
|
||||
#include <chrono>
|
||||
|
||||
namespace wowee {
|
||||
|
|
@ -56,8 +56,8 @@ private:
|
|||
SurfaceSamples surfaces[7];
|
||||
size_t sampleCount = 0;
|
||||
|
||||
std::string tempFilePath = "/tmp/wowee_footstep.wav";
|
||||
pid_t playerPid = -1;
|
||||
std::string tempFilePath = platform::getTempFilePath("wowee_footstep.wav");
|
||||
ProcessHandle playerPid = INVALID_PROCESS;
|
||||
std::chrono::steady_clock::time_point lastPlayTime = std::chrono::steady_clock::time_point{};
|
||||
|
||||
std::mt19937 rng;
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
#pragma once
|
||||
|
||||
#include "platform/process.hpp"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
|
|
@ -31,7 +32,7 @@ private:
|
|||
pipeline::AssetManager* assetManager = nullptr;
|
||||
std::string currentTrack;
|
||||
std::string tempFilePath;
|
||||
pid_t playerPid = -1;
|
||||
ProcessHandle playerPid = INVALID_PROCESS;
|
||||
bool playing = false;
|
||||
|
||||
// Crossfade state
|
||||
|
|
|
|||
122
include/network/net_platform.hpp
Normal file
122
include/network/net_platform.hpp
Normal file
|
|
@ -0,0 +1,122 @@
|
|||
#pragma once
|
||||
|
||||
// Cross-platform socket abstractions for Windows (Winsock2) and POSIX.
|
||||
|
||||
#ifdef _WIN32
|
||||
#ifndef WIN32_LEAN_AND_MEAN
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#endif
|
||||
#include <winsock2.h>
|
||||
#include <ws2tcpip.h>
|
||||
#pragma comment(lib, "ws2_32.lib")
|
||||
|
||||
using socket_t = SOCKET;
|
||||
using ssize_t = int; // recv/send return int on Windows
|
||||
|
||||
inline constexpr socket_t INVALID_SOCK = INVALID_SOCKET;
|
||||
|
||||
#else
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <netdb.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <cerrno>
|
||||
|
||||
using socket_t = int;
|
||||
|
||||
inline constexpr socket_t INVALID_SOCK = -1;
|
||||
|
||||
#endif
|
||||
|
||||
#include <cstring>
|
||||
|
||||
namespace wowee {
|
||||
namespace net {
|
||||
|
||||
// ---- Winsock lifecycle (no-op on Linux) ----
|
||||
|
||||
#ifdef _WIN32
|
||||
struct WinsockInit {
|
||||
WinsockInit() {
|
||||
WSADATA wsa;
|
||||
WSAStartup(MAKEWORD(2, 2), &wsa);
|
||||
}
|
||||
~WinsockInit() { WSACleanup(); }
|
||||
};
|
||||
// Call once at program start (e.g. as a static in Application).
|
||||
inline void ensureInit() {
|
||||
static WinsockInit instance;
|
||||
}
|
||||
#else
|
||||
inline void ensureInit() {}
|
||||
#endif
|
||||
|
||||
// ---- Portable helpers ----
|
||||
|
||||
inline void closeSocket(socket_t s) {
|
||||
#ifdef _WIN32
|
||||
closesocket(s);
|
||||
#else
|
||||
close(s);
|
||||
#endif
|
||||
}
|
||||
|
||||
inline bool setNonBlocking(socket_t s) {
|
||||
#ifdef _WIN32
|
||||
u_long mode = 1;
|
||||
return ioctlsocket(s, FIONBIO, &mode) == 0;
|
||||
#else
|
||||
int flags = fcntl(s, F_GETFL, 0);
|
||||
return fcntl(s, F_SETFL, flags | O_NONBLOCK) != -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
inline int lastError() {
|
||||
#ifdef _WIN32
|
||||
return WSAGetLastError();
|
||||
#else
|
||||
return errno;
|
||||
#endif
|
||||
}
|
||||
|
||||
inline bool isWouldBlock(int err) {
|
||||
#ifdef _WIN32
|
||||
return err == WSAEWOULDBLOCK;
|
||||
#else
|
||||
return err == EAGAIN || err == EWOULDBLOCK;
|
||||
#endif
|
||||
}
|
||||
|
||||
inline bool isInProgress(int err) {
|
||||
#ifdef _WIN32
|
||||
return err == WSAEWOULDBLOCK || err == WSAEALREADY;
|
||||
#else
|
||||
return err == EINPROGRESS;
|
||||
#endif
|
||||
}
|
||||
|
||||
inline const char* errorString(int err) {
|
||||
#ifdef _WIN32
|
||||
// Simple thread-local buffer for FormatMessage
|
||||
thread_local char buf[256];
|
||||
FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
|
||||
nullptr, err, 0, buf, sizeof(buf), nullptr);
|
||||
return buf;
|
||||
#else
|
||||
return strerror(err);
|
||||
#endif
|
||||
}
|
||||
|
||||
// Portable send — Windows recv/send take char*, not void*.
|
||||
inline ssize_t portableSend(socket_t s, const uint8_t* data, size_t len) {
|
||||
return ::send(s, reinterpret_cast<const char*>(data), static_cast<int>(len), 0);
|
||||
}
|
||||
|
||||
inline ssize_t portableRecv(socket_t s, uint8_t* buf, size_t len) {
|
||||
return ::recv(s, reinterpret_cast<char*>(buf), static_cast<int>(len), 0);
|
||||
}
|
||||
|
||||
} // namespace net
|
||||
} // namespace wowee
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
#include "network/socket.hpp"
|
||||
#include <sys/socket.h>
|
||||
#include "network/net_platform.hpp"
|
||||
|
||||
namespace wowee {
|
||||
namespace network {
|
||||
|
|
@ -22,7 +22,7 @@ private:
|
|||
void tryParsePackets();
|
||||
size_t getExpectedPacketSize(uint8_t opcode);
|
||||
|
||||
int sockfd = -1;
|
||||
socket_t sockfd = INVALID_SOCK;
|
||||
bool connected = false;
|
||||
std::vector<uint8_t> receiveBuffer;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
#include "network/socket.hpp"
|
||||
#include "network/packet.hpp"
|
||||
#include "network/net_platform.hpp"
|
||||
#include "auth/rc4.hpp"
|
||||
#include <functional>
|
||||
#include <vector>
|
||||
|
|
@ -73,7 +74,7 @@ private:
|
|||
*/
|
||||
void tryParsePackets();
|
||||
|
||||
int sockfd = -1;
|
||||
socket_t sockfd = INVALID_SOCK;
|
||||
bool connected = false;
|
||||
bool encryptionEnabled = false;
|
||||
|
||||
|
|
|
|||
157
include/platform/process.hpp
Normal file
157
include/platform/process.hpp
Normal file
|
|
@ -0,0 +1,157 @@
|
|||
#pragma once
|
||||
|
||||
// Cross-platform subprocess helpers for spawning ffplay (audio playback).
|
||||
// Linux: fork/exec/kill/waitpid. Windows: CreateProcess/TerminateProcess.
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#ifdef _WIN32
|
||||
#ifndef WIN32_LEAN_AND_MEAN
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#endif
|
||||
#include <windows.h>
|
||||
|
||||
using ProcessHandle = HANDLE;
|
||||
inline constexpr ProcessHandle INVALID_PROCESS = INVALID_HANDLE_VALUE;
|
||||
|
||||
#else
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
#include <csignal>
|
||||
|
||||
using ProcessHandle = pid_t;
|
||||
inline constexpr ProcessHandle INVALID_PROCESS = -1;
|
||||
|
||||
#endif
|
||||
|
||||
#include <filesystem>
|
||||
|
||||
namespace wowee {
|
||||
namespace platform {
|
||||
|
||||
// Return a platform-appropriate temp file path for the given filename.
|
||||
inline std::string getTempFilePath(const std::string& filename) {
|
||||
auto tmp = std::filesystem::temp_directory_path() / filename;
|
||||
return tmp.string();
|
||||
}
|
||||
|
||||
// Spawn ffplay with the given arguments. Returns process handle.
|
||||
// args should be the full argument list (e.g. {"-nodisp", "-autoexit", ...}).
|
||||
// The executable "ffplay" is resolved from PATH.
|
||||
inline ProcessHandle spawnProcess(const std::vector<std::string>& args) {
|
||||
#ifdef _WIN32
|
||||
// Build command line string
|
||||
std::string cmdline = "ffplay";
|
||||
for (const auto& arg : args) {
|
||||
cmdline += " ";
|
||||
// Quote arguments that contain spaces
|
||||
if (arg.find(' ') != std::string::npos) {
|
||||
cmdline += "\"" + arg + "\"";
|
||||
} else {
|
||||
cmdline += arg;
|
||||
}
|
||||
}
|
||||
|
||||
STARTUPINFOA si{};
|
||||
si.cb = sizeof(si);
|
||||
// Hide the subprocess window and suppress stdout/stderr
|
||||
si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
|
||||
si.wShowWindow = SW_HIDE;
|
||||
si.hStdInput = INVALID_HANDLE_VALUE;
|
||||
si.hStdOutput = INVALID_HANDLE_VALUE;
|
||||
si.hStdError = INVALID_HANDLE_VALUE;
|
||||
|
||||
PROCESS_INFORMATION pi{};
|
||||
|
||||
// CreateProcessA needs a mutable char buffer for lpCommandLine
|
||||
std::vector<char> cmdBuf(cmdline.begin(), cmdline.end());
|
||||
cmdBuf.push_back('\0');
|
||||
|
||||
BOOL ok = CreateProcessA(
|
||||
nullptr, // lpApplicationName — resolve from PATH
|
||||
cmdBuf.data(), // lpCommandLine
|
||||
nullptr, nullptr, // process/thread security
|
||||
FALSE, // inherit handles
|
||||
CREATE_NO_WINDOW, // creation flags
|
||||
nullptr, nullptr, // environment, working dir
|
||||
&si, &pi
|
||||
);
|
||||
|
||||
if (!ok) {
|
||||
return INVALID_PROCESS;
|
||||
}
|
||||
|
||||
// We don't need the thread handle
|
||||
CloseHandle(pi.hThread);
|
||||
return pi.hProcess;
|
||||
|
||||
#else
|
||||
pid_t pid = fork();
|
||||
if (pid == 0) {
|
||||
// Child process
|
||||
setpgid(0, 0);
|
||||
freopen("/dev/null", "w", stdout);
|
||||
freopen("/dev/null", "w", stderr);
|
||||
|
||||
// Build argv for exec
|
||||
std::vector<const char*> argv;
|
||||
argv.push_back("ffplay");
|
||||
for (const auto& arg : args) {
|
||||
argv.push_back(arg.c_str());
|
||||
}
|
||||
argv.push_back(nullptr);
|
||||
|
||||
execvp("ffplay", const_cast<char* const*>(argv.data()));
|
||||
_exit(1); // exec failed
|
||||
}
|
||||
return (pid > 0) ? pid : INVALID_PROCESS;
|
||||
#endif
|
||||
}
|
||||
|
||||
// Kill a subprocess (and its children on Linux).
|
||||
inline void killProcess(ProcessHandle& handle) {
|
||||
if (handle == INVALID_PROCESS) return;
|
||||
|
||||
#ifdef _WIN32
|
||||
TerminateProcess(handle, 0);
|
||||
WaitForSingleObject(handle, 2000);
|
||||
CloseHandle(handle);
|
||||
#else
|
||||
kill(-handle, SIGTERM); // kill process group
|
||||
kill(handle, SIGTERM);
|
||||
int status = 0;
|
||||
waitpid(handle, &status, 0);
|
||||
#endif
|
||||
|
||||
handle = INVALID_PROCESS;
|
||||
}
|
||||
|
||||
// Check if a process has exited. If so, clean up and set handle to INVALID_PROCESS.
|
||||
// Returns true if the process is still running.
|
||||
inline bool isProcessRunning(ProcessHandle& handle) {
|
||||
if (handle == INVALID_PROCESS) return false;
|
||||
|
||||
#ifdef _WIN32
|
||||
DWORD result = WaitForSingleObject(handle, 0);
|
||||
if (result == WAIT_OBJECT_0) {
|
||||
// Process has exited
|
||||
CloseHandle(handle);
|
||||
handle = INVALID_PROCESS;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
#else
|
||||
int status = 0;
|
||||
pid_t result = waitpid(handle, &status, WNOHANG);
|
||||
if (result == handle) {
|
||||
handle = INVALID_PROCESS;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
#endif
|
||||
}
|
||||
|
||||
} // namespace platform
|
||||
} // namespace wowee
|
||||
Loading…
Add table
Add a link
Reference in a new issue