Skip to content

Commit

Permalink
[bxon] tpGxTexHead support
Browse files Browse the repository at this point in the history
[kaineconv] tool to convert xap files
  • Loading branch information
yretenai committed Apr 25, 2021
1 parent dc46ab5 commit 486befa
Show file tree
Hide file tree
Showing 32 changed files with 331 additions and 106 deletions.
6 changes: 3 additions & 3 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
BasedOnStyle: LLVM
AccessModifierOffset: -4
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: false
AlignOperands: true
AlignConsecutiveAssignments: None
AlignOperands: Align
AllowAllArgumentsOnNextLine: false
AllowAllConstructorInitializersOnNextLine: false
AllowAllParametersOfDeclarationOnNextLine: false
Expand Down Expand Up @@ -56,7 +56,7 @@ SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: true
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesBeforeTrailingComments: 0
SpacesInAngles: false
SpacesInCStyleCastParentheses: false
SpacesInContainerLiterals: false
Expand Down
6 changes: 3 additions & 3 deletions kaine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ set_target_properties(kaine PROPERTIES POSITION_INDEPENDENT_CODE 1)
target_compile_definitions(kaine PRIVATE DRAGON_LIBRARY_NAME="kaine")

if (MSVC)
generate_export_header(kaine BASE_NAME kaine DEFINE_NO_DEPRECATED EXPORT_FILE_NAME ${PROJECT_SOURCE_DIR}/public/kaine/kaine_export_win.a.h)
generate_export_header(kaine BASE_NAME kaine DEFINE_NO_DEPRECATED EXPORT_FILE_NAME ${PROJECT_SOURCE_DIR}/public/kaine/generated/kaine_export_win.a.h)
elseif (WIN32_CLANG)
generate_export_header(kaine BASE_NAME kaine DEFINE_NO_DEPRECATED EXPORT_FILE_NAME ${PROJECT_SOURCE_DIR}/public/kaine/kaine_export_clang.a.h)
generate_export_header(kaine BASE_NAME kaine DEFINE_NO_DEPRECATED EXPORT_FILE_NAME ${PROJECT_SOURCE_DIR}/public/kaine/generated/kaine_export_clang.a.h)
else ()
generate_export_header(kaine BASE_NAME kaine DEFINE_NO_DEPRECATED EXPORT_FILE_NAME ${PROJECT_SOURCE_DIR}/public/kaine/kaine_export_linux.a.h)
generate_export_header(kaine BASE_NAME kaine DEFINE_NO_DEPRECATED EXPORT_FILE_NAME ${PROJECT_SOURCE_DIR}/public/kaine/generated/kaine_export_linux.a.h)
endif ()

target_include_directories(kaine PUBLIC ${PROJECT_SOURCE_DIR}/public)
Expand Down
7 changes: 3 additions & 4 deletions kaine/private/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
project(kaine)

target_sources(kaine PRIVATE kaine.cpp)
target_sources(kaine PRIVATE kaine.cpp arc.cpp bxon.cpp pack.cpp)

add_subdirectory("bxon_types")

add_subdirectory("arc")
add_subdirectory("bxon")
add_subdirectory("pack")
2 changes: 1 addition & 1 deletion kaine/private/arc/arc.cpp → kaine/private/arc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Created by Lilith on 2021-04-24.
//

#include <kaine/arc/arc.hpp>
#include <kaine/arc.hpp>
#include <zstd.h>

#include <iostream>
Expand Down
3 changes: 0 additions & 3 deletions kaine/private/arc/CMakeLists.txt

This file was deleted.

2 changes: 1 addition & 1 deletion kaine/private/bxon/bxon.cpp → kaine/private/bxon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Created by Lilith on 2021-04-24.
//

#include <kaine/bxon/bxon.hpp>
#include <kaine/bxon.hpp>

#include <cassert>
#include <standard_dragon/exception/not_implemented.hpp>
Expand Down
5 changes: 0 additions & 5 deletions kaine/private/bxon/CMakeLists.txt

This file was deleted.

3 changes: 0 additions & 3 deletions kaine/private/bxon/types/CMakeLists.txt

This file was deleted.

3 changes: 3 additions & 0 deletions kaine/private/bxon_types/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
project(kaine_bxon_types)

target_sources(kaine PRIVATE tp_archive_file_param.cpp tp_gx_tex_head.cpp)
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Created by Lilith on 2021-04-24.
//

#include <kaine/bxon/types/tp_archive_file_param.hpp>
#include <kaine/bxon_types/tp_archive_file_param.hpp>

