-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
998 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
bin/ | ||
obj/ | ||
/packages/ | ||
riderModule.iml | ||
/_ReSharper.Caches/ | ||
.idea/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
13
ValheimWorldBackupUtility/ValheimWorldBackupUtility.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |