From 812a034e8d75233c0865c453cf41d53ec3a3e32f Mon Sep 17 00:00:00 2001
From: chylex <contact@chylex.com>
Date: Tue, 3 Apr 2018 23:44:43 +0200
Subject: [PATCH] Include version header in ScriptLoader files to detect failed
 installs

---
 Resources/PostBuild.ps1   | 11 ++++++++++-
 Resources/ScriptLoader.cs | 31 ++++++++++++++++++++++++++-----
 2 files changed, 36 insertions(+), 6 deletions(-)

diff --git a/Resources/PostBuild.ps1 b/Resources/PostBuild.ps1
index 4714ffd3..139129c9 100644
--- a/Resources/PostBuild.ps1
+++ b/Resources/PostBuild.ps1
@@ -3,6 +3,9 @@ $ErrorActionPreference = "Stop"
 
 Set-Location $dir
 
+$version = (Get-Item TweetDuck.exe).VersionInfo.FileVersion
+Write-Host "TweetDuck version" $version
+
 function Check-Carriage-Return{
   Param([Parameter(Mandatory = $True, Position = 1)] $fname)
   
@@ -19,8 +22,14 @@ function Rewrite-File{
   [CmdletBinding()]
   Param([Parameter(Mandatory = $True, ValueFromPipeline = $True)][array] $lines, [Parameter(Mandatory = $True, Position = 1)] $file)
   
+  $relative = $file.FullName.Substring($dir.Length)
+  
+  if ($relative.StartsWith("scripts\")){
+    $lines = (,("#" + $version) + $lines)
+  }
+  
   $lines | Where { $_ -ne '' } | Set-Content -Path $file.FullName
-  Write-Host "Processed" $file.FullName.Substring($dir.Length)
+  Write-Host "Processed" $relative
 }
 
 try{
diff --git a/Resources/ScriptLoader.cs b/Resources/ScriptLoader.cs
index 230699f0..39e0ffbf 100644
--- a/Resources/ScriptLoader.cs
+++ b/Resources/ScriptLoader.cs
@@ -12,12 +12,29 @@ static class ScriptLoader{
 
         public static string LoadResource(string name, bool silent = false, Control sync = null){
             try{
-                return File.ReadAllText(Path.Combine(Program.ScriptPath, name), Encoding.UTF8);
-            }catch(Exception ex){
-                if (!silent){
-                    ShowLoadError(sync, "Unfortunately, TweetDuck could not load the "+name+" file. The program will continue running with limited functionality.\n\n"+ex.Message);
+                string contents = File.ReadAllText(Path.Combine(Program.ScriptPath, name), Encoding.UTF8);
+                int separator;
+
+                // first line can be either:
+                // #<version>\r\n
+                // #<version>\n
+
+                if (contents[0] != '#'){
+                    ShowLoadError(silent, sync, $"File {name} appears to be corrupted, please try reinstalling the app.");
+                    separator = 0;
+                }
+                else{
+                    separator = contents.IndexOf('\n');
+                    string fileVersion = contents.Substring(1, separator-1).TrimEnd();
+
+                    if (fileVersion != Program.VersionTag){
+                        ShowLoadError(silent, sync, $"File {name} is made for a different version of TweetDuck ({fileVersion}) and may not function correctly in this version, please try reinstalling the app.");
+                    }
                 }
 
+                return contents.Substring(separator).TrimStart();
+            }catch(Exception ex){
+                ShowLoadError(silent, sync, $"Could not load {name}. The program will continue running with limited functionality.\n\n{ex.Message}");
                 return null;
             }
         }
@@ -38,7 +55,11 @@ public static string GetRootIdentifier(string file){
             return "root:"+Path.GetFileNameWithoutExtension(file);
         }
 
-        private static void ShowLoadError(Control sync, string message){
+        private static void ShowLoadError(bool silent, Control sync, string message){
+            if (silent){
+                return;
+            }
+
             if (sync == null){
                 FormMessage.Error("TweetDuck Has Failed :(", message, FormMessage.OK);
             }