Minor improvements

This commit is contained in:
Wojtek Figat
2024-04-22 18:10:58 +02:00
parent e795a8b037
commit 32b15f90ab
9 changed files with 36 additions and 91 deletions

View File

@@ -587,8 +587,8 @@ bool Asset::IsInternalType() const
bool Asset::onLoad(LoadAssetTask* task)
{
// It may fail when task is cancelled and new one is created later (don't crash but just end with an error)
if (task->Asset.Get() != this || Platform::AtomicRead(&_loadingTask) == 0)
// It may fail when task is cancelled and new one was created later (don't crash but just end with an error)
return true;
Locker.Lock();
@@ -601,7 +601,6 @@ bool Asset::onLoad(LoadAssetTask* task)
}
const bool isLoaded = result == LoadResult::Ok;
const bool failed = !isLoaded;
LoadState state = LoadState::Loaded;
Platform::AtomicStore(&_loadState, (int64)(isLoaded ? LoadState::Loaded : LoadState::LoadFailed));
if (failed)
{

View File

@@ -103,7 +103,7 @@ public:
public:
/// <summary>
/// Gets the path to the asset storage file. In Editor it reflects the actual file, in cooked Game, it fakes the Editor path to be informative for developers.
/// Gets the path to the asset storage file. In Editor, it reflects the actual file, in cooked Game, it fakes the Editor path to be informative for developers.
/// </summary>
API_PROPERTY() virtual const String& GetPath() const = 0;

View File

@@ -507,8 +507,7 @@ public:
/// </summary>
/// <param name="asset">The asset.</param>
InitAssetTask(BinaryAsset* asset)
: ContentLoadTask(Type::Custom)
, _asset(asset)
: _asset(asset)
, _dataLock(asset->Storage->Lock())
{
}
@@ -527,8 +526,6 @@ protected:
AssetReference<BinaryAsset> ref = _asset.Get();
if (ref == nullptr)
return Result::MissingReferences;
// Prepare
auto storage = ref->Storage;
auto factory = (BinaryAssetFactoryBase*)Content::GetAssetFactory(ref->GetTypeName());
ASSERT(factory);
@@ -548,7 +545,6 @@ protected:
_dataLock.Release();
_asset = nullptr;
// Base
ContentLoadTask::OnEnd();
}
};

View File

@@ -15,52 +15,11 @@ class ContentLoadTask : public Task
friend LoadingThread;
public:
/// <summary>
/// Describes work type
/// </summary>
DECLARE_ENUM_3(Type, Custom, LoadAsset, LoadAssetData);
/// <summary>
/// Describes work result value
/// </summary>
DECLARE_ENUM_5(Result, Ok, AssetLoadError, MissingReferences, LoadDataError, TaskFailed);
private:
/// <summary>
/// Task type
/// </summary>
Type _type;
protected:
/// <summary>
/// Initializes a new instance of the <see cref="ContentLoadTask"/> class.
/// </summary>
/// <param name="type">The task type.</param>
ContentLoadTask(const Type type)
: _type(type)
{
}
public:
/// <summary>
/// Gets a task type.
/// </summary>
FORCE_INLINE Type GetType() const
{
return _type;
}
public:
/// <summary>
/// Checks if async task is loading given asset resource
/// </summary>
/// <param name="asset">Target asset to check</param>
/// <returns>True if is loading that asset, otherwise false</returns>
bool IsLoading(Asset* asset) const
{
return _type == Type::LoadAsset && HasReference((Object*)asset);
}
protected:
virtual Result run() = 0;

View File

@@ -4,6 +4,7 @@
#include "ContentLoadTask.h"
#include "Engine/Core/Log.h"
#include "Engine/Core/Math/Math.h"
#include "Engine/Core/Collections/Array.h"
#include "Engine/Platform/CPUInfo.h"
#include "Engine/Platform/Thread.h"
#include "Engine/Platform/ConditionVariable.h"
@@ -212,7 +213,7 @@ void ContentLoadingManagerService::Dispose()
String ContentLoadTask::ToString() const
{
return String::Format(TEXT("Content Load Task {0} ({1})"), ToString(GetType()), (int32)GetState());
return String::Format(TEXT("Content Load Task ({})"), (int32)GetState());
}
void ContentLoadTask::Enqueue()

