From dbeb4c72050ccdf4a9a73a8005d1983112492c1a Mon Sep 17 00:00:00 2001
From: chylex <info@chylex.com>
Date: Fri, 2 Sep 2016 20:59:03 +0200
Subject: [PATCH] Reorganize path constants and allow the program to be
 portable

---
 Program.cs                | 13 +++++++++----
 Resources/ScriptLoader.cs |  2 +-
 2 files changed, 10 insertions(+), 5 deletions(-)

diff --git a/Program.cs b/Program.cs
index 0c951bf3..31b3b774 100644
--- a/Program.cs
+++ b/Program.cs
@@ -33,10 +33,15 @@ static class Program{
 
         public static readonly Version Version = new Version(VersionTag);
 
-        public static readonly string StoragePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), BrandName);
-        public static readonly string PluginPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "plugins");
-        public static readonly string TemporaryPath = Path.Combine(Path.GetTempPath(), BrandName);
+        public static readonly bool IsPortable = File.Exists("makeportable");
+
+        public static readonly string ProgramPath = AppDomain.CurrentDomain.BaseDirectory;
+        public static readonly string StoragePath = IsPortable ? Path.Combine(ProgramPath, "portable", "storage") : Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), BrandName);
+        public static readonly string TemporaryPath = IsPortable ? Path.Combine(ProgramPath, "portable", "tmp") : Path.Combine(Path.GetTempPath(), BrandName);
         public static readonly string ConfigFilePath = Path.Combine(StoragePath, "TD_UserConfig.cfg");
+        
+        public static readonly string ScriptPath = Path.Combine(ProgramPath, "scripts");
+        public static readonly string PluginPath = Path.Combine(ProgramPath, "plugins");
 
         public static uint WindowRestoreMessage;
 
@@ -47,7 +52,7 @@ static class Program{
 
         public static string LogFile{
             get{
-                return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "td-log.txt");
+                return Path.Combine(ProgramPath, "td-log.txt");
             }
         }
 
diff --git a/Resources/ScriptLoader.cs b/Resources/ScriptLoader.cs
index 3c68b286..0cc749b8 100644
--- a/Resources/ScriptLoader.cs
+++ b/Resources/ScriptLoader.cs
@@ -11,7 +11,7 @@ static class ScriptLoader{
 
         public static string LoadResource(string name){
             try{
-                return File.ReadAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "scripts", name), Encoding.UTF8);
+                return File.ReadAllText(Path.Combine(Program.ScriptPath, name), Encoding.UTF8);
             }catch(Exception ex){
                 MessageBox.Show("Unfortunately, "+Program.BrandName+" could not load the "+name+" file. The program will continue running with limited functionality.\r\n\r\n"+ex.Message, Program.BrandName+" Has Failed :(", MessageBoxButtons.OK, MessageBoxIcon.Error);
                 return null;