From 4d7c048139e543198cf8803c4fa21d07fabd6cd4 Mon Sep 17 00:00:00 2001
From: chylex <contact@chylex.com>
Date: Wed, 25 Apr 2018 19:35:04 +0200
Subject: [PATCH] Remove versions from official plugins and make them only work
 on one app version

---
 Plugins/Plugin.cs                      | 52 +++++++++-----------------
 Plugins/PluginLoader.cs                | 16 +++++++-
 Resources/Plugins/clear-columns/.meta  |  8 +---
 Resources/Plugins/edit-design/.meta    |  8 +---
 Resources/Plugins/emoji-keyboard/.meta |  8 +---
 Resources/Plugins/reply-account/.meta  |  8 +---
 Resources/Plugins/templates/.meta      |  8 +---
 Resources/Plugins/timeline-polls/.meta |  8 +---
 Resources/PostBuild.ps1                |  5 +++
 9 files changed, 43 insertions(+), 78 deletions(-)

diff --git a/Plugins/Plugin.cs b/Plugins/Plugin.cs
index 6aee94e2..dc45e8b0 100644
--- a/Plugins/Plugin.cs
+++ b/Plugins/Plugin.cs
@@ -4,7 +4,6 @@
 
 namespace TweetDuck.Plugins{
     sealed class Plugin{
-        private const string VersionWildcard = "*";
         private static readonly Version AppVersion = new Version(Program.VersionTag);
 
         public string Identifier { get; }
@@ -119,14 +118,14 @@ public override bool Equals(object obj){
         public sealed class Builder{
             private static readonly Version DefaultRequiredVersion = new Version(0, 0, 0, 0);
 
-            public string Name             { get; private set; }
-            public string Description      { get; private set; } = string.Empty;
-            public string Author           { get; private set; } = "(anonymous)";
-            public string Version          { get; private set; } = "(unknown)";
-            public string Website          { get; private set; } = string.Empty;
-            public string ConfigFile       { get; private set; } = string.Empty;
-            public string ConfigDefault    { get; private set; } = string.Empty;
-            public Version RequiredVersion { get; private set; } = DefaultRequiredVersion;
+            public string Name             { get; set; }
+            public string Description      { get; set; } = string.Empty;
+            public string Author           { get; set; } = "(anonymous)";
+            public string Version          { get; set; } = string.Empty;
+            public string Website          { get; set; } = string.Empty;
+            public string ConfigFile       { get; set; } = string.Empty;
+            public string ConfigDefault    { get; set; } = string.Empty;
+            public Version RequiredVersion { get; set; } = DefaultRequiredVersion;
 
             public PluginEnvironment Environments { get; private set; } = PluginEnvironment.None;
 
@@ -142,36 +141,10 @@ public Builder(PluginGroup group, string name, string pathRoot, string pathData)
                 this.identifier = group.GetIdentifierPrefix()+name;
             }
 
-            public void SetFromTag(string tag, string value){
-                switch(tag){
-                    case "NAME":          this.Name = value; break;
-                    case "DESCRIPTION":   this.Description = value; break;
-                    case "AUTHOR":        this.Author = value; break;
-                    case "VERSION":       this.Version = value; break;
-                    case "WEBSITE":       this.Website = value; break;
-                    case "CONFIGFILE":    this.ConfigFile = value; break;
-                    case "CONFIGDEFAULT": this.ConfigDefault = value; break;
-                    case "REQUIRES":      SetRequiredVersion(value); break;
-                    default: throw new FormatException("Invalid metadata tag: "+tag);
-                }
-            }
-
             public void AddEnvironment(PluginEnvironment environment){
                 this.Environments |= environment;
             }
 
-            private void SetRequiredVersion(string versionStr){
-                if (System.Version.TryParse(versionStr, out Version version)){
-                    this.RequiredVersion = version;
-                }
-                else if (versionStr == VersionWildcard){
-                    this.RequiredVersion = DefaultRequiredVersion;
-                }
-                else{
-                    throw new FormatException("Plugin contains invalid minimum version: "+versionStr);
-                }
-            }
-
             public Plugin BuildAndSetup(){
                 Plugin plugin = new Plugin(group, identifier, pathRoot, pathData, this);
 
@@ -183,6 +156,15 @@ public Plugin BuildAndSetup(){
                     throw new InvalidOperationException("Plugin has no script files");
                 }
 
+                if (plugin.Group == PluginGroup.Official){
+                    if (plugin.RequiredVersion != AppVersion){
+                        throw new InvalidOperationException("Plugin is not supported in this version of TweetDuck, this may indicate a failed update or an unsupported plugin that was not removed automatically");
+                    }
+                    else if (!string.IsNullOrEmpty(plugin.Version)){
+                        throw new InvalidOperationException("Official plugins cannot have a version identifier");
+                    }
+                }
+
                 // setup
 
                 string configPath = plugin.ConfigPath, defaultConfigPath = plugin.DefaultConfigPath;
diff --git a/Plugins/PluginLoader.cs b/Plugins/PluginLoader.cs
index 50b83142..08d1852e 100644
--- a/Plugins/PluginLoader.cs
+++ b/Plugins/PluginLoader.cs
@@ -32,7 +32,7 @@ public static Plugin FromFolder(string path, PluginGroup group){
             foreach(string line in File.ReadAllLines(metaFile, Encoding.UTF8).Concat(EndTag).Select(line => line.TrimEnd()).Where(line => line.Length > 0)){
                 if (line[0] == '[' && line[line.Length-1] == ']'){
                     if (currentTag != null){
-                        builder.SetFromTag(currentTag, currentContents);
+                        SetProperty(builder, currentTag, currentContents);
                     }
 
                     currentTag = line.Substring(1, line.Length-2).ToUpper();
@@ -52,5 +52,19 @@ public static Plugin FromFolder(string path, PluginGroup group){
 
             return builder.BuildAndSetup();
         }
+
+        private static void SetProperty(Plugin.Builder builder, string tag, string value){
+            switch(tag){
+                case "NAME":          builder.Name = value; break;
+                case "DESCRIPTION":   builder.Description = value; break;
+                case "AUTHOR":        builder.Author = value; break;
+                case "VERSION":       builder.Version = value; break;
+                case "WEBSITE":       builder.Website = value; break;
+                case "CONFIGFILE":    builder.ConfigFile = value; break;
+                case "CONFIGDEFAULT": builder.ConfigDefault = value; break;
+                case "REQUIRES":      builder.RequiredVersion = Version.TryParse(value, out Version version) ? version : throw new FormatException("Invalid required minimum version: "+value); break;
+                default: throw new FormatException("Invalid metadata tag: "+tag);
+            }
+        }
     }
 }
diff --git a/Resources/Plugins/clear-columns/.meta b/Resources/Plugins/clear-columns/.meta
index ff645374..c189e22d 100644
--- a/Resources/Plugins/clear-columns/.meta
+++ b/Resources/Plugins/clear-columns/.meta
@@ -8,11 +8,5 @@ Clear columns
 [author]
 chylex
 
-[version]
-1.2.1
-
-[website]
-https://tweetduck.chylex.com
-
 [requires]
-1.4.1
\ No newline at end of file
+{version}
\ No newline at end of file
diff --git a/Resources/Plugins/edit-design/.meta b/Resources/Plugins/edit-design/.meta
index 570e6ac1..06ef459f 100644
--- a/Resources/Plugins/edit-design/.meta
+++ b/Resources/Plugins/edit-design/.meta
@@ -7,11 +7,5 @@ Edit layout & design
 [author]
 chylex
 
-[version]
-1.2.5
-
-[website]
-https://tweetduck.chylex.com
-
 [requires]
-1.10.2
\ No newline at end of file
+{version}
\ No newline at end of file
diff --git a/Resources/Plugins/emoji-keyboard/.meta b/Resources/Plugins/emoji-keyboard/.meta
index bf465d67..7c607549 100644
--- a/Resources/Plugins/emoji-keyboard/.meta
+++ b/Resources/Plugins/emoji-keyboard/.meta
@@ -8,11 +8,5 @@ Emoji keyboard
 [author]
 chylex
 
-[version]
-1.4.4
-
-[website]
-https://tweetduck.chylex.com
-
 [requires]
-1.5.3
\ No newline at end of file
+{version}
\ No newline at end of file
diff --git a/Resources/Plugins/reply-account/.meta b/Resources/Plugins/reply-account/.meta
index b6c25ed6..d4332d7d 100644
--- a/Resources/Plugins/reply-account/.meta
+++ b/Resources/Plugins/reply-account/.meta
@@ -7,12 +7,6 @@ Custom reply account
 [author]
 chylex
 
-[version]
-1.3
-
-[website]
-https://tweetduck.chylex.com
-
 [configfile]
 configuration.js
 
@@ -20,4 +14,4 @@ configuration.js
 configuration.default.js
 
 [requires]
-1.10.3
\ No newline at end of file
+{version}
\ No newline at end of file
diff --git a/Resources/Plugins/templates/.meta b/Resources/Plugins/templates/.meta
index df68a401..690689f1 100644
--- a/Resources/Plugins/templates/.meta
+++ b/Resources/Plugins/templates/.meta
@@ -7,11 +7,5 @@ Templates
 [author]
 chylex
 
-[version]
-1.0.4
-
-[website]
-https://tweetduck.chylex.com
-
 [requires]
-1.5.3
\ No newline at end of file
+{version}
\ No newline at end of file
diff --git a/Resources/Plugins/timeline-polls/.meta b/Resources/Plugins/timeline-polls/.meta
index 7cbb339f..7bc7c692 100644
--- a/Resources/Plugins/timeline-polls/.meta
+++ b/Resources/Plugins/timeline-polls/.meta
@@ -7,11 +7,5 @@ Polls in timelines
 [author]
 chylex
 
-[version]
-1.1
-
-[website]
-https://tweetduck.chylex.com
-
 [requires]
-1.7
\ No newline at end of file
+{version}
\ No newline at end of file
diff --git a/Resources/PostBuild.ps1 b/Resources/PostBuild.ps1
index d35be1a0..88cb7e10 100644
--- a/Resources/PostBuild.ps1
+++ b/Resources/PostBuild.ps1
@@ -93,6 +93,11 @@ try{
   }
   
   Write-Host "------------------------------"
+  ForEach($file in Get-ChildItem -Path $targetDir -Filter "*.meta" -Recurse){
+    $lines = Get-Content -Path $file.FullName
+    $lines = $lines -Replace '\{version\}', $version
+    ,$lines | Rewrite-File $file
+  }
 }catch{
   Write-Host "Encountered an error while running PostBuild.ps1 on line" $_.InvocationInfo.ScriptLineNumber
   Write-Host $_