Terraria
v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
ResourcePack.cs
Go to the documentation of this file.
1
using
System
;
2
using
System.Collections.Generic
;
3
using
System.IO
;
4
using
Ionic.Zip;
5
using
Microsoft.Xna.Framework.Graphics
;
6
using
Newtonsoft.Json.Linq;
7
using
ReLogic.Content
;
8
using
ReLogic.Content.Sources
;
9
using
ReLogic.Utilities
;
10
using
Terraria.GameContent
;
11
12
namespace
Terraria.IO
;
13
14
public
class
ResourcePack
15
{
16
public
enum
BrandingType
17
{
18
None
,
19
SteamWorkshop
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
34
private
Texture2D
_icon
;
35
36
private
IContentSource
_contentSource
;
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
44
public
Texture2D
Icon
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
68
public
ResourcePack
(
IServiceProvider
services
,
string
path
,
BrandingType
branding
=
BrandingType
.None)
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
}
78
FileName
=
Path
.
GetFileName
(
path
);
79
_services
=
services
;
80
FullPath
=
path
;
81
Branding
=
branding
;
82
if
(
IsCompressed
)
83
{
84
_zipFile
= ZipFile.Read(
path
);
85
}
86
LoadManifest
();
87
}
88
89
public
void
Refresh
()
90
{
91
_needsReload
=
true
;
92
}
93
94
public
IContentSource
GetContentSource
()
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
{
104
_contentSource
= (
IContentSource
)
new
ZipContentSource
(
FullPath
,
"Content"
);
105
}
106
else
107
{
108
_contentSource
= (
IContentSource
)
new
FileSystemContentSource
(
Path
.
Combine
(
FullPath
,
"Content"
));
109
}
110
_contentSource.ContentValidator = (
IContentValidator
)(
object
)
VanillaContentValidator
.
Instance
;
111
_needsReload
=
false
;
112
}
113
return
_contentSource
;
114
}
115
116
private
Texture2D
CreateIcon
()
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"
);
123
return
XnaExtensions
.Get<
AssetReaderCollection
>(
_services
).
Read<Texture2D>
(
stream
,
".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
{
135
using
StreamReader
streamReader
=
new
StreamReader
(
stream
);
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
{
148
return
File
.
OpenRead
(
Path
.
Combine
(
FullPath
,
fileName
));
149
}
150
ZipEntry
obj
=
_zipFile
[
fileName
];
151
MemoryStream
memoryStream
=
new
MemoryStream
((
int
)
obj
.UncompressedSize);
152
obj
.Extract((
Stream
)
memoryStream
);
153
memoryStream.Position = 0
L
;
154
return
memoryStream
;
155
}
156
157
private
bool
HasFile
(
string
fileName
)
158
{
159
if
(!
IsCompressed
)
160
{
161
return
File
.
Exists
(
Path
.
Combine
(
FullPath
,
fileName
));
162
}
163
return
_zipFile
.ContainsEntry(
fileName
);
164
}
165
}
Microsoft.Xna.Framework.Graphics.Texture2D
Definition
Texture2D.cs:13
ReLogic.Content.AssetReaderCollection
Definition
AssetReaderCollection.cs:8
ReLogic.Content.Sources.FileSystemContentSource
Definition
FileSystemContentSource.cs:8
ReLogic.Content.Sources.ZipContentSource
Definition
ZipContentSource.cs:11
ReLogic.Utilities.XnaExtensions
Definition
XnaExtensions.cs:6
System.Collections.Generic.Dictionary
Definition
Dictionary.cs:14
System.IO.Directory.Exists
static bool Exists([NotNullWhen(true)] string? path)
Definition
Directory.cs:43
System.IO.Directory
Definition
Directory.cs:8
System.IO.FileNotFoundException
Definition
FileNotFoundException.cs:9
System.IO.File.Exists
static bool Exists([NotNullWhen(true)] string? path)
Definition
File.cs:97
System.IO.File.OpenRead
static FileStream OpenRead(string path)
Definition
File.cs:236
System.IO.File
Definition
File.cs:15
System.IO.MemoryStream
Definition
MemoryStream.cs:9
System.IO.Path.Combine
static string Combine(string path1, string path2)
Definition
Path.cs:304
System.IO.Path.GetFileName
static ? string GetFileName(string? path)
Definition
Path.cs:200
System.IO.Path
Definition
Path.cs:8
System.IO.StreamReader
Definition
StreamReader.cs:9
System.IO.Stream
Definition
Stream.cs:11
System.Version
Definition
Version.cs:10
Terraria.GameContent.VanillaContentValidator.Instance
static VanillaContentValidator Instance
Definition
VanillaContentValidator.cs:29
Terraria.GameContent.VanillaContentValidator
Definition
VanillaContentValidator.cs:10
Terraria.IO.ResourcePack.Refresh
void Refresh()
Definition
ResourcePack.cs:89
Terraria.IO.ResourcePack.Author
string Author
Definition
ResourcePack.cs:60
Terraria.IO.ResourcePack.Branding
readonly BrandingType Branding
Definition
ResourcePack.cs:30
Terraria.IO.ResourcePack.BrandingType
BrandingType
Definition
ResourcePack.cs:17
Terraria.IO.ResourcePack.BrandingType.None
@ None
Terraria.IO.ResourcePack.BrandingType.SteamWorkshop
@ SteamWorkshop
Terraria.IO.ResourcePack._services
readonly IServiceProvider _services
Definition
ResourcePack.cs:26
Terraria.IO.ResourcePack._needsReload
bool _needsReload
Definition
ResourcePack.cs:38
Terraria.IO.ResourcePack.SortingOrder
int SortingOrder
Definition
ResourcePack.cs:66
Terraria.IO.ResourcePack.IsEnabled
bool IsEnabled
Definition
ResourcePack.cs:64
Terraria.IO.ResourcePack.FullPath
readonly string FullPath
Definition
ResourcePack.cs:22
Terraria.IO.ResourcePack.ResourcePack
ResourcePack(IServiceProvider services, string path, BrandingType branding=BrandingType.None)
Definition
ResourcePack.cs:68
Terraria.IO.ResourcePack._contentSource
IContentSource _contentSource
Definition
ResourcePack.cs:36
Terraria.IO.ResourcePack._icon
Texture2D _icon
Definition
ResourcePack.cs:34
Terraria.IO.ResourcePack.FileName
readonly string FileName
Definition
ResourcePack.cs:24
Terraria.IO.ResourcePack.Icon
Texture2D Icon
Definition
ResourcePack.cs:45
Terraria.IO.ResourcePack.GetContentSource
IContentSource GetContentSource()
Definition
ResourcePack.cs:94
Terraria.IO.ResourcePack.PACK_FILE_NAME
const string PACK_FILE_NAME
Definition
ResourcePack.cs:42
Terraria.IO.ResourcePack.Name
string Name
Definition
ResourcePack.cs:56
Terraria.IO.ResourcePack.HasFile
bool HasFile(string fileName)
Definition
ResourcePack.cs:157
Terraria.IO.ResourcePack.ICON_FILE_NAME
const string ICON_FILE_NAME
Definition
ResourcePack.cs:40
Terraria.IO.ResourcePack.LoadManifest
void LoadManifest()
Definition
ResourcePack.cs:126
Terraria.IO.ResourcePack.CreateIcon
Texture2D CreateIcon()
Definition
ResourcePack.cs:116
Terraria.IO.ResourcePack.OpenStream
Stream OpenStream(string fileName)
Definition
ResourcePack.cs:144
Terraria.IO.ResourcePack._zipFile
readonly ZipFile _zipFile
Definition
ResourcePack.cs:32
Terraria.IO.ResourcePack.Description
string Description
Definition
ResourcePack.cs:58
Terraria.IO.ResourcePack.IsCompressed
readonly bool IsCompressed
Definition
ResourcePack.cs:28
Terraria.IO.ResourcePack
Definition
ResourcePack.cs:15
ReLogic.Content.IAssetRepository
Definition
IAssetRepository.cs:9
ReLogic.Content.IContentValidator
Definition
IContentValidator.cs:4
ReLogic.Content.Sources.IContentSource
Definition
IContentSource.cs:8
System.IServiceProvider
Definition
IServiceProvider.cs:4
Extensions
Definition
EnumerationExtensions.cs:3
Microsoft.Xna.Framework.Graphics
Definition
AlphaTestEffect.cs:1
ReLogic.Content.Sources
Definition
ContentSource.cs:6
ReLogic.Content.AssetRequestMode
AssetRequestMode
Definition
AssetRequestMode.cs:4
ReLogic.Content
Definition
IAssetReader.cs:5
ReLogic.Utilities
Definition
AttributeUtilities.cs:5
System.Collections.Generic
Definition
IHashKeyCollection.cs:1
System.IO
Definition
ConsoleStream.cs:3
System.ExceptionArgument.obj
@ obj
System.ExceptionArgument.stream
@ stream
System.ParseFailureKind.Format
@ Format
System.ConsoleKey.L
@ L
System
Definition
BlockingCollection.cs:8
Terraria.GameContent
Definition
AchievementsHelper.cs:1
Terraria.IO
Definition
FavoritesFile.cs:8
Terraria.IO.ResourcePackVersion
Definition
ResourcePackVersion.cs:9
source
Terraria.IO
ResourcePack.cs
Generated by
1.10.0