添加 FSaveStructPtr 管理 SaveStruct
This commit is contained in:
parent
df3e1bdfa5
commit
9e68d7f7e8
@ -28,6 +28,18 @@ void UAutoSaveSubsystem::GetSaveStructInfosWithoutData(TArray<FSaveStructInfo>&
|
||||
}
|
||||
}
|
||||
|
||||
int32 UAutoSaveSubsystem::GetIdleThreadNum() const
|
||||
{
|
||||
int32 Result = 0;
|
||||
|
||||
for (const TUniquePtr<FAsyncTask<FStructLoadOrSaveTask>>& Task : TaskThreads)
|
||||
{
|
||||
if (!Task) ++Result;
|
||||
}
|
||||
|
||||
return Result;
|
||||
}
|
||||
|
||||
FSaveStruct * UAutoSaveSubsystem::AddSaveStructRef(const FString& Filename, UScriptStruct * ScriptStruct, FSaveStructLoadDelegate OnLoaded)
|
||||
{
|
||||
if (StructInfos.Contains(Filename))
|
||||
|
@ -57,6 +57,7 @@ class AUTOSAVE_API UAutoSaveSubsystem : public UGameInstanceSubsystem, public FT
|
||||
GENERATED_BODY()
|
||||
|
||||
friend class UAutoSaveBlueprintLibrary;
|
||||
template<typename SaveStructType> friend class FSaveStructPtr;
|
||||
|
||||
public:
|
||||
|
||||
@ -71,6 +72,9 @@ public:
|
||||
UFUNCTION(BlueprintCallable, Category = "AutoSave")
|
||||
void GetSaveStructInfosWithoutData(TArray<FSaveStructInfo>& OutSaveStructInfos) const;
|
||||
|
||||
UFUNCTION(BlueprintPure, Category = "AutoSave")
|
||||
int32 GetIdleThreadNum() const;
|
||||
|
||||
FSaveStruct* AddSaveStructRef(const FString& Filename, UScriptStruct* ScriptStruct = nullptr, FSaveStructLoadDelegate OnLoaded = FSaveStructLoadDelegate());
|
||||
|
||||
void RemoveSaveStructRef(const FString& Filename);
|
||||
|
@ -0,0 +1,79 @@
|
||||
#pragma once
|
||||
|
||||
#include "CoreMinimal.h"
|
||||
#include "AutoSaveSubsystem.h"
|
||||
#include "Templates/UnrealTemplate.h"
|
||||
|
||||
struct FSaveStructInfo;
|
||||
|
||||
class UAutoSaveSubsystem;
|
||||
|
||||
template<typename SaveStructType>
|
||||
class FSaveStructPtr : public FNoncopyable
|
||||
{
|
||||
public:
|
||||
|
||||
FORCEINLINE FSaveStructPtr()
|
||||
: AutoSaveSubsystem(nullptr)
|
||||
, Info(nullptr)
|
||||
{
|
||||
}
|
||||
|
||||
FORCEINLINE FSaveStructPtr(UAutoSaveSubsystem* InAutoSaveSubsystem, const FString& Filename, FSaveStructLoadDelegate OnLoaded = FSaveStructLoadDelegate())
|
||||
: AutoSaveSubsystem(InAutoSaveSubsystem)
|
||||
, Info(nullptr)
|
||||
{
|
||||
if (AutoSaveSubsystem->AddSaveStructRef(Filename, SaveStructType::StaticStruct(), OnLoaded))
|
||||
{
|
||||
Info = AutoSaveSubsystem->StructInfos[Filename].Get();
|
||||
}
|
||||
}
|
||||
|
||||
FORCEINLINE ~FSaveStructPtr()
|
||||
{
|
||||
if (Info)
|
||||
{
|
||||
AutoSaveSubsystem->RemoveSaveStructRef(Info->Filename);
|
||||
}
|
||||
}
|
||||
|
||||
FORCEINLINE SaveStructType* Get() const
|
||||
{
|
||||
return Info ? (SaveStructType*)Info->Data.GetData() : nullptr;
|
||||
}
|
||||
|
||||
FORCEINLINE explicit operator bool() const
|
||||
{
|
||||
return Info != nullptr;
|
||||
}
|
||||
|
||||
FORCEINLINE const bool IsValid() const
|
||||
{
|
||||
return Info != nullptr;
|
||||
}
|
||||
|
||||
FORCEINLINE const bool IsLoaded() const
|
||||
{
|
||||
check(IsValid());
|
||||
return Info->State == ESaveStructState::Idle || Info->State == ESaveStructState::Saving;
|
||||
}
|
||||
|
||||
FORCEINLINE SaveStructType& operator*() const
|
||||
{
|
||||
check(IsValid());
|
||||
return *Get();
|
||||
}
|
||||
|
||||
FORCEINLINE SaveStructType* operator->() const
|
||||
{
|
||||
check(IsValid());
|
||||
return Get();
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
UAutoSaveSubsystem* AutoSaveSubsystem;
|
||||
|
||||
FSaveStructInfo* Info;
|
||||
|
||||
};
|
Reference in New Issue
Block a user