Platform_EncodeString --> Platform_EncodeUtf8/Utf16, because it's important to know the actual encoding

E.g. if you were trying to use libcurl http backend on windows, Platform_EncodeString would encode the string as utf16. But libcurl requires utf8 strings.
This commit is contained in:
UnknownShadow200 2021-01-06 13:09:46 +11:00
parent dde41af49c
commit 458bd587a2
6 changed files with 48 additions and 43 deletions

View File

@ -508,7 +508,7 @@ void Game_TakeScreenshot(void) {
String_Format3(&filename, "-%p2-%p2-%p2.png", &now.hour, &now.minute, &now.second);
#ifdef CC_BUILD_WEB
Platform_EncodeString(str, &filename);
Platform_EncodeUtf8(str, &filename);
EM_ASM_({
var name = UTF8ToString($0);
var canvas = Module['canvas'];

View File

@ -345,7 +345,7 @@ static void Http_DownloadAsync(struct HttpRequest* req) {
String_InitArray(url, urlBuffer);
Http_BeginRequest(req, &url);
Platform_EncodeString(urlStr, &url);
Platform_EncodeUtf8(urlStr, &url);
EM_ASM_({
var url = UTF8ToString($0);
@ -606,7 +606,7 @@ static cc_result Http_BackendDo(struct HttpRequest* req, cc_string* url) {
_curl_easy_setopt(curl, CURLOPT_HTTPHEADER, req->meta);
Http_SetCurlOpts(req);
Platform_EncodeString(urlStr, url);
Platform_EncodeUtf8(urlStr, url);
_curl_easy_setopt(curl, CURLOPT_URL, urlStr);
if (req->requestType == REQUEST_TYPE_HEAD) {

View File

@ -1320,13 +1320,13 @@ static void DownloadMap(const cc_string* path) {
char strFile[NATIVE_STR_LEN];
cc_string file;
cc_result res;
Platform_EncodeString(strPath, path);
Platform_EncodeUtf8(strPath, path);
/* maps/aaa.schematic -> aaa.cw */
file = *path; Utils_UNSAFE_GetFilename(&file);
file.length = String_LastIndexOf(&file, '.');
String_AppendConst(&file, ".cw");
Platform_EncodeString(strFile, &file);
Platform_EncodeUtf8(strFile, &file);
res = EM_ASM_({
try {
@ -1586,7 +1586,7 @@ static void TexturePackScreen_LoadEntries(struct ListScreen* s) {
#include <emscripten.h>
static void TexturePackScreen_UploadCallback(const cc_string* path) {
char str[NATIVE_STR_LEN];
Platform_EncodeString(str, path);
Platform_EncodeUtf8(str, path);
/* Move from temp into texpacks folder */
/* TODO: This is pretty awful and should be rewritten */

View File

@ -340,7 +340,7 @@ int Directory_Exists(const cc_string* path) {
TCHAR str[NATIVE_STR_LEN];
DWORD attribs;
Platform_EncodeString(str, path);
Platform_EncodeUtf16(str, path);
attribs = GetFileAttributes(str);
return attribs != INVALID_FILE_ATTRIBUTES && (attribs & FILE_ATTRIBUTE_DIRECTORY);
}
@ -349,7 +349,7 @@ cc_result Directory_Create(const cc_string* path) {
TCHAR str[NATIVE_STR_LEN];
BOOL success;
Platform_EncodeString(str, path);
Platform_EncodeUtf16(str, path);
success = CreateDirectory(str, NULL);
return success ? 0 : GetLastError();
}
@ -358,7 +358,7 @@ int File_Exists(const cc_string* path) {
TCHAR str[NATIVE_STR_LEN];
DWORD attribs;
Platform_EncodeString(str, path);
Platform_EncodeUtf16(str, path);
attribs = GetFileAttributes(str);
return attribs != INVALID_FILE_ATTRIBUTES && !(attribs & FILE_ATTRIBUTE_DIRECTORY);
}
@ -375,7 +375,7 @@ cc_result Directory_Enum(const cc_string* dirPath, void* obj, Directory_EnumCall
/* Need to append \* to search for files in directory */
String_InitArray(path, pathBuffer);
String_Format1(&path, "%s\\*", dirPath);
Platform_EncodeString(str, &path);
Platform_EncodeUtf16(str, &path);
find = FindFirstFile(str, &entry);
if (find == INVALID_HANDLE_VALUE) return GetLastError();
@ -409,7 +409,7 @@ cc_result Directory_Enum(const cc_string* dirPath, void* obj, Directory_EnumCall
static cc_result File_Do(cc_file* file, const cc_string* path, DWORD access, DWORD createMode) {
TCHAR str[NATIVE_STR_LEN];
Platform_EncodeString(str, path);
Platform_EncodeUtf16(str, path);
*file = CreateFile(str, access, FILE_SHARE_READ, NULL, createMode, 0, NULL);
return *file != INVALID_HANDLE_VALUE ? 0 : GetLastError();
}
@ -457,13 +457,13 @@ cc_result File_Length(cc_file file, cc_uint32* len) {
int Directory_Exists(const cc_string* path) {
char str[NATIVE_STR_LEN];
struct stat sb;
Platform_EncodeString(str, path);
Platform_EncodeUtf8(str, path);
return stat(str, &sb) == 0 && S_ISDIR(sb.st_mode);
}
cc_result Directory_Create(const cc_string* path) {
char str[NATIVE_STR_LEN];
Platform_EncodeString(str, path);
Platform_EncodeUtf8(str, path);
/* read/write/search permissions for owner and group, and with read/search permissions for others. */
/* TODO: Is the default mode in all cases */
return mkdir(str, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) == -1 ? errno : 0;
@ -472,7 +472,7 @@ cc_result Directory_Create(const cc_string* path) {
int File_Exists(const cc_string* path) {
char str[NATIVE_STR_LEN];
struct stat sb;
Platform_EncodeString(str, path);
Platform_EncodeUtf8(str, path);
return stat(str, &sb) == 0 && S_ISREG(sb.st_mode);
}
@ -484,7 +484,7 @@ cc_result Directory_Enum(const cc_string* dirPath, void* obj, Directory_EnumCall
char* src;
int len, res;
Platform_EncodeString(str, dirPath);
Platform_EncodeUtf8(str, dirPath);
dirPtr = opendir(str);
if (!dirPtr) return errno;
@ -522,7 +522,7 @@ cc_result Directory_Enum(const cc_string* dirPath, void* obj, Directory_EnumCall
static cc_result File_Do(cc_file* file, const cc_string* path, int mode) {
char str[NATIVE_STR_LEN];
Platform_EncodeString(str, path);
Platform_EncodeUtf8(str, path);
*file = open(str, mode, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
return *file == -1 ? errno : 0;
}
@ -1057,14 +1057,14 @@ cc_result Process_StartGame(const cc_string* args) {
String_InitArray(argv, argvBuffer);
String_Format1(&argv, "ClassiCube.exe %s", args);
Platform_EncodeString(raw, &argv);
Platform_EncodeUtf16(raw, &argv);
return Process_RawStart(path, raw);
}
void Process_Exit(cc_result code) { ExitProcess(code); }
void Process_StartOpen(const cc_string* args) {
TCHAR str[NATIVE_STR_LEN];
Platform_EncodeString(str, args);
Platform_EncodeUtf16(str, args);
ShellExecute(NULL, NULL, str, NULL, NULL, SW_SHOWNORMAL);
}
#elif defined CC_BUILD_WEB
@ -1073,7 +1073,7 @@ void Process_Exit(cc_result code) { exit(code); }
void Process_StartOpen(const cc_string* args) {
char str[NATIVE_STR_LEN];
Platform_EncodeString(str, args);
Platform_EncodeUtf8(str, args);
EM_ASM_({ window.open(UTF8ToString($0)); }, str);
}
#elif defined CC_BUILD_ANDROID
@ -1116,7 +1116,7 @@ cc_result Process_StartGame(const cc_string* args) {
if (res) return res;
path[len] = '\0';
Platform_EncodeString(raw, args);
Platform_EncodeUtf8(raw, args);
argv[0] = path; argv[1] = raw;
/* need to null-terminate multiple arguments */
@ -1142,7 +1142,7 @@ void Process_StartOpen(const cc_string* args) {
CFURLRef urlCF;
int len;
len = Platform_EncodeString(str, args);
len = Platform_EncodeUtf8(str, args);
urlCF = CFURLCreateWithBytes(kCFAllocatorDefault, str, len, kCFStringEncodingUTF8, NULL);
LSOpenCFURLRef(urlCF, NULL);
CFRelease(urlCF);
@ -1151,7 +1151,7 @@ void Process_StartOpen(const cc_string* args) {
void Process_StartOpen(const cc_string* args) {
char str[NATIVE_STR_LEN];
char* cmd[3];
Platform_EncodeString(str, args);
Platform_EncodeUtf8(str, args);
cmd[0] = "open"; cmd[1] = str; cmd[2] = NULL;
Process_RawStart("open", cmd);
@ -1160,7 +1160,7 @@ void Process_StartOpen(const cc_string* args) {
void Process_StartOpen(const cc_string* args) {
char str[NATIVE_STR_LEN];
char* cmd[3];
Platform_EncodeString(str, args);
Platform_EncodeUtf8(str, args);
/* TODO: Can xdg-open be used on original Solaris, or is it just an OpenIndiana thing */
cmd[0] = "xdg-open"; cmd[1] = str; cmd[2] = NULL;
@ -1439,7 +1439,7 @@ const cc_string DynamicLib_Ext = String_FromConst(".dll");
void* DynamicLib_Load2(const cc_string* path) {
TCHAR str[NATIVE_STR_LEN];
Platform_EncodeString(str, path);
Platform_EncodeUtf16(str, path);
return LoadLibrary(str);
}
@ -1463,7 +1463,7 @@ const cc_string DynamicLib_Ext = String_FromConst(".dylib");
void* DynamicLib_Load2(const cc_string* path) {
char str[NATIVE_STR_LEN];
Platform_EncodeString(str, path);
Platform_EncodeUtf8(str, path);
return NSAddImage(str, NSADDIMAGE_OPTION_WITH_SEARCHING |
NSADDIMAGE_OPTION_RETURN_ON_ERROR);
}
@ -1505,7 +1505,7 @@ const cc_string DynamicLib_Ext = String_FromConst(".so");
void* DynamicLib_Load2(const cc_string* path) {
char str[NATIVE_STR_LEN];
Platform_EncodeString(str, path);
Platform_EncodeUtf8(str, path);
return dlopen(str, RTLD_NOW);
}
@ -1547,7 +1547,7 @@ cc_bool DynamicLib_GetAll(void* lib, const struct DynamicLibSym* syms, int count
*--------------------------------------------------------Platform---------------------------------------------------------*
*#########################################################################################################################*/
#if defined CC_BUILD_WIN
int Platform_EncodeString(void* data, const cc_string* src) {
int Platform_EncodeUtf16(void* data, const cc_string* src) {
TCHAR* dst = (TCHAR*)data;
int i;
if (src->length > FILENAME_SIZE) Logger_Abort("String too long to expand");
@ -1631,7 +1631,7 @@ cc_bool Platform_DescribeError(cc_result res, cc_string* dst) {
return Platform_DescribeErrorExt(res, dst, NULL);
}
#elif defined CC_BUILD_POSIX
int Platform_EncodeString(void* data, const cc_string* src) {
int Platform_EncodeUtf8(void* data, const cc_string* src) {
cc_uint8* dst = (cc_uint8*)data;
cc_uint8* cur;
int i, len = 0;

View File

@ -30,10 +30,15 @@ extern const cc_result ReturnCode_FileNotFound;
extern const cc_result ReturnCode_SocketInProgess;
extern const cc_result ReturnCode_SocketWouldBlock;
/* Encodes a string in platform specific format. (e.g. unicode on windows, UTF8 on linux) */
/* NOTE: Only useful for platform specific function calls - do NOT try to interpret the data. */
#ifdef CC_BUILD_WIN
/* Encodes a string in UTF16 format, also null terminating the string. */
/* Returns the number of bytes written, excluding trailing NULL terminator. */
int Platform_EncodeString(void* data, const cc_string* src);
int Platform_EncodeUtf16(void* data, const cc_string* src);
#else
/* Encodes a string in UTF8 format, also null terminating the string. */
/* Returns the number of bytes written, excluding trailing NULL terminator. */
int Platform_EncodeUtf8(void* data, const cc_string* src);
#endif
/* Initialises the platform specific state. */
void Platform_Init(void);

View File

@ -158,7 +158,7 @@ void Window_Create(int width, int height) {
void Window_SetTitle(const cc_string* title) {
char str[NATIVE_STR_LEN];
Platform_EncodeString(str, title);
Platform_EncodeUtf8(str, title);
SDL_SetWindowTitle(win_handle, str);
}
@ -173,7 +173,7 @@ void Clipboard_GetText(cc_string* value) {
void Clipboard_SetText(const cc_string* value) {
char str[NATIVE_STR_LEN];
Platform_EncodeString(str, value);
Platform_EncodeUtf8(str, value);
SDL_SetClipboardText(str);
}
@ -743,7 +743,7 @@ void Window_Create(int width, int height) {
void Window_SetTitle(const cc_string* title) {
TCHAR str[NATIVE_STR_LEN];
Platform_EncodeString(str, title);
Platform_EncodeUtf16(str, title);
SetWindowText(win_handle, str);
}
@ -1310,7 +1310,7 @@ void Window_Create(int width, int height) {
void Window_SetTitle(const cc_string* title) {
char str[NATIVE_STR_LEN];
Platform_EncodeString(str, title);
Platform_EncodeUtf8(str, title);
XStoreName(win_display, win_handle, str);
}
@ -1611,7 +1611,7 @@ void Window_ProcessEvents(void) {
if (e.xselectionrequest.selection == xa_clipboard && e.xselectionrequest.target == xa_utf8_string && clipboard_copy_text.length) {
reply.xselection.property = Window_GetSelectionProperty(&e);
char str[800];
int len = Platform_EncodeString(str, &clipboard_copy_text);
int len = Platform_EncodeUtf8(str, &clipboard_copy_text);
XChangeProperty(win_display, reply.xselection.requestor, reply.xselection.property, xa_utf8_string, 8,
PropModeReplace, (unsigned char*)str, len);
@ -2156,7 +2156,7 @@ void Clipboard_SetText(const cc_string* value) {
if (err) Logger_Abort2(err, "Clearing Pasteboard");
PasteboardSynchronize(pbRef);
len = Platform_EncodeString(str, value);
len = Platform_EncodeUtf8(str, value);
cfData = CFDataCreate(NULL, str, len);
if (!cfData) Logger_Abort("CFDataCreate() returned null pointer");
@ -2553,7 +2553,7 @@ void Window_SetTitle(const cc_string* title) {
int len;
/* TODO: This leaks memory, old title isn't released */
len = Platform_EncodeString(str, title);
len = Platform_EncodeUtf8(str, title);
titleCF = CFStringCreateWithBytes(kCFAllocatorDefault, str, len, kCFStringEncodingUTF8, false);
SetWindowTitleWithCFString(win_handle, titleCF);
}
@ -2932,7 +2932,7 @@ void Window_SetTitle(const cc_string* title) {
int len;
/* TODO: This leaks memory, old title isn't released */
len = Platform_EncodeString(str, title);
len = Platform_EncodeUtf8(str, title);
titleCF = CFStringCreateWithBytes(kCFAllocatorDefault, str, len, kCFStringEncodingUTF8, false);
objc_msgSend(winHandle, sel_registerName("setTitle:"), titleCF);
}
@ -3593,7 +3593,7 @@ void Window_Create(int width, int height) {
void Window_SetTitle(const cc_string* title) {
char str[NATIVE_STR_LEN];
Platform_EncodeString(str, title);
Platform_EncodeUtf8(str, title);
EM_ASM_({ document.title = UTF8ToString($0); }, str);
}
@ -3613,7 +3613,7 @@ EMSCRIPTEN_KEEPALIVE void Window_GotClipboardText(char* src) {
void Clipboard_GetText(cc_string* value) { }
void Clipboard_SetText(const cc_string* value) {
char str[NATIVE_STR_LEN];
Platform_EncodeString(str, value);
Platform_EncodeUtf8(str, value);
/* For IE11, use window.clipboardData to set the clipboard */
/* For other browsers, instead use the window.copy events */
@ -3801,7 +3801,7 @@ void Window_OpenKeyboard(const cc_string* text, int type) {
char str[NATIVE_STR_LEN];
keyboardOpen = true;
if (!Input_TouchMode) return;
Platform_EncodeString(str, text);
Platform_EncodeUtf8(str, text);
Platform_LogConst("OPEN SESAME");
EM_ASM_({
@ -3836,7 +3836,7 @@ void Window_OpenKeyboard(const cc_string* text, int type) {
void Window_SetKeyboardText(const cc_string* text) {
char str[NATIVE_STR_LEN];
if (!Input_TouchMode) return;
Platform_EncodeString(str, text);
Platform_EncodeUtf8(str, text);
EM_ASM_({
if (!window.cc_inputElem) return;