Skip to content

Commit cff059b

Browse files
authored
Record and Watch apps (jo3bingham#7)
* Add Record app for better recording of player sessions * Initialize Client from Tibia directory instead of .dat file * Add missing Map base class file * Update Extract app to handle new OXR recording files * Fix issue with skipping client login packet Add a way to kill Record app from the console without blocking the thread * Ignore all client packets when extracting map data * Add Watch app * Remove IsConnected property from Connection class * Add command line paramters to Extract app * Make use of parsed command line arguments in Extract app * Check recording string instead of function return to determine if a recording was supplied
1 parent 9ebc5a9 commit cff059b

22 files changed

Lines changed: 844 additions & 202 deletions

Apps/Extract/Program.cs

Lines changed: 167 additions & 115 deletions
Large diffs are not rendered by default.

Apps/Record/Program.cs

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
using System;
2+
using System.Diagnostics;
3+
using System.IO;
4+
5+
using OXGaming.TibiaAPI;
6+
using OXGaming.TibiaAPI.Constants;
7+
8+
namespace Record
9+
{
10+
class Program
11+
{
12+
static readonly object _writerLock = new object();
13+
14+
static readonly Stopwatch _stopWatch = new Stopwatch();
15+
16+
static BinaryWriter _binaryWriter;
17+
18+
static void Main(string[] args)
19+
{
20+
try
21+
{
22+
var tibiaDirectory = args.Length > 0 ? args[1] : string.Empty;
23+
24+
using (var client = new Client(tibiaDirectory))
25+
{
26+
var utcNow = DateTime.UtcNow;
27+
var filename = $"{utcNow.Day}_{utcNow.Month}_{utcNow.Year}__{utcNow.Hour}_{utcNow.Minute}_{utcNow.Second}.oxr";
28+
var recordingDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Recordings");
29+
if (!Directory.Exists(recordingDirectory))
30+
{
31+
Directory.CreateDirectory(recordingDirectory);
32+
}
33+
34+
_binaryWriter = new BinaryWriter(File.OpenWrite(Path.Combine(recordingDirectory, filename)));
35+
_binaryWriter.Write(client.Version);
36+
37+
client.Proxy.OnReceivedClientMessage += Proxy_OnReceivedClientMessage;
38+
client.Proxy.OnReceivedServerMessage += Proxy_OnReceivedServerMessage;
39+
40+
// Disable packet parsing as we only care about the raw, decrypted packets and speed.
41+
client.StartProxy(enablePacketParsing: false);
42+
43+
while (Console.ReadLine() != "quit")
44+
{
45+
}
46+
47+
client.StopProxy();
48+
49+
client.Proxy.OnReceivedClientMessage -= Proxy_OnReceivedClientMessage;
50+
client.Proxy.OnReceivedServerMessage -= Proxy_OnReceivedServerMessage;
51+
52+
// Give the proxy time to quit, and any pending packets to be consumed.
53+
System.Threading.Thread.Sleep(1000);
54+
}
55+
}
56+
catch (Exception ex)
57+
{
58+
Console.WriteLine(ex);
59+
}
60+
finally
61+
{
62+
if (_binaryWriter != null)
63+
{
64+
_binaryWriter.Close();
65+
}
66+
67+
if (_stopWatch.IsRunning)
68+
{
69+
_stopWatch.Stop();
70+
}
71+
}
72+
}
73+
74+
private static void Proxy_OnReceivedClientMessage(byte[] data)
75+
{
76+
WriteMessage(PacketType.Client, data);
77+
}
78+
79+
private static void Proxy_OnReceivedServerMessage(byte[] data)
80+
{
81+
WriteMessage(PacketType.Server, data);
82+
}
83+
84+
private static void WriteMessage(PacketType packetType, byte[] data)
85+
{
86+
if (!_stopWatch.IsRunning)
87+
{
88+
_stopWatch.Start();
89+
}
90+
91+
var timestamp = _stopWatch.ElapsedMilliseconds;
92+
93+
lock (_writerLock)
94+
{
95+
try
96+
{
97+
_binaryWriter.Write((byte)packetType);
98+
_binaryWriter.Write(timestamp);
99+
_binaryWriter.Write(data.Length);
100+
_binaryWriter.Write(data);
101+
}
102+
catch (Exception ex)
103+
{
104+
Console.WriteLine(ex);
105+
}
106+
}
107+
}
108+
}
109+
}

Apps/Record/Record.csproj

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp2.1</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<ProjectReference Include="..\..\TibiaAPI\TibiaAPI.csproj" />
10+
</ItemGroup>
11+
</Project>

0 commit comments

Comments
 (0)