2017-04-20 17:09:37 +02:00
|
|
|
|
|
|
|
|
|
using System;
|
2017-04-23 23:07:41 +02:00
|
|
|
|
using System.IO;
|
2017-04-24 23:55:40 +02:00
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Xml;
|
2017-04-20 17:09:37 +02:00
|
|
|
|
|
|
|
|
|
namespace BlubbFish.Utils {
|
2017-04-23 23:07:41 +02:00
|
|
|
|
public class Updater : OwnObject {
|
2017-04-20 17:09:37 +02:00
|
|
|
|
private static Updater instances;
|
|
|
|
|
private String url;
|
2017-04-24 23:55:40 +02:00
|
|
|
|
private VersionInfo[] versions;
|
2017-04-25 22:44:17 +02:00
|
|
|
|
private Thread t;
|
2017-04-21 23:55:26 +02:00
|
|
|
|
|
2017-04-24 23:55:40 +02:00
|
|
|
|
public struct VersionInfo {
|
|
|
|
|
public VersionInfo(Type type) {
|
|
|
|
|
this.Name = type.Assembly.GetName().Name;
|
|
|
|
|
this.Version = type.Assembly.GetName().Version.ToString();
|
|
|
|
|
this.Filename = type.Assembly.ManifestModule.Name;
|
|
|
|
|
this.GUID = ((GuidAttribute)type.Assembly.GetCustomAttribute(typeof(GuidAttribute))).Value;
|
2017-04-25 22:44:17 +02:00
|
|
|
|
this.HasUpdate = false;
|
2017-04-24 23:55:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String Name { get; private set; }
|
|
|
|
|
public String Version { get; private set; }
|
|
|
|
|
public String Filename { get; private set; }
|
|
|
|
|
public String GUID { get; private set; }
|
2017-04-25 22:44:17 +02:00
|
|
|
|
public Boolean HasUpdate { get; set; }
|
2017-04-24 23:55:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-04-21 23:55:26 +02:00
|
|
|
|
public delegate void UpdateStatus(Object sender, UpdaterEventArgs e);
|
2017-04-25 22:44:17 +02:00
|
|
|
|
public delegate void UpdateFail(Object sender, UpdaterFailEventArgs e);
|
2017-04-20 17:09:37 +02:00
|
|
|
|
|
|
|
|
|
public event UpdateStatus UpdateResult;
|
2017-04-25 22:44:17 +02:00
|
|
|
|
public event UpdateFail ErrorRaised;
|
2017-04-20 17:09:37 +02:00
|
|
|
|
|
|
|
|
|
private Updater() { }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Get Instance of Updater
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static Updater Instance {
|
|
|
|
|
get {
|
|
|
|
|
if(instances == null) {
|
|
|
|
|
instances = new Updater();
|
|
|
|
|
}
|
|
|
|
|
return instances;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Waits for the Result of the Updater thread.
|
|
|
|
|
/// </summary>
|
2017-04-30 13:14:47 +02:00
|
|
|
|
public void WaitForExit(Boolean exceuteUpdate = true) {
|
2017-04-25 22:44:17 +02:00
|
|
|
|
while (this.t.ThreadState == ThreadState.Running) { }
|
2017-04-30 13:14:47 +02:00
|
|
|
|
if(exceuteUpdate) {
|
|
|
|
|
if(File.Exists("update.bat")) {
|
|
|
|
|
System.Diagnostics.Process.Start("update.bat");
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-04-20 17:09:37 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Set Path to check for Updates
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="url">HTTP URI</param>
|
2017-04-24 23:55:40 +02:00
|
|
|
|
public void SetUpdateInfo(String url, VersionInfo[] versions) {
|
2017-04-20 17:09:37 +02:00
|
|
|
|
this.url = url;
|
2017-04-23 23:07:41 +02:00
|
|
|
|
this.versions = versions;
|
2017-04-24 23:55:40 +02:00
|
|
|
|
FileStream file = new FileStream("version.xml",FileMode.Create);
|
|
|
|
|
XmlTextWriter xml = new XmlTextWriter(file, Encoding.UTF8);
|
|
|
|
|
xml.WriteStartDocument();
|
|
|
|
|
xml.WriteWhitespace("\n");
|
|
|
|
|
xml.WriteStartElement("filelist");
|
|
|
|
|
xml.WriteWhitespace("\n");
|
|
|
|
|
foreach (VersionInfo version in versions) {
|
|
|
|
|
xml.WriteWhitespace("\t");
|
|
|
|
|
xml.WriteStartElement("file");
|
|
|
|
|
xml.WriteAttributeString("Version", version.Version);
|
|
|
|
|
xml.WriteAttributeString("Filename", version.Filename);
|
|
|
|
|
xml.WriteAttributeString("GUID", version.GUID);
|
|
|
|
|
xml.WriteString(version.Name);
|
|
|
|
|
xml.WriteEndElement();
|
|
|
|
|
xml.WriteWhitespace("\n");
|
2017-04-23 23:07:41 +02:00
|
|
|
|
}
|
2017-04-24 23:55:40 +02:00
|
|
|
|
xml.WriteEndElement();
|
|
|
|
|
xml.Flush();
|
2017-04-23 23:07:41 +02:00
|
|
|
|
file.Flush();
|
|
|
|
|
file.Close();
|
2017-04-20 17:09:37 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Check for Updates
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <exception cref="ArgumentException"></exception>
|
|
|
|
|
public void Check() {
|
|
|
|
|
if(this.url == "") {
|
2017-04-25 22:44:17 +02:00
|
|
|
|
throw new ArgumentException("Zuerst eine URL setzen!");
|
2017-04-20 17:09:37 +02:00
|
|
|
|
}
|
2017-04-23 23:07:41 +02:00
|
|
|
|
if(this.versions.Length == 0) {
|
2017-04-25 22:44:17 +02:00
|
|
|
|
throw new ArgumentException("Zuerst Dateien registrieren!");
|
2017-04-23 23:07:41 +02:00
|
|
|
|
}
|
2017-04-20 17:09:37 +02:00
|
|
|
|
if(this.UpdateResult == null) {
|
2017-04-25 22:44:17 +02:00
|
|
|
|
throw new ArgumentNullException("Zuerst das Update Event anhängen.");
|
2017-04-20 17:09:37 +02:00
|
|
|
|
}
|
2017-04-25 22:44:17 +02:00
|
|
|
|
this.t = new Thread(this.Runner);
|
|
|
|
|
this.t.Start();
|
2017-04-24 23:55:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Runner() {
|
2017-04-30 13:14:47 +02:00
|
|
|
|
Thread.Sleep(1000);
|
2017-04-24 23:55:40 +02:00
|
|
|
|
try {
|
2017-04-25 22:44:17 +02:00
|
|
|
|
Stream stream = WebRequest.Create(this.url + "version.xml").GetResponse().GetResponseStream();
|
|
|
|
|
String content = new StreamReader(stream).ReadToEnd();
|
2017-04-26 23:47:46 +02:00
|
|
|
|
Boolean update = false;
|
2017-04-25 22:44:17 +02:00
|
|
|
|
XmlDocument doc = new XmlDocument();
|
|
|
|
|
doc.LoadXml(content);
|
|
|
|
|
foreach (XmlNode node in doc.DocumentElement.ChildNodes) {
|
|
|
|
|
String guid = node.Attributes["GUID"].Value;
|
|
|
|
|
String version = node.Attributes["Version"].Value;
|
|
|
|
|
for(Int32 i=0;i<this.versions.Length;i++) {
|
|
|
|
|
if (this.versions[i].GUID == guid && this.versions[i].Version != version) {
|
|
|
|
|
this.versions[i].HasUpdate = true;
|
2017-04-26 23:47:46 +02:00
|
|
|
|
update = true;
|
2017-04-25 22:44:17 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-04-26 23:47:46 +02:00
|
|
|
|
if (update) {
|
2017-04-25 22:44:17 +02:00
|
|
|
|
this.UpdateResult(this, new UpdaterEventArgs(true, "Update verfügbar"));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
this.ErrorRaised?.Invoke(this, new UpdaterFailEventArgs(e));
|
2017-04-24 23:55:40 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
2017-04-25 22:44:17 +02:00
|
|
|
|
this.UpdateResult(this, new UpdaterEventArgs(false, "Kein Update verfügbar"));
|
2017-04-20 17:09:37 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Update the file
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="afterExit">Updates the Programm after it has been closed</param>
|
|
|
|
|
/// <returns></returns>
|
2017-04-24 23:55:40 +02:00
|
|
|
|
public Boolean Update(Boolean afterExit = true) {
|
2017-04-25 22:44:17 +02:00
|
|
|
|
try {
|
2017-04-26 23:47:46 +02:00
|
|
|
|
if (afterExit) {
|
|
|
|
|
this.UpdateAfter();
|
|
|
|
|
} else {
|
|
|
|
|
this.UpdateNow();
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
2017-04-25 22:44:17 +02:00
|
|
|
|
this.ErrorRaised?.Invoke(this, new UpdaterFailEventArgs(e));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2017-04-20 17:09:37 +02:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2017-04-26 23:47:46 +02:00
|
|
|
|
|
|
|
|
|
private void UpdateAfter() {
|
|
|
|
|
this.UpdateNow(true);
|
2017-04-30 13:14:47 +02:00
|
|
|
|
StreamWriter update = new StreamWriter("update.bat", false);
|
|
|
|
|
update.WriteLine("echo off");
|
|
|
|
|
update.WriteLine("echo \"Warte 10s\"");
|
|
|
|
|
update.WriteLine("ping 127.0.0.1 -n 10");
|
|
|
|
|
update.WriteLine("echo \"Kopiere Dateien....\"");
|
|
|
|
|
foreach (VersionInfo file in this.versions) {
|
|
|
|
|
if (file.HasUpdate) {
|
|
|
|
|
update.WriteLine("echo \"Kopiere " + file.Filename + "\"");
|
|
|
|
|
update.WriteLine("del " + file.Filename);
|
|
|
|
|
update.WriteLine("move " + file.Filename + "_ " + file.Filename);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
update.WriteLine("start cmd /C ping 127.0.0.1 -n 10 & del update.bat");
|
|
|
|
|
update.Flush();
|
|
|
|
|
update.Close();
|
2017-04-26 23:47:46 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void UpdateNow(Boolean forAfter = false) {
|
|
|
|
|
foreach (VersionInfo file in this.versions) {
|
|
|
|
|
if (file.HasUpdate) {
|
|
|
|
|
Stream stream = WebRequest.Create(this.url + file.Filename).GetResponse().GetResponseStream();
|
|
|
|
|
FileStream target = new FileStream(file.Filename + (forAfter ? "_" : ""), FileMode.Create);
|
|
|
|
|
stream.CopyTo(target);
|
|
|
|
|
target.Flush();
|
|
|
|
|
target.Close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-04-20 17:09:37 +02:00
|
|
|
|
}
|
|
|
|
|
}
|