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

Commit

Permalink
VFS: 2/5 - LodFS
Browse files Browse the repository at this point in the history
  • Loading branch information
corwinn committed Jan 21, 2023
1 parent 2fe1ad3 commit 4da700d
Show file tree
Hide file tree
Showing 11 changed files with 669 additions and 43 deletions.
119 changes: 119 additions & 0 deletions game/h3r_lodfs.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/**** 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_lodfs.h"
#include "h3r_log.h"

H3R_NAMESPACE

static int const H3R_LOD_SIGN {0x444f4c};
static int const H3R_LOD_MAX_ENTRIES {1<<13};
static int const H3R_LOD_UNK1 {4}; // unknown 4 bytes
static int const H3R_LOD_UNK2 {80};// unknown 80 bytes

LodFS::LodFS(const String & fname)
: VFS {fname}, _s {fname, H3R_NS::OS::FileStream::Mode::ReadOnly},
_rrs {&_s, 0, 0}, _zis {&_rrs, 0, 0}
{
union { int isign; unsigned char sign[4]; };
int cnt {0};
var FN_cleanup {fname.AsZStr ()};
var FN {FN_cleanup.Data ()};

Stream::Read (_s, &isign);
if (H3R_LOD_SIGN != isign) {
Log::Err (String::Format ("%s: Unknown signature: %00000008Xd" EOL,
FN, isign));
}
OS::Log_stdout ("%s: sign: %s" EOL, FN, sign);

_s.Seek (H3R_LOD_UNK1); //TODO what are those? - c8 @ H3bitmap.lod

Stream::Read (_s, &cnt);
if (cnt <= 0 || cnt > H3R_LOD_MAX_ENTRIES) {
Log::Err (
String::Format ("%s: Suspicious entry count: %d" EOL, FN, cnt));
return;
}
OS::Log_stdout ("%s: entries: %d" EOL, FN, cnt);

_s.Seek (H3R_LOD_UNK2); //TODO what are those? H3bitmap.lod

_entries.Resize (cnt);
var data = static_cast<Entry *>(_entries);
Stream::Read (_s, data, cnt);
/*int i {0};
for (const var & e : _entries)
OS::Log_stdout (
"%s: entry: %004d: %c (%00000008d/%00000008d) \"%s\"" EOL,
FN, i++, (e.SizeU > e.SizeC && e.SizeC > 0 ? 'C' : 'U'),
e.SizeC, e.SizeU, e.Name);*/

_usable = true;
}// LodFS::LodFS()

LodFS::~LodFS() {}

Stream & LodFS::GetStream(const LodFS::Entry & e)
{
// its compressed:
// H3bitmap.lod: entry: 3862: U (00000074/00000074) "TERRNAME.txt"
bool compressed = e.SizeU >= e.SizeC && e.SizeC > 0;
/*OS::Log_stdout ("LodFS::GetStream: compressed: %s" EOL,
(compressed ? "true" : "false"));*/
_rrs.ResetTo (e.Ofs, (compressed ? e.SizeC : e.SizeU));
return compressed
//np start: e.Ofs, size: compressed ? e.SizeC : e.SizeU
? _zis.ResetTo (e.SizeC, e.SizeU)
//np pos: 0, size: e.SizeC, usize: e.SizeU
: _rrs.ResetTo (e.Ofs, e.SizeU);
}

Stream & LodFS::Get(const String & res)//TODO come back after unifying the entry
{
//TODO binary search; sort the entry list (I think they're sorted already)
// What?! - you expected a hash? - there will be one sooner or later,
// no worries.
for (const var & e : _entries)
if (res.EqualsZStr ((const char *)e.Name)) return GetStream (e);
return VFS::Get (res);
}

void LodFS::Walk(bool (*on_entry)(Stream &, const char * name))
{
for (var & e : _entries)
if (! on_entry (GetStream (e), (const char *)e.Name)) break;
}

