| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420 |
- //
- // Copyright (c) 2008-2017 the Urho3D project.
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- //
- #include "../Precompiled.h"
- #include "../Core/Context.h"
- #include "../IO/Deserializer.h"
- #include "../IO/Log.h"
- #include "../Resource/PListFile.h"
- #include "../Resource/XMLFile.h"
- #include <stdio.h>
- #include "../DebugNew.h"
- namespace Atomic
- {
- static PListValue EMPTY_VALUE;
- static PListValueMap EMPTY_VALUEMAP;
- static PListValueVector EMPTY_VALUEVECTOR;
- PListValue::PListValue() :
- type_(PLVT_NONE)
- {
- }
- PListValue::PListValue(int value) :
- type_(PLVT_NONE)
- {
- SetInt(value);
- }
- PListValue::PListValue(bool value) :
- type_(PLVT_NONE)
- {
- SetBool(value);
- }
- PListValue::PListValue(float value) :
- type_(PLVT_NONE)
- {
- SetFloat(value);
- }
- PListValue::PListValue(const String& value) :
- type_(PLVT_NONE)
- {
- SetString(value);
- }
- PListValue::PListValue(PListValueMap& valueMap) :
- type_(PLVT_NONE)
- {
- SetValueMap(valueMap);
- }
- PListValue::PListValue(PListValueVector& valueVector) :
- type_(PLVT_NONE)
- {
- SetValueVector(valueVector);
- }
- PListValue::PListValue(const PListValue& value) :
- type_(PLVT_NONE)
- {
- *this = value;
- }
- PListValue::~PListValue()
- {
- Reset();
- }
- PListValue& PListValue::operator =(const PListValue& rhs)
- {
- switch (rhs.type_)
- {
- case PLVT_NONE:
- Reset();
- break;
- case PLVT_INT:
- SetInt(rhs.int_);
- break;
- case PLVT_BOOL:
- SetBool(rhs.bool_);
- break;
- case PLVT_FLOAT:
- SetFloat(rhs.float_);
- break;
- case PLVT_STRING:
- SetString(*rhs.string_);
- break;
- case PLVT_VALUEMAP:
- SetValueMap(*rhs.valueMap_);
- break;
- case PLVT_VALUEVECTOR:
- SetValueVector(*rhs.valueVector_);
- break;
- }
- return *this;
- }
- void PListValue::SetInt(int value)
- {
- if (type_ != PLVT_INT)
- {
- Reset();
- type_ = PLVT_INT;
- }
- int_ = value;
- }
- void PListValue::SetBool(bool value)
- {
- if (type_ != PLVT_BOOL)
- {
- Reset();
- type_ = PLVT_BOOL;
- }
- bool_ = value;
- }
- void PListValue::SetFloat(float value)
- {
- if (type_ != PLVT_FLOAT)
- {
- Reset();
- type_ = PLVT_FLOAT;
- }
- float_ = value;
- }
- void PListValue::SetString(const String& value)
- {
- if (type_ != PLVT_STRING)
- {
- Reset();
- type_ = PLVT_STRING;
- string_ = new String();
- }
- *string_ = value;
- }
- void PListValue::SetValueMap(const PListValueMap& valueMap)
- {
- if (type_ != PLVT_VALUEMAP)
- {
- Reset();
- type_ = PLVT_VALUEMAP;
- valueMap_ = new PListValueMap();
- }
- *valueMap_ = valueMap;
- }
- void PListValue::SetValueVector(const PListValueVector& valueVector)
- {
- if (type_ != PLVT_VALUEVECTOR)
- {
- Reset();
- type_ = PLVT_VALUEVECTOR;
- valueVector_ = new PListValueVector();
- }
- *valueVector_ = valueVector;
- }
- int PListValue::GetInt() const
- {
- return type_ == PLVT_INT ? int_ : 0;
- }
- bool PListValue::GetBool() const
- {
- return type_ == PLVT_BOOL ? bool_ : false;
- }
- float PListValue::GetFloat() const
- {
- return type_ == PLVT_FLOAT ? float_ : 0.0f;
- }
- const String& PListValue::GetString() const
- {
- return type_ == PLVT_STRING ? *string_ : String::EMPTY;
- }
- IntRect PListValue::GetIntRect() const
- {
- if (type_ != PLVT_STRING)
- return IntRect::ZERO;
- int x, y, w, h;
- sscanf(string_->CString(), "{{%d,%d},{%d,%d}}", &x, &y, &w, &h);
- return IntRect(x, y, x + w, y + h);
- }
- IntVector2 PListValue::GetIntVector2() const
- {
- if (type_ != PLVT_STRING)
- return IntVector2::ZERO;
- int x, y;
- sscanf(string_->CString(), "{%d,%d}", &x, &y);
- return IntVector2(x, y);
- }
- IntVector3 PListValue::GetIntVector3() const
- {
- if (type_ != PLVT_STRING)
- return IntVector3::ZERO;
- int x, y, z;
- sscanf(string_->CString(), "{%d,%d,%d}", &x, &y, &z);
- return IntVector3(x, y, z);
- }
- const PListValueMap& PListValue::GetValueMap() const
- {
- return type_ == PLVT_VALUEMAP ? *valueMap_ : EMPTY_VALUEMAP;
- }
- const PListValueVector& PListValue::GetValueVector() const
- {
- return type_ == PLVT_VALUEVECTOR ? *valueVector_ : EMPTY_VALUEVECTOR;
- }
- PListValueMap& PListValue::ConvertToValueMap()
- {
- if (type_ != PLVT_VALUEMAP)
- {
- Reset();
- type_ = PLVT_VALUEMAP;
- valueMap_ = new PListValueMap();
- }
- return *valueMap_;
- }
- PListValueVector& PListValue::ConvertToValueVector()
- {
- if (type_ != PLVT_VALUEVECTOR)
- {
- Reset();
- type_ = PLVT_VALUEVECTOR;
- valueVector_ = new PListValueVector();
- }
- return *valueVector_;
- }
- void PListValue::Reset()
- {
- if (type_ == PLVT_NONE)
- return;
- switch (type_)
- {
- case PLVT_STRING:
- delete string_;
- break;
- case PLVT_VALUEMAP:
- delete valueMap_;
- break;
- case PLVT_VALUEVECTOR:
- delete valueVector_;
- break;
- default:
- break;
- }
- type_ = PLVT_NONE;
- }
- PListFile::PListFile(Context* context) :
- Resource(context)
- {
- }
- PListFile::~PListFile()
- {
- }
- void PListFile::RegisterObject(Context* context)
- {
- context->RegisterFactory<PListFile>();
- }
- bool PListFile::BeginLoad(Deserializer& source)
- {
- if (GetName().Empty())
- SetName(source.GetName());
- XMLFile xmlFile(context_);
- if (!xmlFile.Load(source))
- {
- ATOMIC_LOGERROR("Could not load property list");
- return false;
- }
- XMLElement plistElem = xmlFile.GetRoot("plist");
- if (!plistElem)
- {
- ATOMIC_LOGERROR("Invalid property list file");
- return false;
- }
- root_.Clear();
- XMLElement dictElem = plistElem.GetChild("dict");
- if (!LoadDict(root_, dictElem))
- return false;
- SetMemoryUse(source.GetSize());
- return true;
- }
- bool PListFile::LoadDict(PListValueMap& dict, const XMLElement& dictElem)
- {
- if (!dictElem)
- return false;
- XMLElement keyElem = dictElem.GetChild("key");
- XMLElement valueElem = keyElem.GetNext();
- while (keyElem && valueElem)
- {
- String key = keyElem.GetValue();
- valueElem = keyElem.GetNext();
- PListValue value;
- if (!LoadValue(value, valueElem))
- return false;
- dict[key] = value;
- keyElem = valueElem.GetNext("key");
- valueElem = keyElem.GetNext();
- }
- return true;
- }
- bool PListFile::LoadArray(PListValueVector& array, const XMLElement& arrayElem)
- {
- if (!arrayElem)
- return false;
- for (XMLElement valueElem = arrayElem.GetChild(); valueElem; valueElem = valueElem.GetNext())
- {
- PListValue value;
- if (!LoadValue(value, valueElem))
- return false;
- array.Push(value);
- }
- return true;
- }
- bool PListFile::LoadValue(PListValue& value, const XMLElement& valueElem)
- {
- String valueType = valueElem.GetName();
- if (valueType == "string")
- value.SetString(valueElem.GetValue());
- else if (valueType == "real")
- value.SetFloat(ToFloat(valueElem.GetValue()));
- else if (valueType == "integer")
- value.SetInt(ToInt(valueElem.GetValue()));
- else if (valueType == "true")
- value.SetBool(true);
- else if (valueType == "false")
- value.SetBool(false);
- else if (valueType == "dict")
- {
- if (!LoadDict(value.ConvertToValueMap(), valueElem))
- return false;
- }
- else if (valueType == "array")
- {
- if (!LoadArray(value.ConvertToValueVector(), valueElem))
- return false;
- }
- else
- {
- ATOMIC_LOGERROR("Supported value type");
- return false;
- }
- return true;
- }
- }
|