bool AssetManager::LoadAsset (const csString& normpath, const csString& file, const csString& mount, iCollection* collection) { csRef<iString> path; if (!normpath.IsEmpty ()) { csRef<scfStringArray> fullPath = ConstructPath (); path = FindAsset (fullPath, normpath, file); if (!path) return Error ("Cannot find asset '%s' in the asset path!\n", normpath.GetData ()); } csString rmount; if (mount.IsEmpty ()) { rmount.Format ("/assets/__mnt_%d__/", mntCounter); mntCounter++; } else { rmount = mount; } if (path) { vfs->Mount (rmount, path->GetData ()); printf ("Mounting '%s' to '%s'\n", path->GetData (), rmount.GetData ()); fflush (stdout); } vfs->PushDir (rmount); // If the file doesn't exist we don't try to load it. That's not an error // as it might be saved later. bool exists = vfs->Exists (file); vfs->PopDir (); if (exists) { if (!LoadLibrary (rmount, file, collection)) return false; } else { Warn ("Warning! File '%s/%s' does not exist!\n", (!path) ? rmount.GetData () : path->GetData (), file.GetData ()); } //if (!path.IsEmpty ()) //vfs->Unmount (rmount, path); return true; }
void FFolderPackage::WriteAsset( FStringConst& Path ) { guard; //Get asset FAsset* Asset = FindAsset( Path ); if(!Asset) { FString Err = FString( L"Asset '" ) + Path.Data + FString( L"' could not be written to." ); Throw( (wchar_t*)Err.Data, false ); } Open( Path.Data, WriteOnly, Create ); Write( Asset->Ptr, Asset->Size ); Close(); unguard; }
void FFolderPackage::LoadAsset( FStringConst& Path ) { guard; //Get asset FAsset* Asset = FindAsset( Path ); if(!Asset) { FString Err = FString( L"Asset '" ) + Path.Data + FString( L"' could not be read." ); Throw( (wchar_t*)Err.Data, false ); } Open( Path.Data ); Asset->Ptr = (uint8*)GMemory->Malloc( Asset->Size ); Read( Asset->Ptr, Asset->Size ); Close(); unguard; }
//TODO: Multi-thread this void FZipPackage::LoadAsset( FStringConst& Path ) { guard; //Check that the asset exists FZipAsset* Asset = (FZipAsset*)FindAsset( Path ); if(!Asset) { FString Err = FString( L"Asset '" ) + Path.Data + FString( L"' could not be read." ); Throw( (wchar_t*)Err.Data, false ); } Seek( Asset->Location, Begin ); Asset->Ptr = new uint8[Asset->Size]; switch (Asset->CompressType) { case 0: { //No compression Read( Asset->Ptr, Asset->Size ); break; } case 8: { //Deflate uint8* CompressData = new uint8[Asset->CompressSize]; Read( CompressData, Asset->CompressSize ); Inflate( CompressData, Asset->Ptr, Asset->CompressSize, Asset->Size ); if(Asset->Type == AT_Text || Asset->Type == AT_Script) { //Null terminate the data if it has formatted text Asset->Ptr[Asset->Size] = 0; } break; } default: { Logf( LOG_ERR, L"Asset '%s' is compressed with an unknown method.", Asset->Name ); } } unguard; }
AssetPtr AssetManager::LoadAsset(const boost::filesystem::path& file) { AssetPtr pAsset = FindAsset(file); if(pAsset) { pAsset->IncRef(); return pAsset; } AssetLoader loader = FindLoader(file); if(loader) { return AssetPtr(); } pAsset = loader(file); m_assets[file.string()] = pAsset; return pAsset; }
bool AssetManager::SaveAsset (iDocumentSystem* docsys, iAsset* asset) { csRef<iDocument> docasset = docsys->CreateDocument (); IntAsset* ia = static_cast<IntAsset*> (asset); iCollection* collection = ia->GetCollection (); csRef<iDocumentNode> root = docasset->CreateRoot (); csRef<iDocumentNode> rootNode = root->CreateNodeBefore (CS_NODE_ELEMENT); rootNode->SetValue ("library"); { csRef<iSaver> saver = csQueryRegistryOrLoad<iSaver> (object_reg, "crystalspace.level.saver"); if (!saver) return Error ("ERROR! Saver plugin is missing. Cannot save!"); if (!saver->SaveLightFactories (collection, rootNode)) return Error ("ERROR! Error saving light factories!\n"); } { csRef<iQuestManager> questmgr = csQueryRegistryOrLoad<iQuestManager> (object_reg, "cel.manager.quests"); if (!questmgr) return false; csRef<iDocumentNode> addonNode = rootNode->CreateNodeBefore (CS_NODE_ELEMENT); addonNode->SetValue ("addon"); addonNode->SetAttribute ("plugin", "cel.addons.questdef"); if (!questmgr->Save (addonNode, collection)) return false; } { csRef<iDynamicWorldSaver> saver = csLoadPluginCheck<iDynamicWorldSaver> (object_reg, "cel.addons.dynamicworld.loader"); if (!saver) return false; csRef<iDocumentNode> addonNode = rootNode->CreateNodeBefore (CS_NODE_ELEMENT); addonNode->SetValue ("addon"); addonNode->SetAttribute ("plugin", "cel.addons.dynamicworld.loader"); if (!saver->WriteFactories (dynworld, addonNode, collection)) return false; } { csRef<iSaverPlugin> saver = csLoadPluginCheck<iSaverPlugin> (object_reg, "cel.addons.celentitytpl"); if (!saver) return 0; csRef<iCelPlLayer> pl = csQueryRegistry<iCelPlLayer> (object_reg); csRef<iCelEntityTemplateIterator> tempIt = pl->GetEntityTemplates (); while (tempIt->HasNext ()) { iCelEntityTemplate* temp = tempIt->Next (); if (!collection || collection->IsParentOf (temp->QueryObject ())) { csString tempName = temp->GetName (); csRef<iDocumentNode> addonNode = rootNode->CreateNodeBefore (CS_NODE_ELEMENT); addonNode->SetValue ("addon"); addonNode->SetAttribute ("plugin", "cel.addons.celentitytpl"); if (!saver->WriteDown (temp, addonNode, 0)) return false; } } } csRef<iString> xml; xml.AttachNew (new scfString ()); docasset->Write (xml); // In order to control the exact location to save we make a new temporary mount // point where we can save. That's to avoid the problem where an asset comes from // a location which is mounted on different real paths. // If the asset cannot be found yet then we will save to the first location on the path. csString normpath = asset->GetNormalizedPath (); if (normpath.IsEmpty ()) { Report ("Writing '%s' at '%s\n", asset->GetFile ().GetData (), asset->GetMountPoint ().GetData ()); vfs->PushDir (asset->GetMountPoint ()); vfs->WriteFile (asset->GetFile (), xml->GetData (), xml->Length ()); vfs->PopDir (); } else { Report ("Writing '%s' at '%s\n", asset->GetFile ().GetData (), asset->GetNormalizedPath ().GetData ()); csRef<scfStringArray> fullPath = ConstructPath (); csRef<iString> path = FindAsset (fullPath, normpath, asset->GetFile (), true); vfs->Mount ("/assets/__mnt_wl__", path->GetData ()); vfs->PushDir ("/assets/__mnt_wl__"); vfs->WriteFile (asset->GetFile (), xml->GetData (), xml->Length ()); vfs->PopDir (); vfs->Unmount ("/assets/__mnt_wl__", path->GetData ()); } return true; }