Skip to content
This repository has been archived by the owner on Nov 10, 2022. It is now read-only.

Commit

Permalink
Hello LeagueClient.Exited!
Browse files Browse the repository at this point in the history
  • Loading branch information
mikaeldui committed Feb 20, 2022
1 parent 1209412 commit e5067cb
Show file tree
Hide file tree
Showing 12 changed files with 102 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static MemberDeclarationSyntax[] RmsEvent(string topic, string identifier
.WithAccessorList(AccessorList(new SyntaxList<AccessorDeclarationSyntax>(new[]
{
AccessorDeclaration(SyntaxKind.AddAccessorDeclaration, ParseStatement(
$"if ({privateEventIdentifier} == null) EventRouter.Subscribe(\"{topic}\", ({typeName} args) => {privateEventIdentifier}?.Invoke(this, args)); {privateEventIdentifier} += value;")
$"if ({privateEventIdentifier} == null) EventRouter.Subscribe(\"{topic}\", (RmsChangeType changeType, {typeName} args) => {privateEventIdentifier}?.Invoke(this, changeType, args)); {privateEventIdentifier} += value;")
.ToBlock()),
AccessorDeclaration(SyntaxKind.RemoveAccessorDeclaration, ParseStatement(
$"{privateEventIdentifier} -= value; if ({privateEventIdentifier} == null) EventRouter.Unsubscribe(\"{topic}\"); ")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace RiotGames.LeagueOfLegends.LeagueClient;
public class LeagueClientWebSocketsTests
{
private static readonly bool IS_LEAGUE_CLIENT_RUNNING = Process.GetProcesses()
.Any(p => p.ProcessName == LeagueClientLockFile.LEAGUECLIENT_DEFAULT_PROCESS_NAME);
.Any(p => p.ProcessName == LeagueClientLockFile.LEAGUE_CLIENT_DEFAULT_PROCESS_NAME);

[TestMethod]
public async Task TestSomething()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System.Diagnostics;
using RiotGames.Messaging;
// ReSharper disable UnusedMember.Global

namespace RiotGames.LeagueOfLegends.LeagueClient;

/// <summary>
/// You can use it to communicate with the League Client. Didn't want to name it LeagueClientClient.
/// </summary>
public partial class LeagueClient
{
private Process? _process;

/// <summary>
/// Occurs when the League Client exits.
/// </summary>
public event EventHandler Exited
{
add
{
_process ??= LockFile.GetProcess();

_process.Exited += value;

}
remove
{
_process!.Exited -= value;

//if (_process.Exited == null)
//{
// _process.Dispose();
// _process = null;
//}
}
}
}
20 changes: 14 additions & 6 deletions RiotGames.LeagueOfLegends.LeagueClient.Client/LeagueClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Diagnostics;
using RiotGames.Messaging;
// ReSharper disable UnusedMember.Global

namespace RiotGames.LeagueOfLegends.LeagueClient;

Expand All @@ -15,8 +17,8 @@ internal LeagueClient(LeagueClientHttpClient httpClient, RmsEventRouter eventRou
{
}

public LeagueClient(string processName = LeagueClientLockFile.LEAGUECLIENT_DEFAULT_PROCESS_NAME,
string lockfilePath = LeagueClientLockFile.LEAGUECLIENT_DEFAULT_LOCKFILE_PATH) : base(processName, lockfilePath)
public LeagueClient(string processName = LeagueClientLockFile.LEAGUE_CLIENT_DEFAULT_PROCESS_NAME,
string lockfilePath = LeagueClientLockFile.LEAGUE_CLIENT_DEFAULT_LOCKFILE_PATH) : base(processName, lockfilePath)
{
}

Expand All @@ -28,8 +30,8 @@ public partial class LeagueOfLegendsClient : LeagueClientBase
/// <summary>
/// Use if you don't need <see cref="LeagueClient" /> endpoints and <see cref="TeamfightTacticsClient" /> endpoints.
/// </summary>
public LeagueOfLegendsClient(string processName = LeagueClientLockFile.LEAGUECLIENT_DEFAULT_PROCESS_NAME,
string lockfilePath = LeagueClientLockFile.LEAGUECLIENT_DEFAULT_LOCKFILE_PATH) : base(processName,
public LeagueOfLegendsClient(string processName = LeagueClientLockFile.LEAGUE_CLIENT_DEFAULT_PROCESS_NAME,
string lockfilePath = LeagueClientLockFile.LEAGUE_CLIENT_DEFAULT_LOCKFILE_PATH) : base(processName,
lockfilePath)
{
}
Expand All @@ -40,10 +42,16 @@ public partial class TeamfightTacticsClient : LeagueClientBase
/// <summary>
/// Use if you don't need <see cref="LeagueClient" /> endpoints and <see cref="LeagueOfLegendsClient" /> endpoints.
/// </summary>
public TeamfightTacticsClient(string processName = LeagueClientLockFile.LEAGUECLIENT_DEFAULT_PROCESS_NAME,
string lockfilePath = LeagueClientLockFile.LEAGUECLIENT_DEFAULT_LOCKFILE_PATH) : base(processName,
public TeamfightTacticsClient(string processName = LeagueClientLockFile.LEAGUE_CLIENT_DEFAULT_PROCESS_NAME,
string lockfilePath = LeagueClientLockFile.LEAGUE_CLIENT_DEFAULT_LOCKFILE_PATH) : base(processName,
lockfilePath)
{
}
}

public override void Dispose()
{
base.Dispose();
_process?.Dispose();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,23 @@ public abstract class LeagueClientBase : IDisposable
private const string LEAGUE_CLIENT_USERNAME = "riot";
internal readonly RmsEventRouter EventRouter;
internal readonly LeagueClientHttpClient HttpClient;
protected LeagueClientLockFile LockFile;

internal LeagueClientBase(LeagueClientHttpClient httpClient, RmsEventRouter eventRouter)
{
HttpClient = httpClient;
EventRouter = eventRouter;
}

protected LeagueClientBase(string processName = LeagueClientLockFile.LEAGUECLIENT_DEFAULT_PROCESS_NAME,
string lockfilePath = LeagueClientLockFile.LEAGUECLIENT_DEFAULT_LOCKFILE_PATH)
protected LeagueClientBase(string processName = LeagueClientLockFile.LEAGUE_CLIENT_DEFAULT_PROCESS_NAME,
string lockfilePath = LeagueClientLockFile.LEAGUE_CLIENT_DEFAULT_LOCKFILE_PATH)
{
var lockfile = lockfilePath == LeagueClientLockFile.LEAGUECLIENT_DEFAULT_LOCKFILE_PATH
LockFile = lockfilePath == LeagueClientLockFile.LEAGUE_CLIENT_DEFAULT_LOCKFILE_PATH
? LeagueClientLockFile.FromProcess(processName)
: LeagueClientLockFile.FromPath(lockfilePath);

HttpClient = new LeagueClientHttpClient(LEAGUE_CLIENT_USERNAME, lockfile.Password, lockfile.Port);
EventRouter = new RmsEventRouter(LEAGUE_CLIENT_USERNAME, lockfile.Password, lockfile.Port);
HttpClient = new LeagueClientHttpClient(LEAGUE_CLIENT_USERNAME, LockFile.Password, LockFile.Port);
EventRouter = new RmsEventRouter(LEAGUE_CLIENT_USERNAME, LockFile.Password, LockFile.Port);
}

public virtual void Dispose()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
namespace RiotGames.LeagueOfLegends.LeagueClient;
using RiotGames.Messaging;

public delegate void LeagueClientEventHandler<in T>(object sender, T args);
namespace RiotGames.LeagueOfLegends.LeagueClient;

public delegate void LeagueClientEventHandler<T>(object sender, LeagueClientEventArgs<T> args);

public class LeagueClientEventArgs<T> : RmsEventArgs<T>
{
public LeagueClientEventArgs(RmsChangeType changeType, T data) : base(changeType, data)
{
}
}

internal static class LeagueClientEventExtensions
{
internal static void Invoke<T>(this LeagueClientEventHandler<T> eventHandler, object sender,
RmsChangeType changeType, T data) =>
eventHandler.Invoke(sender, new LeagueClientEventArgs<T>(changeType, data));
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Exceptions\LeagueClientException.cs" />
<Compile Include="$(MSBuildThisFileDirectory)LeagueClient.cs" />
<Compile Include="$(MSBuildThisFileDirectory)LeagueClient.g.cs" />
<Compile Include="$(MSBuildThisFileDirectory)LeagueClient.Process.cs" />
<Compile Include="$(MSBuildThisFileDirectory)LeagueClientBase.cs" />
<Compile Include="$(MSBuildThisFileDirectory)LeagueClientEnumerables.cs" />
<Compile Include="$(MSBuildThisFileDirectory)LeagueClientEventHandler.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// ReSharper disable UnusedMember.Global
namespace RiotGames.Messaging;

public enum RmsEventType
// TODO: Created, updated, deleted...
public enum RmsChangeType
{
Create,
Update,
Expand Down
6 changes: 3 additions & 3 deletions RiotGames.Messaging.Client/Messages/RmsEventMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ namespace RiotGames.Messaging;
/// <summary>
/// Will be made internal soon.
/// </summary>
[DebuggerDisplay("Topic = {Topic} EventType = {EventType} Uri = {Uri} : {Data}")]
[DebuggerDisplay("Topic = {Topic} ChangeType = {ChangeType} Uri = {Uri} : {Data}")]
public class RmsEventMessage : WampMessage<RmsTypeCode>
{
public RmsEventMessage(RmsTypeCode messageCode, params JsonElement[] elements) : base(messageCode, elements)
{
Topic = elements[0].GetString() ?? throw new RmsException("The WAMP event message didn't have any topic!");
Data = elements[1].GetProperty("data");
EventType = Enum.Parse<RmsEventType>(elements[1].GetProperty("eventType").GetString());
ChangeType = Enum.Parse<RmsChangeType>(elements[1].GetProperty("changeType").GetString());
Uri = new Uri(
elements[1].GetProperty("uri").GetString() ??
throw new RmsException("The event message didn't have any Uri."), UriKind.Relative);
Expand All @@ -28,7 +28,7 @@ public RmsEventMessage(RmsTypeCode messageCode, params JsonElement[] elements) :
/// </summary>
public JsonElement Data { get; }

public RmsEventType EventType { get; }
public RmsChangeType ChangeType { get; }

public Uri Uri { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)Exceptions\RmsException.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Messages\RmsChangeType.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Messages\RmsEventMessage.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Messages\RmsEventType.cs" />
<Compile Include="$(MSBuildThisFileDirectory)RmsClient.cs" />
<Compile Include="$(MSBuildThisFileDirectory)RmsEventArgs.cs" />
<Compile Include="$(MSBuildThisFileDirectory)RmsEventRouter.cs" />
<Compile Include="$(MSBuildThisFileDirectory)RmsTypeCode.cs" />
</ItemGroup>
Expand Down
13 changes: 13 additions & 0 deletions RiotGames.Messaging.Client/RmsEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace RiotGames.Messaging;

public class RmsEventArgs<T> : EventArgs
{
public RmsEventArgs(RmsChangeType changeType, T data)
{
ChangeType = changeType;
Data = data;
}

public RmsChangeType ChangeType { get; }
public T Data { get; }
}
8 changes: 4 additions & 4 deletions RiotGames.Messaging.Client/RmsEventRouter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class RmsEventRouter : IDisposable

private readonly JsonSerializerOptions _jsonSerializerOptions = new() {PropertyNameCaseInsensitive = true};
private readonly RmsClient _rmsClient;
private readonly ConcurrentDictionary<string, Action<JsonElement>> _subscriptions = new();
private readonly ConcurrentDictionary<string, Action<RmsChangeType, JsonElement>> _subscriptions = new();
private CancellationTokenSource? _cancellationTokenSource = new();

public RmsEventRouter(string username, string password, ushort port)
Expand All @@ -30,7 +30,7 @@ public void Dispose()
/// <summary>
/// Will open a connection if there isn't one.
/// </summary>
public void Subscribe<TData>(string topic, Action<TData> handler)
public void Subscribe<TData>(string topic, Action<RmsChangeType, TData> handler)
{
Task.Run(async () =>
{
Expand All @@ -53,7 +53,7 @@ public void Subscribe<TData>(string topic, Action<TData> handler)
Console.WriteLine("Message received");

if (!_subscriptions.TryGetValue(response.Topic, out var subscriber)) continue;
subscriber?.Invoke(response.Data);
subscriber?.Invoke(response.ChangeType, response.Data);
}
}, _cancellationTokenSource.Token).ConfigureAwait(false);
}
Expand All @@ -68,7 +68,7 @@ public void Subscribe<TData>(string topic, Action<TData> handler)
}

if (_subscriptions.TryAdd(topic,
data => handler.Invoke(data.Deserialize<TData>(_jsonSerializerOptions) ??
(changeType, data) => handler.Invoke(changeType, data.Deserialize<TData>(_jsonSerializerOptions) ??
throw new RmsException("Couldn't deserialize the event payload."))))
{
await _rmsClient.SubscribeAsync(topic);
Expand Down

0 comments on commit e5067cb

Please sign in to comment.