diff --git a/source/build/include/animvpx.h b/source/build/include/animvpx.h index cc05da89d..36956a2fa 100644 --- a/source/build/include/animvpx.h +++ b/source/build/include/animvpx.h @@ -36,7 +36,7 @@ typedef struct #include "vfs.h" extern const char *animvpx_read_ivf_header_errmsg[7]; -int32_t animvpx_read_ivf_header(buildvfs_kfd inhandle, animvpx_ivf_header_t *hdr); +int32_t animvpx_read_ivf_header(FileReader & inhandle, animvpx_ivf_header_t *hdr); typedef struct { @@ -48,7 +48,7 @@ typedef struct // VVV everything that follows should be considered private! VVV - buildvfs_kfd inhandle; // the kread() file handle + FileReader *inhandle; // the kread() file handle // state of this struct: // 0: uninited (either not yet or already) @@ -79,7 +79,7 @@ typedef struct } animvpx_codec_ctx; -int32_t animvpx_init_codec(const animvpx_ivf_header_t *info, buildvfs_kfd inhandle, animvpx_codec_ctx *codec); +int32_t animvpx_init_codec(const animvpx_ivf_header_t *info, FileReader & inhandle, animvpx_codec_ctx *codec); int32_t animvpx_uninit_codec(animvpx_codec_ctx *codec); extern const char *animvpx_nextpic_errmsg[8]; diff --git a/source/build/src/animvpx.cpp b/source/build/src/animvpx.cpp index 88446425c..e29fe99af 100644 --- a/source/build/src/animvpx.cpp +++ b/source/build/src/animvpx.cpp @@ -28,11 +28,11 @@ const char *animvpx_read_ivf_header_errmsg[] = { EDUKE32_STATIC_ASSERT(sizeof(animvpx_ivf_header_t) == 32); -int32_t animvpx_read_ivf_header(buildvfs_kfd inhandle, animvpx_ivf_header_t *hdr) +int32_t animvpx_read_ivf_header(FileReader & inhandle, animvpx_ivf_header_t *hdr) { int32_t err; - if (kread(inhandle, hdr, sizeof(animvpx_ivf_header_t)) != sizeof(animvpx_ivf_header_t)) + if (inhandle.Read(hdr, sizeof(animvpx_ivf_header_t)) != sizeof(animvpx_ivf_header_t)) return 1; // "couldn't read header" err = animvpx_check_header(hdr); @@ -86,7 +86,7 @@ static void get_codec_error(animvpx_codec_ctx *codec) } // no checks for double-init! -int32_t animvpx_init_codec(const animvpx_ivf_header_t *info, buildvfs_kfd inhandle, animvpx_codec_ctx *codec) +int32_t animvpx_init_codec(const animvpx_ivf_header_t *info, FileReader & inhandle, animvpx_codec_ctx *codec) { vpx_codec_dec_cfg_t cfg; @@ -98,7 +98,7 @@ int32_t animvpx_init_codec(const animvpx_ivf_header_t *info, buildvfs_kfd inhand codec->height = info->height; // - codec->inhandle = inhandle; + codec->inhandle = &inhandle; codec->pic = (uint8_t *)Xcalloc(info->width*info->height,4); codec->compbuflen = codec->compbufallocsiz = 0; @@ -153,13 +153,13 @@ int32_t animvpx_uninit_codec(animvpx_codec_ctx *codec) ////////// FRAME RETRIEVAL ////////// // read one IVF/VP8 frame, which may code multiple "picture-frames" -static int32_t animvpx_read_frame(buildvfs_kfd inhandle, uint8_t **bufptr, uint32_t *bufsizptr, uint32_t *bufallocsizptr) +static int32_t animvpx_read_frame(FileReader & inhandle, uint8_t **bufptr, uint32_t *bufsizptr, uint32_t *bufallocsizptr) { #pragma pack(push,1) struct { uint32_t framesiz; uint64_t timestamp; } hdr; #pragma pack(pop) - if (kread(inhandle, &hdr, sizeof(hdr)) != sizeof(hdr)) + if (inhandle.Read(&hdr, sizeof(hdr)) != sizeof(hdr)) return 1; if (hdr.framesiz == 0) @@ -184,7 +184,7 @@ static int32_t animvpx_read_frame(buildvfs_kfd inhandle, uint8_t **bufptr, uint3 *bufsizptr = hdr.framesiz; - if (kread(inhandle, *bufptr, hdr.framesiz) != (signed)hdr.framesiz) + if (inhandle.Read(*bufptr, hdr.framesiz) != (signed)hdr.framesiz) return 3; return 0; @@ -226,7 +226,7 @@ int32_t animvpx_nextpic(animvpx_codec_ctx *codec, uint8_t **picptr) read_ivf_frame: corrupted = 0; - ret = animvpx_read_frame(codec->inhandle, &codec->compbuf, &codec->compbuflen, + ret = animvpx_read_frame(*codec->inhandle, &codec->compbuf, &codec->compbuflen, &codec->compbufallocsiz); if (ret == 1) { diff --git a/source/common/utility/files.h b/source/common/utility/files.h index 2dcd7da5a..488232797 100644 --- a/source/common/utility/files.h +++ b/source/common/utility/files.h @@ -210,6 +210,15 @@ public: return buffer; } + TArray ReadPadded(int padding) + { + TArray buffer(mReader->Length + padding, true); + Size length = mReader->Read(&buffer[0], mReader->Length); + if (length < mReader->Length) buffer.Clear(); + else memset(buffer.Data() + mReader->Length, 0, padding); + return buffer; + } + char *Gets(char *strbuf, Size len) { return mReader->Gets(strbuf, (int)len); diff --git a/source/duke3d/src/anim.cpp b/source/duke3d/src/anim.cpp index d3f2c1097..d793edfee 100644 --- a/source/duke3d/src/anim.cpp +++ b/source/duke3d/src/anim.cpp @@ -252,12 +252,12 @@ int32_t Anim_Play(const char *fn) break; dukeanim_t const * origanim = anim; - buildvfs_kfd handle = buildvfs_kfd_invalid; - if (!Bstrcmp(dot, ".ivf")) + FileReader handle; + if (!Bstrcmp(dot, ".ivf")) { - handle = kopen4loadfrommod(fn, 0); - if (handle == buildvfs_kfd_invalid) - break; + handle = kopenFileReader(fn, 0); + if (!handle.isOpen()) + break; } else { @@ -274,9 +274,9 @@ int32_t Anim_Play(const char *fn) vpxfndot[3] = 'f'; vpxfndot[4] = '\0'; - handle = kopen4loadfrommod(vpxfn, 0); - if (handle == buildvfs_kfd_invalid) - break; + handle = kopenFileReader(vpxfn, 0); + if (!handle.isOpen()) + break; anim = Anim_Find(vpxfn); } @@ -287,7 +287,6 @@ int32_t Anim_Play(const char *fn) if (i) { OSD_Printf("Failed reading IVF file: %s\n", animvpx_read_ivf_header_errmsg[i]); - kclose(handle); return 0; } @@ -302,7 +301,6 @@ int32_t Anim_Play(const char *fn) { OSD_Printf("Error initializing VPX codec.\n"); animvpx_restore_glstate(); - kclose(handle); return 0; } @@ -411,7 +409,6 @@ int32_t Anim_Play(const char *fn) animvpx_print_stats(&codec); // - kclose(handle); animvpx_restore_glstate(); animvpx_uninit_codec(&codec); @@ -424,25 +421,17 @@ int32_t Anim_Play(const char *fn) #ifdef USE_OPENGL int32_t ogltexfiltermode = gltexfiltermode; #endif - buildvfs_kfd handle = kopen4load(fn, 0); + TArray buffer; + auto fr = kopenFileReader(fn, 0); - if (handle == buildvfs_kfd_invalid) - return 0; + if (!fr.isOpen()) + goto end_anim; - int32_t length = kfilelength(handle); - TArray buffer(length + 1, true); - - if (length <= 4) - { - OSD_Printf("Warning: skipping playback of empty ANM file \"%s\".\n", fn); - goto end_anim; - } + buffer = fr.ReadPadded(1); + fr.Close(); anim->animbuf = buffer.Data(); - kread(handle, anim->animbuf, length); - kclose(handle); - uint32_t firstfour; Bmemcpy(&firstfour, anim->animbuf, 4); @@ -454,7 +443,7 @@ int32_t Anim_Play(const char *fn) // "LPF " (.anm) if (firstfour != B_LITTLE32(0x2046504C) || - ANIM_LoadAnim(anim->animbuf, length) < 0 || + ANIM_LoadAnim(anim->animbuf, buffer.Size()-1) < 0 || (numframes = ANIM_NumFrames()) <= 0) { // XXX: ANM_LoadAnim() still checks less than the bare minimum, diff --git a/source/rr/src/anim.cpp b/source/rr/src/anim.cpp index 47e481afa..673fa5b5d 100644 --- a/source/rr/src/anim.cpp +++ b/source/rr/src/anim.cpp @@ -286,11 +286,11 @@ int32_t Anim_Play(const char *fn) break; dukeanim_t const * origanim = anim; - int32_t handle = -1; + FileReader handle; if (!Bstrcmp(dot, ".ivf")) { - handle = kopen4loadfrommod(fn, 0); - if (handle == -1) + handle = kopenFileReader(fn, 0); + if (!handle.isOpen()) break; } else @@ -308,9 +308,9 @@ int32_t Anim_Play(const char *fn) vpxfndot[3] = 'f'; vpxfndot[4] = '\0'; - handle = kopen4loadfrommod(vpxfn, 0); - if (handle == -1) - break; + handle = kopenFileReader(vpxfn, 0); + if (!handle.isOpen()) + break; anim = Anim_Find(vpxfn); } @@ -321,7 +321,6 @@ int32_t Anim_Play(const char *fn) if (i) { OSD_Printf("Failed reading IVF file: %s\n", animvpx_read_ivf_header_errmsg[i]); - kclose(handle); return 0; } @@ -336,7 +335,6 @@ int32_t Anim_Play(const char *fn) { OSD_Printf("Error initializing VPX codec.\n"); animvpx_restore_glstate(); - kclose(handle); return 0; } @@ -441,7 +439,6 @@ int32_t Anim_Play(const char *fn) animvpx_print_stats(&codec); // - kclose(handle); animvpx_restore_glstate(); animvpx_uninit_codec(&codec); @@ -454,15 +451,15 @@ int32_t Anim_Play(const char *fn) #ifdef USE_OPENGL int32_t ogltexfiltermode = gltexfiltermode; #endif - int32_t handle = kopen4load(fn, 0); + auto fr = kopenFileReader(fn, 0); - if (handle == -1) + if (!fr.isOpen()) return 0; - int32_t length = kfilelength(handle); - TArray buffer(length + 1, true); + auto buffer = fr.ReadPadded(1); + fr.Close(); - if (length <= 4) + if (buffer.Size() <= 5) { OSD_Printf("Warning: skipping playback of empty ANM file \"%s\".\n", fn); goto end_anim; @@ -472,9 +469,6 @@ int32_t Anim_Play(const char *fn) TileFiles.tileCreate(TILE_ANIM, 200, 320); - kread(handle, anim->animbuf, length); - kclose(handle); - uint32_t firstfour; Bmemcpy(&firstfour, anim->animbuf, 4); @@ -486,7 +480,7 @@ int32_t Anim_Play(const char *fn) // "LPF " (.anm) if (firstfour != B_LITTLE32(0x2046504C) || - ANIM_LoadAnim(anim->animbuf, length) < 0 || + ANIM_LoadAnim(anim->animbuf, buffer.Size()-1) < 0 || (numframes = ANIM_NumFrames()) <= 0) { // XXX: ANM_LoadAnim() still checks less than the bare minimum,