/// <summary> /// Launch after Application.Current is loaded /// </summary> public static void Init() { // Set fallback BaseDir string baseDir = BaseDir = Environment.CurrentDirectory; // Setup unhandled exception handler (requires BaseDir to be set) AppDomain.CurrentDomain.UnhandledException += AppDomain_UnhandledException; // Run ArgumentParser ArgumentParser argParser = new ArgumentParser(); PEBakeryOptions opts = argParser.Parse(Args); if (opts == null) // Arguments parse fail { Environment.Exit(1); // Force Shutdown } // Setup BaseDir if (opts.BaseDir != null) { baseDir = Path.GetFullPath(opts.BaseDir); if (Directory.Exists(baseDir) == false) { MessageBox.Show($"Directory [{baseDir}] does not exist.\r\nRun [PEBkaery --help] for commnad line help message.", "PEBakery CommandLine Error", MessageBoxButton.OK, MessageBoxImage.Error); Environment.Exit(1); // Force Shutdown } Environment.CurrentDirectory = BaseDir = baseDir; } // Database directory string dbDir = Path.Combine(BaseDir, "Database"); if (!Directory.Exists(dbDir)) { Directory.CreateDirectory(dbDir); } // Log Database string logDbFile = Path.Combine(dbDir, "PEBakeryLog.db"); try { Logger = new Logger(logDbFile); Logger.SystemWrite(new LogInfo(LogState.Info, "PEBakery launched")); } catch (SQLiteException) { // Update failure -> retry with clearing existing log db File.Delete(logDbFile); try { Logger = new Logger(logDbFile); Logger.SystemWrite(new LogInfo(LogState.Info, "PEBakery launched, log cleared due to an error")); } catch (SQLiteException e) { // Unable to continue -> raise an error message string msg = $"SQLite Error : {e.Message}\r\n\r\nThe Log database is corrupted and was not able to be repaired.\r\nPlease delete {dbDir}\\PEBakeryLog.db and restart."; MessageBox.Show(msg, "SQLite Error!", MessageBoxButton.OK, MessageBoxImage.Error); if (Application.Current != null) { Application.Current.Shutdown(1); } else { Environment.Exit(1); } } } // Init ProjectCollection Projects = new ProjectCollection(BaseDir); // Setting File string settingFile = Path.Combine(BaseDir, "PEBakery.ini"); Setting = new Setting(settingFile); Setting.ApplySetting(); // Custom Title if (Setting.Interface.UseCustomTitle) { MainViewModel.TitleBar = Setting.Interface.CustomTitle; } // Init script cache DB, regardless of Setting.Script.EnableCache string cacheDbFile = Path.Combine(dbDir, "PEBakeryCache.db"); try { ScriptCache = new ScriptCache(cacheDbFile); int cachedScriptCount = ScriptCache.Table <CacheModel.ScriptCache>().Count(); if (Setting.Script.EnableCache) { Logger.SystemWrite(new LogInfo(LogState.Info, $"ScriptCache enabled, {cachedScriptCount} cached scripts found")); } else { Logger.SystemWrite(new LogInfo(LogState.Info, "ScriptCache disabled")); } } catch (SQLiteException) { // Load failure -> Fallback, delete and remake database File.Delete(cacheDbFile); try { ScriptCache = new ScriptCache(cacheDbFile); Logger.SystemWrite(new LogInfo(LogState.Info, $"ScriptCache enabled, cache cleared due to an error")); } catch (SQLiteException e) { // Unable to continue -> raise an error message string msg = $"SQLite Error : {e.Message}\r\n\r\nThe Cache database is corrupted and was not able to be repaired.\r\nPlease delete {dbDir}\\PEBakeryCache.db and restart."; MessageBox.Show(msg, "SQLite Error!", MessageBoxButton.OK, MessageBoxImage.Error); if (Application.Current != null) { Application.Current.Shutdown(1); } else { Environment.Exit(1); } } } }
/// <summary> /// Launch after Application.Current is loaded /// </summary> public static void Init() { string baseDir = Environment.CurrentDirectory; for (int i = 0; i < Args.Length; i++) { if (Args[i].Equals("/basedir", StringComparison.OrdinalIgnoreCase)) { if (i + 1 < Args.Length) { baseDir = Path.GetFullPath(Args[i + 1]); if (!Directory.Exists(baseDir)) { MessageBox.Show($"Directory [{baseDir}] does not exist", "Invalid BaseDir", MessageBoxButton.OK, MessageBoxImage.Error); Environment.Exit(1); // Force Shutdown } Environment.CurrentDirectory = baseDir; } else { // ReSharper disable once LocalizableElement Console.WriteLine("\'/basedir\' must be used with path\r\n"); } } else if (Args[i].Equals("/?", StringComparison.OrdinalIgnoreCase) || Args[i].Equals("/help", StringComparison.OrdinalIgnoreCase) || Args[i].Equals("/h", StringComparison.OrdinalIgnoreCase)) { // ReSharper disable once LocalizableElement Console.WriteLine("Sorry, help message not implemented\r\n"); } } BaseDir = baseDir; // Database directory string dbDir = Path.Combine(BaseDir, "Database"); if (!Directory.Exists(dbDir)) { Directory.CreateDirectory(dbDir); } // Log Database string logDbFile = Path.Combine(dbDir, "PEBakeryLog.db"); try { Logger = new Logger(logDbFile); Logger.SystemWrite(new LogInfo(LogState.Info, "PEBakery launched")); } catch (SQLiteException e) { // Update failure string msg = $"SQLite Error : {e.Message}\r\n\r\nLog database is corrupted.\r\nPlease delete PEBakeryLog.db and restart."; MessageBox.Show(msg, "SQLite Error!", MessageBoxButton.OK, MessageBoxImage.Error); if (Application.Current != null) { Application.Current.Shutdown(1); } else { Environment.Exit(1); } } // Init ProjectCollection Projects = new ProjectCollection(BaseDir); // Setting File string settingFile = Path.Combine(BaseDir, "PEBakery.ini"); Setting = new Setting(settingFile); Setting.ApplySetting(); // Custom Title if (Setting.Interface.UseCustomTitle) { MainViewModel.TitleBar = Setting.Interface.CustomTitle; } // Load script cache if (Setting.Script.EnableCache) { string cacheDbFile = Path.Combine(dbDir, "PEBakeryCache.db"); try { ScriptCache = new ScriptCache(cacheDbFile); Logger.SystemWrite(new LogInfo(LogState.Info, $"ScriptCache enabled, {ScriptCache.Table<CacheModel.ScriptCache>().Count()} cached scripts found")); } catch (SQLiteException e) { // Load failure string msg = $"SQLite Error : {e.Message}\r\n\r\nCache database is corrupted.\r\nPlease delete PEBakeryCache.db and restart."; MessageBox.Show(msg, "SQLite Error!", MessageBoxButton.OK, MessageBoxImage.Error); if (Application.Current != null) { Application.Current.Shutdown(1); } else { Environment.Exit(1); } } } else { Logger.SystemWrite(new LogInfo(LogState.Info, "ScriptCache disabled")); } }