Simulant  21.12-194
A portable game engine for Windows, OSX, Linux, Dreamcast, and PSP
vfs.h
1 /* * Copyright (c) 2011-2017 Luke Benstead https://simulant-engine.appspot.com
2  *
3  * This file is part of Simulant.
4  *
5  * Simulant is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * Simulant is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with Simulant. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef RESOURCE_LOCATOR_H
20 #define RESOURCE_LOCATOR_H
21 
22 #include <list>
23 #include <vector>
24 #include <string>
25 #include <map>
26 #include <queue>
27 
28 #include "generic/lru_cache.h"
29 #include "generic/optional.h"
30 #include "generic/managed.h"
31 #include "utils/unicode.h"
32 #include "path.h"
33 
34 namespace smlt {
35 
36 class Window;
37 
38 class AssetMissingError : public std::runtime_error {
39 public:
40  AssetMissingError(const std::string& what):
41  std::runtime_error(what) {}
42 };
43 
45  public RefCounted<VirtualFileSystem> {
46 
47 public:
49 
50  std::list<Path>& search_path() { return resource_path_; }
51 
53  const Path& filename,
54  bool use_cache=true,
55  bool fail_silently=false
56  ) const;
57  std::shared_ptr<std::istream> open_file(const Path& filename);
58  std::shared_ptr<std::stringstream> read_file(const Path& filename);
59  std::vector<std::string> read_file_lines(const Path& filename);
60 
61  bool add_search_path(const Path& path);
62  void remove_search_path(const Path& path);
63 
64  /* Returns the number of entries in the location cache */
65  std::size_t location_cache_size() const;
66 
67  /* Purge the location cache of all entries */
68  void clear_location_cache();
69 
70  /* Limit the location cache entries */
71  void set_location_cache_limit(std::size_t entries);
72 
73 private:
74  Path find_executable_directory();
75  Path find_working_directory();
76 
77  std::list<Path> resource_path_;
78 
79  mutable LRUCache<Path, Path> location_cache_;
80 };
81 
82 }
83 
84 #endif // RESOURCE_LOCATOR_H
smlt::VirtualFileSystem
Definition: vfs.h:45
smlt::RefCounted
Definition: managed.h:65
smlt::VirtualFileSystem::locate_file
optional< Path > locate_file(const Path &filename, bool use_cache=true, bool fail_silently=false) const
Definition: vfs.cpp:111
smlt
Definition: animation.cpp:25
smlt::optional
Definition: optional.h:13
smlt::Path
Definition: path.h:7
smlt::AssetMissingError
Definition: vfs.h:38
smlt::LRUCache
Definition: lru_cache.h:9