maybe fix crash on osx

This commit is contained in:
UnknownShadow200 2018-07-16 23:10:31 +10:00
parent 0ebec8142a
commit c992969c9e
11 changed files with 122 additions and 167 deletions

View File

@ -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);

View File

@ -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;

View File

@ -67,7 +67,7 @@ namespace Launcher.Patcher {
List<byte[]> datas = new List<byte[]>();
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);

View File

@ -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) {

View File

@ -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);

View File

@ -308,6 +308,7 @@
<ClCompile Include="SelectionBox.c" />
<ClCompile Include="ServerConnection.c" />
<ClCompile Include="SkyboxRenderer.c" />
<ClCompile Include="Socket.c" />
<ClCompile Include="Stream.c" />
<ClCompile Include="String.c" />
<ClCompile Include="TerrainAtlas.c" />

View File

@ -575,5 +575,8 @@
<ClCompile Include="PosixPlatform.c">
<Filter>Source Files\Platform</Filter>
</ClCompile>
<ClCompile Include="Socket.c">
<Filter>Source Files\Platform</Filter>
</ClCompile>
</ItemGroup>
</Project>

View File

@ -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 <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <fcntl.h>
#include <time.h>
#include <pthread.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#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);

105
src/Client/Socket.c Normal file
View File

@ -0,0 +1,105 @@
#include "Platform.h"
#include "ErrorHandler.h"
#if CC_BUILD_WIN
#define WIN32_LEAN_AND_MEAN
#define _WIN32_WINNT 0x0500
#include <winsock2.h>
ReturnCode ReturnCode_SocketInProgess = WSAEINPROGRESS;
ReturnCode ReturnCode_SocketWouldBlock = WSAEWOULDBLOCK;
#elif CC_BUILD_X11
#include <errno.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
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
}

View File

@ -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];

View File

@ -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? */