|
|
@@ -464,8 +464,9 @@ void Connection::ProcessLoadScene(int msgID, MemoryBuffer& msg)
|
|
|
|
|
|
// In case we have joined other scenes in this session, remove first all downloaded package files from the resource system
|
|
|
// to prevent resource conflicts
|
|
|
- const String& packageCacheDir = GetSubsystem<Network>()->GetPackageCacheDir();
|
|
|
ResourceCache* cache = GetSubsystem<ResourceCache>();
|
|
|
+ const String& packageCacheDir = GetSubsystem<Network>()->GetPackageCacheDir();
|
|
|
+
|
|
|
Vector<SharedPtr<PackageFile> > packages = cache->GetPackageFiles();
|
|
|
for (unsigned i = 0; i < packages.Size(); ++i)
|
|
|
{
|
|
|
@@ -476,61 +477,10 @@ void Connection::ProcessLoadScene(int msgID, MemoryBuffer& msg)
|
|
|
|
|
|
// Now check which packages we have in the resource cache or in the download cache, and which we need to download
|
|
|
unsigned numPackages = msg.ReadVLE();
|
|
|
- packages = cache->GetPackageFiles(); // Refresh resource cache's package list after possible removals
|
|
|
- Vector<String> downloadedPackages;
|
|
|
- if (!packageCacheDir.Empty())
|
|
|
- GetSubsystem<FileSystem>()->ScanDir(downloadedPackages, packageCacheDir, "*.*", SCAN_FILES, false);
|
|
|
-
|
|
|
- for (unsigned i = 0; i < numPackages; ++i)
|
|
|
+ if (!RequestNeededPackages(numPackages, msg))
|
|
|
{
|
|
|
- String name = msg.ReadString();
|
|
|
- unsigned fileSize = msg.ReadUInt();
|
|
|
- unsigned checksum = msg.ReadUInt();
|
|
|
- String checksumString = ToStringHex(checksum);
|
|
|
- bool found = false;
|
|
|
-
|
|
|
- // Check first the resource cache
|
|
|
- for (unsigned j = 0; j < packages.Size(); ++j)
|
|
|
- {
|
|
|
- PackageFile* package = packages[j];
|
|
|
- if (!GetFileNameAndExtension(package->GetName()).Compare(name, false) && package->GetTotalSize() == fileSize &&
|
|
|
- package->GetChecksum() == checksum)
|
|
|
- {
|
|
|
- found = true;
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // Then the download cache
|
|
|
- for (unsigned j = 0; j < downloadedPackages.Size(); ++j)
|
|
|
- {
|
|
|
- const String& fileName = downloadedPackages[j];
|
|
|
- if (!fileName.Find(checksumString) && !fileName.Substring(9).Compare(name, false))
|
|
|
- {
|
|
|
- // Name matches. Check filesize and actual checksum to be sure
|
|
|
- SharedPtr<PackageFile> newPackage(new PackageFile(context_, packageCacheDir + fileName));
|
|
|
- if (newPackage->GetTotalSize() == fileSize && newPackage->GetChecksum() == checksum)
|
|
|
- {
|
|
|
- // Add the package to the resource system now, as we will need it to load the scene
|
|
|
- cache->AddPackageFile(newPackage, true);
|
|
|
- found = true;
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // Package not found, need to request a download
|
|
|
- if (!found)
|
|
|
- {
|
|
|
- if (!packageCacheDir.Empty())
|
|
|
- RequestPackage(name, fileSize, checksum);
|
|
|
- else
|
|
|
- {
|
|
|
- LOGERROR("Can not download required packages, as package cache directory is not set");
|
|
|
- OnSceneLoadFailed();
|
|
|
- return;
|
|
|
- }
|
|
|
- }
|
|
|
+ OnSceneLoadFailed();
|
|
|
+ return;
|
|
|
}
|
|
|
|
|
|
// If no downloads were queued, can load the scene directly
|
|
|
@@ -1347,6 +1297,71 @@ void Connection::ProcessExistingNode(Node* node, NodeReplicationState& nodeState
|
|
|
sceneState_.dirtyNodes_.Erase(node->GetID());
|
|
|
}
|
|
|
|
|
|
+bool Connection::RequestNeededPackages(unsigned numPackages, MemoryBuffer& msg)
|
|
|
+{
|
|
|
+ ResourceCache* cache = GetSubsystem<ResourceCache>();
|
|
|
+ const String& packageCacheDir = GetSubsystem<Network>()->GetPackageCacheDir();
|
|
|
+
|
|
|
+ Vector<SharedPtr<PackageFile> > packages = cache->GetPackageFiles();
|
|
|
+ Vector<String> downloadedPackages;
|
|
|
+ if (!packageCacheDir.Empty())
|
|
|
+ GetSubsystem<FileSystem>()->ScanDir(downloadedPackages, packageCacheDir, "*.*", SCAN_FILES, false);
|
|
|
+
|
|
|
+ for (unsigned i = 0; i < numPackages; ++i)
|
|
|
+ {
|
|
|
+ String name = msg.ReadString();
|
|
|
+ unsigned fileSize = msg.ReadUInt();
|
|
|
+ unsigned checksum = msg.ReadUInt();
|
|
|
+ String checksumString = ToStringHex(checksum);
|
|
|
+ bool found = false;
|
|
|
+
|
|
|
+ // Check first the resource cache
|
|
|
+ for (unsigned j = 0; j < packages.Size(); ++j)
|
|
|
+ {
|
|
|
+ PackageFile* package = packages[j];
|
|
|
+ if (!GetFileNameAndExtension(package->GetName()).Compare(name, false) && package->GetTotalSize() == fileSize &&
|
|
|
+ package->GetChecksum() == checksum)
|
|
|
+ {
|
|
|
+ found = true;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // Then the download cache
|
|
|
+ for (unsigned j = 0; j < downloadedPackages.Size(); ++j)
|
|
|
+ {
|
|
|
+ const String& fileName = downloadedPackages[j];
|
|
|
+ // In download cache, package file name format is checksum_packagename
|
|
|
+ if (!fileName.Find(checksumString) && !fileName.Substring(9).Compare(name, false))
|
|
|
+ {
|
|
|
+ // Name matches. Check filesize and actual checksum to be sure
|
|
|
+ SharedPtr<PackageFile> newPackage(new PackageFile(context_, packageCacheDir + fileName));
|
|
|
+ if (newPackage->GetTotalSize() == fileSize && newPackage->GetChecksum() == checksum)
|
|
|
+ {
|
|
|
+ // Add the package to the resource system now, as we will need it to load the scene
|
|
|
+ cache->AddPackageFile(newPackage, true);
|
|
|
+ found = true;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // Package not found, need to request a download
|
|
|
+ if (!found)
|
|
|
+ {
|
|
|
+ if (!packageCacheDir.Empty())
|
|
|
+ RequestPackage(name, fileSize, checksum);
|
|
|
+ else
|
|
|
+ {
|
|
|
+ LOGERROR("Can not download required packages, as package cache directory is not set");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
void Connection::RequestPackage(const String& name, unsigned fileSize, unsigned checksum)
|
|
|
{
|
|
|
StringHash nameHash(name);
|
|
|
@@ -1464,67 +1479,11 @@ void Connection::ProcessPackageInfo(int msgID, MemoryBuffer& msg)
|
|
|
|
|
|
if (IsClient())
|
|
|
{
|
|
|
- LOGWARNING("Received unexpected packages info message from server");
|
|
|
+ LOGWARNING("Received unexpected packages info message from client");
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- // Now client check all available packages and retrieve missing packages
|
|
|
- Network* network = GetSubsystem<Network>();
|
|
|
- const String& packageCacheDir = network->GetPackageCacheDir();
|
|
|
-
|
|
|
- if (packageCacheDir.Empty())
|
|
|
- {
|
|
|
- LOGERROR("Can not download required packages, as package cache directory is not set");
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- ResourceCache* cache = GetSubsystem<ResourceCache>();
|
|
|
- Vector<SharedPtr<PackageFile> > packages = cache->GetPackageFiles();
|
|
|
-
|
|
|
- // Now check which packages we have in the resource cache or in the download cache, and which we need to download
|
|
|
- Vector<String> downloadedPackages;
|
|
|
-
|
|
|
- if (!packageCacheDir.Empty())
|
|
|
- GetSubsystem<FileSystem>()->ScanDir(downloadedPackages, packageCacheDir, "*.*", SCAN_FILES, false);
|
|
|
-
|
|
|
- String name = msg.ReadString();
|
|
|
- unsigned fileSize = msg.ReadUInt();
|
|
|
- unsigned checksum = msg.ReadUInt();
|
|
|
- String checksumString = ToStringHex(checksum);
|
|
|
- bool found = false;
|
|
|
-
|
|
|
- // Check first the resource cache
|
|
|
- for (unsigned j = 0; j < packages.Size(); ++j)
|
|
|
- {
|
|
|
- PackageFile* package = packages[j];
|
|
|
- if (!GetFileNameAndExtension(package->GetName()).Compare(name, false) && package->GetTotalSize() == fileSize &&
|
|
|
- package->GetChecksum() == checksum)
|
|
|
- {
|
|
|
- found = true;
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // Then the download cache
|
|
|
- for (unsigned j = 0; j < downloadedPackages.Size(); ++j)
|
|
|
- {
|
|
|
- const String& fileName = downloadedPackages[j];
|
|
|
- if (!fileName.Find(checksumString) && !fileName.Substring(9).Compare(name, false))
|
|
|
- {
|
|
|
- // Name matches. Check filesize and actual checksum to be sure
|
|
|
- SharedPtr<PackageFile> newPackage(new PackageFile(context_, packageCacheDir + fileName));
|
|
|
- if (newPackage->GetTotalSize() == fileSize && newPackage->GetChecksum() == checksum)
|
|
|
- {
|
|
|
- // Add the package to the resource system now, as we will need it to load the scene
|
|
|
- cache->AddPackageFile(newPackage, true);
|
|
|
- found = true;
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // Package not found, need to request a download
|
|
|
- if (!found)
|
|
|
- RequestPackage(name, fileSize, checksum);
|
|
|
+ RequestNeededPackages(1, msg);
|
|
|
}
|
|
|
+
|
|
|
}
|