Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
Database.cs
浏览该文件的文档.
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using Engine;
5
7 public class Database {
9
10 ReadOnlyList<DatabaseObjectType> m_databaseObjectTypes;
11
12 public Dictionary<Guid, DatabaseObject> m_databaseObjectsByGuid = [];
13
14 public IList<DatabaseObjectType> DatabaseObjectTypes => m_databaseObjectTypes;
15
17
18 public Database(DatabaseObject root, IEnumerable<DatabaseObjectType> databaseObjectTypes) {
19 List<DatabaseObjectType> objectTypes = databaseObjectTypes.ToList();
20 if (!objectTypes.Contains(root.Type)) {
21 throw new Exception("Database root has invalid database object type.");
22 }
23 if (root.NestingParent != null) {
24 throw new Exception("Database root cannot be nested.");
25 }
26 m_databaseObjectTypes = new ReadOnlyList<DatabaseObjectType>(objectTypes);
27 m_root = root;
28 m_root.m_database = this;
29 }
30
31 public DatabaseObjectType FindDatabaseObjectType(string name, bool throwIfNotFound) {
32 foreach (DatabaseObjectType databaseObjectType in m_databaseObjectTypes) {
33 if (databaseObjectType.Name == name) {
34 return databaseObjectType;
35 }
36 }
37 if (throwIfNotFound) {
38 throw new Exception($"Required database object type \"{name}\" not found.");
39 }
40 return null;
41 }
42
43 public DatabaseObject FindDatabaseObject(Guid guid, DatabaseObjectType type, bool throwIfNotFound) {
44 m_databaseObjectsByGuid.TryGetValue(guid, out DatabaseObject value);
45 if (value != null) {
46 if (type != null
47 && value.Type != type) {
48 throw new InvalidOperationException($"Database object {guid} has invalid type. Expected {type.Name}, found {value.Type.Name}.");
49 }
50 }
51 else if (throwIfNotFound) {
52 throw new InvalidOperationException($"Required database object {guid} not found.");
53 }
54 return value;
55 }
56
57 public DatabaseObject FindDatabaseObject(string name, DatabaseObjectType type, bool throwIfNotFound) =>
58 Root.FindExplicitNestedChild(name, type, false, throwIfNotFound);
59
60 public void FindUsedValueTypes(List<Type> typesList) {
61 foreach (DatabaseObject explicitNestingChild in Root.GetExplicitNestingChildren(null, false)) {
62 if (explicitNestingChild.Value != null
63 && !typesList.Contains(explicitNestingChild.Value.GetType())) {
64 typesList.Add(explicitNestingChild.Value.GetType());
65 }
66 }
67 }
68
69 internal void AddDatabaseObject(DatabaseObject databaseObject, bool checkThatGuidsAreUnique) {
70 if (databaseObject.m_database != null) {
71 throw new InvalidOperationException("Internal error: database object is already in a database.");
72 }
73 if (!m_databaseObjectTypes.Contains(databaseObject.Type)) {
74 throw new InvalidOperationException($"Database object type \"{databaseObject.Type.Name}\" is not supported by the database.");
75 }
76 if (checkThatGuidsAreUnique) {
77 if (databaseObject.Guid != Guid.Empty
78 && m_databaseObjectsByGuid.ContainsKey(databaseObject.Guid)) {
79 throw new InvalidOperationException($"Database object {databaseObject.Guid} is already present in the database.");
80 }
81 foreach (DatabaseObject explicitNestingChild in databaseObject.GetExplicitNestingChildren(null, false)) {
82 if (explicitNestingChild.Guid != Guid.Empty
83 && m_databaseObjectsByGuid.ContainsKey(explicitNestingChild.Guid)) {
84 throw new InvalidOperationException($"Database object {explicitNestingChild.Guid} is already present in the database.");
85 }
86 }
87 }
88 databaseObject.m_database = this;
89 if (databaseObject.Guid != Guid.Empty) {
90 m_databaseObjectsByGuid.Add(databaseObject.Guid, databaseObject);
91 }
92 foreach (DatabaseObject explicitNestingChild2 in databaseObject.GetExplicitNestingChildren(null, true)) {
93 AddDatabaseObject(explicitNestingChild2, false);
94 }
95 }
96
97 internal void RemoveDatabaseObject(DatabaseObject databaseObject) {
98 if (databaseObject.m_database != this) {
99 throw new InvalidOperationException("Internal error: database object is not in the database.");
100 }
101 databaseObject.m_database = null;
102 if (databaseObject.Guid != Guid.Empty
103 && !m_databaseObjectsByGuid.Remove(databaseObject.Guid)) {
104 throw new InvalidOperationException("Internal error: database object not in dictionary.");
105 }
106 foreach (DatabaseObject explicitNestingChild in databaseObject.GetExplicitNestingChildren(null, true)) {
107 RemoveDatabaseObject(explicitNestingChild);
108 }
109 }
110 }
111}
void AddDatabaseObject(DatabaseObject databaseObject, bool checkThatGuidsAreUnique)
DatabaseObjectType FindDatabaseObjectType(string name, bool throwIfNotFound)
Database(DatabaseObject root, IEnumerable< DatabaseObjectType > databaseObjectTypes)
Dictionary< Guid, DatabaseObject > m_databaseObjectsByGuid
void RemoveDatabaseObject(DatabaseObject databaseObject)
ReadOnlyList< DatabaseObjectType > m_databaseObjectTypes
IList< DatabaseObjectType > DatabaseObjectTypes
void FindUsedValueTypes(List< Type > typesList)
DatabaseObject FindDatabaseObject(Guid guid, DatabaseObjectType type, bool throwIfNotFound)
DatabaseObject FindDatabaseObject(string name, DatabaseObjectType type, bool throwIfNotFound)
IEnumerable< DatabaseObject > GetExplicitNestingChildren(DatabaseObjectType type, bool directChildrenOnly)