Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crunch, minor changes #57

Merged
merged 13 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# - misc-include-cleaner: good idea but too much bother in practice
# - misc-non-private-member-variables-in-classes: I like using structs
# - misc-use-anonymous-namespace: doctest macros emit a warning with this check
# - misc-use-internal-linkage: too many false positives in my experience
# - readability-braces-around-statements: Same as above
# - readability-identifier-length: Who decided that 1 letter variable names are bad? It depends on the context
# - readability-magic-numbers: Same as above
Expand All @@ -33,6 +34,7 @@ Checks: >-
-misc-include-cleaner,
-misc-non-private-member-variables-in-classes,
-misc-use-anonymous-namespace,
-misc-use-internal-linkage,
-readability-braces-around-statements,
-readability-identifier-length,
-readability-magic-numbers,
Expand Down
1 change: 1 addition & 0 deletions cmake/config.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ find_dependency("bc7enc")
find_dependency("mpsc_channel")
find_dependency("nlohmann_json")
find_dependency("reproc++")
find_dependency("crunch2")

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}")
170 changes: 170 additions & 0 deletions cmake/ports/crunch2/fix_cmake.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 36e1d89..5bd4fa3 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.12)
-project(Crunch2 VERSION "1.2.0")
+project(crunch2 VERSION "1.2.0")

