-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathConfig.cs
116 lines (105 loc) · 4.74 KB
/
Config.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
115
116
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
namespace MapleShark
{
public sealed class Config
{
public string Interface = "";
public ushort LowPort = 8585;
public ushort HighPort = 8586;
public List<Definition> Definitions = new List<Definition>();
private static Config sInstance = null;
internal static Config Instance
{
get
{
if (sInstance == null)
{
if (!File.Exists("Config.xml"))
{
sInstance = new Config();
sInstance.Save();
}
else
{
using (XmlReader xr = XmlReader.Create("Config.xml"))
{
XmlSerializer xs = new XmlSerializer(typeof(Config));
sInstance = xs.Deserialize(xr) as Config;
}
}
}
return sInstance;
}
}
internal Definition GetDefinition(ushort pBuild, ushort pLocale, bool pOutbound, ushort pOpcode) {
return Definitions.Find(d => d.Locale == pLocale && d.Build == pBuild && d.Outbound == pOutbound && d.Opcode == pOpcode);
}
internal void SaveProperties()
{
Dictionary<ushort, Dictionary<ushort, SortedDictionary<ushort, string>>>[] headerList = new Dictionary<ushort, Dictionary<ushort, SortedDictionary<ushort, string>>>[2];
for (int i = 0; i < 2; i++)
{
headerList[i] = new Dictionary<ushort, Dictionary<ushort, SortedDictionary<ushort, string>>>();
}
foreach (var d in Definitions)
{
if (d.Opcode == 0xFFFF) return;
byte outbound = (byte)(d.Outbound ? 1 : 0);
if (!headerList[outbound].ContainsKey(d.Locale))
headerList[outbound].Add(d.Locale, new Dictionary<ushort, SortedDictionary<ushort, string>>());
if (!headerList[outbound][d.Locale].ContainsKey(d.Build))
headerList[outbound][d.Locale].Add(d.Build, new SortedDictionary<ushort, string>());
if (!headerList[outbound][d.Locale][d.Build].ContainsKey(d.Opcode))
headerList[outbound][d.Locale][d.Build].Add(d.Opcode, d.Name);
else
headerList[outbound][d.Locale][d.Build][d.Opcode] = d.Name;
};
for (int i = 0; i < 2; i++)
{
foreach (var dict in headerList[i])
{
string map = "Scripts" + Path.DirectorySeparatorChar + dict.Key.ToString();
if (!Directory.Exists(map))
Directory.CreateDirectory(map);
foreach (KeyValuePair<ushort, SortedDictionary<ushort, string>> kvp in dict.Value)
{
string map2 = map + Path.DirectorySeparatorChar + kvp.Key;
if (!Directory.Exists(map2))
Directory.CreateDirectory(map2);
string buff = "";
buff += "# Generated by MapleShark\r\n";
foreach (KeyValuePair<ushort, string> kvp2 in kvp.Value)
{
buff += string.Format("{0} = 0x{1:X4}\r\n", kvp2.Value == "" ? "# NOT SET: " : kvp2.Value.Replace(' ', '_'), kvp2.Key);
}
File.WriteAllText(map2 + Path.DirectorySeparatorChar + (i == 0 ? "send" : "recv") + ".properties", buff);
}
}
}
}
internal static string GetPropertiesFile(bool pOutbound, byte pLocale, ushort pVersion)
{
return System.Environment.CurrentDirectory + Path.DirectorySeparatorChar + "Scripts" + Path.DirectorySeparatorChar + pLocale.ToString() + Path.DirectorySeparatorChar + pVersion.ToString() + Path.DirectorySeparatorChar + (pOutbound ? "send" : "recv") + ".properties";
}
internal void Save()
{
XmlWriterSettings xws = new XmlWriterSettings()
{
Indent = true,
IndentChars = " ",
NewLineOnAttributes = true,
OmitXmlDeclaration = true
};
using (XmlWriter xw = XmlWriter.Create("Config.xml", xws))
{
XmlSerializer xs = new XmlSerializer(typeof(Config));
xs.Serialize(xw, this);
}
if (!Directory.Exists("Scripts")) Directory.CreateDirectory("Scripts");
SaveProperties();
}
}
}