Skip to content

Commit a181f48

Browse files
authored
Merge pull request #1633 from Azaezel/alpha41/dsqFindings
dsq followup work 1
2 parents 62e8de2 + 3213ede commit a181f48

File tree

5 files changed

+127
-46
lines changed

5 files changed

+127
-46
lines changed

Engine/source/ts/assimp/assimpShapeLoader.cpp

Lines changed: 58 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
# define new _new
7070
#endif
7171

72+
extern bool gTryUseDSQs;
7273

7374
MODULE_BEGIN( AssimpShapeLoader )
7475
MODULE_INIT_AFTER( ShapeLoader )
@@ -664,13 +665,12 @@ bool AssimpShapeLoader::fillGuiTreeView(const char* sourceShapePath, GuiTreeView
664665
return true;
665666
}
666667

667-
/// Check if an up-to-date cached DTS is available for this DAE file
668+
/// Check if an up-to-date cached DTS is available for this file
668669
bool AssimpShapeLoader::canLoadCachedDTS(const Torque::Path& path)
669670
{
670671
// Generate the cached filename
671672
Torque::Path cachedPath(path);
672-
if (String::compare(path.getExtension(), "dsq") != 0)
673-
cachedPath.setExtension("cached.dts");
673+
cachedPath.setExtension("cached.dts");
674674

675675
// Check if a cached DTS newer than this file is available
676676
FileTime cachedModifyTime;
@@ -689,6 +689,30 @@ bool AssimpShapeLoader::canLoadCachedDTS(const Torque::Path& path)
689689
return false;
690690
}
691691

692+
/// Check if an up-to-date cached DSQ is available for this file
693+
bool AssimpShapeLoader::canLoadCachedDSQ(const Torque::Path& path)
694+
{
695+
// Generate the cached filename
696+
Torque::Path cachedPath(path);
697+
cachedPath.setExtension("dsq");
698+
699+
// Check if a cached DTS newer than this file is available
700+
FileTime cachedModifyTime;
701+
if (Platform::getFileTimes(cachedPath.getFullPath(), NULL, &cachedModifyTime))
702+
{
703+
bool forceLoad = Con::getBoolVariable("$assimp::forceLoad", false);
704+
705+
FileTime daeModifyTime;
706+
if (!Platform::getFileTimes(path.getFullPath(), NULL, &daeModifyTime) ||
707+
(!forceLoad && (Platform::compareFileTimes(cachedModifyTime, daeModifyTime) >= 0)))
708+
{
709+
// Original file not found, or cached DTS is newer
710+
return true;
711+
}
712+
}
713+
return false;
714+
}
715+
692716
void AssimpShapeLoader::assimpLogCallback(const char* message, char* user)
693717
{
694718
Con::printf("[Assimp log message] %s", StringUnit::getUnit(message, 0, "\n"));
@@ -973,30 +997,37 @@ TSShape* assimpLoadShape(const Torque::Path &path)
973997
// TODO: add .cached.dts generation.
974998
// Generate the cached filename
975999
Torque::Path cachedPath(path);
976-
if ( String::compare(path.getExtension(),"dsq") != 0)
977-
cachedPath.setExtension("cached.dts");
1000+
bool canLoadCached = false;
1001+
bool canLoadDSQ = false;
9781002

9791003
// Check if an up-to-date cached DTS version of this file exists, and
9801004
// if so, use that instead.
9811005
if (AssimpShapeLoader::canLoadCachedDTS(path))
1006+
{
1007+
cachedPath.setExtension("cached.dts");
1008+
canLoadCached = true;
1009+
}
1010+
else if (gTryUseDSQs && AssimpShapeLoader::canLoadCachedDSQ(path))
1011+
{
1012+
cachedPath.setExtension("dsq");
1013+
canLoadDSQ = true;
1014+
}
1015+
if (canLoadCached || canLoadDSQ)
9821016
{
9831017
FileStream cachedStream;
9841018
cachedStream.open(cachedPath.getFullPath(), Torque::FS::File::Read);
9851019
if (cachedStream.getStatus() == Stream::Ok)
9861020
{
9871021
TSShape *shape = new TSShape;
988-
if (String::compare(path.getExtension(), "dsq") == 0)
1022+
bool readSuccess = false;
1023+
if (canLoadCached)
9891024
{
990-
if (!shape->importSequences(&cachedStream, cachedPath.getFullPath()))
991-
{
992-
Con::errorf("assimpLoadShape: Load sequence file '%s' failed", cachedPath.getFullPath().c_str());
993-
delete shape;
994-
shape = NULL;
995-
}
996-
cachedStream.close();
997-
return shape;
1025+
readSuccess = shape->read(&cachedStream);
1026+
}
1027+
else
1028+
{
1029+
readSuccess = shape->importSequences(&cachedStream, cachedPath);
9981030
}
999-
bool readSuccess = shape->read(&cachedStream);
10001031
cachedStream.close();
10011032

10021033
if (readSuccess)
@@ -1007,10 +1038,13 @@ TSShape* assimpLoadShape(const Torque::Path &path)
10071038
return shape;
10081039
}
10091040
else
1041+
{
1042+
#ifdef TORQUE_DEBUG
1043+
Con::errorf("assimpLoadShape: Load sequence file '%s' failed", cachedPath.getFullPath().c_str());
1044+
#endif
10101045
delete shape;
1046+
}
10111047
}
1012-
1013-
Con::warnf("Failed to load cached shape from %s", cachedPath.getFullPath().c_str());
10141048
}
10151049

