From 5424b1bb69c9a322ee7f6797e4c3cf52330d209b Mon Sep 17 00:00:00 2001 From: MysterD Date: Sat, 4 Jun 2022 17:58:36 -0700 Subject: [PATCH] Close modfile at the correct time when downloading --- autogen/lua_definitions/structs.lua | 3 +-- docs/lua/structs.md | 3 +-- src/pc/lua/smlua_cobject_autogen.c | 5 ++--- src/pc/mods/mod.h | 3 +-- src/pc/network/packets/packet_download.c | 6 ++++-- 5 files changed, 9 insertions(+), 11 deletions(-) diff --git a/autogen/lua_definitions/structs.lua b/autogen/lua_definitions/structs.lua index ed8b97d2..b89ee40c 100644 --- a/autogen/lua_definitions/structs.lua +++ b/autogen/lua_definitions/structs.lua @@ -738,9 +738,8 @@ --- @class ModFile --- @field public cachedPath string ---- @field public complete boolean ---- @field public curOffset integer --- @field public relativePath string +--- @field public wroteBytes integer --- @class ModeTransitionInfo --- @field public frame integer diff --git a/docs/lua/structs.md b/docs/lua/structs.md index 80e21b0f..3787f76d 100644 --- a/docs/lua/structs.md +++ b/docs/lua/structs.md @@ -1053,9 +1053,8 @@ | Field | Type | Access | | ----- | ---- | ------ | | cachedPath | `string` | read-only | -| complete | `boolean` | read-only | -| curOffset | `integer` | read-only | | relativePath | `string` | read-only | +| wroteBytes | `integer` | read-only | [:arrow_up_small:](#) diff --git a/src/pc/lua/smlua_cobject_autogen.c b/src/pc/lua/smlua_cobject_autogen.c index d157d70e..c7f837f5 100644 --- a/src/pc/lua/smlua_cobject_autogen.c +++ b/src/pc/lua/smlua_cobject_autogen.c @@ -854,15 +854,14 @@ static struct LuaObjectField sModFields[LUA_MOD_FIELD_COUNT] = { // { "size", LVT_???, offsetof(struct Mod, size), true, LOT_??? }, <--- UNIMPLEMENTED }; -#define LUA_MOD_FILE_FIELD_COUNT 4 +#define LUA_MOD_FILE_FIELD_COUNT 3 static struct LuaObjectField sModFileFields[LUA_MOD_FILE_FIELD_COUNT] = { { "cachedPath", LVT_STRING_P, offsetof(struct ModFile, cachedPath), true, LOT_NONE }, - { "complete", LVT_BOOL, offsetof(struct ModFile, complete), true, LOT_NONE }, - { "curOffset", LVT_U64, offsetof(struct ModFile, curOffset), true, LOT_NONE }, // { "dataHash", LOT_???, offsetof(struct ModFile, dataHash), true, LOT_??? }, <--- UNIMPLEMENTED // { "fp", LVT_???, offsetof(struct ModFile, fp), true, LOT_??? }, <--- UNIMPLEMENTED { "relativePath", LVT_STRING, offsetof(struct ModFile, relativePath), true, LOT_NONE }, // { "size", LVT_???, offsetof(struct ModFile, size), true, LOT_??? }, <--- UNIMPLEMENTED + { "wroteBytes", LVT_U64, offsetof(struct ModFile, wroteBytes), true, LOT_NONE }, }; #define LUA_MODE_TRANSITION_INFO_FIELD_COUNT 6 diff --git a/src/pc/mods/mod.h b/src/pc/mods/mod.h index 03dab0c0..62774dd4 100644 --- a/src/pc/mods/mod.h +++ b/src/pc/mods/mod.h @@ -12,8 +12,7 @@ struct ModFile { size_t size; FILE* fp; - u64 curOffset; - bool complete; + u64 wroteBytes; u8 dataHash[16]; char* cachedPath; }; diff --git a/src/pc/network/packets/packet_download.c b/src/pc/network/packets/packet_download.c index edce6541..e418c877 100644 --- a/src/pc/network/packets/packet_download.c +++ b/src/pc/network/packets/packet_download.c @@ -322,6 +322,7 @@ static void open_mod_file(struct Mod* mod, struct ModFile* file) { mod_file_create_directories(mod, file); + file->wroteBytes = 0; file->fp = fopen(fullPath, "wb"); if (file->fp == NULL) { LOG_ERROR("unable to open for write: '%s' - '%s'", fullPath, strerror(errno)); @@ -399,7 +400,7 @@ after_group:; u64 fileWriteLength = MIN((modFile->size - fileWriteOffset), (chunkLength - chunkPour)); // read from file, filling chunk - if (!modFile->cachedPath) { + if (!modFile->cachedPath && (modFile->wroteBytes < modFile->size)) { open_mod_file(mod, modFile); if (modFile->fp == NULL) { LOG_ERROR("Failed to open file for download write: %s", modFile->cachedPath); @@ -407,8 +408,9 @@ after_group:; } fseek(modFile->fp, fileWriteOffset, SEEK_SET); fwrite(&chunk[chunkPour], sizeof(u8), fileWriteLength, modFile->fp); + modFile->wroteBytes += fileWriteLength; - if (modFile->fp != NULL) { + if (modFile->wroteBytes >= modFile->size) { fflush(modFile->fp); fclose(modFile->fp); modFile->fp = NULL;