Symphony Of Empires
io.hpp
Go to the documentation of this file.
1 // Eng3D - General purpouse game engine
2 // Copyright (C) 2021, Eng3D contributors
3 //
4 // This program 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 3 of the License, or
7 // (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 General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <https://www.gnu.org/licenses/>.
16 //
17 // ----------------------------------------------------------------------------
18 // Name:
19 // io.hpp
20 //
21 // Abstract:
22 // Does some important stuff.
23 // ----------------------------------------------------------------------------
24 
25 #pragma once
26 
27 #include <cstdio>
28 #include <cstdlib>
29 #include <string>
30 #include <vector>
31 #include <memory>
32 #include <algorithm>
33 
34 namespace Eng3D {
35  class State;
36 }
37 
41 namespace Eng3D::IO {
44  struct Path {
45  Path() = default;
46  Path(const std::string& path)
47  : str(path)
48  {
49 
50  }
51 
52  Path(const char* path)
53  : str(path)
54  {
55 
56  }
57 
58  ~Path() = default;
59  std::string str;
60  };
61 
62  namespace StreamFlags {
63  enum {
64  READ = 0x01,
65  WRITE = 0x02,
66  TRUNCATE = 0x04,
67  };
68  }
69 
70  enum class SeekType {
71  START,
72  END,
73  CURRENT,
74  };
75 
76  namespace Asset {
77  struct Base {
78  virtual ~Base() = default;
79  std::string get_abs_path() const;
80  virtual void open() {}
81  virtual void close() {}
82  virtual void read(void*, size_t) {}
83  virtual void write(const void*, size_t) {}
84  virtual void seek(Eng3D::IO::SeekType, int) {}
85  virtual size_t get_size(void) const { return 0; }
86  std::string path;
87  std::string abs_path;
90  inline std::string read_all(void) {
91  this->open();
92  const size_t size = this->get_size();
93  std::string str;
94  str.resize(size + 1, ' ');
95  this->read(&str[0], size);
96  str[size] = '\0';
97  this->close();
98  return str;
99  }
100  };
101 
103  struct File : Asset::Base {
104  FILE* fp = nullptr;
105  virtual void open();
106  virtual void close();
107  virtual void read(void* buf, size_t n);
108  virtual void write(const void* buf, size_t n);
109  virtual void seek(Eng3D::IO::SeekType type, int offset);
110  virtual size_t get_size(void) const;
111  };
112  }
113 
114  class PackageException : public std::exception {
115  std::string buffer;
116  public:
117  PackageException(const std::string& _buffer) : buffer(_buffer) {}
118  virtual const char* what() const noexcept {
119  return buffer.c_str();
120  }
121  };
122 
124  struct Package {
125  std::string name;
126  std::string abs_path; // Absolute path of this package root
127  std::vector<std::shared_ptr<Eng3D::IO::Asset::Base>> assets;
128  std::string user_abs_path; // Absolute path for the user files
129  std::vector<std::shared_ptr<Eng3D::IO::Asset::Base>> user_assets;
130  };
131 
133  Eng3D::State& s;
134  public:
135  PackageManager() = delete;
136  PackageManager(Eng3D::State& s, const std::vector<std::string>& pkg_paths);
137  ~PackageManager() = default;
138  void recursive_filesystem_walk(Eng3D::IO::Package& package, const std::string& root, const std::string& current);
139  std::shared_ptr<Eng3D::IO::Asset::Base> get_unique(const Eng3D::IO::Path& path);
140  std::vector<std::shared_ptr<Eng3D::IO::Asset::Base>> get_multiple(const Eng3D::IO::Path& path);
141  std::vector<std::shared_ptr<Eng3D::IO::Asset::Base>> get_multiple_prefix(const Eng3D::IO::Path& path);
142  std::vector<std::string> get_paths(void) const;
143 
144  std::vector<Package> packages;
145  };
146 }
PackageException(const std::string &_buffer)
Definition: io.hpp:117
virtual const char * what() const noexcept
Definition: io.hpp:118
std::vector< std::shared_ptr< Eng3D::IO::Asset::Base > > get_multiple(const Eng3D::IO::Path &path)
Obtains multiple assets iff they share a common path (useful for concating files that might clash,...
Definition: io.cpp:157
void recursive_filesystem_walk(Eng3D::IO::Package &package, const std::string &root, const std::string &current)
Definition: io.cpp:87
std::vector< Package > packages
Definition: io.hpp:144
std::vector< std::shared_ptr< Eng3D::IO::Asset::Base > > get_multiple_prefix(const Eng3D::IO::Path &path)
Obtains all assets starting with a given prefix.
Definition: io.cpp:169
std::vector< std::string > get_paths(void) const
Obtain all the paths that are currently under the management of a package, that is return the absolut...
Definition: io.cpp:181
std::shared_ptr< Eng3D::IO::Asset::Base > get_unique(const Eng3D::IO::Path &path)
Obtaining an unique asset means the "first-found" policy applies.
Definition: io.cpp:146
Implements the I/O functions for interacting with assets, please note that this is however outdated b...
Definition: io.hpp:41
SeekType
Definition: io.hpp:70
virtual void write(const void *, size_t)
Definition: io.hpp:83
virtual void open()
Definition: io.hpp:80
virtual void close()
Definition: io.hpp:81
virtual size_t get_size(void) const
Definition: io.hpp:85
virtual void seek(Eng3D::IO::SeekType, int)
Definition: io.hpp:84
std::string abs_path
Definition: io.hpp:87
std::string get_abs_path() const
Get the abs path object in a safe manner, such as that the access does not occur on null pointers....
Definition: io.cpp:38
virtual ~Base()=default
std::string path
Definition: io.hpp:86
virtual void read(void *, size_t)
Definition: io.hpp:82
std::string read_all(void)
Read the entire file into a string.
Definition: io.hpp:90
A "file" version of the base asset, mostly to identify an asset on a physical disk.
Definition: io.hpp:103
virtual void write(const void *buf, size_t n)
Definition: io.cpp:60
virtual void seek(Eng3D::IO::SeekType type, int offset)
Definition: io.cpp:64
virtual void read(void *buf, size_t n)
Definition: io.cpp:56
virtual void open()
Definition: io.cpp:45
virtual size_t get_size(void) const
Definition: io.cpp:74
virtual void close()
Definition: io.cpp:51
A package containing a set of assets.
Definition: io.hpp:124
std::string abs_path
Definition: io.hpp:126
std::string name
Definition: io.hpp:125
std::vector< std::shared_ptr< Eng3D::IO::Asset::Base > > user_assets
Definition: io.hpp:129
std::vector< std::shared_ptr< Eng3D::IO::Asset::Base > > assets
Definition: io.hpp:127
std::string user_abs_path
Definition: io.hpp:128
The path class abstracts away most of the burden from handling system-dependant filesystem paths.
Definition: io.hpp:44
Path()=default
Path(const char *path)
Definition: io.hpp:52
std::string str
Definition: io.hpp:59
~Path()=default
Path(const std::string &path)
Definition: io.hpp:46