10161050
if (!Torque::FS::IsFile(path))
@@ -1041,27 +1075,22 @@ TSShape* assimpLoadShape(const Torque::Path &path)
10411075
realMesh = true;
10421076
}
10431077

1044-
if (!realMesh)
1078+
if (!realMesh && gTryUseDSQs)
10451079
{
10461080
Torque::Path dsqPath(cachedPath);
10471081
dsqPath.setExtension("dsq");
10481082
FileStream animOutStream;
1049-
for (S32 i = 0; i < tss->sequences.size(); i++)
1083+
dsqPath.setFileName(cachedPath.getFileName());
1084+
if (animOutStream.open(dsqPath.getFullPath(), Torque::FS::File::Write))
10501085
{
1051-
const String& seqName = tss->getName(tss->sequences[i].nameIndex);
1052-
Con::printf("Writing DSQ Animation File for sequence '%s'", seqName.c_str());
1053-
1054-
dsqPath.setFileName(cachedPath.getFileName() + "_" + seqName);
1055-
if (animOutStream.open(dsqPath.getFullPath(), Torque::FS::File::Write))
1056-
{
1057-
tss->exportSequence(&animOutStream, tss->sequences[i], false);
1058-
animOutStream.close();
1059-
}
1060-
1086+
Con::printf("Writing DSQ Animation File for '%s'", dsqPath.getFileName().c_str());
1087+
tss->exportSequences(&animOutStream);
10611088
}
10621089
}
10631090
else
10641091
{
1092+
// Cache the model to a DTS file for faster loading next time.
1093+
cachedPath.setExtension("cached.dts");
10651094
// Cache the model to a DTS file for faster loading next time.
10661095
FileStream dtsStream;
10671096
if (dtsStream.open(cachedPath.getFullPath(), Torque::FS::File::Write))

Engine/source/ts/assimp/assimpShapeLoader.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ class AssimpShapeLoader : public TSShapeLoader
7979
bool fillGuiTreeView(const char* shapePath, GuiTreeViewCtrl* tree);
8080

8181
static bool canLoadCachedDTS(const Torque::Path& path);
82+
static bool canLoadCachedDSQ(const Torque::Path& path);
8283
static void assimpLogCallback(const char* message, char* user);
8384
};
8485

Engine/source/ts/collada/colladaShapeLoader.cpp

Lines changed: 66 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
#include "core/util/zip/zipVolume.h"
5151
#include "gfx/bitmap/gBitmap.h"
5252

53+
extern bool gTryUseDSQs;
5354
MODULE_BEGIN( ColladaShapeLoader )
5455
MODULE_INIT_AFTER( ShapeLoader )
5556
MODULE_INIT
@@ -548,7 +549,39 @@ bool ColladaShapeLoader::canLoadCachedDTS(const Torque::Path& path)
548549

