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

Prevent message box crashes due to corrupted or missing data dir.

Delay factories debug tests after all the subsystems and resource paths are added.
Register MessageBox object factory.
Yao Wei Tjong 姚伟忠 12 жил өмнө
parent
commit
1a6970dcf2

+ 7 - 7
Source/Engine/Engine/Engine.cpp

@@ -157,13 +157,6 @@ bool Engine::Initialize(const VariantMap& parameters)
         RegisterGraphicsLibrary(context_);
     }
 
-    // In debug mode, check now that all factory created objects can be created without crashing
-    #ifdef _DEBUG
-    const HashMap<ShortStringHash, SharedPtr<ObjectFactory> >& factories = context_->GetObjectFactories();
-    for (HashMap<ShortStringHash, SharedPtr<ObjectFactory> >::ConstIterator i = factories.Begin(); i != factories.End(); ++i)
-        SharedPtr<Object> object = i->second_->CreateObject();
-    #endif
-
     // Start logging
     Log* log = GetSubsystem<Log>();
     if (log)
@@ -309,6 +302,13 @@ bool Engine::Initialize(const VariantMap& parameters)
         timeOut_ = GetParameter(parameters, "TimeOut", 0).GetInt() * 1000000LL;
     #endif
 
+    // In debug mode, check now that all factory created objects can be created without crashing
+    #ifdef _DEBUG
+    const HashMap<ShortStringHash, SharedPtr<ObjectFactory> >& factories = context_->GetObjectFactories();
+    for (HashMap<ShortStringHash, SharedPtr<ObjectFactory> >::ConstIterator i = factories.Begin(); i != factories.End(); ++i)
+        SharedPtr<Object> object = i->second_->CreateObject();
+    #endif
+
     frameTimer_.Reset();
 
     initialized_ = true;

+ 18 - 4
Source/Engine/UI/MessageBox.cpp

@@ -37,13 +37,21 @@ namespace Urho3D
 {
 
 MessageBox::MessageBox(Context* context, const String& messageString, const String& titleString, XMLFile* layoutFile, XMLFile* styleFile) :
-    Object(context)
+    Object(context),
+    titleText_(0),
+    messageText_(0),
+    okButton_(0)
 {
     // If layout file is not given, use the default message box layout
     if (!layoutFile)
     {
         ResourceCache* cache = GetSubsystem<ResourceCache>();
         layoutFile = cache->GetResource<XMLFile>("UI/MessageBox.xml");
+        if (!layoutFile)
+        {
+            LOGERROR("Could not find default message box layout file: UI/MessageBox.xml");
+            return;
+        }
     }
 
     UI* ui = GetSubsystem<UI>();
@@ -70,7 +78,9 @@ MessageBox::MessageBox(Context* context, const String& messageString, const Stri
         }
         else
             LOGWARNING("Instantiating a modal window in headless mode!");
+
         window->SetModal(true);
+        SubscribeToEvent(window, E_MODALCHANGED, HANDLER(MessageBox, HandleMessageAcknowledged));
     }
 
     // Bind the buttons (if any in the loaded UI layout) to event handlers
@@ -86,13 +96,17 @@ MessageBox::MessageBox(Context* context, const String& messageString, const Stri
     Button* closeButton = dynamic_cast<Button*>(window_->GetChild("CloseButton", true));
     if (closeButton)
         SubscribeToEvent(closeButton, E_RELEASED, HANDLER(MessageBox, HandleMessageAcknowledged));
-
-    SubscribeToEvent(window_, E_MODALCHANGED, HANDLER(MessageBox, HandleMessageAcknowledged));
 }
 
 MessageBox::~MessageBox()
 {
-    window_->Remove();
+    if (window_)
+        window_->Remove();
+}
+
+void MessageBox::RegisterObject(Context* context)
+{
+    context->RegisterFactory<MessageBox>();
 }
 
 void MessageBox::SetTitle(const String& text)

+ 2 - 0
Source/Engine/UI/MessageBox.h

@@ -42,6 +42,8 @@ public:
     MessageBox(Context* context, const String& messageString = String::EMPTY, const String& titleString = String::EMPTY, XMLFile* layoutFile = 0, XMLFile* styleFile = 0);
     /// Destruct.
     virtual ~MessageBox();
+    /// Register object factory.
+    static void RegisterObject(Context* context);
 
     /// Set title text. No-ops if there is no title text element.
     void SetTitle(const String& text);

+ 2 - 0
Source/Engine/UI/UI.cpp

@@ -36,6 +36,7 @@
 #include "ListView.h"
 #include "Log.h"
 #include "Matrix3x4.h"
+#include "MessageBox.h"
 #include "Profiler.h"
 #include "Renderer.h"
 #include "ResourceCache.h"
@@ -1412,6 +1413,7 @@ void RegisterUILibrary(Context* context)
     Menu::RegisterObject(context);
     DropDownList::RegisterObject(context);
     FileSelector::RegisterObject(context);
+    MessageBox::RegisterObject(context);
     ToolTip::RegisterObject(context);
 }