summaryrefslogtreecommitdiff
path: root/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/Core/Serialization/SimpleZipReplacement.cs
blob: 28f69b2022d9a64868d31afc01afab0ccc7a3fa1 (plain)
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
#if ASTAR_NO_ZIP
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace Pathfinding.Serialization.Zip {
	public enum ZipOption {
		Always
	}

	public class ZipFile {
		public System.Text.Encoding AlternateEncoding;
		public ZipOption AlternateEncodingUsage = ZipOption.Always;
		public int ParallelDeflateThreshold = 0;

		Dictionary<string, ZipEntry> dict = new Dictionary<string, ZipEntry>();

		public void AddEntry (string name, byte[] bytes) {
			dict[name] = new ZipEntry(name, bytes);
		}

		public bool ContainsEntry (string name) {
			return dict.ContainsKey(name);
		}

		public void Save (System.IO.Stream stream) {
			var writer = new System.IO.BinaryWriter(stream);

			writer.Write(dict.Count);
			foreach (KeyValuePair<string, ZipEntry> pair in dict) {
				writer.Write(pair.Key);
				writer.Write(pair.Value.bytes.Length);
				writer.Write(pair.Value.bytes);
			}
		}

		public static ZipFile Read (System.IO.Stream stream) {
			ZipFile file = new ZipFile();

			var reader = new System.IO.BinaryReader(stream);
			int count = reader.ReadInt32();

			for (int i = 0; i < count; i++) {
				var name = reader.ReadString();
				var length = reader.ReadInt32();
				var bytes = reader.ReadBytes(length);

				file.dict[name] = new ZipEntry(name, bytes);
			}

			return file;
		}

		public ZipEntry this[string index] {
			get {
				ZipEntry v;
				dict.TryGetValue(index, out v);
				return v;
			}
		}

		public void Dispose () {
		}
	}

	public class ZipEntry {
		internal string name;
		internal byte[] bytes;

		public ZipEntry (string name, byte[] bytes) {
			this.name = name;
			this.bytes = bytes;
		}

		public void Extract (System.IO.Stream stream) {
			stream.Write(bytes, 0, bytes.Length);
		}
	}
}
#endif