SoundAsset Refactor

This commit is contained in:
marauder2k7 2025-12-12 12:27:33 +00:00
parent 9f29bee45f
commit da40838560
61 changed files with 1333 additions and 1828 deletions

View file

@ -59,33 +59,52 @@
IMPLEMENT_CONOBJECT(SoundAsset);
ConsoleType(SoundAssetPtr, TypeSoundAssetPtr, const char*, ASSET_ID_FIELD_PREFIX)
//-----------------------------------------------------------------------------
// REFACTOR
//-----------------------------------------------------------------------------
IMPLEMENT_STRUCT(AssetPtr<SoundAsset>, AssetPtrSoundAsset, , "")
END_IMPLEMENT_STRUCT
ConsoleType(SoundAssetPtr, TypeSoundAssetPtr, AssetPtr<SoundAsset>, ASSET_ID_FIELD_PREFIX)
ConsoleGetType(TypeSoundAssetPtr)
{
// Fetch asset Id.
return *((const char**)(dptr));
return (*((AssetPtr<SoundAsset>*)dptr)).getAssetId();
}
//-----------------------------------------------------------------------------
ConsoleSetType(TypeSoundAssetPtr)
{
// Was a single argument specified?
if (argc == 1)
{
// Yes, so fetch field value.
*((const char**)dptr) = StringTable->insert(argv[0]);
const char* pFieldValue = argv[0];
// Fetch asset pointer.
AssetPtr<SoundAsset>* pAssetPtr = dynamic_cast<AssetPtr<SoundAsset>*>((AssetPtrBase*)(dptr));
// Is the asset pointer the correct type?
if (pAssetPtr == NULL)
{
Con::warnf("(TypeSoundAssetPtrRefactor) - 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.");
Con::warnf("(TypeSoundAssetPtrRefactor) - Cannot set multiple args to a single asset.");
}
//-----------------------------------------------------------------------------
// REFACTOR END
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
ConsoleType(assetIdString, TypeSoundAssetId, const char*, ASSET_ID_FIELD_PREFIX)
@ -122,7 +141,6 @@ const String SoundAsset::mErrCodeStrings[] =
//-----------------------------------------------------------------------------
SoundAsset::SoundAsset()
: AssetBase()
{
dMemset(mPlaylist.mSlots.mReplayMode, 0, sizeof(mPlaylist.mSlots.mReplayMode));
dMemset(mPlaylist.mSlots.mTransitionIn, 0, sizeof(mPlaylist.mSlots.mTransitionIn));
@ -135,6 +153,7 @@ SoundAsset::SoundAsset()
{
mSoundFile[i] = StringTable->EmptyString();
mSoundPath[i] = StringTable->EmptyString();
mSoundResource[i] = NULL;
mPlaylist.mSlots.mTransitionOut[i] = SFXPlayList::TRANSITION_Wait;
mPlaylist.mSlots.mVolumeScale.mValue[i] = 1.f;
@ -173,7 +192,8 @@ SoundAsset::SoundAsset()
mProfileDesc.mPriority = 1.0f;
mProfileDesc.mSourceGroup = NULL;
mProfileDesc.mFadeInEase = EaseF();
mProfileDesc.mReverb = SFXSoundReverbProperties();
mProfileDesc.mSourceGroup = dynamic_cast<SFXSource*>(Sim::findObject("AudioChannelMaster"));
dMemset(mProfileDesc.mParameters, 0, sizeof(mProfileDesc.mParameters));
mIsPlaylist = false;
@ -183,21 +203,13 @@ SoundAsset::SoundAsset()
mPlaylist.mLoopMode = SFXPlayList::LOOP_All;
mPlaylist.mActiveSlots = 1;
mResolvedTrack = NULL;
}
//-----------------------------------------------------------------------------
SoundAsset::~SoundAsset()
{
for (U32 i = 0; i < SFXPlayList::NUM_SLOTS; i++)
{
if(mSFXProfile[i].isProperlyAdded() && !mSFXProfile[i].isDeleted())
mSFXProfile[i].unregisterObject();
}
if (mPlaylist.isProperlyAdded() && !mPlaylist.isDeleted())
mPlaylist.unregisterObject();
}
//-----------------------------------------------------------------------------
@ -323,15 +335,37 @@ void SoundAsset::initPersistFields()
//------------------------------------------------------------------------------
void SoundAsset::onRemove()
{
for (U32 i = 0; i < SFXPlayList::SFXPlaylistSettings::NUM_SLOTS; i++)
{
if (mSoundFile[i] == StringTable->EmptyString())
break;
Torque::FS::RemoveChangeNotification(mSoundFile[i], this, &SoundAsset::_onResourceChanged);
}
Parent::onRemove();
}
void SoundAsset::inspectPostApply()
{
Parent::inspectPostApply();
refreshAsset();
}
void SoundAsset::copyTo(SimObject* object)
{
// Call to parent.
Parent::copyTo(object);
}
void SoundAsset::initializeAsset(void)
{
Parent::initializeAsset();
for (U32 i = 0; i < SFXPlayList::SFXPlaylistSettings::NUM_SLOTS; i++)
{
if (i == 0 && mSoundFile[i] == StringTable->EmptyString())
@ -340,10 +374,12 @@ void SoundAsset::initializeAsset(void)
if (mSoundFile[i] == StringTable->EmptyString())
break;
mSoundPath[i] = getOwned() ? expandAssetFilePath(mSoundFile[i]) : mSoundPath[i];
if (!Torque::FS::IsFile(mSoundPath[i]))
Con::errorf("SoundAsset::initializeAsset (%s)[%d] could not find %s!", getAssetName(), i, mSoundPath[i]);
mSoundFile[i] = expandAssetFilePath(mSoundFile[i]);
if (getOwned())
Torque::FS::AddChangeNotification(mSoundFile[i], this, &SoundAsset::_onResourceChanged);
}
populateSFXTrack();
}
void SoundAsset::_onResourceChanged(const Torque::Path &path)
@ -354,129 +390,44 @@ void SoundAsset::_onResourceChanged(const Torque::Path &path)
if (path != Torque::Path(mSoundPath[i]))
return;
}
refreshAsset();
}
void SoundAsset::onAssetRefresh(void)
{
for (U32 i = 0; i < SFXPlayList::SFXPlaylistSettings::NUM_SLOTS; i++)
{
if (i == 0 && mSoundFile[i] == StringTable->EmptyString())
return;
if (!isProperlyAdded())
return;
if (mSoundFile[i] == StringTable->EmptyString())
break;
mSoundPath[i] = getOwned() ? expandAssetFilePath(mSoundFile[i]) : mSoundPath[i];
}
Parent::onAssetRefresh();
populateSFXTrack();
}
U32 SoundAsset::load()
{
if (mLoadedState == AssetErrCode::Ok) return mLoadedState;
if (mLoadedState == AssetErrCode::Ok)
return mLoadedState;
// find out how many active slots we have.
U32 numSlots = 0;
for (U32 i = 0; i < SFXPlayList::SFXPlaylistSettings::NUM_SLOTS; i++)
if (!mResolvedTrack)
{
if (i == 0 && mSoundPath[i] == StringTable->EmptyString())
return false;
if (mSoundPath[i] == StringTable->EmptyString())
break;
numSlots++;
}
if (mProfileDesc.mSourceGroup == NULL)
mProfileDesc.mSourceGroup = dynamic_cast<SFXSource*>(Sim::findObject("AudioChannelMaster"));
if (numSlots > 1)
{
mIsPlaylist = true;
for (U32 i = 0; i < numSlots; i++)
if (mIsPlaylist)
{
if (mSoundPath[i])
{
if (!Torque::FS::IsFile(mSoundPath[i]))
{
Con::errorf("SoundAsset::initializeAsset: Attempted to load file %s but it was not valid!", mSoundFile[i]);
mLoadedState = BadFileReference;
mSFXProfile[i].setDescription(NULL);
mSFXProfile[i].setSoundFileName(StringTable->insert(StringTable->EmptyString()));
mSFXProfile[i].setPreload(false);
return mLoadedState;
}
else
{
SFXProfile* trackProfile = new SFXProfile();
trackProfile->setDescription(&mProfileDesc);
trackProfile->setSoundFileName(mSoundPath[i]);
trackProfile->setPreload(mPreload);
mSFXProfile[i] = *trackProfile;
mSFXProfile[i].registerObject(String::ToString("%s_profile_track%d", getAssetName()).c_str());
mPlaylist.mSlots.mTrack[i] = trackProfile;
}
}
mResolvedTrack = buildPlaylist();
}
else
{
mResolvedTrack = buildProfile();
}
mPlaylist.setDescription(&mProfileDesc);
mPlaylist.registerObject(String::ToString("%s_playlist", getAssetName()).c_str());
}
else
{
if (mSoundPath[0])
{
if (!Torque::FS::IsFile(mSoundPath[0]))
{
Con::errorf("SoundAsset::initializeAsset: Attempted to load file %s but it was not valid!", mSoundFile[0]);
mLoadedState = BadFileReference;
mSFXProfile[0].setDescription(NULL);
mSFXProfile[0].setSoundFileName(StringTable->insert(StringTable->EmptyString()));
mSFXProfile[0].setPreload(false);
return mLoadedState;
}
else
{
mSFXProfile[0].setDescription(&mProfileDesc);
mSFXProfile[0].setSoundFileName(mSoundPath[0]);
mSFXProfile[0].setPreload(mPreload);
mSFXProfile[0].registerObject(String::ToString("%s_profile", getAssetName()).c_str());
}
}
mResolvedTrack->registerObject(String::ToString("%s_profile_track", getAssetName()).c_str());
}
mChangeSignal.trigger();
mLoadedState = Ok;
return mLoadedState;
}
bool SoundAsset::_setSoundFile(void* object, const char* index, const char* data)
{
SoundAsset* pData = static_cast<SoundAsset*>(object);
U32 id = 0;
if (index)
id = dAtoui(index);
// Update.
pData->mSoundFile[id] = StringTable->insert(data, true);
if (pData->mSoundFile[id] == StringTable->EmptyString())
pData->mSoundPath[id] = StringTable->EmptyString();
// Refresh the asset.
pData->refreshAsset();
return true;
}
StringTableEntry SoundAsset::getAssetIdByFileName(StringTableEntry fileName)
StringTableEntry SoundAsset::getAssetIdByFilename(StringTableEntry fileName)
{
if (fileName == StringTable->EmptyString())
return StringTable->EmptyString();
@ -519,7 +470,7 @@ U32 SoundAsset::getAssetById(StringTableEntry assetId, AssetPtr<SoundAsset>* sou
}
}
U32 SoundAsset::getAssetByFileName(StringTableEntry fileName, AssetPtr<SoundAsset>* soundAsset)
U32 SoundAsset::getAssetByFilename(StringTableEntry fileName, AssetPtr<SoundAsset>* soundAsset)
{
AssetQuery query;
U32 foundAssetcount = AssetDatabase.findAssetType(&query, "SoundAsset");
@ -548,6 +499,123 @@ U32 SoundAsset::getAssetByFileName(StringTableEntry fileName, AssetPtr<SoundAsse
return AssetErrCode::Failed;
}
SFXProfile* SoundAsset::buildProfile()
{
SFXProfile* profile = new SFXProfile(&mProfileDesc, mSoundFile[0], mPreload);
mSoundResource[0] = profile->getResource();
return profile;
}
//-----------------------------------------------------------------------------
// BUILD PLAYLIST
//-----------------------------------------------------------------------------
SFXPlayList* SoundAsset::buildPlaylist()
{
SFXPlayList* pl = new SFXPlayList();
pl->mRandomMode = mPlaylist.mRandomMode;
pl->mLoopMode = mPlaylist.mLoopMode;
pl->mNumSlotsToPlay = mPlaylist.mNumSlotsToPlay;
pl->mTrace = mPlaylist.mTrace;
pl->mSlots = mPlaylist.mSlots;
// Build child tracks for each valid slot
for (U32 i = 0; i < SFXPlayList::SFXPlaylistSettings::NUM_SLOTS; ++i)
{
if (mSoundFile[i] == StringTable->EmptyString())
continue;
// Build child SFXProfile
SFXProfile* child = new SFXProfile(&mProfileDesc, mSoundFile[i], mPreload);
mSoundResource[i] = child->getResource();
pl->mSlots.mTrack[i] = child;
}
return pl;
}
void SoundAsset::populateSFXTrack(void)
{
U32 count = 0;
for (U32 i = 0; i < SFXPlayList::SFXPlaylistSettings::NUM_SLOTS; ++i)
if (mSoundFile[i] != StringTable->EmptyString() && Torque::FS::IsFile(mSoundFile[i]))
++count;
mIsPlaylist = (count > 1);
if (mResolvedTrack)
{
if (mResolvedTrack->isProperlyAdded() && !mResolvedTrack->isDeleted())
{
// Track will be referenced through simobjectptr
mResolvedTrack->deleteObject();
}
}
mResolvedTrack = NULL;
// reset loaded state.
mLoadedState = AssetErrCode::NotLoaded;
}
void SoundAsset::setSoundFile(StringTableEntry pSoundFile, U32 slot)
{
AssertFatal(pSoundFile != NULL, "Cannot use a NULL sound file.");
pSoundFile = StringTable->insert(pSoundFile);
if (pSoundFile == mSoundFile[slot])
return;
mSoundFile[slot] = getOwned() ? expandAssetFilePath(pSoundFile) : StringTable->insert(pSoundFile);;
refreshAsset();
}
void SoundAsset::onTamlPreWrite(void)
{
// Call parent.
Parent::onTamlPreWrite();
for (U32 i = 0; i < SFXPlayList::SFXPlaylistSettings::NUM_SLOTS; i++)
{
if (mSoundFile[i] == StringTable->EmptyString())
break;
mSoundFile[i] = collapseAssetFilePath(mSoundFile[i]);
}
}
void SoundAsset::onTamlPostWrite(void)
{
for (U32 i = 0; i < SFXPlayList::SFXPlaylistSettings::NUM_SLOTS; i++)
{
if (mSoundFile[i] == StringTable->EmptyString())
break;
mSoundFile[i] = expandAssetFilePath(mSoundFile[i]);
}
}
void SoundAsset::onTamlCustomWrite(TamlCustomNodes& customNodes)
{
// Debug Profiling.
PROFILE_SCOPE(SoundAsset_OnTamlCustomWrite);
// Call parent.
Parent::onTamlCustomRead(customNodes);
}
void SoundAsset::onTamlCustomRead(const TamlCustomNodes& customNodes)
{
// Debug Profiling.
PROFILE_SCOPE(SoundAsset_OnTamlCustomRead);
// Call parent.
Parent::onTamlCustomRead(customNodes);
}
DefineEngineMethod(SoundAsset, getSoundPath, const char*, (), , "")
{
return object->getSoundPath();
@ -581,7 +649,7 @@ DefineEngineStaticMethod(SoundAsset, getAssetIdByFilename, const char*, (const c
"Queries the Asset Database to see if any asset exists that is associated with the provided file path.\n"
"@return The AssetId of the associated asset, if any.")
{
return SoundAsset::getAssetIdByFileName(StringTable->insert(filePath));
return SoundAsset::getAssetIdByFilename(StringTable->insert(filePath));
}
IMPLEMENT_CONOBJECT(GuiInspectorTypeSoundAssetPtr);

View file

@ -87,16 +87,7 @@ class SoundAsset : public AssetBase
typedef AssetPtr<SoundAsset> ConcreteAssetPtr;
protected:
StringTableEntry mSoundFile[SFXPlayList::SFXPlaylistSettings::NUM_SLOTS];
StringTableEntry mSoundPath[SFXPlayList::SFXPlaylistSettings::NUM_SLOTS];
SFXProfile mSFXProfile[SFXPlayList::SFXPlaylistSettings::NUM_SLOTS];
SFXDescription mProfileDesc;
SFXPlayList mPlaylist;
// subtitles
StringTableEntry mSubtitleString;
bool mPreload;
bool mIsPlaylist;
//SFXPlayList::SlotData mSlots;
/*These will be needed in the refactor!
@ -121,9 +112,6 @@ protected:
F32 mPriority;
*/
typedef Signal<void()> SoundAssetChanged;
SoundAssetChanged mChangeSignal;
public:
enum SoundAssetErrCode
{
@ -142,179 +130,151 @@ public:
if (errCode > SoundAssetErrCode::Extended) return "undefined error";
return mErrCodeStrings[errCode - Parent::Extended];
};
private:
StringTableEntry mSoundFile[SFXPlayList::SFXPlaylistSettings::NUM_SLOTS];
StringTableEntry mSoundPath[SFXPlayList::SFXPlaylistSettings::NUM_SLOTS];
Resource<SFXResource> mSoundResource[SFXPlayList::SFXPlaylistSettings::NUM_SLOTS];
SFXDescription mProfileDesc;
SFXPlayList mPlaylist;
// subtitles
StringTableEntry mSubtitleString;
bool mPreload;
bool mIsPlaylist;
SFXTrack* mResolvedTrack;
public:
SoundAsset();
virtual ~SoundAsset();
/// Engine.
static void initPersistFields();
void onRemove() override;
void inspectPostApply() override;
void copyTo(SimObject* object) override;
//SFXResource* getSound() { return mSoundResource; }
Resource<SFXResource> getSoundResource(const U32 slotId = 0) { load(); return mSFXProfile[slotId].getResource(); }
Resource<SFXResource> getSoundResource(const U32 slotId = 0) { load(); return mSoundResource[slotId]; }
/// Declare Console Object.
DECLARE_CONOBJECT(SoundAsset);
static bool _setSoundFile(void* object, const char* index, const char* data);
// asset Base load
U32 load() override;
inline StringTableEntry getSoundPath(const U32 slotId = 0) const { return mSoundPath[slotId]; };
SFXProfile* getSfxProfile(const U32 slotId = 0) { return &mSFXProfile[slotId]; }
SFXPlayList* getSfxPlaylist() { return &mPlaylist; }
SFXTrack* getSFXTrack() { load(); return mIsPlaylist ? dynamic_cast<SFXTrack*>(&mPlaylist) : dynamic_cast<SFXTrack*>(&mSFXProfile[0]); }
void setSoundFile(StringTableEntry pSoundFile, U32 slot = 0);
inline StringTableEntry getSoundFile(U32 slot = 0) { return mSoundFile[slot]; }
inline StringTableEntry getRelativeSoundFile(U32 slot = 0) { return collapseAssetFilePath(mSoundFile[slot]); }
inline StringTableEntry getSoundPath(const U32 slot = 0) const { return mSoundPath[slot]; };
SFXTrack* getSFXTrack() { load(); return mResolvedTrack; }
SFXDescription* getSfxDescription() { return &mProfileDesc; }
bool isPlaylist(){ return mIsPlaylist; }
bool isLoop() { return mProfileDesc.mIsLooping; }
bool is3D() { return mProfileDesc.mIs3D; }
static StringTableEntry getAssetIdByFileName(StringTableEntry fileName);
static StringTableEntry getAssetIdByFilename(StringTableEntry fileName);
static U32 getAssetById(StringTableEntry assetId, AssetPtr<SoundAsset>* materialAsset);
static U32 getAssetByFileName(StringTableEntry fileName, AssetPtr<SoundAsset>* matAsset);
static U32 getAssetByFilename(StringTableEntry fileName, AssetPtr<SoundAsset>* matAsset);
SFXProfile* buildProfile();
SFXPlayList* buildPlaylist();
void populateSFXTrack(void);
protected:
void initializeAsset(void) override;
void _onResourceChanged(const Torque::Path & path);
void onAssetRefresh(void) override;
// Asset Base callback
void initializeAsset(void) override;
void _onResourceChanged(const Torque::Path & path);
void onAssetRefresh(void) override;
/// Taml callbacks.
void onTamlPreWrite(void) override;
void onTamlPostWrite(void) override;
void onTamlCustomWrite(TamlCustomNodes& customNodes) override;
void onTamlCustomRead(const TamlCustomNodes& customNodes) override;
protected:
static bool _setSoundFile(void* obj, const char* index, const char* data) { U32 idx = 0; if (index) idx = dAtoi(index); static_cast<SoundAsset*>(obj)->setSoundFile(data, idx); return false; }
};
DefineConsoleType(TypeSoundAssetPtr, SoundAsset)
DECLARE_STRUCT(AssetPtr<SoundAsset>)
DefineConsoleType(TypeSoundAssetPtr, AssetPtr<SoundAsset>)
DefineConsoleType(TypeSoundAssetId, String)
#pragma region Singular Asset Macros
//Singular assets
/// <Summary>
/// Declares a sound asset
/// This establishes the assetId, asset and legacy filepath fields, along with supplemental getter and setter functions
/// </Summary>
#define DECLARE_SOUNDASSET(className, name) public: \
Resource<SFXResource> m##name;\
StringTableEntry m##name##Name; \
StringTableEntry m##name##AssetId;\
AssetPtr<SoundAsset> m##name##Asset = NULL;\
SFXTrack* m##name##Profile = NULL;\
SFXDescription* m##name##Desc = NULL;\
SimObjectId m##name##SFXId = 0;\
public: \
const StringTableEntry get##name##File() const { return m##name##Name; }\
void set##name##File(const FileName &_in) { m##name##Name = StringTable->insert(_in.c_str());}\
const AssetPtr<SoundAsset> & get##name##Asset() const { return m##name##Asset; }\
void set##name##Asset(const AssetPtr<SoundAsset> &_in) { m##name##Asset = _in;}\
\
bool _set##name(StringTableEntry _in)\
{\
if(m##name##AssetId != _in || m##name##Name != _in)\
{\
if (_in == NULL || !String::compare(_in,StringTable->EmptyString()))\
{\
m##name##Name = StringTable->EmptyString();\
m##name##AssetId = StringTable->EmptyString();\
m##name##Asset = NULL;\
m##name = NULL;\
return true;\
}\
\
if (AssetDatabase.isDeclaredAsset(_in))\
{\
m##name##AssetId = _in;\
\
U32 assetState = SoundAsset::getAssetById(m##name##AssetId, &m##name##Asset);\
\
if (SoundAsset::Ok == assetState)\
{\
m##name##Name = StringTable->EmptyString();\
}\
}\
else\
{\
StringTableEntry assetId = SoundAsset::getAssetIdByFileName(_in);\
if (assetId != StringTable->EmptyString())\
{\
m##name##AssetId = assetId;\
if(SoundAsset::getAssetById(m##name##AssetId, &m##name##Asset) == SoundAsset::Ok)\
{\
m##name##Name = StringTable->EmptyString();\
}\
}\
else\
{\
m##name##Name = _in;\
m##name##AssetId = StringTable->EmptyString();\
m##name##Asset = NULL;\
}\
}\
}\
if (get##name() != StringTable->EmptyString() && m##name##Asset.notNull())\
{\
m##name = m##name##Asset->getSoundResource();\
}\
else\
{\
m##name = NULL;\
}\
if(get##name() == StringTable->EmptyString())\
return true;\
\
if(get##name() == StringTable->EmptyString())\
return true;\
if (m##name##Asset.notNull() && m##name##Asset->getStatus() != SoundAsset::Ok)\
{\
Con::errorf("%s(%s)::_set%s() - sound asset failure\"%s\" due to [%s]", macroText(className), getName(), macroText(name), _in, SoundAsset::getAssetErrstrn(m##name##Asset->getStatus()).c_str());\
return false; \
}\
else if (!m##name && (m##name##Name != StringTable->EmptyString() && !Sim::findObject(m##name##Name)))\
{\
Con::errorf("%s(%s)::_set%s() - Couldn't load sound \"%s\"", macroText(className), getName(), macroText(name), _in);\
return false;\
}\
return true;\
}\
\
const StringTableEntry get##name() const\
{\
if (m##name##Asset && (m##name##Asset->getSoundPath() != StringTable->EmptyString()))\
return m##name##Asset->getSoundPath();\
else if (m##name##AssetId != StringTable->EmptyString())\
return m##name##AssetId;\
else if (m##name##Name != StringTable->EmptyString())\
return StringTable->insert(m##name##Name);\
else\
return StringTable->EmptyString();\
}\
Resource<SFXResource> get##name##Resource() \
{\
return m##name;\
}\
SFXTrack* get##name##Profile()\
{\
if (get##name() != StringTable->EmptyString() && m##name##Asset.notNull()){\
m##name##Profile = m##name##Asset->getSFXTrack(); \
return m##name##Profile;\
}\
return NULL;\
}\
#define DECLARE_SOUNDASSET(className, name) \
private: \
AssetPtr<SoundAsset> m##name##Asset; \
StringTableEntry m##name##File = StringTable->EmptyString(); \
public: \
void _set##name(StringTableEntry _in){ \
if(m##name##Asset.getAssetId() == _in) \
return; \
if(get##name##File() == _in) \
return; \
if(_in == NULL || !String::compare(_in,StringTable->EmptyString())) \
{ \
m##name##Asset = NULL; \
m##name##File = ""; \
return; \
} \
if(!AssetDatabase.isDeclaredAsset(_in)) \
{ \
StringTableEntry soundAssetId = StringTable->EmptyString(); \
AssetQuery query; \
S32 foundAssetcount = AssetDatabase.findAssetLooseFile(&query, _in); \
if (foundAssetcount != 0) \
{ \
soundAssetId = query.mAssetList[0]; \
} \
else if(Torque::FS::IsFile(_in)) \
{ \
soundAssetId = SoundAsset::getAssetIdByFilename(_in); \
if (soundAssetId == StringTable->EmptyString()) \
{ \
SoundAsset* privateSound = new SoundAsset(); \
privateSound->setSoundFile(_in); \
soundAssetId = AssetDatabase.addPrivateAsset(privateSound); \
} \
} \
else \
{ \
Con::warnf("%s::%s: Could not find asset for: %s, there is no fallback for sound assets.", #className, #name, _in); \
soundAssetId = StringTable->EmptyString(); \
} \
m##name##Asset = soundAssetId; \
m##name##File = _in; \
} \
else \
{ \
m##name##Asset = _in; \
m##name##File = get##name##File(); \
} \
}; \
\
inline StringTableEntry _get##name(void) const { return m##name##Asset.getAssetId(); } \
AssetPtr<SoundAsset> 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;} \
StringTableEntry get##name##File(){ return m##name##Asset.notNull() ? m##name##Asset->getSoundFile() : ""; } \
SFXDescription* get##name##Description()\
{\
if (get##name() != StringTable->EmptyString() && m##name##Asset.notNull()){\
m##name##Desc = m##name##Asset->getSfxDescription();\
return m##name##Desc;}\
if (m##name##Asset.notNull())\
return m##name##Asset->getSfxDescription();\
return NULL;\
}\
bool is##name##Valid() { return (get##name() != StringTable->EmptyString() && m##name##Asset && m##name##Asset->getStatus() == AssetBase::Ok); }
SimObjectPtr<SFXTrack> get##name##SFXTrack(){ return m##name##Asset.notNull() ? m##name##Asset->getSFXTrack() : NULL; }
#ifdef TORQUE_SHOW_LEGACY_FILE_FIELDS
#define INITPERSISTFIELD_SOUNDASSET(name, consoleClass, docs) \
addProtectedField(assetText(name, File), TypeSoundFilename, Offset(m##name##Name, consoleClass), _set##name##Data, & defaultProtectedGetFn, assetText(name, docs)); \
addProtectedField(assetText(name, Asset), TypeSoundAssetId, Offset(m##name##AssetId, consoleClass), _set##name##Data, & defaultProtectedGetFn, assetText(name, asset reference.));
#else
#define INITPERSISTFIELD_SOUNDASSET(name, consoleClass, docs) \
addProtectedField(assetText(name, File), TypeSoundFilename, Offset(m##name##Name, consoleClass), _set##name##Data, & defaultProtectedGetFn, assetText(name, docs), AbstractClassRep::FIELD_HideInInspectors); \
addProtectedField(assetText(name, Asset), TypeSoundAssetId, Offset(m##name##AssetId, consoleClass), _set##name##Data, & defaultProtectedGetFn, assetText(name, asset reference.));
#endif // TORQUE_SHOW_LEGACY_FILE_FIELDS
#define INITPERSISTFIELD_SOUNDASSET(name, consoleClass, docs) \
addProtectedField(assetText(name, Asset), TypeSoundAssetPtr, Offset(m##name##Asset, consoleClass), _set##name##Data, &defaultProtectedGetFn, assetDoc(name, asset docs.)); \
addProtectedField(assetText(name, File), TypeFilename, Offset(m##name##File, consoleClass), _set##name##Data, &defaultProtectedGetFn, assetDoc(name, file docs.));
//network send - datablock
#define PACKDATA_SOUNDASSET(name)\
@ -374,136 +334,74 @@ public: \
m##name##SFXId[index] = 0;\
}
#define DECLARE_SOUNDASSET_ARRAY(className,name,max) public: \
static const U32 sm##name##Count = max;\
Resource<SFXResource> m##name[max];\
StringTableEntry m##name##Name[max]; \
StringTableEntry m##name##AssetId[max];\
AssetPtr<SoundAsset> m##name##Asset[max];\
SFXTrack* m##name##Profile[max];\
SimObjectId m##name##SFXId[max];\
public: \
const StringTableEntry get##name##File(const U32& index) const { return m##name##Name[index]; }\
void set##name##File(const FileName &_in, const U32& index) { m##name##Name[index] = StringTable->insert(_in.c_str());}\
const AssetPtr<SoundAsset> & get##name##Asset(const U32& index) const { return m##name##Asset[index]; }\
void set##name##Asset(const AssetPtr<SoundAsset> &_in, const U32& index) { m##name##Asset[index] = _in;}\
\
bool _set##name(StringTableEntry _in, const U32& index)\
{\
if(m##name##AssetId[index] != _in || m##name##Name[index] != _in)\
{\
if(index >= sm##name##Count || index < 0) \
return false;\
if (_in == NULL || !String::compare(_in,StringTable->EmptyString()))\
{\
m##name##Name[index] = StringTable->EmptyString();\
m##name##AssetId[index] = StringTable->EmptyString();\
m##name##Asset[index] = NULL;\
m##name[index] = NULL;\
return true;\
}\
else if(_in[0] == '$' || _in[0] == '#')\
{\
m##name##Name[index] = _in;\
m##name##AssetId[index] = StringTable->EmptyString();\
m##name##Asset[index] = NULL;\
m##name[index] = NULL;\
return true;\
}\
\
if (AssetDatabase.isDeclaredAsset(_in))\
{\
m##name##AssetId[index] = _in;\
\
U32 assetState = SoundAsset::getAssetById(m##name##AssetId[index], &m##name##Asset[index]);\
\
if (SoundAsset::Ok == assetState)\
{\
m##name##Name[index] = StringTable->EmptyString();\
}\
}\
else\
{\
StringTableEntry assetId = SoundAsset::getAssetIdByFileName(_in);\
if (assetId != StringTable->EmptyString())\
{\
m##name##AssetId[index] = assetId;\
if(SoundAsset::getAssetById(m##name##AssetId[index], &m##name##Asset[index]) == SoundAsset::Ok)\
{\
m##name##Name[index] = StringTable->EmptyString();\
}\
}\
else\
{\
m##name##Name[index] = _in;\
m##name##AssetId[index] = StringTable->EmptyString();\
m##name##Asset[index] = NULL;\
}\
}\
}\
if (get##name(index) != StringTable->EmptyString() && m##name##Asset[index].notNull())\
{\
m##name[index] = m##name##Asset[index]->getSoundResource();\
}\
else\
{\
m##name[index] = NULL;\
}\
if(get##name(index) == StringTable->EmptyString())\
return true;\
\
if (m##name##Asset[index].notNull() && m##name##Asset[index]->getStatus() != SoundAsset::Ok)\
{\
Con::errorf("%s(%s)::_set%s(%i) - sound asset failure\"%s\" due to [%s]", macroText(className), getName(), macroText(name),index, _in, SoundAsset::getAssetErrstrn(m##name##Asset[index]->getStatus()).c_str());\
return false; \
}\
else if (!m##name[index] && (m##name##Name[index] != StringTable->EmptyString() && !Sim::findObject(m##name##Name[index])))\
{\
Con::errorf("%s(%s)::_set%s(%i) - Couldn't load sound \"%s\"", macroText(className), getName(), macroText(name),index, _in);\
return false;\
}\
return true;\
}\
\
const StringTableEntry get##name(const U32& index) const\
{\
if (m##name##Asset[index] && (m##name##Asset[index]->getSoundPath() != StringTable->EmptyString()))\
return m##name##Asset[index]->getSoundPath();\
else if (m##name##AssetId[index] != StringTable->EmptyString())\
return m##name##AssetId[index];\
else if (m##name##Name[index] != StringTable->EmptyString())\
return StringTable->insert(m##name##Name[index]);\
else\
return StringTable->EmptyString();\
}\
Resource<SFXResource> get##name##Resource(const U32& id) \
{\
if(id >= sm##name##Count || id < 0)\
return ResourceManager::get().load( "" );\
return m##name[id];\
}\
SFXTrack* get##name##Profile(const U32& id)\
{\
if (m##name##Asset[id].notNull())\
return m##name##Asset[id]->getSFXTrack(); \
return NULL;\
}\
bool is##name##Valid(const U32& id) {return (get##name(id) != StringTable->EmptyString() && m##name##Asset[id] && m##name##Asset[id]->getStatus() == AssetBase::Ok); }
#define DECLARE_SOUNDASSET_ARRAY(className,name,max)\
private: \
AssetPtr<SoundAsset> m##name##Asset[max]; \
StringTableEntry m##name##File[max] = {StringTable->EmptyString() }; \
public: \
void _set##name(StringTableEntry _in, const U32& index){ \
if(m##name##Asset[index].getAssetId() == _in) \
return; \
if(get##name##File(index) == _in) \
return; \
if(_in == NULL || !String::compare(_in,StringTable->EmptyString())) \
{ \
m##name##Asset[index] = NULL; \
m##name##File[index] = ""; \
return; \
} \
if(!AssetDatabase.isDeclaredAsset(_in)) \
{ \
StringTableEntry soundAssetId = StringTable->EmptyString(); \
AssetQuery query; \
S32 foundAssetcount = AssetDatabase.findAssetLooseFile(&query, _in); \
if (foundAssetcount != 0) \
{ \
soundAssetId = query.mAssetList[0]; \
} \
else if(Torque::FS::IsFile(_in)) \
{ \
soundAssetId = SoundAsset::getAssetIdByFilename(_in); \
if (soundAssetId == StringTable->EmptyString()) \
{ \
SoundAsset* privateSound = new SoundAsset(); \
privateSound->setSoundFile(_in); \
soundAssetId = AssetDatabase.addPrivateAsset(privateSound); \
} \
} \
else \
{ \
Con::warnf("%s::%s: Could not find asset for: %s, there is no fallback for sound assets.", #className, #name, _in); \
soundAssetId = StringTable->EmptyString(); \
} \
m##name##Asset[index] = soundAssetId; \
m##name##File[index] = _in; \
} \
else \
{ \
m##name##Asset[index] = _in; \
m##name##File[index] = get##name##File(index); \
} \
}; \
\
inline StringTableEntry _get##name(const U32& index) const { return m##name##Asset[index].getAssetId(); } \
AssetPtr<SoundAsset> 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; } \
StringTableEntry get##name##File(const U32& index) { return m##name##Asset[index].notNull() ? m##name##Asset[index]->getSoundFile() : ""; } \
SFXDescription* get##name##Description(const U32& index) \
{ \
if (m##name##Asset[index].notNull()) \
return m##name##Asset[index]->getSfxDescription(); \
return NULL; \
} \
SimObjectPtr<SFXTrack> get##name##SFXTrack(const U32& index) { return m##name##Asset[index].notNull() ? m##name##Asset[index]->getSFXTrack() : NULL; }
#ifdef TORQUE_SHOW_LEGACY_FILE_FIELDS
#define INITPERSISTFIELD_IMAGEASSET_ARRAY(name, arraySize, consoleClass, docs) \
addProtectedField(#name, TypeSoundFilename, Offset(m##name##Name, consoleClass), _set##name##Data, &defaultProtectedGetFn, arraySize, assetDoc(name, docs)); \
addProtectedField(assetText(name, Asset), TypeImageAssetId, Offset(m##name##AssetId, consoleClass), _set##name##Data, &defaultProtectedGetFn, arraySize, assetDoc(name, asset docs.));
#else
#define INITPERSISTFIELD_SOUNDASSET_ARRAY(name, arraySize, consoleClass, docs) \
addProtectedField(#name, TypeSoundFilename, Offset(m##name##Name, consoleClass), _set##name##Data, &defaultProtectedGetFn, arraySize, assetDoc(name, docs), AbstractClassRep::FIELD_HideInInspectors); \
addProtectedField(assetText(name, Asset), TypeSoundAssetId, Offset(m##name##AssetId, consoleClass), _set##name##Data, &defaultProtectedGetFn, arraySize, assetDoc(name, asset docs.));
addProtectedField(#name, TypeSoundFilename, Offset(m##name##File, consoleClass), _set##name##Data, &defaultProtectedGetFn, arraySize, assetDoc(name, docs), AbstractClassRep::FIELD_HideInInspectors); \
addProtectedField(assetText(name, Asset), TypeSoundAssetPtr, Offset(m##name##Asset, consoleClass), _set##name##Data, &defaultProtectedGetFn, arraySize, assetDoc(name, asset docs.));\
addProtectedField(assetText(name, File), TypeSoundFilename, Offset(m##name##File, consoleClass), _set##name##Data, &defaultProtectedGetFn, arraySize, assetDoc(name, docs), AbstractClassRep::FIELD_HideInInspectors); \
#endif
#define LOAD_SOUNDASSET_ARRAY(name, index)\
if (m##name##AssetId[index] != StringTable->EmptyString())\
@ -530,8 +428,8 @@ if (m##name##AssetId[index] != StringTable->EmptyString())\
const char* enumString = castConsoleTypeToString(static_cast<enumType>(itter));\
if (enumString && enumString[0])\
{\
addField(assetEnumNameConcat(enumString, File), TypeSoundFilename, Offset(m##name##Name[0], consoleClass) + sizeof(m##name##Name[0])*i, assetText(name, docs), AbstractClassRep::FIELD_HideInInspectors); \
addField(assetEnumNameConcat(enumString, Asset), TypeSoundAssetId, Offset(m##name##AssetId[0], consoleClass) + sizeof(m##name##AssetId[0])*i, assetText(name, asset reference.));\
addField(assetEnumNameConcat(enumString, Asset), TypeSoundAssetPtr, Offset(m##name##Asset[0], consoleClass) + sizeof(m##name##Asset[0])*i, assetText(name, asset reference.));\
addField(assetEnumNameConcat(enumString, File), TypeSoundFilename, Offset(m##name##File[0], consoleClass) + sizeof(m##name##File[0])*i, assetText(name, docs), AbstractClassRep::FIELD_HideInInspectors); \
}\
}
@ -544,5 +442,11 @@ if (m##name##AssetId[index] != StringTable->EmptyString())\
#pragma endregion
#pragma region Refactor Macros
#pragma endregion
#endif // _ASSET_BASE_H_

View file

@ -3292,30 +3292,19 @@ Torque::Path AssetImporter::importSoundAsset(AssetImportObject* assetItem)
StringTableEntry assetName = StringTable->insert(assetItem->assetName.c_str());
String soundFileName = assetItem->filePath.getFileName() + "." + assetItem->filePath.getExtension();
String assetPath = targetPath + "/" + soundFileName;
String soundFileName = assetItem->filePath.getFullFileName();
String assetPath = "@" + soundFileName;
String tamlPath = targetPath + "/" + assetName + ".asset.taml";
String originalPath = assetItem->filePath.getFullPath().c_str();
char qualifiedFromFile[2048];
char qualifiedToFile[2048];
#ifndef TORQUE_SECURE_VFS
Platform::makeFullPathName(originalPath.c_str(), qualifiedFromFile, sizeof(qualifiedFromFile));
Platform::makeFullPathName(assetPath.c_str(), qualifiedToFile, sizeof(qualifiedToFile));
#else
dStrcpy(qualifiedFromFile, originalPath.c_str(), sizeof(qualifiedFromFile));
dStrcpy(qualifiedToFile, assetPath.c_str(), sizeof(qualifiedToFile));
#endif
newAsset->setAssetName(assetName);
newAsset->_setSoundFile(newAsset, "0", soundFileName.c_str());
newAsset->setSoundFile(assetPath.c_str());
//If it's not a re-import, check that the file isn't being in-place imported. If it isn't, store off the original
//file path for reimporting support later
if (!isReimport && String::compare(qualifiedFromFile, qualifiedToFile) && Torque::FS::IsFile(qualifiedFromFile))
if (!isReimport)
{
newAsset->setDataField(StringTable->insert("originalFilePath"), nullptr, qualifiedFromFile);
newAsset->setDataField(StringTable->insert("originalFilePath"), nullptr, originalPath.c_str());
}
Taml tamlWriter;
@ -3328,18 +3317,6 @@ Torque::Path AssetImporter::importSoundAsset(AssetImportObject* assetItem)
return "";
}
if (!isReimport)
{
bool isInPlace = !String::compare(qualifiedFromFile, qualifiedToFile);
if (!isInPlace && !Torque::FS::CopyFile(qualifiedFromFile, qualifiedToFile, !isReimport))
{
dSprintf(importLogBuffer, sizeof(importLogBuffer), "Error! Unable to copy file %s", assetItem->filePath.getFullPath().c_str());
activityLog.push_back(importLogBuffer);
return "";
}
}
return tamlPath;
}