Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Asset Handling Refactor - For 4.2 #1343

Open
wants to merge 15 commits into
base: development
Choose a base branch
from
Prev Previous commit
Next Next commit
various cleanups and fixes
basicClouds refactored
null dereference fixes in guiMenuBar
  • Loading branch information
marauder2k7 committed Jan 23, 2025
commit 4d980e5406161d3c588b4bb9c4907d438d391b31
39 changes: 37 additions & 2 deletions Engine/source/T3D/assets/ImageAsset.h
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,8 @@ public:
AssetPtr<ImageAsset> get##name##Asset(void) { return m##name##Asset; } \
static bool _set##name##Data(void* obj, const char* index, const char* data) { static_cast<className*>(obj)->_set##name(_getStringTable()->insert(data)); return false;}

#define DECLARE_IMAGEASSET_NET_REFACTOR(className, name, profile, mask) \

#define DECLARE_IMAGEASSET_NET_REFACTOR(className, name, profile, mask) \
private: \
AssetPtr<ImageAsset> m##name##Asset; \
public: \
Expand Down Expand Up @@ -647,7 +648,8 @@ public:
inline StringTableEntry _get##name(void) const { return m##name##Asset.getAssetId(); } \
GFXTexHandle get##name() { return m##name##Asset.notNull() ? m##name##Asset->getTexture(&profile) : NULL; } \
AssetPtr<ImageAsset> get##name##Asset(void) { return m##name##Asset; } \
static bool _set##name##Data(void* obj, const char* index, const char* data) { static_cast<className*>(obj)->_set##name(_getStringTable()->insert(data)); return false;}
static bool _set##name##Data(void* obj, const char* index, const char* data) { static_cast<className*>(obj)->_set##name(_getStringTable()->insert(data)); return false;}


