2
0
Эх сурвалжийг харах

Applied ScriptInstance hot reload patch from Magic.Lixin, slightly modified.

Lasse Öörni 13 жил өмнө
parent
commit
b449032071

+ 28 - 0
Engine/Core/Object.cpp

@@ -210,6 +210,34 @@ void Object::UnsubscribeFromAllEventsWithUserData()
     }
 }
 
+void Object::UnsubscribeFromAllEventsExcept(const PODVector<StringHash>& exceptions)
+{
+    EventHandler* handler = eventHandlers_.First();
+    EventHandler* previous = 0;
+    
+    while (handler)
+    {
+        EventHandler* next = eventHandlers_.Next(handler);
+        
+        if (!exceptions.Contains(handler->GetEventType()))
+        {
+            if (handler->GetSender())
+                context_->RemoveEventReceiver(this, handler->GetSender(), handler->GetEventType());
+            else
+                context_->RemoveEventReceiver(this, handler->GetEventType());
+            
+            EventHandler* next = eventHandlers_.Next(handler);
+            eventHandlers_.Erase(handler, previous);
+            handler = next;
+        }
+        else
+        {
+            previous = handler;
+            handler = eventHandlers_.Next(handler);
+        }
+    }
+}
+
 void Object::SendEvent(StringHash eventType)
 {
     VariantMap noEventData;

+ 2 - 0
Engine/Core/Object.h

@@ -64,6 +64,8 @@ public:
     void UnsubscribeFromAllEvents();
     /// Unsubscribe from all events with userdata defined in the handler.
     void UnsubscribeFromAllEventsWithUserData();
+    /// Unsubscribe from all events except those listed.
+    void UnsubscribeFromAllEventsExcept(const PODVector<StringHash>& exceptions);
     /// Send event to all subscribers.
     void SendEvent(StringHash eventType);
     /// Send event with parameters to all subscribers.

+ 4 - 1
Engine/Script/ScriptFile.cpp

@@ -88,7 +88,10 @@ bool ScriptFile::Load(Deserializer& source)
     if (result < 0)
     {
         LOGERROR("Failed to compile script module " + GetName() + ":\n" + errors);
-        return false;
+        ResourceCache* cache = GetSubsystem<ResourceCache>();
+        // if a script is compiled error we also tell resource cache do not
+        // remove it from resource group if auto reload is true.
+        return cache->GetAutoReloadResources();
     }
     if (!errors.Empty())
         LOGWARNING(errors);

+ 6 - 1
Engine/Script/ScriptInstance.cpp

@@ -357,7 +357,12 @@ void ScriptInstance::ReleaseObject()
         if (methods_[METHOD_STOP])
             scriptFile_->Execute(scriptObject_, methods_[METHOD_STOP]);
         
-        UnsubscribeFromAllEvents();
+        PODVector<StringHash> exceptions;
+        exceptions.Push(E_RELOADSTARTED);
+        exceptions.Push(E_RELOADFINISHED);
+        UnsubscribeFromAllEventsExcept(exceptions);
+        subscribed_ = false;
+        
         ClearMethods();
         
         scriptObject_->SetUserData(0);