Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
DatabaseManager.cs
浏览该文件的文档.
1using System.Reflection;
2using System.Xml.Linq;
6
7namespace Game {
8 public static class DatabaseManager {
10
11 public static Dictionary<string, ValuesDictionary> m_valueDictionaries = [];
12
13 public static GameDatabase GameDatabase {
14 get {
15 if (m_gameDatabase != null) {
16 return m_gameDatabase;
17 }
18 throw new InvalidOperationException("Database not loaded.");
19 }
20 }
21
22 public static ICollection<ValuesDictionary> EntitiesValuesDictionaries => m_valueDictionaries.Values;
23
24 public static XElement DatabaseNode;
25
26 public static void Initialize() {
27 DatabaseNode = null;
28 m_valueDictionaries.Clear();
29 }
30
31 public static void LoadDataBaseFromXml(XElement node) {
32 m_valueDictionaries.Clear();
34 foreach (DatabaseObject explicitNestingChild in GameDatabase.Database.Root.GetExplicitNestingChildren(
35 GameDatabase.EntityTemplateType,
36 false
37 )) {
38 ValuesDictionary valuesDictionary = new();
39 valuesDictionary.PopulateFromDatabaseObject(explicitNestingChild);
40 m_valueDictionaries.Add(explicitNestingChild.Name, valuesDictionary);
41 }
42 }
43
44 public static ValuesDictionary FindEntityValuesDictionary(string entityTemplateName, bool throwIfNotFound) {
45 if (!m_valueDictionaries.TryGetValue(entityTemplateName, out ValuesDictionary value) && throwIfNotFound) {
46 throw new InvalidOperationException($"EntityTemplate \"{entityTemplateName}\" not found.");
47 }
48 return value;
49 }
50
51 public static ValuesDictionary FindValuesDictionaryForComponent(ValuesDictionary entityVd, Type componentType) {
52 foreach (ValuesDictionary item in entityVd.Values.OfType<ValuesDictionary>()) {
53 if (item.DatabaseObject.Type == GameDatabase.MemberComponentTemplateType) {
54 Type type = TypeCache.FindType(item.GetValue<string>("Class"), true, true);
55 if (componentType.GetTypeInfo().IsAssignableFrom(type.GetTypeInfo())) {
56 return item;
57 }
58 }
59 }
60 return null;
61 }
62
63 [Obsolete]
64 public static Entity CreateEntity(Project project, SpawnEntityData spawnEntityData, bool throwIfNotFound) {
65 Entity entity = CreateEntity(project, spawnEntityData.TemplateName, throwIfNotFound);
66 if (entity != null) {
67 if (spawnEntityData.EntityId != 0) {
68 entity.Id = spawnEntityData.EntityId;
69 }
70 else {
71 entity.Id = 0;
72 }
73 }
74 return entity;
75 }
76
77 public static Entity CreateEntity(Project project, string entityTemplateName, bool throwIfNotFound) {
78 ValuesDictionary valuesDictionary = FindEntityValuesDictionary(entityTemplateName, throwIfNotFound);
79 if (valuesDictionary == null) {
80 return null;
81 }
82 return project.CreateEntity(valuesDictionary);
83 }
84
85 public static Entity CreateEntity(Project project, string entityTemplateName, ValuesDictionary overrides, bool throwIfNotFound) {
86 ValuesDictionary valuesDictionary = FindEntityValuesDictionary(entityTemplateName, throwIfNotFound);
87 if (valuesDictionary != null) {
88 valuesDictionary.ApplyOverrides(overrides);
89 return project.CreateEntity(valuesDictionary);
90 }
91 return null;
92 }
93 }
94}
static Type FindType(string typeName, bool skipSystemAssemblies, bool throwIfNotFound)
static ICollection< ValuesDictionary > EntitiesValuesDictionaries
static GameDatabase m_gameDatabase
static ValuesDictionary FindValuesDictionaryForComponent(ValuesDictionary entityVd, Type componentType)
static Entity CreateEntity(Project project, SpawnEntityData spawnEntityData, bool throwIfNotFound)
static Dictionary< string, ValuesDictionary > m_valueDictionaries
static Entity CreateEntity(Project project, string entityTemplateName, bool throwIfNotFound)
static GameDatabase GameDatabase
static Entity CreateEntity(Project project, string entityTemplateName, ValuesDictionary overrides, bool throwIfNotFound)
static void LoadDataBaseFromXml(XElement node)
static ValuesDictionary FindEntityValuesDictionary(string entityTemplateName, bool throwIfNotFound)
virtual Entity CreateEntity(ValuesDictionary valuesDictionary, int entityId=0)
void PopulateFromDatabaseObject(DatabaseObject databaseObject)
void ApplyOverrides(ValuesDictionary overridesValuesDictionary)