#include <cassert>
#include <iostream>
Expand All @@ -16,12 +16,12 @@ kaine::bxon_types::tp_archive_file_param::tp_archive_file_param(std::shared_ptr<
assert(data_end - data_start == EXPECTED_DATA_SIZE);
#endif

if (buffer->size() < EXPECTED_DATA_SIZE) { throw dragon::exception::invalid_data("Buffer passed to bxon is not a valid bxon buffer."); }
if (buffer->size() < EXPECTED_DATA_SIZE) { throw dragon::exception::invalid_data("Buffer passed to tp_archive_file_param is not a valid tp_archive_file_param buffer."); }

buffer->copy(data_start, 0, EXPECTED_DATA_SIZE);

uint8_t *ptr = buffer->data();
auto offset = 0u;
uint32_t offset;

if (rel_offset_archive_list > 0) {
offset = rel_offset_archive_list + ARC_PARAM_OFFSET;
Expand Down
85 changes: 85 additions & 0 deletions kaine/private/bxon_types/tp_gx_tex_head.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
//
// Created by Lilith on 2021-04-25.
//

#include <kaine/bxon_types/tp_gx_tex_head.hpp>

#include <cassert>

#include <standard_dragon/dds_support.hpp>
#include <standard_dragon/exception/invalid_data.hpp>

kaine::bxon_types::tp_gx_tex_head::tp_gx_tex_head(std::shared_ptr<dragon::Array<uint8_t>> &buffer) {
auto data_start = reinterpret_cast<uintptr_t>(&width);
#ifndef NDEBUG
auto data_end = reinterpret_cast<uintptr_t>(&rel_offset_mip_surface) + sizeof(uint32_t);
assert(data_end - data_start == EXPECTED_DATA_SIZE);
#endif

if (buffer->size() < EXPECTED_DATA_SIZE) { throw dragon::exception::invalid_data("Buffer passed to tp_gx_tex_head is not a valid tp_gx_tex_head buffer."); }

buffer->copy(data_start, 0, EXPECTED_DATA_SIZE);

uint8_t *ptr = buffer->data();
uint32_t offset;

if (rel_offset_mip_surface > 0) {
offset = rel_offset_mip_surface + MIP_SURFACE_OFFSET;
mip_surfaces = dragon::Array<TexHeadMip>(reinterpret_cast<TexHeadMip *>(ptr + offset), mip_surface_count, true);
}
}

std::shared_ptr<dragon::Array<uint8_t>> kaine::bxon_types::tp_gx_tex_head::generate_dds(std::shared_ptr<dragon::Array<uint8_t>> &resource, uintptr_t *offset) const {
auto dds = standard_dragon::support::DDS{};
dds.dx9.linear_size = total_image_size;
dds.dx9.width = width;
dds.dx9.height = height;
dds.dx9.mip_count = mip_surface_count;
dds.dx10.array_size = surfaces;
switch (format) {
default:
case XonSurfaceFormat::UNKNOWN:
std::cerr << "warn: unrecognized XonSurfaceFormat 0x" << HEXLOG32 << static_cast<uint32_t>(format) << " defaulting to BC3_UNORM" << std::endl;
dds.dx10.format = standard_dragon::support::DXGIFormat::BC3_UNORM;
break;
case XonSurfaceFormat::R8G8B8A8_UNORM:
dds.dx10.format = standard_dragon::support::DXGIFormat::R8G8B8A8_UNORM;
break;
case XonSurfaceFormat::R8G8B8A8_UNORM_SRGB:
dds.dx10.format = standard_dragon::support::DXGIFormat::R8G8B8A8_UNORM_SRGB;
break;
case XonSurfaceFormat::BC1_UNORM:
dds.dx10.format = standard_dragon::support::DXGIFormat::BC1_UNORM;
break;
case XonSurfaceFormat::BC1_UNORM_SRGB:
dds.dx10.format = standard_dragon::support::DXGIFormat::BC1_UNORM_SRGB;
break;
case XonSurfaceFormat::BC2_UNORM:
dds.dx10.format = standard_dragon::support::DXGIFormat::BC2_UNORM;
break;
case XonSurfaceFormat::BC2_UNORM_SRGB:
dds.dx10.format = standard_dragon::support::DXGIFormat::BC2_UNORM_SRGB;
break;
case XonSurfaceFormat::BC3_UNORM:
dds.dx10.format = standard_dragon::support::DXGIFormat::BC3_UNORM;
break;
case XonSurfaceFormat::BC3_UNORM_SRGB:
dds.dx10.format = standard_dragon::support::DXGIFormat::BC3_UNORM_SRGB;
break;
case XonSurfaceFormat::BC4_UNORM:
dds.dx10.format = standard_dragon::support::DXGIFormat::BC4_UNORM;
break;
case XonSurfaceFormat::BC5_UNORM:
dds.dx10.format = standard_dragon::support::DXGIFormat::BC5_UNORM;
break;
case XonSurfaceFormat::R32G32B32A32_FLOAT:
dds.dx10.format = standard_dragon::support::DXGIFormat::R32G32B32A32_FLOAT;
break;
}

auto buffer = std::make_shared<dragon::Array<uint8_t>>(total_image_size + sizeof(standard_dragon::support::DDS), nullptr);
std::copy_n(reinterpret_cast<uint8_t *>(&dds), sizeof(standard_dragon::support::DDS), buffer->data());
auto ptr = reinterpret_cast<uintptr_t>(buffer->data() + sizeof(standard_dragon::support::DDS));
resource->lpcopy(&ptr, offset, total_image_size);
return buffer;
}
12 changes: 6 additions & 6 deletions kaine/private/pack/pack.cpp → kaine/private/pack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Created by Lilith on 2021-04-25.
//

#include <kaine/pack/pack.hpp>
#include <kaine/pack.hpp>

#include <cassert>

Expand Down Expand Up @@ -30,12 +30,12 @@ kaine::pack::pack(dragon::Array<uint8_t> &buffer) {
uint32_t offset;
if (rel_offset_preload > 0) {
offset = rel_offset_preload + PRELOAD_OFFSET;
preload = dragon::Array<XapPreload>(reinterpret_cast<XapPreload *>(ptr + offset), preload_count, true);
dependencies = dragon::Array<XapDependency>(reinterpret_cast<XapDependency *>(ptr + offset), preload_count, true);
for (auto i = 0; i < preload_count; ++i) {
auto param = preload[i];
auto param = dependencies[i];
if (param.rel_offset_name > 0) {
auto name_offset = offset + sizeof(XapPreload) * i + param.rel_offset_name + PRELOAD_NAME_OFFSET;
preload_names[param.id] = std::string(reinterpret_cast<const char *>(ptr + name_offset));
auto name_offset = offset + sizeof(XapDependency) * i + param.rel_offset_name + PRELOAD_NAME_OFFSET;
dependency_names[param.id] = std::string(reinterpret_cast<const char *>(ptr + name_offset));
}
}
}
Expand Down Expand Up @@ -74,7 +74,7 @@ kaine::pack::pack(dragon::Array<uint8_t> &buffer) {
}
}

if(resource_size > 0) {
if (resource_size > 0) {
resource = std::make_shared<dragon::Array<uint8_t>>(ptr + serialized_size, resource_size, true);
}
}
3 changes: 0 additions & 3 deletions kaine/private/pack/CMakeLists.txt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ namespace kaine {
std::shared_ptr<dragon::Array<uint8_t>> get_file(uint64_t offset, uint64_t size);
std::shared_ptr<dragon::Array<uint8_t>> decompress_file(uint64_t offset, uint64_t csize, uint64_t size);
};
} // namespace kaine
}// namespace kaine
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#pragma once

