RedCore
Loading...
Searching...
No Matches
ProfileEditBuilder.h
Go to the documentation of this file.
1#pragma once
2
3#include <red/public/ProfileInfo.h>
4#include <red/registry/builder/ProfileBuilder.h>
5#include <red/public/Profile.h>
6#include <red/profile/ProfileEx.h>
7#include <red/event/StaticInitEvent.h>
8#include <telkin/Privilege.h>
9
10namespace red {
11
12/**
13 * @brief Builder for partially modifying fields on a vanilla profile by its numeric ID.
14 * @details Intended to be constructed via a per-mod @c red::Registrar object.
15 * @tparam ID The target profile ID to modify.
16 */
17template <s32 ID>
19public:
20 static_assert(ID <= ProfileInfo::cProfileID_Max, "ERROR: Attempting to edit an invalid vanilla profile ID.");
21
25
26 /**
27 * @brief Completes the builder by applying the changes to the vanilla profile.
28 * @return The vanilla profile which was modified.
29 */
31 static bool instantiated = false;
32 if (instantiated) {
33 tk::fatal("Cannot reuse the same template instanciation for two edits.");
34 }
35 instantiated = true;
36
37 pub::Profile* profile = static_cast<pub::Profile*>(Profile::get(ID));
38
39 // defer until the vanilla profile is inited so we have the final say
40 static const struct {
41 const ActorCreateInfo* mCreateInfo;
42 bool mCreateInfoModified;
43 Profile::Flag mFlag;
44 bool mFlagModified;
45 s16 mDrawPriority;
46 bool mDrawPriorityModified;
47 s16 mExecutePriority;
48 bool mExecutePriorityModified;
49 sead::SafeString* mResources;
50 u8 mResourceCount;
51 s8 mResourceType;
52 bool mResourcesModified;
53 } sSnapshot = {
54 .mCreateInfo = this->mCreateInfo,
55 .mCreateInfoModified = this->mCreateInfoModified,
56 .mFlag = this->mFlag,
57 .mFlagModified = this->mFlagModified,
58 .mDrawPriority = this->mDrawPriority,
59 .mDrawPriorityModified = this->mDrawPriorityModified,
60 .mExecutePriority = this->mExecutePriority,
61 .mExecutePriorityModified = this->mExecutePriorityModified,
62 .mResources = this->mResources,
63 .mResourceCount = this->mResourceCount,
64 .mResourceType = this->mResourceType,
65 .mResourcesModified = this->mResourcesModified
66 };
67
68 static red::StaticInitEvent::Listener listener([](red::StaticInitEvent&) {
69 pub::Profile* profile = static_cast<pub::Profile*>(Profile::get(ID));
70
71 if (sSnapshot.mCreateInfoModified) {
72 profile->mActorCreateInfo = sSnapshot.mCreateInfo;
73 }
74
75 if (sSnapshot.mFlagModified) {
76 profile->mFlag = sSnapshot.mFlag;
77 }
78
79 if (sSnapshot.mDrawPriorityModified) {
80 tk::privilegedWrite(&ProfileInfo::cDrawPriority[ID], &sSnapshot.mDrawPriority, sizeof(ProfileInfo::cDrawPriority[ID]));
81 }
82
83 if (sSnapshot.mExecutePriorityModified) {
84 ProfileEx::setExecutePriority(ID, sSnapshot.mExecutePriority);
85 }
86
87 if (sSnapshot.mResourcesModified) {
88 tk::privilegedWrite(&ProfileInfo::cResList[ID], &sSnapshot.mResources, sizeof(ProfileInfo::cResList[ID])); // NOLINT
89 tk::privilegedWrite(&ProfileInfo::cResNum[ID], &sSnapshot.mResourceCount, sizeof(ProfileInfo::cResNum[ID]));
90 tk::privilegedWrite(&ProfileInfo::cResType[ID], &sSnapshot.mResourceType, sizeof(ProfileInfo::cResType[ID]));
91 }
92 });
93
94 return profile;
95 }
96
97};
98
99}
Builder for partially modifying fields on a vanilla profile by its numeric ID.
Definition ProfileEditBuilder.h:18
Profile * build()
Completes the builder by applying the changes to the vanilla profile.
Definition ProfileEditBuilder.h:30
ProfileEditBuilder()
Definition ProfileEditBuilder.h:22
Definition ActorDonutBlock.h:5