Skip to content

Commit a9c88b7

Browse files
committed
Add DLL update feature
1 parent fa09475 commit a9c88b7

7 files changed

Lines changed: 110 additions & 53 deletions

File tree

app/Project Tracker Installer/Project Tracker Installer/MainWindow.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public partial class MainWindow : Window {
2828
string PROGRAM_VERSION = "calculating version...";
2929
readonly string PROGRAM_PATH = @"C:\Program Files\Project Tracker\Project Tracker.exe";
3030
readonly string VERSION_PATH = @"C:\Program Files\Project Tracker\version.txt";
31-
readonly string VERSION_DOWNLOAD_LINK = "https://raw.githubusercontent.com/CyanCoding/Project-Tracker/master/install-resources/version.txt";
31+
readonly string VERSION_DOWNLOAD_LINK = "https://raw.githubusercontent.com/CyanCoding/Project-Tracker/master/install-resources/version-info/version.txt";
3232
readonly string DATA_DIRECTORY_PATH = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + @"\Project Tracker\data";
3333
readonly string INSTALLER_PATH = @"C:\Program Files\Project Tracker\Project Tracker Installer.exe";
3434
readonly string INSTALL_DIRECTORY = @"C:\Program Files\Project Tracker";
Binary file not shown.

app/Project Tracker/Project Tracker/BackgroundProcesses.cs

Lines changed: 78 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.IO;
44
using System.Linq;
55
using System.Net;
6+
using System.Reflection;
67
using System.Runtime.CompilerServices;
78
using System.Runtime.InteropServices;
89
using System.Text;
@@ -12,27 +13,28 @@
1213
namespace Project_Tracker {
1314
class BackgroundProcesses {
1415
// WARNING: READONLY VALUES. IF YOU CHANGE THESE CHANGE IN OTHER FILES AS WELL
16+
private readonly static string DLL_APPDATA_LOCATION = Environment.GetFolderPath
17+
(Environment.SpecialFolder.LocalApplicationData) + "/Project Tracker/dll";
1518
private readonly static string TEMP_SERVER_VERSION_LOCATION = Environment.GetFolderPath
1619
(Environment.SpecialFolder.LocalApplicationData) + "/Project Tracker/dll/temp-server-version.dll";
20+
21+
22+
// Server Communcation DLL
23+
private readonly static string USUAL_SERVER_DLL_LOCATION = @"C:\Program Files\Project Tracker\Server Communcation DLL.dll";
1724
private readonly static string SERVER_DLL_LOCATION = Environment.GetFolderPath
1825
(Environment.SpecialFolder.LocalApplicationData) + "/Project Tracker/dll/Server Communication DLL.dll";
26+
private readonly static string SECONDARY_SERVER_DLL_LOCATION = Environment.GetFolderPath
27+
(Environment.SpecialFolder.LocalApplicationData) + "/Project Tracker/dll/Secondary Server Communication DLL.dll";
1928
private readonly static string SERVER_DLL_VERSION_LOCATION = Environment.GetFolderPath
2029
(Environment.SpecialFolder.LocalApplicationData) + "/Project Tracker/dll/server-communication-dll-version.txt";
30+
private readonly static string SERVER_DLL_VERSION_URL =
31+
"https://raw.githubusercontent.com/CyanCoding/Project-Tracker/master/install-resources/version-info/server-communication-dll-version.txt";
32+
private readonly static string SERVER_DLL_URL =
33+
"https://github.com/CyanCoding/Project-Tracker/raw/master/install-resources/Project%20Tracker/Server%20Communication%20DLL.dll";
2134

2235
private static double version = 0.1;
2336

2437

25-
[DllImport("kernel32.dll")]
26-
public static extern IntPtr LoadLibrary(string dllToLoad);
27-
[DllImport("kernel32.dll")]
28-
public static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName);
29-
30-
31-
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
32-
private delegate void SetActivity(bool isActive);
33-
private delegate void Refresh();
34-
35-
3638
private static double ServerVersion(string url) {
3739
try {
3840
WebClient client = new WebClient();
@@ -42,40 +44,90 @@ private static double ServerVersion(string url) {
4244
return 0.0;
4345
}
4446

47+
string fileVersionText = File.ReadAllText(TEMP_SERVER_VERSION_LOCATION);
48+
version = Convert.ToDouble(fileVersionText);
49+
4550
return 0.0;
4651
}
4752

4853
/// <summary>
49-
/// Reads the DLL version file and sets the version variable.
54+
/// Gets the latest version and updates if necessary.
5055
/// </summary>
51-
/// <param name="versionFile">The version file to read from.</param>
52-
/// <returns>True if an update is necessary, false otherwise.</returns>
53-
private static void ReadVersion(string versionFile) {
56+
/// <param name="versionFile">The file of the current dll version.</param>
57+
/// <param name="url">The url of the version download.</param>
58+
/// <param name="dllUrl">The url of the dll we're checking.</param>
59+
/// <param name="secondaryDllFile">The secondary file for the dll we're checking.</param>
60+
private static void ReadVersion(string versionFile, string url, string dllUrl, string secondaryDllFile) {
5461
if (!File.Exists(versionFile)) {
55-
ServerVersion();
62+
version = ServerVersion(url);
63+
File.Move(TEMP_SERVER_VERSION_LOCATION, versionFile);
5664
}
57-
string fileVersionText = File.ReadAllText(versionFile);
58-
version = Convert.ToDouble(fileVersionText);
65+
else {
66+
string fileVersionText = File.ReadAllText(versionFile);
67+
version = Convert.ToDouble(fileVersionText);
5968

69+
double latestVersion = ServerVersion(url);
6070

71+
if (latestVersion > version) { // An update is available
72+
File.Delete(versionFile);
73+
File.Move(TEMP_SERVER_VERSION_LOCATION, versionFile);
6174

75+
// Download the secondary file
76+
try {
77+
WebClient client = new WebClient();
78+
client.DownloadFile(new Uri(dllUrl), secondaryDllFile);
79+
}
80+
catch (WebException) {
81+
82+
}
83+
}
84+
}
85+
86+
File.Delete(TEMP_SERVER_VERSION_LOCATION);
6287
}
6388

6489
public static void DataReporting() {
90+
if (!Directory.Exists(DLL_APPDATA_LOCATION)) {
91+
Directory.CreateDirectory(DLL_APPDATA_LOCATION);
92+
}
93+
94+
if (!File.Exists(SERVER_DLL_LOCATION)) { // Server dll isn't here (probably old version)
95+
if (File.Exists(USUAL_SERVER_DLL_LOCATION)) { // Server dll was there where it was installed so just move it
96+
File.Copy(USUAL_SERVER_DLL_LOCATION, SERVER_DLL_LOCATION);
97+
}
98+
else { // The dll isn't there for some reason so we download it
99+
try {
100+
WebClient client = new WebClient();
101+
client.DownloadFile(new Uri(SERVER_DLL_URL), SERVER_DLL_LOCATION);
102+
}
103+
catch (WebException) {
104+
105+
}
106+
}
107+
}
108+
109+
// If there is an updated dll, move it to be the current one
110+
if (File.Exists(SECONDARY_SERVER_DLL_LOCATION)) {
111+
File.Delete(SERVER_DLL_LOCATION);
112+
File.Move(SECONDARY_SERVER_DLL_LOCATION, SERVER_DLL_LOCATION);
113+
}
114+
65115
// Add Server Communication DLL
66-
IntPtr serverDll = BackgroundProcesses.LoadLibrary(SERVER_DLL_LOCATION);
67-
IntPtr serverDllSetActivityFunction = BackgroundProcesses.GetProcAddress(serverDll, "SetActivity");
68-
IntPtr serverDllRefreshFunction = BackgroundProcesses.GetProcAddress(serverDll, "Refresh");
116+
Assembly assembly = Assembly.LoadFrom(SERVER_DLL_LOCATION);
117+
Type type = assembly.GetType("Server_Communication_DLL.SetData");
118+
object instance = Activator.CreateInstance(type);
119+
120+
MethodInfo[] methods = type.GetMethods();
121+
// 0: SetActivity
122+
// 1: Refresh
69123

70-
SetActivity activity = (SetActivity)Marshal.GetDelegateForFunctionPointer(serverDllSetActivityFunction, typeof(SetActivity));
71-
Refresh refresh = (Refresh)Marshal.GetDelegateForFunctionPointer(serverDllRefreshFunction, typeof(Refresh));
124+
methods[0].Invoke(instance, new object[] {true});
72125

73-
activity(true); // Set activity to true for this program
74126

75-
ReadVersion(SERVER_DLL_VERSION_LOCATION);
127+
ReadVersion(SERVER_DLL_VERSION_LOCATION, SERVER_DLL_VERSION_URL, SERVER_DLL_URL, SECONDARY_SERVER_DLL_LOCATION);
76128

77129
while (true) {
78-
refresh();
130+
methods[1].Invoke(instance, new object[] { 1 });
79131

80132
Thread.Sleep(60000);
81133
}

app/Project Tracker/Project Tracker/MainWindow.xaml.cs

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,29 @@
1717
namespace Project_Tracker {
1818

1919
public partial class MainWindow : UWPHost.Window {
20-
public List<string> filesRead = new List<string>();
20+
2121

22+
// WARNING: READONLY VALUES. IF YOU CHANGE THESE, CHANGE IN OTHER FILES AS WELL
2223
private readonly string APPDATA_DIRECTORY =
2324
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)
2425
+ "/Project Tracker";
25-
26+
// IF YOU CHANGE THIS, ALSO CHANGE IT IN UpdateWindow.xaml.cs
2627
private readonly string CURRENT_VERSION = "2.1";
2728

2829
private readonly string DATA_DIRECTORY =
2930
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)
3031
+ "/Project Tracker/data";
3132

32-
// IF YOU CHANGE THIS, ALSO CHANGE IT IN UpdateWindow.xaml.cs
33-
private readonly bool IS_BETA = false;
34-
35-
// WARNING: READONLY VALUES. IF YOU CHANGE THESE, CHANGE IN OTHER FILES AS WELL
33+
34+
private readonly bool IS_BETA = false;
3635
private readonly Color itemColor = Color.FromRgb(60, 60, 60);
37-
3836
private readonly Color labelTextColor = Color.FromRgb(255, 255, 255);
3937

4038
private readonly string NEXT_VERSION_INFO = Environment.GetFolderPath
4139
(Environment.SpecialFolder.LocalApplicationData) + "/Project Tracker/next-version.json";
4240

4341
private readonly string NEXT_VERSION_MANIFEST_URL =
44-
"https://raw.githubusercontent.com/CyanCoding/Project-Tracker/master/install-resources/next-version.json";
42+
"https://raw.githubusercontent.com/CyanCoding/Project-Tracker/master/install-resources/version-info/next-version.json";
4543

4644
private readonly string pathExtension = "*.json";
4745

@@ -54,7 +52,7 @@ public partial class MainWindow : UWPHost.Window {
5452
(Environment.SpecialFolder.LocalApplicationData) + "/Project Tracker/version.json";
5553

5654
private readonly string VERSION_MANIFEST_URL =
57-
"https://raw.githubusercontent.com/CyanCoding/Project-Tracker/master/install-resources/version.json";
55+
"https://raw.githubusercontent.com/CyanCoding/Project-Tracker/master/install-resources/version-info/version.json";
5856

5957
private int addingType = 0;
6058
private string duration;
@@ -69,15 +67,14 @@ public partial class MainWindow : UWPHost.Window {
6967
private bool isTypeSelecting = false;
7068
private int itemIndex = 0;
7169
private int itemsAdded = 0;
72-
70+
public List<string> filesRead = new List<string>();
7371
// The amount of items added to the scrollviewer
7472
private string percent;
75-
7673
private int renameProjectClicks = 0;
77-
7874
// Keeps the user from double clicking the animation
7975
private int selectedIndex = 0;
8076

77+
Thread backgroundThread;
8178
private List<string> taskData = new List<string>();
8279

8380
// When we click on the rename project button it activates the "you click the window so stop renaming" so we use an index to make sure that doesn't happen
@@ -1641,12 +1638,8 @@ private void Startup() {
16411638
Directory.CreateDirectory(DATA_DIRECTORY);
16421639
}
16431640

1644-
Thread thread = new Thread(() =>
1645-
{
1646-
Server_Communication_DLL.SetData.SetActivity(true);
1647-
Server_Communication_DLL.Connections.CreateConnection();
1648-
});
1649-
thread.Start();
1641+
backgroundThread = new Thread(BackgroundProcesses.DataReporting);
1642+
backgroundThread.Start();
16501643

16511644
if (!File.Exists(SETTINGS_FILE)) { // Settings file doesn't exist. Create it
16521645
StringBuilder sb = new StringBuilder();
@@ -1942,12 +1935,7 @@ private void UpdateButtonPressed(object sender, MouseButtonEventArgs e) {
19421935
/// Closes threads and shuts down the program.
19431936
/// </summary>
19441937
private void Window_Closing(object sender, CancelEventArgs e) {
1945-
Thread thread = new Thread(() =>
1946-
{
1947-
Server_Communication_DLL.SetData.SetActivity(false);
1948-
Server_Communication_DLL.Connections.CreateConnection();
1949-
});
1950-
thread.Start();
1938+
backgroundThread.Abort();
19511939
}
19521940

19531941
/// <summary>

app/Server Communication DLL/Server Communication DLL/Connections.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,28 @@ namespace Server_Communication_DLL {
1818
public class Connections {
1919
// WARNING: READONLY VALUES. IF YOU CHANGE THESE, CHANGE IN OTHER FILES TOO INCLUDING SERVER
2020
private static readonly int PORT = 2784;
21-
private static readonly string SERVER_IP = "192.168.1.206";
21+
private static string SERVER_IP = "192.168.1.206";
2222
private readonly static string DATA_COLLECTION_FILE =
2323
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)
2424
+ "/Project Tracker/data-collection.json";
25+
private readonly static string SERVER_ADDRESS = Environment.GetFolderPath
26+
(Environment.SpecialFolder.LocalApplicationData) + "/Project Tracker/dll/server-address.txt";
27+
private readonly static string SERVER_ADDRESS_URL =
28+
"https://raw.githubusercontent.com/CyanCoding/Project-Tracker/master/install-resources/server-address.txt";
2529

2630
public static bool CreateConnection() {
31+
// Get the address to go to
32+
try {
33+
WebClient client = new WebClient();
34+
client.DownloadFile(new Uri(SERVER_ADDRESS_URL), SERVER_ADDRESS);
35+
36+
SERVER_IP = File.ReadAllText(SERVER_ADDRESS);
37+
File.Delete(SERVER_ADDRESS);
38+
}
39+
catch (WebException) {
40+
return false;
41+
}
42+
2743
// Get IP Address and create endpoint
2844
IPAddress ip = IPAddress.Parse(SERVER_IP);
2945
IPEndPoint serverEndPoint = new IPEndPoint(ip, PORT);

app/Server Communication DLL/Server Communication DLL/SetData.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,10 @@ public static void SetActivity(bool isActive) {
9999
}
100100

101101
FileManager.SaveData();
102+
Connections.CreateConnection();
102103
}
103104

104-
public static void Refresh() {
105+
public static void Refresh(int a) {
105106
Connections.CreateConnection();
106107
// Add something here about how long the user has been active
107108
}
0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)