Subversion Repositories Games.Carmageddon

Rev

Rev 18 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1.  
  2. #include "harness/hooks.h"
  3. #include "harness/os.h"
  4. #include "harness/win95_polyfill.h"
  5.  
  6. #include <assert.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <time.h>
  10.  
  11. #if defined(_WIN32) || defined(_WIN64)
  12.  
  13. #include <direct.h>
  14. #include <windows.h>
  15. // https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/getcwd-wgetcwd?view=msvc-170
  16. #define getcwd _getcwd
  17. #define chdir _chdir
  18. #else
  19. #include <dirent.h>
  20. #include <libgen.h>
  21. #include <unistd.h>
  22. #endif
  23.  
  24. // All functions have a "_" suffix to avoid collisions with <windows.h>-defined types
  25.  
  26. uint32_t GetFileAttributesA_(char* lpFileName) {
  27.  
  28.     FILE* f = fopen(lpFileName, "r");
  29.     if (!f) {
  30.         return INVALID_FILE_ATTRIBUTES;
  31.     }
  32.     return FILE_ATTRIBUTE_NORMAL;
  33. }
  34.  
  35. int SetFileAttributesA_(char* lpFileName, uint32_t dwFileAttributes) {
  36.     // no-op for now
  37.     return 0;
  38. }
  39.  
  40. void* CreateFileA_(char* lpFileName, uint32_t dwDesiredAccess, uint32_t dwShareMode, void* lpSecurityAttributes, uint32_t dwCreationDisposition, uint32_t dwFlagsAndAttributes, void* hTemplateFile) {
  41.  
  42.     assert(dwDesiredAccess == GENERIC_READ);
  43.     assert(lpSecurityAttributes == NULL);
  44.     assert(dwCreationDisposition == OPEN_EXISTING);
  45.  
  46.     FILE* f = fopen(lpFileName, "r");
  47.     if (!f) {
  48.         return INVALID_HANDLE_VALUE;
  49.     }
  50.  
  51.     return f;
  52. }
  53.  
  54. uint32_t GetFileSize_(void* hFile, uint32_t* lpFileSizeHigh) {
  55.     assert(lpFileSizeHigh == NULL);
  56.  
  57.     long current_offset = ftell(hFile);
  58.     fseek(hFile, 0, SEEK_END);
  59.     long size = ftell(hFile);
  60.     fseek(hFile, current_offset, SEEK_SET);
  61.     return size;
  62. }
  63.  
  64. int CloseHandle_(void* hObject) {
  65.     fclose(hObject);
  66.     return 0;
  67. }
  68.  
  69. void GlobalMemoryStatus_(MEMORYSTATUS_* lpBuffer) {
  70.     lpBuffer->dwTotalPhys = 64000000;
  71.     lpBuffer->dwAvailPhys = 64000000; // pretend we have a whole 64mb of ram!
  72.     lpBuffer->dwAvailPageFile = 0;
  73.     lpBuffer->dwTotalVirtual = 0;
  74.     lpBuffer->dwAvailVirtual = 0;
  75.     lpBuffer->dwMemoryLoad = 0;
  76.     lpBuffer->dwTotalPageFile = 0;
  77. }
  78.  
  79. int GetCursorPos_(POINT_* lpPoint) {
  80.     int x, y;
  81.     gHarness_platform.GetMousePosition(&x, &y);
  82.     lpPoint->x = x;
  83.     lpPoint->y = y;
  84.     return 0;
  85. }
  86.  
  87. int ScreenToClient_(void* hWnd, POINT_* lpPoint) {
  88.     // no-op, we assume the point is already relative to client
  89.     return 0;
  90. }
  91.  
  92. uint32_t timeGetTime_(void) {
  93.     return gHarness_platform.GetTicks();
  94. }
  95.  
  96. uint32_t GetCurrentDirectoryA_(uint32_t nBufferLength, char* lpBuffer) {
  97.     char* ret = getcwd(lpBuffer, nBufferLength);
  98.     if (ret == NULL) {
  99.         return 0;
  100.     }
  101.     return strlen(lpBuffer);
  102. }
  103.  
  104. int SetCurrentDirectoryA_(char* lpPathName) {
  105.     int ret = chdir(lpPathName);
  106.     // chdir returning zero means success, SetCurrentDirectory returning non-zero means success
  107.     if (ret == 0) {
  108.         return 1;
  109.     } else {
  110.         return 0;
  111.     }
  112. }
  113.  
  114. HANDLE_ FindFirstFileA_(char* lpFileName, WIN32_FIND_DATAA_* lpFindFileData) {
  115.     assert(strcmp(lpFileName, "*.???") == 0);
  116.  
  117. #if defined(_WIN32) || defined(_WIN64)
  118.     WIN32_FIND_DATAA fd; // Pierre-Marie Baty -- explicitly use WIN32_FIND_DATAA instead of WIN32_FIND_DATA
  119.     void* hFile = FindFirstFileA(lpFileName, &fd); // Pierre-Marie Baty -- explicitly use FindFirstFileA() instead of FindFirstFile()
  120.     if (hFile != INVALID_HANDLE_VALUE) {
  121.         strcpy(lpFindFileData->cFileName, fd.cFileName);
  122.     }
  123.     return hFile;
  124. #else
  125.     DIR* dir;
  126.     strcpy(lpFileName, ".");
  127.     dir = opendir(lpFileName);
  128.     if (dir == NULL) {
  129.         return INVALID_HANDLE_VALUE;
  130.     }
  131.     if (FindNextFileA_(dir, lpFindFileData)) {
  132.         return dir;
  133.     } else {
  134.         closedir(dir);
  135.         return INVALID_HANDLE_VALUE;
  136.     }
  137. #endif
  138. }
  139.  
  140. int FindNextFileA_(HANDLE_ hFindFile, WIN32_FIND_DATAA_* lpFindFileData) {
  141. #if defined(_WIN32) || defined(_WIN64)
  142.     WIN32_FIND_DATAA fd; // Pierre-Marie Baty -- explicitly use WIN32_FIND_DATAA instead of WIN32_FIND_DATA
  143.     int result = FindNextFileA(hFindFile, &fd); // Pierre-Marie Baty -- explicitly use FindNextFileA() instead of FindNextFile()
  144.     if (result) {
  145.         strcpy(lpFindFileData->cFileName, fd.cFileName);
  146.     }
  147.     return result;
  148. #else
  149.     struct dirent* entry;
  150.  
  151.     if (hFindFile == NULL) {
  152.         return 0;
  153.     }
  154.     while ((entry = readdir(hFindFile)) != NULL) {
  155.         if (entry->d_type == DT_REG) {
  156.             strcpy(lpFindFileData->cFileName, entry->d_name);
  157.             return 1;
  158.         }
  159.     }
  160.     return 0;
  161. #endif
  162. }
  163.  
  164. int FindClose_(HANDLE_ hFindFile) {
  165. #if defined(_WIN32) || defined(_WIN64)
  166.     return FindClose(hFindFile);
  167. #else
  168.     return closedir(hFindFile);
  169.  
  170. #endif
  171. }
  172.  
  173. void* CreateWindowExA_(uint32_t dwExStyle, char* lpClassName, char* lpWindowName, uint32_t dwStyle, int X, int Y, int nWidth, int nHeight, void* hWndParent, void* hMenu, void* hInstance, void* lpParam) {
  174.     return gHarness_platform.CreateWindowAndRenderer(lpWindowName, X, Y, nWidth, nHeight);
  175. }
  176.  
  177. int SetWindowPos_(void* hWnd, void* hWndInsertAfter, int X, int Y, int cx, int cy, unsigned int uFlags) {
  178.     return gHarness_platform.SetWindowPos(hWnd, X, Y, cx, cy);
  179. }
  180.  
  181. int ShowCursor_(int bShow) {
  182.     gHarness_platform.ShowCursor(bShow);
  183.     return 0;
  184. }
  185.  
  186. int SendMessageA_(void* hWnd, unsigned int Msg, unsigned int wParam, long lParam) {
  187.     return 0;
  188. }
  189.  
  190. int MessageBoxA_(void* hWnd, char* lpText, char* lpCaption, unsigned int uType) {
  191.     // only ever used for errors
  192.     assert(uType == MB_ICONERROR);
  193.     return gHarness_platform.ShowErrorMessage(hWnd, lpText, lpCaption);
  194. }
  195.  
  196. int DestroyWindow_(void* hWnd) {
  197.     gHarness_platform.DestroyWindow(hWnd);
  198.     return 0;
  199. }
  200.  
  201. void ExitProcess_(unsigned int uExitCode) {
  202.     exit(uExitCode);
  203. }
  204.  
  205. void TranslateMessage_(MSG_* lpMsg) {
  206.     // no-op
  207. }
  208.  
  209. void DispatchMessageA_(MSG_* lpMsg) {
  210.     // no-op
  211. }
  212.  
  213. int PeekMessageA_(MSG_* lpMsg, void* hWnd, unsigned int wMsgFilterMin, unsigned int wMsgFilterMax, unsigned int wRemoveMsg) {
  214.     return gHarness_platform.ProcessWindowMessages(lpMsg);
  215. }
  216.  
  217. int GetMessageA_(MSG_* lpMsg, void* hWnd, unsigned int wMsgFilterMin, unsigned int wMsgFilterMax) {
  218.     return gHarness_platform.ProcessWindowMessages(lpMsg);
  219. }
  220.  
  221. void Sleep_(uint32_t dwMilliseconds) {
  222.     gHarness_platform.Sleep(dwMilliseconds);
  223. }
  224.  
  225. void DirectDraw_CreateSurface(int width, int height) {
  226.     // no-op
  227. }
  228.  
  229. void DirectInputDevice_GetDeviceState(unsigned int count, uint8_t* buffer) {
  230.     gHarness_platform.GetKeyboardState(count, buffer);
  231. }
  232.  
  233. void DirectDrawDevice_SetPaletteEntries(PALETTEENTRY_* palette, int pFirst_colour, int pCount) {
  234.     assert(pFirst_colour == 0);
  235.     assert(pCount == 256);
  236.     gHarness_platform.Renderer_SetPalette(palette);
  237.     Harness_RenderLastScreen();
  238. }
  239.  
  240. void _splitpath_(char* path, char* drive, char* dir, char* fname, char* ext) {
  241.     assert(dir == NULL);
  242.     assert(fname != NULL);
  243.  
  244.     char* result = OS_Basename(path);
  245.     strcpy(fname, result);
  246. }
  247.  
  248. int _CrtDbgReport_(int reportType, const char* filename, int linenumber, const char* moduleName, const char* format, ...) {
  249.     printf("_CrtDbgReport: (TODO)\n");
  250.     return 1;
  251. }
  252.