Skip to content

Commit

Permalink
Make Dump Reader only replace database at the end and ask user to con…
Browse files Browse the repository at this point in the history
…firm.
  • Loading branch information
Zoltanar committed Apr 14, 2024
1 parent a6d5df6 commit 5394fad
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
22 changes: 15 additions & 7 deletions DatabaseDumpReader/DumpReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,21 @@ public class DumpReader
public List<VnTag> VnTags { get; } = new();
public Dictionary<int, List<DumpVote>> Votes { get; private set; }

public DumpReader(string dumpFolder, string dbFilePath, int userId)
public DumpReader(string dumpFolder, string currentDatabaseFilePath, int userId, out string inProgressDbFile)
{
if (!Directory.Exists(dumpFolder)) throw new IOException($"Dump folder does not exist: '{dumpFolder}'");
var dbFile = new FileInfo(dbFilePath);
if (!dbFile.Exists) throw new IOException($"Original database does not exist: '{dbFile}'");
var dbDirectory = Path.GetDirectoryName(dbFile.FullName) ?? throw new ArgumentNullException(nameof(Path.GetDirectoryName));
var backupPath = Path.Combine(dbDirectory, $"{Path.GetFileNameWithoutExtension(dbFile.FullName)}-DRB{DateTime.Now:yyyyMMdd-HHmmss}{dbFile.Extension}");
var currentDbFile = new FileInfo(currentDatabaseFilePath);
if (!currentDbFile.Exists) throw new IOException($"Original database does not exist: '{currentDbFile}'");
//UIP = update in progress
inProgressDbFile = GetDatabaseFile(currentDbFile, "-UIP");
File.Copy(currentDatabaseFilePath, inProgressDbFile);
//DRB = dump reader backup
var backupPath = GetDatabaseFile(currentDbFile, "-DRB");
StaticHelpers.Logger.ToFile($"Backing up database to {backupPath}");
File.Copy(dbFile.FullName, backupPath);
File.Copy(currentDbFile.FullName, backupPath);
UserId = userId;
DumpFolder = dumpFolder;
OutputFilePath = dbFile.FullName;
OutputFilePath = inProgressDbFile;
Database = new VisualNovelDatabase(OutputFilePath, false);
StaticHelpers.LocalDatabase = Database;
Database.DeleteForDump();
Expand All @@ -60,6 +63,11 @@ public DumpReader(string dumpFolder, string dbFilePath, int userId)
Database);
}

private string GetDatabaseFile(FileInfo currentDatabaseFile, string suffix)
{
var dbDirectory = Path.GetDirectoryName(currentDatabaseFile.FullName) ?? throw new ArgumentNullException(nameof(Path.GetDirectoryName));
return Path.Combine(dbDirectory, $"{Path.GetFileNameWithoutExtension(currentDatabaseFile.FullName)}{suffix}{DateTime.Now:yyyyMMdd-HHmmss}{currentDatabaseFile.Extension}");
}
public void Run(DateTime dumpDate, int[] previousVnIds, int[] previousCharacterIds)
{
StaticHelpers.Logger.ToFile("Starting Dump Reader...");
Expand Down
26 changes: 24 additions & 2 deletions DatabaseDumpReader/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,34 @@ private static ExitCode Run(string dumpFolder, int userId, out Stopwatch downloa
Debug.Assert(dumpFileInfo.NewFileDate != null, nameof(dumpFileInfo.NewFileDate) + " != null");
StaticHelpers.Logger.ToFile(oldDateString, $"Getting update to: {dumpFileInfo.NewFileDate.Value.ToShortDateString()}.");
runWatch = Stopwatch.StartNew();
var processor = new DumpReader(dumpFileInfo.LatestDumpFolder, StaticHelpers.DatabaseFile, userId);
var processor = new DumpReader(dumpFileInfo.LatestDumpFolder, StaticHelpers.DatabaseFile, userId, out var inProgressFile);
processor.Run(dumpFileInfo.NewFileDate.Value, previousVnIds, previousCharacterIds);
var result = dumpFileInfo.UpToDate ? ExitCode.ReloadLatest : ExitCode.Update;
if (result is ExitCode.ReloadLatest or ExitCode.Update)
{
Console.WriteLine("Update successful, ensure Happy Reader is closed and enter 'y' to replace database file with updated or 'n' to abandon (y/n):");
var line = Console.ReadLine()!;
while (true)
{
if (line.Equals("n", StringComparison.OrdinalIgnoreCase))
{
Console.WriteLine("Update abandoned.");
break;
}

if (line.Equals("y", StringComparison.OrdinalIgnoreCase))
{
File.Delete(StaticHelpers.DatabaseFile);
File.Move(inProgressFile, StaticHelpers.DatabaseFile);
break;
}
Console.WriteLine("Save update? (y/n)");
line = Console.ReadLine()!;
}
}
runWatch.Stop();
RemovePastBackups(dumpFolder, dumpFileInfo);
return result;
return result;
}

private static DumpFileInfo GetLatestDump(DateTime? previousUpdate, string dumpsFolder, out Stopwatch downloadWatch)
Expand Down

0 comments on commit 5394fad

Please sign in to comment.