Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
SurvivalCraftModEntity.cs
浏览该文件的文档.
1using System.Reflection;
2using System.Xml.Linq;
5#if !BROWSER
6using Engine;
7#endif
8
9namespace Game {
11 public new const string fName = "SurvivalCraftModEntity";
12
14 List<IContentReader.IContentReader> readers = new();
15 readers.AddRange(
16 [
17 new AssemblyReader(),
18 new BitmapFontReader(),
19 new DaeModelReader(),
20 new ImageReader(),
21 new JsonArrayReader(),
22 new JsonObjectReader(),
27 new ShaderReader(),
30 new StringReader(),
31 new SubtextureReader(),
32 new Texture2DReader(),
33 new XmlReader(),
35 ]
36 );
37 for (int i = 0; i < readers.Count; i++) {
38 ContentManager.ReaderList.Add(readers[i].Type, readers[i]);
39 }
40#if BROWSER
43 Thread.Sleep(100);
44 }
45 UnmanagedMemoryStream memoryStream = ContentFileBridge.GetStream();
46 Size = memoryStream.Length;
47 ModArchive = ZipArchive.Open(memoryStream);
48#else
49 MemoryStream memoryStream = new();
50 const string ContentPath = "app:/Content.zip";
51 if (Storage.FileExists(ContentPath)) //检测外置资源是否存在,如果不存在就使用内置资源
52 {
53 Storage.OpenFile(ContentPath, OpenFileMode.Read).CopyTo(memoryStream);
54 }
55 else {
56 Assembly.GetExecutingAssembly().GetManifestResourceStream("Game.Content.zip")?.CopyTo(memoryStream);
57 }
58 if (memoryStream == null) {
59 throw new Exception("Unable to load Content.zip file.");
60 }
61 Size = memoryStream.Length;
62 memoryStream.Position = 0L;
63 ModArchive = ZipArchive.Open(memoryStream);
64#endif
66 if (modInfo != null) {
67 modInfo.LoadOrder = int.MinValue;
68 }
69 }
70
71 public override void LoadBlocksData() {
72 LoadingScreen.Info($"[{modInfo?.Name}] {LanguageControl.Get(fName, "1")}");
73 BlocksManager.LoadBlocksData(ContentManager.Get<string>("BlocksData"));
74 ContentManager.Dispose("BlocksData");
75 }
76
77 public override Assembly[] GetAssemblies() => [typeof(BlocksManager).Assembly];
78
79 public override void HandleAssembly(Assembly assembly) {
80#pragma warning disable IL2026
81 Type[] types = assembly.GetTypes();
82#pragma warning restore IL2026
83 foreach (Type type in types) {
84 if (type.IsSubclassOf(typeof(ModLoader))
85 && !type.IsAbstract) {
86#pragma warning disable IL2072
87 if (Activator.CreateInstance(type) is not ModLoader modLoader) {
88#pragma warning restore IL2072
89 continue;
90 }
91 modLoader.Entity = this;
92 modLoader.__ModInitialize();
93 Loader = modLoader;
94 ModsManager.ModLoaders.Add(modLoader);
95 }
96 else if (type.IsSubclassOf(typeof(Block))
97 && !type.IsAbstract) {
98#pragma warning disable IL2072
99 FieldInfo fieldInfo = type.GetRuntimeFields().FirstOrDefault(p => p.Name == "Index" && p.IsPublic && p.IsStatic);
100#pragma warning restore IL2072
101 if (fieldInfo == null
102 || fieldInfo.FieldType != typeof(int)) {
104 new InvalidOperationException($"Block type \"{type.FullName}\" does not have static field Index of type int.")
105 );
106 }
107 else {
108 /*var index = (int)fieldInfo.GetValue(null);
109 var block = (Block)Activator.CreateInstance(type.GetTypeInfo().AsType());
110 block.BlockIndex = index;*/
111 BlockTypes.Add(type);
112 }
113 }
114 }
115 }
116
117 public override void LoadXdb(ref XElement xElement) {
118 LoadingScreen.Info($"[{modInfo?.Name}] {LanguageControl.Get(fName, "2")}");
119 xElement = ContentManager.Get<XElement>("Database");
120 ContentManager.Dispose("Database");
121 }
122
123 public override void LoadCr(ref XElement xElement) {
124 LoadingScreen.Info($"[{modInfo?.Name}] {LanguageControl.Get(fName, "3")}");
125 xElement = ContentManager.Get<XElement>("CraftingRecipes");
126 ContentManager.Dispose("CraftingRecipes");
127 }
128
129 public override void LoadClo(ClothingBlock block, ref XElement xElement) {
130 LoadingScreen.Info($"[{modInfo?.Name}] {LanguageControl.Get(fName, "4")}");
131 xElement = ContentManager.Get<XElement>("Clothes");
132 ContentManager.Dispose("Clothes");
133 }
134
135 public override void SaveSettings(XElement xElement) { }
136 public override void LoadSettings(XElement xElement) { }
137
138 public override void OnBlocksInitalized() {
139 BlocksManager.AddCategory("Terrain");
140 BlocksManager.AddCategory("Minerals");
141 BlocksManager.AddCategory("Plants");
142 BlocksManager.AddCategory("Construction");
143 BlocksManager.AddCategory("Items");
144 BlocksManager.AddCategory("Tools");
145 BlocksManager.AddCategory("Weapons");
146 BlocksManager.AddCategory("Clothes");
147 BlocksManager.AddCategory("Electrics");
149 BlocksManager.AddCategory("Spawner Eggs");
150 BlocksManager.AddCategory("Painted");
152 BlocksManager.AddCategory("Fireworks");
153 }
154 }
155}
Game.IContentReader.StringReader StringReader
static partial void SetContentPtr(IntPtr ptr)
static Stream OpenFile(string path, OpenFileMode openFileMode)
static bool FileExists(string path)
static void LoadBlocksData(string blocksDataString)
static void AddCategory(string category)
static UnmanagedMemoryStream GetStream()
static object Get(Type type, string name)
static Dictionary< string, IContentReader.IContentReader > ReaderList
static void Dispose(string name)
可能需要带上文件后缀,即获取名字+获取的后缀
static void Info(string mesg)
List< Type > BlockTypes
virtual void InitResources()
初始化Content资源
ZipArchive ModArchive
override void LoadBlocksData()
初始化BlocksData资源
override void HandleAssembly(Assembly assembly)
override void LoadCr(ref XElement xElement)
初始化CraftingRecipe
override Assembly[] GetAssemblies()
加载mod程序集
override void LoadClo(ClothingBlock block, ref XElement xElement)
初始化Clothing数据
override void SaveSettings(XElement xElement)
保存设置
override void LoadXdb(ref XElement xElement)
初始化Database数据
override void LoadSettings(XElement xElement)
加载设置
override void OnBlocksInitalized()
BlocksManager初始化完毕
static ZipArchive Open(Stream stream, bool keepStreamOpen=false)
static List< ModLoader > ModLoaders
static void AddException(Exception e, bool AllowContinue_=false)