From 34df8b5e82767c07e0007fb81934e94eeffccf52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lasse=20=C3=96=C3=B6rni?= Date: Sun, 31 Mar 2013 21:41:50 +0000 Subject: [PATCH] Fixed headless mode. Allow StringHash & ShortStringHash to be constructed implicitly. Code cleanup related to the above. --- Engine/Core/Variant.cpp | 8 ++++---- Engine/Core/Variant.h | 20 +++----------------- Engine/Engine/APITemplates.h | 16 ++++++++-------- Engine/Engine/CoreAPI.cpp | 14 +++++++------- Engine/Engine/Engine.cpp | 3 +++ Engine/Engine/NetworkAPI.cpp | 16 ++++++++-------- Engine/Engine/ResourceAPI.cpp | 14 +++++++------- Engine/Graphics/AnimatedModel.cpp | 18 ++++++------------ Engine/Graphics/AnimatedModel.h | 2 ++ Engine/Graphics/Animation.cpp | 4 ++-- Engine/Graphics/Material.cpp | 14 +++++++------- Engine/Graphics/Model.cpp | 2 +- Engine/Graphics/RenderPath.cpp | 10 +++++----- Engine/Graphics/Skeleton.cpp | 2 +- Engine/Graphics/View.cpp | 10 +++++----- Engine/IO/PackageFile.cpp | 2 +- Engine/Math/StringHash.h | 12 ++++-------- Engine/Network/Connection.cpp | 2 +- Engine/Resource/Resource.cpp | 2 +- Engine/Resource/ResourceCache.cpp | 7 ++++++- Engine/Resource/ResourceCache.h | 10 ++++++++++ Engine/Resource/XMLElement.cpp | 6 +++--- Engine/Scene/Node.cpp | 9 +++++++-- Engine/Scene/Node.h | 2 ++ Engine/Scene/Scene.cpp | 6 +++--- Engine/UI/Menu.cpp | 4 ++-- Engine/UI/UI.cpp | 2 +- Engine/UI/UIElement.cpp | 2 +- Tools/AssetImporter/AssetImporter.cpp | 2 +- 29 files changed, 112 insertions(+), 109 deletions(-) diff --git a/Engine/Core/Variant.cpp b/Engine/Core/Variant.cpp index 93628e0b5..a744eba68 100644 --- a/Engine/Core/Variant.cpp +++ b/Engine/Core/Variant.cpp @@ -234,8 +234,8 @@ void Variant::FromString(VariantType type, const char* value) { SetType(VAR_RESOURCEREF); ResourceRef& ref = *(reinterpret_cast(&value_)); - ref.type_ = ShortStringHash(values[0]); - ref.id_ = StringHash(values[1]); + ref.type_ = values[0]; + ref.id_ = values[1]; } } break; @@ -247,10 +247,10 @@ void Variant::FromString(VariantType type, const char* value) { SetType(VAR_RESOURCEREFLIST); ResourceRefList& refList = *(reinterpret_cast(&value_)); - refList.type_ = ShortStringHash(values[0]); + refList.type_ = values[0]; refList.ids_.Resize(values.Size() - 1); for (unsigned i = 1; i < values.Size(); ++i) - refList.ids_[i - 1] = StringHash(values[i]); + refList.ids_[i - 1] = values[i]; } } break; diff --git a/Engine/Core/Variant.h b/Engine/Core/Variant.h index e527c40d7..6724622ac 100644 --- a/Engine/Core/Variant.h +++ b/Engine/Core/Variant.h @@ -119,7 +119,7 @@ struct ResourceRef /// Object type. ShortStringHash type_; - /// Object identifier, for example name hash. + /// Object identifier (name hash.) StringHash id_; /// Test for equality with another reference. @@ -150,7 +150,7 @@ struct ResourceRefList /// Object type. ShortStringHash type_; - /// List of object identifiers, for example name hashes. + /// List of object identifiers (name hashes.) Vector ids_; /// Test for equality with another reference list. @@ -165,21 +165,7 @@ class Variant; typedef Vector VariantVector; /// Map of variants. -class VariantMap : public HashMap -{ -public: - /// Index the map by string hash. Create a new pair if key not found. - Variant& operator [] (const ShortStringHash& key) - { - return HashMap::operator [] (key); - } - - /// Index the map by string. Create a new pair if key not found. - Variant& operator [] (const String& key) - { - return HashMap::operator [] (ShortStringHash(key)); - } -}; +typedef HashMap VariantMap; /// Variable that supports a fixed set of types. class Variant diff --git a/Engine/Engine/APITemplates.h b/Engine/Engine/APITemplates.h index b2ca0d736..8bc033a36 100644 --- a/Engine/Engine/APITemplates.h +++ b/Engine/Engine/APITemplates.h @@ -403,17 +403,17 @@ template void RegisterComponent(asIScriptEngine* engine, const char* c static Component* NodeCreateComponent(const String& typeName, CreateMode mode, Node* ptr) { - return ptr->CreateComponent(ShortStringHash(typeName), mode); + return ptr->CreateComponent(typeName, mode); } static Component* NodeGetOrCreateComponent(const String& typeName, CreateMode mode, Node* ptr) { - return ptr->GetOrCreateComponent(ShortStringHash(typeName), mode); + return ptr->GetOrCreateComponent(typeName, mode); } static void NodeRemoveComponent(const String& typeName, Node* ptr) { - ptr->RemoveComponent(ShortStringHash(typeName)); + ptr->RemoveComponent(typeName); } static Component* NodeGetComponent(unsigned index, Node* ptr) @@ -430,7 +430,7 @@ static Component* NodeGetComponent(unsigned index, Node* ptr) static Component* NodeGetComponentWithType(const String& typeName, Node* ptr) { - return ptr->GetComponent(ShortStringHash(typeName)); + return ptr->GetComponent(typeName); } static CScriptArray* NodeGetComponents(Node* ptr) @@ -441,13 +441,13 @@ static CScriptArray* NodeGetComponents(Node* ptr) static CScriptArray* NodeGetComponentsWithType(const String& typeName, bool recursive, Node* ptr) { PODVector components; - ptr->GetComponents(components, ShortStringHash(typeName), recursive); + ptr->GetComponents(components, typeName, recursive); return VectorToHandleArray(components, "Array"); } static bool NodeHasComponent(const String& typeName, Node* ptr) { - return ptr->HasComponent(ShortStringHash(typeName)); + return ptr->HasComponent(typeName); } static CScriptArray* NodeGetChildren(bool recursive, Node* ptr) @@ -460,7 +460,7 @@ static CScriptArray* NodeGetChildren(bool recursive, Node* ptr) static CScriptArray* NodeGetChildrenWithComponent(String& typeName, bool recursive, Node* ptr) { PODVector nodes; - ptr->GetChildrenWithComponent(nodes, ShortStringHash(typeName), recursive); + ptr->GetChildrenWithComponent(nodes, typeName, recursive); return VectorToHandleArray(nodes, "Array"); } @@ -776,7 +776,7 @@ static bool UIElementSaveXML(File* file, UIElement* ptr) static UIElement* UIElementCreateChild(const String& typeName, const String& name, UIElement* ptr) { - return ptr->CreateChild(ShortStringHash(typeName), name); + return ptr->CreateChild(typeName, name); } static void UIElementRemoveChild(UIElement* child, unsigned index, UIElement* ptr) diff --git a/Engine/Engine/CoreAPI.cpp b/Engine/Engine/CoreAPI.cpp index d34659bfe..ed54cd132 100644 --- a/Engine/Engine/CoreAPI.cpp +++ b/Engine/Engine/CoreAPI.cpp @@ -361,12 +361,12 @@ static Variant& VariantMapAtHash(ShortStringHash key, VariantMap& map) static bool VariantMapContains(const String& key, VariantMap& map) { - return map.Contains(ShortStringHash(key)); + return map.Contains(key); } static bool VariantMapErase(const String& key, VariantMap& map) { - return map.Erase(ShortStringHash(key)); + return map.Erase(key); } static bool VariantMapContainsHash(ShortStringHash key, VariantMap& map) @@ -665,35 +665,35 @@ static void SendEvent(const String& eventType, VariantMap& eventData) { Object* sender = GetScriptContextEventListenerObject(); if (sender) - sender->SendEvent(StringHash(eventType), eventData); + sender->SendEvent(eventType, eventData); } static void SubscribeToEvent(const String& eventType, const String& handlerName) { ScriptEventListener* listener = GetScriptContextEventListener(); if (listener) - listener->AddEventHandler(StringHash(eventType), handlerName); + listener->AddEventHandler(eventType, handlerName); } static void SubscribeToSenderEvent(Object* sender, const String& eventType, const String& handlerName) { ScriptEventListener* listener = GetScriptContextEventListener(); if (listener) - listener->AddEventHandler(sender, StringHash(eventType), handlerName); + listener->AddEventHandler(sender, eventType, handlerName); } static void UnsubscribeFromEvent(const String& eventType) { Object* listener = GetScriptContextEventListenerObject(); if (listener) - listener->UnsubscribeFromEvent(StringHash(eventType)); + listener->UnsubscribeFromEvent(eventType); } static void UnsubscribeFromSenderEvent(Object* sender, const String& eventType) { Object* listener = GetScriptContextEventListenerObject(); if (listener) - listener->UnsubscribeFromEvent(sender, StringHash(eventType)); + listener->UnsubscribeFromEvent(sender, eventType); } static void UnsubscribeFromSenderEvents(Object* sender) diff --git a/Engine/Engine/Engine.cpp b/Engine/Engine/Engine.cpp index e311d55eb..2a9533491 100644 --- a/Engine/Engine/Engine.cpp +++ b/Engine/Engine/Engine.cpp @@ -106,6 +106,9 @@ bool Engine::Initialize(const VariantMap& parameters) if (initialized_) return true; + // Set headless mode + headless_ = GetParameter(parameters, "Headless", false).GetBool(); + // Register object factories and attributes first, then subsystems RegisterObjects(); RegisterSubsystems(); diff --git a/Engine/Engine/NetworkAPI.cpp b/Engine/Engine/NetworkAPI.cpp index 052938c36..cf178d8b8 100644 --- a/Engine/Engine/NetworkAPI.cpp +++ b/Engine/Engine/NetworkAPI.cpp @@ -77,12 +77,12 @@ static void RegisterNetworkPriority(asIScriptEngine* engine) void SendRemoteEvent(const String& eventType, bool inOrder, const VariantMap& eventData, Connection* ptr) { - ptr->SendRemoteEvent(StringHash(eventType), inOrder, eventData); + ptr->SendRemoteEvent(eventType, inOrder, eventData); } void SendRemoteNodeEvent(Node* receiver, const String& eventType, bool inOrder, const VariantMap& eventData, Connection* ptr) { - ptr->SendRemoteEvent(receiver, StringHash(eventType), inOrder, eventData); + ptr->SendRemoteEvent(receiver, eventType, inOrder, eventData); } static void RegisterConnection(asIScriptEngine* engine) @@ -131,32 +131,32 @@ static CScriptArray* NetworkGetClientConnections(Network* ptr) static void NetworkBroadcastRemoteEvent(const String& eventType, bool inOrder, const VariantMap& eventData, Network* ptr) { - ptr->BroadcastRemoteEvent(StringHash(eventType), inOrder, eventData); + ptr->BroadcastRemoteEvent(eventType, inOrder, eventData); } static void NetworkBroadcastRemoteSceneEvent(Scene* scene, const String& eventType, bool inOrder, const VariantMap& eventData, Network* ptr) { - ptr->BroadcastRemoteEvent(scene, StringHash(eventType), inOrder, eventData); + ptr->BroadcastRemoteEvent(scene, eventType, inOrder, eventData); } static void NetworkBroadcastRemoteNodeEvent(Node* node, const String& eventType, bool inOrder, const VariantMap& eventData, Network* ptr) { - ptr->BroadcastRemoteEvent(node, StringHash(eventType), inOrder, eventData); + ptr->BroadcastRemoteEvent(node, eventType, inOrder, eventData); } static void NetworkRegisterRemoteEvent(const String& eventType, Network* ptr) { - ptr->RegisterRemoteEvent(StringHash(eventType)); + ptr->RegisterRemoteEvent(eventType); } static void NetworkUnregisterRemoteEvent(const String& eventType, Network* ptr) { - ptr->UnregisterRemoteEvent(StringHash(eventType)); + ptr->UnregisterRemoteEvent(eventType); } static bool NetworkCheckRemoteEvent(const String& eventType, Network* ptr) { - return ptr->CheckRemoteEvent(StringHash(eventType)); + return ptr->CheckRemoteEvent(eventType); } void RegisterNetwork(asIScriptEngine* engine) diff --git a/Engine/Engine/ResourceAPI.cpp b/Engine/Engine/ResourceAPI.cpp index 4732d0d9d..2941a50e7 100644 --- a/Engine/Engine/ResourceAPI.cpp +++ b/Engine/Engine/ResourceAPI.cpp @@ -38,7 +38,7 @@ void RegisterResource(asIScriptEngine* engine) static Resource* ResourceCacheGetResource(const String& type, const String& name, ResourceCache* ptr) { - return ptr->GetResource(ShortStringHash(type), name); + return ptr->GetResource(type, name); } static File* ResourceCacheGetFile(const String& name, ResourceCache* ptr) @@ -53,32 +53,32 @@ static File* ResourceCacheGetFile(const String& name, ResourceCache* ptr) static void ResourceCacheReleaseResource(const String& type, const String& name, bool force, ResourceCache* ptr) { - ptr->ReleaseResource(ShortStringHash(type), name, force); + ptr->ReleaseResource(type, name, force); } static void ResourceCacheReleaseResources(const String& type, bool force, ResourceCache* ptr) { - ptr->ReleaseResources(ShortStringHash(type), force); + ptr->ReleaseResources(type, force); } static void ResourceCacheReleaseResourcesPartial(const String& type, const String& partialName, bool force, ResourceCache* ptr) { - ptr->ReleaseResources(ShortStringHash(type), partialName, force); + ptr->ReleaseResources(type, partialName, force); } static void ResourceCacheSetMemoryBudget(const String& type, unsigned budget, ResourceCache* ptr) { - ptr->SetMemoryBudget(ShortStringHash(type), budget); + ptr->SetMemoryBudget(type, budget); } static unsigned ResourceCacheGetMemoryBudget(const String& type, ResourceCache* ptr) { - return ptr->GetMemoryBudget(ShortStringHash(type)); + return ptr->GetMemoryBudget(type); } static unsigned ResourceCacheGetMemoryUse(const String& type, ResourceCache* ptr) { - return ptr->GetMemoryUse(ShortStringHash(type)); + return ptr->GetMemoryUse(type); } static ResourceCache* GetResourceCache() diff --git a/Engine/Graphics/AnimatedModel.cpp b/Engine/Graphics/AnimatedModel.cpp index ecaecce0a..d206886aa 100644 --- a/Engine/Graphics/AnimatedModel.cpp +++ b/Engine/Graphics/AnimatedModel.cpp @@ -590,18 +590,12 @@ AnimationState* AnimatedModel::GetAnimationState(Animation* animation) const AnimationState* AnimatedModel::GetAnimationState(const String& animationName) const { - for (Vector >::ConstIterator i = animationStates_.Begin(); i != animationStates_.End(); ++i) - { - Animation* animation = (*i)->GetAnimation(); - if (animation) - { - // Check both the animation and the resource name - if (animation->GetName() == animationName || animation->GetAnimationName() == animationName) - return *i; - } - } - - return 0; + return GetAnimationState(StringHash(animationName)); +} + +AnimationState* AnimatedModel::GetAnimationState(const char* animationName) const +{ + return GetAnimationState(StringHash(animationName)); } AnimationState* AnimatedModel::GetAnimationState(StringHash animationNameHash) const diff --git a/Engine/Graphics/AnimatedModel.h b/Engine/Graphics/AnimatedModel.h index 481cfd2a4..72bebf722 100644 --- a/Engine/Graphics/AnimatedModel.h +++ b/Engine/Graphics/AnimatedModel.h @@ -105,6 +105,8 @@ public: AnimationState* GetAnimationState(Animation* animation) const; /// Return animation state by animation name. AnimationState* GetAnimationState(const String& animationName) const; + /// Return animation state by animation name. + AnimationState* GetAnimationState(const char* animationName) const; /// Return animation state by animation name hash. AnimationState* GetAnimationState(const StringHash animationNameHash) const; /// Return animation state by index. diff --git a/Engine/Graphics/Animation.cpp b/Engine/Graphics/Animation.cpp index 31ddd0fed..4b9165e8c 100644 --- a/Engine/Graphics/Animation.cpp +++ b/Engine/Graphics/Animation.cpp @@ -90,7 +90,7 @@ bool Animation::Load(Deserializer& source) // Read name and length animationName_ = source.ReadString(); - animationNameHash_ = StringHash(animationName_); + animationNameHash_ = animationName_; length_ = source.ReadFloat(); tracks_.Clear(); @@ -103,7 +103,7 @@ bool Animation::Load(Deserializer& source) { AnimationTrack& newTrack = tracks_[i]; newTrack.name_ = source.ReadString(); - newTrack.nameHash_ = StringHash(newTrack.name_); + newTrack.nameHash_ = newTrack.name_; newTrack.channelMask_ = source.ReadUByte(); unsigned keyFrames = source.ReadUInt(); diff --git a/Engine/Graphics/Material.cpp b/Engine/Graphics/Material.cpp index 7b39eb5a8..88935a1ce 100644 --- a/Engine/Graphics/Material.cpp +++ b/Engine/Graphics/Material.cpp @@ -295,10 +295,10 @@ void Material::SetShaderParameter(const String& name, const Vector4& value) MaterialShaderParameter newParam; newParam.name_ = name; newParam.value_ = value; - StringHash stringHash(name); - shaderParameters_[stringHash] = newParam; + StringHash nameHash(name); + shaderParameters_[nameHash] = newParam; - if (stringHash == PSP_MATSPECCOLOR) + if (nameHash == PSP_MATSPECCOLOR) specular_ = value.x_ > 0.0f || value.y_ > 0.0f || value.z_ > 0.0f; } @@ -360,10 +360,10 @@ void Material::SetDepthBias(const BiasParameters& parameters) void Material::RemoveShaderParameter(const String& name) { - StringHash stringHash(name); - shaderParameters_.Erase(stringHash); + StringHash nameHash(name); + shaderParameters_.Erase(nameHash); - if (stringHash == PSP_MATSPECCOLOR) + if (nameHash == PSP_MATSPECCOLOR) specular_ = false; } @@ -421,7 +421,7 @@ Texture* Material::GetTexture(TextureUnit unit) const const Vector4& Material::GetShaderParameter(const String& name) const { - HashMap::ConstIterator i = shaderParameters_.Find(StringHash(name)); + HashMap::ConstIterator i = shaderParameters_.Find(name); return i != shaderParameters_.End() ? i->second_.value_ : Vector4::ZERO; } diff --git a/Engine/Graphics/Model.cpp b/Engine/Graphics/Model.cpp index e3f6ed22a..2925f1405 100644 --- a/Engine/Graphics/Model.cpp +++ b/Engine/Graphics/Model.cpp @@ -200,7 +200,7 @@ bool Model::Load(Deserializer& source) ModelMorph newMorph; newMorph.name_ = source.ReadString(); - newMorph.nameHash_ = StringHash(newMorph.name_); + newMorph.nameHash_ = newMorph.name_; newMorph.weight_ = 0.0f; unsigned nubuffers_ = source.ReadUInt(); diff --git a/Engine/Graphics/RenderPath.cpp b/Engine/Graphics/RenderPath.cpp index ff2a40f9f..d89bc8af6 100644 --- a/Engine/Graphics/RenderPath.cpp +++ b/Engine/Graphics/RenderPath.cpp @@ -118,7 +118,7 @@ void RenderPathCommand::LoadParameters(const XMLElement& element) break; case CMD_SCENEPASS: - pass_ = StringHash(element.GetAttribute("pass")); + pass_ = element.GetAttribute("pass"); sortMode_ = (RenderCommandSortMode)GetStringListIndex(element.GetAttributeLower("sort"), sortModeNames, SORT_FRONTTOBACK); if (element.HasAttribute("marktostencil")) markToStencil_ = element.GetBool("marktostencil"); @@ -144,7 +144,7 @@ void RenderPathCommand::LoadParameters(const XMLElement& element) { String name = parameterElem.GetAttribute("name"); Vector4 value = parameterElem.GetVector("value"); - shaderParameters_[StringHash(name)] = value; + shaderParameters_[name] = value; parameterElem = parameterElem.GetNext("parameter"); } @@ -203,12 +203,12 @@ void RenderPathCommand::SetTextureName(TextureUnit unit, const String& name) void RenderPathCommand::SetShaderParameter(const String& name, const Vector4& value) { - shaderParameters_[StringHash(name)] = value; + shaderParameters_[name] = value; } void RenderPathCommand::RemoveShaderParameter(const String& name) { - shaderParameters_.Erase(StringHash(name)); + shaderParameters_.Erase(name); } void RenderPathCommand::SetNumOutputs(unsigned num) @@ -232,7 +232,7 @@ const String& RenderPathCommand::GetTextureName(TextureUnit unit) const const Vector4& RenderPathCommand::GetShaderParameter(const String& name) const { - HashMap::ConstIterator i = shaderParameters_.Find(StringHash(name)); + HashMap::ConstIterator i = shaderParameters_.Find(name); return i != shaderParameters_.End() ? i->second_ : Vector4::ZERO; } diff --git a/Engine/Graphics/Skeleton.cpp b/Engine/Graphics/Skeleton.cpp index 2a006735d..9da748e9c 100644 --- a/Engine/Graphics/Skeleton.cpp +++ b/Engine/Graphics/Skeleton.cpp @@ -53,7 +53,7 @@ bool Skeleton::Load(Deserializer& source) { Bone newBone; newBone.name_ = source.ReadString(); - newBone.nameHash_ = StringHash(newBone.name_); + newBone.nameHash_ = newBone.name_; newBone.parentIndex_ = source.ReadUInt(); newBone.initialPosition_ = source.ReadVector3(); newBone.initialRotation_ = source.ReadQuaternion(); diff --git a/Engine/Graphics/View.cpp b/Engine/Graphics/View.cpp index de9e79fe3..6674eda95 100644 --- a/Engine/Graphics/View.cpp +++ b/Engine/Graphics/View.cpp @@ -1400,7 +1400,7 @@ void View::SetTextures(const RenderPathCommand& command) } // Bind a rendertarget - HashMap::ConstIterator j = renderTargets_.Find(StringHash(command.textureNames_[i])); + HashMap::ConstIterator j = renderTargets_.Find(command.textureNames_[i]); if (j != renderTargets_.End()) { graphics_->SetTexture(i, j->second_); @@ -1461,11 +1461,11 @@ void View::RenderQuad(const RenderPathCommand& command) float width = (float)renderTargets_[nameHash]->GetWidth(); float height = (float)renderTargets_[nameHash]->GetHeight(); - graphics_->SetShaderParameter(StringHash(invSizeName), Vector4(1.0f / width, 1.0f / height, 0.0f, 0.0f)); + graphics_->SetShaderParameter(invSizeName, Vector4(1.0f / width, 1.0f / height, 0.0f, 0.0f)); #ifdef USE_OPENGL - graphics_->SetShaderParameter(StringHash(offsetsName), Vector4::ZERO); + graphics_->SetShaderParameter(offsetsName, Vector4::ZERO); #else - graphics_->SetShaderParameter(StringHash(offsetsName), Vector4(0.5f / width, 0.5f / height, 0.0f, 0.0f)); + graphics_->SetShaderParameter(offsetsName, Vector4(0.5f / width, 0.5f / height, 0.0f, 0.0f)); #endif } @@ -1559,7 +1559,7 @@ void View::AllocateScreenBuffers() height = rtSize_.y_ / (height ? height : 1); } - renderTargets_[StringHash(rtInfo.name_)] = renderer_->GetScreenBuffer(width, height, rtInfo.format_, rtInfo.filtered_, rtInfo.sRGB_); + renderTargets_[rtInfo.name_] = renderer_->GetScreenBuffer(width, height, rtInfo.format_, rtInfo.filtered_, rtInfo.sRGB_); } } diff --git a/Engine/IO/PackageFile.cpp b/Engine/IO/PackageFile.cpp index 472ed912d..78fbb108d 100644 --- a/Engine/IO/PackageFile.cpp +++ b/Engine/IO/PackageFile.cpp @@ -63,7 +63,7 @@ bool PackageFile::Open(const String& fileName) } fileName_ = fileName; - nameHash_ = StringHash(fileName_); + nameHash_ = fileName_; totalSize_ = file->GetSize(); unsigned numFiles = file->ReadUInt(); diff --git a/Engine/Math/StringHash.h b/Engine/Math/StringHash.h index e91f8f751..7a2c1db92 100644 --- a/Engine/Math/StringHash.h +++ b/Engine/Math/StringHash.h @@ -50,10 +50,9 @@ public: } /// Construct from a C string case-insensitively. - explicit StringHash(const char* str); - + StringHash(const char* str); /// Construct from a string case-insensitively. - explicit StringHash(const String& str); + StringHash(const String& str); /// Assign from another hash. StringHash& operator = (const StringHash& rhs) @@ -134,9 +133,9 @@ public: } /// Construct from a C string case-insensitively. - explicit ShortStringHash(const char* str); + ShortStringHash(const char* str); /// Construct from a string case-insensitively. - explicit ShortStringHash(const String& str); + ShortStringHash(const String& str); /// Assign from another hash. ShortStringHash& operator = (const ShortStringHash& rhs) @@ -188,7 +187,4 @@ private: unsigned short value_; }; -#define HASH(str) (StringHash(#str)) -#define SHORTHASH(str) (ShortStringHash(#str)) - } diff --git a/Engine/Network/Connection.cpp b/Engine/Network/Connection.cpp index cd06bc192..4d92d3da8 100644 --- a/Engine/Network/Connection.cpp +++ b/Engine/Network/Connection.cpp @@ -1352,7 +1352,7 @@ void Connection::RequestPackage(const String& name, unsigned fileSize, unsigned void Connection::SendPackageError(const String& name) { msg_.Clear(); - msg_.WriteStringHash(StringHash(name)); + msg_.WriteStringHash(name); SendMessage(MSG_PACKAGEDATA, true, false, msg_); } diff --git a/Engine/Resource/Resource.cpp b/Engine/Resource/Resource.cpp index 42dbdd061..aaccf6ff5 100644 --- a/Engine/Resource/Resource.cpp +++ b/Engine/Resource/Resource.cpp @@ -49,7 +49,7 @@ bool Resource::Save(Serializer& dest) void Resource::SetName(const String& name) { name_ = name; - nameHash_ = StringHash(name); + nameHash_ = name; } void Resource::SetMemoryUse(unsigned size) diff --git a/Engine/Resource/ResourceCache.cpp b/Engine/Resource/ResourceCache.cpp index 99560a075..486b0f932 100644 --- a/Engine/Resource/ResourceCache.cpp +++ b/Engine/Resource/ResourceCache.cpp @@ -393,6 +393,11 @@ Resource* ResourceCache::GetResource(ShortStringHash type, const String& nameIn) return GetResource(type, StringHash(name)); } +Resource* ResourceCache::GetResource(ShortStringHash type, const char* name) +{ + return GetResource(type, String(name)); +} + Resource* ResourceCache::GetResource(ShortStringHash type, StringHash nameHash) { // If null hash, return null pointer immediately @@ -605,7 +610,7 @@ void ResourceCache::StoreResourceDependency(Resource* resource, const String& de return; StringHash nameHash(resource->GetName()); - HashSet& dependents = dependentResources_[StringHash(dependency)]; + HashSet& dependents = dependentResources_[dependency]; dependents.Insert(nameHash); } diff --git a/Engine/Resource/ResourceCache.h b/Engine/Resource/ResourceCache.h index 6083cd339..1ff61cb57 100644 --- a/Engine/Resource/ResourceCache.h +++ b/Engine/Resource/ResourceCache.h @@ -93,6 +93,8 @@ public: SharedPtr GetFile(const String& name); /// Return a resource by type and name. Load if not loaded yet. Return null if fails. Resource* GetResource(ShortStringHash type, const String& name); + /// Return a resource by type and name. Load if not loaded yet. Return null if fails. + Resource* GetResource(ShortStringHash type, const char* name); /// Return a resource by type and name hash. Load if not loaded yet. Return null if fails. Resource* GetResource(ShortStringHash type, StringHash nameHash); /// Return all loaded resources of a specific type. @@ -105,6 +107,8 @@ public: const Vector >& GetPackageFiles() const { return packages_; } /// Template version of returning a resource by name. template T* GetResource(const String& name); + /// Template version of returning a resource by name. + template T* GetResource(const char* name); /// Template version of returning a resource by name hash. template T* GetResource(StringHash nameHash); /// Template version of returning loaded resources of a specific type. @@ -171,6 +175,12 @@ template T* ResourceCache::GetResource(const String& name) return static_cast(GetResource(type, name)); } +template T* ResourceCache::GetResource(const char* name) +{ + ShortStringHash type = T::GetTypeStatic(); + return static_cast(GetResource(type, name)); +} + template T* ResourceCache::GetResource(StringHash nameHash) { ShortStringHash type = T::GetTypeStatic(); diff --git a/Engine/Resource/XMLElement.cpp b/Engine/Resource/XMLElement.cpp index 764ef80cf..f0da40b9d 100644 --- a/Engine/Resource/XMLElement.cpp +++ b/Engine/Resource/XMLElement.cpp @@ -589,8 +589,8 @@ ResourceRef XMLElement::GetResourceRef() const Vector values = String::Split(GetAttribute("value"), ';'); if (values.Size() == 2) { - ret.type_ = ShortStringHash(values[0]); - ret.id_ = StringHash(values[1]); + ret.type_ = values[0]; + ret.id_ = values[1]; // Whenever we encounter a resource name read from a ResourceRef XML element, store the reverse mapping to // ResourceCache if possible. We will probably use the hash to request a resource shortly afterward @@ -612,7 +612,7 @@ ResourceRefList XMLElement::GetResourceRefList() const // ResourceCache if possible. We will probably use the hashes to request resources shortly afterward ResourceCache* cache = file_ ? file_->GetSubsystem() : 0; - ret.type_ = ShortStringHash(values[0]); + ret.type_ = values[0]; ret.ids_.Resize(values.Size() - 1); for (unsigned i = 1; i < values.Size(); ++i) { diff --git a/Engine/Scene/Node.cpp b/Engine/Scene/Node.cpp index f38c938f6..89caff06c 100644 --- a/Engine/Scene/Node.cpp +++ b/Engine/Scene/Node.cpp @@ -220,7 +220,7 @@ bool Node::SaveXML(Serializer& dest) void Node::SetName(const String& name) { name_ = name; - nameHash_ = StringHash(name); + nameHash_ = name_; MarkNetworkUpdate(); @@ -800,6 +800,11 @@ Node* Node::GetChild(const String& name, bool recursive) const return GetChild(StringHash(name), recursive); } +Node* Node::GetChild(const char* name, bool recursive) const +{ + return GetChild(StringHash(name), recursive); +} + Node* Node::GetChild(StringHash nameHash, bool recursive) const { for (Vector >::ConstIterator i = children_.Begin(); i != children_.End(); ++i) @@ -1044,7 +1049,7 @@ bool Node::LoadXML(const XMLElement& source, SceneResolver& resolver, bool readC { String typeName = compElem.GetAttribute("type"); unsigned compID = compElem.GetInt("id"); - Component* newComponent = CreateComponent(ShortStringHash(typeName), + Component* newComponent = CreateComponent(typeName, (mode == REPLICATED && compID < FIRST_LOCAL_ID) ? REPLICATED : LOCAL, rewriteIDs ? 0 : compID); if (newComponent) { diff --git a/Engine/Scene/Node.h b/Engine/Scene/Node.h index b12b6a157..90b5eae9d 100644 --- a/Engine/Scene/Node.h +++ b/Engine/Scene/Node.h @@ -252,6 +252,8 @@ public: Node* GetChild(unsigned index) const; /// Return child scene node by name. Node* GetChild(const String& name, bool recursive = false) const; + /// Return child scene node by name. + Node* GetChild(const char* name, bool recursive = false) const; /// Return child scene node by name hash. Node* GetChild(StringHash nameHash, bool recursive = false) const; /// Return number of components. diff --git a/Engine/Scene/Scene.cpp b/Engine/Scene/Scene.cpp index f334b69f5..dfef9170c 100644 --- a/Engine/Scene/Scene.cpp +++ b/Engine/Scene/Scene.cpp @@ -431,12 +431,12 @@ void Scene::ClearRequiredPackageFiles() void Scene::RegisterVar(const String& name) { - varNames_[ShortStringHash(name)] = name; + varNames_[name] = name; } void Scene::UnregisterVar(const String& name) { - varNames_.Erase(ShortStringHash(name)); + varNames_.Erase(name); } void Scene::UnregisterAllVars() @@ -740,7 +740,7 @@ void Scene::SetVarNamesAttr(String value) varNames_.Clear(); for (Vector::ConstIterator i = varNames.Begin(); i != varNames.End(); ++i) - varNames_[ShortStringHash(*i)] = *i; + varNames_[*i] = *i; } String Scene::GetVarNamesAttr() const diff --git a/Engine/UI/Menu.cpp b/Engine/UI/Menu.cpp index 8530b076c..5c263b115 100644 --- a/Engine/UI/Menu.cpp +++ b/Engine/UI/Menu.cpp @@ -124,11 +124,11 @@ bool Menu::LoadXML(const XMLElement& source, XMLFile* styleFile) if (!internalElem) { if (!popupElem) - child = CreateChild(ShortStringHash(typeName)); + child = CreateChild(typeName); else { // Do not add the popup element as a child even temporarily, as that can break layouts - SharedPtr popup = DynamicCast(context_->CreateObject(ShortStringHash(typeName))); + SharedPtr popup = DynamicCast(context_->CreateObject(typeName)); if (!popup) LOGERROR("Could not create popup element type " + ShortStringHash(typeName).ToString()); else diff --git a/Engine/UI/UI.cpp b/Engine/UI/UI.cpp index c3dfd54b5..43b0e6af3 100644 --- a/Engine/UI/UI.cpp +++ b/Engine/UI/UI.cpp @@ -374,7 +374,7 @@ SharedPtr UI::LoadLayout(XMLFile* file, XMLFile* styleFile) if (typeName.Empty()) typeName = "UIElement"; - root = DynamicCast(context_->CreateObject(ShortStringHash(typeName))); + root = DynamicCast(context_->CreateObject(typeName)); if (!root) { LOGERROR("Could not create unknown UI element " + typeName); diff --git a/Engine/UI/UIElement.cpp b/Engine/UI/UIElement.cpp index 4126e44ec..d3d9c31a5 100644 --- a/Engine/UI/UIElement.cpp +++ b/Engine/UI/UIElement.cpp @@ -241,7 +241,7 @@ bool UIElement::LoadXML(const XMLElement& source, XMLFile* styleFile) UIElement* child = 0; if (!internalElem) - child = CreateChild(ShortStringHash(typeName)); + child = CreateChild(typeName); else { for (unsigned i = nextInternalChild; i < children_.Size(); ++i) diff --git a/Tools/AssetImporter/AssetImporter.cpp b/Tools/AssetImporter/AssetImporter.cpp index efb76c9c4..0497be905 100644 --- a/Tools/AssetImporter/AssetImporter.cpp +++ b/Tools/AssetImporter/AssetImporter.cpp @@ -864,7 +864,7 @@ void BuildAndSaveAnimations(OutModel& model) AnimationTrack track; track.name_ = channelName; - track.nameHash_ = StringHash(channelName); + track.nameHash_ = channelName; // Check which channels are used track.channelMask_ = 0;