Tanoda
Dana/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 private Hashtable envHashes = new Hashtable();
13
14 public void ClearHashTable()
15 {
16 hashes.Clear();
17 envHashes.Clear();
18
19 if (SavedUser.instance.courseType == SavedUser.CourseType.Dobot)
20 hashes.Add("DOBOTROBOT", "DOBOTROBOT");
21 }
22
23 public string GetHash4GameObject(GameObject go, bool doNotStore = false)
24 {
25 var goStr = go.name;
26 goStr += go.transform.childCount;
27 goStr += go.transform.localRotation.ToString("F3").Replace(',', '.');
28 goStr += go.transform.localPosition.ToString("F3").Replace(',', '.');
29 //goStr += go.transform.hierarchyCount;
30 if (go.transform.childCount > 0)
31 goStr += GetHash4GameObject(go.transform.GetChild(0).gameObject, true);
32
33 if (go.transform.parent != null)
34 {
35 goStr += go.transform.parent.name;
36 goStr += go.transform.parent.localPosition.ToString("F3").Replace(',', '.');
37 }
38
39 //Debug.Log(GetHashString(goStr) + " hash! " + go.name + " goStr = " + goStr);
40 bool done = false;
41 while (!done)
42 {
43 try
44 {
45 if (!doNotStore)
46 {
47 if (pb_Scene.instance.IsLoadingEnvNow)
48 envHashes.Add(GetHashString(goStr), goStr);
49 else
50 hashes.Add(GetHashString(goStr), goStr);
51 }
52 done = true;
53 }
54 catch (Exception)
55 {
56 goStr += "0";
57 //Debug.Log(GetHashString(goStr) + " not unique hash! " + go.name + " goStr = " + goStr); //TODO: OR duplicate item
58 //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.");
59 }
60 }
61 return GetHashString(goStr);
62 }
63 public static byte[] GetHash(string inputString)
64 {
65 using (HashAlgorithm algorithm = SHA256.Create())
66 return algorithm.ComputeHash(Encoding.UTF8.GetBytes(inputString));
67 }
68
69 public GameObject GetGOFromHash(string hash)
70 {
71 var hhs = FindObjectsOfType<HashHolder>(true);
72
73 foreach (var hashHolder in hhs)
74 {
75 if (hashHolder.oldLoadedHash == hash)
76 {
77 hashHolder.gameObject.GetComponent<HashHolder>().Hash = hashHolder.oldLoadedHash;
78 return hashHolder.gameObject;
79 }
80 }
81
82
83 foreach (var hashHolder in hhs)
84 {
85 if (hashHolder.Hash == hash && !hashHolder.name.Contains("(Clone)"))
86 return hashHolder.gameObject;
87 }
88
89 return null;
90 }
91
92 public static string GetHashString(string inputString)
93 {
94 StringBuilder sb = new StringBuilder();
95 foreach (byte b in GetHash(inputString))
96 sb.Append(b.ToString("X2"));
97
98 return sb.ToString();
99 }
100
101 public void AddHash(string hash)
102 {
103 try
104 {
105 hashes.Add(hash, hash);
106 }
107 catch (Exception)
108 {
109 // ignored
110 }
111 }
112}
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)