Skip to content

Commit

Permalink
Using Machine.FFXIV from @ravahn
Browse files Browse the repository at this point in the history
- Removed unused network handling classes
- Fixed status update message during matching in FFXIVPacketHandler
- Removed unused methods from IPacketHandler
- Removed state enum as it was only used locally
  • Loading branch information
easly1989 committed Nov 11, 2019
1 parent 993725b commit ec320a7
Show file tree
Hide file tree
Showing 15 changed files with 48 additions and 818 deletions.
1 change: 0 additions & 1 deletion DFAssist.Contracts/DFAssist.Contracts.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@
<Compile Include="Duty\EventType.cs" />
<Compile Include="Duty\MatchEndType.cs" />
<Compile Include="Duty\MatchType.cs" />
<Compile Include="Duty\MatchingState.cs" />
<Compile Include="IPacketHandler.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Repositories\IDataRepository.cs" />
Expand Down
11 changes: 0 additions & 11 deletions DFAssist.Contracts/Duty/MatchingState.cs

This file was deleted.

9 changes: 3 additions & 6 deletions DFAssist.Contracts/IPacketHandler.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
using DFAssist.Contracts.Duty;
using System;
using DFAssist.Contracts.Duty;

namespace DFAssist.Contracts
{
public delegate void PacketDelegate(int pid, EventType eventType, int[] args);

public interface IPacketHandler
{
event PacketDelegate OnEventReceived;

void Analyze(int processId, byte[] payload, ref MatchingState state);
void HandleMessage(byte[] message, Action<EventType, int[]> fireEvent);
}
}
10 changes: 1 addition & 9 deletions DFAssist.Core/DFAssist.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="DllInjector.cs" />
<Compile Include="Network\Connection.cs" />
<Compile Include="Network\FFXIVPacketHandler.cs" />
<Compile Include="Network\IpPacket.cs" />
<Compile Include="Network\Network.cs" />
<Compile Include="Network\ProcessNetwork.cs" />
<Compile Include="Network\TcpFlags.cs" />
<Compile Include="Network\TcpPacket.cs" />
<Compile Include="Network\TcpRow.cs" />
<Compile Include="Network\TcpTable.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Repositories\DataRepository.cs" />
<Compile Include="Repositories\LocalizationRepository.cs" />
Expand All @@ -95,7 +87,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json">
<Version>12.0.2</Version>
<Version>12.0.3</Version>
</PackageReference>
<PackageReference Include="Splat">
<Version>9.1.1</Version>
Expand Down
37 changes: 0 additions & 37 deletions DFAssist.Core/Network/Connection.cs

This file was deleted.

150 changes: 4 additions & 146 deletions DFAssist.Core/Network/FFXIVPacketHandler.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.IO;
using System.IO.Compression;
using System.Linq;
using DFAssist.Contracts;
using DFAssist.Contracts.Duty;
Expand All @@ -13,12 +11,9 @@ namespace DFAssist.Core.Network
public class FFXIVPacketHandler : IPacketHandler
// ReSharper restore InconsistentNaming
{
public event PacketDelegate OnEventReceived;

private readonly ILogger _logger;
private readonly IDataRepository _dataRepository;

private int _lastMember;
private byte _rouletteCode;

public FFXIVPacketHandler()
Expand All @@ -27,111 +22,7 @@ public FFXIVPacketHandler()
_dataRepository = Locator.Current.GetService<IDataRepository>();
}

