From 3a4f0eb90deb1405dd3a53654ad097a9d0955393 Mon Sep 17 00:00:00 2001 From: marauder2k7 Date: Mon, 30 Dec 2024 17:46:16 +0000 Subject: [PATCH] sound asset refactor init --- Engine/source/T3D/assets/SoundAsset.cpp | 49 +++++++++++++++++ Engine/source/T3D/assets/SoundAsset.h | 72 +++++++++++++++++++++++++ Engine/source/afx/afxMagicMissile.cpp | 17 +++--- Engine/source/afx/afxMagicMissile.h | 3 +- 4 files changed, 130 insertions(+), 11 deletions(-) diff --git a/Engine/source/T3D/assets/SoundAsset.cpp b/Engine/source/T3D/assets/SoundAsset.cpp index a71c08f5be..bd09f3f5b0 100644 --- a/Engine/source/T3D/assets/SoundAsset.cpp +++ b/Engine/source/T3D/assets/SoundAsset.cpp @@ -110,6 +110,55 @@ ConsoleSetType(TypeSoundAssetId) Con::warnf("(TypeAssetId) - Cannot set multiple args to a single asset."); } +//----------------------------------------------------------------------------- +// REFACTOR +//----------------------------------------------------------------------------- + +IMPLEMENT_STRUCT(AssetPtr, AssetPtrSoundAsset, , "") +END_IMPLEMENT_STRUCT + +ConsoleType(SoundAssetPtr, TypeSoundAssetPtrRefactor, AssetPtr, ASSET_ID_FIELD_PREFIX) + + +ConsoleGetType(TypeSoundAssetPtrRefactor) +{ + // Fetch asset Id. + return (*((AssetPtr*)dptr)).getAssetId(); +} + +ConsoleSetType(TypeSoundAssetPtrRefactor) +{ + // Was a single argument specified? + if (argc == 1) + { + // Yes, so fetch field value. + const char* pFieldValue = argv[0]; + + // Fetch asset pointer. + AssetPtr* pAssetPtr = dynamic_cast*>((AssetPtrBase*)(dptr)); + + // Is the asset pointer the correct type? + if (pAssetPtr == NULL) + { + // No, so fail. + Con::warnf("(TypeSoundAssetPtr) - Failed to set asset Id '%d'.", pFieldValue); + return; + } + + // Set asset. + pAssetPtr->setAssetId(pFieldValue); + + return; + } + + // Warn. + Con::warnf("(TypeSoundAssetPtr) - Cannot set multiple args to a single asset."); +} + +//----------------------------------------------------------------------------- +// REFACTOR END +//----------------------------------------------------------------------------- + const String SoundAsset::mErrCodeStrings[] = { "BadProfile", diff --git a/Engine/source/T3D/assets/SoundAsset.h b/Engine/source/T3D/assets/SoundAsset.h index f867e0e4f9..41a01e75ef 100644 --- a/Engine/source/T3D/assets/SoundAsset.h +++ b/Engine/source/T3D/assets/SoundAsset.h @@ -180,6 +180,9 @@ class SoundAsset : public AssetBase DefineConsoleType(TypeSoundAssetPtr, SoundAsset) DefineConsoleType(TypeSoundAssetId, String) +DECLARE_STRUCT(AssetPtr) +DefineConsoleType(TypeSoundAssetPtrRefactor, AssetPtr) + #pragma region Singular Asset Macros //Singular assets @@ -579,5 +582,74 @@ if (m##name##AssetId[index] != StringTable->EmptyString())\ } #pragma endregion + +#pragma region Refactor Asset Macros + +#define DECLARE_SOUNDASSET_REFACTOR(className, name) \ +private: \ + AssetPtr m##name##Asset; \ +public: \ + void _set##name(StringTableEntry _in) { \ + \ + if (m##name##Asset.getAssetId() == _in) \ + return; \ + \ + if (!AssetDatabase.isDeclaredAsset(_in)) \ + { \ + StringTableEntry imageAssetId = StringTable->EmptyString(); \ + AssetQuery query; \ + S32 foundAssetcount = AssetDatabase.findAssetLooseFile(&query, _in); \ + if (foundAssetcount != 0) \ + { \ + imageAssetId = query.mAssetList[0]; \ + } \ + m##name##Asset = imageAssetId; \ + } \ + else \ + { \ + m##name##Asset = _in; \ + } \ +}; \ + \ +inline StringTableEntry _get##name(void) const { return m##name##Asset.getAssetId(); } \ +AssetPtr get##name##Asset(void) { return m##name##Asset; } \ +static bool _set##name##Data(void* obj, const char* index, const char* data) { static_cast(obj)->_set##name(_getStringTable()->insert(data)); return false; } + +#define DECLARE_SOUNDASSET_NET_REFACTOR(className, name, profile, mask) \ +private: \ + AssetPtr m##name##Asset; \ +public: \ + void _set##name(StringTableEntry _in){ \ + if(m##name##Asset.getAssetId() == _in) \ + return; \ + \ + if(!AssetDatabase.isDeclaredAsset(_in)) \ + { \ + StringTableEntry imageAssetId = StringTable->EmptyString(); \ + AssetQuery query; \ + S32 foundAssetcount = AssetDatabase.findAssetLooseFile(&query, _in); \ + if (foundAssetcount != 0) \ + { \ + imageAssetId = query.mAssetList[0]; \ + } \ + m##name##Asset = imageAssetId; \ + } \ + else \ + { \ + m##name##Asset = _in; \ + } \ + setMaskBits(mask); \ + }; \ + \ + inline StringTableEntry _get##name(void) const { return m##name##Asset.getAssetId(); } \ + AssetPtr get##name##Asset(void) { return m##name##Asset; } \ + static bool _set##name##Data(void* obj, const char* index, const char* data) { static_cast(obj)->_set##name(_getStringTable()->insert(data)); return false;} + + +#define INITPERSISTFIELD_SSOUNDASSET_REFACTOR(name, consoleClass, docs) \ + addProtectedField(assetText(name, Asset), TypeSoundAssetPtrRefactor, Offset(m##name##Asset, consoleClass), _set##name##Data, &defaultProtectedGetFn, assetDoc(name, asset docs.)); + +#pragma endregion + #endif // _ASSET_BASE_H_ diff --git a/Engine/source/afx/afxMagicMissile.cpp b/Engine/source/afx/afxMagicMissile.cpp index e797c2dc1b..adeda26204 100644 --- a/Engine/source/afx/afxMagicMissile.cpp +++ b/Engine/source/afx/afxMagicMissile.cpp @@ -142,7 +142,6 @@ U32 Projectile::smProjectileWarpTicks = 5; afxMagicMissileData::afxMagicMissileData() { INIT_ASSET(ProjectileShape); - INIT_ASSET(ProjectileSound); /* From stock Projectile code... explosion = NULL; @@ -247,7 +246,7 @@ afxMagicMissileData::afxMagicMissileData(const afxMagicMissileData& other, bool { CLONE_ASSET(ProjectileShape); projectileShape = other.projectileShape; // -- TSShape loads using projectileShapeName - CLONE_ASSET(ProjectileSound); + CLONE_ASSET_REFACTOR(ProjectileSound); splash = other.splash; splashId = other.splashId; // -- for pack/unpack of splash ptr lightDesc = other.lightDesc; @@ -345,7 +344,7 @@ void afxMagicMissileData::initPersistFields() endGroup("Particle Effects"); addGroup("Sounds"); - INITPERSISTFIELD_SOUNDASSET(ProjectileSound, afxMagicMissileData, "sound for the projectile"); + INITPERSISTFIELD_SSOUNDASSET_REFACTOR(ProjectileSound, afxMagicMissileData, "sound for the projectile") endGroup("Sounds"); addGroup("Light Emitter"); @@ -517,7 +516,7 @@ bool afxMagicMissileData::preload(bool server, String &errorStr) Con::errorf(ConsoleLogEntry::General, "ProjectileData::preload: Invalid packet, bad datablockId(decal): %d", decalId); */ - if (!isProjectileSoundValid()) + if (mProjectileShapeAsset.isNull()) { //return false; -TODO: trigger asset download } @@ -627,7 +626,7 @@ void afxMagicMissileData::packData(BitStream* stream) DataBlockObjectIdLast); */ - PACKDATA_ASSET(ProjectileSound); + PACKDATA_ASSET_REFACTOR(ProjectileSound) if ( stream->writeFlag(lightDesc != NULL)) stream->writeRangedU32(lightDesc->getId(), DataBlockObjectIdFirst, @@ -733,7 +732,7 @@ void afxMagicMissileData::unpackData(BitStream* stream) decalId = stream->readRangedU32(DataBlockObjectIdFirst, DataBlockObjectIdLast); */ - UNPACKDATA_ASSET(ProjectileSound); + UNPACKDATA_ASSET_REFACTOR(ProjectileSound) if (stream->readFlag()) lightDescId = stream->readRangedU32(DataBlockObjectIdFirst, DataBlockObjectIdLast); @@ -1157,8 +1156,8 @@ bool afxMagicMissile::onNewDataBlock(GameBaseData* dptr, bool reload) SFX_DELETE( mSound ); - if (mDataBlock->getProjectileSound()) - mSound = SFX->createSource(mDataBlock->getProjectileSoundProfile()); + if (mDataBlock->getProjectileSoundAsset().notNull()) + mSound = SFX->createSource(mDataBlock->getProjectileSoundAsset()->getSFXTrack()); } return true; @@ -1993,7 +1992,7 @@ void afxMagicMissile::get_launch_data(Point3F& pos, Point3F& vel) void afxMagicMissile::updateSound() { - if (!mDataBlock->isProjectileSoundValid()) + if (mDataBlock->getProjectileSoundAsset().isNull()) return; if ( mSound ) diff --git a/Engine/source/afx/afxMagicMissile.h b/Engine/source/afx/afxMagicMissile.h index e3f6ee7557..9f0c90227e 100644 --- a/Engine/source/afx/afxMagicMissile.h +++ b/Engine/source/afx/afxMagicMissile.h @@ -126,8 +126,7 @@ class afxMagicMissileData : public GameBaseData SplashData* splash; // Water Splash Datablock S32 splashId; // Water splash ID - DECLARE_SOUNDASSET(afxMagicMissileData, ProjectileSound); - DECLARE_ASSET_SETGET(afxMagicMissileData, ProjectileSound); + DECLARE_SOUNDASSET_REFACTOR(afxMagicMissileData, ProjectileSound) LightDescription *lightDesc; S32 lightDescId;