Avoid including Windows headers for WinINet backend

This commit is contained in:
UnknownShadow200 2021-07-24 10:09:30 +10:00
parent 04f9fd932a
commit bb9d8fb29f
4 changed files with 63 additions and 25 deletions

View File

@ -281,7 +281,7 @@ cc_result Audio_IsFinished(struct AudioContext* ctx, cc_bool* finished) {
/*########################################################################################################################*
*------------------------------------------------------WinMM backend------------------------------------------------------*
*#########################################################################################################################*/
/* == BEGIN WINDOWS TYPEDEFS == */
/* === BEGIN WINDOWS HEADERS === */
typedef unsigned long DWORD;
typedef unsigned short WORD;
typedef unsigned int UINT;
@ -291,12 +291,12 @@ typedef char* LPSTR;
#define WINAPI __stdcall
#define DECLSPEC_IMPORT __declspec(dllimport)
/* == BEGIN mmsyscom.h == */
/* === BEGIN mmsyscom.h === */
#define CALLBACK_NULL 0x00000000l
typedef UINT MMRESULT;
#define WINMMAPI DECLSPEC_IMPORT
/* == BEGIN mmeapi.h == */
/* === BEGIN mmeapi.h === */
typedef struct WAVEHDR_ {
LPSTR lpData;
DWORD dwBufferLength;
@ -324,13 +324,13 @@ typedef void* HWAVEOUT;
#define WHDR_DONE 0x00000001
#define WHDR_PREPARED 0x00000002
WINMMAPI MMRESULT WINAPI waveOutOpen(HWAVEOUT* phwo, UINT uDeviceID, const WAVEFORMATEX* pwfx, DWORD_PTR dwCallback, DWORD_PTR dwInstance, DWORD fdwOpen);
WINMMAPI MMRESULT WINAPI waveOutOpen(HWAVEOUT* phwo, UINT deviceID, const WAVEFORMATEX* fmt, DWORD_PTR callback, DWORD_PTR instance, DWORD flags);
WINMMAPI MMRESULT WINAPI waveOutClose(HWAVEOUT hwo);
WINMMAPI MMRESULT WINAPI waveOutPrepareHeader(HWAVEOUT hwo, WAVEHDR* pwh, UINT cbwh);
WINMMAPI MMRESULT WINAPI waveOutUnprepareHeader(HWAVEOUT hwo, WAVEHDR* pwh, UINT cbwh);
WINMMAPI MMRESULT WINAPI waveOutWrite(HWAVEOUT hwo, WAVEHDR* pwh, UINT cbwh);
WINMMAPI MMRESULT WINAPI waveOutPrepareHeader(HWAVEOUT hwo, WAVEHDR* hdr, UINT hdrSize);
WINMMAPI MMRESULT WINAPI waveOutUnprepareHeader(HWAVEOUT hwo, WAVEHDR* hdr, UINT hdrSize);
WINMMAPI MMRESULT WINAPI waveOutWrite(HWAVEOUT hwo, WAVEHDR* hdr, UINT hdrSize);
WINMMAPI MMRESULT WINAPI waveOutReset(HWAVEOUT hwo);
/* == END WINDOWS HEADERS == */
/* === END WINDOWS HEADERS === */
struct AudioContext {
HWAVEOUT handle;

View File

@ -386,17 +386,51 @@ static cc_result Http_BackendDo(struct HttpRequest* req, cc_string* url) {
/*########################################################################################################################*
*-----------------------------------------------------WinINet backend-----------------------------------------------------*
*#########################################################################################################################*/
#define WIN32_LEAN_AND_MEAN
#define NOSERVICE
#define NOMCX
#define NOIME
#ifndef UNICODE
#define UNICODE
#define _UNICODE
#endif
#include <windows.h>
#include <wininet.h>
static HINTERNET hInternet;
/* === BEGIN WINDOWS HEADERS === */
typedef unsigned long DWORD;
typedef int BOOL;
typedef unsigned short WORD;
typedef void* PVOID;
typedef const char* PCSTR;
typedef cc_uintptr DWORD_PTR;
#define WINAPI _stdcall
#define WINBASEAPI __declspec(dllimport)
WINBASEAPI DWORD WINAPI GetLastError(void);
/* === BEGIN wininet.h === */
#define INETAPI __declspec(dllimport)
typedef PVOID HINTERNET;
typedef WORD INTERNET_PORT;
#define INTERNET_OPEN_TYPE_PRECONFIG 0 // use registry configuration
#define INTERNET_OPEN_TYPE_DIRECT 1 // direct to net
#define INTERNET_SERVICE_HTTP 3
#define HTTP_ADDREQ_FLAG_ADD 0x20000000
#define HTTP_ADDREQ_FLAG_REPLACE 0x80000000
#define INTERNET_FLAG_RELOAD 0x80000000
#define INTERNET_FLAG_NO_CACHE_WRITE 0x04000000
#define INTERNET_FLAG_SECURE 0x00800000
#define INTERNET_FLAG_NO_COOKIES 0x00080000
#define INTERNET_FLAG_NO_UI 0x00000200
#define SECURITY_FLAG_IGNORE_REVOCATION 0x00000080
#define HTTP_QUERY_RAW_HEADERS 21
#define INTERNET_OPTION_SECURITY_FLAGS 31
INETAPI BOOL WINAPI InternetCloseHandle(HINTERNET hInternet);
INETAPI HINTERNET WINAPI InternetConnectA(HINTERNET hInternet, PCSTR serverName, INTERNET_PORT serverPort, PCSTR userName, PCSTR password, DWORD service, DWORD flags, DWORD_PTR context);
INETAPI HINTERNET WINAPI InternetOpenA(PCSTR agent, DWORD accessType, PCSTR lpszProxy, PCSTR proxyBypass, DWORD flags);
INETAPI BOOL WINAPI InternetQueryOptionW(HINTERNET hInternet, DWORD option, PVOID buffer, DWORD* bufferLength);
INETAPI BOOL WINAPI InternetSetOptionW(HINTERNET hInternet, DWORD option, PVOID buffer, DWORD bufferLength);
INETAPI BOOL WINAPI InternetQueryDataAvailable(HINTERNET hFile, DWORD* numBytesAvailable, DWORD flags, DWORD_PTR context);
INETAPI BOOL WINAPI InternetReadFile(HINTERNET hFile, PVOID buffer, DWORD numBytesToRead, DWORD* numBytesRead);
INETAPI BOOL WINAPI HttpQueryInfoA(HINTERNET hRequest, DWORD infoLevel, PVOID buffer, DWORD* bufferLength, DWORD* index);
INETAPI BOOL WINAPI HttpAddRequestHeadersA(HINTERNET hRequest, PCSTR headers, DWORD headersLength, DWORD modifiers);
INETAPI HINTERNET WINAPI HttpOpenRequestA(HINTERNET hConnect, PCSTR verb, PCSTR objectName, PCSTR version, PCSTR referrer, PCSTR* acceptTypes, DWORD flags, DWORD_PTR context);
INETAPI BOOL WINAPI HttpSendRequestA(HINTERNET hRequest, PCSTR headers, DWORD headersLength, PVOID optional, DWORD optionalLength);
/* === END WINDOWS HEADERS === */
/* caches connections to web servers */
struct HttpCacheEntry {
@ -408,6 +442,7 @@ struct HttpCacheEntry {
};
#define HTTP_CACHE_ENTRIES 10
static struct HttpCacheEntry http_cache[HTTP_CACHE_ENTRIES];
static HINTERNET hInternet;
/* Converts characters to UTF8, then calls Http_URlEncode on them. */
static void HttpCache_UrlEncodeUrl(cc_string* dst, const cc_string* src) {
@ -492,7 +527,7 @@ static cc_result HttpCache_Lookup(struct HttpCacheEntry* e) {
}
cc_bool Http_DescribeError(cc_result res, cc_string* dst) {
return Platform_DescribeErrorExt(res, dst, GetModuleHandle(TEXT("wininet.dll")));
return Platform_DescribeErrorExt(res, dst, "wininet.dll");
}
static void Http_BackendInit(void) {
@ -533,9 +568,9 @@ static cc_result Http_StartRequest(struct HttpRequest* req, cc_string* url, HINT
/* ignore revocation stuff */
bufferLen = sizeof(flags);
InternetQueryOption(*handle, INTERNET_OPTION_SECURITY_FLAGS, (void*)&bufferLen, &flags);
InternetQueryOptionW(*handle, INTERNET_OPTION_SECURITY_FLAGS, (void*)&bufferLen, &flags);
flags |= SECURITY_FLAG_IGNORE_REVOCATION;
InternetSetOption(*handle, INTERNET_OPTION_SECURITY_FLAGS, &flags, sizeof(flags));
InternetSetOptionW(*handle, INTERNET_OPTION_SECURITY_FLAGS, &flags, sizeof(flags));
Http_SetRequestHeaders(req);
return HttpSendRequestA(*handle, NULL, 0, req->data, req->size) ? 0 : GetLastError();

View File

@ -37,6 +37,7 @@ extern const cc_result ReturnCode_DirectoryExists;
int Platform_EncodeUtf16(void* data, const cc_string* src);
/* Converts a null terminated WCHAR* to char* in-place */
void Platform_Utf16ToAnsi(void* data);
cc_bool Platform_DescribeErrorExt(cc_result res, cc_string* dst, const char* file);
#else
/* Encodes a string in UTF8 format, also null terminating the string. */
/* Returns the number of bytes written, excluding trailing NULL terminator. */
@ -60,7 +61,6 @@ cc_result Platform_Decrypt(const void* data, int len, cc_string* dst);
/* NOTE: This is for general functions like file I/O. If a more specific
describe exists (e.g. Http_DescribeError), that should be preferred. */
cc_bool Platform_DescribeError(cc_result res, cc_string* dst);
cc_bool Platform_DescribeErrorExt(cc_result res, cc_string* dst, void* lib);
/* Starts the game with the given arguments. */
CC_API cc_result Process_StartGame(const cc_string* args);

View File

@ -774,9 +774,12 @@ void Platform_Free(void) {
HeapDestroy(heap);
}
cc_bool Platform_DescribeErrorExt(cc_result res, cc_string* dst, void* lib) {
cc_bool Platform_DescribeErrorExt(cc_result res, cc_string* dst, const char* file) {
WCHAR chars[NATIVE_STR_LEN];
DWORD flags = FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS;
void* lib = NULL;
if (file) lib = GetModuleHandleA(file);
if (lib) flags |= FORMAT_MESSAGE_FROM_HMODULE;
res = FormatMessageW(flags, lib, res, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),