Skip to content

Commit

Permalink
feat(CutsceneLibrary) Introduce save APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
PolyMeilex authored and lmichaelis committed Apr 19, 2024
1 parent 82d28c4 commit 31536b3
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 1 deletion.
9 changes: 9 additions & 0 deletions include/zenkit-capi/Archive.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright © 2023. GothicKit Contributors
// SPDX-License-Identifier: MIT
#pragma once

typedef enum {
ZkArchiveFormat_BINARY = 0,
ZkArchiveFormat_BINSAFE = 1,
ZkArchiveFormat_ASCII = 2,
} ZkArchiveFormat;
5 changes: 5 additions & 0 deletions include/zenkit-capi/CutsceneLibrary.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright © 2023. GothicKit Contributors
// SPDX-License-Identifier: MIT
#pragma once
#include "Archive.h"
#include "Library.h"
#include "Stream.h"
#include "Vfs.h"
Expand All @@ -21,6 +22,10 @@ typedef ZkBool (*ZkCutsceneBlockEnumerator)(void* ctx, ZkCutsceneBlock const* bl
ZKC_API ZkCutsceneLibrary* ZkCutsceneLibrary_load(ZkRead* buf);
ZKC_API ZkCutsceneLibrary* ZkCutsceneLibrary_loadPath(ZkString path);
ZKC_API ZkCutsceneLibrary* ZkCutsceneLibrary_loadVfs(ZkVfs const* vfs, ZkString string);

ZKC_API void ZkCutsceneLibrary_save(ZkCutsceneLibrary* slf, ZkWrite* buf, ZkArchiveFormat fmt);
ZKC_API void ZkCutsceneLibrary_savePath(ZkCutsceneLibrary* slf, ZkString path, ZkArchiveFormat fmt);

ZKC_API void ZkCutsceneLibrary_del(ZkCutsceneLibrary* slf);

ZKC_API ZkSize ZkCutsceneLibrary_getBlockCount(ZkCutsceneLibrary const* slf);
Expand Down
2 changes: 2 additions & 0 deletions src/CutsceneLibrary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
ZKC_LOADER(ZkCutsceneLibrary);
ZKC_PATH_LOADER(ZkCutsceneLibrary);
ZKC_VFS_LOADER(ZkCutsceneLibrary);
ZKC_SAVER(ZkCutsceneLibrary);
ZKC_PATH_SAVER(ZkCutsceneLibrary);
ZKC_DELETER(ZkCutsceneLibrary);

ZkSize ZkCutsceneLibrary_getBlockCount(ZkCutsceneLibrary const* slf) {
Expand Down
34 changes: 33 additions & 1 deletion src/Internal.hh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: MIT
#pragma once
#include <zenkit/Archive.hh>
#include "zenkit-capi/Archive.h"

#define ZKC_LOADER(cls) \
cls* cls##_load(ZkRead* buf) { \
Expand Down Expand Up @@ -53,6 +54,37 @@
return cls##_load(rd.get()); \
}

#define ZKC_SAVER(cls) \
void cls##_save(cls* slf, ZkWrite* buf, ZkArchiveFormat fmt) { \
if (buf == nullptr) { \
ZKC_LOG_WARN_NULL(#cls "_save"); \
return; \
} \
\
try { \
slf->save(buf, static_cast<zenkit::ArchiveFormat>(fmt)); \
} catch (std::exception const& exc) { \
ZKC_LOG_ERROR(#cls "_save() failed: %s", exc.what()); \
return; \
} \
}

#define ZKC_PATH_SAVER(cls) \
void cls##_savePath(cls* slf, ZkString path, ZkArchiveFormat fmt) { \
if (path == nullptr) { \
ZKC_LOG_WARN_NULL(#cls "_savePath"); \
return; \
} \
\
try { \
auto buf = zenkit::Write::to(path); \
slf->save(buf.get(), static_cast<zenkit::ArchiveFormat>(fmt)); \
} catch (std::exception const& exc) { \
ZKC_LOG_ERROR(#cls "_savePath() failed: %s", exc.what()); \
return; \
} \
}

#define ZKC_DELETER(cls) \
void cls##_del(cls* slf) { \
ZKC_TRACE_FN(); \
Expand Down Expand Up @@ -170,4 +202,4 @@ inline bool zk_any_nullptr(T*... args) {
} while (false)

#define ZKC_TRACE_FN() ZKC_LOG_TRACE("%s()", __func__)
#define SLF (*slf)
#define SLF (*slf)

0 comments on commit 31536b3

Please sign in to comment.