forked from ToadsworthLP/desktoptale
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
91 lines (80 loc) · 2.82 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Ipc;
using System.Threading;
using CommandLine;
using Desktoptale.Messages;
using Desktoptale.Messaging;
namespace Desktoptale
{
public static class Program
{
public const string AppId = "Desktoptale-14a4cfe9-1a59-45a4-b139-870346c425cb";
public const string IpcChannelUri = "DesktoptaleIPC";
[STAThread]
public static void Main(string[] args)
{
if (args != null && args.Length > 0)
{
WindowsUtils.AttachConsole();
Console.WriteLine();
Parser parser = new Parser(config =>
{
config.HelpWriter = Console.Out;
});
parser.ParseArguments<Settings>(args)
.WithParsed<Settings>((settings) => Run(settings, args))
.WithNotParsed(e =>
{
WindowsUtils.FreeConsole();
});
}
else
{
var settings = new Settings();
Run(settings, args);
}
}
private static void Run(Settings settings, string[] args)
{
using (Mutex mutex = new Mutex(false, AppId))
{
if (!mutex.WaitOne(0))
{
RunOtherInstance(args);
return;
}
RunFirstInstance(settings);
}
}
private static void RunFirstInstance(Settings settings)
{
settings.Validate();
IpcChannel channel = new IpcChannel(AppId);
ChannelServices.RegisterChannel(channel, false);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(IpcObject), IpcChannelUri, WellKnownObjectMode.Singleton);
Desktoptale game = new Desktoptale(settings);
game.Run();
game.Dispose();
if (settings.PrintRegistryKeys)
{
WindowsUtils.FreeConsole();
}
}
private static void RunOtherInstance(string[] argsToPass)
{
IpcChannel channel = new IpcChannel();
ChannelServices.RegisterChannel(channel, false);
IpcObject ipcObject = (IpcObject)Activator.GetObject(typeof(IpcObject), $"ipc://{AppId}/{IpcChannelUri}");
ipcObject.SendOtherInstanceStartedMessage(argsToPass);
}
private class IpcObject : MarshalByRefObject
{
public void SendOtherInstanceStartedMessage(string[] args)
{
MessageBus.Send(new OtherInstanceStartedMessage() { Args = args });
}
}
}
}