Gorgon Game Engine
File.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <stdexcept>
4 #include <iostream>
5 #include <fstream>
6 #include <functional>
7 #include <map>
8 #include <memory>
9 
10 #include "../Utils/Assert.h"
11 #include "Base.h"
12 #include "Folder.h"
13 #include "../Filesystem.h"
14 #include "../IO/Stream.h"
15 
16 #include "Reader.h"
17 #include "Writer.h"
18 
19 namespace Gorgon { namespace Resource {
20 
23  class Loader {
24  public:
33  typedef std::function<Base* (std::weak_ptr<File>, std::shared_ptr<Reader>, unsigned long)> LoaderFunction;
34 
36  Loader(GID::Type gid, LoaderFunction handler) : GID(gid), Handler(handler) { }
37 
39  Loader() : GID(Resource::GID::None) { }
40 
43 
46  };
47 
48 
53  class File {
54  public:
55 
57  File();
58 
60  virtual ~File() {
61  delete root;
62  }
63 
65  Folder &Root() { return *root; }
66 
69  std::unique_ptr<Folder> Release() {
70  std::unique_ptr<Folder> r(root);
71  root=new Folder;
72 
73  return r;
74  }
75 
78  void Prepare() {
79  root->Prepare();
80  }
81 
85  void Discard() {
86  root->Discard();
87 
88  decltype(mapping) temp;
89  mapping.swap(temp);
90  }
91 
94  void Destroy() {
95  delete root;
96  root=new Folder(*this);
97  fileversion=0;
99  }
100 
101 
108  void LoadFile(const std::string &filename) {
109  createfilereader(filename);
110 
111  load(false, false);
112  }
113 
119  void LoadFirst(const std::string &filename) {
120  createfilereader(filename);
121 
122  load(true, false);
123  }
124 
134  void LoadShallow(const std::string &filename) {
135  createfilereader(filename);
136 
137  load(false, true);
138  }
139 
142  void Save(const std::string &filename) {
143  writer.reset(new FileWriter(filename));
144  save();
145  writer->Close();
146  }
147 
148 
154  Base *LoadChunk(Base &self, GID::Type gid, unsigned long size, bool skipobjects=false);
155 
156 
162  Base *LoadChunk(GID::Type gid, unsigned long size);
163 
164 
167  std::weak_ptr<File> Self() const {
168  return self;
169  }
170 
171 
174  std::map<GID::Type, Loader> Loaders;
175 
176 
181  mutable std::map<SGuid, Base*> mapping;
182 
183 
184  protected:
186  void load(bool first, bool shallow);
187 
189  void save() const;
190 
193 
196 
199  bool keepopen=false;
200 
203  unsigned long fileversion;
204 
206  std::shared_ptr<Reader> reader;
207 
208  std::shared_ptr<Writer> writer;
209 
210  private:
211  void createfilereader(std::string filename);
212  std::shared_ptr<File> self;
213  };
214 } }
Gorgon::Resource::Base::Resolve
virtual void Resolve(File &file)
This function shall resolve links or similar constructs.
Definition: Base.cpp:6
Sound.h
Gorgon::Resource::File::keepopen
bool keepopen
Keeps the file open even after loading is completed.
Definition: File.h:199
Gorgon::String::From
std::enable_if< decltype(gorgon__enum_tr_loc(T_()))::isupgradedenum, std::string >::type From(const T_ &e)
Definition: Enum.h:303
Gorgon::Resource::GID::Data
constexpr Type Data
Data resource.
Definition: GID.h:164
Gorgon::Resource::File::mapping
std::map< SGuid, Base * > mapping
INTERNAL, allows guid to object mapping.
Definition: File.h:181
Gorgon::Resource::Blob::LoadResource
static Blob * LoadResource(std::weak_ptr< File > file, std::shared_ptr< Reader > reader, unsigned long size)
This function loads a blob resource from the given file.
Definition: Blob.cpp:7
Gorgon::Resource::GID::Pointer
constexpr Type Pointer
Definition: GID.h:196
Gorgon::Resource::GID::Type::AsInteger
constexpr uint32_t AsInteger() const
Returns the value of the GID as an integer.
Definition: GID.h:32
Gorgon::Resource::File::Self
std::weak_ptr< File > Self() const
Returns a weak reference to this file.
Definition: File.h:167
File.h
Gorgon::Resource::Base::GetGID
virtual GID::Type GetGID() const =0
This function shall return Gorgon ID of this resource.
Gorgon::Resource::GID::Null
constexpr Type Null
Null resource.
Definition: GID.h:105
Gorgon::Resource::File::Release
std::unique_ptr< Folder > Release()
Detaches the root of the File from the File object.
Definition: File.h:69
Gorgon::Resource::Writer::stream
std::ostream * stream
This is the stream that will be used to write data to.
Definition: Writer.h:403
Gorgon::Resource::File::File
File()
Default constructor.
Definition: File.cpp:23
Gorgon::Resource::Writer::WriteStringWithSize
void WriteStringWithSize(const std::string &value)
Writes a string from a given stream.
Definition: Writer.h:268
Gorgon::Resource::GID::StackedObject
constexpr Type StackedObject
Definition: GID.h:250
Gorgon::Resource::LoadError::VersionMismatch
@ VersionMismatch
Version in the file is not recognized.
Definition: Reader.h:31
Gorgon::Resource::File::Destroy
void Destroy()
Destroys the Gorgon resource tree that this file holds.
Definition: File.h:94
Base.h
Gorgon::Resource::File::root
Folder * root
The root folder, root changes while loading a file.
Definition: File.h:192
Gorgon::Resource::ScalableObject::LoadResource
static ScalableObject * LoadResource(std::weak_ptr< File > file, std::shared_ptr< Reader > reader, unsigned long size)
This function loads a tinted object resource from the file.
Definition: ScalableObject.cpp:34
Reader.h
Gorgon::Resource::Folder::load
bool load(std::shared_ptr< Reader > data, unsigned long size, bool first, bool shallow, bool load)
This is the actual load function.
Definition: Folder.cpp:72
Gorgon::Resource::GID::Font
constexpr Type Font
Definition: GID.h:199
Image.h
Gorgon::Resource::GID::MaskedObject
constexpr Type MaskedObject
Definition: GID.h:242
Gorgon::Resource::LoadError::FileCannotBeOpened
@ FileCannotBeOpened
Cannot open the given file.
Definition: Reader.h:40
Gorgon::Resource::FileWriter
Allows data to be written to a file.
Definition: Writer.h:409
Gorgon::Resource::File::Root
Folder & Root()
Returns the root folder of the file.
Definition: File.h:65
Gorgon::Resource::Loader::GID
Resource::GID::Type GID
Gorgon ID of the resource that this loaded can load.
Definition: File.h:42
Gorgon::Resource::Writer::Marker
Definition: Writer.h:63
Gorgon::Resource::LoadError::Containment
@ Containment
There is no containing root folder.
Definition: Reader.h:34
Gorgon::Resource::GID::Line
constexpr Type Line
Definition: GID.h:234
Gorgon::Resource::Data::LoadResource
static Data * LoadResource(std::weak_ptr< File > file, std::shared_ptr< Reader > reader, unsigned long totalsize)
Loads a data resource.
Definition: Data.cpp:18
Gorgon::Resource::GID::Image
constexpr Type Image
Image resource.
Definition: GID.h:149
Gorgon::Resource::Writer::WriteObjectStart
Marker WriteObjectStart(const Base &base)
Writes the start of an object. Should have a matching WriteEnd with the returned marker.
Definition: File.cpp:211
Gorgon::Resource::GID::Folder
constexpr Type Folder
Folder resource Gorgon::Resource::Folder.
Definition: GID.h:91
Gorgon::Resource::Writer::WriteChunkSize
void WriteChunkSize(unsigned long value)
Writes chunk size to the stream.
Definition: Writer.h:319
Gorgon::Resource::Folder::save
void save(Writer &writer) const override
Definition: Folder.cpp:137
Gorgon::Resource::Base::begin
const Containers::Collection< Base >::ConstIterator begin() const
Allows easy iteration through range based fors.
Definition: Base.h:100
Gorgon::Resource::Loader
This class defines a resource loader.
Definition: File.h:23
Gorgon::Resource::GID::TintedObject
constexpr Type TintedObject
Definition: GID.h:244
Gorgon::Resource::Pointer::LoadResource
static Resource::Pointer * LoadResource(std::weak_ptr< Gorgon::Resource::File > file, std::shared_ptr< Gorgon::Resource::Reader > reader, long unsigned int size)
This function loads a bitmap font resource from the given file.
Definition: Pointer.cpp:6
Font.h
Gorgon::Resource::Writer::WriteGuid
void WriteGuid(const SGuid &value)
Writes a GUID to the given stream.
Definition: Writer.h:311
Gorgon::Resource::LoadError::Signature
@ Signature
File does not contain correct signature and is probably not a Gorgon resource.
Definition: Reader.h:28
Gorgon::Resource::File::fileversion
unsigned long fileversion
Version of the loaded file.
Definition: File.h:203
Gorgon::Resource::Writer::IsGood
bool IsGood() const
Checks if the stream is open and it can be written to.
Definition: Writer.h:104
Pointer.h
Gorgon::Resource::File::writer
std::shared_ptr< Writer > writer
Definition: File.h:208
Gorgon::Resource::Rectangle::LoadResource
static Rectangle * LoadResource(std::weak_ptr< File > file, std::shared_ptr< Reader > reader, unsigned long size)
This function loads a rectangle resource from the file.
Definition: Rectangle.cpp:83
Gorgon::Resource::File::Save
void Save(const std::string &filename)
Saves this file to the disk using the given filename.
Definition: File.h:142
Gorgon
Root namespace for Gorgon Game Engine.
Definition: Any.h:19
ScalableObject.h
Gorgon::Resource::WriteError::ErrorStrings
static const std::string ErrorStrings[3]
Strings for error codes.
Definition: Writer.h:51
Gorgon::Resource::Loader::Handler
LoaderFunction Handler
Load handler function.
Definition: File.h:45
Gorgon::Resource::File::LoadChunk
Base * LoadChunk(Base &self, GID::Type gid, unsigned long size, bool skipobjects=false)
Loads a resource object from the given file, GID and size.
Definition: File.cpp:54
Rectangle.h
Gorgon::Resource::GID::SGuid
constexpr Type SGuid
Identifies resources.
Definition: GID.h:115
ASSERT
#define ASSERT(expression, message,...)
Replaces regular assert to allow messages and backtrace.
Definition: Assert.h:161
Gorgon::Resource::Loader::Loader
Loader(GID::Type gid, LoaderFunction handler)
Filling constructor.
Definition: File.h:36
MaskedObject.h
Gorgon::Resource::File
This class represents a logical resource file.
Definition: File.h:53
Gorgon::Resource::GID::Name
constexpr Type Name
Name of a resource, names are not required to be unique.
Definition: GID.h:117
Gorgon::Encoding::Lzma
LZMA Lzma
A default constructed LZMA object.
Definition: LZMA.cpp:196
Gorgon::Resource::GID::None
constexpr Type None
Empty, different from Null resource.
Definition: GID.h:81
Gorgon::Resource::Loader::LoaderFunction
std::function< Base *(std::weak_ptr< File >, std::shared_ptr< Reader >, unsigned long)> LoaderFunction
This is Resource loader function prototype.
Definition: File.h:33
Gorgon::Resource::GID::Blob
constexpr Type Blob
Definition: GID.h:227
Gorgon::Resource::File::Loaders
std::map< GID::Type, Loader > Loaders
Resource Loaders.
Definition: File.h:174
Gorgon::Resource::Font::LoadResource
static Font * LoadResource(std::weak_ptr< File > file, std::shared_ptr< Reader > reader, unsigned long size)
This function loads a bitmap font resource from the given file.
Definition: Font.cpp:116
Gorgon::Resource::GID::Animation
constexpr Type Animation
Definition: GID.h:187
Gorgon::Resource::Base
This class is the base for all Gorgon Resources.
Definition: Base.h:20
Gorgon::Resource::Sound::LoadResource
static Sound * LoadResource(std::weak_ptr< File > file, std::shared_ptr< Reader > reader, unsigned long size)
This function loads a sound resource from the given file.
Definition: Sound.cpp:7
Gorgon::Resource::GID::Animation_Image
constexpr Type Animation_Image
Definition: GID.h:188
Blob.h
Gorgon::Resource::Base::Discard
virtual void Discard()
This function shall discard any transitional data which is not vital after Prepare function is issued...
Definition: Base.cpp:22
Gorgon::Resource::Line::LoadResource
static Line * LoadResource(std::weak_ptr< File > file, std::shared_ptr< Reader > reader, unsigned long size)
This function loads a line resource from the file.
Definition: Line.cpp:44
TintedObject.h
Gorgon::Resource::MaskedObject::LoadResource
static MaskedObject * LoadResource(std::weak_ptr< File > file, std::shared_ptr< Reader > reader, unsigned long size)
This function loads a masked object resource from the file.
Definition: MaskedObject.cpp:37
Gorgon::Resource::File::save
void save() const
This function performs the save operation.
Definition: File.cpp:197
Writer.h
Gorgon::Filesystem::Delete
bool Delete(const std::string &path)
Deletes the given file or directory.
Definition: Linux.cpp:106
Gorgon::Resource::LoadError
This class represents a loading error.
Definition: Reader.h:16
Gorgon::Resource::Base::GetGuid
virtual SGuid GetGuid() const
Returns the guid of the object.
Definition: Base.h:53
Gorgon::Resource::File::Discard
void Discard()
Discards any data that is not required after preparation.
Definition: File.h:85
Gorgon::Resource::GID::Rectangle
constexpr Type Rectangle
Definition: GID.h:238
Gorgon::Resource::File::Prepare
void Prepare()
Prepares all resources in this file to be used.
Definition: File.h:78
Data.h
Gorgon::Resource::Folder::LoadResource
static Folder * LoadResource(std::weak_ptr< File > file, std::shared_ptr< Reader > data, unsigned long size)
Definition: Folder.cpp:150
Gorgon::Resource::GID::GameFile
constexpr Type GameFile
Denotes this file is a game file.
Definition: GID.h:87
Folder.h
Line.h
Gorgon::Resource::LoadError::ErrorStrings
static const std::string ErrorStrings[8]
Strings for error codes.
Definition: Reader.h:63
Gorgon::Filesystem::IsFile
bool IsFile(const std::string &path)
Checks whether the given path is a file.
Definition: Linux.cpp:46
Gorgon::Resource::File::filetype
GID::Type filetype
Type of the loaded file.
Definition: File.h:195
Gorgon::Resource::GID::Type
Type to store GID information.
Definition: GID.h:23
Gorgon::Resource::File::LoadFile
void LoadFile(const std::string &filename)
Loads the given file.
Definition: File.h:108
Gorgon::Resource::Folder::Prepare
virtual void Prepare() override
Prepares children to be used.
Definition: Folder.cpp:12
Gorgon::Resource::File::load
void load(bool first, bool shallow)
This is the actual load function.
Definition: File.cpp:110
Gorgon::Resource::Image::LoadResource
static Image * LoadResource(std::weak_ptr< File > file, std::shared_ptr< Reader > reader, unsigned long size)
This function loads a image resource from the given file.
Definition: Image.cpp:9
Gorgon::Resource::File::LoadFirst
void LoadFirst(const std::string &filename)
Loads only the first object of the given file.
Definition: File.h:119
Gorgon::Resource::File::LoadShallow
void LoadShallow(const std::string &filename)
Loads only the first tier of objects.
Definition: File.h:134
Gorgon::Resource::Animation::LoadResource
static Animation * LoadResource(std::weak_ptr< File > file, std::shared_ptr< Reader > reader, unsigned long size)
This function loads an animation resource from the given file.
Definition: Animation.h:51
Gorgon::Resource::Writer::WriteGID
void WriteGID(GID::Type value)
Writes a GID to the given stream.
Definition: Writer.h:303
Gorgon::Resource::File::reader
std::shared_ptr< Reader > reader
The reader that would be used to read the file.
Definition: File.h:206
Gorgon::IO::ReadGuid
SGuid ReadGuid(std::istream &stream)
Reads a GUID from the given stream.
Definition: Stream.h:163
StackedObject.h
Gorgon::Resource::Reader::ReadCommonChunk
bool ReadCommonChunk(Base &self, GID::Type gid, unsigned long size)
Definition: File.cpp:41
Gorgon::Resource::StackedObject::LoadResource
static StackedObject * LoadResource(std::weak_ptr< File > file, std::shared_ptr< Reader > reader, unsigned long size)
This function loads a stacked object resource from the file.
Definition: StackedObject.cpp:36
Gorgon::Resource::Folder
This is basic folder resource, it contains other resources.
Definition: Folder.h:14
Gorgon::Resource::Base::GetName
const std::string & GetName() const
Returns the name of this object.
Definition: Base.h:61
Gorgon::Resource::GID::Sound
constexpr Type Sound
Definition: GID.h:219
Gorgon::Resource::File::~File
virtual ~File()
Destroys file object. If the root is not detached, it will destroy resource tree as well.
Definition: File.h:60
Animation.h
Gorgon::Resource::Writer::Tell
unsigned long Tell() const
Tells the current position.
Definition: Writer.h:114
Gorgon::Encoding::LZMA::Decode
void Decode(I_ &input, O_ &output, Byte *compressionproperties=nullptr, unsigned long long fsize=(unsigned long long)(long long) -1)
Decodes LZMA compressed data.
Definition: LZMA.h:317
Gorgon::Resource::Loader::Loader
Loader()
Empty constructor.
Definition: File.h:39
Gorgon::IO::ReadString
std::string ReadString(std::istream &stream)
Reads a string from a given stream.
Definition: Stream.h:135
Gorgon::Resource::GID::ScalableObject
constexpr Type ScalableObject
Definition: GID.h:247
Gorgon::Resource::TintedObject::LoadResource
static TintedObject * LoadResource(std::weak_ptr< File > file, std::shared_ptr< Reader > reader, unsigned long size)
This function loads a tinted object resource from the file.
Definition: TintedObject.cpp:35