Browse Source

Bruteforce a nonsense component name from hash if necessary so that XML and binary save of an UnknownComponent are compatible.

Lasse Öörni 12 years ago
parent
commit
6b38a1cff7
1 changed files with 47 additions and 1 deletions
  1. 47 1
      Source/Engine/Scene/UnknownComponent.cpp

+ 47 - 1
Source/Engine/Scene/UnknownComponent.cpp

@@ -31,6 +31,52 @@
 namespace Urho3D
 {
 
+static HashMap<ShortStringHash, String> unknownTypeToName;
+static String letters("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
+
+static String GenerateNameFromType(ShortStringHash typeHash)
+{
+    if (unknownTypeToName.Contains(typeHash))
+        return unknownTypeToName[typeHash];
+    
+    String test;
+    
+    // Begin brute-force search
+    unsigned numLetters = letters.Length();
+    unsigned combinations = numLetters;
+    bool found = false;
+    
+    for (unsigned i = 1; i < 6; ++i)
+    {
+        test.Resize(i);
+        
+        for (unsigned j = 0; j < combinations; ++j)
+        {
+            unsigned current = j;
+            
+            for (unsigned k = 0; k < i; ++k)
+            {
+                test[k] = letters[current % numLetters];
+                current /= numLetters;
+            }
+            
+            if (ShortStringHash(test) == typeHash)
+            {
+                found = true;
+                break;
+            }
+        }
+        
+        if (found)
+            break;
+        
+        combinations *= numLetters;
+    }
+    
+    unknownTypeToName[typeHash] = test;
+    return test;
+}
+
 UnknownComponent::UnknownComponent(Context* context) :
     Component(context),
     useXML_(false)
@@ -125,7 +171,7 @@ void UnknownComponent::SetTypeName(const String& typeName)
 
 void UnknownComponent::SetType(ShortStringHash typeHash)
 {
-    typeName_ = "Unknown " + typeHash.ToString();
+    typeName_ = GenerateNameFromType(typeHash);
     typeHash_ = typeHash;
 }