Made IPropertyTemplate::CookPreference non-virtual and replaced it with IProperty::ShouldCook

This commit is contained in:
parax0 2016-02-24 07:18:29 -07:00
parent 6e9f2e9037
commit baeb898f22
4 changed files with 71 additions and 82 deletions

View File

@ -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);
}

View File

@ -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();

View File

@ -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 <typename PropType, EPropertyType TypeEnum, class ValueClass>
template <typename ValueType, EPropertyType TypeEnum, class ValueClass>
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<bool, eBoolProperty, CBoolValue> TBoolProperty;
typedef TTypedProperty<char, eByteProperty, CByteValue> TByteProperty;
@ -113,53 +115,48 @@ typedef TTypedProperty<long, eLongProperty, CLongValue>
typedef TTypedProperty<long, eEnumProperty, CLongValue> TEnumProperty;
typedef TTypedProperty<long, eBitfieldProperty, CHexLongValue> TBitfieldProperty;
typedef TTypedProperty<float, eFloatProperty, CFloatValue> TFloatProperty;
typedef TTypedProperty<TString, eStringProperty, CStringValue> TStringProperty;
typedef TTypedProperty<CVector3f, eVector3Property, CVector3Value> TVector3Property;
typedef TTypedProperty<CColor, eColorProperty, CColorValue> TColorProperty;
typedef TTypedProperty<std::vector<u8>, 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<TString, eStringProperty, CStringValue>
{
public:
IMPLEMENT_PROPERTY_CTORS(TStringProperty, TString)
virtual bool MatchesDefault() { return Get().IsEmpty(); }
virtual bool ShouldCook() { return true; }
};
class TFileProperty : public TTypedProperty<CResourceInfo, eFileProperty, CFileValue>
{
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<CAnimationParameters, eCharacterProperty, CCharacterValue>
{
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<std::vector<u8>, eMayaSplineProperty, CMayaSplineValue>
{
public:
TMayaSplineProperty(IPropertyTemplate *pTemp, CPropertyStruct *pParent)
: TTypedProperty(pTemp, pParent) {}
TMayaSplineProperty(IPropertyTemplate *pTemp, CPropertyStruct *pParent, const std::vector<u8>& v)
: TTypedProperty(pTemp, pParent, v) {}
IMPLEMENT_PROPERTY_CTORS(TMayaSplineProperty, std::vector<u8>)
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); }

View File

@ -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<PropType, PropTypeEnum, ValueClass> 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<TString, eStringProperty, CStringValue, false>
@ -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<std::vector<u8>, 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")