#include <kaine/bxon/types/abstract_bxon.hpp>
#include <kaine/bxon_types/abstract_bxon.hpp>
#include <kaine/kaine.hpp>

#include <standard_dragon/Array.hpp>
Expand Down Expand Up @@ -39,10 +39,10 @@ namespace kaine {
template<class T>
typename std::enable_if<std::is_base_of<kaine::bxon_types::abstract_bxon, T>::value, std::shared_ptr<T>>::type
get_instance() {
if (T::get_name() != name) {
throw dragon::exception::invalid_data("expected type to be " + name + " instead got " + T::get_name());
if (name != T::bxon_name) {
throw dragon::exception::invalid_data("expected type to be " + name + " instead got " + T::bxon_name);
}
return std::make_shared<T>(data);
}
};
} // namespace kaine
}// namespace kaine
10 changes: 10 additions & 0 deletions kaine/public/kaine/bxon_types.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//
// Created by Lilith on 2021-04-25.
//

#pragma once

#include <kaine/bxon_types/abstract_bxon.hpp>

#include <kaine/bxon_types/tp_archive_file_param.hpp>
#include <kaine/bxon_types/tp_gx_tex_head.hpp>
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ namespace kaine::bxon_types {
public:
virtual ~abstract_bxon() noexcept = default;
};
} // namespace kaine::bxon_types
}// namespace kaine::bxon_types
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

