/
Program.cs
198 lines (176 loc) · 7.34 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using GameAnalyticsSDK.Net;
using LumiSoft.Net.STUN.Client;
using PlasmaShared;
using Steamworks;
using ZkData;
namespace ChobbyLauncher
{
public static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
public static void Main(string[] args)
{
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture;
Trace.Listeners.Add(new ConsoleTraceListener());
var logStringBuilder = new StringBuilder();
var threadSafeWriter = TextWriter.Synchronized(new StringWriter(logStringBuilder));
Trace.Listeners.Add(new TextWriterTraceListener(threadSafeWriter));
try
{
GameAnalytics.Initialize(GlobalConst.GameAnalyticsGameKey, GlobalConst.GameAnalyticsToken);
}
catch (Exception ex)
{
Trace.TraceError("Error starting GameAnalytics: {0}", ex);
}
string chobbyTag, engineOverride;
ulong connectLobbyID;
ParseCommandLine(args, out connectLobbyID, out chobbyTag, out engineOverride);
var startupPath = Path.GetDirectoryName(Path.GetFullPath(Application.ExecutablePath));
if (!SpringPaths.IsDirectoryWritable(startupPath))
{
MessageBox.Show("Please move this program to a writable folder",
"Cannot write to startup folder",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
try
{
GameAnalytics.AddErrorEvent(EGAErrorSeverity.Error, "Wrapper cannot start, folder not writable");
}
catch (Exception ex)
{
Trace.TraceError("Error adding GA error event: {0}", ex);
}
Environment.Exit(0);
return;
}
try
{
var chobbyla = new Chobbyla(startupPath, chobbyTag, engineOverride);
RunWrapper(chobbyla, connectLobbyID, threadSafeWriter, logStringBuilder);
}
catch (Exception ex)
{
Trace.TraceError("Error starting chobby: {0}", ex);
MessageBox.Show(ex.ToString(), "Error starting Chobby", MessageBoxButtons.OK, MessageBoxIcon.Warning);
try
{
GameAnalytics.AddErrorEvent(EGAErrorSeverity.Critical, "Wrapper crash: " + ex);
}
catch (Exception ex2)
{
Trace.TraceError("Error adding GA error event: {0}", ex2);
}
}
try
{
GameAnalytics.OnQuit();
}
catch (Exception ex)
{
Trace.TraceWarning("Error ending GA session: {0}", ex);
}
threadSafeWriter.Flush();
try
{
File.WriteAllText(Path.Combine(startupPath, "infolog_full.txt"), logStringBuilder.ToString());
}
catch (Exception ex)
{
Trace.TraceWarning("Error writing full infolog: {0}",ex);
}
Environment.Exit(0);
}
private static void ParseCommandLine(string[] args, out ulong connectLobbyID, out string chobbyTag, out string engineOverride)
{
connectLobbyID = 0;
chobbyTag = null;
engineOverride = null;
if (args.Length > 0)
{
for (var i = 0; i < args.Length - 1; i++)
{
var a = args[i];
if (a == "+connect_lobby")
{
ulong.TryParse(args[i + 1], out connectLobbyID);
args = args.Where((x, j) => (j != i) && (j != i + 1)).ToArray();
if (args.Length < 1) return;
break;
}
}
if ((args[0] == "--help") || (args[0] == "-h") || (args[0] == "/?"))
{
Console.WriteLine(
"Zero-K.exe [rapid_tag] [engine_override] \n\nUse zkmenu:stable or chobby:test\nTo run local dev version use Zero-K.exe dev");
MessageBox.Show(
"Zero-K.exe [rapid_tag] [engine_override] \n\nUse zkmenu:stable or chobby:test\nTo run local dev version use Zero-K.exe dev");
}
chobbyTag = args[0];
if (args.Length > 1) engineOverride = args[1];
}
}
private static void RunWrapper(Chobbyla chobbyla, ulong connectLobbyID, TextWriter logWriter, StringBuilder logSb)
{
if (!chobbyla.IsSteamFolder) // not steam, show gui
{
try
{
Trace.TraceInformation("Trying to start with GUI");
var cf = new ChobbylaForm(chobbyla) { StartPosition = FormStartPosition.CenterScreen };
if (cf.ShowDialog() != DialogResult.OK) return;
}
catch (Exception ex)
{
Trace.TraceWarning("WinForms doesn't work, consider launching with 'mono Zero-K.exe' - wrapper GUI off: {0}", ex.Message);
// in case of gui crash just do simple prepare, no gui
if (!PrepareWithoutGui(chobbyla).ConfigureAwait(false).GetAwaiter().GetResult()) return;
}
}
else if (!PrepareWithoutGui(chobbyla).ConfigureAwait(false).GetAwaiter().GetResult()) return;// otherwise just do simple prepare, no gui
var springRunOk = chobbyla.Run(connectLobbyID, logWriter);
Trace.TraceInformation("Spring exited");
if (!springRunOk) Trace.TraceWarning("Spring crash detected");
logWriter.Flush();
var logStr = logSb.ToString();
CrashReportHelper.CheckAndReportErrors(logStr, springRunOk, chobbyla.BugReportTitle, chobbyla.BugReportDescription, chobbyla.engine);
}
static async Task<bool> PrepareWithoutGui(Chobbyla chobbyla)
{
string lastStatus = "";
var prepTask = chobbyla.Prepare();
while (!prepTask.IsCompleted)
{
await Task.WhenAny(Task.Delay(5000), prepTask);
var prog = chobbyla.Progress;
if (prog.Status != lastStatus)
{
Console.WriteLine(prog.Status);
lastStatus = prog.Status;
}
var cd = prog.Download;
if (cd != null)
{
Console.WriteLine($"Downloading {Math.Round(cd.TotalProgress)}% {cd.Name} {cd.CurrentSpeed / 1024}kB/s ETA: {cd.TimeRemaining}");
}
}
return prepTask.Result;
}
}
}