forked from HearthSim/Hearthstone-Deck-Tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
110 lines (102 loc) · 2.18 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#region
using System;
using System.Diagnostics;
using System.IO;
using System.IO.Pipes;
using System.Linq;
using System.Net;
using System.Web;
#endregion
namespace HDTHelper
{
internal class Program
{
private static void Main(string[] args)
{
Console.Title = "HDT Helper";
if(args.Length > 0)
{
switch(args[0])
{
case "registerProtocol":
HDTProtocol.Register();
break;
case "deleteProtocol":
HDTProtocol.Delete();
break;
case "hdt:start":
StartHdt();
break;
case "hdt:sync":
HdtGeneral("sync");
break;
}
if(args[0].StartsWith("hdt:import="))
{
var value = args[0].Substring(11);
string json = null;
if(value.StartsWith("%7B")) // encoded '{'
json = HttpUtility.UrlDecode(value);
else if(value.StartsWith("http://"))
json = GetJsonFromUrl(value);
else if(File.Exists(value))
json = GetJsonFromFile(value);
if(json != null)
HdtImport(json);
}
}
}
private static void HdtImport(string json)
{
StartHdt();
using(var pipe = new NamedPipeClientStream(".", "hdtimport", PipeDirection.Out, PipeOptions.Asynchronous))
{
pipe.Connect();
using(var sw = new StreamWriter(pipe))
sw.WriteLine(json);
}
}
private static void HdtGeneral(string message)
{
using(var pipe = new NamedPipeClientStream(".", "hdtgeneral", PipeDirection.Out, PipeOptions.Asynchronous))
{
pipe.Connect();
using(var sw = new StreamWriter(pipe))
sw.Write(message);
}
}
private static void StartHdt()
{
if(!Process.GetProcessesByName("Hearthstone Deck Tracker").Any())
{
Console.Write("Starting HDT...");
var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Hearthstone Deck Tracker.exe");
Process.Start(path);
}
}
private static string GetJsonFromFile(string path)
{
try
{
using(var sr = new StreamReader(path))
return sr.ReadToEnd();
}
catch(Exception)
{
return "";
}
}
private static string GetJsonFromUrl(string url)
{
try
{
using(var wc = new WebClient())
return wc.DownloadString(url);
}
catch(Exception)
{
return "";
}
}
}
}