Skip to content
This repository has been archived by the owner on Aug 23, 2023. It is now read-only.

Commit

Permalink
VFS: 1/5 - stream that
Browse files Browse the repository at this point in the history
  • Loading branch information
corwinn committed Jan 20, 2023
1 parent ffcbd70 commit dd36591
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 36 deletions.
2 changes: 2 additions & 0 deletions os/h3r_os.h
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,8 @@ template <typename T> bool EnumFiles(T & c, const char * dn,
return true;
} // EnumFiles

//LATER - per OS - get_process_path

} // namespace OS
NAMESPACE_H3R

Expand Down
41 changes: 41 additions & 0 deletions stream/h3r_nostream.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**** BEGIN LICENSE BLOCK ****
BSD 3-Clause License
Copyright (c) 2021-2023, the wind.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**** END LICENCE BLOCK ****/

#include "h3r_nostream.h"

H3R_NAMESPACE

Stream * Stream::NoStream() { static class NoStream s; return &s; }

NAMESPACE_H3R
61 changes: 61 additions & 0 deletions stream/h3r_nostream.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**** BEGIN LICENSE BLOCK ****
BSD 3-Clause License
Copyright (c) 2021-2023, the wind.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**** END LICENCE BLOCK ****/

#ifndef _H3R_NOSTREAM_H_
#define _H3R_NOSTREAM_H_

#include "h3r_stream.h"

H3R_NAMESPACE

// A stream that does nothing, including not decorating anything.
class NoStream final : public Stream
{
public: NoStream() : Stream {} {}
public: NoStream(Stream * s) : Stream {s} {}
public: ~NoStream() override {}
public: inline operator bool() override { return false; }
public: inline Stream & Seek(off_t) override { return *this; }
public: inline off_t Tell() override { return 0; }
public: inline off_t Size() override { return 0; }
public: inline Stream & Read(void *, size_t) override { return *this; }
public: inline Stream & Write(const void *, size_t) override
{
return *this;
}
};

NAMESPACE_H3R

#endif
8 changes: 6 additions & 2 deletions stream/h3r_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,14 @@ H3R_NAMESPACE

class Stream
{
private: Stream * _f {};
// The book states the decorator shall differ the component, but it fails
// to give reason; so lets find out:
private: Stream * _f{};

public: Stream(Stream * s) : _f {s} {};
public: Stream(Stream * s) : _f{nullptr == s ? NoStream () : s} {}
protected: Stream() {} // no decorator constructor
public: virtual ~Stream() {}
protected: static Stream * NoStream(); // no "if (_f)"
protected: inline const Stream * BaseStream() const { return _f; }

// true when the stream is ok
Expand Down
13 changes: 4 additions & 9 deletions utils/h3r_vfs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,13 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

H3R_NAMESPACE

VFS::VFS(String)
{
}
VFS::VFS(const String &) {}

VFS::~VFS()
{
}
VFS::~VFS() {}

Stream VFS::Get(String)
Stream VFS::Get(const String &)
{
Stream s {nullptr};
return s;
return static_cast<Stream &&>(Stream {nullptr});
}

NAMESPACE_H3R
31 changes: 6 additions & 25 deletions utils/h3r_vfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,32 +45,15 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

H3R_NAMESPACE

// Singleton. The 2nd instance will get you an exit() with an assertion failed.
//
// Access .mp3, .h3m, .h3c, .h3r (savegame), .wav, .pcx, .def, .txt, etc.
// Currently this VFS is using the POSIX tolower() - and I have no idea how
// it handles "Unicode", so unless I specifically find the time to enable
// "unicode" support, consider ASCII to be the safe choice for your filenames.
//
// For in-game resources:
// 1st the original VFS will be searched, and next the FS. The FS files can
// override the VFS ones, but the names shall be distinct per FS.
class VFS final
//TODO unicode support,
// Consider ASCII to be the safe choice for your filenames.
class VFS
{
// Due to case-sensitive FS-es, file enumeration shall be used to set the
// required game directories and file paths.
// The different ones: AB overrides when there is no SoD installed;
// SoD overrides otherwise, but - there are text files that has to be
// merged it seems? - see "vfs_info"
//TODO check with AB installed only

public: enum class FileType {nvm,
bik, def, fnt, h3c, h3m, h3r, mp3, msk, pal, pcx, smk, txt, wav};

// Best to get that from main()
//LATER - per OS - get_process_path
public: VFS(String process_dir);
public: ~VFS();
public: VFS(const String & path);
public: virtual ~VFS();

// You request by name - you get a stream. You request the same name
// again - you get another stream. What you do with the returned stream
Expand All @@ -83,9 +66,7 @@ class VFS final
// use-case 1: on_game_start (cache all needed resources into RAM).
// use-case 2: on_map_load (cache all needed resources into RAM).
// use-case 3: on_map_load (invalidate, and do use-case #2)
// The role of this object is to abstract away data access - caching is
// not part of its duties.
public: Stream Get(String name);
public: virtual Stream Get(const String & name);
};

NAMESPACE_H3R
Expand Down

0 comments on commit dd36591

Please sign in to comment.