// ----------------------------------------------------------------------- // // // MODULE : windows_compat.c // // PURPOSE : Implementation of the Win32 surface declared in compat/windows.h. // // NOTE : Functions that genuinely cannot work yet call // LTCOMPAT_UNIMPLEMENTED() and RETURN A DEFINED VALUE. They are never // silent. Anything here that returns a plausible-looking value // without doing the work is a liability, so it says so out loud the // first time it is reached. // // ----------------------------------------------------------------------- // #include "windows.h" #include #include #include #include #include // ----------------------------------------------------------------------- // // Stub reporting // ----------------------------------------------------------------------- // void LTCompat_ReportUnimplemented(const char* fn, const char* file, int line) { fprintf(stderr, "[LTCOMPAT] UNIMPLEMENTED: %s() (%s:%d) -- first call only\n", fn ? fn : "?", file ? file : "?", line); fflush(stderr); } // ----------------------------------------------------------------------- // // Last error // ----------------------------------------------------------------------- // static DWORD g_LastError = 0; DWORD GetLastError(void) { return g_LastError; } void SetLastError(DWORD e) { g_LastError = e; } // ----------------------------------------------------------------------- // // String helpers // ----------------------------------------------------------------------- // #undef strupr #undef strlwr char* _strupr(char* s) { if (!s) return s; for (char* p = s; *p; ++p) *p = (char)toupper((unsigned char)*p); return s; } char* _strlwr(char* s) { if (!s) return s; for (char* p = s; *p; ++p) *p = (char)tolower((unsigned char)*p); return s; } int wsprintf(char* buffer, const char* format, ...) { va_list args; va_start(args, format); // Win32's wsprintf caps at 1024 bytes including the terminator. Matching // that bound is deliberate: code written against it may rely on the clamp. int n = vsnprintf(buffer, 1024, format, args); va_end(args); return n; } char* lstrcpy(char* dst, const char* src) { return strcpy(dst, src); } char* lstrcat(char* dst, const char* src) { return strcat(dst, src); } int lstrlen(const char* s) { return s ? (int)strlen(s) : 0; } int lstrcmp(const char* a, const char* b) { return strcmp(a, b); } int lstrcmpi(const char* a, const char* b){ return strcasecmp(a, b); } int _mkdir(const char* path) { return mkdir(path, 0755); } // Win32 path splitting. NOLF 1 uses this on asset paths, which are authored // with backslashes, so BOTH separators must be recognised. void _splitpath(const char* path, char* drive, char* dir, char* fname, char* ext) { if (drive) drive[0] = '\0'; if (dir) dir[0] = '\0'; if (fname) fname[0] = '\0'; if (ext) ext[0] = '\0'; if (!path) return; const char* p = path; // Drive letter, e.g. "C:" if (p[0] && p[1] == ':') { if (drive) { drive[0] = p[0]; drive[1] = ':'; drive[2] = '\0'; } p += 2; } // Last separator -- either flavour. const char* lastSlash = NULL; for (const char* q = p; *q; ++q) if (*q == '/' || *q == '\\') lastSlash = q; const char* nameStart = p; if (lastSlash) { size_t n = (size_t)(lastSlash - p) + 1; if (dir) { memcpy(dir, p, n); dir[n] = '\0'; } nameStart = lastSlash + 1; } const char* dot = strrchr(nameStart, '.'); if (dot) { if (fname) { size_t n = (size_t)(dot - nameStart); memcpy(fname, nameStart, n); fname[n] = '\0'; } if (ext) strcpy(ext, dot); } else { if (fname) strcpy(fname, nameStart); } } void _makepath(char* path, const char* drive, const char* dir, const char* fname, const char* ext) { if (!path) return; path[0] = '\0'; if (drive && drive[0]) strcat(path, drive); if (dir && dir[0]) strcat(path, dir); if (fname && fname[0]) strcat(path, fname); if (ext && ext[0]) strcat(path, ext); } // ----------------------------------------------------------------------- // // Time // ----------------------------------------------------------------------- // // ⚠️ A SECOND CLOCK, AND IT HAD TO BE PUT UNDER THE ENGINE'S CONTROL. // // The port replaced the engine clock with a FIXED TIMESTEP so runs are // reproducible (engine/kernel/time/). GetTickCount is a *different* clock, // and NOLF drives real animation from it: CWinUtil::GetTime() is // `GetTickCount() / 1000.0f` (WinUtil.cpp:128), and CBaseScaleFX uses that // for every interface effect's start time and phase (BaseScaleFX.cpp:230, // :261). // // So while this returned wall time, the main menu's rotating flower and // panels sat at a DIFFERENT ANGLE on every run: six runs in fresh // directories produced three distinct frames, differing by up to 38% of // pixels. The rasteriser and the scene were entirely deterministic; the // clock underneath them was not. // // On Win32 both clocks advanced together in real time, so having them // disagree is the unfaithful state -- this restores the pairing. // LT_REALTIME=1 leaves the hook unset and the real clock is used, exactly as // documented. static DWORD (*g_pfnTickSource)(void) = 0; void LTCompat_SetTickSource(DWORD (*pfn)(void)) { g_pfnTickSource = pfn; } DWORD GetTickCount(void) { struct timeval tv; if (g_pfnTickSource) return g_pfnTickSource(); gettimeofday(&tv, NULL); // Wraps at 2^32 ms just as Win32 does. Code comparing tick deltas with // unsigned arithmetic stays correct across the wrap; code that casts the // difference to a SIGNED wider type does not (this exact mistake kicked // every client off a NOLF 2 server after ~30 seconds). return (DWORD)((uint64_t)tv.tv_sec * 1000u + (uint64_t)tv.tv_usec / 1000u); } void Sleep(DWORD ms) { usleep((useconds_t)ms * 1000); } // ----------------------------------------------------------------------- // // Debug output // ----------------------------------------------------------------------- // #undef OutputDebugString void OutputDebugStringA(const char* s) { if (s) { fputs(s, stderr); fflush(stderr); } } // ----------------------------------------------------------------------- // // File I/O -- POSIX fd carried inside the HANDLE // ----------------------------------------------------------------------- // #undef CreateFile #undef CreateDirectory #undef GetFileAttributes #undef DeleteFile static HANDLE fd_to_handle(int fd) { return (fd < 0) ? INVALID_HANDLE_VALUE : (HANDLE)(intptr_t)(fd + 1); } static int handle_to_fd(HANDLE h) { return (h == INVALID_HANDLE_VALUE || h == NULL) ? -1 : (int)(intptr_t)h - 1; } HANDLE CreateFileA(const char* name, DWORD access, DWORD share, void* security, DWORD creation, DWORD flags, HANDLE tmpl) { (void)share; (void)security; (void)flags; (void)tmpl; int oflag = 0; if ((access & GENERIC_READ) && (access & GENERIC_WRITE)) oflag = O_RDWR; else if (access & GENERIC_WRITE) oflag = O_WRONLY; else oflag = O_RDONLY; switch (creation) { case CREATE_NEW: oflag |= O_CREAT | O_EXCL; break; case CREATE_ALWAYS: oflag |= O_CREAT | O_TRUNC; break; case OPEN_ALWAYS: oflag |= O_CREAT; break; case TRUNCATE_EXISTING: oflag |= O_TRUNC; break; case OPEN_EXISTING: default: break; } int fd = open(name, oflag, 0644); if (fd < 0) { SetLastError((DWORD)errno); return INVALID_HANDLE_VALUE; } return fd_to_handle(fd); } // NB: the out-params are NOT named `read`/`write` -- those would shadow the // POSIX calls this function is built on. BOOL ReadFile(HANDLE h, LPVOID buf, DWORD toRead, DWORD* bytesRead, void* ov) { (void)ov; int fd = handle_to_fd(h); if (fd < 0) return FALSE; ssize_t n = read(fd, buf, toRead); if (n < 0) { SetLastError((DWORD)errno); if (bytesRead) *bytesRead = 0; return FALSE; } if (bytesRead) *bytesRead = (DWORD)n; return TRUE; } BOOL WriteFile(HANDLE h, LPCVOID buf, DWORD toWrite, DWORD* bytesWritten, void* ov) { (void)ov; int fd = handle_to_fd(h); if (fd < 0) return FALSE; ssize_t n = write(fd, buf, toWrite); if (n < 0) { SetLastError((DWORD)errno); if (bytesWritten) *bytesWritten = 0; return FALSE; } if (bytesWritten) *bytesWritten = (DWORD)n; return TRUE; } BOOL CloseHandle(HANDLE h) { int fd = handle_to_fd(h); if (fd < 0) return FALSE; return close(fd) == 0 ? TRUE : FALSE; } DWORD SetFilePointer(HANDLE h, LONG dist, LONG* distHigh, DWORD method) { (void)distHigh; int fd = handle_to_fd(h); if (fd < 0) return (DWORD)-1; int whence = (method == FILE_CURRENT) ? SEEK_CUR : (method == FILE_END) ? SEEK_END : SEEK_SET; off_t r = lseek(fd, dist, whence); return (r < 0) ? (DWORD)-1 : (DWORD)r; } DWORD GetFileSize(HANDLE h, DWORD* sizeHigh) { if (sizeHigh) *sizeHigh = 0; int fd = handle_to_fd(h); if (fd < 0) return (DWORD)-1; struct stat st; if (fstat(fd, &st) != 0) return (DWORD)-1; return (DWORD)st.st_size; } BOOL CreateDirectoryA(const char* path, void* security) { (void)security; return mkdir(path, 0755) == 0 ? TRUE : FALSE; } DWORD GetFileAttributesA(const char* path) { struct stat st; if (stat(path, &st) != 0) return INVALID_FILE_ATTRIBUTES; return S_ISDIR(st.st_mode) ? FILE_ATTRIBUTE_DIRECTORY : FILE_ATTRIBUTE_NORMAL; } BOOL DeleteFileA(const char* path) { return unlink(path) == 0 ? TRUE : FALSE; } // ----------------------------------------------------------------------- // // INI files -- the Win32 private-profile API. // // ⚠️ NOLF'S SAVE-GAME LIST LIVES HERE, and with both calls stubbed no save // could ever be loaded: CGameClientShell::SaveGame records each slot as // `[No One Lives Forever] SaveGame00=|