/// <summary> /// Initializes the <see cref="Database"/> class. /// </summary> static Database() { if (string.IsNullOrWhiteSpace(Signature.InstallPath)) { Log.Fatal("Stopping database initialization because $InstallPath is null or empty."); return; } if (!Directory.Exists(DataPath)) { Log.Info("Creating database folder: " + DataPath); Directory.CreateDirectory(DataPath); } var tmp = Path.Combine(Signature.UACVirtualPath, "db"); if (Directory.Exists(tmp)) { Log.Info("$UACVirtualPath\\db exists, initiating migration to $DataPath..."); foreach (var dir in Directory.EnumerateDirectories(tmp)) { var fn = Path.GetFileName(dir); var nir = Path.Combine(DataPath, fn); Log.Info("Migrating " + fn + "..."); if (Directory.Exists(nir) || !File.Exists(Path.Combine(dir, "info")) || !File.Exists(Path.Combine(dir, "conf"))) { Log.Info(fn + " already exists in the target directory or is not a valid TV show directory."); continue; } try { var show = TVShow.Load(dir); show.ID += 1000; show.Directory = nir; Directory.CreateDirectory(show.Directory); show.Save(); show.SaveTracking(); Directory.Delete(dir, true); } catch (Exception ex) { Log.Warn("Exception while migrating data.", ex); continue; } } Log.Info("Migration finished."); } LoadDatabase(); }
/// <summary> /// Loads the database files. /// </summary> public static void LoadDatabase() { Log.Info("Loading database..."); DataChange = DateTime.Now; TVShows = new Dictionary <int, TVShow>(); foreach (var dir in Directory.EnumerateDirectories(DataPath)) { var fn = Path.GetFileName(dir); if (Log.IsTraceEnabled) { Log.Trace("Reading " + fn + "..."); } if (!File.Exists(Path.Combine(dir, "info")) || !File.Exists(Path.Combine(dir, "conf"))) { Log.Warn(fn + " is not a valid TV show directory."); continue; } try { var show = TVShow.Load(dir); TVShows[show.ID] = show; } catch (Exception ex) { Log.Error("Error while loading TV show data " + fn + ".", ex); continue; //MainWindow.HandleUnexpectedException(new Exception("Couldn't load db\\" + fn + " due to an error.", ex)); } } Log.Debug("Database loaded in " + (DateTime.Now - DataChange).TotalSeconds + "s, containing " + TVShows.Count + " shows."); Log.Info("UUID is " + Utils.GetUUID()); }