Added utility methods to AssetBase:

getAssetDependencyFieldCount
clearAssetDependencyFields
addAssetDependencyField
saveAsset

Updated the saveAsset function for terrain block to utilize utility methods to ensure the terrain asset's material dependencies, so they will load properly.
This commit is contained in:
Areloch 2019-11-07 00:42:55 -06:00
parent 906c7095f1
commit d8cc73f5a1
6 changed files with 120 additions and 100 deletions

View file

@ -284,6 +284,78 @@ void AssetBase::refreshAsset(void)
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
S32 AssetBase::getAssetDependencyFieldCount(const char* pFieldName)
{
S32 matchedFieldCount = 0;
SimFieldDictionary* fieldDictionary = getFieldDictionary();
for (SimFieldDictionaryIterator itr(fieldDictionary); *itr; ++itr)
{
SimFieldDictionary::Entry* entry = *itr;
if (String(entry->slotName).startsWith(pFieldName))
{
matchedFieldCount++;
}
}
return matchedFieldCount;
}
//-----------------------------------------------------------------------------
void AssetBase::clearAssetDependencyFields(const char* pFieldName)
{
SimFieldDictionary* fieldDictionary = getFieldDictionary();
for (SimFieldDictionaryIterator itr(fieldDictionary); *itr; ++itr)
{
SimFieldDictionary::Entry* entry = *itr;
if (String(entry->slotName).startsWith(pFieldName))
{
setDataField(entry->slotName, NULL, "");
}
}
}
//-----------------------------------------------------------------------------
void AssetBase::addAssetDependencyField(const char* pFieldName, const char* pAssetId)
{
U32 existingFieldCount = getAssetDependencyFieldCount(pFieldName);
//we have a match!
char depSlotName[50];
dSprintf(depSlotName, sizeof(depSlotName), "%s%d", pFieldName, existingFieldCount);
char depValue[255];
dSprintf(depValue, sizeof(depValue), "@Asset=%s", pAssetId);
setDataField(StringTable->insert(depSlotName), NULL, StringTable->insert(depValue));
}
//-----------------------------------------------------------------------------
bool AssetBase::saveAsset()
{
// Set the format mode.
Taml taml;
// Yes, so set it.
taml.setFormatMode(Taml::getFormatModeEnum("xml"));
// Turn-off auto-formatting.
taml.setAutoFormat(false);
// Read object.
bool success = taml.write(this, AssetDatabase.getAssetFilePath(getAssetId()));
if (!success)
return false;
return true;
}
//-----------------------------------------------------------------------------
void AssetBase::acquireAssetReference(void) void AssetBase::acquireAssetReference(void)
{ {
// Acquired the acquired reference count. // Acquired the acquired reference count.

View file

@ -104,6 +104,12 @@ public:
void refreshAsset(void); void refreshAsset(void);
S32 getAssetDependencyFieldCount(const char* pFieldName);
void clearAssetDependencyFields(const char* pFieldName);
void addAssetDependencyField(const char* pFieldName, const char* pAssetId);
bool saveAsset();
/// Declare Console Object. /// Declare Console Object.
DECLARE_CONOBJECT(AssetBase); DECLARE_CONOBJECT(AssetBase);

View file

@ -1,4 +1,4 @@
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// Copyright (c) 2013 GarageGames, LLC // Copyright (c) 2013 GarageGames, LLC
// //
// Permission is hereby granted, free of charge, to any person obtaining a copy // Permission is hereby granted, free of charge, to any person obtaining a copy
@ -39,3 +39,31 @@ DefineEngineMethod(AssetBase, getAssetId, String, (), ,
{ {
return object->getAssetId(); return object->getAssetId();
} }
DefineEngineMethod(AssetBase, getAssetDependencyFieldCount, S32, (const char* pFieldName), (""),
"Gets the assets' Asset Id. This is only available if the asset was acquired from the asset manager.\n"
"@return The assets' Asset Id.\n")
{
return object->getAssetDependencyFieldCount(pFieldName);
}
DefineEngineMethod(AssetBase, clearAssetDependencyFields, void, (const char* pFieldName), (""),
"Gets the assets' Asset Id. This is only available if the asset was acquired from the asset manager.\n"
"@return The assets' Asset Id.\n")
{
object->clearAssetDependencyFields(pFieldName);
}
DefineEngineMethod(AssetBase, addAssetDependencyField, void, (const char* pFieldName, const char* pAssetId), ("", ""),
"Gets the assets' Asset Id. This is only available if the asset was acquired from the asset manager.\n"
"@return The assets' Asset Id.\n")
{
object->addAssetDependencyField(pFieldName, pAssetId);
}
DefineEngineMethod(AssetBase, saveAsset, bool, (), ,
"Gets the assets' Asset Id. This is only available if the asset was acquired from the asset manager.\n"
"@return The assets' Asset Id.\n")
{
return object->saveAsset();
}

View file

@ -370,7 +370,7 @@ bool TerrainBlock::setTerrainAsset(const StringTableEntry terrainAssetId)
if (!file) if (!file)
return false; return false;
mFile = file; setFile(file);
return true; return true;
} }
@ -383,25 +383,13 @@ bool TerrainBlock::saveAsset()
{ {
if (!mTerrainAsset.isNull() && mTerrainAsset->isAssetValid()) if (!mTerrainAsset.isNull() && mTerrainAsset->isAssetValid())
{ {
//first, clear out our old dependency references mTerrainAsset->clearAssetDependencyFields("terrainMaterailAsset");
/*SimFieldDictionary* fieldDictionary = mTerrainAsset->getFieldDictionary();
for (SimFieldDictionaryIterator itr(fieldDictionary); *itr; ++itr)
{
SimFieldDictionary::Entry* entry = *itr;
if (String(entry->slotName).startsWith("terrainMaterailAsset"))
{
//got one, so clear it's value
setDataField(entry->slotName, NULL, "");
}
}
AssetQuery* pAssetQuery = new AssetQuery(); AssetQuery* pAssetQuery = new AssetQuery();
AssetDatabase.findAssetType(pAssetQuery, "TerrainMaterialAsset"); AssetDatabase.findAssetType(pAssetQuery, "TerrainMaterialAsset");
TerrainBlock* clientTerr = static_cast<TerrainBlock*>(getClientObject()); TerrainBlock* clientTerr = static_cast<TerrainBlock*>(getClientObject());
U32 terrMatIdx = 0;
for (U32 i = 0; i < pAssetQuery->mAssetList.size(); i++) for (U32 i = 0; i < pAssetQuery->mAssetList.size(); i++)
{ {
//Acquire it so we can check it for matches //Acquire it so we can check it for matches
@ -414,16 +402,7 @@ bool TerrainBlock::saveAsset()
StringTableEntry assetMatDefName = terrMatAsset->getMaterialDefinitionName(); StringTableEntry assetMatDefName = terrMatAsset->getMaterialDefinitionName();
if (assetMatDefName == intMatName) if (assetMatDefName == intMatName)
{ {
//we have a match! mTerrainAsset->addAssetDependencyField("terrainMaterailAsset", terrMatAsset.getAssetId());
char depSlotName[30];
dSprintf(depSlotName, sizeof(depSlotName), "terrainMaterialAsset%d", terrMatIdx);
char depValue[255];
dSprintf(depValue, sizeof(depValue), "@Asset=%s", terrMatAsset.getAssetId());
setDataField(depSlotName, NULL, depValue);
terrMatIdx++;
} }
} }
@ -432,20 +411,10 @@ bool TerrainBlock::saveAsset()
pAssetQuery->destroySelf(); pAssetQuery->destroySelf();
// Set the format mode. bool saveAssetSuccess = mTerrainAsset->saveAsset();
Taml taml;
// Yes, so set it. if (!saveAssetSuccess)
taml.setFormatMode(Taml::getFormatModeEnum("xml")); return false;
// Turn-off auto-formatting.
taml.setAutoFormat(false);
// Read object.
bool success = taml.write(mTerrainAsset, AssetDatabase.getAssetFilePath(mTerrainAsset.getAssetId()));
if (!success)
return false;*/
return mFile->save(mTerrainAsset->getTerrainFilePath()); return mFile->save(mTerrainAsset->getTerrainFilePath());
} }
@ -1251,15 +1220,15 @@ void TerrainBlock::setScale( const VectorF &scale )
void TerrainBlock::initPersistFields() void TerrainBlock::initPersistFields()
{ {
addGroup( "Media" ); addGroup( "Media" );
addProtectedField( "terrainFile", TypeStringFilename, Offset( mTerrFileName, TerrainBlock ),
&TerrainBlock::_setTerrainFile, &defaultProtectedGetFn,
"The source terrain data file." );
addProtectedField("terrainAsset", TypeTerrainAssetPtr, Offset(mTerrainAsset, TerrainBlock), addProtectedField("terrainAsset", TypeTerrainAssetPtr, Offset(mTerrainAsset, TerrainBlock),
&TerrainBlock::_setTerrainAsset, &defaultProtectedGetFn, &TerrainBlock::_setTerrainAsset, &defaultProtectedGetFn,
"The source terrain data asset."); "The source terrain data asset.");
addProtectedField( "terrainFile", TypeStringFilename, Offset( mTerrFileName, TerrainBlock ),
&TerrainBlock::_setTerrainFile, &defaultProtectedGetFn,
"The source terrain data file." );
endGroup( "Media" ); endGroup( "Media" );
addGroup( "Misc" ); addGroup( "Misc" );
@ -1320,7 +1289,6 @@ U32 TerrainBlock::packUpdate(NetConnection* con, U32 mask, BitStream *stream)
if ( stream->writeFlag( mask & FileMask ) ) if ( stream->writeFlag( mask & FileMask ) )
{ {
stream->write( mTerrFileName ); stream->write( mTerrFileName );
stream->writeString( mTerrainAssetId );
stream->write( mCRC ); stream->write( mCRC );
} }
@ -1361,25 +1329,12 @@ void TerrainBlock::unpackUpdate(NetConnection* con, BitStream *stream)
{ {
FileName terrFile; FileName terrFile;
stream->read( &terrFile ); stream->read( &terrFile );
char buffer[256];
stream->readString(buffer);
StringTableEntry terrainAsset = StringTable->insert(buffer);
stream->read( &mCRC ); stream->read( &mCRC );
if (terrainAsset != StringTable->EmptyString()) if ( isProperlyAdded() )
{ setFile( terrFile );
if (isProperlyAdded())
setTerrainAsset(StringTable->insert(terrFile.c_str()));
else
mTerrainAssetId = StringTable->insert(terrFile.c_str());
}
else else
{ mTerrFileName = terrFile;
if (isProperlyAdded())
setFile(terrFile);
else
mTerrFileName = terrFile;
}
} }
if ( stream->readFlag() ) // SizeMask if ( stream->readFlag() ) // SizeMask

View file

@ -83,45 +83,4 @@ function GuiInspectorTypeTerrainAssetPtr::onClick( %this, %fieldName )
function GuiInspectorTypeTerrainAssetPtr::onControlDropped( %this, %payload, %position ) function GuiInspectorTypeTerrainAssetPtr::onControlDropped( %this, %payload, %position )
{ {
}
//AssetDatabase.acquireAsset("pbr:NewTerrain");
function TerrainAsset::saveAsset(%this)
{
%matDepIdx = 0;
while(%this.getFieldValue("terrainMaterialAsset", %matDepIdx) !$= "")
{
%this.setFieldValue("terrainMaterialAsset", "", %matDepIdx);
}
%filePath = AssetDatabase.getAssetFilePath(%this.getAssetId());
%mats = ETerrainEditor.getMaterials();
%assetQuery = new AssetQuery();
AssetDatabase.findAssetType(%assetQuery, "TerrainMaterialAsset");
%count = %assetQuery.getCount();
%matDepIdx = 0;
for( %i = 0; %i < getRecordCount( %mats ); %i++ )
{
%matInternalName = getRecord( %mats, %i );
for(%m=0; %m < %count; %m++)
{
%assetId = %assetQuery.getAsset(%m);
%terrMatAssetDef = AssetDatabase.acquireAsset(%assetId);
if(%terrMatAssetDef.materialDefinitionName $= %matInternalName)
{
%this.setFieldValue("terrainMaterialAsset", "@Asset=" @ %assetId, %matDepIdx);
%matDepIdx++;
}
}
}
%assetQuery.delete();
TAMLWrite(%this, %filePath);
} }