NAMESPACE_H3R
78 changes: 78 additions & 0 deletions game/h3r_lodfs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/**** 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_LODFS_H_
#define _H3R_LODFS_H_

#include "h3r.h"
#include "h3r_vfs.h"
#include "h3r_string.h"
#include "h3r_stream.h"
#include "h3r_filestream.h"
#include "h3r_array.h"
#include "h3r_refreadstream.h"
#include "h3r_zipinflatestream.h"

H3R_NAMESPACE

class LodFS: public VFS
{
private: OS::FileStream _s;
private: bool _usable {false};
// 1 stream for now
private: RefReadStream _rrs;
private: ZipInflateStream _zis;
#pragma pack(push, 1)
private: struct Entry final
{
unsigned char Name[16];
int Ofs; // SEEK_SET
int SizeU; // Uncompressed size [bytes]
int Type;
int SizeC; // Compressed size [bytes]
};
#pragma pack(pop)
private: Array<Entry> _entries {};
private: Stream & GetStream(const LodFS::Entry &);
public: LodFS(const String & path);
public: ~LodFS() override;
public: virtual Stream & Get(const String & name) override;
public: virtual inline operator bool() const override { return _usable; }

public: void Walk(bool (*on_entry)(Stream &, const char * name));
};

NAMESPACE_H3R

#endif
47 changes: 15 additions & 32 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,38 +40,6 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <SDL.h>
#include <SDL2/SDL_mixer.h>

// TODO: string, os::path
/*char const * const gDataRoot = "~/.wine/drive_c/games/h3";
char const * const gData1 = "Data";
char const * const gData2 = "Heroes3/Data";
char const * const gMusic = "MP3";
char const * const gMaps = "Maps";
char const * const gSave = "games";
Heroes3.snd
Heroes3.vid
H3ab_ahd.snd
H3ab_bmp.lod
H3bitmap.lod
Heroes3.snd
VIDEO.VID
H3ab_ahd.vid
H3ab_spr.lod
H3sprite.lod
H3BITMAP.LOD
H3SPRITE.LOD
H3psprit.lod
Heroes3.snd
Video.vid
h3ab_ahd.snd
h3ab_ahd.vid
h3ab_bmp.lod
h3ab_spr.lod
h3abp_bm.lod
h3abp_sp.lod*/

#include "h3r_os_error.h"
H3R_ERR_DEFINE_UNHANDLED
H3R_ERR_DEFINE_HANDLER(Memory,H3R_ERR_HANDLER_UNHANDLED)
Expand Down Expand Up @@ -119,6 +87,9 @@ button click : Data_Heroes3_snd/BUTTON.wav
#include "h3r_gamearchives.h"
#include "h3r_asyncfsenum.h"

#include "h3r_lodfs.h"
#include "h3r_array.h"

#include "h3r_asyncadapter.h"
#include "h3r_taskthread.h"
class foo
Expand Down Expand Up @@ -207,6 +178,18 @@ int main(int argc, char ** argv)
H3R_NS::OS::Log_stdout ("Scaned: %d files, and %d folders" EOL,
test_file_enum.Files (), test_file_enum.Directories ());

H3R_NS::LodFS lod_fs1 {"test.lod"};
var & stream1 = lod_fs1.Get ("Rmg.txt");
H3R_NS::OS::Log_stdout ("stream1.size: %zu" EOL, stream1.Size ());
H3R_NS::Array<unsigned char> stream1_data {};
stream1_data.Resize (stream1.Size ());
unsigned char * stream1_data_buf = stream1_data;
stream1.Read (stream1_data_buf, stream1.Size ());
H3R_NS::OS::FileStream stream1_to_file {
"Rmg.txt",
H3R_NS::OS::FileStream::Mode::WriteOnly};
stream1_to_file.Write (stream1_data_buf, stream1.Size ());

/*if (SDL_Init (SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0)
return H3R_NS::Log::Info (H3R_NS::String::Format (
"SDL_Init error: %s" EOL, SDL_GetError ())), 9;
Expand Down
85 changes: 85 additions & 0 deletions stream/h3r_refreadstream.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/**** 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_refreadstream.h"

H3R_NAMESPACE

RefReadStream::RefReadStream(Stream * s, off_t start, off_t size)
: Stream {s}, _start{start}, _size{size}
{
Reset ();
}

Stream & RefReadStream::Seek(off_t ofs)
{
H3R_ENSURE(_pos + ofs <= _size, "Bug: RefReadStream: seek overflow")
_pos += ofs;
return *this;
}

Stream & RefReadStream::Read(void * buf, size_t bytes)
{
var p = _start + _pos;
if (Stream::Tell () != p) Stream::Seek (p - Stream::Tell ());
H3R_ENSURE(Stream::Tell () == p, "Bug: RefReadStream: can't sync to base")
H3R_ENSURE(_pos + static_cast<off_t>(bytes) <= _size,
"Bug: RefReadStream: read overflow")
return _pos += bytes, Stream::Read (buf, bytes), *this;
}

Stream & RefReadStream::Write(const void *, size_t)
{
H3R_ENSURE(false, "RefReadStream: Write is not supported.")
}

Stream & RefReadStream::ResetTo(off_t start, off_t size)
{
_start = {start};
_size = {size};
Reset ();
return *this;
}

void RefReadStream::Reset()
{
_ok = false;
_pos = 0;
Stream::Seek (_start - Stream::Tell ());
H3R_ENSURE(
Stream::Tell () == _start, "Bug: RefReadStream: can't sync to base")
_ok = true;//TODO should this be used for all ops?
}

NAMESPACE_H3R

0 comments on commit 4da700d

Please sign in to comment.