Fix AssetsCache to include project path and reject cache when project gets duplicated with cache

#1631
This commit is contained in:
Wojtek Figat
2023-10-09 11:33:02 +02:00
parent 1ac4fef8d0
commit d7e9e2ed16
3 changed files with 21 additions and 41 deletions

View File

@@ -3,7 +3,7 @@
"Version": {
"Major": 1,
"Minor": 6,
"Build": 6345
"Build": 6346
},
"Company": "Flax",
"Copyright": "Copyright (c) 2012-2023 Wojciech Figat. All rights reserved.",

View File

@@ -15,15 +15,8 @@
#include "Engine/Engine/Globals.h"
#include "FlaxEngine.Gen.h"
AssetsCache::AssetsCache()
: _isDirty(false)
, _registry(4096)
{
}
void AssetsCache::Init()
{
// Cache data
Entry e;
int32 count;
const DateTime loadStartTime = DateTime::Now();
@@ -32,13 +25,11 @@ void AssetsCache::Init()
#else
_path = Globals::ProjectContentFolder / TEXT("AssetsCache.dat");
#endif
LOG(Info, "Loading Asset Cache {0}...", _path);
// Check if assets registry exists
if (!FileSystem::FileExists(_path))
{
// Back
_isDirty = true;
LOG(Warning, "Cannot find assets cache file");
return;
@@ -49,7 +40,6 @@ void AssetsCache::Init()
DeleteMe<FileReadStream> deleteStream(stream);
// Load version
// Note: every Engine build is using different assets cache
int32 version;
stream->ReadInt32(&version);
if (version != FLAXENGINE_VERSION_BUILD)
@@ -58,24 +48,28 @@ void AssetsCache::Init()
return;
}
// Load Engine workspace path
String workspacePath;
stream->ReadString(&workspacePath, -410);
// Load paths
String enginePath, projectPath;
stream->ReadString(&enginePath, -410);
stream->ReadString(&projectPath, -410);
// Flags
AssetsCacheFlags flags;
stream->ReadInt32((int32*)&flags);
// Check if other engine instance used this cache (cache depends on engine build and install location)
// Skip it for relative paths mode
if (!(flags & AssetsCacheFlags::RelativePaths) && workspacePath != Globals::StartupFolder)
// Check if other workspace instance used this cache
if (EnumHasNoneFlags(flags, AssetsCacheFlags::RelativePaths) && enginePath != Globals::StartupFolder)
{
LOG(Warning, "Assets cache generated by the different engine installation in \'{0}\'", workspacePath);
LOG(Warning, "Assets cache generated by the different {1} installation in \'{0}\'", enginePath, TEXT("engine"));
return;
}
if (EnumHasNoneFlags(flags, AssetsCacheFlags::RelativePaths) && projectPath != Globals::ProjectFolder)
{
LOG(Warning, "Assets cache generated by the different {1} installation in \'{0}\'", projectPath, TEXT("project"));
return;
}
ScopeLock lock(_locker);
_isDirty = false;
// Load elements count
@@ -103,15 +97,11 @@ void AssetsCache::Init()
e.Info.Path = Globals::StartupFolder / e.Info.Path;
}
// Validate entry
if (!IsEntryValid(e))
{
// Reject
// Use only valid entries
if (IsEntryValid(e))
_registry.Add(e.Info.ID, e);
else
rejectedCount++;
continue;
}
_registry.Add(e.Info.ID, e);
}
// Paths mapping
@@ -148,7 +138,6 @@ void AssetsCache::Init()
}
}
// End
const int32 loadTimeInMs = static_cast<int32>((DateTime::Now() - loadStartTime).GetTotalMilliseconds());
LOG(Info, "Asset Cache loaded {0} entries in {1} ms ({2} rejected)", _registry.Count(), loadTimeInMs, rejectedCount);
}
@@ -188,8 +177,9 @@ bool AssetsCache::Save(const StringView& path, const Registry& entries, const Pa
// Version
stream->WriteInt32(FLAXENGINE_VERSION_BUILD);
// Engine workspace path
// Paths
stream->WriteString(Globals::StartupFolder, -410);
stream->WriteString(Globals::ProjectFolder, -410);
// Flags
stream->WriteInt32((int32)flags);
@@ -202,7 +192,6 @@ bool AssetsCache::Save(const StringView& path, const Registry& entries, const Pa
for (auto i = entries.Begin(); i.IsNotEnd(); ++i)
{
auto& e = i->Value;
stream->Write(e.Info.ID);
stream->WriteString(e.Info.TypeName, index - 13);
stream->WriteString(e.Info.Path, index);
@@ -211,7 +200,6 @@ bool AssetsCache::Save(const StringView& path, const Registry& entries, const Pa
#else
stream->WriteInt64(0);
#endif
index++;
}
@@ -222,7 +210,6 @@ bool AssetsCache::Save(const StringView& path, const Registry& entries, const Pa
{
stream->Write(i->Value);
stream->WriteString(i->Key, index + 73);
index++;
}

View File

@@ -74,7 +74,7 @@ public:
typedef Dictionary<String, Guid> PathsMapping;
private:
bool _isDirty;
bool _isDirty = false;
CriticalSection _locker;
Registry _registry;
PathsMapping _pathsMapping;
@@ -82,15 +82,8 @@ private:
public:
/// <summary>
/// Initializes a new instance of the <see cref="AssetsCache"/> class.
/// Gets amount of registered assets.
/// </summary>
AssetsCache();
public:
/// <summary>
/// Gets amount of registered assets
/// </summary>
/// <returns>Registry size</returns>
int32 Size() const
{
_locker.Lock();