1 /* Copyright (C) 2018 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. 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 General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef INCLUDED_MODINSTALLER
19 #define INCLUDED_MODINSTALLER
20 
21 #include "CStr.h"
22 #include "lib/file/vfs/vfs.h"
23 #include "scriptinterface/ScriptInterface.h"
24 
25 #include <vector>
26 
27 /**
28  * Install a mod into the mods directory.
29  */
30 class CModInstaller
31 {
32 public:
33 	enum ModInstallationResult
34 	{
35 		SUCCESS,
36 		FAIL_ON_VFS_MOUNT,
37 		FAIL_ON_MOD_LOAD,
38 		FAIL_ON_PARSE_JSON,
39 		FAIL_ON_EXTRACT_NAME,
40 		FAIL_ON_MOD_MOVE
41 	};
42 
43 	/**
44 	 * Initialise the mod installer for processing the given mod.
45 	 *
46 	 * @param modsdir path to the data directory that contains mods
47 	 * @param tempdir path to a writable directory for temporary files
48 	 */
49 	CModInstaller(const OsPath& modsdir, const OsPath& tempdir);
50 
51 	~CModInstaller();
52 
53 	/**
54 	 * Process and unpack the mod.
55 	 * @param mod path of .pyromod/.zip file
56 	 * @param keepFile if true, copy the file, if false move it
57 	 */
58 	ModInstallationResult Install(
59 		const OsPath& mod,
60 		const std::shared_ptr<ScriptRuntime>& scriptRuntime,
61 		bool keepFile);
62 
63 	/**
64 	 * @return a list of all mods installed so far by this CModInstaller.
65 	 */
66 	const std::vector<CStr>& GetInstalledMods() const;
67 
68 	/**
69 	 * @return whether the path has a mod-like extension.
70 	 */
IsDefaultModExtension(const Path & ext)71 	static bool IsDefaultModExtension(const Path& ext)
72 	{
73 		return ext == ".pyromod" || ext == ".zip";
74 	}
75 
76 private:
77 	PIVFS m_VFS;
78 	OsPath m_ModsDir;
79 	OsPath m_TempDir;
80 	VfsPath m_CacheDir;
81 	std::vector<CStr> m_InstalledMods;
82 };
83 
84 #endif // INCLUDED_MODINSTALLER
85