Browse Source

Added FromString() function to XMLFile.

Lasse Öörni 11 years ago
parent
commit
0d183d6301

+ 2 - 1
Source/Engine/LuaScript/pkgs/Resource/XMLFile.pkg

@@ -5,9 +5,10 @@ class XMLFile : public Resource
     XMLFile();
     ~XMLFile();
 
+    bool FromString(const String source);
     XMLElement CreateRoot(const String name = String::EMPTY);
     XMLElement GetRoot(const String name = String::EMPTY);
-            
+
     void Patch(XMLFile* patchFile);
     void Patch(XMLElement patchElement);
 };

+ 10 - 0
Source/Engine/Resource/XMLFile.cpp

@@ -25,6 +25,7 @@
 #include "Context.h"
 #include "Deserializer.h"
 #include "Log.h"
+#include "MemoryBuffer.h"
 #include "Profiler.h"
 #include "ResourceCache.h"
 #include "Serializer.h"
@@ -146,6 +147,15 @@ XMLElement XMLFile::CreateRoot(const String& name)
     return XMLElement(this, root.internal_object());
 }
 
+bool XMLFile::FromString(const String& source)
+{
+    if (source.Empty())
+        return false;
+    
+    MemoryBuffer buffer(source.CString(), source.Length());
+    return Load(buffer);
+}
+
 XMLElement XMLFile::GetRoot(const String& name)
 {
     pugi::xml_node root = document_->first_child();

+ 2 - 0
Source/Engine/Resource/XMLFile.h

@@ -53,6 +53,8 @@ public:
     /// Save resource. Return true if successful. Only supports saving to a File.
     virtual bool Save(Serializer& dest) const;
     
+    /// Deserialize from a string. Return true if successful.
+    bool FromString(const String& source);
     /// Clear the document and create a root element.
     XMLElement CreateRoot(const String& name);
     

+ 1 - 0
Source/Engine/Script/ResourceAPI.cpp

@@ -481,6 +481,7 @@ static XMLElement XMLFileGetRootDefault(XMLFile* ptr)
 
 static void RegisterXMLFile(asIScriptEngine* engine)
 {
+    engine->RegisterObjectMethod("XMLFile", "bool FromString(const String&in)", asMETHOD(XMLFile, FromString), asCALL_THISCALL);
     engine->RegisterObjectMethod("XMLFile", "XMLElement CreateRoot(const String&in)", asMETHOD(XMLFile, CreateRoot), asCALL_THISCALL);
     engine->RegisterObjectMethod("XMLFile", "XMLElement GetRoot(const String&in name = String())", asMETHOD(XMLFile, GetRoot), asCALL_THISCALL);
     engine->RegisterObjectMethod("XMLFile", "XMLElement get_root()", asFUNCTION(XMLFileGetRootDefault), asCALL_CDECL_OBJLAST);

+ 1 - 4
Source/Samples/Sample.inl

@@ -95,11 +95,8 @@ void Sample::InitTouchInput()
     if (!patchString.Empty())
     {
         // Patch the screen joystick layout further on demand
-        VectorBuffer buffer;
-        buffer.WriteString(patchString);
-        buffer.Seek(0);
         SharedPtr<XMLFile> patchFile(new XMLFile(context_));
-        if (patchFile->Load(buffer))
+        if (patchFile->FromString(patchString))
             layout->Patch(patchFile);
     }
     screenJoystickIndex_ = input->AddScreenJoystick(layout, cache->GetResource<XMLFile>("UI/DefaultStyle.xml"));