com.wallstop-studios.unity-helpers 2.0.0-rc78.9 → 2.0.0-rc79

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,115 @@
1
+ namespace WallstopStudios.UnityHelpers.Utils
2
+ {
3
+ using System;
4
+ using System.Collections.Generic;
5
+ using System.Text;
6
+ using System.Threading;
7
+
8
+ public static class Ascii85
9
+ {
10
+ private static readonly ThreadLocal<StringBuilder> StringBuilderCache = new(() =>
11
+ new StringBuilder()
12
+ );
13
+
14
+ private static readonly ThreadLocal<byte[]> ChunkCache = new(() => new byte[4]);
15
+ private static readonly ThreadLocal<char[]> EncodedCache = new(() => new char[5]);
16
+ private static readonly ThreadLocal<List<byte>> ByteListCache = new(() => new List<byte>());
17
+
18
+ private static readonly uint[] Pow85 = { 85 * 85 * 85 * 85, 85 * 85 * 85, 85 * 85, 85, 1 };
19
+
20
+ public static string Encode(byte[] data)
21
+ {
22
+ if (data == null)
23
+ {
24
+ return null;
25
+ }
26
+
27
+ StringBuilder stringBuilder = StringBuilderCache.Value;
28
+ stringBuilder.Clear();
29
+ int index = 0;
30
+
31
+ byte[] chunk = ChunkCache.Value;
32
+ char[] encoded = EncodedCache.Value;
33
+ while (index < data.Length)
34
+ {
35
+ Array.Clear(chunk, 0, chunk.Length);
36
+ int chunkLength = 0;
37
+
38
+ for (int i = 0; i < 4 && index < data.Length; ++i)
39
+ {
40
+ chunk[i] = data[index++];
41
+ chunkLength++;
42
+ }
43
+
44
+ uint val =
45
+ ((uint)chunk[0] << 24)
46
+ | ((uint)chunk[1] << 16)
47
+ | ((uint)chunk[2] << 8)
48
+ | chunk[3];
49
+
50
+ if (val == 0 && chunkLength == 4)
51
+ {
52
+ stringBuilder.Append('z');
53
+ continue;
54
+ }
55
+
56
+ Array.Clear(encoded, 0, encoded.Length);
57
+ for (int i = 0; i < encoded.Length; ++i)
58
+ {
59
+ encoded[i] = (char)(val / Pow85[i] + '!');
60
+ val %= Pow85[i];
61
+ }
62
+
63
+ for (int i = 0; i < chunkLength + 1; ++i)
64
+ {
65
+ stringBuilder.Append(encoded[i]);
66
+ }
67
+ }
68
+ return stringBuilder.ToString();
69
+ }
70
+
71
+ public static byte[] Decode(string encoded)
72
+ {
73
+ if (string.IsNullOrEmpty(encoded))
74
+ {
75
+ return Array.Empty<byte>();
76
+ }
77
+
78
+ encoded = encoded.Replace("z", "!!!!!");
79
+
80
+ List<byte> result = ByteListCache.Value;
81
+ result.Clear();
82
+ int index = 0;
83
+ char[] chunk = EncodedCache.Value;
84
+ byte[] decodedBytes = ChunkCache.Value;
85
+ while (index < encoded.Length)
86
+ {
87
+ Array.Fill(chunk, (char)117);
88
+ int chunkLen = 0;
89
+
90
+ for (int i = 0; i < 5 && index < encoded.Length; ++i)
91
+ {
92
+ chunk[i] = encoded[index++];
93
+ chunkLen++;
94
+ }
95
+
96
+ uint val = 0;
97
+ for (int i = 0; i < 5; ++i)
98
+ {
99
+ val += (uint)(chunk[i] - '!') * Pow85[i];
100
+ }
101
+
102
+ decodedBytes[0] = (byte)(val >> 24);
103
+ decodedBytes[1] = (byte)(val >> 16);
104
+ decodedBytes[2] = (byte)(val >> 8);
105
+ decodedBytes[3] = (byte)val;
106
+
107
+ for (int i = 0; i < chunkLen - 1; ++i)
108
+ {
109
+ result.Add(decodedBytes[i]);
110
+ }
111
+ }
112
+ return result.ToArray();
113
+ }
114
+ }
115
+ }
@@ -0,0 +1,3 @@
1
+ fileFormatVersion: 2
2
+ guid: 852c7e2a84f64cfc9e5e44b0f5d1f597
3
+ timeCreated: 1756399903
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "com.wallstop-studios.unity-helpers",
3
- "version": "2.0.0-rc78.9",
3
+ "version": "2.0.0-rc79",
4
4
  "displayName": "Unity Helpers",
5
5
  "description": "Various Unity Helper Library",
6
6
  "dependencies": {},
@@ -43,3 +43,4 @@
43
43
 
44
44
 
45
45
 
46
+