View File

@@ -15,28 +15,24 @@
class LoadAssetDataTask : public ContentLoadTask
{
private:
WeakAssetReference<BinaryAsset> _asset; // Don't keep ref to the asset (so it can be unloaded if none using it, task will fail then)
AssetChunksFlag _chunks;
FlaxStorage::LockData _dataLock;
public:
/// <summary>
/// Initializes a new instance of the <see cref="LoadAssetDataTask"/> class.
/// </summary>
/// <param name="asset">The asset to load.</param>
/// <param name="chunks">The chunks to load.</param>
LoadAssetDataTask(BinaryAsset* asset, AssetChunksFlag chunks)
: ContentLoadTask(Type::LoadAssetData)
, _asset(asset)
: _asset(asset)
, _chunks(chunks)
, _dataLock(asset->Storage->Lock())
{
}
public:
// [ContentLoadTask]
bool HasReference(Object* obj) const override
{
@@ -44,7 +40,6 @@ public:
}
protected:
// [ContentLoadTask]
Result run() override
{

View File

@@ -15,38 +15,35 @@
class LoadAssetTask : public ContentLoadTask
{
public:
/// <summary>
/// Initializes a new instance of the <see cref="LoadAssetTask"/> class.
/// </summary>
/// <param name="asset">The asset to load.</param>
LoadAssetTask(Asset* asset)
: ContentLoadTask(Type::LoadAsset)
, Asset(asset)
: Asset(asset)
{
}
~LoadAssetTask()
{
if (Asset)
auto asset = Asset.Get();
if (asset)
{
Asset->Locker.Lock();
if (Platform::AtomicRead(&Asset->_loadingTask) == (intptr)this)
asset->Locker.Lock();
if (Platform::AtomicRead(&asset->_loadingTask) == (intptr)this)
{
Platform::AtomicStore(&Asset->_loadState, (int64)Asset::LoadState::LoadFailed);
Platform::AtomicStore(&Asset->_loadingTask, 0);
Platform::AtomicStore(&asset->_loadState, (int64)Asset::LoadState::LoadFailed);
Platform::AtomicStore(&asset->_loadingTask, 0);
LOG(Error, "Loading asset \'{0}\' result: {1}.", ToString(), ToString(Result::TaskFailed));
}
Asset->Locker.Unlock();
asset->Locker.Unlock();
}
}
public:
WeakAssetReference<Asset> Asset;
public:
// [ContentLoadTask]
bool HasReference(Object* obj) const override
{
@@ -54,7 +51,6 @@ public:
}
protected:
// [ContentLoadTask]
Result run() override
{
@@ -68,32 +64,36 @@ protected:
// Call loading
if (ref->onLoad(this))
return Result::AssetLoadError;
return Result::Ok;
}
void OnFail() override
{
if (Asset)
auto asset = Asset.Get();
if (asset)
{
Asset->Locker.Lock();
if (Platform::AtomicRead(&Asset->_loadingTask) == (intptr)this)
Platform::AtomicStore(&Asset->_loadingTask, 0);
Asset->Locker.Unlock();
Asset = nullptr;
asset->Locker.Lock();
if (Platform::AtomicRead(&asset->_loadingTask) == (intptr)this)
Platform::AtomicStore(&asset->_loadingTask, 0);
asset->Locker.Unlock();
}
// Base
ContentLoadTask::OnFail();
}
void OnEnd() override
{
if (Asset)
auto asset = Asset.Get();
if (asset)
{
Asset->Locker.Lock();
if (Platform::AtomicRead(&Asset->_loadingTask) == (intptr)this)
Platform::AtomicStore(&Asset->_loadingTask, 0);
Asset->Locker.Unlock();
Asset = nullptr;
asset->Locker.Lock();
if (Platform::AtomicRead(&asset->_loadingTask) == (intptr)this)
Platform::AtomicStore(&asset->_loadingTask, 0);
asset->Locker.Unlock();
asset = nullptr;
}
// Base