From 9e68d7f7e82796a2066a741a72ed7696442efe86 Mon Sep 17 00:00:00 2001 From: _Redstone_c_ <2824517378@qq.com> Date: Sun, 13 Dec 2020 11:20:57 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20FSaveStructPtr=20=E7=AE=A1?= =?UTF-8?q?=E7=90=86=20SaveStruct?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Source/AutoSave/Private/AutoSaveSubsystem.cpp | 12 +++ Source/AutoSave/Public/AutoSaveSubsystem.h | 4 + Source/AutoSave/Public/SaveStruct.h | 79 +++++++++++++++++++ 3 files changed, 95 insertions(+) diff --git a/Source/AutoSave/Private/AutoSaveSubsystem.cpp b/Source/AutoSave/Private/AutoSaveSubsystem.cpp index 1e55fab..39fb6d6 100644 --- a/Source/AutoSave/Private/AutoSaveSubsystem.cpp +++ b/Source/AutoSave/Private/AutoSaveSubsystem.cpp @@ -28,6 +28,18 @@ void UAutoSaveSubsystem::GetSaveStructInfosWithoutData(TArray& } } +int32 UAutoSaveSubsystem::GetIdleThreadNum() const +{ + int32 Result = 0; + + for (const TUniquePtr>& Task : TaskThreads) + { + if (!Task) ++Result; + } + + return Result; +} + FSaveStruct * UAutoSaveSubsystem::AddSaveStructRef(const FString& Filename, UScriptStruct * ScriptStruct, FSaveStructLoadDelegate OnLoaded) { if (StructInfos.Contains(Filename)) diff --git a/Source/AutoSave/Public/AutoSaveSubsystem.h b/Source/AutoSave/Public/AutoSaveSubsystem.h index b5e2de9..5b023d4 100644 --- a/Source/AutoSave/Public/AutoSaveSubsystem.h +++ b/Source/AutoSave/Public/AutoSaveSubsystem.h @@ -57,6 +57,7 @@ class AUTOSAVE_API UAutoSaveSubsystem : public UGameInstanceSubsystem, public FT GENERATED_BODY() friend class UAutoSaveBlueprintLibrary; + template friend class FSaveStructPtr; public: @@ -71,6 +72,9 @@ public: UFUNCTION(BlueprintCallable, Category = "AutoSave") void GetSaveStructInfosWithoutData(TArray& 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); diff --git a/Source/AutoSave/Public/SaveStruct.h b/Source/AutoSave/Public/SaveStruct.h index e69de29..33cca7f 100644 --- a/Source/AutoSave/Public/SaveStruct.h +++ b/Source/AutoSave/Public/SaveStruct.h @@ -0,0 +1,79 @@ +#pragma once + +#include "CoreMinimal.h" +#include "AutoSaveSubsystem.h" +#include "Templates/UnrealTemplate.h" + +struct FSaveStructInfo; + +class UAutoSaveSubsystem; + +template +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; + +};