Arma Reforger Explorer 1.7.0.54
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
Loading...
Searching...
No Matches
SCR_ResourceSystemSubscriptionManager.c
Go to the documentation of this file.
2{
3 static const int REPLICATION_MAX_FRAME_BUDGET = 10;
4 static const int GRACEFUL_HANDLES_MAX_FRAME_BUDGET = 10;
5 static const int GRACEFUL_HANDLES_POKED_THRESHOLD = 10000;
6
9
10 protected ref array<ref SCR_ResourceSystemSubscriptionListing> m_aListings = {};
11 protected ref array<SCR_ResourceSystemSubscriptionHandleBase> m_aHandles = {};
12 protected ref array<ref SCR_ResourceSystemSubscriptionHandleBase> m_aGracefulHandles = {};
13
14 //------------------------------------------------------------------------------------------------
15 protected SCR_ResourceSystemSubscriptionListing GetListing(notnull SCR_ResourceInteractor interactor)
16 {
17 int higherLimitPosition = m_aListings.Count();
18
19 if (higherLimitPosition == 0)
20 return null;
21
22 int position;
24
25 while (position < higherLimitPosition)
26 {
27 if (GetNextListingCandidate(position, higherLimitPosition, listing, interactor))
28 break;
29 }
30
31 if (listing
32 && position == m_aListings.Count()
33 && listing.GetInteractor() != interactor)
34 return null;
35
36 return listing;
37 }
38
39 //------------------------------------------------------------------------------------------------
40 protected bool GetNextListingCandidate(inout int position, inout int higherLimitPosition, inout SCR_ResourceSystemSubscriptionListing listing, notnull SCR_ResourceInteractor interactor)
41 {
42 int comparePosition = position + ((higherLimitPosition - position) >> 1);
43 listing = m_aListings[comparePosition];
44
45 if (!listing)
46 return false;
47
48 SCR_ResourceInteractor compareInteractor = listing.GetInteractor();
49
50 if (interactor > compareInteractor)
51 position = comparePosition + 1;
52 else if (interactor < compareInteractor)
53 higherLimitPosition = comparePosition;
54 else
55 {
56 listing = m_aListings[comparePosition];
57
58 return true;
59 }
60
61 listing = null;
62
63 return false;
64 }
65
66 //------------------------------------------------------------------------------------------------
67 SCR_ResourceSystemSubscriptionHandleBase GetHandle(RplId resourceComponentRplId, typename interactorType, EResourceType resourceType, EResourceGeneratorID resourceIdentifier)
68 {
69 int higherLimitPosition = m_aHandles.Count();
70
71 if (higherLimitPosition == 0)
72 return null;
73
74 int position;
76
77 while (position < higherLimitPosition)
78 {
79 if (GetNextHandleCandidate(position, higherLimitPosition, handle, resourceComponentRplId, interactorType, resourceType, resourceIdentifier))
80 break;
81 }
82
83 if (handle
84 && position == m_aHandles.Count()
85 && (handle.GetResourceComponentRplId() != resourceComponentRplId
86 || handle.GetInteractorType() != interactorType
87 || handle.GetResourceType() != resourceType
88 || handle.GetResourceIdentifier() != resourceIdentifier))
89 return null;
90
91 return handle;
92 }
93
94 //------------------------------------------------------------------------------------------------
95 protected bool GetNextHandleCandidate(inout int position, inout int higherLimitPosition, inout SCR_ResourceSystemSubscriptionHandleBase handle, RplId resourceComponentRplId, typename interactorType, EResourceType resourceType, EResourceGeneratorID resourceIdentifier)
96 {
97 int comparePosition = position + ((higherLimitPosition - position) >> 1);
98 handle = m_aHandles[comparePosition];
99
100 if (!handle)
101 return false;
102
103 RplId compareResourceComponentRplId = handle.GetResourceComponentRplId();
104 typename compareInteractorType = handle.GetInteractorType();
105 EResourceType compareResourceType = handle.GetResourceType();
106 EResourceGeneratorID compareResourceIdentifier = handle.GetResourceIdentifier();
107
109 if (resourceComponentRplId > compareResourceComponentRplId)
110 position = comparePosition + 1;
111 else if (resourceComponentRplId < compareResourceComponentRplId)
112 higherLimitPosition = comparePosition;
113
115 else if (interactorType.ToString() > compareInteractorType.ToString())
116 position = comparePosition + 1;
117 else if (interactorType.ToString() < compareInteractorType.ToString())
118 higherLimitPosition = comparePosition;
119
121 else if (resourceType > compareResourceType)
122 position = comparePosition + 1;
123 else if (resourceType < compareResourceType)
124 higherLimitPosition = comparePosition;
125
127 else if (resourceIdentifier > compareResourceIdentifier)
128 position = comparePosition + 1;
129 else if (resourceIdentifier < compareResourceIdentifier)
130 higherLimitPosition = comparePosition;
131
133 else
134 {
135 handle = m_aHandles[comparePosition];
136
137 return true;
138 }
139
140 handle = null;
141
142 return false;
143 }
144
145 //------------------------------------------------------------------------------------------------
146 bool SubscribeListener(RplId listener, notnull SCR_ResourceInteractor interactor)
147 {
148 if (!listener.IsValid())
149 return false;
150
152
153 if (listing)
154 {
155 listing.SubscribeListener(listener);
156
157 return true;
158 }
159
160 int position;
161 int comparePosition;
162 int maxPosition = m_aListings.Count();
163 SCR_ResourceInteractor compareInteractor;
165
166 while (position < maxPosition)
167 {
168 comparePosition = position + ((maxPosition - position) >> 1);
169 compareListing = m_aListings[comparePosition];
170 compareInteractor = compareListing.GetInteractor();
171
172 if (interactor > compareInteractor)
173 position = comparePosition + 1;
174 else if (interactor < compareInteractor)
175 maxPosition = comparePosition;
176
177 else
178 break;
179 }
180
181 listing = new SCR_ResourceSystemSubscriptionListing(interactor);
182
183 listing.SubscribeListener(listener);
184 m_aListings.InsertAt(listing, position);
185
186 return true;
187 }
188
189 //------------------------------------------------------------------------------------------------
190 bool UnsubscribeListener(RplId listener, notnull SCR_ResourceInteractor interactor)
191 {
192 if (!listener.IsValid())
193 return false;
194
196
197 if (!listing)
198 return false;
199
200 listing.UnsubscribeListener(listener);
201
202 if (listing.IsEmpty())
203 m_aListings.RemoveOrdered(m_aListings.Find(listing));
204
205 return true;
206 }
207
208 //------------------------------------------------------------------------------------------------
210 {
211 if (!listener.IsValid())
212 return;
213
215
216 for (int i = m_aListings.Count() - 1; i >= 0; --i)
217 {
218 listing = m_aListings[i];
219
220 if (!listing)
221 {
222 m_aListings.RemoveOrdered(i);
223
224 continue;
225 }
226
227 listing.UnsubscribeListener(listener)
228 }
229 }
230
231 //------------------------------------------------------------------------------------------------
233 {
234 const WorldTimestamp currentTime = GetGame().GetWorld().GetTimestamp();
236
237 // Clear out null graceful handles.
238 m_aGracefulHandles.RemoveItem(null);
239
240 for (int i = 0; i < SCR_ResourceSystemSubscriptionManager.GRACEFUL_HANDLES_MAX_FRAME_BUDGET && !m_aGracefulHandles.IsEmpty(); ++i)
241 {
243
244 if (currentTime.DiffMilliseconds(handle.GetLastPokedAt()) >= SCR_ResourceSystemSubscriptionManager.GRACEFUL_HANDLES_POKED_THRESHOLD)
245 {
246 m_aGracefulHandles.RemoveItem(handle);
247 }
248 }
249 }
250
251 //------------------------------------------------------------------------------------------------
253 {
254 // Clear out null listings.
255 m_aListings.RemoveItem(null);
256
257 for (int i = 0; i < SCR_ResourceSystemSubscriptionManager.REPLICATION_MAX_FRAME_BUDGET && !m_aListings.IsEmpty(); ++i)
258 {
259 m_aListings[m_iReplicateListenersPivot++ % m_aListings.Count()].Replicate();
260 }
261 }
262
263 //------------------------------------------------------------------------------------------------
264 SCR_ResourceSystemSubscriptionHandleBase RequestSubscriptionListenerHandle(notnull SCR_ResourceInteractor interactor, RplId ownerRplId)
265 {
266 if (!ownerRplId.IsValid())
267 return null;
268
269 RplId resourceComponentRplId = Replication.FindItemId(interactor.GetComponent());
270
271 if (!resourceComponentRplId.IsValid())
272 return null;
273
274 typename interactorType = interactor.Type();
275 EResourceType resourceType = interactor.GetResourceType();
276 EResourceGeneratorID resourceIdentifier = interactor.GetIdentifier();
277
278 SCR_ResourceSystemSubscriptionHandleBase handle = GetHandle(resourceComponentRplId, interactorType, resourceType, resourceIdentifier);
279
280 if (handle)
281 return handle;
282
283 int position;
284 int comparePosition;
285 int maxPosition = m_aHandles.Count();
287 RplId compareResourceComponentRplId;
288 typename compareInteractorType;
289 EResourceType compareResourceType;
290 EResourceGeneratorID compareResourceIdentifier;
291
292 while (position < maxPosition)
293 {
294 comparePosition = position + ((maxPosition - position) >> 1);
295 compareHandle = m_aHandles[comparePosition];
296 compareResourceComponentRplId = compareHandle.GetResourceComponentRplId();
297 compareInteractorType = compareHandle.GetInteractorType();
298 compareResourceType = compareHandle.GetResourceType();
299 compareResourceIdentifier = compareHandle.GetResourceIdentifier();
300
302 if (resourceComponentRplId > compareResourceComponentRplId)
303 position = comparePosition + 1;
304 else if (resourceComponentRplId < compareResourceComponentRplId)
305 maxPosition = comparePosition;
306
308 else if (interactorType.ToString() > compareInteractorType.ToString())
309 position = comparePosition + 1;
310 else if (interactorType.ToString() < compareInteractorType.ToString())
311 maxPosition = comparePosition;
312
314 else if (resourceType > compareResourceType)
315 position = comparePosition + 1;
316 else if (resourceType < compareResourceType)
317 maxPosition = comparePosition;
318
320 else if (resourceIdentifier > compareResourceIdentifier)
321 position = comparePosition + 1;
322 else if (resourceIdentifier < compareResourceIdentifier)
323 maxPosition = comparePosition;
324
325 else
326 break;
327 }
328
329 handle = SCR_ResourceSystemSubscriptionHandleBase.CreateHandle(this, ownerRplId, resourceComponentRplId, interactorType, resourceType, resourceIdentifier);
330
331 if (!handle)
332 return null;
333
334 m_aHandles.InsertAt(handle, position);
335
336 return handle;
337 }
338
339 //------------------------------------------------------------------------------------------------
341 {
342 if (!ownerRplId.IsValid())
343 return null;
344
345 RplId resourceComponentRplId = Replication.FindItemId(interactor.GetComponent());
346
347 if (!resourceComponentRplId.IsValid())
348 return null;
349
350 typename interactorType = interactor.Type();
351 EResourceType resourceType = interactor.GetResourceType();
352 EResourceGeneratorID resourceIdentifier = interactor.GetIdentifier();
353
354 SCR_ResourceSystemSubscriptionHandleBase handle = GetHandle(resourceComponentRplId, interactorType, resourceType, resourceIdentifier);
355
356 if (handle)
357 {
358 handle.Poke();
359
360 return handle;
361 }
362
363 int position;
364 int comparePosition;
365 int maxPosition = m_aHandles.Count();
367 RplId compareResourceComponentRplId;
368 typename compareInteractorType;
369 EResourceType compareResourceType;
370 EResourceGeneratorID compareResourceIdentifier;
371
372 while (position < maxPosition)
373 {
374 comparePosition = position + ((maxPosition - position) >> 1);
375 compareHandle = m_aHandles[comparePosition];
376 compareResourceComponentRplId = compareHandle.GetResourceComponentRplId();
377 compareInteractorType = compareHandle.GetInteractorType();
378 compareResourceType = compareHandle.GetResourceType();
379 compareResourceIdentifier = compareHandle.GetResourceIdentifier();
380
382 if (resourceComponentRplId > compareResourceComponentRplId)
383 position = comparePosition + 1;
384 else if (resourceComponentRplId < compareResourceComponentRplId)
385 maxPosition = comparePosition;
386
388 else if (interactorType.ToString() > compareInteractorType.ToString())
389 position = comparePosition + 1;
390 else if (interactorType.ToString() < compareInteractorType.ToString())
391 maxPosition = comparePosition;
392
394 else if (resourceType > compareResourceType)
395 position = comparePosition + 1;
396 else if (resourceType < compareResourceType)
397 maxPosition = comparePosition;
398
400 else if (resourceIdentifier > compareResourceIdentifier)
401 position = comparePosition + 1;
402 else if (resourceIdentifier < compareResourceIdentifier)
403 maxPosition = comparePosition;
404
405 else
406 break;
407 }
408
409 handle = SCR_ResourceSystemSubscriptionHandleBase.CreateHandle(this, ownerRplId, resourceComponentRplId, interactorType, resourceType, resourceIdentifier);
410
411 if (!handle)
412 return null;
413
414 m_aHandles.InsertAt(handle, position);
415 m_aGracefulHandles.Insert(handle);
416 handle.Poke();
417
418 return handle;
419 }
420
421 //------------------------------------------------------------------------------------------------
423 {
424 m_aHandles.RemoveOrdered(m_aHandles.Find(handle));
425
426 if (m_aGracefulHandles.Contains(handle))
427 m_aGracefulHandles.RemoveItem(handle);
428 }
429
430 //------------------------------------------------------------------------------------------------
431 void OnResourceInteractorDeleted(notnull SCR_ResourceInteractor interactor)
432 {
434
435 if (!listing)
436 return;
437
438 m_aListings.RemoveItem(listing);
439 }
440}
ArmaReforgerScripted GetGame()
Definition game.c:1398
vector position
EResourceGeneratorID
Main replication API.
Definition Replication.c:14
Replication item identifier.
Definition RplId.c:14
static SCR_ResourceSystemSubscriptionHandleBase CreateHandle(notnull SCR_ResourceSystemSubscriptionManager manager, RplId ownerRplId, RplId resourceComponentRplId, typename interactorType, EResourceType resourceType, EResourceGeneratorID resourceIdentifier)
ref array< ref SCR_ResourceSystemSubscriptionListing > m_aListings
SCR_ResourceSystemSubscriptionListing GetListing(notnull SCR_ResourceInteractor interactor)
ref array< ref SCR_ResourceSystemSubscriptionHandleBase > m_aGracefulHandles
SCR_ResourceSystemSubscriptionHandleBase RequestSubscriptionListenerHandle(notnull SCR_ResourceInteractor interactor, RplId ownerRplId)
ref array< SCR_ResourceSystemSubscriptionHandleBase > m_aHandles
void OnSubscriptionListenerHandleDeleted(notnull SCR_ResourceSystemSubscriptionHandleBase handle)
bool SubscribeListener(RplId listener, notnull SCR_ResourceInteractor interactor)
bool GetNextListingCandidate(inout int position, inout int higherLimitPosition, inout SCR_ResourceSystemSubscriptionListing listing, notnull SCR_ResourceInteractor interactor)
void OnResourceInteractorDeleted(notnull SCR_ResourceInteractor interactor)
bool UnsubscribeListener(RplId listener, notnull SCR_ResourceInteractor interactor)
SCR_ResourceSystemSubscriptionHandleBase RequestSubscriptionListenerHandleGraceful(notnull SCR_ResourceInteractor interactor, RplId ownerRplId)
SCR_ResourceSystemSubscriptionHandleBase GetHandle(RplId resourceComponentRplId, typename interactorType, EResourceType resourceType, EResourceGeneratorID resourceIdentifier)
bool GetNextHandleCandidate(inout int position, inout int higherLimitPosition, inout SCR_ResourceSystemSubscriptionHandleBase handle, RplId resourceComponentRplId, typename interactorType, EResourceType resourceType, EResourceGeneratorID resourceIdentifier)