forked from gardenappl/BossExpertise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLegacyConfigV1.cs
114 lines (96 loc) · 3.07 KB
/
LegacyConfigV1.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 Newtonsoft.Json;
using System;
using System.IO;
using Terraria;
using Terraria.ModLoader.Config;
using Terraria.ModLoader;
namespace BossExpertise
{
public static class LegacyConfigV1
{
static string ConfigFolderPath = Path.Combine(Main.SavePath, "Mod Configs", "BossExpertise");
static string ConfigPath = Path.Combine(ConfigFolderPath, "config.txt");
static string ConfigVersionPath = Path.Combine(ConfigFolderPath, "configVersion.txt");
static bool DropBags = false;
static bool ChangeBossAI = true;
static bool AddCheatSheetButton = true;
static bool AddExpertCommand = false;
public static void Load()
{
if (!Directory.Exists(ConfigFolderPath))
return;
ModContent.GetInstance<BossExpertise>().Logger.Warn("Found config file in old format! Reading config.txt...");
bool success = ReadConfig();
if (success)
{
ModContent.GetInstance<BossExpertise>().Logger.Warn("Saving outdated config as JSON...");
SaveAsJson();
}
ModContent.GetInstance<BossExpertise>().Logger.Warn("Deleting outdated config...");
try
{
File.Delete(ConfigPath);
File.Delete(ConfigVersionPath);
if (Directory.GetFiles(ConfigFolderPath).Length == 0 && Directory.GetDirectories(ConfigFolderPath).Length == 0)
{
Directory.Delete(ConfigFolderPath);
}
else
{
ModContent.GetInstance<BossExpertise>().Logger.Warn("Outdated config folder still cotains some files/directories. They will not get deleted.");
}
}
catch (Exception e)
{
ModContent.GetInstance<BossExpertise>().Logger.Error("Unable to delete old config!", e);
}
}
static bool ReadConfig()
{
bool success = false;
if (File.Exists(ConfigPath))
{
ModContent.GetInstance<BossExpertise>().Logger.Warn("Found config file with old format! Reading outdated config...");
using (var file = new StreamReader(ConfigPath))
{
try
{
file.ReadLine();
bool.TryParse(file.ReadLine().Split(':')[1], out DropBags);
file.ReadLine();
bool.TryParse(file.ReadLine().Split(':')[1], out ChangeBossAI);
file.ReadLine();
bool.TryParse(file.ReadLine().Split(':')[1], out AddCheatSheetButton);
file.ReadLine();
bool.TryParse(file.ReadLine().Split(':')[1], out AddExpertCommand);
success = true;
}
catch (Exception e)
{
ModContent.GetInstance<BossExpertise>().Logger.Error("Unable to read old config file!", e);
}
}
}
return success;
}
static void SaveAsJson()
{
var configObject = new
{
DropTreasureBagsInNormal = DropBags,
AddCheatSheetButton = AddCheatSheetButton,
AddExpertCommand = AddExpertCommand,
ChangeBossAI = ChangeBossAI
};
string json = JsonConvert.SerializeObject(configObject, ConfigManager.serializerSettings);
File.WriteAllText(Path.Combine(ConfigManager.ModConfigPath, "BossExpertise_Config.json"), json);
}
public static void Unload()
{
ConfigPath = null;
ConfigFolderPath = null;
ConfigVersionPath = null;
}
}
}