From aa62d7d43f17b6ae03359af4abbbb6064195736d Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sun, 6 May 2018 13:58:27 +1000 Subject: [PATCH] More work on asyncdownloader --- src/Client/Bitmap.c | 2 +- src/Client/Platform.h | 7 +++++++ src/Client/Program.c | 14 ++++++++++++-- src/Client/WinPlatform.c | 32 ++++++++++++++++++++++++++++++-- 4 files changed, 50 insertions(+), 5 deletions(-) diff --git a/src/Client/Bitmap.c b/src/Client/Bitmap.c index 87e909723..68af8d8a6 100644 --- a/src/Client/Bitmap.c +++ b/src/Client/Bitmap.c @@ -345,7 +345,7 @@ void Bitmap_DecodePng(Bitmap* bmp, Stream* stream) { if (Stream_ReadU8(stream) != 0) ErrorHandler_Fail("PNG filter method must be ADAPTIVE"); if (Stream_ReadU8(stream) != 0) ErrorHandler_Fail("PNG interlacing not supported"); - static UInt32 samplesPerPixel[7] = { 1,0,3,1,2,0,4 }; + static UInt32 samplesPerPixel[7] = { 1, 0, 3, 1, 2, 0, 4 }; bytesPerPixel = ((samplesPerPixel[col] * bitsPerSample) + 7) >> 3; scanlineSize = ((samplesPerPixel[col] * bitsPerSample * bmp->Width) + 7) >> 3; scanlineBytes = scanlineSize + 1; /* Add 1 byte for filter byte of each scanline */ diff --git a/src/Client/Platform.h b/src/Client/Platform.h index f22aaa351..009a87f6b 100644 --- a/src/Client/Platform.h +++ b/src/Client/Platform.h @@ -8,6 +8,7 @@ */ typedef struct DrawTextArgs_ DrawTextArgs; typedef struct Bitmap_ Bitmap; +typedef struct AsyncRequest_ AsyncRequest; extern UInt8* Platform_NewLine; /* Newline for text */ extern UInt8 Platform_DirectorySeparator; @@ -86,4 +87,10 @@ ReturnCode Platform_SocketWrite(void* socket, UInt8* buffer, UInt32 count, UInt3 ReturnCode Platform_SocketClose(void* socket); ReturnCode Platform_SocketAvailable(void* socket, UInt32* available); ReturnCode Platform_SocketSelectRead(void* socket, Int32 microseconds, bool* success); + +void Platform_HttpInit(void); +ReturnCode Platform_HttpMakeRequest(AsyncRequest* request, void** handle); +ReturnCode Platform_HttpGetRequestData(AsyncRequest* request, void* handle); +ReturnCode Platform_HttpFreeRequest(void* handle); +ReturnCode Platform_HttpFree(void); #endif \ No newline at end of file diff --git a/src/Client/Program.c b/src/Client/Program.c index 29c897590..de1899a9d 100644 --- a/src/Client/Program.c +++ b/src/Client/Program.c @@ -10,11 +10,21 @@ #include "Constants.h" #include "Game.h" #include "Funcs.h" +#include "AsyncDownloader.h" int main(void) { ErrorHandler_Init("client.log"); Platform_Init(); + /*Platform_HttpInit(); + AsyncRequest req = { 0 }; + String url = String_FromEmptyArray(req.URL); + String_AppendConst(&url, "http://static.classicube.net/skins/UnknownShadow200.png"); + void* reqHandle = NULL; + ReturnCode ret = Platform_HttpMakeRequest(&req, &reqHandle); + ReturnCode ret2 = Platform_HttpFreeRequest(reqHandle); + ReturnCode ret3 = Platform_HttpFree();*/ + String maps = String_FromConst("maps"); if (!Platform_DirectoryExists(&maps)) { ReturnCode result = Platform_DirectoryCreate(&maps); @@ -48,8 +58,8 @@ int main(void) { } String title = String_FromConst(PROGRAM_APP_NAME); - String rawArgs = Platform_GetCommandLineArgs(); - // rawArgs = String_FromReadonly("UnknownShadow200 fff 127.0.0.1 25566"); + String rawArgs = Platform_GetCommandLineArgs(); + rawArgs = String_FromReadonly("UnknownShadow200 fff 127.0.0.1 25566"); //rawArgs = String_FromReadonly("UnknownShadow200"); String args[5]; UInt32 argsCount = Array_Elems(args); diff --git a/src/Client/WinPlatform.c b/src/Client/WinPlatform.c index 39eed06ed..ff3355b80 100644 --- a/src/Client/WinPlatform.c +++ b/src/Client/WinPlatform.c @@ -5,6 +5,7 @@ #include "ErrorHandler.h" #include "Drawer2D.h" #include "Funcs.h" +#include "AsyncDownloader.h" #define WIN32_LEAN_AND_MEAN #define NOSERVICE #define NOMCX @@ -13,8 +14,10 @@ #include #include #include +#include #pragma comment(lib, "ws2_32.lib") +#pragma comment (lib, "Wininet.lib") HDC hdc; HANDLE heap; bool stopwatch_highResolution; @@ -269,8 +272,7 @@ ReturnCode Platform_FileWrite(void* file, UInt8* buffer, UInt32 count, UInt32* b } ReturnCode Platform_FileClose(void* file) { - BOOL success = CloseHandle((HANDLE)file); - return success ? 0 : GetLastError(); + return CloseHandle((HANDLE)file) ? 0 : GetLastError(); } ReturnCode Platform_FileSeek(void* file, Int32 offset, Int32 seekType) { @@ -593,4 +595,30 @@ ReturnCode Platform_SocketSelectRead(void* socket, Int32 microseconds, bool* suc } else { *success = args[0] != 0; return 0; } +} + +HINTERNET hInternet; +void Platform_HttpInit(void) { + /* TODO: Should we use INTERNET_OPEN_TYPE_PRECONFIG instead? */ + hInternet = InternetOpenA(PROGRAM_APP_NAME, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0); + if (hInternet == NULL) ErrorHandler_FailWithCode(GetLastError(), "Failed to init WinINet"); +} + +ReturnCode Platform_HttpMakeRequest(AsyncRequest* request, void** handle) { + String url = String_FromRawArray(request->URL); + *handle = InternetOpenUrlA(hInternet, url.buffer, NULL, 0, + INTERNET_FLAG_NO_CACHE_WRITE | INTERNET_FLAG_NO_UI | INTERNET_FLAG_RELOAD, NULL); + return *handle == NULL ? GetLastError() : 0; +} + +ReturnCode Platform_HttpGetRequestData(AsyncRequest* request, void* handle) { + +} + +ReturnCode Platform_HttpFreeRequest(void* handle) { + return InternetCloseHandle(handle) ? 0 : GetLastError(); +} + +ReturnCode Platform_HttpFree(void) { + return InternetCloseHandle(hInternet) ? 0 : GetLastError(); } \ No newline at end of file