Browse Source

Merge pull request #2128 from eugeneko/master

Fix crash in AS named object constructor & minor refactoring
Eugene Kozlov 8 years ago
parent
commit
7dde6073aa

+ 6 - 3
Source/Urho3D/AngelScript/APITemplates.h

@@ -380,12 +380,15 @@ template <class T> void RegisterObject(asIScriptEngine* engine, const char* clas
 
 template <class T> T* ConstructObject()
 {
-    return new T(GetScriptContext());
+    T* object = new T(GetScriptContext());
+    object->AddRef();
+    return object;
 }
 
 template <class T> T* ConstructNamedObject(const String& name)
 {
     T* object = new T(GetScriptContext());
+    object->AddRef();
     object->SetName(name);
     return object;
 }
@@ -393,14 +396,14 @@ template <class T> T* ConstructNamedObject(const String& name)
 /// Template function for registering a default constructor for a class derived from Object.
 template <class T> void RegisterObjectConstructor(asIScriptEngine* engine, const char* className)
 {
-    String declFactory(String(className) + "@+ f()");
+    String declFactory(String(className) + "@ f()");
     engine->RegisterObjectBehaviour(className, asBEHAVE_FACTORY, declFactory.CString(), asFUNCTION(ConstructObject<T>), asCALL_CDECL);
 }
 
 /// Template function for registering a named constructor for a class derived from Object.
 template <class T> void RegisterNamedObjectConstructor(asIScriptEngine* engine, const char* className)
 {
-    String declFactoryWithName(String(className) + "@+ f(const String&in)");
+    String declFactoryWithName(String(className) + "@ f(const String&in)");
     engine->RegisterObjectBehaviour(className, asBEHAVE_FACTORY, declFactoryWithName.CString(), asFUNCTION(ConstructNamedObject<T>), asCALL_CDECL);
 }
 

+ 3 - 3
Source/Urho3D/Scene/ValueAnimation.cpp

@@ -340,7 +340,7 @@ bool ValueAnimation::IsValid() const
            (interpolationMethod_ == IM_SPLINE && keyFrames_.Size() > 2);
 }
 
-Variant ValueAnimation::GetAnimationValue(float scaledTime)
+Variant ValueAnimation::GetAnimationValue(float scaledTime) const
 {
     unsigned index = 1;
     for (; index < keyFrames_.Size(); ++index)
@@ -436,7 +436,7 @@ Variant ValueAnimation::LinearInterpolation(unsigned index1, unsigned index2, fl
     }
 }
 
-Variant ValueAnimation::SplineInterpolation(unsigned index1, unsigned index2, float scaledTime)
+Variant ValueAnimation::SplineInterpolation(unsigned index1, unsigned index2, float scaledTime) const
 {
     if (splineTangentsDirty_)
         UpdateSplineTangents();
@@ -488,7 +488,7 @@ Variant ValueAnimation::SplineInterpolation(unsigned index1, unsigned index2, fl
     }
 }
 
-void ValueAnimation::UpdateSplineTangents()
+void ValueAnimation::UpdateSplineTangents() const
 {
     splineTangents_.Clear();
 

+ 5 - 5
Source/Urho3D/Scene/ValueAnimation.h

@@ -124,7 +124,7 @@ public:
     float GetEndTime() const { return endTime_; }
 
     /// Return animation value.
-    Variant GetAnimationValue(float scaledTime);
+    Variant GetAnimationValue(float scaledTime) const;
 
     /// Has event frames.
     bool HasEventFrames() const { return !eventFrames_.Empty(); }
@@ -136,9 +136,9 @@ protected:
     /// Linear interpolation.
     Variant LinearInterpolation(unsigned index1, unsigned index2, float scaledTime) const;
     /// Spline interpolation.
-    Variant SplineInterpolation(unsigned index1, unsigned index2, float scaledTime);
+    Variant SplineInterpolation(unsigned index1, unsigned index2, float scaledTime) const;
     /// Update spline tangents.
-    void UpdateSplineTangents();
+    void UpdateSplineTangents() const;
     /// Return (value1 - value2) * t.
     Variant SubstractAndMultiply(const Variant& value1, const Variant& value2, float t) const;
 
@@ -159,9 +159,9 @@ protected:
     /// Key frames.
     Vector<VAnimKeyFrame> keyFrames_;
     /// Spline tangents.
-    VariantVector splineTangents_;
+    mutable VariantVector splineTangents_;
     /// Spline tangents dirty.
-    bool splineTangentsDirty_;
+    mutable bool splineTangentsDirty_;
     /// Event frames.
     Vector<VAnimEventFrame> eventFrames_;
 };