diff --git a/src/Chat.c b/src/Chat.c index b666d8d3d..7155161b7 100644 --- a/src/Chat.c +++ b/src/Chat.c @@ -122,6 +122,7 @@ static cc_bool CreateLogsDirectory(void) { } static void OpenChatLog(struct cc_datetime* now) { + cc_filepath raw_path; cc_result res; int i; if (Platform_ReadonlyFilesystem || !CreateLogsDirectory()) return; @@ -137,7 +138,9 @@ static void OpenChatLog(struct cc_datetime* now) { String_Format1(&logPath, "%s.txt", &logName); } - res = Stream_AppendFile(&logStream, &logPath); + Platform_EncodePath(&raw_path, &logPath); + res = Stream_AppendPath(&logStream, &raw_path); + if (res && res != ReturnCode_FileShareViolation) { Chat_DisableLogging(); Logger_SysWarn2(res, "appending to", &logPath); diff --git a/src/Game.c b/src/Game.c index b51f4b613..a62d16143 100644 --- a/src/Game.c +++ b/src/Game.c @@ -594,10 +594,10 @@ void Game_TakeScreenshot(void) { cc_string filename; char fileBuffer[STRING_SIZE]; cc_string path; char pathBuffer[FILENAME_SIZE]; struct cc_datetime now; + cc_filepath raw_path; cc_result res; -#ifdef CC_BUILD_WEB - cc_filepath str; -#else + +#ifndef CC_BUILD_WEB struct Stream stream; #endif Game_ScreenshotRequested = false; @@ -609,14 +609,15 @@ void Game_TakeScreenshot(void) { #ifdef CC_BUILD_WEB extern void interop_TakeScreenshot(const char* path); - Platform_EncodePath(&str, &filename); - interop_TakeScreenshot(str.buffer); + Platform_EncodePath(&raw_path, &filename); + interop_TakeScreenshot(raw_path.buffer); #else if (!Utils_EnsureDirectory("screenshots")) return; String_InitArray(path, pathBuffer); String_Format1(&path, "screenshots/%s", &filename); - res = Stream_CreateFile(&stream, &path); + Platform_EncodePath(&raw_path, &path); + res = Stream_CreatePath(&stream, &raw_path); if (res) { Logger_SysWarn2(res, "creating", &path); return; } res = Gfx_TakeScreenshot(&stream); diff --git a/src/Logger.c b/src/Logger.c index 6a29dc8c1..5061f667b 100644 --- a/src/Logger.c +++ b/src/Logger.c @@ -1165,10 +1165,14 @@ static struct Stream logStream; static cc_bool logOpen; void Logger_Log(const cc_string* msg) { - static const cc_string path = String_FromConst("client.log"); + static const cc_string log_path = String_FromConst("client.log"); + cc_filepath raw_path; + if (!logOpen) { logOpen = true; - Stream_AppendFile(&logStream, &path); + + Platform_EncodePath(&raw_path, &log_path); + Stream_AppendPath(&logStream, &raw_path); } if (!logStream.meta.file) return; diff --git a/src/Menus.c b/src/Menus.c index 6f6b1f728..a0e83396e 100644 --- a/src/Menus.c +++ b/src/Menus.c @@ -1363,10 +1363,13 @@ static cc_result DoSaveMap(const cc_string* path, struct GZipState* state) { static const cc_string schematic = String_FromConst(".schematic"); static const cc_string mine = String_FromConst(".mine"); struct Stream stream, compStream; + cc_filepath raw_path; cc_result res; - res = Stream_CreateFile(&stream, path); + Platform_EncodePath(&raw_path, path); + res = Stream_CreatePath(&stream, &raw_path); if (res) { Logger_SysWarn2(res, "creating", path); return res; } + GZip_MakeStream(&compStream, state, &stream); if (String_CaselessEnds(path, &schematic)) { diff --git a/src/Platform_WinCE.c b/src/Platform_WinCE.c index 72601e49e..db7b8fa9b 100644 --- a/src/Platform_WinCE.c +++ b/src/Platform_WinCE.c @@ -237,9 +237,9 @@ void Platform_EncodePath(cc_filepath* dst, const cc_string* src) { void Platform_DecodePath(cc_string* dst, const cc_filepath* path) { int i; - for (i = 0; i < FILENAME_SIZE && dst->uni[i]; i++) + for (i = 0; i < FILENAME_SIZE && path->uni[i]; i++) { - String_Append(dst, Convert_CodepointToCP437(dst->uni[i])); + String_Append(dst, Convert_CodepointToCP437(path->uni[i])); } } diff --git a/src/Platform_Windows.c b/src/Platform_Windows.c index f2be06a7e..f7cac4ad3 100644 --- a/src/Platform_Windows.c +++ b/src/Platform_Windows.c @@ -307,9 +307,9 @@ void Platform_EncodePath(cc_filepath* dst, const cc_string* src) { void Platform_DecodePath(cc_string* dst, const cc_filepath* path) { int i; - for (i = 0; i < FILENAME_SIZE && dst->uni[i]; i++) + for (i = 0; i < FILENAME_SIZE && path->uni[i]; i++) { - String_Append(dst, Convert_CodepointToCP437(dst->uni[i])); + String_Append(dst, Convert_CodepointToCP437(path->uni[i])); } } diff --git a/src/Resources.c b/src/Resources.c index 9b1b4cd63..48003a7f9 100644 --- a/src/Resources.c +++ b/src/Resources.c @@ -65,8 +65,10 @@ static void ZipFile_InspectEntries(const cc_string* path, Zip_SelectEntry select struct Stream stream; cc_result res; struct ZipEntry entries[64]; + cc_filepath raw_path; - res = Stream_OpenFile(&stream, path); + Platform_EncodePath(&raw_path, path); + res = Stream_OpenPath(&stream, &raw_path); if (res == ReturnCode_FileNotFound) return; if (res) { Logger_SysWarn2(res, "opening", path); return; } @@ -267,9 +269,11 @@ static cc_result ZipFile_WriteEntries(struct Stream* s, struct ResourceZipEntry* static void ZipFile_Create(const cc_string* path, struct ResourceZipEntry* entries, int numEntries) { struct Stream s; + cc_filepath raw_path; cc_result res; - res = Stream_CreateFile(&s, path); + Platform_EncodePath(&raw_path, path); + res = Stream_CreatePath(&s, &raw_path); if (res) { Logger_SysWarn2(res, "creating", path); return; } diff --git a/src/Stream.c b/src/Stream.c index 6ce8ef4bf..9bf174b78 100644 --- a/src/Stream.c +++ b/src/Stream.c @@ -114,33 +114,39 @@ static cc_result Stream_FileLength(struct Stream* s, cc_uint32* length) { cc_result Stream_OpenFile(struct Stream* s, const cc_string* path) { cc_filepath str; - cc_file file; - cc_result res; Platform_EncodePath(&str, path); - res = File_Open(&file, &str); - Stream_FromFile(s, file); - return res; + return Stream_OpenPath(s, &str); } cc_result Stream_CreateFile(struct Stream* s, const cc_string* path) { cc_filepath str; - cc_file file; - cc_result res; Platform_EncodePath(&str, path); - res = File_Create(&file, &str); + return Stream_CreatePath(s, &str); +} + +cc_result Stream_OpenPath(struct Stream* s, const cc_filepath* path) { + cc_file file; + cc_result res = File_Open(&file, path); + Stream_FromFile(s, file); return res; } -cc_result Stream_AppendFile(struct Stream* s, const cc_string* path) { - cc_filepath str; +cc_result Stream_CreatePath(struct Stream* s, const cc_filepath* path) { + cc_file file; + cc_result res = File_Create(&file, path); + + Stream_FromFile(s, file); + return res; +} + +cc_result Stream_AppendPath(struct Stream* s, const cc_filepath* path) { cc_file file; cc_result res; - Platform_EncodePath(&str, path); - if ((res = File_OpenOrCreate(&file, &str))) return res; + if ((res = File_OpenOrCreate(&file, path))) return res; if ((res = File_Seek(file, 0, FILE_SEEKFROM_END))) return res; Stream_FromFile(s, file); return res; @@ -149,8 +155,10 @@ cc_result Stream_AppendFile(struct Stream* s, const cc_string* path) { cc_result Stream_WriteAllTo(const cc_string* path, const cc_uint8* data, cc_uint32 length) { struct Stream stream; cc_result res, closeRes; + cc_filepath raw_path; - res = Stream_CreateFile(&stream, path); + Platform_EncodePath(&raw_path, path); + res = Stream_CreatePath(&stream, &raw_path); if (res) return res; res = Stream_Write(&stream, data, length); diff --git a/src/Stream.h b/src/Stream.h index 6d3f0e5d5..4e174b710 100644 --- a/src/Stream.h +++ b/src/Stream.h @@ -49,14 +49,20 @@ typedef cc_result (*FP_Stream_Write)(struct Stream* s, const cc_uint8* buffer, c /* Initialises default function pointers for a stream. (all read, write, seeks return an error) */ void Stream_Init(struct Stream* s); -/* Wrapper for File_Open() then Stream_FromFile() */ +/* Wrapper for Stream_OpenPath() */ CC_API cc_result Stream_OpenFile( struct Stream* s, const cc_string* path); typedef cc_result (*FP_Stream_OpenFile)(struct Stream* s, const cc_string* path); -/* Wrapper for File_Create() then Stream_FromFile() */ +/* Wrapper for Stream_CreatePath() */ CC_API cc_result Stream_CreateFile( struct Stream* s, const cc_string* path); typedef cc_result (*FP_Stream_CreateFile)(struct Stream* s, const cc_string* path); + +/* Wrapper for File_Open() then Stream_FromFile() */ +cc_result Stream_OpenPath( struct Stream* s, const cc_filepath* path); +/* Wrapper for File_Create() then Stream_FromFile() */ +cc_result Stream_CreatePath(struct Stream* s, const cc_filepath* path); /* Wrapper for File_OpenOrCreate, then File_Seek(END), then Stream_FromFile() */ -cc_result Stream_AppendFile(struct Stream* s, const cc_string* path); +cc_result Stream_AppendPath(struct Stream* s, const cc_filepath* path); + /* Creates or overwrites a file, setting the contents to the given data. */ cc_result Stream_WriteAllTo(const cc_string* path, const cc_uint8* data, cc_uint32 length); /* Wraps a file, allowing reading from/writing to/seeking in the file. */ diff --git a/src/UWP/Platform_UWP.cpp b/src/UWP/Platform_UWP.cpp index dd0187659..d0e14399f 100644 --- a/src/UWP/Platform_UWP.cpp +++ b/src/UWP/Platform_UWP.cpp @@ -224,9 +224,9 @@ void Platform_EncodePath(cc_filepath* dst, const cc_string* src) { void Platform_DecodePath(cc_string* dst, const cc_filepath* path) { int i; - for (i = 0; i < FILENAME_SIZE && dst->uni[i]; i++) + for (i = 0; i < FILENAME_SIZE && path->uni[i]; i++) { - String_Append(dst, Convert_CodepointToCP437(dst->uni[i])); + String_Append(dst, Convert_CodepointToCP437(path->uni[i])); } } diff --git a/src/Utils.c b/src/Utils.c index ed4a4cf57..4306eae33 100644 --- a/src/Utils.c +++ b/src/Utils.c @@ -239,12 +239,14 @@ cc_result EntryList_Load(struct StringsBuffer* list, const char* file, char sepa cc_uint8 buffer[2048]; struct Stream stream, buffered; + cc_filepath raw_path; cc_result res; path = String_FromReadonly(file); maxLen = list->_lenMask ? list->_lenMask : STRINGSBUFFER_DEF_LEN_MASK; - res = Stream_OpenFile(&stream, &path); + Platform_EncodePath(&raw_path, &path); + res = Stream_OpenPath(&stream, &raw_path); if (res == ReturnCode_FileNotFound) return res; if (res) { Logger_SysWarn2(res, "opening", &path); return res; } @@ -294,13 +296,15 @@ cc_result EntryList_UNSAFE_Load(struct StringsBuffer* list, const char* file) { void EntryList_Save(struct StringsBuffer* list, const char* file) { cc_string path, entry; char pathBuffer[FILENAME_SIZE]; struct Stream stream; - int i; + cc_filepath raw_path; cc_result res; + int i; String_InitArray(path, pathBuffer); String_AppendConst(&path, file); - res = Stream_CreateFile(&stream, &path); + Platform_EncodePath(&raw_path, &path); + res = Stream_CreatePath(&stream, &raw_path); if (res) { Logger_SysWarn2(res, "creating", &path); return; } for (i = 0; i < list->count; i++) {