This repository has been archived by the owner on Jan 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathCustomWorldData.cs
173 lines (162 loc) · 4.69 KB
/
CustomWorldData.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Terraria;
namespace Tremor
{
public static class CustomWorldData
{
public const char SpaceChar = '~';
public static Dictionary<string, float> Number = new Dictionary<string, float>();
public static Dictionary<string, bool> Switch = new Dictionary<string, bool>();
public static Dictionary<string, string> String = new Dictionary<string, string>();
public static string WorldName => Main.worldPathName.Split('/', '\\')[Main.worldPathName.Split('/', '\\').Length - 1].Remove(Main.worldPathName.Split('/', '\\')[Main.worldPathName.Split('/', '\\').Length - 1].Length - 4);
public static string LastLoadedWorld = "";
public static void SetNumber(string Key, float Value)
{
if (Number.Count < 1 || LastLoadedWorld != WorldName)
Load(true);
if (Number.ContainsKey(Key))
Number[Key] = Value;
else
Number.Add(Key, Value);
Save(true);
}
public static void SetSwitch(string Key, bool Value)
{
if (Switch.Count < 1 || LastLoadedWorld != WorldName)
Load(false, true);
if (Switch.ContainsKey(Key))
Switch[Key] = Value;
else
Switch.Add(Key, Value);
Save(false, true);
}
public static void SetString(string Key, string Value)
{
if (String.Count < 1 || LastLoadedWorld != WorldName)
Load(false, false, true);
if (String.ContainsKey(Key))
String[Key] = Value;
else
String.Add(Key, Value);
Save(false, false, true);
}
public static float GetNumber(string Key)
{
if (Number.Count < 1 || LastLoadedWorld != WorldName)
Load(true);
if (Number.ContainsKey(Key))
return Number[Key];
return 0.0f;
}
public static bool GetSwitch(string Key)
{
if (Switch.Count < 1 || LastLoadedWorld != WorldName)
Load(false, true);
if (Switch.ContainsKey(Key))
return Switch[Key];
return false;
}
public static string GetString(string Key)
{
if (String.Count < 1 || LastLoadedWorld != WorldName)
Load(false, false, true);
if (String.ContainsKey(Key))
return String[Key];
return "";
}
public static void Save(bool Nu, bool Sw = false, bool St = false)
{
if (Number.Count > 0 && Nu)
{
string FileName = Main.WorldPath + "\\" + WorldName + ".tnu";
FileStream FS = new FileStream(FileName, FileMode.Create);
StreamWriter SW = new StreamWriter(FS, Encoding.Default);
foreach (string Key in Number.Keys)
SW.WriteLine(Key + SpaceChar + Number[Key]);
SW.Close();
FS.Close();
}
if (Switch.Count > 0 && Sw)
{
string FileName = Main.WorldPath + "\\" + WorldName + ".tsw";
FileStream FS = new FileStream(FileName, FileMode.Create);
StreamWriter SW = new StreamWriter(FS, Encoding.Default);
foreach (string Key in Switch.Keys)
SW.WriteLine(Key + SpaceChar + Switch[Key]);
SW.Close();
FS.Close();
}
if (String.Count > 0 && St)
{
string FileName = Main.WorldPath + "\\" + WorldName + ".tst";
FileStream FS = new FileStream(FileName, FileMode.Create);
StreamWriter SW = new StreamWriter(FS, Encoding.Default);
foreach (string Key in String.Keys)
SW.WriteLine(Key + SpaceChar + String[Key]);
SW.Close();
FS.Close();
}
}
public static void Load(bool Nu, bool Sw = false, bool St = false)
{
LastLoadedWorld = Main.worldPathName;
Number = new Dictionary<string, float>();
Switch = new Dictionary<string, bool>();
String = new Dictionary<string, string>();
if (Nu)
{
string FileName = Main.WorldPath + "\\" + WorldName + ".tnu";
FileStream FS = new FileStream(FileName, FileMode.OpenOrCreate);
StreamReader SR = new StreamReader(FS, Encoding.Default);
while (!SR.EndOfStream)
{
try
{
string[] Part = SR.ReadLine().Split(SpaceChar);
Number.Add(Part[0], Convert.ToSingle(Part[1]));
}
catch { }
}
SR.Close();
FS.Close();
}
if (Sw)
{
string FileName = Main.WorldPath + "\\" + WorldName + ".tsw";
FileStream FS = new FileStream(FileName, FileMode.OpenOrCreate);
StreamReader SR = new StreamReader(FS, Encoding.Default);
while (!SR.EndOfStream)
{
try
{
string[] Part = SR.ReadLine().Split(SpaceChar);
Switch.Add(Part[0], Convert.ToBoolean(Part[1]));
}
catch { }
}
SR.Close();
FS.Close();
}
if (St)
{
string FileName = Main.WorldPath + "\\" + WorldName + ".tst";
FileStream FS = new FileStream(FileName, FileMode.OpenOrCreate);
StreamReader SR = new StreamReader(FS, Encoding.Default);
while (!SR.EndOfStream)
{
try
{
string[] Part = SR.ReadLine().Split(SpaceChar);
String.Add(Part[0], Part[1]);
}
catch { }
}
SR.Close();
FS.Close();
}
}
}
}