2021-01-30 01:37:36 +01:00
|
|
|
|
using Flowframes.Data;
|
|
|
|
|
|
using Flowframes.IO;
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Threading.Tasks;
|
2021-02-03 20:51:36 +01:00
|
|
|
|
using I = Flowframes.Interpolate;
|
2021-01-30 01:37:36 +01:00
|
|
|
|
|
|
|
|
|
|
namespace Flowframes.Main
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
class ResumeUtils
|
|
|
|
|
|
{
|
2021-02-02 21:48:18 +01:00
|
|
|
|
public static bool resumeNextRun;
|
|
|
|
|
|
|
2021-01-30 01:37:36 +01:00
|
|
|
|
public static float timeBetweenSaves = 10;
|
|
|
|
|
|
public static int minFrames = 100;
|
2021-02-01 18:05:50 +01:00
|
|
|
|
public static int safetyDelayFrames = 50;
|
|
|
|
|
|
public static string resumeFilename = "resume.ini";
|
|
|
|
|
|
public static string interpSettingsFilename = "interpSettings.ini";
|
|
|
|
|
|
public static string filenameMapFilename = "frameFilenames.ini";
|
2021-01-30 01:37:36 +01:00
|
|
|
|
|
|
|
|
|
|
public static Stopwatch timeSinceLastSave = new Stopwatch();
|
|
|
|
|
|
|
|
|
|
|
|
public static void Save ()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (timeSinceLastSave.IsRunning && timeSinceLastSave.ElapsedMilliseconds < (timeBetweenSaves * 1000f).RoundToInt()) return;
|
2021-02-03 20:51:36 +01:00
|
|
|
|
int frames = (int)Math.Round((float)InterpolateUtils.interpolatedInputFramesCount / I.current.interpFactor) - safetyDelayFrames;
|
2021-02-01 18:05:50 +01:00
|
|
|
|
if (frames < 1) return;
|
2021-01-30 01:37:36 +01:00
|
|
|
|
timeSinceLastSave.Restart();
|
2021-02-03 20:51:36 +01:00
|
|
|
|
Directory.CreateDirectory(Path.Combine(I.current.tempFolder, Paths.resumeDir));
|
2021-02-01 18:05:50 +01:00
|
|
|
|
SaveState(frames);
|
|
|
|
|
|
SaveInterpSettings();
|
|
|
|
|
|
SaveFilenameMap();
|
2021-01-30 01:37:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-02-01 18:05:50 +01:00
|
|
|
|
static void SaveState (int frames)
|
2021-01-30 01:37:36 +01:00
|
|
|
|
{
|
2021-02-03 20:51:36 +01:00
|
|
|
|
ResumeState state = new ResumeState(I.currentlyUsingAutoEnc, frames);
|
|
|
|
|
|
string filePath = Path.Combine(I.current.tempFolder, Paths.resumeDir, resumeFilename);
|
2021-02-01 18:05:50 +01:00
|
|
|
|
File.WriteAllText(filePath, state.ToString());
|
2021-01-30 01:37:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static async Task SaveFilenameMap ()
|
|
|
|
|
|
{
|
2021-02-03 20:51:36 +01:00
|
|
|
|
string filePath = Path.Combine(I.current.tempFolder, Paths.resumeDir, filenameMapFilename);
|
2021-01-30 01:37:36 +01:00
|
|
|
|
|
|
|
|
|
|
if (File.Exists(filePath) && IOUtils.GetFilesize(filePath) > 0)
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
string fileContent = "";
|
|
|
|
|
|
int counter = 0;
|
|
|
|
|
|
|
|
|
|
|
|
foreach (KeyValuePair<string, string> entry in AiProcess.filenameMap)
|
|
|
|
|
|
{
|
2021-01-31 11:38:41 +01:00
|
|
|
|
if (counter % 1000 == 0) await Task.Delay(1);
|
2021-01-30 01:37:36 +01:00
|
|
|
|
fileContent += $"{entry.Key}|{entry.Value}\n";
|
2021-01-31 11:38:41 +01:00
|
|
|
|
counter++;
|
2021-01-30 01:37:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
File.WriteAllText(filePath, fileContent);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-02-01 18:05:50 +01:00
|
|
|
|
static void SaveInterpSettings ()
|
|
|
|
|
|
{
|
2021-02-03 20:51:36 +01:00
|
|
|
|
string filepath = Path.Combine(I.current.tempFolder, Paths.resumeDir, interpSettingsFilename);
|
|
|
|
|
|
File.WriteAllText(filepath, I.current.Serialize());
|
2021-02-01 18:05:50 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static void LoadTempFolder (string tempFolderPath)
|
|
|
|
|
|
{
|
|
|
|
|
|
string resumeFolderPath = Path.Combine(tempFolderPath, Paths.resumeDir);
|
2021-02-01 22:48:22 +01:00
|
|
|
|
string interpSettingsPath = Path.Combine(resumeFolderPath, interpSettingsFilename);
|
|
|
|
|
|
InterpSettings interpSettings = new InterpSettings(File.ReadAllText(interpSettingsPath));
|
|
|
|
|
|
Program.mainForm.LoadBatchEntry(interpSettings);
|
2021-02-01 18:05:50 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-02-02 21:48:18 +01:00
|
|
|
|
public static async Task PrepareResumedRun ()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!resumeNextRun) return;
|
|
|
|
|
|
|
2021-02-03 20:51:36 +01:00
|
|
|
|
string stateFilepath = Path.Combine(I.current.tempFolder, Paths.resumeDir, resumeFilename);
|
2021-02-02 21:48:18 +01:00
|
|
|
|
ResumeState state = new ResumeState(File.ReadAllText(stateFilepath));
|
|
|
|
|
|
|
2021-02-03 20:51:36 +01:00
|
|
|
|
string fileMapFilepath = Path.Combine(I.current.tempFolder, Paths.resumeDir, filenameMapFilename);
|
|
|
|
|
|
List<string> inputFrameLines = File.ReadAllLines(fileMapFilepath).Where(l => l.Trim().Length > 3).ToList();
|
|
|
|
|
|
List<string> inputFrames = inputFrameLines.Select(l => Path.Combine(I.current.framesFolder, l.Split('|')[1])).ToList();
|
2021-02-02 21:48:18 +01:00
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < state.interpolatedInputFrames; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
IOUtils.TryDeleteIfExists(inputFrames[i]);
|
2021-02-03 20:51:36 +01:00
|
|
|
|
if (i % 1000 == 0) await Task.Delay(1);
|
2021-02-02 21:48:18 +01:00
|
|
|
|
}
|
2021-02-03 20:51:36 +01:00
|
|
|
|
|
2021-02-03 21:10:37 +01:00
|
|
|
|
Directory.Move(I.current.interpFolder, I.current.interpFolder + Paths.prevSuffix); // Move existing interp frames
|
|
|
|
|
|
Directory.CreateDirectory(I.current.interpFolder); // Re-create empty interp folder
|
|
|
|
|
|
|
2021-02-03 20:51:36 +01:00
|
|
|
|
LoadFilenameMap();
|
2021-02-02 21:48:18 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-01-30 01:37:36 +01:00
|
|
|
|
static void LoadFilenameMap()
|
|
|
|
|
|
{
|
|
|
|
|
|
Dictionary<string, string> dict = new Dictionary<string, string>();
|
2021-02-03 20:51:36 +01:00
|
|
|
|
string filePath = Path.Combine(I.current.tempFolder, Paths.resumeDir, filenameMapFilename);
|
2021-01-30 01:37:36 +01:00
|
|
|
|
string[] dictLines = File.ReadAllLines(filePath);
|
|
|
|
|
|
|
|
|
|
|
|
foreach (string line in dictLines)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (line.Length < 5) continue;
|
|
|
|
|
|
string[] keyValuePair = line.Split('|');
|
|
|
|
|
|
dict.Add(keyValuePair[0].Trim(), keyValuePair[1].Trim());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
AiProcess.filenameMap = dict;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|