|
|
|
|
| #pragma once
|
|
|
| #include "Containers/Ticker.h"
|
| #include "UObject/SoftObjectPtr.h"
|
|
|
| #define UE_API ASYNCMIXIN_API
|
|
|
| class FAsyncCondition;
|
| class FName;
|
| class UPrimaryDataAsset;
|
| struct FPrimaryAssetId;
|
| struct FStreamableHandle;
|
| template <class TClass> class TSubclassOf;
|
|
|
| DECLARE_DELEGATE_OneParam(FStreamableHandleDelegate, TSharedPtr<FStreamableHandle>)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| class FAsyncMixin : public FNoncopyable
|
| {
|
| protected:
|
| UE_API FAsyncMixin();
|
|
|
| public:
|
| UE_API virtual ~FAsyncMixin();
|
|
|
| protected:
|
|
|
| virtual void OnStartedLoading() { }
|
|
|
| virtual void OnFinishedLoading() { }
|
|
|
| protected:
|
|
|
| template<typename T = UObject>
|
| void AsyncLoad(TSoftClassPtr<T> SoftClass, TFunction<void()>&& Callback)
|
| {
|
| AsyncLoad(SoftClass.ToSoftObjectPath(), FSimpleDelegate::CreateLambda(MoveTemp(Callback)));
|
| }
|
|
|
|
|
| template<typename T = UObject>
|
| void AsyncLoad(TSoftClassPtr<T> SoftClass, TFunction<void(TSubclassOf<T>)>&& Callback)
|
| {
|
| AsyncLoad(SoftClass.ToSoftObjectPath(),
|
| FSimpleDelegate::CreateLambda([SoftClass, UserCallback = MoveTemp(Callback)]() mutable {
|
| UserCallback(SoftClass.Get());
|
| })
|
| );
|
| }
|
|
|
|
|
| template<typename T = UObject>
|
| void AsyncLoad(TSoftClassPtr<T> SoftClass, const FSimpleDelegate& Callback = FSimpleDelegate())
|
| {
|
| AsyncLoad(SoftClass.ToSoftObjectPath(), Callback);
|
| }
|
|
|
|
|
| template<typename T = UObject>
|
| void AsyncLoad(TSoftObjectPtr<T> SoftObject, TFunction<void()>&& Callback)
|
| {
|
| AsyncLoad(SoftObject.ToSoftObjectPath(), FSimpleDelegate::CreateLambda(MoveTemp(Callback)));
|
| }
|
|
|
|
|
| template<typename T = UObject>
|
| void AsyncLoad(TSoftObjectPtr<T> SoftObject, TFunction<void(T*)>&& Callback)
|
| {
|
| AsyncLoad(SoftObject.ToSoftObjectPath(),
|
| FSimpleDelegate::CreateLambda([SoftObject, UserCallback = MoveTemp(Callback)]() mutable {
|
| UserCallback(SoftObject.Get());
|
| })
|
| );
|
| }
|
|
|
|
|
| template<typename T = UObject>
|
| void AsyncLoad(TSoftObjectPtr<T> SoftObject, const FSimpleDelegate& Callback = FSimpleDelegate())
|
| {
|
| AsyncLoad(SoftObject.ToSoftObjectPath(), Callback);
|
| }
|
|
|
|
|
| UE_API void AsyncLoad(FSoftObjectPath SoftObjectPath, const FSimpleDelegate& Callback = FSimpleDelegate());
|
|
|
|
|
| void AsyncLoad(const TArray<FSoftObjectPath>& SoftObjectPaths, TFunction<void()>&& Callback)
|
| {
|
| AsyncLoad(SoftObjectPaths, FSimpleDelegate::CreateLambda(MoveTemp(Callback)));
|
| }
|
|
|
|
|
| UE_API void AsyncLoad(const TArray<FSoftObjectPath>& SoftObjectPaths, const FSimpleDelegate& Callback = FSimpleDelegate());
|
|
|
|
|
| template<typename T = UPrimaryDataAsset>
|
| void AsyncPreloadPrimaryAssetsAndBundles(const TArray<T*>& Assets, const TArray<FName>& LoadBundles, const FSimpleDelegate& Callback = FSimpleDelegate())
|
| {
|
| TArray<FPrimaryAssetId> PrimaryAssetIds;
|
| for (const T* Item : Assets)
|
| {
|
| PrimaryAssetIds.Add(Item);
|
| }
|
|
|
| AsyncPreloadPrimaryAssetsAndBundles(PrimaryAssetIds, LoadBundles, Callback);
|
| }
|
|
|
|
|
| void AsyncPreloadPrimaryAssetsAndBundles(const TArray<FPrimaryAssetId>& AssetIds, const TArray<FName>& LoadBundles, TFunction<void()>&& Callback)
|
| {
|
| AsyncPreloadPrimaryAssetsAndBundles(AssetIds, LoadBundles, FSimpleDelegate::CreateLambda(MoveTemp(Callback)));
|
| }
|
|
|
|
|
| UE_API void AsyncPreloadPrimaryAssetsAndBundles(const TArray<FPrimaryAssetId>& AssetIds, const TArray<FName>& LoadBundles, const FSimpleDelegate& Callback = FSimpleDelegate());
|
|
|
|
|
| UE_API void AsyncCondition(TSharedRef<FAsyncCondition> Condition, const FSimpleDelegate& Callback = FSimpleDelegate());
|
|
|
| |
| |
| |
| |
|
|
| void AsyncEvent(TFunction<void()>&& Callback)
|
| {
|
| AsyncEvent(FSimpleDelegate::CreateLambda(MoveTemp(Callback)));
|
| }
|
|
|
| |
| |
| |
| |
|
|
| UE_API void AsyncEvent(const FSimpleDelegate& Callback);
|
|
|
|
|
| UE_API void StartAsyncLoading();
|
|
|
|
|
| UE_API void CancelAsyncLoading();
|
|
|
|
|
| UE_API bool IsAsyncLoadingInProgress() const;
|
|
|
| private:
|
| |
| |
| |
|
|
| class FLoadingState : public TSharedFromThis<FLoadingState>
|
| {
|
| public:
|
| FLoadingState(FAsyncMixin& InOwner);
|
| virtual ~FLoadingState();
|
|
|
|
|
| void Start();
|
|
|
|
|
| void CancelAndDestroy();
|
|
|
| void AsyncLoad(FSoftObjectPath SoftObject, const FSimpleDelegate& DelegateToCall);
|
| void AsyncLoad(const TArray<FSoftObjectPath>& SoftObjectPaths, const FSimpleDelegate& DelegateToCall);
|
| void AsyncPreloadPrimaryAssetsAndBundles(const TArray<FPrimaryAssetId>& PrimaryAssetIds, const TArray<FName>& LoadBundles, const FSimpleDelegate& DelegateToCall);
|
| void AsyncCondition(TSharedRef<FAsyncCondition> Condition, const FSimpleDelegate& Callback);
|
| void AsyncEvent(const FSimpleDelegate& Callback);
|
|
|
| bool IsLoadingComplete() const { return !IsLoadingInProgress(); }
|
| bool IsLoadingInProgress() const;
|
| bool IsLoadingInProgressOrPending() const;
|
| bool IsPendingDestroy() const;
|
|
|
| private:
|
| void CancelOnly(bool bDestroying);
|
| void CancelStartTimer();
|
| void TryScheduleStart();
|
| void TryCompleteAsyncLoading();
|
| void CompleteAsyncLoading();
|
|
|
| private:
|
| void RequestDestroyThisMemory();
|
| void CancelDestroyThisMemory(bool bDestroying);
|
|
|
|
|
| FAsyncMixin& OwnerRef;
|
|
|
| |
| |
| |
|
|
| bool bPreloadedBundles = false;
|
|
|
| class FAsyncStep
|
| {
|
| public:
|
| FAsyncStep(const FSimpleDelegate& InUserCallback);
|
| FAsyncStep(const FSimpleDelegate& InUserCallback, const TSharedPtr<FStreamableHandle>& InStreamingHandle);
|
| FAsyncStep(const FSimpleDelegate& InUserCallback, const TSharedPtr<FAsyncCondition>& InCondition);
|
|
|
| ~FAsyncStep();
|
|
|
| void ExecuteUserCallback();
|
|
|
| bool IsLoadingInProgress() const
|
| {
|
| return !IsComplete();
|
| }
|
|
|
| bool IsComplete() const;
|
| void Cancel();
|
|
|
| bool BindCompleteDelegate(const FSimpleDelegate& NewDelegate);
|
| bool IsCompleteDelegateBound() const;
|
|
|
| private:
|
| FSimpleDelegate UserCallback;
|
| bool bIsCompletionDelegateBound = false;
|
|
|
|
|
| TSharedPtr<FStreamableHandle> StreamingHandle;
|
| TSharedPtr<FAsyncCondition> Condition;
|
| };
|
|
|
| bool bHasStarted = false;
|
|
|
| int32 CurrentAsyncStep = 0;
|
| TArray<TUniquePtr<FAsyncStep>> AsyncSteps;
|
| TArray<TUniquePtr<FAsyncStep>> AsyncStepsPendingDestruction;
|
|
|
| FTSTicker::FDelegateHandle StartTimerDelegate;
|
| FTSTicker::FDelegateHandle DestroyMemoryDelegate;
|
| };
|
|
|
| UE_API const FLoadingState& GetLoadingStateConst() const;
|
|
|
| UE_API FLoadingState& GetLoadingState();
|
|
|
| UE_API bool HasLoadingState() const;
|
|
|
| UE_API bool IsLoadingInProgressOrPending() const;
|
|
|
| private:
|
| static UE_API TMap<FAsyncMixin*, TSharedRef<FLoadingState>> Loading;
|
| };
|
|
|
| |
| |
| |
| |
| |
| |
|
|
| class FAsyncScope : public FAsyncMixin
|
| {
|
| public:
|
| using FAsyncMixin::AsyncLoad;
|
|
|
| using FAsyncMixin::AsyncPreloadPrimaryAssetsAndBundles;
|
|
|
| using FAsyncMixin::AsyncCondition;
|
|
|
| using FAsyncMixin::AsyncEvent;
|
|
|
| using FAsyncMixin::CancelAsyncLoading;
|
|
|
| using FAsyncMixin::StartAsyncLoading;
|
|
|
| using FAsyncMixin::IsAsyncLoadingInProgress;
|
| };
|
|
|
|
|
|
|
|
|
| enum class EAsyncConditionResult : uint8
|
| {
|
| TryAgain,
|
| Complete
|
| };
|
|
|
| DECLARE_DELEGATE_RetVal(EAsyncConditionResult, FAsyncConditionDelegate);
|
|
|
| |
| |
|
|
| class FAsyncCondition : public TSharedFromThis<FAsyncCondition>
|
| {
|
| public:
|
| FAsyncCondition(const FAsyncConditionDelegate& Condition);
|
| FAsyncCondition(TFunction<EAsyncConditionResult()>&& Condition);
|
| virtual ~FAsyncCondition();
|
|
|
| protected:
|
| bool IsComplete() const;
|
| bool BindCompleteDelegate(const FSimpleDelegate& NewDelegate);
|
|
|
| private:
|
| bool TryToContinue(float DeltaTime);
|
|
|
| FTSTicker::FDelegateHandle RepeatHandle;
|
| FAsyncConditionDelegate UserCondition;
|
| FSimpleDelegate CompletionDelegate;
|
|
|
| friend FAsyncMixin;
|
| };
|
|
|
| #undef UE_API
|
|
|