public void Analyze(int processId, byte[] payload, ref MatchingState state)
{
try
{
while (true)
{
if (payload.Length < 4)
break;

var type = BitConverter.ToUInt16(payload, 0);
if (type == 0x0000 || type == 0x5252)
{
if (payload.Length < 28)
break;

var length = BitConverter.ToInt32(payload, 24);
if (length <= 0 || payload.Length < length)
break;

using (var messages = new MemoryStream(payload.Length))
{
using (var stream = new MemoryStream(payload, 0, length))
{
stream.Seek(40, SeekOrigin.Begin);

if (payload[33] == 0x00)
{
stream.CopyTo(messages);
}
else
{
// .Net DeflateStream (Force the previous 2 bytes)
stream.Seek(2, SeekOrigin.Current);
using (var z = new DeflateStream(stream, CompressionMode.Decompress))
{
z.CopyTo(messages);
}
}
}

messages.Seek(0, SeekOrigin.Begin);

var messageCount = BitConverter.ToUInt16(payload, 30);
for (var i = 0; i < messageCount; i++)
{
try
{
var buffer = new byte[4];
var read = messages.Read(buffer, 0, 4);
if (read < 4)
{
_logger.Write(
$"A: Length Error while analyzing Message: {read} {i}/{messageCount}",
LogLevel.Error);
break;
}

var messageLength = BitConverter.ToInt32(buffer, 0);

var message = new byte[messageLength];
messages.Seek(-4, SeekOrigin.Current);
messages.Read(message, 0, messageLength);

HandleMessage(processId, message, ref state);
}
catch (Exception ex)
{
_logger.Write(ex, "A: Error while analyzing Message", LogLevel.Error);
}
}
}

if (length < payload.Length)
{
// Packets still need to be processed
payload = payload.Skip(length).ToArray();
continue;
}
}
else
{
// Forward-Cut packet workaround
// Discard one truncated packet and find just the next packet
for (var offset = 0; offset < payload.Length - 2; offset++)
{
var possibleType = BitConverter.ToUInt16(payload, offset);
if (possibleType != 0x5252)
continue;

payload = payload.Skip(offset).ToArray();
Analyze(processId, payload, ref state);
break;
}
}

break;
}
}
catch (Exception ex)
{
_logger.Write(ex, "A: Error while handling Message", LogLevel.Error);
}
}

private void HandleMessage(int pid, byte[] message, ref MatchingState state)
public void HandleMessage(byte[] message, Action<EventType, int[]> fireEvent)
{
try
{
Expand Down Expand Up @@ -177,14 +68,6 @@ private void HandleMessage(int pid, byte[] message, ref MatchingState state)
}
else if (opcode == 0x008F) // 5.1 Duties
{
// ReSharper disable UnusedVariable
var status = data[0];
var reason = data[4];
// ReSharper restore UnusedVariable

state = MatchingState.QUEUED;

_rouletteCode = data[20];
_rouletteCode = data[8];

if (_rouletteCode != 0 && (data[15] == 0 || data[15] == 64)) // Roulette, on Korean Server || on Global Server
Expand All @@ -210,8 +93,7 @@ private void HandleMessage(int pid, byte[] message, ref MatchingState state)
var matchedRoulette = BitConverter.ToUInt16(data, 2);
var matchedCode = BitConverter.ToUInt16(data, 20);

state = MatchingState.MATCHED;
FireEvent(pid, EventType.MATCH_ALERT, new int[] { matchedRoulette, matchedCode });
fireEvent?.Invoke(EventType.MATCH_ALERT, new int[] { matchedRoulette, matchedCode });

var instanceString = $"{matchedCode} - {_dataRepository.GetInstance(matchedCode).Name}";
_logger.Write(matchedRoulette != 0
Expand All @@ -226,8 +108,6 @@ private void HandleMessage(int pid, byte[] message, ref MatchingState state)
{
if (data[3] != 0) return;

state = MatchingState.IDLE;

_logger.Write("Duty Canceled!", LogLevel.Debug);
}
else if (opcode == 0x0121)
Expand All @@ -246,25 +126,8 @@ private void HandleMessage(int pid, byte[] message, ref MatchingState state)
var dps = data[12];
var dpsMax = data[13];
// ReSharper restore UnusedVariable

var member = tank * 10000 + dps * 100 + healer;

switch (state)
{
case MatchingState.MATCHED when _lastMember != member:
// Plugin started with duty finder in progress
case MatchingState.IDLE:
// We get here when the queue is stopped by someone else (?)
state = MatchingState.QUEUED;
break;
case MatchingState.QUEUED:
// in queue
break;
}

_lastMember = member;

var memberinfo = $"Tanks: {tank}, Healers: {healer}, Dps: {dps} | Total Members: {member}";

var memberinfo = $"Tanks: {tank}/{tankMax}, Healers: {healer}/{healerMax}, Dps: {dps}/{dpsMax}";
_logger.Write($"Q: Matching State Updated [{memberinfo}]", LogLevel.Debug);
}
else if (opcode == 0x00AE) // Participant check status packet (received after matching)
Expand All @@ -287,10 +150,5 @@ private void HandleMessage(int pid, byte[] message, ref MatchingState state)
_logger.Write(ex, "A: Error while analyzing Message", LogLevel.Error);
}
}

private void FireEvent(int pid, EventType eventType, int[] args)
{
OnEventReceived?.Invoke(pid, eventType, args);
}
}
}
55 changes: 0 additions & 55 deletions DFAssist.Core/Network/IpPacket.cs

This file was deleted.

Loading

0 comments on commit ec320a7

Please sign in to comment.