Skip to content

Commit b0ac15e

Browse files
author
tompeeters_macomi
committed
Make a bit more use of direct extension methods.
1 parent e97184e commit b0ac15e

12 files changed

Lines changed: 73 additions & 81 deletions

File tree

WCSharp.Buffs/Aura.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -163,13 +163,13 @@ public void Action()
163163
{
164164
SearchIntervalLeft = SearchInterval;
165165
var units = GetAuraTargets();
166-
for (var i = 0; i < units.Count; i++)
166+
for (var i = 1; i <= units.Count; i++)
167167
{
168-
var unit = units[i];
168+
var unit = units.DirectGet(i);
169169
var found = false;
170-
for (var j = 0; j < this.activeBuffs.Count; j++)
170+
for (var j = 1; j <= this.activeBuffs.Count; j++)
171171
{
172-
var activeBuff = this.activeBuffs[j];
172+
var activeBuff = this.activeBuffs.DirectGet(j);
173173
if (activeBuff.Unit == unit)
174174
{
175175
activeBuff.Duration = Duration;
@@ -191,13 +191,16 @@ public void Action()
191191
SearchIntervalLeft -= BuffSystem.TickInterval;
192192
}
193193

194-
for (var i = this.activeBuffs.Count - 1; i >= 0; i--)
194+
var size = this.activeBuffs.Count;
195+
for (var i = 1; i <= size; i++)
195196
{
196-
var buff = this.activeBuffs[i];
197+
var buff = this.activeBuffs.DirectGet(i);
197198
if (buff.Duration <= 0)
198199
{
199200
buff.Buff.Stacks--;
200-
this.activeBuffs.RemoveAt(i);
201+
this.activeBuffs.DirectNilShift(i, size);
202+
size--;
203+
i--;
201204
}
202205
else
203206
{

WCSharp.Buffs/BuffSystem.cs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ private static void OnDeath()
7979
}
8080
}
8181

82-
for (var i = 0; i < buffsOnUnit.Count; i++)
82+
for (var i = 1; i <= buffsOnUnit.Count; i++)
8383
{
84-
var buff = buffsOnUnit[i];
84+
var buff = buffsOnUnit.DirectGet(i);
8585
if (buff.Active)
8686
{
8787
buff.Active = false;
@@ -101,9 +101,9 @@ public static Buff Add(Buff buff, StackBehaviour stackBehaviour = StackBehaviour
101101
if (stackBehaviour != StackBehaviour.None)
102102
{
103103
var type = buff.GetType();
104-
for (var i = 0; i < buffsOnUnit.Count; i++)
104+
for (var i = 1; i <= buffsOnUnit.Count; i++)
105105
{
106-
var currentBuff = buffsOnUnit[i];
106+
var currentBuff = buffsOnUnit.DirectGet(i);
107107
if (currentBuff.Active && currentBuff.GetType() == type)
108108
{
109109
if (stackBehaviour == StackBehaviour.Stack ||
@@ -171,19 +171,19 @@ private static void OnUnitTypeChangesOwner()
171171
if (GetUnitTypeId(unit) == DummySystem.UNIT_TYPE_DUMMY)
172172
return;
173173

174-
for (var i = 0; i < buffs.Count; i++)
174+
for (var i = 1; i <= buffs.Count; i++)
175175
{
176-
if (buffs[i].Caster == unit)
176+
if (buffs.DirectGet(i).Caster == unit)
177177
{
178-
buffs[i].CastingPlayer = GetOwningPlayer(unit);
178+
buffs.DirectGet(i).CastingPlayer = GetOwningPlayer(unit);
179179
}
180180
}
181181

182182
var owner = GetOwningPlayer(unit);
183183
var list = GetBuffsOnUnit(unit);
184-
for (var i = 0; i < list.Count; i++)
184+
for (var i = 1; i <= list.Count; i++)
185185
{
186-
var buff = list[i];
186+
var buff = list.DirectGet(i);
187187
buff.TargetPlayer = owner;
188188
}
189189
}
@@ -210,9 +210,9 @@ public static List<Dispel> Dispel(unit target, unit dispeller, bool isBeneficial
210210
{
211211
var dispels = new List<Dispel>();
212212
var list = GetBuffsOnUnit(target);
213-
for (var i = 0; i < list.Count; i++)
213+
for (var i = 1; i <= list.Count; i++)
214214
{
215-
var buff = list[i];
215+
var buff = list.DirectGet(i);
216216
if (buff.IsBeneficial == isBeneficial)
217217
{
218218
var stacks = buff.Stacks;
@@ -246,9 +246,9 @@ public static List<Dispel> Dispel(unit target, unit dispeller, bool isBeneficial
246246
{
247247
var dispels = new List<Dispel>();
248248
var list = GetBuffsOnUnit(target);
249-
for (var i = 0; i < list.Count; i++)
249+
for (var i = 1; i <= list.Count; i++)
250250
{
251-
var buff = list[i];
251+
var buff = list.DirectGet(i);
252252
if (buff.IsBeneficial == isBeneficial && buff.BuffTypes.Contains(dispelType))
253253
{
254254
var stacks = buff.Stacks;
@@ -282,9 +282,9 @@ public static List<Dispel> Dispel(unit target, unit dispeller, bool isBeneficial
282282
{
283283
var dispels = new List<Dispel>();
284284
var list = GetBuffsOnUnit(target);
285-
for (var i = 0; i < list.Count; i++)
285+
for (var i = 1; i <= list.Count; i++)
286286
{
287-
var buff = list[i];
287+
var buff = list.DirectGet(i);
288288
if (buff.IsBeneficial == isBeneficial && buff.BuffTypes.Any(x => dispelTypes.Contains(x)))
289289
{
290290
var stacks = buff.Stacks;
@@ -319,9 +319,9 @@ public static List<Dispel> Dispel(unit target, unit dispeller, bool isBeneficial
319319
{
320320
var dispels = new List<Dispel>();
321321
var list = GetBuffsOnUnit(target);
322-
for (var i = 0; i < list.Count; i++)
322+
for (var i = 1; i <= list.Count; i++)
323323
{
324-
var buff = list[i];
324+
var buff = list.DirectGet(i);
325325
if (buff.IsBeneficial == isBeneficial && buff.BuffTypes.Any(x => dispelTypes.Contains(x)) && !buff.BuffTypes.Any(x => exclusions.Contains(x)))
326326
{
327327
var stacks = buff.Stacks;

WCSharp.Dummies/DummySystem.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,19 @@ private static void OnDummyCast()
4040
private static void Countdown(Timer timer)
4141
{
4242
var size = dummiesBeingRecycled.Count;
43-
for (var i = size - 1; i >= 0; i--)
43+
for (var i = size; i > 0; i--)
4444
{
45-
var dummy = dummiesBeingRecycled[i];
45+
var dummy = dummiesBeingRecycled.DirectGet(i);
4646
if (dummy.TimeLeft <= 0)
4747
{
4848
SetUnitX(dummy.Dummy, 0);
4949
SetUnitY(dummy.Dummy, 0);
50-
dummiesReady.Add(dummy.Dummy);
50+
dummiesReady.DirectAdd(dummy.Dummy);
5151

52+
dummiesBeingRecycled.DirectMove(size, i);
5253
size--;
53-
dummiesBeingRecycled[i] = dummiesBeingRecycled[size];
5454
dummiesBeingRecycled.RemoveAt(size);
55+
i++;
5556
}
5657
else
5758
{
@@ -69,7 +70,7 @@ public static void RecycleDummy(unit dummy, float recycleTime = 2.0f)
6970
{
7071
TimerSystem.Add(timer);
7172
}
72-
dummiesBeingRecycled.Add(new DummyBeingRecycled(dummy, recycleTime));
73+
dummiesBeingRecycled.DirectAdd(new DummyBeingRecycled(dummy, recycleTime));
7374
}
7475

7576
/// <summary>

WCSharp.Events/EventHandlers/EventSet.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public EventSet()
1818

1919
public void Add(Action action, object filterObj)
2020
{
21-
this.actions.Add(action);
21+
this.actions.DirectAdd(action);
2222
}
2323

2424
public bool Remove(Action action, object filterObj)

WCSharp.Events/EventHandlers/PlayerUnitEventHandlers/AbstractPlayerUnitEventHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ private IEventSet AddEventSet(IEventSet eventSet)
135135
EnableTrigger(this.trigger);
136136
}
137137

138-
this.eventSets.Add(eventSet);
138+
this.eventSets.DirectAdd(eventSet);
139139
return eventSet;
140140
}
141141

WCSharp.Events/PeriodicDisposableTrigger.cs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Collections.Generic;
22
using System.Linq;
3+
using WCSharp.Shared.Extensions;
34

45
namespace WCSharp.Events
56
{
@@ -48,26 +49,22 @@ public void Add(T periodicDisposableTrigger)
4849
private bool Periodic()
4950
{
5051
var size = this.actions.Count;
51-
var i = 0;
5252

53-
while (i < size)
53+
for (var i = 1; i <= size; i++)
5454
{
55-
var action = this.actions[i];
55+
var action = this.actions.DirectGet(i);
5656
if (action.Active)
5757
{
5858
action.Action();
5959
}
6060

61-
if (action.Active)
62-
{
63-
i++;
64-
}
65-
else
61+
if (!action.Active)
6662
{
63+
action.Dispose();
64+
this.actions.DirectMove(size, i);
6765
size--;
68-
this.actions[i] = this.actions[size];
6966
this.actions.RemoveAt(size);
70-
action.Dispose();
67+
i--;
7168
}
7269
}
7370

WCSharp.Events/PeriodicEvents.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using WCSharp.Api;
4+
using WCSharp.Shared.Extensions;
45
using static WCSharp.Api.Common;
56

67
namespace WCSharp.Events
@@ -72,22 +73,20 @@ public static void AddPeriodicEvent(PeriodicEvent timerEvent)
7273
private static void Tick()
7374
{
7475
var size = timerEvents.Count;
75-
var i = 0;
76-
while (i < size)
76+
77+
for (var i = 1; i <= size; i++)
7778
{
78-
// Purposely written stupidly to avoid decompilation into a for loop
79-
var timerEvent = timerEvents[i];
80-
i++;
79+
var timerEvent = timerEvents.DirectGet(i);
8180
timerEvent.IntervalLeft -= SYSTEM_INTERVAL;
8281
while (timerEvent.IntervalLeft <= 0)
8382
{
8483
timerEvent.IntervalLeft += timerEvent.Interval;
8584
if (!timerEvent.Method.Invoke())
8685
{
87-
i--;
86+
timerEvents.DirectMove(size, i);
8887
size--;
89-
timerEvents[i] = timerEvents[size];
9088
timerEvents.RemoveAt(size);
89+
i--;
9190
break;
9291
}
9392
}

WCSharp.Events/PeriodicTrigger.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Collections.Generic;
22
using System.Linq;
3+
using WCSharp.Shared.Extensions;
34

45
namespace WCSharp.Events
56
{
@@ -47,25 +48,21 @@ public void Add(T periodicTrigger)
4748
private bool Periodic()
4849
{
4950
var size = this.actions.Count;
50-
var i = 0;
5151

52-
while (i < size)
52+
for (var i = 1; i <= size; i++)
5353
{
54-
var action = this.actions[i];
54+
var action = this.actions.DirectGet(i);
5555
if (action.Active)
5656
{
5757
action.Action();
5858
}
5959

60-
if (action.Active)
61-
{
62-
i++;
63-
}
64-
else
60+
if (!action.Active)
6561
{
62+
this.actions.DirectMove(size, i);
6663
size--;
67-
this.actions[i] = this.actions[size];
6864
this.actions.RemoveAt(size);
65+
i--;
6966
}
7067
}
7168

WCSharp.Events/PlayerUnitEvents.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using WCSharp.Api;
44
using WCSharp.Events.EventHandlers.PlayerUnitEventHandlers;
55
using WCSharp.Shared.Data;
6+
using WCSharp.Shared.Extensions;
67
using static WCSharp.Api.Common;
78

89
namespace WCSharp.Events
@@ -194,7 +195,7 @@ private static void Register(int @event, Action action)
194195
else
195196
{
196197
AbstractPlayerUnitEventHandler.RequiresUpdate = true;
197-
pendingUpdates.Add(() => Register(@event, action));
198+
pendingUpdates.DirectAdd(() => Register(@event, action));
198199
}
199200
}
200201

@@ -209,7 +210,7 @@ private static void Register(int @event, Action action, int filterId)
209210
else
210211
{
211212
AbstractPlayerUnitEventHandler.RequiresUpdate = true;
212-
pendingUpdates.Add(() => Register(@event, action, filterId));
213+
pendingUpdates.DirectAdd(() => Register(@event, action, filterId));
213214
}
214215
}
215216

@@ -224,7 +225,7 @@ private static void Register(int @event, Action action, handle handle)
224225
else
225226
{
226227
AbstractPlayerUnitEventHandler.RequiresUpdate = true;
227-
pendingUpdates.Add(() => Register(@event, action, handle));
228+
pendingUpdates.DirectAdd(() => Register(@event, action, handle));
228229
}
229230
}
230231

@@ -388,7 +389,7 @@ private static void Unregister(int @event, Action action)
388389
else
389390
{
390391
AbstractPlayerUnitEventHandler.RequiresUpdate = true;
391-
pendingUpdates.Add(() => Unregister(@event, action));
392+
pendingUpdates.DirectAdd(() => Unregister(@event, action));
392393
}
393394
}
394395

@@ -403,7 +404,7 @@ private static void Unregister(int @event, Action action, int filterId)
403404
else
404405
{
405406
AbstractPlayerUnitEventHandler.RequiresUpdate = true;
406-
pendingUpdates.Add(() => Unregister(@event, action, filterId));
407+
pendingUpdates.DirectAdd(() => Unregister(@event, action, filterId));
407408
}
408409
}
409410

@@ -418,7 +419,7 @@ private static void Unregister(int @event, Action action, handle handle)
418419
else
419420
{
420421
AbstractPlayerUnitEventHandler.RequiresUpdate = true;
421-
pendingUpdates.Add(() => Unregister(@event, action, handle));
422+
pendingUpdates.DirectAdd(() => Unregister(@event, action, handle));
422423
}
423424
}
424425

WCSharp.Events/SmoothDisposableTrigger.cs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Collections.Generic;
22
using System.Linq;
3+
using WCSharp.Shared.Extensions;
34
using static WCSharp.Api.Common;
45

56
namespace WCSharp.Events
@@ -73,13 +74,10 @@ public void Add(T smoothDisposableTrigger)
7374
private bool Periodic()
7475
{
7576
var size = this.actions.Count;
76-
var i = 0;
7777

78-
while (i < size)
78+
for (var i = 1; i <= size; i++)
7979
{
80-
var action = this.actions[i];
81-
// Purposely written stupidly to avoid decompilation into a for loop
82-
i++;
80+
var action = this.actions.DirectGet(i);
8381
action.TicksLeft--;
8482
if (action.TicksLeft <= 0)
8583
{
@@ -92,11 +90,10 @@ private bool Periodic()
9290
if (!action.Active)
9391
{
9492
action.Dispose();
95-
i--;
96-
93+
this.actions.DirectMove(size, i);
9794
size--;
98-
this.actions[i] = this.actions[size];
9995
this.actions.RemoveAt(size);
96+
i--;
10097
}
10198
}
10299
}

0 commit comments

Comments
 (0)