Tanoda
Alap/HashingManager.cs
Go to the documentation of this file.
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using System.Security.Cryptography;
5using System.Text;
6using GILES;
7using UnityEngine;
8
9public class HashingManager : pb_MonoBehaviourSingleton<HashingManager>
10{
11 private Hashtable hashes = new Hashtable();
12
13 public void ClearHashTable()
14 {
15 hashes.Clear();
16
17 if (SavedUser.instance.courseType == SavedUser.CourseType.Dobot)
18 hashes.Add("DOBOTROBOT", "DOBOTROBOT");
19 }
20
21 public string GetHash4GameObject(GameObject go, bool doNotStore = false)
22 {
23 var goStr = go.name;
24 goStr += go.transform.childCount;
25 goStr += go.transform.localRotation.ToString("F3").Replace(',', '.');
26 goStr += go.transform.localPosition.ToString("F3").Replace(',', '.');
27 //goStr += go.transform.hierarchyCount;
28 if (go.transform.childCount > 0)
29 goStr += GetHash4GameObject(go.transform.GetChild(0).gameObject, true);
30
31 if (go.transform.parent != null)
32 {
33 goStr += go.transform.parent.name;
34 goStr += go.transform.parent.localPosition.ToString("F3").Replace(',', '.');
35 }
36
37 //Debug.Log(GetHashString(goStr) + " hash! " + go.name + " goStr = " + goStr);
38 bool done = false;
39 while (!done)
40 {
41 try
42 {
43 if (!doNotStore)
44 hashes.Add(GetHashString(goStr),GetHashString(goStr));
45 done = true;
46 }
47 catch (Exception)
48 {
49 goStr += "0";
50 //Debug.Log(GetHashString(goStr) + " not unique hash! " + go.name + " goStr = " + goStr); //TODO: OR duplicate item
51 //PopupManager.instance.ShowPopup("Bad model structure!", "The model doesn't have unique object names OR another model has the same structure as the new one, saving/restoring the model can result in unexpected outcome.");
52 }
53 }
54 return GetHashString(goStr);
55 }
56 public static byte[] GetHash(string inputString)
57 {
58 using (HashAlgorithm algorithm = SHA256.Create())
59 return algorithm.ComputeHash(Encoding.UTF8.GetBytes(inputString));
60 }
61
62 public GameObject GetGOFromHash(string hash)
63 {
64 var hhs = FindObjectsOfType<HashHolder>(true);
65
66 foreach (var hashHolder in hhs)
67 {
68 if (hashHolder.oldLoadedHash == hash)
69 {
70 hashHolder.gameObject.GetComponent<HashHolder>().Hash = hashHolder.oldLoadedHash;
71 return hashHolder.gameObject;
72 }
73 }
74
75
76 foreach (var hashHolder in hhs)
77 {
78 if (hashHolder.Hash == hash && !hashHolder.name.Contains("(Clone)"))
79 return hashHolder.gameObject;
80 }
81
82 return null;
83 }
84
85 public static string GetHashString(string inputString)
86 {
87 StringBuilder sb = new StringBuilder();
88 foreach (byte b in GetHash(inputString))
89 sb.Append(b.ToString("X2"));
90
91 return sb.ToString();
92 }
93
94 public void AddHash(string hash)
95 {
96 try
97 {
98 hashes.Add(hash, hash);
99 }
100 catch (Exception)
101 {
102 // ignored
103 }
104 }
105}
string oldLoadedHash
Definition: HashHolder.cs:30
string GetHash4GameObject(GameObject go, bool doNotStore=false)
static byte[] GetHash(string inputString)
void AddHash(string hash)
GameObject GetGOFromHash(string hash)
static string GetHashString(string inputString)