-
Notifications
You must be signed in to change notification settings - Fork 0
/
modUpdate.cs
142 lines (133 loc) · 5.74 KB
/
modUpdate.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
using System;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Net;
using System.Reflection;
namespace XRFAgent
{
class modUpdate
{
private static WebClient UpdateDownloadClient;
/// <summary>
/// Loading the update module is NOT NEEDED
/// </summary>
public static void Load() { }
/// <summary>
/// Unloading the update module is NOT NEEDED
/// </summary>
public static void Unload() { }
/// <summary>
/// Checks to see if a newer version of the agent is available
/// </summary>
/// <returns>(int) -1 for error, 0 for up-to-date, x for latest version</returns>
public static int CheckVersion()
{
if (modNetwork.IsOnline == false)
{
modLogging.LogEvent("Update check failed", EventLogEntryType.Error, 6021);
return -1;
}
Version currentVersion = Assembly.GetExecutingAssembly().GetName().Version;
UpdateDownloadClient = new WebClient();
int latestVersion = -1;
try
{
latestVersion = int.Parse(UpdateDownloadClient.DownloadString(Properties.Settings.Default.Update_SourceURI + "currentagent.txt"));
}
catch(Exception err)
{
modLogging.LogEvent(err.Message, EventLogEntryType.Error, 6021);
return -1;
}
if (currentVersion.Revision >= latestVersion)
{
modLogging.LogEvent("No update needed", EventLogEntryType.Information);
return 0;
}
else if (currentVersion.Revision < latestVersion)
{
modLogging.LogEvent("Application revision is " + currentVersion.Revision.ToString() + ", current version is " + latestVersion.ToString(), EventLogEntryType.Information);
return latestVersion;
}
else
{
modLogging.LogEvent("Update check failed", EventLogEntryType.Error, 6021);
return -1;
}
}
/// <summary>
/// Checks to see if a newer version of the agent is available and initiates an update
/// </summary>
/// <returns>(int) -1 for error, 0 for up-to-date, x for latest version</returns>
public static int UpdateAgent()
{
int updateNeeded = CheckVersion();
if (updateNeeded >= 1)
{
modLogging.LogEvent("Updating agent", EventLogEntryType.Information, 6023);
try
{
try
{
Directory.Delete(Properties.Settings.Default.Scripts_FolderURI + "agenttemp", true);
}
catch(DirectoryNotFoundException) { }
Directory.CreateDirectory(Properties.Settings.Default.Scripts_FolderURI + "agenttemp");
string UpdateFile = "agent" + updateNeeded.ToString() + ".zip";
UpdateDownloadClient.DownloadFile(Properties.Settings.Default.Update_SourceURI + UpdateFile, Properties.Settings.Default.Scripts_FolderURI + UpdateFile);
ZipFile.ExtractToDirectory(Properties.Settings.Default.Scripts_FolderURI + UpdateFile, Properties.Settings.Default.Scripts_FolderURI + "agenttemp");
File.Delete(Properties.Settings.Default.Scripts_FolderURI + UpdateFile);
File.Copy(Properties.Settings.Default.Scripts_FolderURI + @"agenttemp\agentupdate.bat", Properties.Settings.Default.Scripts_FolderURI + "agentupdate.bat", true);
Process UpdateRunner = new Process();
UpdateRunner.StartInfo.UseShellExecute = true;
UpdateRunner.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
UpdateRunner.StartInfo.FileName = Properties.Settings.Default.Scripts_FolderURI + "agentupdate.bat";
UpdateRunner.Start();
}
catch(Exception err)
{
modLogging.LogEvent(err.Message, EventLogEntryType.Error, 6022);
return -1;
}
}
return updateNeeded;
}
public static int InstallWindowsUpdatePush()
{
try
{
Directory.CreateDirectory(Properties.Settings.Default.Tools_FolderURI);
UpdateDownloadClient.DownloadFile(Properties.Settings.Default.Update_SourceURI + "windowsupdatepush.zip", Properties.Settings.Default.Tools_FolderURI + "windowsupdatepush.zip");
ZipFile.ExtractToDirectory(Properties.Settings.Default.Tools_FolderURI + "windowsupdatepush.zip", Properties.Settings.Default.Tools_FolderURI);
File.Delete(Properties.Settings.Default.Tools_FolderURI + "windowsupdatepush.zip");
return 0;
}
catch (Exception err)
{
modLogging.LogEvent(err.Message, EventLogEntryType.Error, 6024);
return -1;
}
}
public static int Autoupdate()
{
if (modDatabase.GetConfig("Update_Autoupdate") == "enabled")
{
return UpdateAgent();
} else
{
return CheckVersion();
}
}
public static string DisableAutoupdate()
{
modSync.SendSingleConfig("Update_Autoupdate", "disabled");
return "Autoupdate disabled";
}
public static string EnableAutoupdate()
{
modSync.SendSingleConfig("Update_Autoupdate", "enabled");
return "Autoupdate enabled";
}
}
}