forked from bdfzchen2015/ServerSideCharacter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayerData.cs
181 lines (170 loc) · 5.4 KB
/
PlayerData.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
174
175
176
177
178
179
180
181
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using ServerSideCharacter.Items;
using Terraria;
using Terraria.ModLoader;
namespace ServerSideCharacter
{
public class PlayerData
{
public Dictionary<string, ServerPlayer> Data = new Dictionary<string, ServerPlayer>();
public PlayerData()
{
}
private static string ReadNext(XmlNodeList info, ref int i)
{
var xmlNode = info.Item(i++);
if (xmlNode != null) return xmlNode.InnerText;
throw new NullReferenceException("XMLNode is null");
}
private void TryReadItemInfo(Dictionary<string, Mod> modTable, XmlNodeList info,
ServerPlayer player, ref int i, int id, ref Item[] slots)
{
int type;
string text = ReadNext(info, ref i);
//如果是mod物品
if (text[0] == '$')
{
text = text.Substring(1);
string modName = text.Substring(0, text.IndexOf('.'));
string itemName = text.Substring(text.LastIndexOf('.') + 1);
//解析物品id,字典中有mod名字
if (modTable.ContainsKey(modName))
{
modTable[modName].TryFind("TestItem", out ModItem it);
type = it.Type;
//如果数据合法
if (type > 0)
{
slots[id].netDefaults(type);
foreach (var pair in ModDataHooks.ItemExtraInfoTable)
{
ModDataHooks.InterpretStringTable[pair.Key].Invoke(
((info.Item(i - 1) as XmlElement).GetAttribute(pair.Key)),
slots[id]);
}
}
else
{
ServerSideCharacter.Instance.TryFind("TestItem", out ModItem item);
slots[id].netDefaults(item.Type);
((TestItem)slots[id].ModItem).SetUp(text);
//物品数据会丢失
}
}
else
{
ServerSideCharacter.Instance.TryFind("TestItem", out ModItem item);
slots[id].netDefaults(item.Type);
((TestItem)slots[id].ModItem).SetUp(text);
//物品数据会丢失
}
}
else
{
type = Convert.ToInt32(text);
if (type != 0)
{
slots[id].netDefaults(type);
}
foreach (var pair in ModDataHooks.ItemExtraInfoTable)
{
ModDataHooks.InterpretStringTable[pair.Key].Invoke(
((info.Item(i - 1) as XmlElement).GetAttribute(pair.Key)),
slots[id]);
}
}
}
public PlayerData(string path)
{
ServerPlayer.ResetUuid();
if (File.Exists(path))
{
XmlReaderSettings settings = new XmlReaderSettings { IgnoreComments = true };
//忽略文档里面的注释
XmlDocument xmlDoc = new XmlDocument();
XmlReader reader = XmlReader.Create(path, settings);
xmlDoc.Load(reader);
XmlNode xn = xmlDoc.SelectSingleNode("Players");
var list = xn.ChildNodes;
Dictionary<string, Mod> modTable = new Dictionary<string, Mod>();
foreach (var mod in ModLoader.Mods)
modTable.Add(mod.Name, mod);
foreach (var node in list)
{
XmlElement pData = (XmlElement)node;
ServerPlayer player = new ServerPlayer();
var info = pData.ChildNodes;
int i = 0;
player.Name = pData.GetAttribute("name");
player.Uuid = int.Parse(pData.GetAttribute("uuid"));
try
{
player.PermissionGroup = ServerSideCharacter.GroupManager.Groups[pData.GetAttribute("group")];
}
catch
{
player.PermissionGroup = ServerSideCharacter.GroupManager.Groups["default"];
}
foreach (var pair in ModDataHooks.InterpretPlayerStringTable)
{
pair.Value(pData.GetAttribute(pair.Key), player);
}
player.HasPassword = Convert.ToBoolean(ReadNext(info, ref i));
player.Password = ReadNext(info, ref i);
player.LifeMax = Convert.ToInt32(ReadNext(info, ref i));
player.StatLife = Convert.ToInt32(ReadNext(info, ref i));
player.ManaMax = Convert.ToInt32(ReadNext(info, ref i));
player.StatMana = Convert.ToInt32(ReadNext(info, ref i));
for (int id = 0; id < player.Inventory.Length; id++)
{
TryReadItemInfo(modTable, info, player, ref i, id, ref player.Inventory);
//player.inventory[id].Prefix(Convert.ToByte((info.Item(i - 1) as XmlElement).GetAttribute("prefix")));
//player.inventory[id].stack =
// Convert.ToInt32((info.Item(i - 1) as XmlElement).GetAttribute("stack"));
}
for (int id = 0; id < player.Armor.Length; id++)
{
TryReadItemInfo(modTable, info, player, ref i, id, ref player.Armor);
}
for (int id = 0; id < player.Dye.Length; id++)
{
TryReadItemInfo(modTable, info, player, ref i, id, ref player.Dye);
}
for (int id = 0; id < player.MiscEquips.Length; id++)
{
TryReadItemInfo(modTable, info, player, ref i, id, ref player.MiscEquips);
}
for (int id = 0; id < player.MiscDye.Length; id++)
{
TryReadItemInfo(modTable, info, player, ref i, id, ref player.MiscDye);
}
for (int id = 0; id < player.Bank.item.Length; id++)
{
TryReadItemInfo(modTable, info, player, ref i, id, ref player.Bank.item);
}
for (int id = 0; id < player.Bank2.item.Length; id++)
{
TryReadItemInfo(modTable, info, player, ref i, id, ref player.Bank2.item);
}
for (int id = 0; id < player.Bank3.item.Length; id++)
{
TryReadItemInfo(modTable, info, player, ref i, id, ref player.Bank3.item);
}
Data.Add(player.Name, player);
ServerPlayer.IncreaseUuid();
}
ServerSideCharacter.MainWriter = new XmlWriter(path);
reader.Close();
}
else
{
XmlWriter writer = new XmlWriter(path);
writer.Create();
ServerSideCharacter.MainWriter = writer;
}
}
}
}