Payback 0.0.1
CK Graduation Project
로딩중...
검색중...
일치하는것 없음
SnapshotSubsystem.h
이 파일의 문서화 페이지로 가기
1// Fill out your copyright notice in the Description page of Project Settings.
2
3#pragma once
4
5#include "CK_UE.h"
7#include "Engine/GameInstance.h"
8#include "Engine/World.h"
9#include "Subsystems/GameInstanceSubsystem.h"
10#include "Templates/UnrealTypeTraits.h"
11#include "SnapshotSubsystem.generated.h"
12
13
14UCLASS()
15class CK_UE_API USnapshotSubsystem : public UGameInstanceSubsystem
16{
17 GENERATED_BODY()
18
19public:
21
22 virtual void Deinitialize() override;
23
24public:
25 bool ProcessCommand(UCommandBase* Command);
26 void RecordCommand(UCommandBase* Command);
27 void MarkRollbackPoint(FName RollbackPointID);
28 bool RollbackTo(FName RollbackPointID);
29 void ResetCommands();
30
31#if WITH_EDITOR
32 void PrintCommandStack();
33#endif
34
35public:
36 template <typename TCommandType, typename TInitializeCommand>
37 TCommandType* ProcessNewCommand(TInitializeCommand InitializeCommand);
38
39 template <typename TCommandType, typename TInitializeCommand>
40 static TCommandType* ProcessNewCommand(UObject* WorldContextObject, TInitializeCommand InitializeCommand);
41
42public:
43 FORCEINLINE bool IsRollingBack() const
44 {
45 return RollbackDepth > 0;
46 }
47
48private:
49 UPROPERTY(Transient)
50 TArray<TObjectPtr<UCommandBase>> CommandStack;
51
52private:
53 TMap<FName, int32> RollbackPointByID;
54 int32 RollbackDepth = 0;
55};
56
57
58template <typename TCommandType, typename TInitializeCommand>
59TCommandType* USnapshotSubsystem::ProcessNewCommand(TInitializeCommand InitializeCommand)
60{
61 static_assert(TIsDerivedFrom<TCommandType, UCommandBase>::Value, "TCommandType must derive from UCommandBase.");
62
63 if (IsRollingBack())
64 {
65 return nullptr;
66 }
67
68 TCommandType* Command = NewObject<TCommandType>(this);
69
70 if (Command == nullptr)
71 {
72 return nullptr;
73 }
74
75 InitializeCommand(Command);
76
77 if (ProcessCommand(Command) == false)
78 {
79 return nullptr;
80 }
81 else
82 {
83 return Command;
84 }
85}
86
87
88template <typename TCommandType, typename TInitializeCommand>
89TCommandType* USnapshotSubsystem::ProcessNewCommand(UObject* WorldContextObject, TInitializeCommand InitializeCommand)
90{
91 static_assert(TIsDerivedFrom<TCommandType, UCommandBase>::Value, "TCommandType must derive from UCommandBase.");
92
93 if (WorldContextObject == nullptr)
94 {
95 return nullptr;
96 }
97
98 UGameInstance* GameInstance = Cast<UGameInstance>(WorldContextObject);
99
100 if (GameInstance == nullptr)
101 {
102 UWorld* World = WorldContextObject->GetWorld();
103 GameInstance = World ? World->GetGameInstance() : nullptr;
104 check(GameInstance);
105 }
106
107 USnapshotSubsystem* SnapshotSubsystem = GameInstance->GetSubsystem<USnapshotSubsystem>();
108
109 if (SnapshotSubsystem == nullptr)
110 {
111 LOG_ERROR(LogCK, "%s를 가져올 수 없습니다. ", NAMEOF(USnapshotSubsystem));
112 return nullptr;
113 }
114 else
115 {
116 return SnapshotSubsystem->ProcessNewCommand<TCommandType>(InitializeCommand);
117 }
118}
#define LOG_ERROR(Category, Format,...)
Definition CK_UE.h:40
#define NAMEOF(Type)
Definition CK_UE.h:30
Definition CommandBase.h:11
Definition SnapshotSubsystem.h:16
TCommandType * ProcessNewCommand(TInitializeCommand InitializeCommand)
Definition SnapshotSubsystem.h:59
FORCEINLINE bool IsRollingBack() const
Definition SnapshotSubsystem.h:43