-
Notifications
You must be signed in to change notification settings - Fork 2
/
Utils.cs
116 lines (102 loc) · 3.56 KB
/
Utils.cs
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
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using UnityEngine;
namespace MapSharingMadeEasy
{
public class Utils
{
public static int GetStableHashCode(string str)
{
int num1 = 5381;
int num2 = num1;
for (int index = 0; index < str.Length && str[index] != char.MinValue; index += 2)
{
num1 = (num1 << 5) + num1 ^ (int) str[index];
if (index != str.Length - 1 && str[index + 1] != char.MinValue)
num2 = (num2 << 5) + num2 ^ (int) str[index + 1];
else
break;
}
return num1 + num2 * 1566083941;
}
public static string GetWhatData(bool map, bool pins)
{
if (map && pins)
{
return "map and pins";
}
else if (map)
{
return "map";
}
else if (pins)
{
return "pins";
}
return "";
}
public static string DecompressString(string compressedText)
{
var gZipBuffer = Convert.FromBase64String(compressedText);
using (var memoryStream = new MemoryStream())
{
var dataLength = BitConverter.ToInt32(gZipBuffer, 0);
memoryStream.Write(gZipBuffer, 4, gZipBuffer.Length - 4);
var buffer = new byte[dataLength];
memoryStream.Position = 0;
using (var gZipStream = new GZipStream(memoryStream, CompressionMode.Decompress))
{
gZipStream.Read(buffer, 0, buffer.Length);
}
return Encoding.UTF8.GetString(buffer);
}
}
public static bool AddMPieceToPieceTable(List<GameObject> pieces, GameObject toAdd)
{
Piece piece = toAdd.GetComponent<Piece>();
var added = false;
for (var i = 0; i < pieces.Count; i++)
{
if (pieces[i].GetComponent<Piece>().m_name == piece.m_name)
{
added = true;
break;
}
}
if (!added)
{
Log($"Adding missing recipe to pieces");
pieces.Add(toAdd);
return true;
}
return false;
}
public static void Log(string message)
{
Debug.Log($"MapSharingMadeEasy::{message}");
}
}
public static class SerializeExtension
{
public static string SerializeList(this Dictionary<string, PlayerSyncData> data)
{
var memoryStream = new MemoryStream();
var binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(memoryStream, data);
var outpd = Convert.ToBase64String(memoryStream.ToArray());
return outpd;
}
public static string SerializeList(this Dictionary<string, ExtendedPinData> data)
{
var memoryStream = new MemoryStream();
var binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(memoryStream, data);
var outpd = Convert.ToBase64String(memoryStream.ToArray());
return outpd;
}
}
}