-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathContentManager.cs
181 lines (169 loc) · 4.99 KB
/
ContentManager.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 Engine;
using Engine.Content;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Xml.Linq;
using XmlUtilities;
namespace Game
{
public static class ContentManager
{
/// <summary>
/// 路径
/// </summary>
public static string Path;
public static void Initialize()
{
Directory.CreateDirectory(Path =
#if ENV_ANDROID
#if Survivalcraft
//ModsManager.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, "Survivalcraft/Mods"))
//ModsManager.Combine(((AndroidSdCardExternalContentProvider)ExternalContentManager.m_providers).m_rootDirectory, "Survivalcraft/Mods"))
//ModsManager.Combine(Storage.GetDirectoryName(Window.Activity.GetExternalFilesDir(Environment.DirectoryPictures).AbsolutePath), "Survivalcraft/Mods"))
"/sdcard/Survivalcraft/Mods"
#elif Bugs
"/sdcard/Bugs/Mods"
#else
"/sdcard/RuthlessConquest/Mods"
#endif
#elif USE_DATA_PATH
Storage.GetSystemPath("data:Mods")
#else
"Mods"
#endif
);
ModsManager.Initialize();
ContentCache.AddPackage(Storage.OpenFile("app:Content.pak", OpenFileMode.Read));
for (var enumerator = ModsManager.GetEntries(".pak").GetEnumerator(); enumerator.MoveNext();)
ContentCache.AddPackage(enumerator.Current.Stream);
}
public static object Get(string name)
{
return ContentCache.Get(name);
}
public static object Get(Type type, string name)
{
if (type == typeof(Subtexture))
return TextureAtlasManager.GetSubtexture(name);
if (type == typeof(string) && name.StartsWith("Strings/"))
return StringsManager.GetString(name.Substring(8));
var obj = Get(name);
if (!type.GetTypeInfo().IsAssignableFrom(obj.GetType().GetTypeInfo()))
throw new InvalidOperationException(string.Format("Content \"{0}\" has type {1}, requested type was {2}", name, obj.GetType().FullName, type.FullName));
return obj;
}
public static T Get<T>(string name)
{
return (T)Get(typeof(T), name);
}
public static void Dispose(string name)
{
ContentCache.Dispose(name);
}
public static bool IsContent(object content)
{
return ContentCache.IsContent(content);
}
public static ReadOnlyList<ContentInfo> List()
{
return ContentCache.List("");
}
public static ReadOnlyList<ContentInfo> List(string directory)
{
return ContentCache.List(directory);
}
/// <summary>
/// 合并xml文件
/// </summary>
/// <param name="node"></param>
/// <param name="files"></param>
/// <param name="attr1"></param>
/// <param name="attr2"></param>
/// <param name="type"></param>
/// <returns></returns>
public static XElement CombineXml(XElement node, IEnumerable<FileEntry> files, string attr1 = null, string attr2 = null, string type = null)
{
var enumerator = files.GetEnumerator();
while (enumerator.MoveNext())
{
try
{
var xml =
#if Bugs
XElement.Load(enumerator.Current.Stream);
#else
XmlUtils.LoadXmlFromStream(enumerator.Current.Stream, null, true);
#endif
Modify(node, xml, attr1, attr2, type);
}
catch (Exception e)
{
ModsManager.ErrorHandler(enumerator.Current, e);
}
}
return node;
}
/// <summary>
/// 修改
/// </summary>
/// <param name="dst">目标</param>
/// <param name="src">源</param>
/// <param name="attr1"></param>
/// <param name="attr2"></param>
/// <param name="type"></param>
public static void Modify(XElement dst, XElement src, string attr1 = null, string attr2 = null, XName type = null)
{
var list = new List<XElement>();
var enumerator = src.Elements().GetEnumerator();
while (enumerator.MoveNext())
{
var node = enumerator.Current;
var nn = node.Name.LocalName;
var attr = node.Attribute(attr1);
var guid = attr?.Value;
attr = node.Attribute(attr2);
var name = attr?.Value;
int startIndex = nn.Length >= 2 && nn[0] == 'r' && nn[1] == '-' ? node.
#if Bugs
HasElements
#else
IsEmpty
#endif
? 2 : -2 : 0;
var enumerator2 = dst.DescendantsAndSelf(nn.Length == 2 && startIndex != 0 ? type : node.Name.LocalName.Substring(Math.Abs(startIndex))).GetEnumerator();
while (enumerator2.MoveNext())
{
var current = enumerator2.Current;
for (var i = current.Attributes().GetEnumerator(); i.MoveNext();)
{
nn = i.Current.Name.LocalName;
string value = i.Current.Value;
if (guid != null && string.Equals(nn, attr1))
{
if (!string.Equals(value, guid)) goto next;
}
else if (name != null && string.Equals(nn, attr2))
{
if (!string.Equals(value, name)) goto next;
}
else if ((attr = node.Attribute(XName.Get("new-" + nn))) != null)
current.SetAttributeValue(XName.Get(nn), attr.Value);
}
if (startIndex < 0)
{
current.RemoveNodes();
current.Add(node.Elements());
}
else if (startIndex > 0)
list.Add(current);
else if (!node.IsEmpty)
current.Add(node.Elements());
next:;
}
}
for (var i = list.GetEnumerator(); i.MoveNext();) i.Current.Remove();
}
}
}