33using System . IO ;
44using System . Linq ;
55using System . Net ;
6+ using System . Reflection ;
67using System . Runtime . CompilerServices ;
78using System . Runtime . InteropServices ;
89using System . Text ;
1213namespace 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 }
0 commit comments