Skip to content

Commit

Permalink
#19 Add converter for .bik files
Browse files Browse the repository at this point in the history
We just strip off the 16 bytes at the start. Never seen them change, so why bother keeping them?

Fixes #19
  • Loading branch information
Xaymar committed Feb 29, 2024
1 parent 32c9201 commit b63bbeb
Show file tree
Hide file tree
Showing 9 changed files with 175 additions and 9 deletions.
3 changes: 1 addition & 2 deletions source/converter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ namespace hellextractor::converter {
typedef std::function<std::shared_ptr<hellextractor::converter::base>(helldivers2::data::meta_t meta)> function_t;

struct do_register {

public:
do_register(std::list<uint64_t> types, function_t fn);
};
Expand All @@ -41,7 +40,7 @@ namespace hellextractor::converter {
public:
virtual ~base();
base(helldivers2::data::meta_t meta);

/** Retrieve a list of outputs
*
* Key is the section that is exported.
Expand Down
55 changes: 55 additions & 0 deletions source/converter_bik.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2024 Michael Fabian 'Xaymar' Dirks <info@xaymar.com>
//
// 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.

#include "converter_bik.hpp"
#include <fstream>
#include <string_view>
#include "converter.hpp"
#include "endian.h"

static constexpr uint64_t type = 0x18fa2930f06559aaull; // bik
static constexpr std::string_view section_default = "bik";

static auto instance = hellextractor::converter::registry::do_register(
std::list<uint64_t>{
type,
},
[](helldivers2::data::meta_t meta) { return std::make_shared<hellextractor::converter::bik>(meta); });

hellextractor::converter::bik::~bik() {}

hellextractor::converter::bik::bik(helldivers2::data::meta_t meta) : base(meta), _bik(meta) {}

std::map<std::string, std::pair<size_t, std::string>> hellextractor::converter::bik::outputs()
{
return {
{std::string{section_default},
{
_bik.size(),
_bik.extension(),
}},
};
}

void hellextractor::converter::bik::extract(std::string section, std::filesystem::path path)
{
if (section_default == section) { // Extract "texture" section.
std::ofstream stream{path, std::ios::trunc | std::ios::binary | std::ios::out};
if (!stream || stream.bad() || !stream.is_open()) {
throw std::runtime_error("Unable to open output file");
}

for (auto const& section : _bik.sections()) {
stream.write(reinterpret_cast<char const*>(section.first), section.second);
}

stream.close();
}
}
28 changes: 28 additions & 0 deletions source/converter_bik.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2024 Michael Fabian 'Xaymar' Dirks <info@xaymar.com>
//
// 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.

#pragma once
#include "converter.hpp"
#include "hd2_bik.hpp"
#include "hd2_data.hpp"

namespace hellextractor::converter {
class bik : public base {
helldivers2::bik _bik;

public:
virtual ~bik();
bik(helldivers2::data::meta_t meta);

std::map<std::string, std::pair<size_t, std::string>> outputs() override;

void extract(std::string section, std::filesystem::path path) override;
};
} // namespace hellextractor::converter
6 changes: 3 additions & 3 deletions source/converter_texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include "hd2_texture.hpp"

static constexpr uint64_t type = 0x329ec6a0c63842cdull; // texture
static constexpr std::string_view section_texture = "texture";
static constexpr std::string_view section_default = "texture";

static auto instance = hellextractor::converter::registry::do_register(
std::list<uint64_t>{
Expand All @@ -31,7 +31,7 @@ hellextractor::converter::texture::texture(helldivers2::data::meta_t meta) : bas
std::map<std::string, std::pair<size_t, std::string>> hellextractor::converter::texture::outputs()
{
return {
{std::string{section_texture},
{std::string{section_default},
{
_texture.size(),
_texture.extension(),
Expand All @@ -41,7 +41,7 @@ std::map<std::string, std::pair<size_t, std::string>> hellextractor::converter::

void hellextractor::converter::texture::extract(std::string section, std::filesystem::path path)
{
if (section_texture == section) { // Extract "texture" section.
if (section_default == section) { // Extract "texture" section.
std::ofstream stream{path, std::ios::trunc | std::ios::binary | std::ios::out};
if (!stream || stream.bad() || !stream.is_open()) {
throw std::runtime_error("Unable to open output file");
Expand Down
40 changes: 40 additions & 0 deletions source/hd2_bik.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2024 Michael Fabian 'Xaymar' Dirks <info@xaymar.com>
//
// 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.

#include "hd2_bik.hpp"

helldivers2::bik::~bik() {}

helldivers2::bik::bik(helldivers2::data::meta_t meta) : _meta(meta)
{
_header = reinterpret_cast<decltype(_header)>(_meta.main);
_data_header = reinterpret_cast<decltype(_data_header)>(reinterpret_cast<uint8_t const*>(_header) + sizeof(header_t));
_data_header_sz = _meta.main_size - sizeof(header_t);
_data = reinterpret_cast<decltype(_data)>(_meta.stream ? _meta.stream : _meta.gpu);
_data_sz = _meta.stream_size ? _meta.stream_size : _meta.gpu_size;
}

size_t helldivers2::bik::size()
{
return _data_header_sz + _data_sz;
}

std::string helldivers2::bik::extension()
{
return "bik";
}

std::list<std::pair<void const*, size_t>> helldivers2::bik::sections()
{
return {
{_data_header, _data_header_sz},
{_data, _data_sz},
};
}
45 changes: 45 additions & 0 deletions source/hd2_bik.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2024 Michael Fabian 'Xaymar' Dirks <info@xaymar.com>
//
// 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.

#pragma once
#include <cinttypes>
#include <cstddef>
#include <list>
#include "hd2_data.hpp"

namespace helldivers2 {
class bik {
public:
struct header_t {
uint32_t __unk0;
uint32_t __unk1;
uint32_t __unk2;
uint32_t __unk3;
};

private:
helldivers2::data::meta_t _meta;
header_t const* _header;
uint8_t const* _data_header;
size_t _data_header_sz;
uint8_t const* _data;
size_t _data_sz;

public:
~bik();
bik(helldivers2::data::meta_t meta);

size_t size();

std::string extension();

std::list<std::pair<void const*, size_t>> sections();
};
} // namespace helldivers2
4 changes: 2 additions & 2 deletions source/hd2_texture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ namespace helldivers2 {
helldivers2::data::meta_t _meta;
header_t const* _header;
uint8_t const* _data_header;
size_t _data_header_sz;
size_t _data_header_sz;
uint8_t const* _data;
size_t _data_sz;
size_t _data_sz;

public:
~texture();
Expand Down
1 change: 0 additions & 1 deletion source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ try {
// }
//}


return 0;
} catch (const std::exception& ex) {
std::cerr << "Exception: " << ex.what() << std::endl;
Expand Down
2 changes: 1 addition & 1 deletion source/mapped_file.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class mapped_file {
std::filesystem::path _path;
std::shared_ptr<void> _file;
std::shared_ptr<void> _map;
uint8_t const* _ptr;
uint8_t const* _ptr;

public:
mapped_file();
Expand Down

0 comments on commit b63bbeb

Please sign in to comment.