#pragma once

#include <kaine/arc/arc.hpp>
#include <kaine/bxon/types/abstract_bxon.hpp>
#include <kaine/arc.hpp>
#include <kaine/bxon_types/abstract_bxon.hpp>
#include <kaine/kaine_export.h>

#include <standard_dragon/dragon.hpp>
Expand Down Expand Up @@ -64,6 +64,6 @@ namespace kaine::bxon_types {
std::shared_ptr<dragon::Array<uint8_t>> read_file(kaine::arc &archive, uint32_t hash);
static std::shared_ptr<dragon::Array<uint8_t>> read_file(kaine::arc &archive, ArchiveFileParam &param);

static std::string get_name() { return "tpArchiveFileParam"; }
static constexpr const char *bxon_name = "tpArchiveFileParam";
};
} // namespace kaine::bxon_types
}// namespace kaine::bxon_types
74 changes: 74 additions & 0 deletions kaine/public/kaine/bxon_types/tp_gx_tex_head.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//
// Created by Lilith on 2021-04-25.
//

#pragma once

#include <kaine/bxon_types/abstract_bxon.hpp>
#include <kaine/kaine_export.h>

#include <standard_dragon/Array.hpp>
#include <standard_dragon/dragon.hpp>

#include <cstdint>

namespace kaine::bxon_types {
class KAINE_EXPORT tp_gx_tex_head : public abstract_bxon {
private:
static constexpr uintptr_t EXPECTED_DATA_SIZE = 0x24;
static constexpr uintptr_t MIP_SURFACE_OFFSET = 0x20;

public:
explicit tp_gx_tex_head(std::shared_ptr<dragon::Array<uint8_t>> &buffer);
~tp_gx_tex_head() override = default;

#pragma pack(push, 4)
typedef struct TEX_HEAD_MIP_SURFACE {
uint32_t offset;
uint32_t unknown_1;
uint32_t unknown_2;
uint32_t unknown_3;
uint32_t size;
uint32_t unknown_5;
uint32_t width;
uint32_t height;
uint32_t unknown_8;
uint32_t unknown_9;
} TexHeadMip;
DRAGON_ASSERT(sizeof(TEX_HEAD_MIP_SURFACE) == 0x28, "ArchiveArcParam has an invalid size");

enum class XonSurfaceFormat : uint32_t {
UNKNOWN = 0,
R8G8B8A8_UNORM = 0x00010800,
R8G8B8A8_UNORM_SRGB = 0x00010B00,
BC1_UNORM = 0x00010F00,
BC1_UNORM_SRGB = 0x00011000,
BC2_UNORM = 0x00011100,
BC2_UNORM_SRGB = 0x00011200,
BC3_UNORM = 0x00011300,
BC3_UNORM_SRGB = 0x00011400,
BC4_UNORM = 0x00011500,
BC5_UNORM = 0x00011600,
R32G32B32A32_FLOAT = 0x00030000,
};

struct {
uint32_t width = 0;
uint32_t height = 0;
uint32_t surfaces = 0;
uint32_t unknown_1 = 0;
uint32_t total_image_size = 0;
uint32_t unknown_2 = 0;
XonSurfaceFormat format = XonSurfaceFormat::UNKNOWN;
uint32_t mip_surface_count = 0;
uint32_t rel_offset_mip_surface = 0;
};
#pragma pack(pop)

dragon::Array<TexHeadMip> mip_surfaces;

std::shared_ptr<dragon::Array<uint8_t>> generate_dds(std::shared_ptr<dragon::Array<uint8_t>> &resource, uintptr_t *offset) const;

static constexpr const char *bxon_name = "tpGxTexHead";
};
}// namespace kaine::bxon_types
File renamed without changes.
File renamed without changes.
File renamed without changes.
8 changes: 4 additions & 4 deletions kaine/public/kaine/kaine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@

#include <kaine/kaine_export.h>

#include <kaine/arc/arc.hpp>
#include <kaine/bxon/bxon.hpp>
#include <kaine/pack/pack.hpp>
#include <kaine/arc.hpp>
#include <kaine/bxon.hpp>
#include <kaine/pack.hpp>

#include <string>

namespace kaine {
KAINE_EXPORT std::string get_version_str();
KAINE_EXPORT int get_version();
} // namespace kaine
}// namespace kaine
Loading

0 comments on commit 486befa

Please sign in to comment.