Simulant  21.12-1292
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  const std::list<Path>& search_path() { return resource_path_; }
51 
52  optional<Path> locate_file(
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  bool insert_search_path(uint32_t index, const Path& path);
63  void remove_search_path(const Path& path);
64 
65  std::size_t search_path_size() const { return resource_path_.size(); }
66 
67  /* Returns the number of entries in the location cache */
68  std::size_t location_cache_size() const;
69 
70  /* Purge the location cache of all entries */
71  void clear_location_cache();
72 
73  /* Limit the location cache entries */
74  void set_location_cache_limit(std::size_t entries);
75 
81  read_blocking_enabled_ = true;
82  }
83 
84  void disable_read_blocking() {
85  read_blocking_enabled_ = false;
86  }
87 
88  bool read_blocking_enabled() const {
89  return read_blocking_enabled_;
90  }
91 
92 
93 private:
94  bool read_blocking_enabled_ = false;
95 
96  Path find_executable_directory();
97  Path find_working_directory();
98 
99  std::list<Path> resource_path_;
100 
101  mutable LRUCache<Path, Path> location_cache_;
102 };
103 
104 }
105 
106 #endif // RESOURCE_LOCATOR_H
smlt::VirtualFileSystem
Definition: vfs.h:45
smlt::RefCounted
Definition: managed.h:71
smlt
Definition: animation.cpp:25
smlt::VirtualFileSystem::enable_read_blocking
void enable_read_blocking()
Definition: vfs.h:80
smlt::optional
Definition: optional.h:19
smlt::Path
Definition: path.h:7
smlt::AssetMissingError
Definition: vfs.h:38