Skip to content

Commit

Permalink
Connection data (#317)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexMacocian authored Jul 24, 2023
1 parent 6e8333d commit c1b7000
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Daybreak/Daybreak.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<LangVersion>preview</LangVersion>
<ApplicationIcon>Daybreak.ico</ApplicationIcon>
<IncludePackageReferencesDuringMarkupCompilation>true</IncludePackageReferencesDuringMarkupCompilation>
<Version>0.9.8.90</Version>
<Version>0.9.8.91</Version>
<EnableWindowsTargeting>true</EnableWindowsTargeting>
<UserSecretsId>cfb2a489-db80-448d-a969-80270f314c46</UserSecretsId>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
Expand Down
8 changes: 8 additions & 0 deletions Daybreak/Models/Guildwars/ConnectionData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System.Net;

namespace Daybreak.Models.Guildwars;

public sealed class ConnectionData
{
public IPAddress? IPAddress { get; init; }
}
12 changes: 12 additions & 0 deletions Daybreak/Models/Interop/IPAddressContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Runtime.InteropServices;

namespace Daybreak.Models.Interop;

[StructLayout(LayoutKind.Sequential)]
public readonly struct IPAddressContext
{
public readonly byte Byte1;
public readonly byte Byte2;
public readonly byte Byte3;
public readonly byte Byte4;
}
6 changes: 6 additions & 0 deletions Daybreak/Services/Scanner/GuildwarsMemoryCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public sealed class GuildwarsMemoryCache : IGuildwarsMemoryCache
private readonly CachedData<SessionData?> sessionDataCache = new();
private readonly CachedData<UserData?> userDataCache = new();
private readonly CachedData<MainPlayerData?> mainPlayerDataCache = new();
private readonly CachedData<ConnectionData?> connectionDataCache = new();

public GuildwarsMemoryCache(
IGuildwarsMemoryReader guildwarsMemoryReader,
Expand Down Expand Up @@ -76,6 +77,11 @@ public GuildwarsMemoryCache(
return this.ReadDataInternal(this.mainPlayerDataCache, this.guildwarsMemoryReader.ReadMainPlayerData, cancellationToken);
}

public Task<ConnectionData?> ReadConnectionData(CancellationToken cancellationToken)
{
return this.ReadDataInternal(this.connectionDataCache, this.guildwarsMemoryReader.ReadConnectionData, cancellationToken);
}

private async Task<T?> ReadDataInternal<T>(CachedData<T?> cachedData, Func<CancellationToken, Task<T?>> task, CancellationToken cancellationToken)
{
if (DateTime.Now - cachedData.SetTime <= TimeSpan.FromMilliseconds(this.liveOptions.Value.MemoryReaderFrequency))
Expand Down
28 changes: 28 additions & 0 deletions Daybreak/Services/Scanner/GuildwarsMemoryReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using System.Globalization;
using System.Linq;
using System.Logging;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
Expand Down Expand Up @@ -176,6 +177,16 @@ public void Stop()
return Task.Run(() => this.SafeReadGameMemory(this.ReadMainPlayerDataInternal), cancellationToken);
}

public Task<ConnectionData?> ReadConnectionData(CancellationToken cancellationToken)
{
if (this.memoryScanner.Scanning is false)
{
return Task.FromResult<ConnectionData?>(default);
}

return Task.Run(() => this.SafeReadGameMemory(this.ReadConnectionData), cancellationToken);
}

private async Task InitializeSafe(Process process, ScopedLogger<GuildwarsMemoryReader> scopedLogger)
{
if (this.memoryScanner.Process is null ||
Expand Down Expand Up @@ -608,6 +619,23 @@ private async Task ResilientBeginScanner(ScopedLogger<GuildwarsMemoryReader> sco
};
}

private ConnectionData? ReadConnectionData()
{
/*
* IP Address in long format is at one of the following addresses:
* startAddress + 00629204 -> +0x18 -> +0x44 -> +0x1A4
*/

var ipAddressContext = this.memoryScanner.ReadPtrChain<IPAddressContext>(this.memoryScanner.ModuleStartAddress, finalPointerOffset: 0x1A4, 0x00629204, 0x18, 0x44);

if (!IPAddress.TryParse($"{ipAddressContext.Byte1}.{ipAddressContext.Byte2}.{ipAddressContext.Byte3}.{ipAddressContext.Byte4}", out var ipAddress))
{
return default;
}

return new ConnectionData { IPAddress = ipAddress };
}

private uint GetPlayerIdPointer()
{
if (this.playerIdPointer == 0)
Expand Down
1 change: 1 addition & 0 deletions Daybreak/Services/Scanner/IGuildwarsMemoryCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ public interface IGuildwarsMemoryCache
Task<SessionData?> ReadSessionData(CancellationToken cancellationToken);
Task<UserData?> ReadUserData(CancellationToken cancellationToken);
Task<MainPlayerData?> ReadMainPlayerData(CancellationToken cancellationToken);
Task<ConnectionData?> ReadConnectionData(CancellationToken cancellationToken);
}
1 change: 1 addition & 0 deletions Daybreak/Services/Scanner/IGuildwarsMemoryReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ public interface IGuildwarsMemoryReader
Task<UserData?> ReadUserData(CancellationToken cancellationToken);
Task<SessionData?> ReadSessionData(CancellationToken cancellationToken);
Task<MainPlayerData?> ReadMainPlayerData(CancellationToken cancellationToken);
Task<ConnectionData?> ReadConnectionData(CancellationToken cancellationToken);
void Stop();
}

0 comments on commit c1b7000

Please sign in to comment.