Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
BlocksTexturesManager.cs
浏览该文件的文档.
1using Engine;
3using SixLabors.ImageSharp;
4using Image = Engine.Media.Image;
5
6namespace Game {
7 public static class BlocksTexturesManager {
8 public const string fName = "BlocksTexturesManager";
9 public static List<string> m_blockTextureNames = [];
10
11 public static Texture2D DefaultBlocksTexture { get; set; }
12
13 public static ReadOnlyList<string> BlockTexturesNames => new(m_blockTextureNames);
14
16
17 public static event Action<string> BlocksTextureDeleted;
18
23
24 public static bool IsBuiltIn(string name) => string.IsNullOrEmpty(name);
25
26 public static string GetFileName(string name) {
27 if (IsBuiltIn(name)) {
28 return null;
29 }
31 }
32
33 public static string GetDisplayName(string name) {
34 if (IsBuiltIn(name)) {
35 return LanguageControl.Get("Usual", "gameName");
36 }
38 }
39
40 public static DateTime GetCreationDate(string name) {
41 try {
42 if (!IsBuiltIn(name)) {
44 }
45 }
46 catch {
47 // ignored
48 }
49 return new DateTime(2000, 1, 1);
50 }
51
52 public static Texture2D LoadTexture(string name) {
53 Texture2D texture2D = null;
54 if (!IsBuiltIn(name)) {
55 try {
56 string fileName = GetFileName(name);
57 if (Storage.FileExists(fileName)) {
58 string extension = Storage.GetExtension(fileName.Replace(".scbtex", "")).ToLower();
59 if (extension == ".astc"
60 || extension == ".astcsrgb") {
61#if DIRECT3D11
62 throw new NotImplementedException();
63#else
64 using (Stream stream = Storage.OpenFile(fileName, OpenFileMode.Read)) {
65 if (CompressedTexture2D.GetParameters(stream, out int width, out int height, out _, out _)) {
66 ValidateBlocksTexture(width, height);
67 texture2D = CompressedTexture2D.Load(stream);
68 }
69 }
70#endif
71 }
72 else {
73 Image image = Image.Load(fileName);
75 texture2D = Texture2D.Load(image);
76 texture2D.Tag = image;
77 }
78 }
79 else {
80 Log.Warning(string.Format(LanguageControl.Get(fName, "1"), name));
81 }
82 }
83 catch (Exception ex) {
84 Log.Warning(string.Format(LanguageControl.Get(fName, "2"), name, ex.Message));
85 }
86 }
87 texture2D ??= DefaultBlocksTexture;
88 return texture2D;
89 }
90
91 public static string ImportBlocksTexture(string name, Stream stream) {
93 if (ex != null) {
94 throw ex;
95 }
96 string extension = Storage.GetExtension(name).ToLower();
97 if (extension != ".scbtex") {
98 name += ".scbtex";
99 }
100 if (extension == ".astc"
101 || extension == ".astcsrgb") {
102#if DIRECT3D11
103 throw new NotImplementedException();
104#else
105 if (CompressedTexture2D.GetParameters(stream, out int width, out int height, out _, out _)) {
106 ValidateBlocksTexture(width, height);
107 }
108 else {
109 throw new InvalidOperationException("Invalid ASTC file.");
110 }
111#endif
112 }
113 else {
114 ValidateBlocksTexture(stream);
115 }
116 stream.Position = 0L;
117 using (Stream destination = Storage.OpenFile(GetFileName(name), OpenFileMode.Create)) {
118 stream.CopyTo(destination);
119 return name;
120 }
121 }
122
123 public static void DeleteBlocksTexture(string name) {
124 try {
125 string fileName = GetFileName(name);
126 if (!string.IsNullOrEmpty(fileName)) {
127 Storage.DeleteFile(fileName);
128 BlocksTextureDeleted?.Invoke(name);
129 }
130 }
131 catch (Exception e) {
132 ExceptionManager.ReportExceptionToUser(string.Format(LanguageControl.Get(fName, "3"), name), e);
133 }
134 }
135
136 public static void UpdateBlocksTexturesList() {
137 m_blockTextureNames.Clear();
138 m_blockTextureNames.Add(string.Empty);
139 foreach (string item in Storage.ListFileNames(BlockTexturesDirectoryName)) {
140 m_blockTextureNames.Add(item);
141 }
142 }
143
148 public static void ValidateBlocksTexture(Stream stream) {
149 ImageInfo imageInfo = SixLabors.ImageSharp.Image.Identify(stream);
150 ValidateBlocksTexture(imageInfo.Width, imageInfo.Height);
151 }
152
153 public static void ValidateBlocksTexture(Image image) {
154 ValidateBlocksTexture(image.Width, image.Height);
155 }
156
157 public static void ValidateBlocksTexture(int width, int height) {
158 if (width > 65536
159 || height > 65536) {
160 throw new InvalidOperationException(string.Format(LanguageControl.Get(fName, "4"), width, height));
161 }
162 if (!MathUtils.IsPowerOf2(width)
163 || !MathUtils.IsPowerOf2(height)) {
164 throw new InvalidOperationException(string.Format(LanguageControl.Get(fName, "5"), width, height));
165 }
166 }
167 }
168}
Engine.Media.Image Image
static new CompressedTexture2D Load(Stream stream, bool linear=true, int mipLevelsCount=1)
static bool GetParameters(Stream stream, out int width, out int height, out int blockWidth, out int blockHeight)
static Texture2D Load(LegacyImage image, int mipLevelsCount=1)
static void Warning(object message)
定义 Log.cs:68
static bool IsPowerOf2(uint x)
static void CreateDirectory(string path)
static IEnumerable< string > ListFileNames(string path)
static Stream OpenFile(string path, OpenFileMode openFileMode)
static string GetExtension(string path)
static DateTime GetFileLastWriteTime(string path)
static bool FileExists(string path)
static string GetFileNameWithoutExtension(string path)
static void DeleteFile(string path)
static string CombinePaths(params string[] paths)
static List< string > m_blockTextureNames
static Action< string > BlocksTextureDeleted
static string ImportBlocksTexture(string name, Stream stream)
static DateTime GetCreationDate(string name)
static void ValidateBlocksTexture(int width, int height)
static ReadOnlyList< string > BlockTexturesNames
static void ValidateBlocksTexture(Stream stream)
Only for Image type stream.
static string GetDisplayName(string name)
static Texture2D LoadTexture(string name)
static string GetFileName(string name)
static void DeleteBlocksTexture(string name)
static void ValidateBlocksTexture(Image image)
static object Get(Type type, string name)
static void ReportExceptionToUser(string additionalMessage, Exception e)
static Exception VerifyExternalContentName(string name)
static string Get(string className, int key)
获取在当前语言类名键对应的字符串
static string BlockTexturesDirectoryName