-
Notifications
You must be signed in to change notification settings - Fork 3
/
LegacyConfig.cs
106 lines (93 loc) · 2.71 KB
/
LegacyConfig.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
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using Terraria;
using Terraria.ID;
using Terraria.ModLoader.Config;
using Terraria.ModLoader;
namespace OmniSwing
{
static class LegacyConfig
{
static List<ItemDefinition> BlacklistedItemIDs = new List<ItemDefinition>();
static List<ItemDefinition> WhitelistedItemIDs = new List<ItemDefinition>();
static string ConfigFolderPath = Path.Combine(Main.SavePath, "Mod Configs");
static string ConfigPath = Path.Combine(ConfigFolderPath, "OmniSwing.txt");
public static void Load()
{
if(File.Exists(ConfigPath))
{
ModContent.GetInstance<OmniSwing>().Logger.Warn("Found legacy config file. Reading...");
ReadLegacyConfig();
ModContent.GetInstance<OmniSwing>().Logger.Warn("Migrating to new version");
MigrateToNewFormat();
ModContent.GetInstance<OmniSwing>().Logger.Warn("Migrated, deleting old config file.");
File.Delete(ConfigPath);
}
}
static void ReadLegacyConfig()
{
int lineNumber = 0;
using(var reader = new StreamReader(ConfigPath))
{
while(!reader.EndOfStream)
{
lineNumber++;
string line = reader.ReadLine().Split('#')[0].Trim();
if(line.Length == 0)
continue;
if(line[0] == '!')
{
line = line.Remove(0, 1).Trim();
ParseConfigLine(line, lineNumber, WhitelistedItemIDs);
}
else
{
ParseConfigLine(line, lineNumber, BlacklistedItemIDs);
}
}
}
}
static void ParseConfigLine(string line, int lineNumber, List<ItemDefinition> idList)
{
int id = 0;
if(Int32.TryParse(line, out id))
{
if(id <= 0 || id >= ItemID.Count)
throw new Exception(id + " is not a valid Vanilla Item ID!");
idList.Add(new ItemDefinition(id));
}
else
{
string[] splitLine = line.Split(':');
if(splitLine.Length < 2)
throw new Exception("Wrong OmniSwing.txt config format at line " + lineNumber);
string modName = splitLine[0];
string itemName = splitLine[1];
idList.Add(new ItemDefinition(modName, itemName));
}
}
static void MigrateToNewFormat()
{
string newConfigPath = Path.Combine(ConfigManager.ModConfigPath,
nameof(OmniSwing) + '_' + nameof(Config) + ".json");
var newConfig = new
{
Whitelist = WhitelistedItemIDs,
Blacklist = BlacklistedItemIDs
};
string json = JsonConvert.SerializeObject(newConfig, ConfigManager.serializerSettings);
File.WriteAllText(newConfigPath, json);
}
public static void Unload()
{
BlacklistedItemIDs.Clear();
BlacklistedItemIDs = null;
WhitelistedItemIDs.Clear();
WhitelistedItemIDs = null;
ConfigFolderPath = null;
ConfigPath = null;
}
}
}