Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
ZipContentSource.cs
Go to the documentation of this file.
1using System;
3using System.IO;
4using System.Linq;
6using Ionic.Zip;
7
9
11{
12 private readonly ZipFile _zipFile;
13
15
16 private readonly string _basePath;
17
18 private bool _isDisposed;
19
20 public int EntryCount => _entries.Count;
21
22 public ZipContentSource(string path)
23 : this(path, "")
24 {
25 }
26
27 public ZipContentSource(string path, string contentDir)
28 {
29 _zipFile = ZipFile.Read(path);
31 {
32 throw new ArgumentException("Content directory cannot contain \"..\"", "contentDir");
33 }
36 }
37
38 public ZipContentSource(ZipFile zip, string contentDir)
39 {
40 _zipFile = zip;
42 {
43 throw new ArgumentException("Content directory cannot contain \"..\"", "contentDir");
44 }
47 }
48
49 public override Stream OpenStream(string assetName)
50 {
52 {
54 }
55 MemoryStream memoryStream = new MemoryStream((int)value.UncompressedSize);
57 {
58 value.Extract((Stream)memoryStream);
59 }
60 memoryStream.Position = 0L;
61 return memoryStream;
62 }
63
64 private void BuildEntryList()
65 {
67 foreach (ZipEntry item in _zipFile.Entries.Where((ZipEntry entry) => !entry.IsDirectory && entry.FileName.StartsWith(_basePath)))
68 {
69 string fileName = item.FileName;
70 string path = fileName.Substring(_basePath.Length, fileName.Length - _basePath.Length);
73 }
75 }
76
77 private static bool ZipPathContainsInvalidCharacters(string path)
78 {
79 if (!path.Contains("../"))
80 {
81 return path.Contains("..\\");
82 }
83 return true;
84 }
85
86 private static string CleanZipPath(string path)
87 {
88 path = path.Replace('\\', '/');
89 path = Regex.Replace(path, "^[./]+", "");
90 if (path.Length != 0 && !path.EndsWith("/"))
91 {
92 path += "/";
93 }
94 return path;
95 }
96
97 protected virtual void Dispose(bool disposing)
98 {
99 if (!_isDisposed)
100 {
101 if (disposing)
102 {
103 _entries.Clear();
104 _zipFile.Dispose();
105 }
106 _isDisposed = true;
107 }
108 }
109
110 public void Dispose()
111 {
112 Dispose(disposing: true);
113 }
114}
static AssetLoadException FromMissingAsset(string assetName, Exception innerException=null)
static string CleanPath(string path)
void SetAssetNames(IEnumerable< string > paths)
static string CleanZipPath(string path)
readonly Dictionary< string, ZipEntry > _entries
override Stream OpenStream(string assetName)
Must be threadsafe!
ZipContentSource(ZipFile zip, string contentDir)
ZipContentSource(string path, string contentDir)
virtual void Dispose(bool disposing)
static bool ZipPathContainsInvalidCharacters(string path)
bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value)
bool ICollection< KeyValuePair< TKey, TValue > >. Contains(KeyValuePair< TKey, TValue > keyValuePair)
static string Replace(string input, string pattern, string replacement)
Definition Regex.cs:514