Merge branch 'ryzom/ark-features' into main/gingo-test
[ryzomcore.git] / nel / src / misc / streamed_package_manager.cpp
blobd7eecd85ef48497fb6bd1def8647214557e9f096
1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2014-2020 Jan BOON (Kaetemi) <jan.boon@kaetemi.be>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "stdmisc.h"
19 // Project includes
20 #include <nel/misc/streamed_package_manager.h>
21 #include <nel/misc/file.h>
22 #include <nel/misc/path.h>
23 #include <nel/misc/sha1.h>
25 namespace NLMISC
28 NLMISC_SAFE_SINGLETON_IMPL(CStreamedPackageManager);
30 CStreamedPackageManager::CStreamedPackageManager() : Provider(NULL)
32 // init
35 CStreamedPackageManager::~CStreamedPackageManager()
37 // release
40 bool CStreamedPackageManager::loadPackage(const std::string &package)
42 nldebug("Load package '%s'", package.c_str());
44 std::string packname = NLMISC::toLowerAscii(CFile::getFilename(package));
45 m_Packages[packname] = CStreamedPackage();
46 std::map<std::string, CStreamedPackage>::iterator it = m_Packages.find(packname);
47 CStreamedPackage &p = it->second;
49 try
51 CIFile fi;
52 fi.open(package);
53 fi.serial(p);
55 catch (Exception &e)
57 nlerror("Package serial exception: '%s'", e.what());
58 m_Packages.erase(it);
59 return false;
62 for (CStreamedPackage::TEntries::const_iterator it(p.Entries.begin()), end(p.Entries.end()); it != end; ++it)
64 const CStreamedPackage::CEntry &entry = (*it);
65 m_Entries[entry.Name] = &entry;
68 return true;
71 void CStreamedPackageManager::list(std::vector<std::string> &fileNames, const std::string &package)
73 // nldebug("List package '%s'", package.c_str());
75 std::map<std::string, CStreamedPackage>::iterator it = m_Packages.find(package);
76 CStreamedPackage &p = it->second;
78 for (CStreamedPackage::TEntries::const_iterator it(p.Entries.begin()), end(p.Entries.end()); it != end; ++it)
80 const CStreamedPackage::CEntry &entry = (*it);
81 fileNames.push_back(entry.Name);
85 void CStreamedPackageManager::unloadAll()
87 m_Packages.clear();
88 m_Entries.clear();
91 bool CStreamedPackageManager::getFile(std::string &filePath, const std::string &fileName)
93 // nldebug("Get file path for streamed file '%s'", fileName.c_str());
95 IStreamedPackageProvider *provider = Provider;
96 if (!provider)
98 nlerrornoex("No streamed package provider was set");
99 return false;
102 TEntries::iterator it = m_Entries.find(fileName);
103 if (it == m_Entries.end())
105 return false;
107 const CStreamedPackage::CEntry *entry = it->second;
109 return provider->getFile(filePath, entry->Hash, fileName);
111 return true;
114 bool CStreamedPackageManager::getFileSize(uint32 &fileSize, const std::string &fileName)
116 // nldebug("Get file size for streamed file '%s'", fileName.c_str());
117 TEntries::iterator it = m_Entries.find(fileName);
118 if (it == m_Entries.end())
119 return false;
120 fileSize = it->second->Size;
121 return true;
124 } /* namespace NLMISC */
126 /* end of file */