From baeb898f22b270efcc90dfba198c5e6f253e6e74 Mon Sep 17 00:00:00 2001 From: parax0 Date: Wed, 24 Feb 2016 07:18:29 -0700 Subject: [PATCH] Made IPropertyTemplate::CookPreference non-virtual and replaced it with IProperty::ShouldCook --- src/Core/Resource/Cooker/CScriptCooker.cpp | 6 +- src/Core/Resource/Script/IProperty.cpp | 32 +++++++++ src/Core/Resource/Script/IProperty.h | 69 ++++++++++---------- src/Core/Resource/Script/IPropertyTemplate.h | 46 +------------ 4 files changed, 71 insertions(+), 82 deletions(-) diff --git a/src/Core/Resource/Cooker/CScriptCooker.cpp b/src/Core/Resource/Cooker/CScriptCooker.cpp index a07be1bb..d97451e4 100644 --- a/src/Core/Resource/Cooker/CScriptCooker.cpp +++ b/src/Core/Resource/Cooker/CScriptCooker.cpp @@ -140,11 +140,9 @@ void CScriptCooker::WriteProperty(IProperty *pProp, bool InSingleStruct) for (u32 iProp = 0; iProp < pStruct->Count(); iProp++) { - IProperty *pSubProp = pStruct->PropertyByIndex(iProp); - ECookPreference Pref = pSubProp->Template()->CookPreference(); - if (Pref == eNeverCook) continue; + IProperty *pSubProp = pStruct->PropertyByIndex(iProp);\ - if (mVersion < eReturns || pTemp->IsSingleProperty() || Pref == eAlwaysCook || !pSubProp->MatchesDefault()) + if (pTemp->IsSingleProperty() || pSubProp->ShouldCook()) PropertiesToWrite.push_back(pSubProp); } diff --git a/src/Core/Resource/Script/IProperty.cpp b/src/Core/Resource/Script/IProperty.cpp index bb9e47d3..c3cb0a51 100644 --- a/src/Core/Resource/Script/IProperty.cpp +++ b/src/Core/Resource/Script/IProperty.cpp @@ -41,6 +41,20 @@ TIDString IProperty::IDString(bool FullPath) const return mpTemplate->IDString(FullPath); } +bool IProperty::ShouldCook() +{ + if (mpTemplate->CookPreference() == eNeverCook) return false; + else if (mpTemplate->CookPreference() == eAlwaysCook) return true; + + else + { + if (mpTemplate->Game() == eReturns) + return !MatchesDefault(); + else + return true; + } +} + bool IProperty::MatchesDefault() { const IPropertyValue *pkValue = RawValue(); @@ -63,6 +77,19 @@ void CPropertyStruct::Copy(const IProperty *pkProp) mProperties[iSub] = pkSource->mProperties[iSub]->Clone(this); } +bool CPropertyStruct::ShouldCook() +{ + if (mpTemplate->CookPreference() == eNeverCook) return false; + + for (u32 iProp = 0; iProp < mProperties.size(); iProp++) + { + if (mProperties[iProp]->ShouldCook()) + return true; + } + + return false; +} + IProperty* CPropertyStruct::PropertyByIndex(u32 index) const { return mProperties[index]; @@ -138,6 +165,11 @@ CPropertyStruct* CPropertyStruct::StructByIDString(const TIDString& rkStr) const } // ************ CArrayProperty ************ +bool CArrayProperty::ShouldCook() +{ + return (mpTemplate->CookPreference() == eNeverCook ? false : true); +} + void CArrayProperty::Resize(int Size) { int OldSize = mProperties.size(); diff --git a/src/Core/Resource/Script/IProperty.h b/src/Core/Resource/Script/IProperty.h index f8b39d7f..e14f6f02 100644 --- a/src/Core/Resource/Script/IProperty.h +++ b/src/Core/Resource/Script/IProperty.h @@ -45,6 +45,9 @@ public: virtual IProperty* Clone(CPropertyStruct *pParent = 0) const = 0; virtual bool Matches(const IProperty *pkProp) const = 0; + virtual bool ShouldCook(); // Can't be const because it calls MatchesDefault() + virtual bool MatchesDefault(); // Can't be const because RawValue() isn't const + inline CPropertyStruct* Parent() const { return mpParent; } inline void SetParent(CPropertyStruct *pParent) { mpParent = pParent; } @@ -56,13 +59,12 @@ public: TString Name() const; u32 ID() const; TIDString IDString(bool FullPath) const; - virtual bool MatchesDefault(); }; /* * TTypedProperty is a template subclass for actual properties. */ -template +template class TTypedProperty : public IProperty { friend class CScriptLoader; @@ -71,7 +73,7 @@ public: TTypedProperty(IPropertyTemplate *pTemp, CPropertyStruct *pParent) : IProperty(pTemp, pParent) {} - TTypedProperty(IPropertyTemplate *pTemp, CPropertyStruct *pParent, PropType v) + TTypedProperty(IPropertyTemplate *pTemp, CPropertyStruct *pParent, ValueType v) : IProperty(pTemp, pParent), mValue(v) {} ~TTypedProperty() {} @@ -103,8 +105,8 @@ public: mValue.Matches(&pkTyped->mValue) ); } - inline PropType Get() const { return mValue.Get(); } - inline void Set(PropType v) { mValue.Set(v); } + inline ValueType Get() const { return mValue.Get(); } + inline void Set(ValueType v) { mValue.Set(v); } }; typedef TTypedProperty TBoolProperty; typedef TTypedProperty TByteProperty; @@ -113,53 +115,48 @@ typedef TTypedProperty typedef TTypedProperty TEnumProperty; typedef TTypedProperty TBitfieldProperty; typedef TTypedProperty TFloatProperty; -typedef TTypedProperty TStringProperty; typedef TTypedProperty TVector3Property; typedef TTypedProperty TColorProperty; typedef TTypedProperty, eUnknownProperty, CUnknownValue> TUnknownProperty; /* - * TFileProperty and TCharacterProperty get little subclasses in order to override MatchesDefault. + * TStringProperty, TFileProperty, and TCharacterProperty get little subclasses in order to override some virtual functions. */ +#define IMPLEMENT_PROPERTY_CTORS(ClassName, ValueType) \ + ClassName(IPropertyTemplate *pTemp, CPropertyStruct *pParent) \ + : TTypedProperty(pTemp, pParent) {} \ + \ + ClassName(IPropertyTemplate *pTemp, CPropertyStruct *pParent, ValueType v) \ + : TTypedProperty(pTemp, pParent, v) {} + +class TStringProperty : public TTypedProperty +{ +public: + IMPLEMENT_PROPERTY_CTORS(TStringProperty, TString) + virtual bool MatchesDefault() { return Get().IsEmpty(); } + virtual bool ShouldCook() { return true; } +}; + class TFileProperty : public TTypedProperty { public: - TFileProperty(IPropertyTemplate *pTemp, CPropertyStruct *pParent) - : TTypedProperty(pTemp, pParent) {} - - TFileProperty(IPropertyTemplate *pTemp, CPropertyStruct *pParent, CResourceInfo v) - : TTypedProperty(pTemp, pParent, v) {} - - virtual bool MatchesDefault() - { - return !Get().IsValid(); - } + IMPLEMENT_PROPERTY_CTORS(TFileProperty, CResourceInfo) + virtual bool MatchesDefault() { return !Get().IsValid(); } + virtual bool ShouldCook() { return true; } }; class TCharacterProperty : public TTypedProperty { public: - TCharacterProperty(IPropertyTemplate *pTemp, CPropertyStruct *pParent) - : TTypedProperty(pTemp, pParent) {} - - TCharacterProperty(IPropertyTemplate *pTemp, CPropertyStruct *pParent, CAnimationParameters v) - : TTypedProperty(pTemp, pParent, v) {} - - virtual bool MatchesDefault() - { - return Get().AnimSet() == nullptr; - } + IMPLEMENT_PROPERTY_CTORS(TCharacterProperty, CAnimationParameters) + virtual bool MatchesDefault() { return Get().AnimSet() == nullptr; } + virtual bool ShouldCook() { return true; } }; class TMayaSplineProperty : public TTypedProperty, eMayaSplineProperty, CMayaSplineValue> { public: - TMayaSplineProperty(IPropertyTemplate *pTemp, CPropertyStruct *pParent) - : TTypedProperty(pTemp, pParent) {} - - TMayaSplineProperty(IPropertyTemplate *pTemp, CPropertyStruct *pParent, const std::vector& v) - : TTypedProperty(pTemp, pParent, v) {} - + IMPLEMENT_PROPERTY_CTORS(TMayaSplineProperty, std::vector) virtual bool MatchesDefault() { return Get().empty(); } }; @@ -175,7 +172,8 @@ public: CPropertyStruct(IPropertyTemplate *pTemp, CPropertyStruct *pParent) : IProperty(pTemp, pParent) {} - ~CPropertyStruct() { + ~CPropertyStruct() + { for (auto it = mProperties.begin(); it != mProperties.end(); it++) delete *it; } @@ -223,6 +221,8 @@ public: return true; } + virtual bool ShouldCook(); + // Inline inline u32 Count() const { return mProperties.size(); } inline void AddSubProperty(IProperty *pProp) { mProperties.push_back(pProp); } @@ -260,6 +260,7 @@ public: } virtual bool MatchesDefault() { return mProperties.empty(); } + virtual bool ShouldCook(); // Inline inline void Reserve(u32 amount) { mProperties.reserve(amount); } diff --git a/src/Core/Resource/Script/IPropertyTemplate.h b/src/Core/Resource/Script/IPropertyTemplate.h index df1166da..8eb947c0 100644 --- a/src/Core/Resource/Script/IPropertyTemplate.h +++ b/src/Core/Resource/Script/IPropertyTemplate.h @@ -92,7 +92,6 @@ public: virtual bool HasValidRange() const { return false; } virtual TString RangeToString() const { return ""; } virtual TString Suffix() const { return ""; } - virtual ECookPreference CookPreference() const { return mCookPreference; } virtual void SetParam(const TString& rkParamName, const TString& rkValue) { @@ -124,6 +123,7 @@ public: inline TString Name() const { return mName; } inline TString Description() const { return mDescription; } inline u32 PropertyID() const { return mID; } + inline ECookPreference CookPreference() const { return mCookPreference; } inline CStructTemplate* Parent() const { return mpParent; } inline CScriptTemplate* ScriptTemplate() const { return mpScriptTemplate; } inline CMasterTemplate* MasterTemplate() const { return mpMasterTemplate; } @@ -167,9 +167,7 @@ public: virtual IProperty* InstantiateProperty(CPropertyStruct *pParent) { typedef TTypedProperty TPropertyType; - - TPropertyType *pOut = new TPropertyType(this, pParent); - pOut->Set(GetDefaultValue()); + TPropertyType *pOut = new TPropertyType(this, pParent, GetDefaultValue()); return pOut; } @@ -327,14 +325,6 @@ public: { return new TCharacterProperty(this, pParent, CAnimationParameters(Game())); } - - ECookPreference CookPreference() const - { - if (mCookPreference != eNoCookPreference) - return mCookPreference; - else - return eAlwaysCook; - } }; class TStringTemplate : public TTypedPropertyTemplate @@ -353,14 +343,6 @@ public: { return new TStringProperty(this, pParent); } - - ECookPreference CookPreference() const - { - if (mCookPreference != eNoCookPreference) - return mCookPreference; - else - return eAlwaysCook; - } }; class TMayaSplineTemplate : public TTypedPropertyTemplate, eMayaSplineProperty, CMayaSplineValue, false> @@ -421,14 +403,6 @@ public: (mAcceptedExtensions == pkFile->mAcceptedExtensions) ); } - ECookPreference CookPreference() const - { - if (mCookPreference != eNoCookPreference) - return mCookPreference; - else - return eAlwaysCook; - } - bool AcceptsExtension(const TString& rkExtension) { for (auto it = mAcceptedExtensions.begin(); it != mAcceptedExtensions.end(); it++) @@ -694,20 +668,12 @@ public: return false; } - virtual ECookPreference CookPreference() const { - if (mCookPreference != eNoCookPreference) return mCookPreference; - bool SubsNeverCook = true; for (u32 iProp = 0; iProp < mSubProperties.size(); iProp++) { IPropertyTemplate *pProp = mSubProperties[iProp]; - ECookPreference Pref = pProp->CookPreference(); - if (Pref != eNeverCook) SubsNeverCook = false; - if (Pref == eAlwaysCook) return eAlwaysCook; } - - return (SubsNeverCook ? eNeverCook : eNoCookPreference); } inline TString SourceFile() const { return mSourceFile; } @@ -771,14 +737,6 @@ public: (CStructTemplate::Matches(pkTemp)) ); } - ECookPreference CookPreference() - { - if (mCookPreference != eNoCookPreference) - return mCookPreference; - else - return eAlwaysCook; - } - void SetParam(const TString& rkParamName, const TString& rkValue) { if (rkParamName == "element_name")