Skip to content

Commit

Permalink
first version;
Browse files Browse the repository at this point in the history
  • Loading branch information
f1am3d committed May 29, 2021
1 parent b62a4cf commit 9c4af68
Show file tree
Hide file tree
Showing 8 changed files with 998 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
bin/
obj/
/packages/
riderModule.iml
/_ReSharper.Caches/
.idea/
674 changes: 674 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions ValheimWorldBackupUtility.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ValheimWorldBackupUtility", "ValheimWorldBackupUtility\ValheimWorldBackupUtility.csproj", "{F470849F-300C-413B-9FDE-2C4C9F0066C8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{F470849F-300C-413B-9FDE-2C4C9F0066C8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F470849F-300C-413B-9FDE-2C4C9F0066C8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F470849F-300C-413B-9FDE-2C4C9F0066C8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F470849F-300C-413B-9FDE-2C4C9F0066C8}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
152 changes: 152 additions & 0 deletions ValheimWorldBackupUtility/BackupController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Timers;

namespace ValheimWorldBackupUtility
{
internal enum FileExt
{
fwl,
db
}

public class BackupController
{
private Timer _timer;
private string _worldsRootPath;
private string _backupPath;

private string _worldPath
{
get
{
if (Program.options.worldName != null)
{
return Path.Combine(_worldsRootPath, $"{Program.options.worldName}.fwl");
}

return null;
}
}

public BackupController()
{
initializePaths();
}

public void initializePaths()
{
// Server worlds path
_worldsRootPath = Path.GetFullPath(
Path.Combine(
Environment.ExpandEnvironmentVariables("%AppData%"),
@"..\LocalLow\IronGate\Valheim\worlds"
)
);

if (Directory.Exists(_worldsRootPath) == false)
{
throw new DirectoryNotFoundException(
$"Server worlds directory not found in {_worldsRootPath}"
);
}

_backupPath = Path.Combine(_worldsRootPath, "backups");
}

public void start()
{
int interval = Program.options.interval ?? 1;

if (Program.options.worldName != null && File.Exists(_worldPath) == false)
{
throw new FileNotFoundException($"World with name {Program.options.worldName} not found");
}

Console.WriteLine("Creating initial backup...");

createBackup();

Console.WriteLine($"Waiting...");

// ms * s * m * h
_timer = new Timer(1000 * 60 * 60 * interval);
_timer.Elapsed += createBackup;
_timer.AutoReset = true;
_timer.Enabled = true;
}

private void createBackup(object source = null, ElapsedEventArgs e = null)
{
string[] files = Directory.GetFiles(_worldsRootPath);
DateTime date = e?.SignalTime ?? DateTime.Now;
string folderName = $"backup-{string.Format("{0:dd-MM-yyyy_HH-mm}", date)}";
string folderPath = Path.Combine(_backupPath, folderName);


// Create new dir
if (Directory.Exists(folderPath) == false)
{
Directory.CreateDirectory(folderPath);
}


foreach (string file in files)
{
if (!Program.options.backupEverything)
{
if (Regex.Match(file, Program.options.worldName).Success)
{
backupFile(file, folderName, folderPath);
}

continue;
}

backupFile(file, folderName, folderPath);
}

Console.WriteLine($"Successfully created backup {folderName}");
}

public void backupFile(string filePath, string folderName, string folderPath)
{
FileExt? type = null;
string fileName;

// Detect file extension
if (Regex.Match(filePath, @"\.fwl$").Success)
{
type = FileExt.fwl;
}

if (Regex.Match(filePath, @"\.db$").Success)
{
type = FileExt.db;
}

fileName = Regex.Match(filePath, @"\\([\d\w\.]+)$").Groups[1]?.Value;

if (type == null || fileName == null) return;

string newFilePath = Path.Combine(folderPath, fileName);


// Copy file
try
{
File.Copy(filePath, newFilePath);
}
catch
{
throw new FileNotFoundException($"Unable to copy file {filePath}");
}
}

public void stop()
{
_timer.Dispose();
}
}
}
56 changes: 56 additions & 0 deletions ValheimWorldBackupUtility/ConsoleController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;

namespace ValheimWorldBackupUtility
{
public class ConsoleController
{
public void sayHello()
{
Console.WriteLine($"Valheim World Backup Utility v.{Program.VERSION}");
}

public void readOptions()
{
readWorldName();
readInterval();
}

public void readWorldName()
{
Console.WriteLine("World name (empty to backup all):");

string input = Console.ReadLine();

Program.options.setWorldName(input);
}

public void readInterval()
{
Console.WriteLine("Interval in hours (1h - default):");

try
{
string input = Console.ReadLine();
int interval = int.Parse(input ?? "1");

bool result = Program.options.setInterval(interval);

if(!result) throw new Exception();
}
catch
{
Console.WriteLine("Invalid interval");
readInterval();
}
}

public void readExitConfirmation()
{
Console.WriteLine("Write \"QUIT\" to stop the app.");

string input = Console.ReadLine();

if (input.ToLower() != "quit") readExitConfirmation();
}
}
}
33 changes: 33 additions & 0 deletions ValheimWorldBackupUtility/OptionsController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
namespace ValheimWorldBackupUtility
{
public class OptionsController
{
public string ?worldName;
public int ?interval;
public bool backupEverything
{
get { return Program.options.worldName == null; }
}

public void setWorldName(string input)
{
if (input.Length < 1)
{
return;
}

worldName = input;
}

public bool setInterval(int input)
{
if (input < 1)
{
return false;
}

interval = input;
return true;
}
}
}
48 changes: 48 additions & 0 deletions ValheimWorldBackupUtility/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using System.IO;

namespace ValheimWorldBackupUtility
{

abstract class Program
{
public static readonly string VERSION = "0.9.0";
public static readonly int APP_ID = 892970;
public static BackupController backup = new BackupController();
public static OptionsController options = new OptionsController();
public static ConsoleController console = new ConsoleController();


static void Main(string[] args)
{
console.sayHello();
console.readOptions();

try
{
backup.start();
}
catch (FileNotFoundException error)
{
Console.WriteLine(error.Message);
}
catch (DirectoryNotFoundException error)
{
Console.WriteLine(error.Message);
}
catch
{
Console.WriteLine("Unknown error.");
exit();
}

console.readExitConfirmation();
}

public static void exit()
{
backup.stop();
Environment.Exit(0);
}
}
}
13 changes: 13 additions & 0 deletions ValheimWorldBackupUtility/ValheimWorldBackupUtility.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net45</TargetFramework>
<LangVersion>9</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Gameloop.Vdf" Version="0.6.1" />
</ItemGroup>

</Project>

0 comments on commit 9c4af68

Please sign in to comment.