-
Notifications
You must be signed in to change notification settings - Fork 0
/
BotManager.cs
225 lines (191 loc) · 6.95 KB
/
BotManager.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using CoolFishNS.Bots;
using CoolFishNS.Bots.CoolFishBot;
using CoolFishNS.Bots.PoolFishingBot;
using CoolFishNS.Management.CoolManager;
using CoolFishNS.Management.CoolManager.HookingLua;
using CoolFishNS.PluginSystem;
using CoolFishNS.Properties;
using CoolFishNS.Utilities;
using GreyMagic;
namespace CoolFishNS.Management
{
/// <summary>
/// Bot manager class for performing operations on IBot implemented classes
/// </summary>
public static class BotManager
{
internal static readonly Dictionary<string, IBot> LoadedBots = new Dictionary<string, IBot>();
internal static bool WasCut;
static BotManager()
{
LoadBot(new CoolFishBot(), true);
LoadBot(new PoolFishingBot());
}
public static ExternalProcessReader Memory { get; private set; }
/// <summary>
/// Currently active IBot object that the user interface will interact with.
/// This field should be set to whatever Bot object you want to respond to UI functions (Start, Stop, etc.)
/// </summary>
public static IBot ActiveBot { get; private set; }
private static bool IsReadyToBot
{
get { return Memory != null && ActiveBot != null && Memory.IsProcessOpen && DxHook.Instance.IsApplied && !ActiveBot.IsRunning(); }
}
/// <summary>
/// Loads an IBot implementing class into CoolFish's BotManager for display and use from the interface
/// </summary>
/// <param name="botToLoad">IBot implementing instance to load into CoolFish</param>
/// <param name="setAsActive">true to set the loaded bot as the currently active one; otherwise, set to false</param>
public static void LoadBot(IBot botToLoad, bool setAsActive = false)
{
if (!IsBotLoaded(botToLoad))
{
LoadedBots.Add(GetBotId(botToLoad), botToLoad);
}
else
{
Logging.Write("Bot \"" + GetBotId(botToLoad) + "\" has already been loaded. Skipping load...");
}
if (setAsActive)
{
SetActiveBot(botToLoad);
}
}
/// <summary>
/// Returns whether or not a bot with a particular unique keyId has been loaded or not
/// </summary>
/// <param name="bot">IBot to look up</param>
/// <returns>true if the bot is already loaded; otherwise, false</returns>
public static bool IsBotLoaded(IBot bot)
{
return LoadedBots.ContainsKey(GetBotId(bot));
}
/// <summary>
/// Get the unique identifier string the BotManager uses to compare IBot classes.
/// Currently implemented as "bot.GetName() - bot.GetVersion()"
/// No two bots should be loaded with the same name and version combination
/// </summary>
/// <param name="bot">bot to get the Id of</param>
/// <returns>string identifier of the IBot class</returns>
public static string GetBotId(IBot bot)
{
return bot.GetName() + "-" + bot.GetVersion();
}
/// <summary>
/// Sets the actively running bot based on the passed unique keyId
/// </summary>
/// <param name="bot">IBot to set as active</param>
/// <returns>true if bot was set as active; false, if bot is not loaded and was not set as active</returns>
public static bool SetActiveBot(IBot bot)
{
if (!IsBotLoaded(bot))
{
Logging.Write("Bot \"" + GetBotId(bot) + "\" has not yet been loaded");
return false;
}
StopActiveBot();
ActiveBot = LoadedBots[GetBotId(bot)];
return true;
}
/// <summary>
/// Attach all manipulation related classes to the passed process.
/// ObjectManager and Hook related operations will be available after this call
/// </summary>
/// <param name="process"></param>
public static void AttachToProcess(Process process)
{
if (WasCut)
{
return;
}
StopActiveBot();
try
{
if (Offsets.FindOffsets(process))
{
Memory = new ExternalProcessReader(process);
if (DxHook.Instance.Apply())
{
Logging.Write(LocalSettings.Translations["Attached to"] + ": " +
process.Id);
}
else
{
Logging.Write(LocalSettings.Translations["Error"]);
Memory.Dispose();
Memory = null;
}
}
else
{
Logging.Write(LocalSettings.Translations["Unhandled Exception"]);
}
}
catch (Exception ex)
{
Logging.Write(LocalSettings.Translations["Unhandled Exception"]);
Logging.Log(ex);
}
}
/// <summary>
/// Start the currently Active Bot
/// </summary>
public static void StartActiveBot()
{
if (WasCut)
{
return;
}
if (IsReadyToBot)
{
ActiveBot.StartBot();
}
else
{
Logging.Write(Resources.BotIsNotReadyError);
}
}
/// <summary>
/// Stop the currently Active Bot
/// </summary>
public static void StopActiveBot()
{
if (ActiveBot != null && ActiveBot.IsRunning())
{
ActiveBot.StopBot();
}
}
internal static void StartUp()
{
LocalSettings.LoadSettings();
PluginManager.LoadPlugins();
PluginManager.StartPlugins();
Logging.Log("Start Up.");
}
internal static void ShutDown()
{
StopActiveBot();
PluginManager.StopPlugins();
PluginManager.ShutDownPlugins();
LocalSettings.SaveSettings();
DxHook.Instance.Dispose();
if (Memory != null)
{
Memory.Dispose();
}
Logging.Log("Shut Down.");
}
/// <summary>
/// Get the currently logged in toon's name
/// </summary>
/// <returns>string of the player's name</returns>
public static string GetToonName()
{
return Offsets.Addresses.ContainsKey("PlayerName") ? Memory.ReadString(Offsets.Addresses["PlayerName"], Encoding.UTF8) : string.Empty;
}
}
}