-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
151 lines (130 loc) · 7.62 KB
/
Program.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading;
using System.Timers;
using DS1Stats.Models;
using DS1Stats.Savegame;
using Newtonsoft.Json;
namespace DS1Stats
{
class Program
{
private static Dictionary<int, CharacterInfo> _characterInfoList;
private static string _outputDirectory;
static void Main(string[] args)
{
Console.OutputEncoding = Encoding.Unicode;
var saveGameDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), @"NBGI\DarkSouls\");
var saveGameFileName = "DRAKS0005.sl2";
var saveGameFile = Path.Combine(saveGameDirectory, saveGameFileName);
_outputDirectory = Directory.GetCurrentDirectory();
Console.WriteLine($"Using {saveGameFile}");
if (!File.Exists(saveGameFile))
{
Console.WriteLine("Could not find savegame file...");
Console.ReadKey();
return;
}
var fwatcher = new FileSystemWatcher
{
Path = saveGameDirectory,
Filter = saveGameFileName,
NotifyFilter = NotifyFilters.LastWrite
};
fwatcher.Changed += FwatcherOnChanged;
fwatcher.EnableRaisingEvents = true;
//build initial info
_characterInfoList = new Dictionary<int, CharacterInfo>();
DoParse(saveGameFile, _outputDirectory, _characterInfoList);
//Timer to update "time since last death"
var timer = new System.Timers.Timer
{
AutoReset = true,
Interval = 1000,
Enabled = true
};
timer.Elapsed += TimerOnElapsed;
Console.WriteLine("Monitoring save game file for changes. Press Q to quit.");
while (Console.ReadKey().Key != ConsoleKey.Q) { }
}
private static void TimerOnElapsed(object sender, ElapsedEventArgs elapsedEventArgs)
{
foreach (var characterInfo in _characterInfoList)
{
var timeSpan = (DateTime.UtcNow - characterInfo.Value.LastDeath);
File.WriteAllText(Path.Combine(_outputDirectory, $"ds1counter_{characterInfo.Key}_timespan_last_death.txt"), timeSpan.ToString(@"hh\:mm\:ss"));
}
}
private static void FwatcherOnChanged(object sender, FileSystemEventArgs fileSystemEventArgs)
{
Console.WriteLine($"Detected changes in file {fileSystemEventArgs.FullPath} Parsing...");
DoParse(fileSystemEventArgs.FullPath, _outputDirectory, _characterInfoList);
}
private static void DoParse(string saveGameFile, string outputDirectory, Dictionary<int, CharacterInfo> characterInfoList, int maxNumberOfTries = 20)
{
FileStream fileStream;
if (!TryOpenFile(saveGameFile, maxNumberOfTries, out fileStream))
{
Console.WriteLine("Could not get read-mode on savegame file. Aborting attempt...");
return;
}
//Buffer the save game so we don't block it
var memoryStream = new MemoryStream();
fileStream.CopyTo(memoryStream);
fileStream.Close();
_characterInfoList = SaveGameParser.ParseSaveGame(memoryStream, characterInfoList);
WriteOutputFiles(outputDirectory, characterInfoList);
}
private static void WriteOutputFiles(string outputDirectory, Dictionary<int, CharacterInfo> characterInfoList)
{
foreach (var characterInfo in _characterInfoList)
{
Console.WriteLine($"Saving data to files for {characterInfo.Value.Name} in slot #{characterInfo.Key}");
File.WriteAllText(Path.Combine(outputDirectory, $"ds1counter_{characterInfo.Key}_deaths.txt"), characterInfo.Value.Deaths.ToString());
File.WriteAllText(Path.Combine(outputDirectory, $"ds1counter_{characterInfo.Key}_name.txt"), characterInfo.Value.Name);
File.WriteAllText(Path.Combine(outputDirectory, $"ds1counter_{characterInfo.Key}_time_last_death.txt"), characterInfo.Value.LastDeath.ToLocalTime().ToLongTimeString());
File.WriteAllText(Path.Combine(outputDirectory, $"ds1counter_{characterInfo.Key}_session_deaths.txt"), characterInfo.Value.SessionDeaths.ToString());
File.WriteAllText(Path.Combine(outputDirectory, $"ds1counter_{characterInfo.Key}_session_deaths_total_deaths.txt"), $"{characterInfo.Value.SessionDeaths} / {characterInfo.Value.Deaths}");
File.WriteAllText(Path.Combine(outputDirectory, $"ds1counter_{characterInfo.Key}_average_life_length.txt"), characterInfo.Value.CalculateAverageLifeLength().ToString(@"hh\:mm\:ss"));
File.WriteAllText(Path.Combine(outputDirectory, $"ds1counter_{characterInfo.Key}_stats_Vitality.txt"), characterInfo.Value.Vitality.ToString());
File.WriteAllText(Path.Combine(outputDirectory, $"ds1counter_{characterInfo.Key}_stats_Attunement.txt"), characterInfo.Value.Attunement.ToString());
File.WriteAllText(Path.Combine(outputDirectory, $"ds1counter_{characterInfo.Key}_stats_Endurance.txt"), characterInfo.Value.Endurance.ToString());
File.WriteAllText(Path.Combine(outputDirectory, $"ds1counter_{characterInfo.Key}_stats_Strength.txt"), characterInfo.Value.Strength.ToString());
File.WriteAllText(Path.Combine(outputDirectory, $"ds1counter_{characterInfo.Key}_stats_Dexterity.txt"), characterInfo.Value.Dexterity.ToString());
File.WriteAllText(Path.Combine(outputDirectory, $"ds1counter_{characterInfo.Key}_stats_Intelligence.txt"), characterInfo.Value.Intelligence.ToString());
File.WriteAllText(Path.Combine(outputDirectory, $"ds1counter_{characterInfo.Key}_stats_Faith.txt"), characterInfo.Value.Faith.ToString());
File.WriteAllText(Path.Combine(outputDirectory, $"ds1counter_{characterInfo.Key}_stats_Humanity.txt"), characterInfo.Value.Humanity.ToString());
File.WriteAllText(Path.Combine(outputDirectory, $"ds1counter_{characterInfo.Key}_stats_Resistance.txt"), characterInfo.Value.Resistance.ToString());
File.WriteAllText(Path.Combine(outputDirectory, $"ds1counter_{characterInfo.Key}_stats_Level.txt"), characterInfo.Value.Level.ToString());
File.WriteAllText(Path.Combine(outputDirectory, $"ds1counter_{characterInfo.Key}_stats_Souls.txt"), characterInfo.Value.Souls.ToString());
File.WriteAllText(Path.Combine(outputDirectory, $"ds1counter_{characterInfo.Key}_stats_EarnedSouls.txt"), characterInfo.Value.EarnedSouls.ToString());
File.WriteAllText(Path.Combine(outputDirectory, "ds1counter.json"), JsonConvert.SerializeObject(characterInfoList, Formatting.Indented));
}
}
private static bool TryOpenFile(string saveGameFile, int maxNumberOfTries, out FileStream fileStream)
{
//Haxy way to wait for the file to be unlocked after an update
var attemptNumber = 0;
while (true)
{
if (++attemptNumber > maxNumberOfTries)
{
fileStream = null;
return false;
}
try
{
fileStream = File.Open(saveGameFile, FileMode.Open);
break;
}
catch (IOException)
{
Thread.Sleep(100);
}
}
return true;
}
}
}