Skip to content

Commit

Permalink
feat(World): add load*Versioned APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
lmichaelis committed Apr 1, 2024
1 parent 4ee4438 commit 647927d
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 20 deletions.
3 changes: 3 additions & 0 deletions include/zenkit-capi/World.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ typedef struct {
ZKC_API ZkWorld* ZkWorld_load(ZkRead* buf);
ZKC_API ZkWorld* ZkWorld_loadPath(ZkString path);
ZKC_API ZkWorld* ZkWorld_loadVfs(ZkVfs* vfs, ZkString name);
ZKC_API ZkWorld* ZkWorld_loadVersioned(ZkRead* buf, ZkGameVersion version);
ZKC_API ZkWorld* ZkWorld_loadPathVersioned(ZkString path, ZkGameVersion version);
ZKC_API ZkWorld* ZkWorld_loadVfsVersioned(ZkVfs* vfs, ZkString name, ZkGameVersion version);
ZKC_API void ZkWorld_del(ZkWorld* slf);

ZKC_API ZkMesh const* ZkWorld_getMesh(ZkWorld const* slf);
Expand Down
72 changes: 52 additions & 20 deletions src/World.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,22 @@

ZkWorld* ZkWorld_load(ZkRead* buf) {
ZKC_TRACE_FN();
if (buf == nullptr) {
ZKC_LOG_WARN_NULL("ZkWorld"
"_load");
return nullptr;
}
ZKC_CHECK_NULL(buf);

try {
ZkWorld* slf = new ZkWorld(std::make_shared<zenkit::World>());
SLF->load(buf);
return slf;
} catch (std::exception const& exc) {
ZKC_LOG_ERROR("ZkWorld"
"_load() failed: %s",
exc.what());
ZKC_LOG_ERROR("ZkWorld_load() failed: %s", exc.what());
return nullptr;
}
}

ZkWorld* ZkWorld_loadPath(ZkString path) {
ZKC_TRACE_FN();
if (path == nullptr) {
ZKC_LOG_WARN_NULL("ZkWorld"
"_loadPath");
return nullptr;
}
ZKC_CHECK_NULL(path);

try {
auto buf = zenkit::Read::from(path);

Expand All @@ -38,23 +30,63 @@ ZkWorld* ZkWorld_loadPath(ZkString path) {

return slf;
} catch (std::exception const& exc) {
ZKC_LOG_ERROR("ZkWorld"
"_loadPath() failed: %s",
exc.what());
ZKC_LOG_ERROR("ZkWorld_loadPath() failed: %s", exc.what());
return nullptr;
}
}

ZkWorld* ZkWorld_loadVfs(ZkVfs* vfs, ZkString name) {
if (vfs == nullptr || name == nullptr) {
ZKC_LOG_WARN_NULL("ZkWorld"
"_loadVfs");
ZKC_TRACE_FN();
ZKC_CHECK_NULL(vfs, name);

auto node = vfs->find(name);
if (node == nullptr) return nullptr;
auto rd = node->open_read();
return ZkWorld_load(rd.get());
}

ZkWorld* ZkWorld_loadVersioned(ZkRead* buf, ZkGameVersion version) {
ZKC_TRACE_FN();
ZKC_CHECK_NULL(buf);

try {
ZkWorld* slf = new ZkWorld(std::make_shared<zenkit::World>());
SLF->load(buf, static_cast<zenkit::GameVersion>(version));
return slf;
} catch (std::exception const& exc) {
ZKC_LOG_ERROR("ZkWorld_load() failed: %s",
exc.what());
return nullptr;
}

}

ZkWorld* ZkWorld_loadPathVersioned(ZkString path, ZkGameVersion version) {
ZKC_TRACE_FN();
ZKC_CHECK_NULL(path);

try {
auto buf = zenkit::Read::from(path);

ZkWorld* slf = new ZkWorld(std::make_shared<zenkit::World>());
SLF->load(buf.get(), static_cast<zenkit::GameVersion>(version));

return slf;
} catch (std::exception const& exc) {
ZKC_LOG_ERROR("ZkWorld_loadPath() failed: %s",
exc.what());
return nullptr;
}
}

ZkWorld* ZkWorld_loadVfsVersioned(ZkVfs* vfs, ZkString name, ZkGameVersion version) {
ZKC_TRACE_FN();
ZKC_CHECK_NULL(vfs, name);

auto node = vfs->find(name);
if (node == nullptr) return nullptr;
auto rd = node->open_read();
return ZkWorld_load(rd.get());
return ZkWorld_loadVersioned(rd.get(), version);
}

ZKC_DELETER(ZkWorld);
Expand Down

0 comments on commit 647927d

Please sign in to comment.