Skip to content

Commit

Permalink
failsafe accessing assets
Browse files Browse the repository at this point in the history
  • Loading branch information
malytomas committed Aug 7, 2024
1 parent 2c069d6 commit f80b844
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 12 deletions.
3 changes: 1 addition & 2 deletions sources/include/cage-core/assetManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ namespace cage
load_(Scheme, assetName, textId, customScheme, std::move(customData));
}

// returns null if the asset is not yet loaded or has different scheme
// throws an exception if the asset failed to load
// returns null if the asset is not yet loaded, failed to load, or has different scheme
template<uint32 Scheme, class T>
Holder<T> get(uint32 assetName) const
{
Expand Down
7 changes: 1 addition & 6 deletions sources/libcore/assets/assetManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -743,13 +743,8 @@ namespace cage
if (it == publicIndex.end())
return {}; // not found
const auto &a = it->second;
if (a->scheme != scheme)
if (a->scheme != scheme || a->failed)
return {}; // different scheme
if (a->failed)
{
CAGE_LOG_THROW(Stringizer() + "asset real name: " + realName + ", text name: " + a->textName);
CAGE_THROW_ERROR(Exception, "accessing asset that failed to load");
}
CAGE_ASSERT(a->ref);
CAGE_ASSERT(+a->ref != (void *)1);
return a->ref.share();
Expand Down
8 changes: 4 additions & 4 deletions sources/test-core/assetManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ void testAssetManager()
waitProcessing(man);
detail::globalBreakpointOverride(true);
}
CAGE_TEST_THROWN((man->get<AssetSchemeIndexRaw, PointerRange<const char>>(10)));
CAGE_TEST((!man->get<AssetSchemeIndexRaw, PointerRange<const char>>(10)));
man->unload(10);
man->waitTillEmpty();
}
Expand All @@ -482,7 +482,7 @@ void testAssetManager()
waitProcessing(man);
detail::globalBreakpointOverride(true);
}
CAGE_TEST_THROWN((man->get<AssetSchemeIndexCounter, AssetCounter>(5000)));
CAGE_TEST((!man->get<AssetSchemeIndexCounter, AssetCounter>(5000)));
man->unload(5000);
man->waitTillEmpty();
}
Expand All @@ -502,7 +502,7 @@ void testAssetManager()
CAGE_TEST(AssetCounter::counter == 2);
CAGE_TEST((man->get<AssetSchemeIndexCounter, AssetCounter>(10)));
CAGE_TEST((man->get<AssetSchemeIndexCounter, AssetCounter>(20)));
CAGE_TEST_THROWN((man->get<AssetSchemeIndexCounter, AssetCounter>(5000)));
CAGE_TEST((!man->get<AssetSchemeIndexCounter, AssetCounter>(5000)));
man->unload(5000);
waitProcessing(man);
CAGE_TEST(AssetCounter::counter == 0);
Expand All @@ -524,7 +524,7 @@ void testAssetManager()
CAGE_TEST(AssetCounter::counter == 2);
CAGE_TEST((man->get<AssetSchemeIndexCounter, AssetCounter>(10)));
CAGE_TEST((man->get<AssetSchemeIndexCounter, AssetCounter>(20)));
CAGE_TEST_THROWN((man->get<AssetSchemeIndexCounter, AssetCounter>(5000)));
CAGE_TEST((!man->get<AssetSchemeIndexCounter, AssetCounter>(5000)));
man->unload(10);
waitProcessing(man);
CAGE_TEST(AssetCounter::counter == 0);
Expand Down

0 comments on commit f80b844

Please sign in to comment.