71 lines
2.3 KiB
C#
71 lines
2.3 KiB
C#
using System.IO;
|
|
|
|
namespace InstaFollowersOverseer;
|
|
|
|
public abstract class DtsodFile
|
|
{
|
|
public bool LoadedSuccessfully = false;
|
|
|
|
public readonly string FileNameWithoutExt;
|
|
public readonly string FileName;
|
|
public readonly string FileExampleName;
|
|
|
|
public DtsodFile(string fileNameWithoutExt)
|
|
{
|
|
FileNameWithoutExt = fileNameWithoutExt;
|
|
FileName = fileNameWithoutExt + ".dtsod";
|
|
FileExampleName = fileNameWithoutExt + "-example.dtsod";
|
|
}
|
|
|
|
public void CreateBackup()
|
|
{
|
|
string backupPath=$"backups/{FileNameWithoutExt}.d/{FileNameWithoutExt}"
|
|
+DateTime.Now.ToString(MyTimeFormat.ForFileNames)+".dtsod";
|
|
Program.MainLogger.LogInfo($"creating backup if file {FileName} at path {backupPath}");
|
|
File.Copy(FileName,backupPath,false);
|
|
}
|
|
|
|
public DtsodV23 ReadDtsodFromFile(bool trhowIfFileNotFound)
|
|
{
|
|
Program.MainLogger.LogInfo($"reading file {FileName}");
|
|
EmbeddedResources.CopyToFile(
|
|
$"{EmbeddedResourcesPrefix}.{FileExampleName}",
|
|
FileExampleName);
|
|
|
|
if (!File.Exists(FileName))
|
|
{
|
|
File.WriteAllText(FileName, "#DtsodV23\n");
|
|
string message = $"file {FileName} doesnt exist, created new blank";
|
|
if (trhowIfFileNotFound)
|
|
throw new FileNotFoundException(message);
|
|
Program.MainLogger.LogWarn(message);
|
|
return new DtsodV23();
|
|
}
|
|
|
|
string fileText = File.ReadAllText(FileName);
|
|
Program.MainLogger.LogDebug(fileText);
|
|
// should be set to false on LoadFromFile() errors
|
|
LoadedSuccessfully = true;
|
|
return new DtsodV23(fileText);
|
|
}
|
|
|
|
public abstract void LoadFromFile();
|
|
|
|
public abstract DtsodV23 ToDtsod();
|
|
|
|
public void SaveToFile()
|
|
{
|
|
if(!LoadedSuccessfully)
|
|
return;
|
|
|
|
if(File.Exists(FileName))
|
|
CreateBackup();
|
|
Program.MainLogger.LogInfo($"saving file {FileName}");
|
|
string dtsodStr = ToDtsod().ToString();
|
|
Program.MainLogger.LogDebug(dtsodStr);
|
|
File.OpenWrite(FileName)
|
|
.FluentWriteString("#DtsodV23\n")
|
|
.FluentWriteString(dtsodStr)
|
|
.Close();
|
|
}
|
|
} |