/
oTimerGetItems.cs
143 lines (126 loc) · 3.73 KB
/
oTimerGetItems.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using LBAMemoryModule;
namespace LBAHUD
{
class oTimerGetItems
{
byte LBAVer;
Timer tmr = new Timer();
Mem memory = new Mem();
List<ActionItem> itemList = new List<ActionItem>();
private int interval = 50;
//Func x;
//Func<string, string> myFunction;
public enum LBAVersion { One = 1, Two = 2 }
public oTimerGetItems()
{
objectLoad(interval);
}
public oTimerGetItems(int interval)
{
this.interval = interval;
objectLoad(interval);
}
private void objectLoad(int interval)
{
tmr.Interval = interval;
tmr.Tick += timer_Tick;
}
public void AddItem(Action<ushort> act, Item item)
{
itemAdded(new ActionItem(act, item));
}
public void AddItem(Action<ushort> act, uint memoryOffset, ushort size)
{
itemAdded(new ActionItem(act, new Item(memoryOffset, size)));
}
public void AddItem(ActionItem ai)
{
itemAdded(ai);
}
private void itemAdded(ActionItem ai)
{
//Handle event, start timer if not already active i.e. if first item added
itemList.Add(ai);
if (1 == itemList.Count())
{
StartTimer();
}
}
private void StartTimer()
{
tmr.Enabled = true;
}
private void StopTimer()
{
tmr.Enabled = false;
}
private void timer_Tick(object sender, EventArgs e)
{
for (int i = 0; i < itemList.Count; i++)
{
ushort val = (ushort)memory.readVal(itemList[i].item.memoryOffset, itemList[i].item.size);
itemList[i].act(val);
}
}
public bool IsEmpty()
{
return 0 == itemList.Count;
}
public bool Contains(uint memoryOffSet)
{
for (int i = 0; i < itemList.Count; i++)
if (itemList[i].item.memoryOffset == memoryOffSet)
return true;
return false;
}
/* Returns true if the item is removed, else false */
public bool RemoveIfExists(uint memoryOffset)
{
bool removed = false;
for (int i = 0; i < itemList.Count; i++)
if (itemList[i].item.memoryOffset == memoryOffset)
{
itemList.RemoveAt(i);
removed = true;
break;
}
if (0 == itemList.Count) StopTimer();
return removed;
}
public void RemoveAll()
{
StopTimer();
while (0 != itemList.Count)
RemoveIfExists(itemList[itemList.Count - 1].item.memoryOffset);
memory = null;
memory = new Mem();
}
public bool UpdateItem(uint memoryOffset, ushort newVal)
{
for (int i = 0; i < itemList.Count; i++)
if (itemList[i].item.memoryOffset == memoryOffset)
{
itemList[i].item.maxVal = newVal;
return true;
}
return false;
}
public class ActionItem
{
public Item item;
public Action<ushort> act;
public ActionItem() { }
public ActionItem(Action<ushort> act, Item item)
{
this.item = item;
this.act = act;
}
}
}
}