Updater/GamerService Updater/Form1.cs

278 lines
9.4 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.Windows.Forms;
using System.Net;
using System.IO;
using System.IO.Compression;
using System.Net.Sockets;
using System.Threading;
namespace GamerService_Updater
{
public partial class Form1 : Form
{
#region structs
public struct afile{
public int action;
public string path;
public string rev;
}
#endregion
#region fncs
public void MakeFolder(string path){
List<string> folders = String_Explode(path, "/");
string curdir = System.IO.Directory.GetCurrentDirectory();
while(folders.Count > 0){
curdir += "/" + folders[0];
if (!System.IO.Directory.Exists(curdir))
System.IO.Directory.CreateDirectory(path);
folders.RemoveAt(0);
}
}
public List<string> String_Explode(string text, string spliter){
List<string> returnz = new List<string>();
for (int i = 0; i < text.Length - spliter.Length; i++){
while (text.Substring(i, spliter.Length) == spliter){
returnz.Add(text.Substring(0, i));
text = text.Substring(i + spliter.Length, text.Length - i - spliter.Length);
i = 0;
if (text.Length == 0)return returnz;
}
}
if (text.Length > 0){
returnz.Add(text.Substring(0, text.Length));
}
return returnz;
}
public string String_Implode(List<string> Data, string spliter){
string returnz = "";
foreach (string a in Data)
returnz += a + spliter;
if (returnz == "") return "";
return returnz.Substring(0, returnz.Length - spliter.Length);
}
#endregion
public class DownloadInfo {
public string Path;
public string URL;
public string Version;
public int GameID;
public int TotalActions;
public byte Action;
}
public List<DownloadInfo> FilesToDownload;
public List<afile> files = new List<afile>();
public string Curversion = "0";
public string NewVersion = "";
public string Ip = "";
public int Port = 0;
public Form1()
{
InitializeComponent();
CheckForIllegalCrossThreadCalls = false;
MrAG.Config.Read("config.gs");
Curversion = MrAG.Config.Value_Get("Main", "Version", "0.0.0.0");
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
System.Diagnostics.Process.GetCurrentProcess().Close();
}
System.Net.WebClient client;
string currenthandelingrev;
private void DoUpdate(){
this.timer1.Enabled = true;
client = new WebClient();
client.Proxy = null;
string[] FileData = null;
int actions = 0;
try {
FileData = new StreamReader(client.OpenRead("http://" + "gs.mrag.nl/download.php?gid=-1&file=version.txt")).ReadToEnd().Replace("\r", "").Split('\n');
} catch(Exception e) {
return;
}
FilesToDownload = new List<DownloadInfo>();
lock (FilesToDownload) {
currenthandelingrev = "";
List<string> filesdone = new List<string>();
foreach (string line in FileData){
if (line.Length > 0) {
string[] args = null;
if (line.Contains("=")) args = line.Split('=');
if (line.TrimStart('\t', ' ').StartsWith("#")){
}else if (args[0] == "VER"){
if (currenthandelingrev.Length == 0){
currenthandelingrev = args[1];
}
if (args[1] == Curversion) {
break;
}
}else{
if (!filesdone.Contains(args[1])) {
filesdone.Add(args[1]);
DownloadInfo info = new DownloadInfo();
info.Action = 0;
info.URL = "http://" + "gs.mrag.nl/download.php?gid=-1&file=" + args[1];
info.Path = args[1];
switch (args[0]) {
case "ADD":
info.Action = 0;
break;
case "MNF":
info.Action = 1;
break;
case "DEL":
info.Action = 2;
break;
case "MDR":
info.Action = 3;
break;
}
FilesToDownload.Add(info);
actions++;
}
}
}
}
for (int i = FilesToDownload.Count - 1; i > 0; i--) {
FilesToDownload[i].TotalActions = actions;
}
if (FilesToDownload.Count != 0)
new Thread(new ThreadStart(DoDownloads)).Start();
else {
MessageBox.Show("No updates :D", "Newest version");
Application.Exit();
}
}
}
private void DoDownloads() {
while (FilesToDownload.Count > 0) {
DownloadInfo info = FilesToDownload[0];
switch (info.Action) {
case 0:
int failsafe = 0;
if (File.Exists(info.Path)){
while (failsafe < 50) {
failsafe++;
try{
File.Delete(info.Path);
break;
}catch{
}
}
}
if (failsafe > 49) {
MessageBox.Show("Fatal error while trying to remove old file: " + info.Path + "\nPlease do this remotely!", "FATAL ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
Application.Exit();
return;
}
client.DownloadFile(new Uri(info.URL.Replace("\\", "/")), info.Path);
break;
case 1:
if (File.Exists(info.Path))
File.Delete(info.Path);
File.Create(info.Path);
break;
case 2:
if (Directory.Exists(info.Path))
Directory.Delete(info.Path, true);
if (File.Exists(info.Path))
File.Delete(info.Path);
break;
case 3:
if (Directory.Exists(info.Path))
Directory.Delete(info.Path, true);
Directory.CreateDirectory(info.Path);
break;
}
FilesToDownload.RemoveAt(0);
}
MrAG.Config.Value_Set("Main", "Version", currenthandelingrev);
MrAG.Config.Write("config.gs");
Application.Exit();
System.Diagnostics.Process.Start("GamerServices.exe");
}
List<List<CheckBox>> cblist = new List<List<CheckBox>>();
private void Form1_Load(object sender, EventArgs e)
{
int currow = 0;
foreach(CheckBox cb in this.Controls){
if (cb.Checked) {
if (cb.Location.Y != currow) {
cblist.Add(new List<CheckBox>());
}
cblist[cblist.Count - 1].Add(cb);
cb.Checked = false;
}
}
DoUpdate();
}
private byte timeranimation = 0;
private bool kak = true;
private void timer1_Tick(object sender, EventArgs e) {
foreach (CheckBox cb in cblist[timeranimation]) {
cb.Checked = kak;
}
timeranimation++;
if (timeranimation > cblist.Count - 1) {
timeranimation = 0;
kak = !kak;
}
}
}
}