Skip to content

Commit 06b5a28

Browse files
committed
Added launch arguments to the examples
1 parent 49c6a07 commit 06b5a28

File tree

3 files changed

+70
-22
lines changed

3 files changed

+70
-22
lines changed

DiscordRPC.Example/Basic.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
namespace DiscordRPC.Example
66
{
7-
public class Basic : IExample
7+
class Basic : IExample
88
{
99
private DiscordRpcClient client;
1010

11-
public void Setup()
11+
public void Setup(Options opts)
1212
{
13-
client = new DiscordRpcClient("424087019149328395")
13+
client = new DiscordRpcClient(opts.ClientId, pipe: opts.Pipe)
1414
{
15-
Logger = new Logging.ConsoleLogger(Logging.LogLevel.Info, true)
15+
Logger = new Logging.ConsoleLogger(opts.LogLevel, true)
1616
};
1717

1818
client.OnReady += (sender, msg) =>

DiscordRPC.Example/Joining.cs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,26 @@
66

77
namespace DiscordRPC.Example
88
{
9-
public class Joining : IExample
9+
class Joining : IExample
1010
{
1111
private DiscordRpcClient client;
1212
private string lobbyId = "1234567890"; // Example lobby ID
1313
private CancellationTokenSource waitingForJoinCancellationTokenSource;
14-
private int partySize = 1; // Initial party size, can be updated as users join
14+
private int partySize = 2; // Initial party size, can be updated as users join
1515

16-
public void Setup()
16+
public void Setup(Options opts)
1717
{
1818
Random random = new Random();
1919
waitingForJoinCancellationTokenSource = new CancellationTokenSource();
20-
client = new DiscordRpcClient("424087019149328395")
21-
{
22-
Logger = new Logging.ConsoleLogger(Logging.LogLevel.Trace, true),
23-
};
2420

25-
// Register the URI scheme.
26-
// This is how Discord will launch your game when a user clicks on a join or spectate button.
27-
client.RegisterUriScheme();
21+
client = new DiscordRpcClient(opts.ClientId, pipe: opts.Pipe)
22+
{
23+
Logger = new Logging.ConsoleLogger(opts.LogLevel, true)
24+
};
25+
26+
// Register the URI scheme.
27+
// This is how Discord will launch your game when a user clicks on a join or spectate button.
28+
client.RegisterUriScheme();
2829

2930
// Listen to some events
3031
client.Subscribe(EventType.Join | EventType.JoinRequest); // Tell Unity we want to handle these events ourselves.
@@ -46,7 +47,7 @@ public void Setup()
4647
Max = 4,
4748
},
4849

49-
// // Set the secrets. This is how a launching app will figure out how to connect to the game.
50+
// Set the secrets. This is how a launching app will figure out how to connect to the game.
5051
Secrets = new()
5152
{
5253
Join = $"join:{lobbyId}"

DiscordRPC.Example/Program.cs

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,71 @@
77

88
namespace DiscordRPC.Example
99
{
10-
interface IExample
10+
class Options
11+
{
12+
public string Example { get; set; } = "Basic";
13+
public string ClientId { get; set; } = "424087019149328395";
14+
public int Pipe { get; set; } = -1;
15+
public Logging.LogLevel LogLevel { get; set; } = Logging.LogLevel.Info;
16+
}
17+
18+
interface IExample
1119
{
12-
void Setup();
20+
void Setup(Options opts);
1321
Task Run();
1422
void Teardown();
1523
}
1624

17-
class Program
25+
class Program
1826
{
1927
//Main Loop
2028
static void Main(string[] args)
2129
{
22-
IExample example;
30+
// Parse arguments
31+
Options opts = new Options();
32+
if (args.Length > 0)
33+
{
34+
foreach (string arg in args)
35+
{
36+
if (arg.StartsWith("--pipe="))
37+
{
38+
opts.Pipe = int.Parse(arg.Substring("--pipe=".Length));
39+
}
40+
else if (arg.StartsWith("--clientid="))
41+
{
42+
opts.ClientId = arg.Substring("--clientid=".Length);
43+
}
44+
else if (arg.StartsWith("--example="))
45+
{
46+
opts.Example = arg.Substring("--example=".Length);
47+
}
48+
else if (arg.StartsWith("--loglevel="))
49+
{
50+
if (Enum.TryParse<Logging.LogLevel>(arg.Substring("--loglevel=".Length), true, out var logLevel))
51+
{
52+
opts.LogLevel = logLevel;
53+
}
54+
else
55+
{
56+
Console.WriteLine($"Invalid log level: {arg.Substring("--loglevel=".Length)}");
57+
}
58+
}
59+
else
60+
{
61+
Console.WriteLine($"Unknown argument: {arg}");
62+
}
63+
}
64+
}
2365

24-
// example = new Basic();
25-
example = new Joining();
66+
// Run example
67+
IExample example = opts.Example switch
68+
{
69+
"Basic" => new Basic(),
70+
"Joining" => new Joining(),
71+
_ => throw new ArgumentException($"Unknown example: {opts.Example}")
72+
};
2673

27-
example.Setup();
74+
example.Setup(opts);
2875
example.Run().GetAwaiter().GetResult();
2976

3077
Console.WriteLine("Press any key to terminate");

0 commit comments

Comments
 (0)