-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathModConfig.cs
115 lines (95 loc) · 3.96 KB
/
ModConfig.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using log4net;
using System.IO;
using Terraria;
using Terraria.IO;
using Terraria.ModLoader.Config;
using System.ComponentModel;
namespace WMITF
{
public class Config : ModConfig
{
public override ConfigScope Mode => ConfigScope.ClientSide;
[DefaultValue(false)]
public bool DisplayWorldTooltips;
[DefaultValue(true)]
public bool DisplayItemTooltips;
[DefaultValue(false)]
public bool DisplayTechnicalNames;
}
public class LegacyConfig
{
static bool DisplayWorldTooltips = false;
const string DisplayWorldTooltipsKey = "DisplayWorldTooltips";
static bool DisplayItemTooltips = true;
const string DisplayItemTooltipsKey = "DisplayItemTooltips";
static bool DisplayTechnicalNames = false;
const string DisplayTechnicalNamesKey = "DisplayTechnicalNames";
static string ConfigPath = Path.Combine(Main.SavePath, "Mod Configs", "WMITF.json");
static string OldConfigFolderPath = Path.Combine(Main.SavePath, "Mod Configs", "WMITF");
static string OldConfigPath = Path.Combine(OldConfigFolderPath, "config.json");
static string OldConfigVersionPath = Path.Combine(OldConfigFolderPath, "config.version");
static readonly Preferences Settings = new Preferences(ConfigPath);
//Make our own logger because it's too early in the mod load process to use VanillaTweaks.Instance
static ILog Logger = LogManager.GetLogger("WMITFConfig");
public static void Load()
{
if (Directory.Exists(OldConfigFolderPath))
{
if (File.Exists(OldConfigPath))
{
if (File.Exists(ConfigPath))
{
Logger.Warn("Found config file in old folder! Deleting...");
File.Delete(OldConfigPath);
}
else
{
Logger.Warn("Found config file in old folder! Moving config...");
File.Move(OldConfigPath, ConfigPath);
}
}
if (File.Exists(OldConfigVersionPath))
File.Delete(OldConfigVersionPath);
if (Directory.GetFiles(OldConfigFolderPath).Length == 0 && Directory.GetDirectories(OldConfigFolderPath).Length == 0)
Directory.Delete(OldConfigFolderPath);
else
Logger.Warn("Old config folder still cotains some files/directories. They will not get deleted.");
}
if (!File.Exists(ConfigPath))
return;
if (!ReadConfig())
{
Logger.Warn("Failed to read legacy config file! Deleting...");
File.Delete(ConfigPath);
return;
}
MoveToNewFormat();
}
public static bool ReadConfig()
{
if (Settings.Load())
{
Settings.Get(DisplayWorldTooltipsKey, ref DisplayWorldTooltips);
Settings.Get(DisplayItemTooltipsKey, ref DisplayItemTooltips);
Settings.Get(DisplayTechnicalNamesKey, ref DisplayTechnicalNames);
return true;
}
return false;
}
static void MoveToNewFormat()
{
Logger.Warn("Migrating to new format...");
var newConfigPath = Path.Combine(ConfigManager.ModConfigPath,
nameof(WMITF) + "_Config" + ".json");
var newConfig = new Preferences(newConfigPath);
newConfig.Put(DisplayWorldTooltipsKey, DisplayWorldTooltips);
newConfig.Put(DisplayItemTooltipsKey, DisplayItemTooltips);
newConfig.Put(DisplayTechnicalNamesKey, DisplayTechnicalNames);
newConfig.Save();
if (!File.Exists(newConfigPath))
File.Move(ConfigPath, newConfigPath);
else
File.Delete(ConfigPath);
}
}
}