forked from nemomomo/PSO2ACT
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPSO2ACT.cs
More file actions
executable file
·359 lines (318 loc) · 13.4 KB
/
PSO2ACT.cs
File metadata and controls
executable file
·359 lines (318 loc) · 13.4 KB
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using Advanced_Combat_Tracker;
using System.Reflection;
using System.IO;
using System.Threading;
using System.Net;
namespace PSO2ACT
{
public class PSO2ACT : IActPluginV1
{
config Config;
Label lblStatus;
string settingsFile = Path.Combine(ActGlobals.oFormActMain.AppDataFolder.FullName, "Config\\PluginPSO2.config.xml");
SettingsSerializer xmlSettings;
Queue<string> queueActions = new Queue<string>();
static ushort currInstID = 0xFFFF;
static string charName = "";
Thread logThread;
struct Skill
{
public string Name;
public string Type;
public string Comment;
}
Dictionary<uint, Skill> skillDict = new Dictionary<uint, Skill>();
public void DeInitPlugin()
{
SaveSettings();
logThread.Abort();
ActGlobals.oFormActMain.BeforeLogLineRead -= oFormActMain_BeforeLogLineRead;
ActGlobals.oFormActMain.OnCombatEnd -= oFormActMain_OnCombatEnd;
lblStatus.Text = "Plugin Exited";
}
public void InitPlugin(TabPage pluginScreenSpace, Label pluginStatusText)
{
Config = new config();
lblStatus = pluginStatusText;
pluginScreenSpace.Controls.Add(Config);
xmlSettings = new SettingsSerializer(Config);
LoadSettings();
Config.selectedFolder = Config.Controls["directory"].Text;
lblStatus.Text = "Loaded PSO2ACT Plugin";
if (ActGlobals.oFormActMain.GetAutomaticUpdatesAllowed())
new Thread(oFormActMain_UpdateCheckClicked).Start();
else
Config.refreshFlag = true;
ActGlobals.oFormActMain.GetDateTimeFromLog = ParseDateTime;
ActGlobals.oFormActMain.BeforeLogLineRead += new LogLineEventDelegate(oFormActMain_BeforeLogLineRead);
ActGlobals.oFormActMain.OnCombatEnd += new CombatToggleEventDelegate(oFormActMain_OnCombatEnd);
ActGlobals.oFormActMain.OnCombatStart += new CombatToggleEventDelegate(oFormActMain_OnCombatStart);
try
{
InitializeSkillDict();
logThread = new Thread(this.LogThread);
logThread.Start();
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message);
}
return;
}
private void LogThread()
{
while (true)
{
Thread.Sleep(100);
if (!Config.refreshFlag)
continue;
Config.refreshFlag = false;
string dir = String.Format(@"{0}\damagelogs", Config.selectedFolder);
DirectoryInfo dirInfo = new DirectoryInfo(dir);
if (!dirInfo.Exists)
{
Config.Controls["lblLogFile"].Text = "damagelogs folder not found";
continue;
}
FileInfo[] dr = dirInfo.GetFiles("*.csv");
if (dr == null || dr.Length == 0)
{
Config.Controls["lblLogFile"].Text = "No logs, make sure your damage dump plugin is enabled.";
continue;
}
FileInfo file = (from f in dr orderby f.LastWriteTime descending select f).FirstOrDefault();
Config.Controls["lblLogFile"].Text = String.Format("Reading {0}", file.Name ?? "<NULL>");
ActGlobals.oFormActMain.LogFilePath = file.FullName;
ActGlobals.oFormActMain.OpenLog(false, false);
}
}
struct Action
{
public uint timestamp;
public ushort instanceID;
public uint sourceID;
public string sourceName;
public uint targetID;
public string targetName;
public uint attackID;
public int damage;
public bool isJA;
public bool isCrit;
public bool isMultiHit;
public bool isMisc;
public bool isMisc2;
}
void oFormActMain_OnCombatStart(bool isImport, CombatToggleEventArgs encounterInfo)
{
if (!isImport)
encounterInfo.encounter.CharName = charName;
}
void oFormActMain_OnCombatEnd(bool isImport, CombatToggleEventArgs encounterInfo)
{
currInstID = 0xFFFF;
if (!isImport)
encounterInfo.encounter.CharName = charName;
}
void oFormActMain_BeforeLogLineRead(bool isImport, LogLineEventArgs logInfo)
{
Action aAction = new Action();
string logLine = logInfo.logLine;
string[] tmp = logInfo.logLine.Split(',');
if (tmp[0].Equals("timestamp"))
return;
try
{
aAction.timestamp = Convert.ToUInt32(tmp[0]);
aAction.instanceID = Convert.ToUInt16(tmp[1]);
aAction.sourceID = Convert.ToUInt32(tmp[2]);
aAction.sourceName = tmp[3];
aAction.targetID = Convert.ToUInt32(tmp[4]);
aAction.targetName = tmp[5];
aAction.attackID = Convert.ToUInt32(tmp[6]);
aAction.damage = Convert.ToInt32(tmp[7]);
aAction.isJA = (Convert.ToInt32(tmp[8]) == 1);
aAction.isCrit = (Convert.ToInt32(tmp[9]) == 1);
aAction.isMultiHit = (Convert.ToInt32(tmp[10]) == 1);
aAction.isMisc = (Convert.ToInt32(tmp[11]) == 1);
aAction.isMisc2 = (Convert.ToInt32(tmp[12]) == 1);
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message + "\n" + logLine);
return;
}
//TODO: deal with when the first thing they do is counter
if (aAction.targetID == 0 ||
(aAction.instanceID == 0 && currInstID == 0xFFFF))
return;
DateTime time = ActGlobals.oFormActMain.LastKnownTime;
int gts = ActGlobals.oFormActMain.GlobalTimeSorter;
if (aAction.instanceID == 0)
aAction.instanceID = currInstID;
SwingTypeEnum e;
string sourceName = aAction.sourceName == "YOU" ? "YOU" : aAction.sourceName + "_" + aAction.sourceID.ToString();
string targetName = aAction.targetName == "YOU" ? "YOU" : aAction.targetName + "_" + aAction.targetID.ToString();
string actionType = aAction.attackID.ToString();
string damageType = aAction.attackID.ToString();
if (aAction.damage < 0 && aAction.isMisc)
e = SwingTypeEnum.Healing;
else
e = SwingTypeEnum.Melee;
Dnum dmg = new Dnum(aAction.damage) * ((e == SwingTypeEnum.Healing) ? -1 : 1);
if (skillDict.ContainsKey(aAction.attackID))
{
actionType = skillDict[aAction.attackID].Name;
damageType = skillDict[aAction.attackID].Type;
}
MasterSwing ms = new MasterSwing(
Convert.ToInt32(e),
aAction.isCrit,
"",
dmg,
time,
gts,
actionType,
sourceName,
damageType,
targetName
);
if (aAction.instanceID != currInstID)
{
currInstID = aAction.instanceID;
ActGlobals.oFormActMain.ChangeZone(aAction.instanceID.ToString());
}
if (ActGlobals.oFormActMain.SetEncounter(time, sourceName, targetName))
ActGlobals.oFormActMain.AddCombatAction(ms);
}
private void InitializeSkillDict()
{
try
{
var asm = Assembly.GetExecutingAssembly();
string rsrcName = "PSO2ACT.skills.csv";
Stream f = asm.GetManifestResourceStream(rsrcName);
using (StreamReader sr = new StreamReader(f))
{
string line;
while ((line = sr.ReadLine()) != null)
{
string[] tmp = line.Split(',');
Skill s;
s.Name = tmp[0];
s.Type = tmp[2];
s.Comment = tmp[3];
if (skillDict.ContainsKey(Convert.ToUInt32(tmp[1])))
{
MessageBox.Show("Duplicate ID: " + line);
}
skillDict.Add(Convert.ToUInt32(tmp[1]), s);
}
}
f.Dispose();
}
catch (Exception ex)
{
throw ex;
}
}
DateTime ParseDateTime(string logLine)
{
string[] tmp = logLine.Split(',');
if (tmp[0] == "timestamp")
return DateTime.MinValue;
System.DateTime dateTime = new System.DateTime(1970, 1, 1, 0, 0, 0, 0);
dateTime = dateTime.AddSeconds(Convert.ToUInt32(tmp[0]));
return dateTime.ToLocalTime();
}
void LoadSettings()
{
xmlSettings.AddControlSetting("directory", Config.Controls["directory"]);
if (File.Exists(settingsFile))
{
FileStream fs = new FileStream(settingsFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
XmlTextReader xReader = new XmlTextReader(fs);
try
{
while (xReader.Read())
{
if (xReader.NodeType == XmlNodeType.Element)
{
if (xReader.LocalName == "SettingsSerializer")
{
xmlSettings.ImportFromXml(xReader);
}
}
}
}
catch (Exception ex)
{
lblStatus.Text = "Error loading settings: " + ex.Message;
}
xReader.Close();
}
}
void SaveSettings()
{
FileStream fs = new FileStream(settingsFile, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
XmlTextWriter xWriter = new XmlTextWriter(fs, Encoding.UTF8);
xWriter.Formatting = Formatting.Indented;
xWriter.Indentation = 1;
xWriter.IndentChar = '\t';
xWriter.WriteStartDocument(true);
xWriter.WriteStartElement("Config"); // <Config>
xWriter.WriteStartElement("SettingsSerializer"); // <Config><SettingsSerializer>
xmlSettings.ExportToXml(xWriter); // Fill the SettingsSerializer XML
xWriter.WriteEndElement(); // </SettingsSerializer>
xWriter.WriteEndElement(); // </Config>
xWriter.WriteEndDocument(); // Tie up loose ends (shouldn't be any)
xWriter.Flush(); // Flush the file buffer to disk
xWriter.Close();
}
void oFormActMain_UpdateCheckClicked()
{
try
{
string fileURL = @"http://www.vxyz.me/files/PSO2ACT/PSO2ACT.dll";
DateTime localDate = ActGlobals.oFormActMain.PluginGetSelfDateUtc(this);
DateTime remoteDate = GetRemoteLastUpdated(fileURL);
if (localDate < remoteDate)
{
DialogResult result = MessageBox.Show("There is an updated version of the PSO2 Parsing Plugin. Update it now?\n\n(If there is an update to ACT, you should click No and update ACT first.)", "New Version", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
ActPluginData pluginData = ActGlobals.oFormActMain.PluginGetSelfData(this);
WebClient w = new WebClient();
w.DownloadFile(fileURL, pluginData.pluginFile.FullName + ".tmp");
pluginData.pluginFile.Delete();
File.Move(pluginData.pluginFile.FullName + ".tmp", pluginData.pluginFile.FullName);
ThreadInvokes.CheckboxSetChecked(ActGlobals.oFormActMain, pluginData.cbEnabled, false);
Application.DoEvents();
ThreadInvokes.CheckboxSetChecked(ActGlobals.oFormActMain, pluginData.cbEnabled, true);
}
}
}
catch (Exception ex)
{
MessageBox.Show("Plugin Update Failed: " + ex.Message);
ActGlobals.oFormActMain.WriteExceptionLog(ex, "Plugin Update Failed");
}
Config.refreshFlag = true;
return;
}
DateTime GetRemoteLastUpdated(string url)
{
var request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "HEAD";
var response = (HttpWebResponse)request.GetResponse();
response.Close();
return response.LastModified.ToUniversalTime();
}
}
}