Client/GamerServices/Systems/Forms/FormDevUpdate.cs

868 lines
25 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.IO.Compression;
using ICSharpCode.SharpZipLib.Tar;
using System.Net;
using System.Collections.Specialized;
namespace GamerServices {
public partial class FormDevUpdate : Form {
class Prop{
public String Path;
public String Action;
}
private Game G;
private String CurPath = "";
private List<Prop> Props = new List<Prop>();
public FormDevUpdate(Game g) {
G = g;
CurPath = "";
CheckForIllegalCrossThreadCalls = false;
this.Icon = G.Main.Icon;
InitializeComponent();
}
private void FormDevUpdate_Load(object sender, EventArgs e) {
listBox1.Items.Clear();
listBox2.Items.Clear();
Browse();
}
private void Browse() {
try {
string startdir = Directory.GetCurrentDirectory() + "/Games/" + G.GameID + "/";
int startpos = startdir.Length;
listBox1.Items.Clear();
int slashes = 0;
int lastshalsh = CurPath.IndexOf("/");
while(lastshalsh > -1){
slashes++;
lastshalsh = CurPath.IndexOf("/", lastshalsh + 1);
}
if (slashes > 0) {
listBox1.Items.Add("...");
}
foreach (string dir in Directory.GetDirectories(startdir + CurPath)) {
listBox1.Items.Add(dir.Substring(startpos + CurPath.Length));
}
foreach (string file in Directory.GetFiles(startdir + CurPath)) {
if (!(CurPath == "" && file.Substring(startpos + CurPath.Length) == "version.txt")) {
listBox1.Items.Add(file.Substring(startpos + CurPath.Length));
}
}
} catch(Exception e) {
}
}
private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e) {
if ((string)listBox1.SelectedItem == "...") {
string[] splited = CurPath.Split('/');
CurPath = "";
if (splited.Length > 1) {
for (int i = 0; i < splited.Length - 2; i++ ) {
CurPath += splited[i] + "/";
}
}
} else {
if (Directory.Exists(Directory.GetCurrentDirectory() + "/Games/" + G.GameID + "/" + CurPath + (string)listBox1.SelectedItem))
CurPath += (string)listBox1.SelectedItem + "/";
else
additem(CurPath + (string)listBox1.SelectedItem);
}
Browse();
}
private void button3_Click(object sender, EventArgs e) {
groupBox1.Enabled = false;
groupBox2.Enabled = false;
button3.Enabled = false;
timer1.Enabled = true;
new System.Threading.Thread(new System.Threading.ThreadStart(TarMaker)).Start();
}
private void TarMaker() {
progressBar1.Value = 0;
string updatedata = "";
string root = Directory.GetCurrentDirectory() + "/Games/" + G.GameID + "/";
string newver = "";
labelInfo.Text = "Recieving latest version info...";
try {
System.Net.WebClient client = new System.Net.WebClient();
client.Proxy = null;
string[] verdata = new StreamReader(client.OpenRead("http://" + "gs.mrag.nl/download.php?gid=" + G.GameID + "&file=updates.txt")).ReadToEnd().Replace("\r", "").Split('\n');
foreach (string line in verdata){
if (line.Length > 0) {
string[] args = null;
if (line.Contains("=")) args = line.Split('=');
if (line.TrimStart('\t', ' ').StartsWith("#")){
}else if (args[0] == "VER"){
short[] splitver = new short[4];
string[] tmp = args[1].Split('.');
for (int i = 0; i < tmp.Length; i++)
splitver[i] = byte.Parse(tmp[i]);
splitver[3]++;
for (int i = 2; i > -1; i--) {
if (splitver[i + 1] > 255) {
splitver[i]++;
splitver[i + 1] = 0;
}
}
newver = splitver[0] + "." + splitver[1] + "." + splitver[2] + "." + splitver[3];
break;
}
}
}
} catch {
newver = "0.0.0.0";
}
labelInfo.Text = "Initializing version " + newver;
string newverfile = root + "update-" + newver.Replace('.', '_');
updatedata = "VER=" + newver;
labelInfo.Text = "Removing old files...";
while (true) {
try {
if (File.Exists(newverfile + ".tar"))
File.Delete(newverfile + ".tar");
if (File.Exists(newverfile + ".txt"))
File.Delete(newverfile + ".txt");
break;
} catch (Exception e){
if (MessageBox.Show(e.Message, "Error while removing old version files!", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error) != DialogResult.Retry) {
break;
}
}
}
labelInfo.Text = "Counting total file size...";
int totalsize = 0;
for (int i = 0; i < Props.Count; i++) {
if (Props[i].Action == "ADD") {
while (true) {
try {
totalsize += (int)new FileInfo(root + (string)listBox2.Items[i]).Length;
updatedata += "\r\n" + Props[i].Action + "=" + Props[i].Path;
break;
} catch(Exception e) {
if (MessageBox.Show(e.Message, "Error while reading filesize!", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error) != DialogResult.Retry) {
break;
}
}
}
}else
updatedata += "\r\n" + Props[i].Action + "=" + Props[i].Path;
}
progressBar1.Minimum = 0;
progressBar1.Maximum = totalsize;
using (TarOutputStream tarOutputStream = new TarOutputStream(File.Create(newverfile + ".tar"))) {
while (Props.Count > 0) {
labelInfo.Text = "Writing tar file... " + Props[0].Path;
listBox2.ClearSelected();
listBox2.SetSelected(0, true);
listBox2.SetSelected(0, false);
if (Props[0].Action == "ADD") {
while (true) {
try {
FileInfo info = new FileInfo(root + Props[0].Path);
using (Stream inputStream = File.OpenRead(root + Props[0].Path)) {
long fileSize = inputStream.Length;
TarEntry entry = TarEntry.CreateTarEntry(Props[0].Path.Replace("\\", "/"));
entry.Size = fileSize;
tarOutputStream.PutNextEntry(entry);
byte[ ] localBuffer = new byte[32 * 1024];
while (true) {
int numRead = inputStream.Read(localBuffer, 0, localBuffer.Length);
if (numRead <= 0) {
break;
}
tarOutputStream.Write(localBuffer, 0, numRead);
}
}
tarOutputStream.CloseEntry();
progressBar1.Increment((int)info.Length);
break;
} catch(Exception e) {
if (MessageBox.Show(e.Message, "Error while parsing file!", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error) != DialogResult.Retry) {
break;
}
}
}
}
listBox2.Items.RemoveAt(0);
Props.RemoveAt(0);
}
labelInfo.Text = "Saving tar file... ";
}
labelInfo.Text = "Writing patch config...";
File.WriteAllText(newverfile + ".txt", updatedata);
labelInfo.Text = "Initializing upload...";
button2.Tag = Math.Round((double)new FileInfo(newverfile + ".tar").Length / 1024 / 1024, 3).ToString();
button3.Tag = newver;
new System.Threading.Thread(new System.Threading.ThreadStart(Upload)).Start();
}
private void button2_Click(object sender, EventArgs e) {
for (int i = 0; i < listBox2.SelectedItems.Count; i++){
for (int i2 = 0; i2 < Props.Count; i2++) {
if (Props[i2].Path == (string)listBox2.SelectedItems[i]){
Props.RemoveAt(i2);
break;
}
}
listBox2.Items.Remove((string)listBox2.SelectedItems[i]);
i--;
}
if (listBox2.Items.Count == 0)
button3.Enabled = false;
}
private void button1_Click(object sender, EventArgs e) {
foreach (object obj in listBox1.SelectedItems) {
additem(CurPath + (string)obj);
if (Directory.Exists(Directory.GetCurrentDirectory() + "/Games/" + G.GameID + "/" + CurPath + (string)obj)) {
adddir(CurPath + (string)obj);
}
}
}
private void adddir(string addpath) {
string startdir = Directory.GetCurrentDirectory() + "/Games/" + G.GameID + "/" + addpath;
string reppath = Directory.GetCurrentDirectory() + "/Games/" + G.GameID + "/";
int startpos = reppath.Length;
foreach (string dir in Directory.GetDirectories(startdir)) {
additem(dir.Substring(startpos));
adddir(dir.Substring(startpos));
}
foreach (string file in Directory.GetFiles(startdir)) {
additem(file.Substring(startpos));
}
}
private void additem(string item) {
if (!listBox2.Items.Contains(item)) {
string itempath = Directory.GetCurrentDirectory() + "/Games/" + G.GameID + "/" + item;
Prop p = new Prop();
p.Path = item;
p.Action = Directory.Exists(itempath) ? "MDR" : File.Exists(itempath) ? "ADD" : "MNF";
Props.Add(p);
listBox2.Items.Add(item);
listBox2.ClearSelected();
listBox2.SetSelected(listBox2.Items.Count - 1, true);
listBox2.SetSelected(listBox2.Items.Count - 1, false);
button3.Enabled = true;
}
}
private void listBox2_SelectedIndexChanged(object sender, EventArgs e) {
if (listBox2.SelectedItems.Count > 0){
string defcmd = "";
foreach (Prop p in Props) {
if ((string)listBox2.SelectedItems[0] == p.Path) {
defcmd = p.Action;
break;
}
}
if (listBox2.SelectedItems.Count > 1) {
foreach (string itm in listBox2.SelectedItems) {
foreach (Prop p in Props) {
if (itm == p.Path) {
if (p.Action != defcmd) {
defcmd = "*";
break;
}
}
}
if (defcmd == "*")
break;
}
comboCommand.Text = defcmd;
textPath.Text = "*";
} else {
comboCommand.Text = Props[listBox2.Items.IndexOf((string)listBox2.SelectedItems[0])].Action;
textPath.Text = (string)listBox2.SelectedItems[0];
}
switch (comboCommand.Text) {
case "ADD":
labelHelp.Text = "Adds a new file to the folder, if it already exists it wil be overwritten.";
break;
case "DEL":
labelHelp.Text = "Deletes a file/folder.";
break;
case "MNF":
labelHelp.Text = "Creates a new empty file.";
break;
case "MDR":
labelHelp.Text = "Creates an folder.";
break;
}
groupBox2.Enabled = true;
}else
groupBox2.Enabled = false;
}
private void button6_Click(object sender, EventArgs e) {
additem("Empty");
}
private void button4_Click(object sender, EventArgs e) {
Props[listBox2.SelectedIndex].Action = comboCommand.Text;
Props[listBox2.SelectedIndex].Path = textPath.Text;
listBox2.Items[listBox2.SelectedIndex] = textPath.Text;
}
private void comboCommand_SelectionChangeCommitted(object sender, EventArgs e) {
foreach (string itm in listBox2.SelectedItems) {
foreach (Prop p in Props) {
if (itm == p.Path) {
p.Action = comboCommand.Text;
break;
}
}
}
}
private void comboCommand_SelectedIndexChanged(object sender, EventArgs e) {
}
private void Upload(){
WebClient wClient = new WebClient();
wClient.Proxy = null;
labelInfo.Text = "Uploading new version file...";
string[] txt = null;
int failsafe = 0;
while (failsafe < 3) {
try {
txt = MyUploader(Directory.GetCurrentDirectory() + "/Games/" + G.GameID + "/" + "update-" + (button3.Tag as string).Replace('.', '_') + ".txt", "http://" + "gs.mrag.nl/api/" + G.Main.Sessionkey + "/txt/" + G.GameID).Split('\n');
break;
} catch {
failsafe++;
}
}
if (failsafe > 2) {
G.Main.Error(txt[1]);
labelInfo.Text = "Failed to upload new patch, the WebRequest was rejected by the server.";
groupBox1.Enabled = true;
groupBox2.Enabled = true;
timer1.Enabled = false;
timeranimation = 0;
radioButton1.Checked = false;
radioButton2.Checked = false;
radioButton3.Checked = false;
radioButton4.Checked = false;
radioButton5.Checked = false;
radioButton6.Checked = false;
radioButton7.Checked = false;
radioButton8.Checked = false;
radioButton9.Checked = false;
radioButton10.Checked = false;
radioButton11.Checked = false;
radioButton12.Checked = false;
radioButton13.Checked = false;
radioButton14.Checked = false;
radioButton15.Checked = false;
radioButton16.Checked = false;
radioButton17.Checked = false;
radioButton18.Checked = false;
radioButton19.Checked = false;
radioButton20.Checked = false;
radioButton21.Checked = false;
radioButton22.Checked = false;
radioButton23.Checked = false;
radioButton24.Checked = false;
radioButton25.Checked = false;
radioButton26.Checked = false;
return;
}
if (txt[0] == "no") {
G.Main.Error(txt[1]);
labelInfo.Text = "Failed!";
groupBox1.Enabled = true;
groupBox2.Enabled = true;
timer1.Enabled = false;
timeranimation = 0;
radioButton1.Checked = false;
radioButton2.Checked = false;
radioButton3.Checked = false;
radioButton4.Checked = false;
radioButton5.Checked = false;
radioButton6.Checked = false;
radioButton7.Checked = false;
radioButton8.Checked = false;
radioButton9.Checked = false;
radioButton10.Checked = false;
radioButton11.Checked = false;
radioButton12.Checked = false;
radioButton13.Checked = false;
radioButton14.Checked = false;
radioButton15.Checked = false;
radioButton16.Checked = false;
radioButton17.Checked = false;
radioButton18.Checked = false;
radioButton19.Checked = false;
radioButton20.Checked = false;
radioButton21.Checked = false;
radioButton22.Checked = false;
radioButton23.Checked = false;
radioButton24.Checked = false;
radioButton25.Checked = false;
radioButton26.Checked = false;
return;
}
labelInfo.Text = "Uploading new patch...";
string[] tar = null;
failsafe = 0;
while (failsafe < 3) {
try {
tar = MyUploader(Directory.GetCurrentDirectory() + "/Games/" + G.GameID + "/" + "update-" + (button3.Tag as string).Replace('.', '_') + ".tar", "http://" + "gs.mrag.nl/api/" + G.Main.Sessionkey + "/tar/" + G.GameID).Split('\n');
break;
} catch {
failsafe++;
}
}
if (failsafe > 2) {
G.Main.Error(txt[1]);
labelInfo.Text = "Failed to upload new patch, the WebRequest was rejected by the server.";
groupBox1.Enabled = true;
groupBox2.Enabled = true;
timer1.Enabled = false;
timeranimation = 0;
radioButton1.Checked = false;
radioButton2.Checked = false;
radioButton3.Checked = false;
radioButton4.Checked = false;
radioButton5.Checked = false;
radioButton6.Checked = false;
radioButton7.Checked = false;
radioButton8.Checked = false;
radioButton9.Checked = false;
radioButton10.Checked = false;
radioButton11.Checked = false;
radioButton12.Checked = false;
radioButton13.Checked = false;
radioButton14.Checked = false;
radioButton15.Checked = false;
radioButton16.Checked = false;
radioButton17.Checked = false;
radioButton18.Checked = false;
radioButton19.Checked = false;
radioButton20.Checked = false;
radioButton21.Checked = false;
radioButton22.Checked = false;
radioButton23.Checked = false;
radioButton24.Checked = false;
radioButton25.Checked = false;
radioButton26.Checked = false;
return;
}
if (tar[0] == "no") {
G.Main.Error(txt[1]);
labelInfo.Text = "Failed!";
groupBox1.Enabled = true;
groupBox2.Enabled = true;
timer1.Enabled = false;
timeranimation = 0;
radioButton1.Checked = false;
radioButton2.Checked = false;
radioButton3.Checked = false;
radioButton4.Checked = false;
radioButton5.Checked = false;
radioButton6.Checked = false;
radioButton7.Checked = false;
radioButton8.Checked = false;
radioButton9.Checked = false;
radioButton10.Checked = false;
radioButton11.Checked = false;
radioButton12.Checked = false;
radioButton13.Checked = false;
radioButton14.Checked = false;
radioButton15.Checked = false;
radioButton16.Checked = false;
radioButton17.Checked = false;
radioButton18.Checked = false;
radioButton19.Checked = false;
radioButton20.Checked = false;
radioButton21.Checked = false;
radioButton22.Checked = false;
radioButton23.Checked = false;
radioButton24.Checked = false;
radioButton25.Checked = false;
radioButton26.Checked = false;
return;
}
labelInfo.Text = "Removing temp update files...";
while (true) {
try {
File.Delete(Directory.GetCurrentDirectory() + "/Games/" + G.GameID + "/" + "update-" + (button3.Tag as string).Replace('.', '_') + ".tar");
break;
} catch (Exception e) {
if (MessageBox.Show(e.Message, "Error while removing temp files!", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error) != DialogResult.Retry) {
break;
}
}
}
while (true) {
try {
File.Delete(Directory.GetCurrentDirectory() + "/Games/" + G.GameID + "/" + "update-" + (button3.Tag as string).Replace('.', '_') + ".txt");
break;
} catch (Exception e) {
if (MessageBox.Show(e.Message, "Error while removing temp files!", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error) != DialogResult.Retry) {
break;
}
}
}
labelInfo.Text = "Updateing local version file...";
while (true) {
try {
File.WriteAllText(Directory.GetCurrentDirectory() + "/Games/" + G.GameID + "/version.txt", (string)button3.Tag);
break;
} catch (Exception e) {
if (MessageBox.Show(e.Message, "Error while writing local version file!", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error) != DialogResult.Retry) {
break;
}
}
}
labelInfo.Text = "Done!";
groupBox1.Enabled = true;
groupBox2.Enabled = true;
timer1.Enabled = false;
timeranimation = 0;
radioButton1.Checked = false;
radioButton2.Checked = false;
radioButton3.Checked = false;
radioButton4.Checked = false;
radioButton5.Checked = false;
radioButton6.Checked = false;
radioButton7.Checked = false;
radioButton8.Checked = false;
radioButton9.Checked = false;
radioButton10.Checked = false;
radioButton11.Checked = false;
radioButton12.Checked = false;
radioButton13.Checked = false;
radioButton14.Checked = false;
radioButton15.Checked = false;
radioButton16.Checked = false;
radioButton17.Checked = false;
radioButton18.Checked = false;
radioButton19.Checked = false;
radioButton20.Checked = false;
radioButton21.Checked = false;
radioButton22.Checked = false;
radioButton23.Checked = false;
radioButton24.Checked = false;
radioButton25.Checked = false;
radioButton26.Checked = false;
MessageBox.Show("Uploaded patch for version " + button3.Tag + "!\nFile size: " + button2.Tag + " MB", "Done!", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
public static string MyUploader(string strFileToUpload, string strUrl)
{
string strFileFormName = "file";
Uri oUri = new Uri(strUrl);
string strBoundary = "----------" + DateTime.Now.Ticks.ToString("x");
// The trailing boundary string
byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + strBoundary + "\r\n");
// The post message header
StringBuilder sb = new StringBuilder();
sb.Append("--");
sb.Append(strBoundary);
sb.Append("\r\n");
sb.Append("Content-Disposition: form-data; name=\"");
sb.Append(strFileFormName);
sb.Append("\"; filename=\"");
sb.Append(Path.GetFileName(strFileToUpload));
sb.Append("\"");
sb.Append("\r\n");
sb.Append("Content-Type: ");
sb.Append("application/octet-stream");
sb.Append("\r\n");
sb.Append("\r\n");
string strPostHeader = sb.ToString();
byte[] postHeaderBytes = Encoding.UTF8.GetBytes(strPostHeader);
// The WebRequest
HttpWebRequest oWebrequest = (HttpWebRequest)WebRequest.Create(oUri);
oWebrequest.ContentType = "multipart/form-data; boundary=" + strBoundary;
oWebrequest.Method = "POST";
// This is important, otherwise the whole file will be read to memory anyway...
oWebrequest.AllowWriteStreamBuffering = false;
// Get a FileStream and set the final properties of the WebRequest
FileStream oFileStream = new FileStream(strFileToUpload, FileMode.Open, FileAccess.Read);
long length = postHeaderBytes.Length + oFileStream.Length + boundaryBytes.Length;
oWebrequest.ContentLength = length;
Stream oRequestStream = oWebrequest.GetRequestStream();
// Write the post header
oRequestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
// Stream the file contents in small pieces (4096 bytes, max).
byte[] buffer = new Byte[checked((uint)Math.Min(4096, (int)oFileStream.Length))];
int bytesRead = 0;
while ((bytesRead = oFileStream.Read(buffer, 0, buffer.Length)) != 0)
oRequestStream.Write(buffer, 0, bytesRead);
oFileStream.Close();
// Add the trailing boundary
oRequestStream.Write(boundaryBytes, 0, boundaryBytes.Length);
WebResponse oWResponse = oWebrequest.GetResponse();
Stream s = oWResponse.GetResponseStream();
StreamReader sr = new StreamReader(s);
String sReturnString = sr.ReadToEnd();
// Clean up
oFileStream.Close();
oRequestStream.Close();
s.Close();
sr.Close();
return sReturnString;
}
private byte timeranimation = 0;
private void timer1_Tick(object sender, EventArgs e) {
switch (timeranimation) {
case 0:
radioButton1.Checked = false;
radioButton2.Checked = true;
break;
case 1:
radioButton2.Checked = false;
radioButton3.Checked = true;
break;
case 2:
radioButton3.Checked = false;
radioButton4.Checked = true;
break;
case 3:
radioButton4.Checked = false;
radioButton5.Checked = true;
break;
case 4:
radioButton5.Checked = false;
radioButton6.Checked = true;
break;
case 5:
radioButton6.Checked = false;
radioButton7.Checked = true;
break;
case 6:
radioButton7.Checked = false;
radioButton8.Checked = true;
break;
case 7:
radioButton8.Checked = false;
radioButton9.Checked = true;
break;
case 8:
radioButton9.Checked = false;
radioButton10.Checked = true;
break;
case 9:
radioButton11.Checked = false;
radioButton12.Checked = true;
break;
case 10:
radioButton12.Checked = false;
radioButton13.Checked = true;
break;
case 11:
radioButton13.Checked = false;
radioButton14.Checked = true;
break;
case 12:
radioButton14.Checked = false;
radioButton15.Checked = true;
break;
case 13:
radioButton15.Checked = false;
radioButton16.Checked = true;
break;
case 14:
radioButton16.Checked = false;
radioButton17.Checked = true;
break;
case 15:
radioButton17.Checked = false;
radioButton18.Checked = true;
break;
case 16:
radioButton18.Checked = false;
radioButton19.Checked = true;
break;
case 17:
radioButton19.Checked = false;
radioButton20.Checked = true;
break;
case 18:
radioButton20.Checked = false;
radioButton21.Checked = true;
break;
case 19:
radioButton21.Checked = false;
radioButton22.Checked = true;
break;
case 20:
radioButton22.Checked = false;
radioButton23.Checked = true;
break;
case 21:
radioButton23.Checked = false;
radioButton24.Checked = true;
break;
case 22:
radioButton24.Checked = false;
radioButton25.Checked = true;
break;
case 23:
radioButton25.Checked = false;
radioButton26.Checked = true;
break;
case 24:
radioButton26.Checked = false;
radioButton1.Checked = true;
break;
}
timeranimation++;
if (timeranimation > 24)
timeranimation = 0;
}
private void comboCommand_SelectedValueChanged(object sender, EventArgs e) {
switch (comboCommand.Text) {
case "ADD":
labelHelp.Text = "Adds a new file to the folder, if it already exists it wil be overwritten.";
break;
case "DEL":
labelHelp.Text = "Deletes an file/folder.";
break;
case "MNF":
labelHelp.Text = "Creates an new empty file.";
break;
case "MDR":
labelHelp.Text = "Creates an folder.";
break;
}
}
}
}