549550
return false;
550551
}
552+
bool ColladaShapeLoader::canLoadCachedDSQ(const Torque::Path& path)
553+
{
554+
// Generate the cached filename
555+
Torque::Path cachedPath(path);
556+
cachedPath.setExtension("dsq");
557+
558+
// Check if a cached DSQ newer than this file is available
559+
FileTime cachedModifyTime;
560+
if (Platform::getFileTimes(cachedPath.getFullPath(), NULL, &cachedModifyTime))
561+
{
562+
bool forceLoadDAE = Con::getBoolVariable("$collada::forceLoadDAE", false);
563+
564+
FileTime daeModifyTime;
565+
if (!Platform::getFileTimes(path.getFullPath(), NULL, &daeModifyTime) ||
566+
(!forceLoadDAE && (Platform::compareFileTimes(cachedModifyTime, daeModifyTime) >= 0)))
567+
{
568+
// DAE not found, or cached DTS is newer
569+
return true;
570+
}
571+
}
572+
573+
//assume the dts is good since it was zipped on purpose
574+
Torque::FS::FileSystemRef ref = Torque::FS::GetFileSystem(cachedPath);
575+
if (ref && !String::compare("Zip", ref->getTypeStr().c_str()))
576+
{
577+
bool forceLoadDAE = Con::getBoolVariable("$collada::forceLoadDAE", false);
578+
579+
if (!forceLoadDAE && Torque::FS::IsFile(cachedPath))
580+
return true;
581+
}
551582

583+
return false;
584+
}
552585
bool ColladaShapeLoader::checkAndMountSketchup(const Torque::Path& path, String& mountPoint, Torque::Path& daePath)
553586
{
554587
bool isSketchup = path.getExtension().equal("kmz", String::NoCase);
@@ -655,18 +688,36 @@ TSShape* loadColladaShape(const Torque::Path &path)
655688
#ifndef DAE2DTS_TOOL
656689
// Generate the cached filename
657690
Torque::Path cachedPath(path);
658-
cachedPath.setExtension("cached.dts");
659-
691+
bool canLoadCached = false;
692+
bool canLoadDSQ = false;
660693
// Check if an up-to-date cached DTS version of this file exists, and
661694
// if so, use that instead.
662695
if (ColladaShapeLoader::canLoadCachedDTS(path))
696+
{
697+
cachedPath.setExtension("cached.dts");
698+
canLoadCached = true;
699+
}
700+
else if (gTryUseDSQs && ColladaShapeLoader::canLoadCachedDSQ(path))
701+
{
702+
cachedPath.setExtension("dsq");
703+
canLoadDSQ = true;
704+
}
705+
if (canLoadCached || canLoadDSQ)
663706
{
664707
FileStream cachedStream;
665708
cachedStream.open(cachedPath.getFullPath(), Torque::FS::File::Read);
666709
if (cachedStream.getStatus() == Stream::Ok)
667710
{
668711
TSShape *shape = new TSShape;
669-
bool readSuccess = shape->read(&cachedStream);
712+
bool readSuccess = false;
713+
if (canLoadCached)
714+
{
715+
readSuccess = shape->read(&cachedStream);
716+
}
717+
else
718+
{
719+
readSuccess = shape->importSequences(&cachedStream, cachedPath);
720+
}
670721
cachedStream.close();
671722

672723
if (readSuccess)
@@ -677,10 +728,13 @@ TSShape* loadColladaShape(const Torque::Path &path)
677728
return shape;
678729
}
679730
else
731+
{
732+
#ifdef TORQUE_DEBUG
733+
Con::errorf("loadColladaShape: Load sequence file '%s' failed", cachedPath.getFullPath().c_str());
734+
#endif
680735
delete shape;
736+
}
681737
}
682-
683-
Con::warnf("Failed to load cached COLLADA shape from %s", cachedPath.getFullPath().c_str());
684738
}
685739
#endif // DAE2DTS_TOOL
686740

@@ -732,28 +786,23 @@ TSShape* loadColladaShape(const Torque::Path &path)
732786
realMesh = true;
733787
}
734788

735-
if (!realMesh)
789+
if (!realMesh && gTryUseDSQs)
736790
{
737791
Torque::Path dsqPath(cachedPath);
738792
dsqPath.setExtension("dsq");
739793
FileStream animOutStream;
740-
for (S32 i = 0; i < tss->sequences.size(); i++)
794+
dsqPath.setFileName(cachedPath.getFileName());
795+
if (animOutStream.open(dsqPath.getFullPath(), Torque::FS::File::Write))
741796
{
742-
const String& seqName = tss->getName(tss->sequences[i].nameIndex);
743-
Con::printf("Writing DSQ Animation File for sequence '%s'", seqName.c_str());
744-
745-
dsqPath.setFileName(cachedPath.getFileName() + "_" + seqName);
746-
if (animOutStream.open(dsqPath.getFullPath(), Torque::FS::File::Write))
747-
{
748-
tss->exportSequence(&animOutStream, tss->sequences[i], false);
749-
animOutStream.close();
750-
}
751-
797+
Con::printf("Writing DSQ Animation File for '%s'", dsqPath.getFileName().c_str());
798+
tss->exportSequences(&animOutStream);
799+
animOutStream.close();
752800
}
753801
}
754802
else
755803
{
756804
// Cache the Collada model to a DTS file for faster loading next time.
805+
cachedPath.setExtension("cached.dts");
757806
FileStream dtsStream;
758807
if (dtsStream.open(cachedPath.getFullPath(), Torque::FS::File::Write))
759808
{

Engine/source/ts/collada/colladaShapeLoader.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class ColladaShapeLoader : public TSShapeLoader
5353
void computeBounds(Box3F& bounds) override;
5454

5555
static bool canLoadCachedDTS(const Torque::Path& path);
56+
static bool canLoadCachedDSQ(const Torque::Path& path);
5657
static bool checkAndMountSketchup(const Torque::Path& path, String& mountPoint, Torque::Path& daePath);
5758
static domCOLLADA* getDomCOLLADA(const Torque::Path& path);
5859
static domCOLLADA* readColladaFile(const String& path);

Engine/source/ts/loader/tsShapeLoader.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ MODULE_BEGIN( ShapeLoader )
4040
}
4141
MODULE_END;
4242

43+
bool gTryUseDSQs = false;
4344
const F32 TSShapeLoader::DefaultTime = -1.0f;
4445
const F64 TSShapeLoader::MinFrameRate = 15.0f;
4546
const F64 TSShapeLoader::MaxFrameRate = 60.0f;

0 commit comments

Comments
 (0)