This repository has been archived by the owner on Aug 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
278 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/**** 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_vidfs.h" | ||
#include "h3r_log.h" | ||
|
||
H3R_NAMESPACE | ||
|
||
static int const H3R_VID_MAX_ENTRIES {1<<10}; | ||
|
||
VidFS::VidFS(const String & fname) | ||
: VFS {fname}, _s {fname, H3R_NS::OS::FileStream::Mode::ReadOnly}, | ||
_rrs {&_s, 0, 0}, _last_offset{_s.Size ()} | ||
{ | ||
int cnt {0}; | ||
var FN_cleanup {fname.AsZStr ()}; | ||
var FN {FN_cleanup.Data ()}; | ||
|
||
Stream::Read (_s, &cnt); | ||
if (cnt <= 0 || cnt > H3R_VID_MAX_ENTRIES) { | ||
Log::Err ( | ||
String::Format ("%s: Suspicious entry count: %d" EOL, FN, cnt)); | ||
return; | ||
} | ||
OS::Log_stdout ("%s: entries: %d" EOL, FN, cnt); | ||
|
||
_entries.Resize (cnt); | ||
var data = static_cast<VidFS::Entry *>(_entries); | ||
Stream::Read (_s, data, cnt); | ||
// Validate | ||
for (size_t i = 0; i < _entries.Length (); i++) { | ||
int a = _entries[i].Ofs; | ||
int b = i < _entries.Length ()-1 ? _entries[i+1].Ofs : _last_offset; | ||
if (b <= a) { | ||
Log::Err (String::Format ("%s: Wrong Entry[%003d].Ofs: " | ||
"Entry%003d.Ofs: %zu >= Entry%003d.Ofs: %zu" EOL, | ||
FN, i, i, a, i+1, b)); | ||
return; | ||
} | ||
_entries[i].Name[39] = '\0'; | ||
} | ||
/*int j {0}; | ||
for (const var & e : _entries) | ||
OS::Log_stdout ( | ||
"%s: entry: %004d: %00000008d \"%s\"" EOL, | ||
FN, j++, e.Ofs, e.Name);*/ | ||
_usable = true; | ||
}// VidFS::VidFS() | ||
|
||
VidFS::~VidFS() {} | ||
|
||
Stream & VidFS::GetStream(const VidFS::Entry & e, int size) | ||
{ | ||
//np start: e.Ofs, size: size | ||
return _rrs.ResetTo (e.Ofs, size); | ||
} | ||
|
||
Stream & VidFS::Get(const String & res) | ||
{ | ||
for (size_t i = 0; i < _entries.Length (); i++) | ||
if (res.EqualsZStr ((const char *)_entries[i].Name)) | ||
return GetStream (_entries[i], GetSize (i)); | ||
return VFS::Get (res); | ||
} | ||
|
||
int VidFS::GetSize(size_t idx) | ||
{ | ||
var size = (idx < _entries.Length ()-1 ? _entries[idx+1].Ofs : _last_offset) | ||
- _entries[idx].Ofs; | ||
H3R_ENSURE(size > 0, "Validate again") | ||
return size; | ||
} | ||
|
||
void VidFS::Walk(bool (*on_entry)(Stream &, const VFS::Entry &)) | ||
{ | ||
static VFS::Entry vfs_e {}; | ||
for (size_t i = 0; i < _entries.Length (); i++) { | ||
vfs_e.Name = reinterpret_cast<const char *>(_entries[i].Name); | ||
vfs_e.Size = GetSize (i); | ||
if (! on_entry (GetStream (_entries[i], vfs_e.Size), vfs_e)) break; | ||
} | ||
} | ||
|
||
NAMESPACE_H3R |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/**** 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_VIDFS_H_ | ||
#define _H3R_VIDFS_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" | ||
|
||
H3R_NAMESPACE | ||
|
||
class VidFS final : public VFS | ||
{ | ||
#define public public: | ||
#define private private: | ||
|
||
private OS::FileStream _s; | ||
private bool _usable {false}; | ||
// 1 stream for now | ||
private RefReadStream _rrs; | ||
private off_t _last_offset {}; | ||
#pragma pack(push, 1) | ||
private struct Entry final | ||
{ | ||
unsigned char Name[40]; | ||
int Ofs; // SEEK_SET | ||
}; | ||
#pragma pack(pop) | ||
private Array<VidFS::Entry> _entries {}; | ||
private Stream & GetStream(const VidFS::Entry &, int); | ||
private int GetSize(size_t); | ||
public VidFS(const String & path); | ||
public ~VidFS() override; | ||
public Stream & Get(const String & name) override; | ||
public inline operator bool() const override { return _usable; } | ||
|
||
public void Walk(bool (*)(Stream &, const VFS::Entry &)) override; | ||
|
||
#undef public | ||
#undef private | ||
}; | ||
|
||
NAMESPACE_H3R | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/**** 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 ****/ | ||
|
||
// vid file format unpacker; it unpacks to the current directory! | ||
// H3R_MM rule: when the engine is built -DH3R_MM, so does this | ||
//c clang++ -std=c++11 -I. -Ios -Ios/posix -Iutils -Istream -Igame -DH3R_MM -O0 -g -DH3R_DEBUG -fsanitize=address,undefined,integer,leak -fvisibility=hidden -fno-exceptions -fno-threadsafe-statics unpack_vid_vfs.cpp -o unpack_vid_vfs main.a h3r_game.o -lz | ||
|
||
#include "h3r_os_error.h" | ||
H3R_ERR_DEFINE_UNHANDLED | ||
H3R_ERR_DEFINE_HANDLER(Memory,H3R_ERR_HANDLER_UNHANDLED) | ||
H3R_ERR_DEFINE_HANDLER(Log,H3R_ERR_HANDLER_UNHANDLED) | ||
|
||
#include "h3r_filestream.h" | ||
#include "h3r_game.h" | ||
#include "h3r_vidfs.h" | ||
|
||
// allow file replacement | ||
H3R_NAMESPACE | ||
namespace OS { class FileErrReplace final : public Error | ||
{ | ||
public: bool Handled(Error * e = nullptr) override | ||
{ | ||
FileError * fe = static_cast<FileError *>(e); | ||
if (FileError::Op::Replace == fe->Op) | ||
return fe->Replace = true; | ||
return false; | ||
} | ||
}; } | ||
NAMESPACE_H3R | ||
static H3R_NS::OS::FileErrReplace my_file_err; | ||
H3R_ERR_DEFINE_HANDLER(File, my_file_err) | ||
|
||
int main(int c, char ** v) | ||
{//TODO async IO, with the main thread displaying progress; and wise quotes | ||
if (2 != c) | ||
return printf ("usage: unpack_vid vidfile\n"); | ||
|
||
// init the log service | ||
H3R_NS::Game game; | ||
|
||
H3R_NS::VidFS {v[1]} | ||
.Walk([](H3R_NS::Stream & stream, const H3R_NS::VFS::Entry & e) -> bool | ||
{ | ||
H3R_NS::OS::FileStream { | ||
e.Name, H3R_NS::OS::FileStream::Mode::WriteOnly} | ||
.H3R_NS::Stream::Write (stream); | ||
return true; | ||
}); | ||
|
||
return 0; | ||
} |