-
Notifications
You must be signed in to change notification settings - Fork 9
/
Form1.cs
439 lines (327 loc) · 13.8 KB
/
Form1.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using System.Net;
using System.Runtime.InteropServices;
using System.Threading;
//using Gecko;
//using GeckoFX;
namespace PB2Launcher
{
public partial class Form1 : Form
{
private WebBrowser webBrowser1 = null;
//private GeckoWebBrowser webBrowser1 = null;
[DllImport("user32.dll")]
static extern int SetWindowText(IntPtr hWnd, string text);
[DllImport("user32.dll")]
static extern bool SetWindowPos(
IntPtr hWnd,
int hWndInsertAfter, // IntPtr
int X,
int Y,
int cx,
int cy,
uint uFlags
);
[DllImport("USER32.DLL")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);
[DllImport("user32.dll")]
static extern IntPtr GetMenu(IntPtr hWnd);
[DllImport("user32.dll")]
static extern int GetMenuItemCount(IntPtr hMenu);
[DllImport("user32.dll")]
static extern bool DrawMenuBar(IntPtr hWnd);
[DllImport("user32.dll")]
static extern bool RemoveMenu(IntPtr hMenu, uint uPosition, uint uFlags);
public static uint MF_BYPOSITION = 0x400;
public static uint MF_REMOVE = 0x1000;
public static void WindowsReStyle(Process proc)
{
//Process[] Procs = Process.GetProcesses();
//foreach (Process proc in Procs)
{
// if (proc.ProcessName.StartsWith("notepad"))
{
//get menu
IntPtr HMENU = GetMenu(proc.MainWindowHandle);
//get item count
int count = GetMenuItemCount(HMENU);
//loop & remove
for (int i = 0; i < count; i++)
RemoveMenu(HMENU, 0, (MF_BYPOSITION | MF_REMOVE));
//force a redraw
DrawMenuBar(proc.MainWindowHandle);
}
}
}
[DllImport("user32.dll")]
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr LoadImage(IntPtr hInst, string lpsz, IntPtr uType, IntPtr cxDesired, IntPtr cyDesired, IntPtr fuLoad);
private const int SW_MAXIMIZE = 3;
private const int SW_MINIMIZE = 6;
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
public Form1()
{
InitializeComponent();
if (!Directory.Exists("data"))
{
MessageBox.Show("Try to unpack contents of this .zip archieve before executing it (application was not able to find 'data' folder near .exe file, usually it only happens when launcher is being executed in .zip archieve and not extracted).",
"Unable to continue",
MessageBoxButtons.OK,
MessageBoxIcon.None);
//this.Close();
//return;
//System.Windows.Forms.Application.Exit();
System.Environment.Exit(0);
return;
}
/*Gecko.Xpcom.Initialize(@"C:\_HomeData\PC\Projects C#\PB2Launcher\xulrunner");
//Xpcom.Initialize("Firefox");
webBrowser1 = new GeckoWebBrowser { Dock = DockStyle.Fill };
//Form f = new Form();
//f.Controls.Add(geckoWebBrowser);
webBrowser1.Navigate("www.google.com");
// Application.Run(f);
*/
webBrowser1 = new WebBrowser();
webBrowser1.Size = new Size(this.ClientSize.Width, this.ClientSize.Height);
webBrowser1.AllowWebBrowserDrop = false;
//webBrowser1.IsWebBrowserContextMenuEnabled = false;
//webBrowser1.WebBrowserShortcutsEnabled = false;
webBrowser1.ObjectForScripting = this;
this.Controls.Add(webBrowser1);
try
{
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(OnDocumentCompleted);
webBrowser1.Navigate("https://www.plazmaburst2.com/launcher/index.php");
//webBrowser1.Navigate("about:blank");
/*WebClient myWebClient_html_page = new WebClient();
byte[] myDataBuffer_html_page = myWebClient_html_page.DownloadData("https://www.plazmaburst2.com/launcher/index.php");
webBrowser1.Document.Write(String.Empty);
webBrowser1.DocumentText = System.Text.Encoding.UTF8.GetString( myDataBuffer_html_page );*/
}
catch (System.UriFormatException)
{
File.WriteAllTextAsync("_error2.v", "URI format exception - could not navigate");
/*try
{
webBrowser1.Navigate("https://www.plazmaburst2.com/launcher/index.php");
}
catch(System.UriFormatException)
{
File.WriteAllTextAsync("_error3.v", "URI format exception - could not navigate. But was able to proceed with regular string");
}*/
return;
}
}
/*private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
Debug.WriteLine(e.Url.ToString());
//e.Url.ToString();
//e.Cancel = true;
}*/
private bool IsOnline = true;
private void OnDocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
try
{
WebBrowser webBrowser1 = (WebBrowser)sender;
if (webBrowser1.ReadyState.Equals(WebBrowserReadyState.Complete))
{
//webBrowser1.ShowPrintDialog();
// Debug.WriteLine("Complete. IsOffline: " + );
if (webBrowser1.DocumentText.IndexOf("pb2_logo.png") == -1)
{
// Take 2, for Windows 7 bug handling
WebClient myWebClient_html_page = new WebClient();
byte[] myDataBuffer_html_page = myWebClient_html_page.DownloadData("https://www.plazmaburst2.com/launcher/index.php");
webBrowser1.Document.Write(String.Empty);
string new_html = System.Text.Encoding.UTF8.GetString(myDataBuffer_html_page);
webBrowser1.DocumentText = new_html;
if (new_html.IndexOf("pb2_logo.png") == -1)
{
File.WriteAllTextAsync("_error1.v", "Contents of a page is following (has no 'pb2_logo.png' on page '" + webBrowser1.Url + "'): " + webBrowser1.DocumentText);
IsOnline = false;
RunGame("");
}
}
}
// else
// Debug.WriteLine("State: " + webBrowser1.ReadyState.ToString() );
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
File.WriteAllTextAsync("_error4.v", "Exception during document complete (can be result of offline mode): " + ex.ToString() );
IsOnline = false;
RunGame("");
}
}
public long GetLastUpdateServerTime()
{
if (File.Exists("data\\last_update.v"))
{
String text = File.ReadAllText("data\\last_update.v");
return Int64.Parse(text);
}
return 0;
}
public bool UpdateGame()
{
try
{
WebClient myWebClient_time = new WebClient();
byte[] myDataBuffer_time = myWebClient_time.DownloadData("https://www.plazmaburst2.com/launcher/time.php");
WebClient myWebClient = new WebClient();
byte[] myDataBuffer = myWebClient.DownloadData("https://www.plazmaburst2.com/pb2/pb2_re34.swf");
File.WriteAllBytes("data\\pb2_re34_alt.swf", myDataBuffer);
File.WriteAllBytes("data\\last_update.v", myDataBuffer_time);
return true;
}
catch
{
return false;
}
}
public void RunGame(String s)
{
//MessageBox.Show(message, "executed RunGame");
string myparams = "?from_standalone=1";
if (File.Exists("data\\Plazma Burst 2.auth"))
{
String[] text = File.ReadAllText("data\\Plazma Burst 2.auth").Split('\n', 2);
myparams = "?l=" + text[0] + "&p=" + text[1] + "&from_standalone=1";
}
ProcessStartInfo startInfo = new ProcessStartInfo("data\\flashplayer11_7r700_224_win_sa.exe");
startInfo.WindowStyle = ProcessWindowStyle.Normal;
if ( s == "" )
startInfo.Arguments = "\"data\\pb2_re34_alt.swf" + myparams + "\"";
else
startInfo.Arguments = "\"data\\" + s + "\"";
Process p = Process.Start(startInfo);
p.WaitForInputIdle();
int xx = this.Size.Width - this.ClientSize.Width;
int yy = this.Size.Height - this.ClientSize.Height;
if (s == "")
SetWindowText(p.MainWindowHandle, "Plazma Burst 2");
else
if ( s == "plazma_burst_fttp.swf" )
SetWindowText(p.MainWindowHandle, "Plazma Burst: Forward to the Past");
WindowsReStyle(p);
IntPtr m_pIcon;
// Load the empty icon (from a file in this example)
//string strIconFilePath = @"C:\_HomeData\PC\projects fl\PB2\publish exe 2\Plazma Burst 2\data\favicon.ico"; // Replace with a valid path on your system!
string strIconFilePath = Directory.GetCurrentDirectory() + "\\data\\favicon.ico";
const int IMAGE_ICON = 1;
const int LR_LOADFROMFILE = 0x10;
// edit: IntPtr pIcon = LoadImage(IntPtr.Zero, strIconFilePath, IMAGE_ICON, 0, 0, LR_LOADFROMFILE);
m_pIcon = LoadImage(IntPtr.Zero, strIconFilePath, (IntPtr)IMAGE_ICON, (IntPtr)0, (IntPtr)0, (IntPtr)LR_LOADFROMFILE);
SendMessage(p.MainWindowHandle, 0x80, IntPtr.Zero, m_pIcon);
SetWindowPos(
p.MainWindowHandle,
0,
this.Left + this.Size.Width / 2 - 800 / 2 - xx / 2,
this.Top,
800 + xx,
400 + yy,
0x0000
//0x0001 // SWP_NOSIZE
);
if (this.WindowState == FormWindowState.Maximized )
{
ShowWindow(p.MainWindowHandle, SW_MAXIMIZE);
}
// Task.Delay( 1000 );
this.Close();
/*
this.WindowState = FormWindowState.Minimized;
p.WaitForExit();
this.WindowState = FormWindowState.Normal;
//this.BringToFront();
this.Activate();
//if (!IsOnline)
{
this.Close();
}*/
}
public String GetLogin()
{
if (File.Exists("data\\Plazma Burst 2.auth"))
{
string text = File.ReadAllText("data\\Plazma Burst 2.auth");
text = text.Split('\n', 2)[0];
return text;
}
return "Guest";
}
public void SaveLoginPassword(String l, String p)
{
File.WriteAllText("data\\Plazma Burst 2.auth", l + '\n' + p);
}
public void DontAskForLogin()
{
File.WriteAllText("data\\skip_auth.v", "1");
}
public bool GetAskForLogin()
{
if (File.Exists("data\\skip_auth.v"))
return false;
return true;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Form1_Resize(object sender, EventArgs e)
{
if (webBrowser1 != null)
webBrowser1.Size = new Size(this.ClientSize.Width, this.ClientSize.Height);
}
public void PopUpLink(String url)
{
//Process.Start(url);
//System.Diagnostics.Process.Start(url);
if (url.StartsWith("/"))
{
url = "https://www.plazmaburst2.com" + url;
}
try
{
Process.Start(url);
}
catch
{
// hack because of this: https://github.com/dotnet/corefx/issues/10361
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
url = url.Replace("&", "^&");
Process.Start(new ProcessStartInfo("cmd", $"/c start {url}") { CreateNoWindow = true });
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
Process.Start("xdg-open", url);
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
Process.Start("open", url);
}
else
{
throw;
}
}
}
}
}