From c992969c9ef3d0dff0d04426ff9298c6cc94b016 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Mon, 16 Jul 2018 23:10:31 +1000 Subject: [PATCH] maybe fix crash on osx --- Launcher2/Gui/Views/UpdatesView.cs | 2 +- Launcher2/Patcher/ResourceChecker.cs | 2 +- Launcher2/Patcher/ResourcePatcher.cs | 6 +- Launcher2/Updater/Applier.cs | 2 +- OpenTK/Platform/MacOS/CarbonWindow.cs | 9 +-- src/Client/Client.vcxproj | 1 + src/Client/Client.vcxproj.filters | 3 + src/Client/PosixPlatform.c | 80 +------------------- src/Client/Socket.c | 105 ++++++++++++++++++++++++++ src/Client/Utils.c | 2 +- src/Client/WinPlatform.c | 77 +------------------ 11 files changed, 122 insertions(+), 167 deletions(-) create mode 100644 src/Client/Socket.c diff --git a/Launcher2/Gui/Views/UpdatesView.cs b/Launcher2/Gui/Views/UpdatesView.cs index 299416853..5097341d9 100644 --- a/Launcher2/Gui/Views/UpdatesView.cs +++ b/Launcher2/Gui/Views/UpdatesView.cs @@ -39,7 +39,7 @@ namespace Launcher.Gui.Views { const string dateFormat = "dd-MM-yyyy HH:mm"; protected override void MakeWidgets() { widgetIndex = 0; - DateTime writeTime = Platform.FileGetWriteTime("ClassicalSharp.exe"); + DateTime writeTime = Platform.FileGetModifiedTime("ClassicalSharp.exe"); Makers.Label(this, "Your build:", textFont) .SetLocation(Anchor.Centre, Anchor.Centre, -55, -120); diff --git a/Launcher2/Patcher/ResourceChecker.cs b/Launcher2/Patcher/ResourceChecker.cs index ba59acd30..1ea156d18 100644 --- a/Launcher2/Patcher/ResourceChecker.cs +++ b/Launcher2/Patcher/ResourceChecker.cs @@ -85,7 +85,7 @@ namespace Launcher.Patcher { } bool SelectZipEntry(string path) { - string name = Utils.ToFilename(path); + string name = Utils.GetFilename(path); for (int i = 0; i < ResourceList.Filenames.Length; i++) { if (ResourceList.FilesExist[i]) continue; if (name != ResourceList.Filenames[i]) continue; diff --git a/Launcher2/Patcher/ResourcePatcher.cs b/Launcher2/Patcher/ResourcePatcher.cs index 4b39da0c1..bf38a0dac 100644 --- a/Launcher2/Patcher/ResourcePatcher.cs +++ b/Launcher2/Patcher/ResourcePatcher.cs @@ -67,7 +67,7 @@ namespace Launcher.Patcher { List datas = new List(); void ExtractExisting(string path, byte[] data, ZipEntry entry) { - entry.Filename = Utils.ToFilename(path); + entry.Filename = Utils.GetFilename(path); existing.Add(entry.Filename); entries.Add(entry); datas.Add(data); @@ -93,7 +93,7 @@ namespace Launcher.Patcher { void ProcessZipEntry_Classic(string path, byte[] data, ZipEntry entry) { if (!Utils.CaselessEnds(path, ".png")) return; - entry.Filename = Utils.ToFilename(path); + entry.Filename = Utils.GetFilename(path); if (entry.Filename != "terrain.png") { if (entry.Filename == "gui.png") @@ -159,7 +159,7 @@ namespace Launcher.Patcher { } void ProcessZipEntry_Modern(string path, byte[] data, ZipEntry entry) { - entry.Filename = Utils.ToFilename(path); + entry.Filename = Utils.GetFilename(path); if (path == "assets/minecraft/textures/environment/snow.png") { if (!existing.Contains("snow.png")) { writer.WriteZipEntry(entry, data); diff --git a/Launcher2/Updater/Applier.cs b/Launcher2/Updater/Applier.cs index 269497033..a1896e64a 100644 --- a/Launcher2/Updater/Applier.cs +++ b/Launcher2/Updater/Applier.cs @@ -57,7 +57,7 @@ namespace Launcher.Updater { Platform.WriteAllBytes(path, data); try { - Platform.FileSetWriteTime(path, PatchTime); + Platform.FileSetModifiedTime(path, PatchTime); } catch (IOException ex) { ErrorHandler.LogError("I/O exception when trying to set modified time for: " + filename, ex); } catch (UnauthorizedAccessException ex) { diff --git a/OpenTK/Platform/MacOS/CarbonWindow.cs b/OpenTK/Platform/MacOS/CarbonWindow.cs index d4e0065a4..5f61db520 100644 --- a/OpenTK/Platform/MacOS/CarbonWindow.cs +++ b/OpenTK/Platform/MacOS/CarbonWindow.cs @@ -86,14 +86,13 @@ namespace OpenTK.Platform.MacOS { void CreateNativeWindow(WindowClass @class, WindowAttributes attrib, Rect r) { Debug.Print("Creating window..."); - IntPtr windowRef; - OSStatus err = API.CreateNewWindow(@class, attrib, ref r, out windowRef); + OSStatus err = API.CreateNewWindow(@class, attrib, ref r, out WinHandle); API.CheckReturn(err); - Debug.Print("Created window " + windowRef.ToString()); + Debug.Print("Created window " + WinHandle.ToString()); IntPtr titleCF = CF.CFSTR(title); - Debug.Print("Setting window title: {0}, CFstring : {1}, Text : {2}", windowRef, titleCF, title); - API.SetWindowTitleWithCFString(windowRef, titleCF); + Debug.Print("Setting window title: {0}, CFstring : {1}, Text : {2}", WinHandle, titleCF, title); + API.SetWindowTitleWithCFString(WinHandle, titleCF); SetLocation(r.X, r.Y); SetSize(r.Width, r.Height); diff --git a/src/Client/Client.vcxproj b/src/Client/Client.vcxproj index 503a0f132..d18b37157 100644 --- a/src/Client/Client.vcxproj +++ b/src/Client/Client.vcxproj @@ -308,6 +308,7 @@ + diff --git a/src/Client/Client.vcxproj.filters b/src/Client/Client.vcxproj.filters index 7f26652e1..32ba46929 100644 --- a/src/Client/Client.vcxproj.filters +++ b/src/Client/Client.vcxproj.filters @@ -575,5 +575,8 @@ Source Files\Platform + + Source Files\Platform + \ No newline at end of file diff --git a/src/Client/PosixPlatform.c b/src/Client/PosixPlatform.c index 85f6fc36d..2d2612c1b 100644 --- a/src/Client/PosixPlatform.c +++ b/src/Client/PosixPlatform.c @@ -1,5 +1,5 @@ #include "Platform.h" -#define CC_BUILD_X11 1 +#define CC_BUILD_X11 0 #if CC_BUILD_X11 #include "PackedCol.h" #include "Drawer2D.h" @@ -9,16 +9,12 @@ #include #include #include -#include #include #include #include #include #include #include -#include -#include -#include #define UNIX_EPOCH 62135596800 @@ -27,8 +23,6 @@ UChar Platform_DirectorySeparator = '/'; ReturnCode ReturnCode_FileShareViolation = 1000000000; /* TODO: not used apparently */ ReturnCode ReturnCode_FileNotFound = ENOENT; ReturnCode ReturnCode_NotSupported = EPERM; -extern ReturnCode ReturnCode_SocketInProgess; -ReturnCode ReturnCode_SocketWouldBlock = EWOULDBLOCK; static void Platform_UnicodeExpand(UInt8* dst, STRING_PURE String* src) { if (src->length > FILENAME_SIZE) ErrorHandler_Fail("String too long to expand"); @@ -233,78 +227,6 @@ void Platform_SetBitmap(struct Bitmap* bmp); struct Size2D Platform_TextDraw(struct DrawTextArgs* args, Int32 x, Int32 y, PackedCol col); void Platform_ReleaseBitmap(void); -void Platform_SocketCreate(void** socketResult) { - *socketResult = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); - if (*socketResult == -1) { - ErrorHandler_FailWithCode(errno, "Failed to create socket"); - } -} - -ReturnCode Platform_SocketAvailable(void* socket, UInt32* available) { - return ioctl(socket, FIONREAD, available); -} - -ReturnCode Platform_SocketSetBlocking(void* socket, bool blocking) { - Int32 blocking_raw = blocking ? 0 : -1; - return ioctl(socket, FIONBIO, &blocking_raw); -} - -ReturnCode Platform_SocketGetError(void* socket, ReturnCode* result) { - Int32 resultSize = sizeof(ReturnCode); - return getsockopt(socket, SOL_SOCKET, SO_ERROR, result, &resultSize); -} - -ReturnCode Platform_SocketConnect(void* socket, STRING_PURE String* ip, Int32 port) { - struct sockaddr_in addr; - addr.sin_family = AF_INET; - addr.sin_addr.s_addr = inet_addr(ip->buffer); - addr.sin_port = htons((UInt16)port); - - ReturnCode result = connect(socket, (struct sockaddr*)(&addr), sizeof(addr)); - return result == -1 ? errno : 0; -} - -ReturnCode Platform_SocketRead(void* socket, UInt8* buffer, UInt32 count, UInt32* modified) { - Int32 recvCount = recv(socket, buffer, count, 0); - if (recvCount != -1) { *modified = recvCount; return 0; } - *modified = 0; return errno; -} - -ReturnCode Platform_SocketWrite(void* socket, UInt8* buffer, UInt32 count, UInt32* modified) { - Int32 sentCount = send(socket, buffer, count, 0); - if (sentCount != -1) { *modified = sentCount; return 0; } - *modified = 0; return errno; -} - -ReturnCode Platform_SocketClose(void* socket) { - ReturnCode result = 0; - ReturnCode result1 = shutdown(socket, SHUT_RDWR); - if (result1 == -1) result = errno; - - ReturnCode result2 = closesocket(socket); - if (result2 == -1) result = errno; - return result; -} - -ReturnCode Platform_SocketSelect(void* socket, Int32 selectMode, bool* success) { - void* args[2]; args[0] = (void*)1; args[1] = socket; - struct timeval time = { 0 }; - Int32 selectCount; - - if (selectMode == SOCKET_SELECT_READ) { - selectCount = select(1, &args, NULL, NULL, &time); - } else if (selectMode == SOCKET_SELECT_WRITE) { - selectCount = select(1, NULL, &args, NULL, &time); - } else if (selectMode == SOCKET_SELECT_ERROR) { - selectCount = select(1, NULL, NULL, &args, &time); - } else { - selectCount = -1; - } - - if (selectCount != -1) { *success = args[0] != 0; return 0; } - *success = false; return errno; -} - void Platform_HttpInit(void); ReturnCode Platform_HttpMakeRequest(struct AsyncRequest* request, void** handle); ReturnCode Platform_HttpGetRequestHeaders(struct AsyncRequest* request, void* handle, UInt32* size); diff --git a/src/Client/Socket.c b/src/Client/Socket.c new file mode 100644 index 000000000..dad69b846 --- /dev/null +++ b/src/Client/Socket.c @@ -0,0 +1,105 @@ +#include "Platform.h" +#include "ErrorHandler.h" + +#if CC_BUILD_WIN +#define WIN32_LEAN_AND_MEAN +#define _WIN32_WINNT 0x0500 +#include + +ReturnCode ReturnCode_SocketInProgess = WSAEINPROGRESS; +ReturnCode ReturnCode_SocketWouldBlock = WSAEWOULDBLOCK; +#elif CC_BUILD_X11 +#include +#include +#include +#include +#include + +ReturnCode ReturnCode_SocketInProgess = EINPROGRESS; +ReturnCode ReturnCode_SocketWouldBlock = EWOULDBLOCK; +#else +#error "You're not using BSD sockets, define the interface in Socket.c" +#endif + +void Platform_SocketCreate(void** socketResult) { + *socketResult = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + if (*socketResult == -1) { + ErrorHandler_FailWithCode(WSAGetLastError(), "Failed to create socket"); + } +} + +ReturnCode Platform_SocketAvailable(void* socket, UInt32* available) { + return ioctlsocket(socket, FIONREAD, available); +} + +ReturnCode Platform_SocketSetBlocking(void* socket, bool blocking) { + Int32 blocking_raw = blocking ? 0 : -1; + return ioctlsocket(socket, FIONBIO, &blocking_raw); +} + +ReturnCode Platform_SocketGetError(void* socket, ReturnCode* result) { + Int32 resultSize = sizeof(ReturnCode); + return getsockopt(socket, SOL_SOCKET, SO_ERROR, result, &resultSize); +} + +ReturnCode Platform_SocketConnect(void* socket, STRING_PURE String* ip, Int32 port) { + struct sockaddr_in addr; + addr.sin_family = AF_INET; + addr.sin_addr.s_addr = inet_addr(ip->buffer); + addr.sin_port = htons((UInt16)port); + + ReturnCode result = connect(socket, (struct sockaddr*)(&addr), sizeof(addr)); + return result == -1 ? WSAGetLastError() : 0; +} + +ReturnCode Platform_SocketRead(void* socket, UInt8* buffer, UInt32 count, UInt32* modified) { + Int32 recvCount = recv(socket, buffer, count, 0); + if (recvCount == -1) { *modified = recvCount; return 0; } + *modified = 0; return WSAGetLastError(); +} + +ReturnCode Platform_SocketWrite(void* socket, UInt8* buffer, UInt32 count, UInt32* modified) { + Int32 sentCount = send(socket, buffer, count, 0); + if (sentCount != -1) { *modified = sentCount; return 0; } + *modified = 0; return WSAGetLastError(); +} + +ReturnCode Platform_SocketClose(void* socket) { + ReturnCode result = 0; +#if CC_BUILD_WIN + ReturnCode result1 = shutdown(socket, SD_BOTH); +#else + ReturnCode result1 = shutdown(socket, SHUT_RDWR); +#endif + if (result1 == -1) result = WSAGetLastError(); + + ReturnCode result2 = closesocket(socket); + if (result2 == -1) result = WSAGetLastError(); + return result; +} + +ReturnCode Platform_SocketSelect(void* socket, Int32 selectMode, bool* success) { + fd_set set; + FD_ZERO(&set); + FD_SET(socket, &set); + + struct timeval time = { 0 }; + Int32 selectCount; + + if (selectMode == SOCKET_SELECT_READ) { + selectCount = select(1, &set, NULL, NULL, &time); + } else if (selectMode == SOCKET_SELECT_WRITE) { + selectCount = select(1, NULL, &set, NULL, &time); + } else if (selectMode == SOCKET_SELECT_ERROR) { + selectCount = select(1, NULL, NULL, &set, &time); + } else { + selectCount = -1; + } + + if (selectCount == -1) { *success = false; return WSAGetLastError(); } +#if CC_BUILD_WIN + *success = set.fd_count != 0; return 0; +#else + *success = FD_ISSET(socket, &set); return 0; +#endif +} \ No newline at end of file diff --git a/src/Client/Utils.c b/src/Client/Utils.c index bf9d23a98..c0c8a95a9 100644 --- a/src/Client/Utils.c +++ b/src/Client/Utils.c @@ -119,7 +119,7 @@ bool Utils_IsUrlPrefix(STRING_PURE String* value, Int32 index) { || String_IndexOfString(value, &https) == index; } -void String_UNSAFE_GetFilename(STRING_TRANSIENT String* str) { +void Utils_UNSAFE_GetFilename(STRING_TRANSIENT String* str) { Int32 i; for (i = str->length - 1; i >= 0; i--) { UChar c = str->buffer[i]; diff --git a/src/Client/WinPlatform.c b/src/Client/WinPlatform.c index f0a061195..b31a9a2ee 100644 --- a/src/Client/WinPlatform.c +++ b/src/Client/WinPlatform.c @@ -30,8 +30,6 @@ UChar Platform_DirectorySeparator = '\\'; ReturnCode ReturnCode_FileShareViolation = ERROR_SHARING_VIOLATION; ReturnCode ReturnCode_FileNotFound = ERROR_FILE_NOT_FOUND; ReturnCode ReturnCode_NotSupported = ERROR_NOT_SUPPORTED; -ReturnCode ReturnCode_SocketInProgess = WSAEINPROGRESS; -ReturnCode ReturnCode_SocketWouldBlock = WSAEWOULDBLOCK; static void Platform_UnicodeExpand(WCHAR* dst, STRING_PURE String* src) { if (src->length > FILENAME_SIZE) ErrorHandler_Fail("String too long to expand"); @@ -198,7 +196,7 @@ ReturnCode Platform_EnumFiles(STRING_PURE String* path, void* obj, Platform_Enum } path.length = i; - String filename = path; String_UNSAFE_GetFilename(&filename); + String filename = path; Utils_UNSAFE_GetFilename(&filename); callback(&filename, obj); } while (FindNextFileW(find, &data)); @@ -474,79 +472,6 @@ void Platform_ReleaseBitmap(void) { platform_bmp = NULL; } - -void Platform_SocketCreate(void** socketResult) { - *socketResult = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); - if (*socketResult == INVALID_SOCKET) { - ErrorHandler_FailWithCode(WSAGetLastError(), "Failed to create socket"); - } -} - -ReturnCode Platform_SocketAvailable(void* socket, UInt32* available) { - return ioctlsocket(socket, FIONREAD, available); -} - -ReturnCode Platform_SocketSetBlocking(void* socket, bool blocking) { - Int32 blocking_raw = blocking ? 0 : -1; - return ioctlsocket(socket, FIONBIO, &blocking_raw); -} - -ReturnCode Platform_SocketGetError(void* socket, ReturnCode* result) { - Int32 resultSize = sizeof(ReturnCode); - return getsockopt(socket, SOL_SOCKET, SO_ERROR, result, &resultSize); -} - -ReturnCode Platform_SocketConnect(void* socket, STRING_PURE String* ip, Int32 port) { - struct sockaddr_in addr; - addr.sin_family = AF_INET; - addr.sin_addr.s_addr = inet_addr(ip->buffer); - addr.sin_port = htons((UInt16)port); - - ReturnCode result = connect(socket, (struct sockaddr*)(&addr), sizeof(addr)); - return result == SOCKET_ERROR ? WSAGetLastError() : 0; -} - -ReturnCode Platform_SocketRead(void* socket, UInt8* buffer, UInt32 count, UInt32* modified) { - Int32 recvCount = recv(socket, buffer, count, 0); - if (recvCount == SOCKET_ERROR) { *modified = recvCount; return 0; } - *modified = 0; return WSAGetLastError(); -} - -ReturnCode Platform_SocketWrite(void* socket, UInt8* buffer, UInt32 count, UInt32* modified) { - Int32 sentCount = send(socket, buffer, count, 0); - if (sentCount != SOCKET_ERROR) { *modified = sentCount; return 0; } - *modified = 0; return WSAGetLastError(); -} - -ReturnCode Platform_SocketClose(void* socket) { - ReturnCode result = 0; - ReturnCode result1 = shutdown(socket, SD_BOTH); - if (result1 == SOCKET_ERROR) result = WSAGetLastError(); - - ReturnCode result2 = closesocket(socket); - if (result2 == SOCKET_ERROR) result = WSAGetLastError(); - return result; -} - -ReturnCode Platform_SocketSelect(void* socket, Int32 selectMode, bool* success) { - void* args[2]; args[0] = (void*)1; args[1] = socket; - struct timeval time = { 0 }; - Int32 selectCount; - - if (selectMode == SOCKET_SELECT_READ) { - selectCount = select(1, &args, NULL, NULL, &time); - } else if (selectMode == SOCKET_SELECT_WRITE) { - selectCount = select(1, NULL, &args, NULL, &time); - } else if (selectMode == SOCKET_SELECT_ERROR) { - selectCount = select(1, NULL, NULL, &args, &time); - } else { - selectCount = SOCKET_ERROR; - } - - if (selectCount != SOCKET_ERROR) { *success = args[0] != 0; return 0; } - *success = false; return WSAGetLastError(); -} - HINTERNET hInternet; void Platform_HttpInit(void) { /* TODO: Should we use INTERNET_OPEN_TYPE_PRECONFIG instead? */