-
Notifications
You must be signed in to change notification settings - Fork 2
/
VortexWorlds.cs
93 lines (84 loc) · 3.8 KB
/
VortexWorlds.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using UnityEngine;
using Debug = UnityEngine.Debug;
namespace VortexWorlds
{
public static class ModInfo
{
public const string Name = "Vortex Worlds";
public const string Version = "1.0.0";
}
[BepInPlugin("vortex-worlds", ModInfo.Name, ModInfo.Version)]
public class VortexWorlds : BaseUnityPlugin
{
private const string VortexWorldsDir = "vortex-worlds";
private static void Log(string msg) => Debug.Log($"[vortex-worlds] {msg}");
private static void Error(string msg) => Debug.LogError($"[vortex-worlds] {msg}");
private void Awake()
{
// Sync files from the vortex-worlds folder to the worlds folder
var vortexWorldsDir = Path.Combine(Utils.GetSaveDataPath(), VortexWorldsDir);
var valheimWorldsDir = Path.Combine(Utils.GetSaveDataPath(), "worlds");
if (!Directory.Exists(vortexWorldsDir))
{
// Definitely nothing for us to do if the directory doesn't exist...
return;
}
// Make sure the Valheim worlds directory is created, always
if (!Directory.Exists(valheimWorldsDir))
{
Directory.CreateDirectory(valheimWorldsDir);
}
IEnumerable<string> GetWorldsFromDir(string dir) => Directory
.GetFiles(dir, "*.fwl")
.Select(p => Path.GetFileNameWithoutExtension(p));
// Determine if there are any new worlds that need deploying
var valheimWorlds = GetWorldsFromDir(valheimWorldsDir);
var vortexWorlds = GetWorldsFromDir(vortexWorldsDir);
var worldsToDeploy = vortexWorlds.Except(valheimWorlds, StringComparer.InvariantCultureIgnoreCase).ToList();
if (worldsToDeploy.Count > 0)
{
Log($"Found {worldsToDeploy.Count} new worlds to deploy");
foreach (var world in worldsToDeploy)
{
// Generate a manifest of files to copy
var sourceFiles = Directory.GetFiles(vortexWorldsDir, $"{world}.*");
var sourceAndTarget = sourceFiles
.Select(path => new
{src = path, tgt = Path.Combine(valheimWorldsDir, Path.GetFileName(path))})
.ToList();
// If any of the source files already exist then we don't copy anything ...
if (sourceAndTarget.Any(st => File.Exists(st.tgt)))
{
Error($"Deploying {world} failed as one of more files already existed in the target directory ...");
}
else
{
try
{
foreach (var st in sourceAndTarget)
{
File.Copy(st.src, st.tgt, overwrite: false);
}
Log($"Deploying {world} completed");
}
catch (Exception ex)
{
// We think we checked all conditions above, but its always possible something changed in the
// meantime.
Error($"Deploying {world} failed due to an exception occurring: {ex.Message}");
// We will NOT try and "clean up" here, because we don't know what happened, and might make things
// worse.
}
}
}
}
}
}
}