if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/conanbuildinfo.cmake")
include("${CMAKE_CURRENT_SOURCE_DIR}/conanbuildinfo.cmake")
diff --git a/cmake/config.cmake.in b/cmake/config.cmake.in
new file mode 100644
index 0000000..0fedff8
--- /dev/null
+++ b/cmake/config.cmake.in
@@ -0,0 +1 @@
+include(${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@-targets.cmake)
diff --git a/crnlib/CMakeLists.txt b/crnlib/CMakeLists.txt
index 7571707..b2ec9b7 100644
--- a/crnlib/CMakeLists.txt
+++ b/crnlib/CMakeLists.txt
@@ -133,9 +133,9 @@ set(CRNLIB_SRCS
${CMAKE_CURRENT_SOURCE_DIR}/crn_vector2d.h
${CMAKE_CURRENT_SOURCE_DIR}/crn_version.cpp
${CMAKE_CURRENT_SOURCE_DIR}/crnlib.cpp
- ${CMAKE_CURRENT_SOURCE_DIR}/../inc/crn_decomp.h
- ${CMAKE_CURRENT_SOURCE_DIR}/../inc/crnlib.h
- ${CMAKE_CURRENT_SOURCE_DIR}/../inc/dds_defs.h
+ ${CMAKE_CURRENT_SOURCE_DIR}/crn_decomp.h
+ ${CMAKE_CURRENT_SOURCE_DIR}/crnlib.h
+ ${CMAKE_CURRENT_SOURCE_DIR}/dds_defs.h
)

if(WIN32)
@@ -158,17 +158,15 @@ else()
endif()
endif()

-add_library(crn ${CRNLIB_SRCS})
-set_property(TARGET crn PROPERTY CXX_STANDARD 11)
-target_include_directories(crn
+add_library("${PROJECT_NAME}" ${CRNLIB_SRCS})
+set_property(TARGET "${PROJECT_NAME}" PROPERTY CXX_STANDARD 11)
+target_include_directories("${PROJECT_NAME}"
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
- $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../inc>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
- # PRIVATE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
)
-target_link_libraries(crn
+target_link_libraries("${PROJECT_NAME}"
PRIVATE
$<BUILD_INTERFACE:lzma>
$<BUILD_INTERFACE:jpeg-compressor>
@@ -177,19 +175,35 @@ target_link_libraries(crn
)

include(GenerateExportHeader)
-generate_export_header(crn)
+generate_export_header("${PROJECT_NAME}"
+ BASE_NAME crn
+)

if(NOT WIN32)
find_package(Threads)
- target_link_libraries(crn PUBLIC Threads::Threads)
+ target_link_libraries("${PROJECT_NAME}" PUBLIC Threads::Threads)
endif()

-install(TARGETS crn EXPORT crnTargets)
-install(EXPORT crnTargets
- FILE crunchTargets.cmake
+include(CMakePackageConfigHelpers)
+
+install(TARGETS "${PROJECT_NAME}" EXPORT "${PROJECT_NAME}-targets")
+
+install(EXPORT "${PROJECT_NAME}-targets"
NAMESPACE crunch::
- DESTINATION lib/cmake/crunch
+ DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
)
-file(GLOB CRN_INC_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/../inc/*.h)
+
+configure_file("${CMAKE_CURRENT_SOURCE_DIR}/../cmake/config.cmake.in"
+ "${PROJECT_NAME}Config.cmake" @ONLY)
+
+write_basic_package_version_file(
+ "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
+ COMPATIBILITY SameMajorVersion)
+
+install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
+ "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
+ DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}")
+
+file(GLOB CRN_INC_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/*.h)
list(APPEND CRN_INC_HEADERS ${CMAKE_CURRENT_BINARY_DIR}/crn_export.h)
-install(FILES ${CRN_INC_HEADERS} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
+install(FILES ${CRN_INC_HEADERS} DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/crunch")
diff --git a/inc/crn_decomp.h b/crnlib/crn_decomp.h
similarity index 100%
rename from inc/crn_decomp.h
rename to crnlib/crn_decomp.h
diff --git a/inc/crn_defs.h b/crnlib/crn_defs.h
similarity index 100%
rename from inc/crn_defs.h
rename to crnlib/crn_defs.h
diff --git a/crnlib/crn_mipmapped_texture.cpp b/crnlib/crn_mipmapped_texture.cpp
index f89ecba..46597c8 100644
--- a/crnlib/crn_mipmapped_texture.cpp
+++ b/crnlib/crn_mipmapped_texture.cpp
@@ -22,6 +22,7 @@
*/

#include "crn_core.h"
+#include "crn_defs.h"
#include "crn_mipmapped_texture.h"
#include "crn_cfile_stream.h"
#include "crn_image_utils.h"
@@ -29,8 +30,6 @@
#include "crn_texture_comp.h"
#include "crn_ktx_texture.h"

-#include "../inc/crn_defs.h"
-
namespace crnlib {
const vec2I g_vertical_cross_image_offsets[6] = {vec2I(2, 1), vec2I(0, 1), vec2I(1, 0), vec2I(1, 2), vec2I(1, 1), vec2I(1, 3)};

diff --git a/crnlib/crn_mipmapped_texture.h b/crnlib/crn_mipmapped_texture.h
index 2a3cdd1..061dc27 100644
--- a/crnlib/crn_mipmapped_texture.h
+++ b/crnlib/crn_mipmapped_texture.h
@@ -24,7 +24,6 @@
#pragma once

#include "crn_dxt_image.h"
-#include "../inc/dds_defs.h"
#include "crn_pixel_format.h"
#include "crn_image.h"
#include "crn_resampler.h"
@@ -34,6 +33,7 @@
#include "crn_texture_file_types.h"
#include "crn_image_utils.h"
#include "crn_export.h"
+#include "dds_defs.h"

namespace crnlib {
extern const vec2I g_vertical_cross_image_offsets[6];
diff --git a/inc/crnlib.h b/crnlib/crnlib.h
similarity index 100%
rename from inc/crnlib.h
rename to crnlib/crnlib.h
diff --git a/inc/dds_defs.h b/crnlib/dds_defs.h
similarity index 100%
rename from inc/dds_defs.h
rename to crnlib/dds_defs.h
diff --git a/crunch/CMakeLists.txt b/crunch/CMakeLists.txt
index a7e9e32..be7bedd 100644
--- a/crunch/CMakeLists.txt
+++ b/crunch/CMakeLists.txt
@@ -8,6 +8,6 @@ set(CRUNCH_SRCS

add_executable(crunch ${CRUNCH_SRCS})
set_property(TARGET crunch PROPERTY CXX_STANDARD 11)
-target_link_libraries(crunch crn)
+target_link_libraries(crunch crunch2)

install(TARGETS crunch)
26 changes: 26 additions & 0 deletions cmake/ports/crunch2/fix_linux_compile.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
diff --git a/inc/crn_decomp.h b/inc/crn_decomp.h
index 6af702a..eb2030c 100644
--- a/inc/crn_decomp.h
+++ b/inc/crn_decomp.h
@@ -48,6 +48,7 @@
#else
#include <malloc.h>
#endif
+#include <cstdint>
#include <stdarg.h>
#include <new> // needed for placement new, _msize, _expand

@@ -2819,11 +2820,11 @@ namespace crnd
uint32 crnd_get_segmented_file_size(const void* pData, uint32 data_size)
{
if ((!pData) || (data_size < cCRNHeaderMinSize))
- return NULL;
+ return 0;

const crn_header* pHeader = crnd_get_header(pData, data_size);
if (!pHeader)
- return NULL;
+ return 0;

uint32 size = pHeader->m_header_size;

34 changes: 34 additions & 0 deletions cmake/ports/crunch2/portfile.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
vcpkg_check_linkage(ONLY_STATIC_LIBRARY)
vcpkg_from_github(
OUT_SOURCE_PATH
SOURCE_PATH
REPO
FrozenStormInteractive/Crunch2
REF
d5cd924754a1b39ea597335a979273ce71ef95a3
SHA512
56095fbbea8fe405258434a1b2d14cf29d634e4b5c22d4ae6002db4641d8ff8ee74ae7ce4e47781e5b4bc4bb248055fead8921389df3e82bb9fc4dd1a98a4a28
HEAD_REF
master
PATCHES
"fix_linux_compile.patch"
"fix_cmake.patch")

vcpkg_cmake_configure(SOURCE_PATH "${SOURCE_PATH}")

vcpkg_cmake_install()
vcpkg_copy_pdbs()
vcpkg_cmake_config_fixup(CONFIG_PATH "lib/cmake/crunch2")

file(REMOVE_RECURSE ${CURRENT_PACKAGES_DIR}/debug/include
${CURRENT_PACKAGES_DIR}/debug/share)

if(VCPKG_LIBRARY_LINKAGE STREQUAL "static")
file(REMOVE_RECURSE "${CURRENT_PACKAGES_DIR}/bin"
"${CURRENT_PACKAGES_DIR}/debug/bin")
endif()

file(
INSTALL "${SOURCE_PATH}/license.txt"
DESTINATION "${CURRENT_PACKAGES_DIR}/share/crunch2"
RENAME copyright)
14 changes: 14 additions & 0 deletions cmake/ports/crunch2/vcpkg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "crunch2",
"version-string": "1",
"dependencies": [
{
"name": "vcpkg-cmake",
"host": true
},
{
"name": "vcpkg-cmake-config",
"host": true
}
]
}
Binary file added data/crunch.7z
Binary file not shown.
Binary file added data/crunch_compress_dxt1.7z
Binary file not shown.
Binary file added data/crunch_compress_dxt5.7z
Binary file not shown.
Binary file added data/crunch_mipmaps.7z
Binary file not shown.
Binary file added data/crunch_resize.7z
Binary file not shown.
Binary file added data/modfolder_ignore_existing.7z
Binary file not shown.
3 changes: 2 additions & 1 deletion include/btu/modmanager/mod_folder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class ModFolder
using enum ModFolderIteratorBase::ArchiveTooLargeAction;
using enum ModFolderIteratorBase::ArchiveTooLargeState;

explicit ModFolder(Path directory, bsa::Settings bsa_settings);
explicit ModFolder(Path directory, bsa::Settings bsa_settings, bool ignore_existing_archives = false);

/// Get the size of the folder, including files in archives.
/// Utility function, equivalent to iterate() and counting the files.
Expand All @@ -102,6 +102,7 @@ class ModFolder
private:
Path dir_;
bsa::Settings bsa_settings_;
bool ignore_existing_archives_;
common::ThreadPool thread_pool_;
};
} // namespace btu::modmanager
17 changes: 17 additions & 0 deletions include/btu/tex/crunch_functions.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include "btu/tex/crunch_texture.hpp"
#include "btu/tex/detail/common.hpp"
#include "btu/tex/detail/formats_string.hpp"
#include "btu/tex/dimension.hpp"

#include <crunch/crn_dxt_image.h>
#include <crunch/crn_texture_conversion.h>
#include <crunch/dds_defs.h>

namespace btu::tex {
using crnlib::texture_conversion::convert_params;
[[nodiscard]] auto resize(CrunchTexture &&file, Dimension dim) -> ResultCrunch;
[[nodiscard]] auto generate_mipmaps(CrunchTexture &&file) -> ResultCrunch;
[[nodiscard]] auto convert(CrunchTexture &&file, DXGI_FORMAT format) -> ResultCrunch;
} // namespace btu::tex
58 changes: 58 additions & 0 deletions include/btu/tex/crunch_texture.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/* Copyright (C) 2021 Edgar B
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

#pragma once

#include <btu/common/path.hpp>
#include <btu/tex/detail/common.hpp>
#include <btu/tex/dxtex.hpp> // for DXGI_FORMAT
#include <crunch/crn_mipmapped_texture.h>

#include <filesystem>
#include <span>

namespace crnlib {
class mipmapped_texture;

auto operator==(const image_u8 &lhs, const image_u8 &rhs) noexcept -> bool;
auto operator==(const mipmapped_texture &lhs, const mipmapped_texture &rhs) noexcept -> bool;
} // namespace crnlib

namespace btu::tex {
using crnlib::mipmapped_texture;

struct Dimension;
enum class TextureType : std::uint8_t;

class CrunchTexture
{
public:
void set(const mipmapped_texture &tex) noexcept;

[[nodiscard]] auto get() noexcept -> mipmapped_texture &;
[[nodiscard]] auto get() const noexcept -> const mipmapped_texture &;

[[nodiscard]] auto get_dimension() const noexcept -> Dimension;

[[nodiscard]] auto get_load_path() const noexcept -> const Path &;
void set_load_path(Path path) noexcept;

[[nodiscard]] auto get_texture_type() const noexcept -> TextureType;
[[nodiscard]] auto get_format_as_dxgi() const noexcept -> DXGI_FORMAT;

[[nodiscard]] auto operator==(const CrunchTexture &) const noexcept -> bool = default;

private:
Path load_path_;
mipmapped_texture tex_;
};

[[nodiscard]] auto load_crunch(Path path) noexcept -> tl::expected<CrunchTexture, Error>;
[[nodiscard]] auto load_crunch(Path relative_path,
std::span<const std::byte> data) noexcept -> tl::expected<CrunchTexture, Error>;

[[nodiscard]] auto save(const CrunchTexture &tex, const Path &path) noexcept -> ResultError;
[[nodiscard]] auto save(const CrunchTexture &tex) noexcept -> tl::expected<std::vector<std::byte>, Error>;
} // namespace btu::tex
6 changes: 4 additions & 2 deletions include/btu/tex/detail/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@

namespace btu::tex {
class Texture;
class CrunchTexture;

using Result = tl::expected<Texture, Error>;
using ResultError = tl::expected<void, Error>;
using Result = tl::expected<Texture, Error>;
using ResultCrunch = tl::expected<CrunchTexture, Error>;
using ResultError = tl::expected<void, Error>;
} // namespace btu::tex
3 changes: 2 additions & 1 deletion include/btu/tex/error_code.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ enum class TextureErr : std::uint8_t
Unknown = 1,
BadInput,
MemoryAllocation,

WriteFailure,
ReadFailure,
};
} // namespace btu::tex

Expand Down
Loading
Loading