-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
184 lines (174 loc) · 6.45 KB
/
Program.cs
File metadata and controls
184 lines (174 loc) · 6.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
//Yup Core 2021
// Part of YupEngine
namespace AssetBundleUtils
{
public class AssetBundleUtil
{
public static byte[] Compress(byte[] data)
{
MemoryStream output = new MemoryStream();
using (DeflateStream dstream = new DeflateStream(output, CompressionLevel.Optimal))
{
dstream.Write(data, 0, data.Length);
dstream.Close();
}
byte[] res = output.ToArray();
output.Close();
return res;
}
public static byte[] Decompress(byte[] data)
{
MemoryStream input = new MemoryStream(data);
MemoryStream output = new MemoryStream();
using (DeflateStream dstream = new DeflateStream(input, CompressionMode.Decompress))
{
dstream.CopyTo(output);
dstream.Close();
}
input.Close();
byte[] result = output.ToArray();
output.Close();
return result;
}
}
public class AssetBundle
{
public string bundleInfoStr;
public string bundlePathRelative;
public FileStream bundleFile = null;
public static AssetBundle CreateBundle(string bundleName, int headerSize, string[] args)
{
FileStream ms = new FileStream(bundleName, FileMode.Create);
long fileEndIndex = 0;
string bInfo = "";
for (int i = 0; i < args.Length; i++)
{
byte[] uncompressedFile = File.ReadAllBytes(args[i]);
byte[] localFile = AssetBundleUtil.Compress(uncompressedFile);
fileEndIndex += localFile.LongLength;
if (i == args.Length - 1)
{
bInfo += Path.GetFileName(args[i]) + ":" + fileEndIndex;
}
else
{
bInfo += Path.GetFileName(args[i]) + ":" + fileEndIndex + ";";
}
ms.Write(localFile, 0, localFile.Length);
GC.Collect();
}
AssetBundle bundle = new AssetBundle();
bundle.bundleInfoStr = bInfo;
bundle.bundlePathRelative = bundleName;
byte[] headerB = Encoding.UTF8.GetBytes(bInfo);
List<byte> header = new List<byte>(headerSize); // Create a header for files
for (int i = 0; i < headerB.Length; i++)
{
header.Add(headerB[i]);
}
var whitespace = Encoding.UTF8.GetBytes("!");
while (header.Count < headerSize)
{
for (int i = 0; i < whitespace.Length; i++)
{
header.Add(whitespace[i]);
}
}
ms.Write(header.ToArray(), 0, header.Count);
ms.Close();
bundle.bundleFile = new FileStream(bundleName, FileMode.Open);
return bundle;
}
public static AssetBundle CacheBundleInfo(string bundlePath, long headerSize)
{
AssetBundle bundle = new AssetBundle();
bundle.bundlePathRelative = bundlePath;
bundle.bundleFile = new FileStream(bundlePath, FileMode.Open);
//Header read part
bundle.bundleFile.Position = bundle.bundleFile.Length - headerSize;
byte[] headerBytes = new byte[headerSize];
bundle.bundleFile.Read(headerBytes, 0, headerBytes.Length);
string headerStr = Encoding.UTF8.GetString(headerBytes);
string bundleInfo = headerStr.Replace("!", "");
bundle.bundleFile.Position = 0;
bundle.bundleInfoStr = bundleInfo;
return bundle;
}
public void Close()
{
bundleFile.Close();
}
public string[] ListFiles()
{
string[] files = bundleInfoStr.Split(';');
List<string> names = new List<string>();
foreach (string str in files)
{
names.Add(str.Split(':')[0]);
}
return names.ToArray();
}
public async Task<byte[]> ReadDataAsync(string fileName)
{
var t = await Task.Run(() => ReadData(fileName));
return t;
}
public byte[] ReadData(string FileName)
{
string bundleInfo = bundleInfoStr;
string[] split1 = bundleInfo.Split(';');
List<string> names = new List<string>();
List<long> startIndexes = new List<long>();
List<string> spt = new List<string>();
foreach (string str in split1)
{
names.Add(str.Split(':')[0]);
spt.Add(str);
}
foreach (string str in spt)
{
try
{
startIndexes.Add(long.Parse(str.Split(':')[1]));
}
catch
{
continue;
}
}
for (int i = 0; i < names.Count; i++)
{
string str = names[i];
if (str == FileName)
{
if (i == 0)
{
byte[] segment = new byte[startIndexes[names.IndexOf(str)]];
bundleFile.Read(segment, 0, segment.Length);
byte[] uncompressed = AssetBundleUtil.Decompress(segment);
GC.Collect();
return uncompressed;
}
else
{
byte[] segment = new byte[startIndexes[names.IndexOf(str)] - startIndexes[names.IndexOf(str) - 1]];
bundleFile.Position = startIndexes[names.IndexOf(str) - 1];
bundleFile.Read(segment, 0, segment.Length);
byte[] uncompressed = AssetBundleUtil.Decompress(segment);
GC.Collect();
return uncompressed;
}
}
}
return null;
}
}
}