mirror of
https://github.com/TorqueGameEngines/Torque3D.git
synced 2026-01-19 20:24:49 +00:00
Merge pull request #1573 from marauder2k9-torque/Taml-Json
Taml Binary compilation and JSON format
This commit is contained in:
commit
1277b1a347
|
|
@ -115,7 +115,8 @@ torqueAddSourceDirectories("scene" "scene/culling" "scene/zones" "scene/mixin")
|
|||
torqueAddSourceDirectories("math" "math/util")
|
||||
|
||||
# Handle persistence
|
||||
torqueAddSourceDirectories("persistence/taml" "persistence/taml/binary" "persistence/taml/xml")
|
||||
set(TORQUE_INCLUDE_DIRECTORIES ${TORQUE_INCLUDE_DIRECTORIES} "persistence/rapidjson")
|
||||
torqueAddSourceDirectories("persistence/taml" "persistence/taml/binary" "persistence/taml/xml" "persistence/taml/json")
|
||||
|
||||
# Handle Cinterface
|
||||
torqueAddSourceDirectories("cinterface")
|
||||
|
|
|
|||
|
|
@ -964,6 +964,148 @@ bool AssetManager::renameReferencedAsset( const char* pAssetIdFrom, const char*
|
|||
return true;
|
||||
}
|
||||
|
||||
bool AssetManager::compileAllAssets(const bool compressed, const bool includeUnloaded)
|
||||
{
|
||||
// Debug Profiling.
|
||||
PROFILE_SCOPE(AssetManager_CompileAllAssets);
|
||||
|
||||
// Info.
|
||||
if (mEchoInfo)
|
||||
{
|
||||
Con::printSeparator();
|
||||
Con::printf("Asset Manager: Started compiling ALL assets.");
|
||||
}
|
||||
|
||||
Vector<typeAssetId> assetsToRelease;
|
||||
|
||||
// Are we including unloaded assets?
|
||||
if (includeUnloaded)
|
||||
{
|
||||
// Yes, so prepare a list of assets to release and load them.
|
||||
for (typeDeclaredAssetsHash::iterator assetItr = mDeclaredAssets.begin(); assetItr != mDeclaredAssets.end(); ++assetItr)
|
||||
{
|
||||
// Fetch asset Id.
|
||||
typeAssetId assetId = assetItr->key;
|
||||
|
||||
// Skip if asset is loaded.
|
||||
if (assetItr->value->mpAssetBase != NULL)
|
||||
continue;
|
||||
|
||||
// Note asset as needing a release.
|
||||
assetsToRelease.push_back(assetId);
|
||||
|
||||
// Acquire the asset.
|
||||
acquireAsset<AssetBase>(assetId);
|
||||
}
|
||||
}
|
||||
|
||||
bool oldCompressed = mTaml.getBinaryCompression();
|
||||
mTaml.setBinaryCompression(compressed);
|
||||
bool success = false;
|
||||
// Refresh the current loaded assets.
|
||||
// NOTE: This will result in some assets being refreshed more than once due to asset dependencies.
|
||||
for (typeDeclaredAssetsHash::iterator assetItr = mDeclaredAssets.begin(); assetItr != mDeclaredAssets.end(); ++assetItr)
|
||||
{
|
||||
// Skip private assets.
|
||||
if (assetItr->value->mAssetPrivate)
|
||||
continue;
|
||||
|
||||
// Refresh asset if it's loaded.
|
||||
success = compileAsset(assetItr->key);
|
||||
if (!success)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
mTaml.setBinaryCompression(oldCompressed);
|
||||
|
||||
// Are we including unloaded assets?
|
||||
if (includeUnloaded)
|
||||
{
|
||||
// Yes, so release the assets we loaded.
|
||||
for (Vector<typeAssetId>::iterator assetItr = assetsToRelease.begin(); assetItr != assetsToRelease.end(); ++assetItr)
|
||||
{
|
||||
releaseAsset(*assetItr);
|
||||
}
|
||||
}
|
||||
|
||||
// Info.
|
||||
if (mEchoInfo)
|
||||
{
|
||||
Con::printSeparator();
|
||||
Con::printf("Asset Manager: Finished compiling ALL assets.");
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
bool AssetManager::compileModuleAssets(ModuleDefinition* pModuleDefinition)
|
||||
{
|
||||
// Sanity!
|
||||
AssertFatal(pModuleDefinition != NULL, "Cannot remove declared assets using a NULL module definition");
|
||||
|
||||
// Fetch module assets.
|
||||
ModuleDefinition::typeModuleAssetsVector& moduleAssets = pModuleDefinition->getModuleAssets();
|
||||
|
||||
// Remove all module assets.
|
||||
while (moduleAssets.size() > 0)
|
||||
{
|
||||
// Fetch asset definition.
|
||||
AssetDefinition* pAssetDefinition = *moduleAssets.begin();
|
||||
bool success = compileAsset(pAssetDefinition->mAssetId);
|
||||
if (!success)
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool AssetManager::compileAsset(const char* pAssetId)
|
||||
{
|
||||
// Sanity!
|
||||
AssertFatal(pAssetId != NULL, "Cannot compile NULL asset");
|
||||
|
||||
AssetDefinition* pAssetDefinition = findAsset(pAssetId);
|
||||
|
||||
// Does the asset exist?
|
||||
if (pAssetDefinition == NULL)
|
||||
{
|
||||
Con::warnf("Asset Manager::compileAsset Failed to compile asset Id '%s' as it does not exist", pAssetId);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Info.
|
||||
if (mEchoInfo)
|
||||
{
|
||||
Con::printSeparator();
|
||||
Con::printf("Asset Manager::compileAsset Started compiling Asset Id '%s'...", pAssetId);
|
||||
}
|
||||
|
||||
AssetBase* pAssetBase = pAssetDefinition->mpAssetBase;
|
||||
if (pAssetBase != NULL)
|
||||
{
|
||||
Torque::Path binaryPath = pAssetDefinition->mAssetBaseFilePath;
|
||||
binaryPath.setExtension(mTaml.getAutoFormatBinaryExtension());
|
||||
// Save asset.
|
||||
mTaml.write(pAssetBase, binaryPath.getFullPath().c_str());
|
||||
}
|
||||
else
|
||||
{
|
||||
Con::warnf("Asset Manager::compileAsset Failed to compile asset Id '%s' as it does not have an assetBase", pAssetId);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Info.
|
||||
if (mEchoInfo)
|
||||
{
|
||||
Con::printSeparator();
|
||||
Con::printf("Asset Manager: Finished compiling Asset Id '%s'.", pAssetId);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
bool AssetManager::releaseAsset( const char* pAssetId )
|
||||
|
|
@ -2550,133 +2692,148 @@ bool AssetManager::scanDeclaredAssets( const char* pPath, const char* pExtension
|
|||
|
||||
TamlAssetDeclaredVisitor assetDeclaredVisitor;
|
||||
|
||||
// Iterate files.
|
||||
for (S32 i = 0; i < numAssets; ++i)
|
||||
{
|
||||
Torque::Path assetPath = files[i];
|
||||
// Iterate files.
|
||||
for (S32 i = 0; i < numAssets; ++i)
|
||||
{
|
||||
Torque::Path assetPath = files[i];
|
||||
|
||||
Torque::Path compiledPath = assetPath;
|
||||
compiledPath.setExtension(mTaml.getAutoFormatBinaryExtension());
|
||||
|
||||
// Clear declared assets.
|
||||
assetDeclaredVisitor.clear();
|
||||
if (Torque::FS::IsFile(compiledPath))
|
||||
{
|
||||
Torque::FS::FileNodeRef assetFile = Torque::FS::GetFileNode(assetPath);
|
||||
Torque::FS::FileNodeRef compiledFile = Torque::FS::GetFileNode(compiledPath);
|
||||
|
||||
// Format full file-path.
|
||||
char assetFileBuffer[1024];
|
||||
dSprintf( assetFileBuffer, sizeof(assetFileBuffer), "%s/%s", assetPath.getPath().c_str(), assetPath.getFullFileName().c_str());
|
||||
if (assetFile != NULL && compiledFile != NULL)
|
||||
{
|
||||
if (compiledFile->getModifiedTime() >= assetFile->getModifiedTime())
|
||||
assetPath = compiledPath;
|
||||
}
|
||||
}
|
||||
|
||||
// Parse the filename.
|
||||
if ( !mTaml.parse( assetFileBuffer, assetDeclaredVisitor ) )
|
||||
{
|
||||
// Warn.
|
||||
Con::warnf( "Asset Manager: Failed to parse file containing asset declaration: '%s'.", assetFileBuffer );
|
||||
continue;
|
||||
}
|
||||
// Clear declared assets.
|
||||
assetDeclaredVisitor.clear();
|
||||
|
||||
// Fetch asset definition.
|
||||
AssetDefinition& foundAssetDefinition = assetDeclaredVisitor.getAssetDefinition();
|
||||
// Format full file-path.
|
||||
char assetFileBuffer[1024];
|
||||
dSprintf( assetFileBuffer, sizeof(assetFileBuffer), "%s/%s", assetPath.getPath().c_str(), assetPath.getFullFileName().c_str());
|
||||
|
||||
// Did we get an asset name?
|
||||
if ( foundAssetDefinition.mAssetName == StringTable->EmptyString() )
|
||||
{
|
||||
// No, so warn.
|
||||
Con::warnf( "Asset Manager: Parsed file '%s' but did not encounter an asset.", assetFileBuffer );
|
||||
continue;
|
||||
}
|
||||
// Parse the filename.
|
||||
if ( !mTaml.parse( assetFileBuffer, assetDeclaredVisitor ) )
|
||||
{
|
||||
// Warn.
|
||||
Con::warnf( "Asset Manager: Failed to parse file containing asset declaration: '%s'.", assetFileBuffer );
|
||||
continue;
|
||||
}
|
||||
|
||||
// Set module definition.
|
||||
foundAssetDefinition.mpModuleDefinition = pModuleDefinition;
|
||||
// Fetch asset definition.
|
||||
AssetDefinition& foundAssetDefinition = assetDeclaredVisitor.getAssetDefinition();
|
||||
|
||||
// Format asset Id.
|
||||
char assetIdBuffer[1024];
|
||||
dSprintf(assetIdBuffer, sizeof(assetIdBuffer), "%s%s%s",
|
||||
pModuleDefinition->getModuleId(),
|
||||
ASSET_SCOPE_TOKEN,
|
||||
foundAssetDefinition.mAssetName );
|
||||
// Did we get an asset name?
|
||||
if ( foundAssetDefinition.mAssetName == StringTable->EmptyString() )
|
||||
{
|
||||
// No, so warn.
|
||||
Con::warnf( "Asset Manager: Parsed file '%s' but did not encounter an asset.", assetFileBuffer );
|
||||
continue;
|
||||
}
|
||||
|
||||
// Set asset Id.
|
||||
foundAssetDefinition.mAssetId = StringTable->insert( assetIdBuffer );
|
||||
// Set module definition.
|
||||
foundAssetDefinition.mpModuleDefinition = pModuleDefinition;
|
||||
|
||||
// Does this asset already exist?
|
||||
if ( mDeclaredAssets.contains( foundAssetDefinition.mAssetId ) )
|
||||
{
|
||||
// Yes, so warn.
|
||||
Con::warnf( "Asset Manager: Encountered asset Id '%s' in asset file '%s' but it conflicts with existing asset Id in asset file '%s'.",
|
||||
foundAssetDefinition.mAssetId,
|
||||
foundAssetDefinition.mAssetBaseFilePath,
|
||||
mDeclaredAssets.find( foundAssetDefinition.mAssetId )->value->mAssetBaseFilePath );
|
||||
// Format asset Id.
|
||||
char assetIdBuffer[1024];
|
||||
dSprintf(assetIdBuffer, sizeof(assetIdBuffer), "%s%s%s",
|
||||
pModuleDefinition->getModuleId(),
|
||||
ASSET_SCOPE_TOKEN,
|
||||
foundAssetDefinition.mAssetName );
|
||||
|
||||
continue;
|
||||
}
|
||||
// Set asset Id.
|
||||
foundAssetDefinition.mAssetId = StringTable->insert( assetIdBuffer );
|
||||
|
||||
// Create new asset definition.
|
||||
AssetDefinition* pAssetDefinition = new AssetDefinition( foundAssetDefinition );
|
||||
// Does this asset already exist?
|
||||
if ( mDeclaredAssets.contains( foundAssetDefinition.mAssetId ) )
|
||||
{
|
||||
// Yes, so warn.
|
||||
Con::warnf( "Asset Manager: Encountered asset Id '%s' in asset file '%s' but it conflicts with existing asset Id in asset file '%s'.",
|
||||
foundAssetDefinition.mAssetId,
|
||||
foundAssetDefinition.mAssetBaseFilePath,
|
||||
mDeclaredAssets.find( foundAssetDefinition.mAssetId )->value->mAssetBaseFilePath );
|
||||
|
||||
// Store in declared assets.
|
||||
mDeclaredAssets.insert( pAssetDefinition->mAssetId, pAssetDefinition );
|
||||
continue;
|
||||
}
|
||||
|
||||
// Store in module assets.
|
||||
moduleAssets.push_back( pAssetDefinition );
|
||||
// Create new asset definition.
|
||||
AssetDefinition* pAssetDefinition = new AssetDefinition( foundAssetDefinition );
|
||||
|
||||
// Store in declared assets.
|
||||
mDeclaredAssets.insert( pAssetDefinition->mAssetId, pAssetDefinition );
|
||||
|
||||
// Store in module assets.
|
||||
moduleAssets.push_back( pAssetDefinition );
|
||||
|
||||
// Info.
|
||||
if ( mEchoInfo )
|
||||
{
|
||||
Con::printSeparator();
|
||||
Con::printf( "Asset Manager: Adding Asset Id '%s' of type '%s' in asset file '%s'.",
|
||||
pAssetDefinition->mAssetId,
|
||||
pAssetDefinition->mAssetType,
|
||||
pAssetDefinition->mAssetBaseFilePath );
|
||||
}
|
||||
// Info.
|
||||
if ( mEchoInfo )
|
||||
{
|
||||
Con::printSeparator();
|
||||
Con::printf( "Asset Manager: Adding Asset Id '%s' of type '%s' in asset file '%s'.",
|
||||
pAssetDefinition->mAssetId,
|
||||
pAssetDefinition->mAssetType,
|
||||
pAssetDefinition->mAssetBaseFilePath );
|
||||
}
|
||||
|
||||
// Fetch asset Id.
|
||||
StringTableEntry assetId = pAssetDefinition->mAssetId;
|
||||
// Fetch asset Id.
|
||||
StringTableEntry assetId = pAssetDefinition->mAssetId;
|
||||
|
||||
// Fetch asset dependencies.
|
||||
TamlAssetDeclaredVisitor::typeAssetIdVector& assetDependencies = assetDeclaredVisitor.getAssetDependencies();
|
||||
// Fetch asset dependencies.
|
||||
TamlAssetDeclaredVisitor::typeAssetIdVector& assetDependencies = assetDeclaredVisitor.getAssetDependencies();
|
||||
|
||||
// Are there any asset dependencies?
|
||||
if ( assetDependencies.size() > 0 )
|
||||
{
|
||||
// Yes, so iterate dependencies.
|
||||
for( TamlAssetDeclaredVisitor::typeAssetIdVector::iterator assetDependencyItr = assetDependencies.begin(); assetDependencyItr != assetDependencies.end(); ++assetDependencyItr )
|
||||
{
|
||||
// Fetch asset Ids.
|
||||
StringTableEntry dependencyAssetId = *assetDependencyItr;
|
||||
// Are there any asset dependencies?
|
||||
if ( assetDependencies.size() > 0 )
|
||||
{
|
||||
// Yes, so iterate dependencies.
|
||||
for( TamlAssetDeclaredVisitor::typeAssetIdVector::iterator assetDependencyItr = assetDependencies.begin(); assetDependencyItr != assetDependencies.end(); ++assetDependencyItr )
|
||||
{
|
||||
// Fetch asset Ids.
|
||||
StringTableEntry dependencyAssetId = *assetDependencyItr;
|
||||
|
||||
// Insert depends-on.
|
||||
mAssetDependsOn.insertEqual( assetId, dependencyAssetId );
|
||||
// Insert depends-on.
|
||||
mAssetDependsOn.insertEqual( assetId, dependencyAssetId );
|
||||
|
||||
// Insert is-depended-on.
|
||||
mAssetIsDependedOn.insertEqual( dependencyAssetId, assetId );
|
||||
// Insert is-depended-on.
|
||||
mAssetIsDependedOn.insertEqual( dependencyAssetId, assetId );
|
||||
|
||||
// Info.
|
||||
if ( mEchoInfo )
|
||||
{
|
||||
Con::printf( "Asset Manager: Asset Id '%s' has dependency of Asset Id '%s'", assetId, dependencyAssetId );
|
||||
}
|
||||
}
|
||||
}
|
||||
// Info.
|
||||
if ( mEchoInfo )
|
||||
{
|
||||
Con::printf( "Asset Manager: Asset Id '%s' has dependency of Asset Id '%s'", assetId, dependencyAssetId );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Fetch asset loose files.
|
||||
TamlAssetDeclaredVisitor::typeLooseFileVector& assetLooseFiles = assetDeclaredVisitor.getAssetLooseFiles();
|
||||
// Fetch asset loose files.
|
||||
TamlAssetDeclaredVisitor::typeLooseFileVector& assetLooseFiles = assetDeclaredVisitor.getAssetLooseFiles();
|
||||
|
||||
// Are there any loose files?
|
||||
if ( assetLooseFiles.size() > 0 )
|
||||
{
|
||||
// Yes, so iterate loose files.
|
||||
for( TamlAssetDeclaredVisitor::typeLooseFileVector::iterator assetLooseFileItr = assetLooseFiles.begin(); assetLooseFileItr != assetLooseFiles.end(); ++assetLooseFileItr )
|
||||
{
|
||||
// Fetch loose file.
|
||||
StringTableEntry looseFile = *assetLooseFileItr;
|
||||
// Are there any loose files?
|
||||
if ( assetLooseFiles.size() > 0 )
|
||||
{
|
||||
// Yes, so iterate loose files.
|
||||
for( TamlAssetDeclaredVisitor::typeLooseFileVector::iterator assetLooseFileItr = assetLooseFiles.begin(); assetLooseFileItr != assetLooseFiles.end(); ++assetLooseFileItr )
|
||||
{
|
||||
// Fetch loose file.
|
||||
StringTableEntry looseFile = *assetLooseFileItr;
|
||||
|
||||
// Info.
|
||||
if ( mEchoInfo )
|
||||
{
|
||||
Con::printf( "Asset Manager: Asset Id '%s' has loose file '%s'.", assetId, looseFile );
|
||||
}
|
||||
// Info.
|
||||
if ( mEchoInfo )
|
||||
{
|
||||
Con::printf( "Asset Manager: Asset Id '%s' has loose file '%s'.", assetId, looseFile );
|
||||
}
|
||||
|
||||
// Store loose file.
|
||||
pAssetDefinition->mAssetLooseFiles.push_back( looseFile );
|
||||
}
|
||||
}
|
||||
}
|
||||
// Store loose file.
|
||||
pAssetDefinition->mAssetLooseFiles.push_back( looseFile );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Info.
|
||||
if ( mEchoInfo )
|
||||
|
|
|
|||
|
|
@ -151,6 +151,28 @@ public:
|
|||
bool isReferencedAsset( const char* pAssetId );
|
||||
bool renameReferencedAsset( const char* pAssetIdFrom, const char* pAssetIdTo );
|
||||
|
||||
/// <summary>
|
||||
/// Compile all assets.
|
||||
/// </summary>
|
||||
/// <param name="compressed">Do we want binary compression?</param>
|
||||
/// <param name="includeUnloaded">Do we want to include unloaded assets?</param>
|
||||
/// <returns>True if successful, otherwise false.</returns>
|
||||
bool compileAllAssets(const bool compressed = false, const bool includeUnloaded = false);
|
||||
|
||||
/// <summary>
|
||||
/// Compile all assets for a module.
|
||||
/// </summary>
|
||||
/// <param name="pModuleDefinition">The module definition to compile.</param>
|
||||
/// <returns></returns>
|
||||
bool compileModuleAssets(ModuleDefinition* pModuleDefinition);
|
||||
|
||||
/// <summary>
|
||||
/// Compile a single asset to binary.
|
||||
/// </summary>
|
||||
/// <param name="pAssetId">The asset id to be compiled.</param>
|
||||
/// <returns>True if successful, otherwise false.</returns>
|
||||
bool compileAsset(const char* pAssetId);
|
||||
|
||||
/// Public asset acquisition.
|
||||
template<typename T> T* acquireAsset( const char* pAssetId )
|
||||
{
|
||||
|
|
@ -404,4 +426,4 @@ private:
|
|||
|
||||
extern AssetManager AssetDatabase;
|
||||
|
||||
#endif // _ASSET_MANAGER_H_
|
||||
#endif // _ASSET_MANAGER_H_
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------------
|
||||
// Copyright (c) 2013 GarageGames, LLC
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
|
|
@ -20,11 +20,12 @@
|
|||
// IN THE SOFTWARE.
|
||||
//-----------------------------------------------------------------------------
|
||||
#include "console/engineAPI.h"
|
||||
#include "assetBase.h"
|
||||
#include "assetManager.h"
|
||||
#include "assets/assetBase.h"
|
||||
#include "assets/assetManager.h"
|
||||
#include "module/moduleDefinition.h"
|
||||
#include "console/sim.h"
|
||||
|
||||
|
||||
DefineEngineMethod(AssetManager, compileReferencedAssets, bool, (const char* moduleDefinition), (""),
|
||||
"Compile the referenced assets determined by the specified module definition.\n"
|
||||
"@param moduleDefinition The module definition specifies the asset manifest.\n"
|
||||
|
|
@ -853,3 +854,40 @@ DefineEngineMethod(AssetManager, dumpDeclaredAssets, void, (), ,
|
|||
{
|
||||
return object->dumpDeclaredAssets();
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
DefineEngineMethod(AssetManager, compileAllAssets, bool, (bool compressed, bool includeUnloaded),(false, true),
|
||||
"Compile all assets.\n"
|
||||
"@return true on success.\n")
|
||||
{
|
||||
return object->compileAllAssets(compressed, includeUnloaded);
|
||||
}
|
||||
|
||||
DefineEngineMethod(AssetManager, compileModuleAssets, bool, (const char* moduleDefinition), (""),
|
||||
"Compile all assets for a module.\n"
|
||||
"@return true on success.\n")
|
||||
{
|
||||
// Fetch module definition.
|
||||
ModuleDefinition* pModuleDefinition;
|
||||
Sim::findObject(moduleDefinition, pModuleDefinition);
|
||||
|
||||
// Did we find the module definition?
|
||||
if (pModuleDefinition == NULL)
|
||||
{
|
||||
// No, so warn.
|
||||
Con::warnf("AssetManager::compileModuleAssets() - Could not find the module definition '%s'.", moduleDefinition);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Remove declared assets.
|
||||
return object->compileModuleAssets(pModuleDefinition);
|
||||
}
|
||||
|
||||
DefineEngineMethod(AssetManager, compileAsset, bool, (const char* assetId), (""),
|
||||
"Compile a single asset.\n"
|
||||
"@return true on success.\n")
|
||||
{
|
||||
return object->compileAsset(assetId);
|
||||
}
|
||||
|
||||
|
|
|
|||
213
Engine/source/persistence/taml/binary/tamlBinaryParser.cpp
Normal file
213
Engine/source/persistence/taml/binary/tamlBinaryParser.cpp
Normal file
|
|
@ -0,0 +1,213 @@
|
|||
#include "persistence/taml/binary/tamlBinaryParser.h"
|
||||
#include "console/console.h"
|
||||
#include "core/util/zip/zipSubStream.h"
|
||||
#include "platform/profiler.h"
|
||||
#include "persistence/taml/taml.h"
|
||||
|
||||
bool TamlBinaryParser::accept(const char* pFilename, TamlVisitor& visitor)
|
||||
{
|
||||
isRoot = true;
|
||||
PROFILE_SCOPE(TamlBinaryParser_Accept);
|
||||
AssertFatal(pFilename != NULL, "TamlBinaryParser::accept - NULL filename.");
|
||||
|
||||
char filenameBuffer[1024];
|
||||
Con::expandScriptFilename(filenameBuffer, sizeof(filenameBuffer), pFilename);
|
||||
|
||||
FileStream stream;
|
||||
if (!stream.open(filenameBuffer, Torque::FS::File::Read))
|
||||
{
|
||||
Con::warnf("TamlBinaryParser::accept - Could not open file '%s'.", filenameBuffer);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Read TAML signature.
|
||||
StringTableEntry signature = stream.readSTString();
|
||||
if (signature != StringTable->insert(TAML_SIGNATURE))
|
||||
{
|
||||
Con::warnf("TamlBinaryParser::accept - Invalid signature in '%s'.", filenameBuffer);
|
||||
stream.close();
|
||||
return false;
|
||||
}
|
||||
|
||||
setParsingFilename(pFilename);
|
||||
|
||||
U32 versionId;
|
||||
stream.read(&versionId);
|
||||
|
||||
bool compressed;
|
||||
stream.read(&compressed);
|
||||
|
||||
if (compressed)
|
||||
{
|
||||
ZipSubRStream zipStream;
|
||||
zipStream.attachStream(&stream);
|
||||
parseElement(zipStream, visitor, versionId);
|
||||
zipStream.detachStream();
|
||||
}
|
||||
else
|
||||
{
|
||||
parseElement(stream, visitor, versionId);
|
||||
}
|
||||
|
||||
stream.close();
|
||||
|
||||
setParsingFilename(StringTable->EmptyString());
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
bool TamlBinaryParser::parseElement(Stream& stream, TamlVisitor& visitor, const U32 versionId)
|
||||
{
|
||||
PROFILE_SCOPE(TamlBinaryParser_ParseElement);
|
||||
|
||||
// --- Read element header ---
|
||||
StringTableEntry pElementName = stream.readSTString();
|
||||
StringTableEntry pObjectName = stream.readSTString();
|
||||
|
||||
// Read references.
|
||||
U32 refId, refToId;
|
||||
stream.read(&refId);
|
||||
stream.read(&refToId);
|
||||
|
||||
// If this is a reference to another object, skip it.
|
||||
if (refToId != 0)
|
||||
return true;
|
||||
|
||||
// Create a property visitor state.
|
||||
TamlVisitor::PropertyState propertyState;
|
||||
|
||||
propertyState.setObjectName(pElementName, isRoot);
|
||||
if (pObjectName != StringTable->EmptyString())
|
||||
propertyState.setProperty("Name", pObjectName);
|
||||
|
||||
if(isRoot)
|
||||
isRoot = false;
|
||||
|
||||
// --- Attributes ---
|
||||
parseAttributes(stream, visitor, versionId, propertyState);
|
||||
// --- Children ---
|
||||
parseChildren(stream, visitor, versionId);
|
||||
// --- Custom elements ---
|
||||
parseCustomElements(stream, visitor, versionId);
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void TamlBinaryParser::parseAttributes(Stream& stream, TamlVisitor& visitor, const U32 versionId, TamlVisitor::PropertyState& state)
|
||||
{
|
||||
PROFILE_SCOPE(TamlBinaryParser_ParseAttributes);
|
||||
|
||||
U32 attributeCount;
|
||||
stream.read(&attributeCount);
|
||||
if (attributeCount == 0)
|
||||
return;
|
||||
|
||||
char valueBuffer[4096];
|
||||
for (U32 i = 0; i < attributeCount; ++i)
|
||||
{
|
||||
StringTableEntry attrName = stream.readSTString();
|
||||
stream.readLongString(4096, valueBuffer);
|
||||
state.setProperty(attrName, valueBuffer);
|
||||
const bool visitStatus = visitor.visit(*this, state);
|
||||
if (!visitStatus)
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
void TamlBinaryParser::parseChildren(Stream& stream, TamlVisitor& visitor, const U32 versionId)
|
||||
{
|
||||
PROFILE_SCOPE(TamlBinaryParser_ParseChildren);
|
||||
|
||||
U32 childCount = 0;
|
||||
stream.read(&childCount);
|
||||
if (childCount == 0)
|
||||
return;
|
||||
|
||||
for (U32 i = 0; i < childCount; ++i)
|
||||
{
|
||||
parseElement(stream, visitor, versionId);
|
||||
}
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
void TamlBinaryParser::parseCustomElements(Stream& stream, TamlVisitor& visitor, const U32 versionId)
|
||||
{
|
||||
PROFILE_SCOPE(TamlBinaryParser_ParseCustomElements);
|
||||
|
||||
U32 customNodeCount = 0;
|
||||
stream.read(&customNodeCount);
|
||||
if (customNodeCount == 0)
|
||||
return;
|
||||
|
||||
TamlVisitor::PropertyState state;
|
||||
|
||||
for (U32 nodeIndex = 0; nodeIndex < customNodeCount; ++nodeIndex)
|
||||
{
|
||||
StringTableEntry nodeName = stream.readSTString();
|
||||
state.setObjectName(nodeName, false);
|
||||
|
||||
U32 nodeChildrenCount = 0;
|
||||
stream.read(&nodeChildrenCount);
|
||||
if (nodeChildrenCount == 0)
|
||||
return;
|
||||
|
||||
for(U32 nodeChild = 0; nodeChild < nodeChildrenCount; ++nodeChild)
|
||||
parseCustomNode(stream, visitor, versionId, state);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
bool TamlBinaryParser::parseCustomNode(Stream& stream, TamlVisitor& visitor, const U32 versionId, TamlVisitor::PropertyState& state)
|
||||
{
|
||||
PROFILE_SCOPE(TamlBinaryParser_ParseCustomNode);
|
||||
|
||||
bool isProxyObject;
|
||||
stream.read(&isProxyObject);
|
||||
if (isProxyObject)
|
||||
{
|
||||
// Parse nested proxy element.
|
||||
return parseElement(stream, visitor, versionId);
|
||||
}
|
||||
|
||||
StringTableEntry nodeName = stream.readSTString();
|
||||
|
||||
char nodeValue[MAX_TAML_NODE_FIELDVALUE_LENGTH];
|
||||
|
||||
stream.readLongString(MAX_TAML_NODE_FIELDVALUE_LENGTH, nodeValue);
|
||||
|
||||
U32 childCount;
|
||||
stream.read(&childCount);
|
||||
for (U32 i = 0; i < childCount; ++i)
|
||||
{
|
||||
if (!parseCustomNode(stream, visitor, versionId, state))
|
||||
return false;
|
||||
}
|
||||
|
||||
U32 fieldCount;
|
||||
stream.read(&fieldCount);
|
||||
|
||||
if (fieldCount > 0)
|
||||
{
|
||||
char valueBuffer[MAX_TAML_NODE_FIELDVALUE_LENGTH];
|
||||
for (U32 f = 0; f < fieldCount; ++f)
|
||||
{
|
||||
StringTableEntry fieldName = stream.readSTString();
|
||||
stream.readLongString(MAX_TAML_NODE_FIELDVALUE_LENGTH, valueBuffer);
|
||||
state.setProperty(fieldName, valueBuffer);
|
||||
const bool visitStatus = visitor.visit(*this, state);
|
||||
if (!visitStatus)
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
42
Engine/source/persistence/taml/binary/tamlBinaryParser.h
Normal file
42
Engine/source/persistence/taml/binary/tamlBinaryParser.h
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
#pragma once
|
||||
#ifndef _TAML_BINARYPARSER_H_
|
||||
|
||||
#ifndef _TAML_PARSER_H_
|
||||
#include "persistence/taml/tamlParser.h"
|
||||
#endif
|
||||
|
||||
#ifndef _TAML_VISITOR_H_
|
||||
#include "persistence/taml/tamlVisitor.h"
|
||||
#endif
|
||||
|
||||
#ifndef _STREAM_H_
|
||||
#include "core/stream/stream.h"
|
||||
#endif
|
||||
|
||||
#ifndef _FILESTREAM_H_
|
||||
#include "core/stream/fileStream.h"
|
||||
#endif
|
||||
|
||||
class TamlBinaryParser : public TamlParser
|
||||
{
|
||||
public:
|
||||
TamlBinaryParser() {}
|
||||
virtual ~TamlBinaryParser() {}
|
||||
|
||||
/// Whether the parser can change a property or not.
|
||||
bool canChangeProperty(void) override { return false; }
|
||||
|
||||
/// Accept visitor.
|
||||
bool accept(const char* pFilename, TamlVisitor& visitor) override;
|
||||
|
||||
private:
|
||||
bool parseElement(Stream& stream, TamlVisitor& visitor, const U32 versionId);
|
||||
void parseAttributes(Stream& stream, TamlVisitor& visitor, const U32 versionId, TamlVisitor::PropertyState& state);
|
||||
void parseChildren(Stream& stream, TamlVisitor& visitor, const U32 versionId);
|
||||
void parseCustomElements(Stream& stream, TamlVisitor& visitor, const U32 versionId);
|
||||
bool parseCustomNode(Stream& stream, TamlVisitor& visitor, const U32 versionId, TamlVisitor::PropertyState& state);
|
||||
|
||||
bool isRoot;
|
||||
};
|
||||
|
||||
#endif // !_TAML_BINARYPARSER_H_
|
||||
|
|
@ -345,8 +345,14 @@ void TamlBinaryReader::parseCustomElements( Stream& stream, TamlCallbacks* pCall
|
|||
// Add custom node.
|
||||
TamlCustomNode* pCustomNode = customNodes.addNode( nodeName );
|
||||
|
||||
U32 nodeChildrenCount = 0;
|
||||
stream.read(&nodeChildrenCount);
|
||||
if (nodeChildrenCount == 0)
|
||||
return;
|
||||
|
||||
// Parse the custom node.
|
||||
parseCustomNode( stream, pCustomNode, versionId );
|
||||
for (U32 nodeChild = 0; nodeChild < nodeChildrenCount; ++nodeChild)
|
||||
parseCustomNode( stream, pCustomNode, versionId );
|
||||
}
|
||||
|
||||
// Do we have callbacks?
|
||||
|
|
@ -428,4 +434,4 @@ void TamlBinaryReader::parseCustomNode( Stream& stream, TamlCustomNode* pCustomN
|
|||
pChildNode->addField( fieldName, valueBuffer );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -212,6 +212,8 @@ void TamlBinaryWriter::writeCustomElements( Stream& stream, const TamlWriteNode*
|
|||
// Fetch node children.
|
||||
const TamlCustomNodeVector& nodeChildren = pCustomNode->getChildren();
|
||||
|
||||
stream.write((U32)nodeChildren.size());
|
||||
|
||||
// Iterate children nodes.
|
||||
for( TamlCustomNodeVector::const_iterator childNodeItr = nodeChildren.begin(); childNodeItr != nodeChildren.end(); ++childNodeItr )
|
||||
{
|
||||
|
|
@ -294,4 +296,4 @@ void TamlBinaryWriter::writeCustomNode( Stream& stream, const TamlCustomNode* pC
|
|||
stream.writeLongString( MAX_TAML_NODE_FIELDVALUE_LENGTH, pField->getFieldValue() );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
|
|
@ -226,7 +226,7 @@ DefineEngineMethod(Taml, read, SimObject*, (const char* filename), , "(filena
|
|||
//-----------------------------------------------------------------------------
|
||||
|
||||
DefineEngineFunction(TamlWrite, bool, (SimObject* simObject, const char* filename, const char* format, bool compressed),
|
||||
("xml", true),
|
||||
("xml", false),
|
||||
"(object, filename, [format], [compressed]) - Writes an object to a file using Taml.\n"
|
||||
"@param object The object to write.\n"
|
||||
"@param filename The filename to write to.\n"
|
||||
|
|
@ -235,37 +235,30 @@ DefineEngineFunction(TamlWrite, bool, (SimObject* simObject, const char* filenam
|
|||
"@return Whether the write was successful or not.")
|
||||
{
|
||||
|
||||
// Did we find the object?
|
||||
if ( simObject == NULL )
|
||||
{
|
||||
// No, so warn.
|
||||
//Con::warnf( "TamlWrite() - Could not find object '%s' to write to file '%s'.", simObject->getIdString(), filename );
|
||||
Con::warnf( "TamlWrite() - Could not find object to write to file '%s'.", filename );
|
||||
return false;
|
||||
}
|
||||
|
||||
Taml taml;
|
||||
|
||||
taml.setFormatMode( Taml::getFormatModeEnum(format) );
|
||||
|
||||
// Yes, so is the format mode binary?
|
||||
if ( taml.getFormatMode() == Taml::BinaryFormat )
|
||||
// Did we find the object?
|
||||
if ( simObject == NULL )
|
||||
{
|
||||
// Yes, so set binary compression.
|
||||
taml.setBinaryCompression( compressed );
|
||||
}
|
||||
else
|
||||
{
|
||||
#ifdef TORQUE_DEBUG
|
||||
// No, so warn.
|
||||
Con::warnf( "TamlWrite() - Setting binary compression is only valid for XML formatting." );
|
||||
#endif
|
||||
Con::warnf( "TamlWrite() - Could not find object to write to file '%s'.", filename );
|
||||
return false;
|
||||
}
|
||||
|
||||
// Turn-off auto-formatting.
|
||||
taml.setAutoFormat( false );
|
||||
Taml taml;
|
||||
|
||||
// Write.
|
||||
if (filename != NULL && filename[0] != '\0')
|
||||
{
|
||||
taml.setFormatMode(Taml::getFormatModeEnum(format));
|
||||
// Turn-off auto-formatting.
|
||||
taml.setAutoFormat(false);
|
||||
}
|
||||
|
||||
if (taml.getFormatMode() == Taml::BinaryFormat)
|
||||
{
|
||||
// Yes, so set binary compression.
|
||||
taml.setBinaryCompression(compressed);
|
||||
}
|
||||
|
||||
// Write.
|
||||
return taml.write( simObject, filename );
|
||||
}
|
||||
|
||||
|
|
@ -277,27 +270,27 @@ DefineEngineFunction(TamlRead, const char*, (const char* filename, const char* f
|
|||
"@return (Object) The object read from the file or an empty string if read failed.")
|
||||
{
|
||||
|
||||
// Set the format mode.
|
||||
Taml taml;
|
||||
// Set the format mode.
|
||||
Taml taml;
|
||||
|
||||
// Yes, so set it.
|
||||
taml.setFormatMode( Taml::getFormatModeEnum(format) );
|
||||
// Yes, so set it.
|
||||
taml.setFormatMode( Taml::getFormatModeEnum(format) );
|
||||
|
||||
// Turn-off auto-formatting.
|
||||
taml.setAutoFormat( false );
|
||||
// Turn-off auto-formatting.
|
||||
taml.setAutoFormat( false );
|
||||
|
||||
// Read object.
|
||||
// Read object.
|
||||
SimObject* pSimObject = taml.read( filename );
|
||||
|
||||
// Did we find the object?
|
||||
if ( pSimObject == NULL )
|
||||
{
|
||||
// No, so warn.
|
||||
Con::warnf( "TamlRead() - Could not read object from file '%s'.", filename );
|
||||
return StringTable->EmptyString();
|
||||
}
|
||||
// Did we find the object?
|
||||
if ( pSimObject == NULL )
|
||||
{
|
||||
// No, so warn.
|
||||
Con::warnf( "TamlRead() - Could not read object from file '%s'.", filename );
|
||||
return StringTable->EmptyString();
|
||||
}
|
||||
|
||||
return pSimObject->getIdString();
|
||||
return pSimObject->getIdString();
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
|
@ -306,8 +299,8 @@ DefineEngineFunction(GenerateTamlSchema, bool, (), , "() - Generate a TAML schem
|
|||
"The schema file is specified using the console variable '" TAML_SCHEMA_VARIABLE "'.\n"
|
||||
"@return Whether the schema file was writtent or not." )
|
||||
{
|
||||
// Generate the schema.
|
||||
return Taml::generateTamlSchema();
|
||||
// Generate the schema.
|
||||
return Taml::generateTamlSchema();
|
||||
}
|
||||
|
||||
#endif //_TAML_SCRIPTBINDING_H
|
||||
|
|
|
|||
Loading…
Reference in a new issue