Arma Reforger Explorer 1.7.0.54
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
Loading...
Searching...
No Matches
SCR_ResourceComponentSerializer.c
Go to the documentation of this file.
2{
3 EResourceType m_eResourceType;
4 float m_fValue;
5}
6
7class SCR_ResourceComponentSerializer : ScriptedComponentSerializer
8{
9 [Attribute(desc: "Filter for which resource types to save - or all if empty.", uiwidget: UIWidgets.SearchComboBox, enums: ParamEnumArray.FromEnum(EResourceType))]
10 protected ref array<EResourceType> m_aResourceTypeFilter;
11
12 protected bool m_bUseTypeDescriminator = false;
13
14 //------------------------------------------------------------------------------------------------
15 override static typename GetTargetType()
16 {
17 return SCR_ResourceComponent;
18 }
19
20 //------------------------------------------------------------------------------------------------
21 override protected ESerializeResult Serialize(notnull IEntity owner, notnull GenericComponent component, notnull SaveContext context)
22 {
23 const SCR_ResourceComponent resource = SCR_ResourceComponent.Cast(component);
24
25 const BaseContainer source = component.GetComponentSource(owner);
26 auto containerSources = source.GetObjectArray("m_aContainers");
27 auto containers = resource.GetContainers();
28
29 array<ref SCR_PersistentResource> resources();
30 if (containers)
31 {
32 if (containerSources.Count() != containers.Count())
33 return ESerializeResult.ERROR;
34
35 foreach (int idx, auto container : containers)
36 {
37 if (container.IsEncapsulated())
38 continue;
39
40 const EResourceType resourceType = container.GetResourceType();
41 if (!m_aResourceTypeFilter.IsEmpty() && !m_aResourceTypeFilter.Contains(resourceType))
42 continue;
43
44 const float value = container.GetResourceValue();
45
46 float defaultValue;
47 const SCR_ResourceEncapsulator encapsulator = resource.GetEncapsulator(resourceType);
48 if (encapsulator)
49 {
50 bool overrideByAction = false;
51 auto actions = encapsulator.GetActions();
52 foreach (auto action : actions)
53 {
54 auto changeDefaultAction = SCR_ResourceEncapsulatorActionChangeResourceValue.Cast(action);
55 if (!changeDefaultAction)
56 continue;
57
58 overrideByAction = true;
59 defaultValue = changeDefaultAction.GetValueCurrent();
60 break;
61 }
62 if (!overrideByAction)
63 {
64 const SCR_ResourceContainerQueueBase queue = encapsulator.GetContainerQueue();
65 for (int i = 0, count = queue.GetContainerCount(); i < count; ++i)
66 {
67 const SCR_ResourceContainer childContainer = queue.GetContainerAt(i);
68 const SCR_ResourceComponent childComponent = childContainer.GetComponent();
69 const int containerIdx = childComponent.GetContainers().Find(childContainer);
70 auto childContainerSources = childComponent.GetComponentSource(childComponent.GetOwner()).GetObjectArray("m_aContainers");
71 if (containerIdx == -1 || containerIdx >= childContainerSources.Count())
72 return ESerializeResult.ERROR;
73
74 float childDefaultValue;
75 childContainerSources.Get(containerIdx).Get("m_fResourceValueCurrent", childDefaultValue);
76 defaultValue += childDefaultValue;
77 }
78 }
79 }
80 else
81 {
82 containerSources.Get(idx).Get("m_fResourceValueCurrent", defaultValue);
83 }
84
85 if (!float.AlmostEqual(value, defaultValue))
86 {
88 entry.m_eResourceType = resourceType;
89 entry.m_fValue = value;
90 resources.Insert(entry);
91 }
92 }
93 }
94
95 array<EResourceType> disabledTypes();
96 resource.GetDisabledResourceTypes(disabledTypes);
97
98 array<EResourceType> disabledTypesDefault();
99 source.Get("m_aDisabledResourceTypes", disabledTypesDefault);
100
101 if (SCR_ArrayHelperT<EResourceType>.AreEqual(disabledTypes, disabledTypesDefault))
102 disabledTypes = null;
103
104 if (resources.IsEmpty() && !disabledTypes)
105 return ESerializeResult.DEFAULT;
106
107 context.WriteValue("version", 1);
108
109 if (!resources.IsEmpty() || !context.CanSeekMembers())
110 {
111 const bool prev = context.EnableTypeDiscriminator(m_bUseTypeDescriminator);
112 context.Write(resources);
113 context.EnableTypeDiscriminator(prev);
114 }
115
116 if (disabledTypes || !context.CanSeekMembers())
117 context.Write(disabledTypes);
118
119 return ESerializeResult.OK;
120 }
121
122 //------------------------------------------------------------------------------------------------
123 override protected bool Deserialize(notnull IEntity owner, notnull GenericComponent component, notnull LoadContext context)
124 {
125 SCR_ResourceComponent resource = SCR_ResourceComponent.Cast(component);
126
127 int version;
128 context.ReadValue("version", version);
129
130 const bool prev = context.EnableTypeDiscriminator(m_bUseTypeDescriminator);
131 array<ref SCR_PersistentResource> resources;
132 const bool resourcesRead = context.Read(resources);
133 context.EnableTypeDiscriminator(prev);
134 if (resourcesRead)
135 {
136 resource.Initialize(owner);
137
138 foreach (SCR_PersistentResource loadResource : resources)
139 {
140 SCR_ResourceContainer container = resource.GetContainer(loadResource.m_eResourceType);
141 if (container)
142 container.SetResourceValue(loadResource.m_fValue);
143 }
144 }
145
146 array<EResourceType> disabledTypes;
147 context.Read(disabledTypes);
148 if (disabledTypes)
149 {
150 // Enable those who are not disabled in save-game
151 array<EResourceType> currentDisabled();
152 resource.GetDisabledResourceTypes(currentDisabled);
153 foreach (EResourceType type : currentDisabled)
154 {
155 if (!disabledTypes.Contains(type))
156 resource.SetResourceTypeEnabled(true, type);
157 }
158
159 // Disable all from save-game
160 foreach (EResourceType type : disabledTypes)
161 {
162 resource.SetResourceTypeEnabled(false, type);
163 }
164 }
165
166 return true;
167 }
168}
class SCR_PersistentThreatSector GetTargetType()
class SCR_ArrayHelper AreEqual(notnull array< T > array1, notnull array< T > array2)
EDamageType type
UI Textures DeployMenu Briefing conflict_HintBanner_1_UI desc
bool SetResourceValue(float value, bool notifyChange=true)
SCR_ResourceContainer GetContainerAt(int index)
array< ref SCR_ResourceEncapsulatorActionBase > GetActions()
override SCR_ResourceContainerQueueBase GetContainerQueue()
ESerializeResult Serialize(notnull IEntity owner, notnull GenericComponent component, notnull SaveContext context)
bool Deserialize(notnull IEntity owner, notnull GenericComponent component, notnull LoadContext context)
SCR_FieldOfViewSettings Attribute
ESerializeResult