Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
ResourcePack.cs
Go to the documentation of this file.
1using System;
3using System.IO;
4using Ionic.Zip;
6using Newtonsoft.Json.Linq;
11
12namespace Terraria.IO;
13
14public class ResourcePack
15{
16 public enum BrandingType
17 {
18 None,
20 }
21
22 public readonly string FullPath;
23
24 public readonly string FileName;
25
26 private readonly IServiceProvider _services;
27
28 public readonly bool IsCompressed;
29
30 public readonly BrandingType Branding;
31
32 private readonly ZipFile _zipFile;
33
35
37
38 private bool _needsReload = true;
39
40 private const string ICON_FILE_NAME = "icon.png";
41
42 private const string PACK_FILE_NAME = "pack.json";
43
45 {
46 get
47 {
48 if (_icon == null)
49 {
50 _icon = CreateIcon();
51 }
52 return _icon;
53 }
54 }
55
56 public string Name { get; private set; }
57
58 public string Description { get; private set; }
59
60 public string Author { get; private set; }
61
62 public ResourcePackVersion Version { get; private set; }
63
64 public bool IsEnabled { get; set; }
65
66 public int SortingOrder { get; set; }
67
69 {
70 if (File.Exists(path))
71 {
72 IsCompressed = true;
73 }
74 else if (!Directory.Exists(path))
75 {
76 throw new FileNotFoundException("Unable to find file or folder for resource pack at: " + path);
77 }
80 FullPath = path;
82 if (IsCompressed)
83 {
84 _zipFile = ZipFile.Read(path);
85 }
87 }
88
89 public void Refresh()
90 {
91 _needsReload = true;
92 }
93
95 {
96 //IL_0039: Unknown result type (might be due to invalid IL or missing references)
97 //IL_0043: Expected O, but got Unknown
98 //IL_001c: Unknown result type (might be due to invalid IL or missing references)
99 //IL_0026: Expected O, but got Unknown
100 if (_needsReload)
101 {
102 if (IsCompressed)
103 {
105 }
106 else
107 {
109 }
110 _contentSource.ContentValidator = (IContentValidator)(object)VanillaContentValidator.Instance;
111 _needsReload = false;
112 }
113 return _contentSource;
114 }
115
117 {
118 if (!HasFile("icon.png"))
119 {
120 return XnaExtensions.Get<IAssetRepository>(_services).Request<Texture2D>("Images/UI/DefaultResourcePackIcon", (AssetRequestMode)1).Value;
121 }
122 using Stream stream = OpenStream("icon.png");
124 }
125
126 private void LoadManifest()
127 {
128 if (!HasFile("pack.json"))
129 {
130 throw new FileNotFoundException(string.Format("Resource Pack at \"{0}\" must contain a {1} file.", FullPath, "pack.json"));
131 }
132 JObject val;
133 using (Stream stream = OpenStream("pack.json"))
134 {
136 val = JObject.Parse(streamReader.ReadToEnd());
137 }
138 Name = Extensions.Value<string>((IEnumerable<JToken>)val["Name"]);
139 Description = Extensions.Value<string>((IEnumerable<JToken>)val["Description"]);
140 Author = Extensions.Value<string>((IEnumerable<JToken>)val["Author"]);
141 Version = val["Version"].ToObject<ResourcePackVersion>();
142 }
143
144 private Stream OpenStream(string fileName)
145 {
146 if (!IsCompressed)
147 {
149 }
151 MemoryStream memoryStream = new MemoryStream((int)obj.UncompressedSize);
152 obj.Extract((Stream)memoryStream);
153 memoryStream.Position = 0L;
154 return memoryStream;
155 }
156
157 private bool HasFile(string fileName)
158 {
159 if (!IsCompressed)
160 {
162 }
163 return _zipFile.ContainsEntry(fileName);
164 }
165}
static bool Exists([NotNullWhen(true)] string? path)
Definition Directory.cs:43
static bool Exists([NotNullWhen(true)] string? path)
Definition File.cs:97
static FileStream OpenRead(string path)
Definition File.cs:236
static string Combine(string path1, string path2)
Definition Path.cs:304
static ? string GetFileName(string? path)
Definition Path.cs:200
readonly BrandingType Branding
readonly IServiceProvider _services
readonly string FullPath
ResourcePack(IServiceProvider services, string path, BrandingType branding=BrandingType.None)
IContentSource _contentSource
readonly string FileName
IContentSource GetContentSource()
const string PACK_FILE_NAME
bool HasFile(string fileName)
const string ICON_FILE_NAME
Stream OpenStream(string fileName)
readonly ZipFile _zipFile
readonly bool IsCompressed