From fffd45f09f4a6b947148f6e1d775cde75b9eea26 Mon Sep 17 00:00:00 2001 From: hbl917070 Date: Sat, 16 Mar 2024 03:33:39 +0800 Subject: [PATCH] Improve startup speed --- README.md | 2 +- README.zh_TW.md | 2 +- Tiefsee/AppPath.cs | 72 ++++++++++++++++++++++++++++------- Tiefsee/Program.cs | 16 +++++--- Tiefsee/StartWindow.cs | 2 + TiefseeLauncherDll/Program.cs | 16 ++++---- 6 files changed, 80 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index 2fb3206..791df7c 100644 --- a/README.md +++ b/README.md @@ -70,7 +70,7 @@ Website:hbl917070.github.io/ - There is no functional difference between the Store and Portable versions - The Store version can be updated through the store and will not leave any data after Tiefsee is deleted - The Portable version requires the installation of [.NET Desktop Runtime 7 (x64)](https://dotnet.microsoft.com/en-us/download/dotnet/7.0) to run - - The Portable version can use 'portable mode' by creating a `portableMode` folder next to Tiefsee.exe, and all data will be stored inside + - The Portable version can use 'portable mode' by creating a `PortableMode` folder next to Tiefsee.exe, and all data will be stored inside > If there are no special requirements, it is recommended to use the Store version diff --git a/README.zh_TW.md b/README.zh_TW.md index 14a796e..f8b89e1 100644 --- a/README.zh_TW.md +++ b/README.zh_TW.md @@ -71,7 +71,7 @@ Tiefsee - 商店版與免安裝版功能上沒有差異 - 商店版可以透過商店來進行更新,且刪除 Tiefsee 後不會留下任何資料 - 免安裝版需要安裝 [.NET Desktop Runtime 7 (x64)](https://dotnet.microsoft.com/en-us/download/dotnet/7.0) 才能運行 - - 免安裝版可以使用「便攜模式」 ,在 Tiefsee.exe 旁邊新建一個`portableMode`資料夾,資料就都會儲存在裡面 + - 免安裝版可以使用「便攜模式」 ,在 Tiefsee.exe 旁邊新建一個`PortableMode`資料夾,資料就都會儲存在裡面 > 沒有特別需求的話,我推薦使用商店版 diff --git a/Tiefsee/AppPath.cs b/Tiefsee/AppPath.cs index 542327b..6f04f78 100644 --- a/Tiefsee/AppPath.cs +++ b/Tiefsee/AppPath.cs @@ -37,24 +37,58 @@ public class AppPath { /// 工作列右下角的圖示 public static string logoIcon = ""; - public static void Init() { - - try { // 商店版 - appData = Windows.Storage.ApplicationData.Current.LocalCacheFolder.Path; - appData = Path.Combine(appData, "Local", "Tiefsee"); - StartWindow.isStoreApp = true; + /// + /// 在使用 Init() 之前如果就需要使用 appData 的話,就使用此方法 + /// + public static void InitAppData() { + if (appData != null) { return; } + + // 便攜模式 (如果存在此資料夾,就把資料儲存在這裡 + string portableMode = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "PortableMode"); + if (Directory.Exists(portableMode)) { + appData = portableMode; + StartWindow.isPortableMode = true; + } + else { + appData = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Tiefsee"); + StartWindow.isPortableMode = false; } - catch { // 傳統應用程式版 + appDataStartIni = Path.Combine(appData, "Start.ini"); + } + + public static void Init(string iniAppData, bool iniIsStoreApp) { - string portableMode = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "portableMode"); - if (Directory.Exists(portableMode)) { // 便攜模式 (如果存在此資料夾,就把資料儲存在這裡 - appData = portableMode; + InitAppData(); + + // 需要更新 ini + bool needUpdateIni = false; + + // 便攜模式 + if (StartWindow.isPortableMode) { + StartWindow.isStoreApp = false; + } + // 不存在此路徑,表示是傳統應用程式版 + else if (File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "../TiefseeLauncher/Tiefsee.exe")) == false) { + StartWindow.isStoreApp = false; + } + // 如果已經 ini 檔案有資料,就使用 ini 檔案的資料 + else if (iniAppData != "") { + StartWindow.isStoreApp = iniIsStoreApp; + appData = iniAppData; + } + else { + try { + // 商店版 + appData = Windows.Storage.ApplicationData.Current.LocalCacheFolder.Path; + appData = Path.Combine(appData, "Local", "Tiefsee"); + StartWindow.isStoreApp = true; + needUpdateIni = true; } - else { + catch { + // 傳統應用程式版 appData = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Tiefsee"); + StartWindow.isStoreApp = false; } - - StartWindow.isStoreApp = false; } appDataLock = Path.Combine(appData, "Lock"); @@ -76,13 +110,23 @@ public static void Init() { //------ // 如果資料夾不存在,就新建 - if (Directory.Exists(appData) == false) { Directory.CreateDirectory(appData); } if (Directory.Exists(appDataPlugin) == false) { Directory.CreateDirectory(appDataPlugin); } + + if (needUpdateIni) { + // 重新讀取 ini + var iniManager = new IniManager(AppPath.appDataStartIni); + Program.startPort = Int32.Parse(iniManager.ReadIniFile("setting", "startPort", "4876")); + Program.startType = Int32.Parse(iniManager.ReadIniFile("setting", "startType", "3")); + // 把資料寫入 ini 檔案,下次就可以直接讀取 + iniManager.WriteIniFile("temporary", "appData", appData); + iniManager.WriteIniFile("temporary", "isStoreApp", StartWindow.isStoreApp.ToString()); + } + } } diff --git a/Tiefsee/Program.cs b/Tiefsee/Program.cs index 09d80ed..a45afc0 100644 --- a/Tiefsee/Program.cs +++ b/Tiefsee/Program.cs @@ -26,8 +26,16 @@ static class Program { static void Main(string[] args) { // 修改 工作目錄 為程式資料夾 (如果有傳入 args 的話,工作目錄會被修改,所以需要改回來 - Directory.SetCurrentDirectory(System.AppDomain.CurrentDomain.BaseDirectory); - AppPath.Init(); + Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory); + AppPath.InitAppData(); + + var iniManager = new IniManager(AppPath.appDataStartIni); + startPort = Int32.Parse(iniManager.ReadIniFile("setting", "startPort", "4876")); + startType = Int32.Parse(iniManager.ReadIniFile("setting", "startType", "3")); + var appData = iniManager.ReadIniFile("temporary", "appData", ""); + var isStoreApp = iniManager.ReadIniFile("temporary", "isStoreApp", "") == "True"; + + AppPath.Init(appData, isStoreApp); // 如果是商店 APP 版,且是來自「開機自動啟動」 if (StartWindow.isStoreApp) { @@ -42,10 +50,6 @@ static void Main(string[] args) { catch { } } - IniManager iniManager = new IniManager(AppPath.appDataStartIni); - startPort = Int32.Parse(iniManager.ReadIniFile("setting", "startPort", "4876")); - startType = Int32.Parse(iniManager.ReadIniFile("setting", "startType", "3")); - bool argsIsNone = (args.Length == 1 && args[0] == "none"); // 啟動參數是 none if (args.Length >= 1 && args[0] == "restart") { // 啟動參數是 restart diff --git a/Tiefsee/StartWindow.cs b/Tiefsee/StartWindow.cs index b7e73a9..74f75e5 100644 --- a/Tiefsee/StartWindow.cs +++ b/Tiefsee/StartWindow.cs @@ -10,6 +10,8 @@ public class StartWindow : Form { public static bool isRunGC = false; /// 是否為商店版 APP public static bool isStoreApp = false; + /// 是否為 便攜模式 + public static bool isPortableMode = false; /// 用於鎖定 port 檔案 private FileStream fsPort; /// 是否為 win11 diff --git a/TiefseeLauncherDll/Program.cs b/TiefseeLauncherDll/Program.cs index 4f7b307..3feee75 100644 --- a/TiefseeLauncherDll/Program.cs +++ b/TiefseeLauncherDll/Program.cs @@ -5,11 +5,11 @@ namespace TiefseeLauncher; public class Program { - - /*[STAThread] + /* + [STAThread] public static void Main(string[] args) { new Launcher().Init(args); - }*/ + } */ // 導出到 C++ [UnmanagedCallersOnly(EntryPoint = "run")] @@ -56,7 +56,7 @@ public void Init(string[] args) { exePath = Path.Combine(baseDirectory, "../Tiefsee/TiefseeCore.exe"); } - string portableMode = Path.Combine(baseDirectory, "portableMode"); + string portableMode = Path.Combine(baseDirectory, "PortableMode"); if (Directory.Exists(portableMode)) { // 便攜模式 (如果存在此資料夾,就把資料儲存在這裡 appData = portableMode; } @@ -66,7 +66,6 @@ public void Init(string[] args) { appDataStartIni = Path.Combine(appData, "Start.ini"); appDataPort = Path.Combine(appData, "Port"); - var iniManager = new IniManager(appDataStartIni); startType = Int32.Parse(iniManager.ReadIniFile("setting", "startType", "3")); @@ -174,8 +173,9 @@ public string ReadIniFile(string section, string key, string defaultValue) { return lpReturnedString.ToString(); } + /* // write ini data depend on section and key - // public void WriteIniFile(string section, string key, Object value) { - // WritePrivateProfileString(section, key, value.ToString(), filePath); - // } + public void WriteIniFile(string section, string key, Object value) { + WritePrivateProfileString(section, key, value.ToString(), filePath); + }*/ }