#define INITPERSISTFIELD_IMAGEASSET_REFACTOR(name, consoleClass, docs) \
addProtectedField(assetText(name, Asset), TypeImageAssetPtrRefactor, Offset(m##name##Asset, consoleClass), _set##name##Data, &defaultProtectedGetFn, assetDoc(name, asset docs.));
Expand Down Expand Up @@ -683,6 +685,39 @@ public:
AssetPtr<ImageAsset> get##name##Asset(const U32& index) { return m##name##Asset[index]; } \
static bool _set##name##Data(void* obj, const char* index, const char* data) { static_cast<className*>(obj)->_set##name(_getStringTable()->insert(data), dAtoi(index)); return false;}


#define DECLARE_IMAGEASSET_ARRAY_NET_REFACTOR(className, name, profile, max, mask) \
private: \
AssetPtr<ImageAsset> m##name##Asset[max]; \
public: \
void _set##name(StringTableEntry _in, const U32& index){ \
if(m##name##Asset[index].getAssetId() == _in) \
return; \
\
if(!AssetDatabase.isDeclaredAsset(_in)) \
{ \
StringTableEntry imageAssetId = ImageAsset::smNoImageAssetFallback; \
AssetQuery query; \
S32 foundAssetcount = AssetDatabase.findAssetLooseFile(&query, _in); \
if (foundAssetcount != 0) \
{ \
imageAssetId = query.mAssetList[0]; \
} \
m##name##Asset[index] = imageAssetId; \
} \
else \
{ \
m##name##Asset[index] = _in; \
} \
setMaskBits(mask); \
}; \
\
inline StringTableEntry _get##name(const U32& index) const { return m##name##Asset[index].getAssetId(); } \
GFXTexHandle get##name(const U32& index) { return m##name##Asset[index].notNull() ? m##name##Asset[index]->getTexture(&profile) : NULL; } \
AssetPtr<ImageAsset> get##name##Asset(const U32& index) { return m##name##Asset[index]; } \
static bool _set##name##Data(void* obj, const char* index, const char* data) { static_cast<className*>(obj)->_set##name(_getStringTable()->insert(data), dAtoi(index)); return false;}


#define INITPERSISTFIELD_IMAGEASSET_ARRAY_REFACTOR(name, arraySize, consoleClass, docs) \
addProtectedField(assetText(name, Asset), TypeImageAssetPtrRefactor, Offset(m##name##Asset, consoleClass), _set##name##Data, &defaultProtectedGetFn, arraySize, assetDoc(name, asset docs.));

Expand Down
25 changes: 23 additions & 2 deletions Engine/source/T3D/assets/assetMacroHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ if (m##name##AssetId != StringTable->EmptyString())\

//network send - datablock
#define PACKDATA_ASSET_ARRAY_REFACTOR(name, max)\
for (i = 0; i < max; i++)\
for (U32 i = 0; i < max; i++)\
{\
if (stream->writeFlag(m##name##Asset[i].notNull()))\
{\
Expand All @@ -98,14 +98,35 @@ for (i = 0; i < max; i++)\

//network recieve - datablock
#define UNPACKDATA_ASSET_ARRAY_REFACTOR(name, max)\
for (i = 0; i < max; i++)\
for (U32 i = 0; i < max; i++)\
{\
if (stream->readFlag())\
{\
m##name##Asset[i] = stream->readSTString();\
}\
}

//network send - object-instance
#define PACK_ASSET_ARRAY_REFACTOR(netconn, name, max)\
for (U32 i = 0; i < max; i++)\
{\
if (stream->writeFlag(m##name##Asset[i].notNull()))\
{\
NetStringHandle assetIdStr = m##name##Asset[i].getAssetId();\
netconn->packNetStringHandleU(stream, assetIdStr);\
}\
}

//network recieve - object-instance
#define UNPACK_ASSET_ARRAY_REFACTOR(netconn, name, max)\
for (U32 i = 0; i < max; i++)\
{\
if (stream->readFlag())\
{\
m##name##Asset[i] = StringTable->insert(netconn->unpackNetStringHandleU(stream).getString());\
}\
}

#define DEF_ASSET_BINDS_REFACTOR(className,name)\
DefineEngineMethod(className, get##name, StringTableEntry, (), , "get name")\
{\
Expand Down
8 changes: 4 additions & 4 deletions Engine/source/T3D/fx/splash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ void SplashData::packData(BitStream* stream)
stream->writeRangedU32(explosion->getId(), DataBlockObjectIdFirst, DataBlockObjectIdLast);
}

PACKDATA_ASSET_ARRAY_REFACTOR(Texture, NUM_TEX);

S32 i;
for( i=0; i<NUM_EMITTERS; i++ )
{
Expand All @@ -201,8 +203,6 @@ void SplashData::packData(BitStream* stream)
{
stream->write( times[i] );
}

PACKDATA_ASSET_ARRAY_REFACTOR(Texture, NUM_TEX);
}

//--------------------------------------------------------------------------
Expand Down Expand Up @@ -236,6 +236,8 @@ void SplashData::unpackData(BitStream* stream)
explosionId = stream->readRangedU32( DataBlockObjectIdFirst, DataBlockObjectIdLast );
}

UNPACKDATA_ASSET_ARRAY_REFACTOR(Texture, NUM_TEX);

U32 i;
for( i=0; i<NUM_EMITTERS; i++ )
{
Expand All @@ -254,8 +256,6 @@ void SplashData::unpackData(BitStream* stream)
{
stream->read( &times[i] );
}

UNPACKDATA_ASSET_ARRAY_REFACTOR(Texture, NUM_TEX);
}

//--------------------------------------------------------------------------
Expand Down
27 changes: 10 additions & 17 deletions Engine/source/environment/basicClouds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,6 @@ BasicClouds::BasicClouds()
mTexOffset[0].set( 0.5f, 0.5f );
mTexOffset[1].set( 0.5f, 0.5f );
mTexOffset[2].set( 0.5f, 0.5f );

for (U32 i=0; i< TEX_COUNT;i++)
INIT_IMAGEASSET_ARRAY(Texture, GFXStaticTextureSRGBProfile, i);
}

IMPLEMENT_CO_NETOBJECT_V1( BasicClouds );
Expand Down Expand Up @@ -177,7 +174,7 @@ void BasicClouds::initPersistFields()
addField( "layerEnabled", TypeBool, Offset( mLayerEnabled, BasicClouds ), TEX_COUNT,
"Enable or disable rendering of this layer." );

INITPERSISTFIELD_IMAGEASSET_ARRAY(Texture, TEX_COUNT, BasicClouds, "Texture for this layer.");
INITPERSISTFIELD_IMAGEASSET_ARRAY_REFACTOR(Texture, TEX_COUNT, BasicClouds, "Texture for this layer.");

addField( "texScale", TypeF32, Offset( mTexScale, BasicClouds ), TEX_COUNT,
"Texture repeat for this layer." );
Expand Down Expand Up @@ -215,12 +212,11 @@ U32 BasicClouds::packUpdate( NetConnection *conn, U32 mask, BitStream *stream )
{
U32 retMask = Parent::packUpdate( conn, mask, stream );

PACK_ASSET_ARRAY_REFACTOR(conn, Texture, TEX_COUNT)

for ( U32 i = 0; i < TEX_COUNT; i++ )
{
stream->writeFlag( mLayerEnabled[i] );

PACK_ASSET_ARRAY(conn, Texture, i);

stream->write( mTexScale[i] );
mathWrite( *stream, mTexDirection[i] );
stream->write( mTexSpeed[i] );
Expand All @@ -236,12 +232,11 @@ void BasicClouds::unpackUpdate( NetConnection *conn, BitStream *stream )
{
Parent::unpackUpdate( conn, stream );

UNPACK_ASSET_ARRAY_REFACTOR(conn, Texture, TEX_COUNT)

for ( U32 i = 0; i < TEX_COUNT; i++ )
{
mLayerEnabled[i] = stream->readFlag();

UNPACK_ASSET_ARRAY(conn, Texture, i);

stream->read( &mTexScale[i] );
mathRead( *stream, &mTexDirection[i] );
stream->read( &mTexSpeed[i] );
Expand Down Expand Up @@ -315,14 +310,14 @@ void BasicClouds::renderObject( ObjectRenderInst *ri, SceneRenderState *state, B

for ( U32 i = 0; i < TEX_COUNT; i++ )
{
if ( !mLayerEnabled[i] )
if ( !mLayerEnabled[i] || mTextureAsset[i].isNull())
continue;

mShaderConsts->setSafe( mTexScaleSC, mTexScale[i] );
mShaderConsts->setSafe( mTexDirectionSC, mTexDirection[i] * mTexSpeed[i] );
mShaderConsts->setSafe( mTexOffsetSC, mTexOffset[i] );

GFX->setTexture( mDiffuseMapSC->getSamplerRegister(), mTexture[i] );
GFX->setTexture( mDiffuseMapSC->getSamplerRegister(), getTexture(i) );
GFX->setVertexBuffer( mVB[i] );

GFX->drawIndexedPrimitive( GFXTriangleList, 0, 0, smVertCount, 0, smTriangleCount );
Expand All @@ -337,13 +332,11 @@ void BasicClouds::_initTexture()
{
for ( U32 i = 0; i < TEX_COUNT; i++ )
{
if ( !mLayerEnabled[i] )
if ( mLayerEnabled[i] && mTextureAsset[i].notNull())
{
mTexture[i] = NULL;
continue;
// load the resource.
getTexture(i);
}

_setTexture(getTexture(i), i);
}
}

Expand Down
5 changes: 2 additions & 3 deletions Engine/source/environment/basicClouds.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,8 @@ class BasicClouds : public SceneObject
static U32 smVertCount;
static U32 smTriangleCount;

DECLARE_IMAGEASSET_ARRAY(BasicClouds, Texture, TEX_COUNT, onTextureChanged);
DECLARE_IMAGEASSET_ARRAY_NET_SETGET(BasicClouds, Texture, -1);
void onTextureChanged() {}
DECLARE_IMAGEASSET_ARRAY_NET_REFACTOR(BasicClouds, Texture, GFXStaticTextureSRGBProfile, TEX_COUNT, -1)

GFXStateBlockRef mStateblock;

GFXShaderRef mShader;
Expand Down
32 changes: 28 additions & 4 deletions Engine/source/gui/editor/guiMenuBar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -515,10 +515,22 @@ void GuiMenuBar::processTick()

void GuiMenuBar::insert(SimObject* pObject, S32 pos)
{
PopupMenu* menu = dynamic_cast<PopupMenu*>(pObject);
PopupMenu* menu = nullptr;
if (pObject != nullptr)
{
menu = dynamic_cast<PopupMenu*>(pObject);
}

if (menu == nullptr)
{
Con::errorf("GuiMenuBar::insert() - attempted to insert non-popupMenu object: %d", pObject->getId());
if (pObject != nullptr)
{
Con::errorf("GuiMenuBar::insert() - attempted to insert non-popupMenu object: %d", pObject->getId());
}
else
{
Con::errorf("GuiMenuBar::insert() - attempted to insert a nullptr object.");
}
return;
}

Expand All @@ -541,10 +553,22 @@ void GuiMenuBar::insert(SimObject* pObject, S32 pos)

void GuiMenuBar::remove(SimObject* pObject)
{
PopupMenu* menu = dynamic_cast<PopupMenu*>(pObject);
PopupMenu* menu = nullptr;
if (pObject != nullptr)
{
menu = dynamic_cast<PopupMenu*>(pObject);
}

if (menu == nullptr)
{
Con::errorf("GuiMenuBar::remove() - attempted to remove non-popupMenu object: %d", pObject->getId());
if (pObject != nullptr)
{
Con::errorf("GuiMenuBar::insert() - attempted to insert non-popupMenu object: %d", pObject->getId());
}
else
{
Con::errorf("GuiMenuBar::insert() - attempted to insert a nullptr object.");
}
return;
}

Expand Down