Add core functionality for loading and processing game assets
- Implemented palette loading from binary files in `palette.cpp`. - Added runtime utilities for reading text and JSON files in `runtime.cpp`. - Created shape archive handling in `shape_archive.cpp`, including frame parsing and pixel decoding. - Introduced type flag loading functionality in `type_flags.cpp`. - Developed tests for combat tactics, entity dispatch entries, file system operations, fixed maps, flex archives, glob archives, map rendering, palette loading, shape archives, and type flags. - Added a Python script for importing Ghidra-exported function slices with reporting capabilities.
This commit is contained in:
parent
6ca3b18c3e
commit
8907604b84
41 changed files with 2889 additions and 0 deletions
144
rebuild/remorse/CMakeLists.txt
Normal file
144
rebuild/remorse/CMakeLists.txt
Normal file
|
|
@ -0,0 +1,144 @@
|
|||
cmake_minimum_required(VERSION 3.20)
|
||||
|
||||
project(remorse_rebuild LANGUAGES CXX)
|
||||
|
||||
add_library(remorse_core
|
||||
src/combat_tactics.cpp
|
||||
src/entity_dispatch_entry.cpp
|
||||
src/file_system.cpp
|
||||
src/flex_archive.cpp
|
||||
src/fixed_map.cpp
|
||||
src/glob_archive.cpp
|
||||
src/map_renderer.cpp
|
||||
src/palette.cpp
|
||||
src/runtime.cpp
|
||||
src/shape_archive.cpp
|
||||
src/type_flags.cpp
|
||||
)
|
||||
|
||||
target_compile_features(remorse_core PUBLIC cxx_std_17)
|
||||
target_include_directories(remorse_core PUBLIC include)
|
||||
|
||||
add_executable(remorse_rebuild
|
||||
src/main.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(remorse_rebuild PRIVATE remorse_core)
|
||||
|
||||
add_executable(remorse_map_preview
|
||||
src/map_preview_main.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(remorse_map_preview PRIVATE remorse_core)
|
||||
|
||||
enable_testing()
|
||||
|
||||
add_executable(entity_dispatch_entry_tests
|
||||
tests/entity_dispatch_entry_tests.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(entity_dispatch_entry_tests PRIVATE remorse_core)
|
||||
|
||||
add_test(NAME entity_dispatch_entry_tests COMMAND entity_dispatch_entry_tests)
|
||||
|
||||
add_executable(file_system_tests
|
||||
tests/file_system_tests.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(file_system_tests PRIVATE remorse_core)
|
||||
|
||||
add_test(NAME file_system_tests COMMAND file_system_tests)
|
||||
|
||||
add_executable(palette_tests
|
||||
tests/palette_tests.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(palette_tests PRIVATE remorse_core)
|
||||
|
||||
add_test(NAME palette_tests COMMAND palette_tests)
|
||||
|
||||
add_executable(flex_archive_tests
|
||||
tests/flex_archive_tests.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(flex_archive_tests PRIVATE remorse_core)
|
||||
|
||||
add_test(NAME flex_archive_tests COMMAND flex_archive_tests)
|
||||
|
||||
add_executable(fixed_map_tests
|
||||
tests/fixed_map_tests.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(fixed_map_tests PRIVATE remorse_core)
|
||||
|
||||
add_test(NAME fixed_map_tests COMMAND fixed_map_tests)
|
||||
|
||||
add_executable(glob_archive_tests
|
||||
tests/glob_archive_tests.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(glob_archive_tests PRIVATE remorse_core)
|
||||
|
||||
add_test(NAME glob_archive_tests COMMAND glob_archive_tests)
|
||||
|
||||
add_executable(type_flags_tests
|
||||
tests/type_flags_tests.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(type_flags_tests PRIVATE remorse_core)
|
||||
|
||||
add_test(NAME type_flags_tests COMMAND type_flags_tests)
|
||||
|
||||
add_executable(shape_archive_tests
|
||||
tests/shape_archive_tests.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(shape_archive_tests PRIVATE remorse_core)
|
||||
|
||||
add_test(NAME shape_archive_tests COMMAND shape_archive_tests)
|
||||
|
||||
add_executable(map_renderer_tests
|
||||
tests/map_renderer_tests.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(map_renderer_tests PRIVATE remorse_core)
|
||||
|
||||
add_test(NAME map_renderer_tests COMMAND map_renderer_tests)
|
||||
|
||||
add_executable(combat_tactics_tests
|
||||
tests/combat_tactics_tests.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(combat_tactics_tests PRIVATE remorse_core)
|
||||
|
||||
add_test(NAME combat_tactics_tests COMMAND combat_tactics_tests)
|
||||
|
||||
if(MSVC)
|
||||
target_compile_options(remorse_core PRIVATE /W4 /permissive-)
|
||||
target_compile_options(combat_tactics_tests PRIVATE /W4 /permissive-)
|
||||
target_compile_options(map_renderer_tests PRIVATE /W4 /permissive-)
|
||||
target_compile_options(remorse_map_preview PRIVATE /W4 /permissive-)
|
||||
target_compile_options(remorse_rebuild PRIVATE /W4 /permissive-)
|
||||
target_compile_options(entity_dispatch_entry_tests PRIVATE /W4 /permissive-)
|
||||
target_compile_options(file_system_tests PRIVATE /W4 /permissive-)
|
||||
target_compile_options(flex_archive_tests PRIVATE /W4 /permissive-)
|
||||
target_compile_options(fixed_map_tests PRIVATE /W4 /permissive-)
|
||||
target_compile_options(glob_archive_tests PRIVATE /W4 /permissive-)
|
||||
target_compile_options(palette_tests PRIVATE /W4 /permissive-)
|
||||
target_compile_options(shape_archive_tests PRIVATE /W4 /permissive-)
|
||||
target_compile_options(type_flags_tests PRIVATE /W4 /permissive-)
|
||||
else()
|
||||
target_compile_options(remorse_core PRIVATE -Wall -Wextra -Wpedantic)
|
||||
target_compile_options(combat_tactics_tests PRIVATE -Wall -Wextra -Wpedantic)
|
||||
target_compile_options(map_renderer_tests PRIVATE -Wall -Wextra -Wpedantic)
|
||||
target_compile_options(remorse_map_preview PRIVATE -Wall -Wextra -Wpedantic)
|
||||
target_compile_options(remorse_rebuild PRIVATE -Wall -Wextra -Wpedantic)
|
||||
target_compile_options(entity_dispatch_entry_tests PRIVATE -Wall -Wextra -Wpedantic)
|
||||
target_compile_options(file_system_tests PRIVATE -Wall -Wextra -Wpedantic)
|
||||
target_compile_options(flex_archive_tests PRIVATE -Wall -Wextra -Wpedantic)
|
||||
target_compile_options(fixed_map_tests PRIVATE -Wall -Wextra -Wpedantic)
|
||||
target_compile_options(glob_archive_tests PRIVATE -Wall -Wextra -Wpedantic)
|
||||
target_compile_options(palette_tests PRIVATE -Wall -Wextra -Wpedantic)
|
||||
target_compile_options(shape_archive_tests PRIVATE -Wall -Wextra -Wpedantic)
|
||||
target_compile_options(type_flags_tests PRIVATE -Wall -Wextra -Wpedantic)
|
||||
endif()
|
||||
43
rebuild/remorse/build_msvc.cmd
Normal file
43
rebuild/remorse/build_msvc.cmd
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
@echo off
|
||||
setlocal
|
||||
|
||||
set "CONFIG=%~1"
|
||||
if "%CONFIG%"=="" set "CONFIG=Debug"
|
||||
|
||||
set "REPO_ROOT=%~dp0..\.."
|
||||
for %%I in ("%REPO_ROOT%") do set "REPO_ROOT=%%~fI"
|
||||
|
||||
set "VSWHERE=%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe"
|
||||
if not exist "%VSWHERE%" (
|
||||
echo vswhere.exe was not found at "%VSWHERE%".
|
||||
exit /b 2
|
||||
)
|
||||
|
||||
for /f "usebackq delims=" %%I in (`"%VSWHERE%" -latest -products * -requires Microsoft.VisualStudio.Component.VC.Tools.x86.x64 -property installationPath`) do set "VSINSTALL=%%I"
|
||||
|
||||
if "%VSINSTALL%"=="" (
|
||||
echo Visual Studio with MSVC x64 tools was not found.
|
||||
exit /b 2
|
||||
)
|
||||
|
||||
set "VCVARS=%VSINSTALL%\VC\Auxiliary\Build\vcvars64.bat"
|
||||
if not exist "%VCVARS%" (
|
||||
echo vcvars64.bat was not found at "%VCVARS%".
|
||||
exit /b 2
|
||||
)
|
||||
|
||||
call "%VCVARS%" || exit /b 2
|
||||
|
||||
cd /d "%REPO_ROOT%" || exit /b 2
|
||||
|
||||
cmake -S rebuild/remorse -B rebuild/remorse/out/build || exit /b 1
|
||||
cmake --build rebuild/remorse/out/build --config "%CONFIG%" || exit /b 1
|
||||
ctest --test-dir rebuild/remorse/out/build -C "%CONFIG%" --output-on-failure || exit /b 1
|
||||
|
||||
set "EXE=rebuild\remorse\out\build\%CONFIG%\remorse_rebuild.exe"
|
||||
if not exist "%EXE%" (
|
||||
echo Built executable was not found at "%EXE%".
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
"%EXE%" exports\src_remorse\export_manifest.json || exit /b 1
|
||||
22
rebuild/remorse/include/remorse/combat_tactics.h
Normal file
22
rebuild/remorse/include/remorse/combat_tactics.h
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
#pragma once
|
||||
|
||||
#include <array>
|
||||
#include <filesystem>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "remorse/compat.h"
|
||||
|
||||
namespace remorse {
|
||||
|
||||
struct CombatTactic {
|
||||
u32 index = 0;
|
||||
std::string name;
|
||||
std::array<u16, 4> blockOffsets{};
|
||||
std::vector<u8> bytecode;
|
||||
};
|
||||
|
||||
std::optional<std::vector<CombatTactic>> loadCombatTactics(const std::filesystem::path& path);
|
||||
|
||||
} // namespace remorse
|
||||
48
rebuild/remorse/include/remorse/compat.h
Normal file
48
rebuild/remorse/include/remorse/compat.h
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
#pragma once
|
||||
|
||||
namespace remorse {
|
||||
|
||||
using u8 = unsigned char;
|
||||
using s8 = signed char;
|
||||
using u16 = unsigned short;
|
||||
using s16 = signed short;
|
||||
using u32 = unsigned int;
|
||||
using s32 = signed int;
|
||||
|
||||
static_assert(sizeof(u8) == 1, "u8 must be 8 bits");
|
||||
static_assert(sizeof(u16) == 2, "u16 must be 16 bits");
|
||||
static_assert(sizeof(u32) == 4, "u32 must be 32 bits");
|
||||
|
||||
struct FarPtr16 {
|
||||
u16 offset;
|
||||
u16 segment;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct FarPtr {
|
||||
u16 offset;
|
||||
u16 segment;
|
||||
|
||||
constexpr FarPtr16 raw() const noexcept
|
||||
{
|
||||
return FarPtr16{offset, segment};
|
||||
}
|
||||
};
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
#define CR_PACK_PUSH_1 __pragma(pack(push, 1))
|
||||
#define CR_PACK_POP __pragma(pack(pop))
|
||||
#define CR_PACKED
|
||||
#else
|
||||
#define CR_PACK_PUSH_1 _Pragma("pack(push, 1)")
|
||||
#define CR_PACK_POP _Pragma("pack(pop)")
|
||||
#define CR_PACKED __attribute__((packed))
|
||||
#endif
|
||||
|
||||
#define CR_CDECL
|
||||
#define CR_STDCALL
|
||||
#define CR_THISCALL
|
||||
#define CR_FARCALL
|
||||
#define CR_VSLOT(index)
|
||||
|
||||
} // namespace remorse
|
||||
31
rebuild/remorse/include/remorse/entity_dispatch_entry.h
Normal file
31
rebuild/remorse/include/remorse/entity_dispatch_entry.h
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
#pragma once
|
||||
|
||||
#include "remorse/compat.h"
|
||||
|
||||
namespace remorse {
|
||||
|
||||
CR_PACK_PUSH_1
|
||||
struct EntityDispatchEntryBase {
|
||||
u8 bytes[0x32];
|
||||
} CR_PACKED;
|
||||
|
||||
struct EntityDispatchEntryRuntimeState {
|
||||
u8 bytes[0x18];
|
||||
} CR_PACKED;
|
||||
CR_PACK_POP
|
||||
|
||||
static_assert(sizeof(EntityDispatchEntryBase) == 0x32, "EntityDispatchEntryBase size must match allocation evidence");
|
||||
|
||||
class EntityDispatchEntry {
|
||||
public:
|
||||
static constexpr u16 initBaseSegment = 0x11e0;
|
||||
static constexpr u16 initBaseOffset = 0x0000;
|
||||
|
||||
static EntityDispatchEntryBase* initBase(EntityDispatchEntryBase* entry);
|
||||
static void setGroupId(EntityDispatchEntryBase& entry, u8 groupId);
|
||||
static void incrementGroupId(EntityDispatchEntryBase& entry);
|
||||
static void setSourceType(EntityDispatchEntryBase& entry, u16 sourceType);
|
||||
static void setEventTypeChecked(EntityDispatchEntryBase& entry, u16 eventType);
|
||||
};
|
||||
|
||||
} // namespace remorse
|
||||
20
rebuild/remorse/include/remorse/export_prelude.h
Normal file
20
rebuild/remorse/include/remorse/export_prelude.h
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
#pragma once
|
||||
|
||||
#include "remorse/compat.h"
|
||||
|
||||
using remorse::s16;
|
||||
using remorse::s32;
|
||||
using remorse::s8;
|
||||
using remorse::u16;
|
||||
using remorse::u32;
|
||||
using remorse::u8;
|
||||
|
||||
using undefined = u8;
|
||||
using undefined1 = u8;
|
||||
using undefined2 = u16;
|
||||
using undefined4 = u32;
|
||||
using byte = u8;
|
||||
using word = u16;
|
||||
using uint = unsigned int;
|
||||
using ulong = unsigned long;
|
||||
using dword = u32;
|
||||
24
rebuild/remorse/include/remorse/file_system.h
Normal file
24
rebuild/remorse/include/remorse/file_system.h
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
#pragma once
|
||||
|
||||
#include <filesystem>
|
||||
#include <optional>
|
||||
#include <vector>
|
||||
|
||||
#include "remorse/compat.h"
|
||||
|
||||
namespace remorse {
|
||||
|
||||
struct GameDataRoot {
|
||||
std::filesystem::path path;
|
||||
bool hasExecutable = false;
|
||||
bool hasConfig = false;
|
||||
bool hasStaticData = false;
|
||||
bool hasUsecode = false;
|
||||
};
|
||||
|
||||
GameDataRoot inspectGameDataRoot(const std::filesystem::path& root);
|
||||
std::optional<GameDataRoot> findGameDataRoot(const std::filesystem::path& start);
|
||||
std::optional<std::filesystem::path> resolveGameDataPath(const GameDataRoot& root, const std::filesystem::path& relativePath);
|
||||
std::optional<std::vector<u8>> readBinaryFile(const std::filesystem::path& path);
|
||||
|
||||
} // namespace remorse
|
||||
37
rebuild/remorse/include/remorse/fixed_map.h
Normal file
37
rebuild/remorse/include/remorse/fixed_map.h
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
#pragma once
|
||||
|
||||
#include <filesystem>
|
||||
#include <optional>
|
||||
#include <vector>
|
||||
|
||||
#include "remorse/compat.h"
|
||||
|
||||
namespace remorse {
|
||||
|
||||
struct FixedMapEntry {
|
||||
u16 x = 0;
|
||||
u16 y = 0;
|
||||
u8 z = 0;
|
||||
u16 shape = 0;
|
||||
u8 frame = 0;
|
||||
u16 flags = 0;
|
||||
u16 quality = 0;
|
||||
u8 npcNumber = 0;
|
||||
u8 mapNumber = 0;
|
||||
u16 next = 0;
|
||||
};
|
||||
|
||||
struct FixedMap {
|
||||
u32 index = 0;
|
||||
std::vector<FixedMapEntry> entries;
|
||||
};
|
||||
|
||||
struct FixedMapArchive {
|
||||
u32 mapCount = 0;
|
||||
std::vector<u8> bytes;
|
||||
};
|
||||
|
||||
std::optional<FixedMapArchive> loadFixedMapArchive(const std::filesystem::path& path);
|
||||
std::optional<FixedMap> loadFixedMap(const FixedMapArchive& archive, u32 mapIndex);
|
||||
|
||||
} // namespace remorse
|
||||
26
rebuild/remorse/include/remorse/flex_archive.h
Normal file
26
rebuild/remorse/include/remorse/flex_archive.h
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
#pragma once
|
||||
|
||||
#include <filesystem>
|
||||
#include <optional>
|
||||
#include <vector>
|
||||
|
||||
#include "remorse/compat.h"
|
||||
|
||||
namespace remorse {
|
||||
|
||||
struct FlexEntry {
|
||||
u32 index = 0;
|
||||
u32 offset = 0;
|
||||
u32 size = 0;
|
||||
};
|
||||
|
||||
struct FlexArchive {
|
||||
u32 entryCount = 0;
|
||||
std::vector<FlexEntry> populatedEntries;
|
||||
std::vector<u8> bytes;
|
||||
};
|
||||
|
||||
std::optional<FlexArchive> loadFlexArchive(const std::filesystem::path& path);
|
||||
std::optional<std::vector<u8>> readFlexEntry(const FlexArchive& archive, u32 index);
|
||||
|
||||
} // namespace remorse
|
||||
26
rebuild/remorse/include/remorse/glob_archive.h
Normal file
26
rebuild/remorse/include/remorse/glob_archive.h
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
#pragma once
|
||||
|
||||
#include <filesystem>
|
||||
#include <optional>
|
||||
#include <vector>
|
||||
|
||||
#include "remorse/compat.h"
|
||||
|
||||
namespace remorse {
|
||||
|
||||
struct GlobObjectEntry {
|
||||
u8 x = 0;
|
||||
u8 y = 0;
|
||||
u8 z = 0;
|
||||
u16 shape = 0;
|
||||
u8 frame = 0;
|
||||
};
|
||||
|
||||
struct GlobObject {
|
||||
u32 index = 0;
|
||||
std::vector<GlobObjectEntry> entries;
|
||||
};
|
||||
|
||||
std::optional<std::vector<GlobObject>> loadGlobObjects(const std::filesystem::path& path);
|
||||
|
||||
} // namespace remorse
|
||||
47
rebuild/remorse/include/remorse/map_renderer.h
Normal file
47
rebuild/remorse/include/remorse/map_renderer.h
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
#pragma once
|
||||
|
||||
#include <filesystem>
|
||||
#include <optional>
|
||||
#include <vector>
|
||||
|
||||
#include "remorse/compat.h"
|
||||
#include "remorse/file_system.h"
|
||||
#include "remorse/palette.h"
|
||||
|
||||
namespace remorse {
|
||||
|
||||
struct WorldRect {
|
||||
s32 minX = 0;
|
||||
s32 minY = 0;
|
||||
s32 maxX = 0;
|
||||
s32 maxY = 0;
|
||||
};
|
||||
|
||||
struct MapRenderOptions {
|
||||
u32 mapIndex = 1;
|
||||
u32 maxWidth = 1600;
|
||||
u32 maxHeight = 900;
|
||||
bool expandGlobs = true;
|
||||
bool includeRoofs = false;
|
||||
bool includeHiddenMarkers = false;
|
||||
std::optional<WorldRect> worldRect;
|
||||
};
|
||||
|
||||
struct RenderedMapImage {
|
||||
u32 width = 0;
|
||||
u32 height = 0;
|
||||
u32 rawItemCount = 0;
|
||||
u32 renderItemCount = 0;
|
||||
u32 decodedItemCount = 0;
|
||||
u32 paintedItemCount = 0;
|
||||
s32 sourceLeft = 0;
|
||||
s32 sourceTop = 0;
|
||||
s32 fullWidth = 0;
|
||||
s32 fullHeight = 0;
|
||||
std::vector<RgbColor> pixels;
|
||||
};
|
||||
|
||||
std::optional<RenderedMapImage> renderMapPreview(const GameDataRoot& root, const MapRenderOptions& options);
|
||||
bool writeBmp24(const std::filesystem::path& path, const RenderedMapImage& image);
|
||||
|
||||
} // namespace remorse
|
||||
23
rebuild/remorse/include/remorse/palette.h
Normal file
23
rebuild/remorse/include/remorse/palette.h
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
#pragma once
|
||||
|
||||
#include <array>
|
||||
#include <filesystem>
|
||||
#include <optional>
|
||||
|
||||
#include "remorse/compat.h"
|
||||
|
||||
namespace remorse {
|
||||
|
||||
struct RgbColor {
|
||||
u8 red = 0;
|
||||
u8 green = 0;
|
||||
u8 blue = 0;
|
||||
};
|
||||
|
||||
struct Palette256 {
|
||||
std::array<RgbColor, 256> colors{};
|
||||
};
|
||||
|
||||
std::optional<Palette256> loadPalette256(const std::filesystem::path& path);
|
||||
|
||||
} // namespace remorse
|
||||
24
rebuild/remorse/include/remorse/runtime.h
Normal file
24
rebuild/remorse/include/remorse/runtime.h
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
#pragma once
|
||||
|
||||
#include <filesystem>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
|
||||
#include "remorse/compat.h"
|
||||
|
||||
namespace remorse {
|
||||
|
||||
struct ExportManifestSummary {
|
||||
std::string program;
|
||||
std::filesystem::path outputRoot;
|
||||
u32 functionFiles = 0;
|
||||
u32 fallbackFunctionFiles = 0;
|
||||
u32 globalBuckets = 0;
|
||||
u32 globalDefinitions = 0;
|
||||
};
|
||||
|
||||
std::optional<ExportManifestSummary> loadExportManifestSummary(const std::filesystem::path& manifestPath);
|
||||
std::filesystem::path defaultManifestPath(const std::filesystem::path& currentPath);
|
||||
void printStartupReport(const ExportManifestSummary& manifest);
|
||||
|
||||
} // namespace remorse
|
||||
35
rebuild/remorse/include/remorse/shape_archive.h
Normal file
35
rebuild/remorse/include/remorse/shape_archive.h
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
#pragma once
|
||||
|
||||
#include <filesystem>
|
||||
#include <optional>
|
||||
#include <vector>
|
||||
|
||||
#include "remorse/compat.h"
|
||||
#include "remorse/flex_archive.h"
|
||||
|
||||
namespace remorse {
|
||||
|
||||
struct ShapeFrame {
|
||||
bool compressed = false;
|
||||
u32 width = 0;
|
||||
u32 height = 0;
|
||||
s32 xOffset = 0;
|
||||
s32 yOffset = 0;
|
||||
std::vector<u32> lineOffsets;
|
||||
std::vector<u8> rleData;
|
||||
};
|
||||
|
||||
struct DecodedShapeFrame {
|
||||
ShapeFrame frame;
|
||||
std::vector<s16> pixels;
|
||||
};
|
||||
|
||||
struct ShapeArchive {
|
||||
FlexArchive archive;
|
||||
};
|
||||
|
||||
std::optional<ShapeArchive> loadShapeArchive(const std::filesystem::path& path);
|
||||
std::optional<std::vector<ShapeFrame>> loadShapeFrames(const ShapeArchive& archive, u32 shapeIndex);
|
||||
std::optional<DecodedShapeFrame> decodeShapeFrame(const ShapeArchive& archive, u32 shapeIndex, u32 frameIndex);
|
||||
|
||||
} // namespace remorse
|
||||
22
rebuild/remorse/include/remorse/type_flags.h
Normal file
22
rebuild/remorse/include/remorse/type_flags.h
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
#pragma once
|
||||
|
||||
#include <filesystem>
|
||||
#include <optional>
|
||||
#include <vector>
|
||||
|
||||
#include "remorse/compat.h"
|
||||
|
||||
namespace remorse {
|
||||
|
||||
struct ShapeTypeInfo {
|
||||
u8 family = 0;
|
||||
u32 flags = 0;
|
||||
u8 x = 0;
|
||||
u8 y = 0;
|
||||
u8 z = 0;
|
||||
u8 animationType = 0;
|
||||
};
|
||||
|
||||
std::optional<std::vector<ShapeTypeInfo>> loadTypeFlags(const std::filesystem::path& path);
|
||||
|
||||
} // namespace remorse
|
||||
58
rebuild/remorse/src/combat_tactics.cpp
Normal file
58
rebuild/remorse/src/combat_tactics.cpp
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
#include "remorse/combat_tactics.h"
|
||||
|
||||
#include "remorse/flex_archive.h"
|
||||
|
||||
namespace remorse {
|
||||
namespace {
|
||||
|
||||
constexpr u32 tacticNameSize = 16;
|
||||
constexpr u32 tacticBlockOffsetCount = 4;
|
||||
constexpr u32 tacticHeaderSize = tacticNameSize + tacticBlockOffsetCount * 2;
|
||||
|
||||
u16 readU16Le(const std::vector<u8>& bytes, u32 offset)
|
||||
{
|
||||
return static_cast<u16>(bytes[offset] | (static_cast<u16>(bytes[offset + 1]) << 8));
|
||||
}
|
||||
|
||||
std::string readNullPaddedName(const std::vector<u8>& bytes)
|
||||
{
|
||||
std::string name;
|
||||
for (u32 index = 0; index < tacticNameSize; ++index) {
|
||||
if (bytes[index] == 0) {
|
||||
break;
|
||||
}
|
||||
name.push_back(static_cast<char>(bytes[index]));
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
std::optional<std::vector<CombatTactic>> loadCombatTactics(const std::filesystem::path& path)
|
||||
{
|
||||
const auto archive = loadFlexArchive(path);
|
||||
if (!archive) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::vector<CombatTactic> tactics;
|
||||
for (const FlexEntry& entry : archive->populatedEntries) {
|
||||
const auto entryBytes = readFlexEntry(*archive, entry.index);
|
||||
if (!entryBytes || entryBytes->size() < tacticHeaderSize) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
CombatTactic tactic;
|
||||
tactic.index = entry.index;
|
||||
tactic.name = readNullPaddedName(*entryBytes);
|
||||
for (u32 offsetIndex = 0; offsetIndex < tacticBlockOffsetCount; ++offsetIndex) {
|
||||
tactic.blockOffsets[offsetIndex] = readU16Le(*entryBytes, tacticNameSize + offsetIndex * 2);
|
||||
}
|
||||
tactic.bytecode.assign(entryBytes->begin() + tacticHeaderSize, entryBytes->end());
|
||||
tactics.push_back(tactic);
|
||||
}
|
||||
|
||||
return tactics;
|
||||
}
|
||||
|
||||
} // namespace remorse
|
||||
49
rebuild/remorse/src/entity_dispatch_entry.cpp
Normal file
49
rebuild/remorse/src/entity_dispatch_entry.cpp
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
#include "remorse/entity_dispatch_entry.h"
|
||||
|
||||
namespace remorse {
|
||||
|
||||
namespace {
|
||||
constexpr u16 sourceItemOffset = 0x04;
|
||||
constexpr u16 eventTypeOffset = 0x06;
|
||||
constexpr u16 groupIdOffset = 0x08;
|
||||
|
||||
void writeLe16(EntityDispatchEntryBase& entry, u16 offset, u16 value)
|
||||
{
|
||||
entry.bytes[offset] = static_cast<u8>(value & 0xff);
|
||||
entry.bytes[offset + 1] = static_cast<u8>(value >> 8);
|
||||
}
|
||||
}
|
||||
|
||||
EntityDispatchEntryBase* EntityDispatchEntry::initBase(EntityDispatchEntryBase* entry)
|
||||
{
|
||||
if (entry == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
for (u8& byte : entry->bytes) {
|
||||
byte = 0;
|
||||
}
|
||||
return entry;
|
||||
}
|
||||
|
||||
void EntityDispatchEntry::setGroupId(EntityDispatchEntryBase& entry, u8 groupId)
|
||||
{
|
||||
entry.bytes[groupIdOffset] = static_cast<u8>((entry.bytes[groupIdOffset] & 0xe0) | (groupId & 0x1f));
|
||||
}
|
||||
|
||||
void EntityDispatchEntry::incrementGroupId(EntityDispatchEntryBase& entry)
|
||||
{
|
||||
++entry.bytes[groupIdOffset];
|
||||
}
|
||||
|
||||
void EntityDispatchEntry::setSourceType(EntityDispatchEntryBase& entry, u16 sourceType)
|
||||
{
|
||||
writeLe16(entry, sourceItemOffset, sourceType);
|
||||
}
|
||||
|
||||
void EntityDispatchEntry::setEventTypeChecked(EntityDispatchEntryBase& entry, u16 eventType)
|
||||
{
|
||||
writeLe16(entry, eventTypeOffset, eventType);
|
||||
}
|
||||
|
||||
} // namespace remorse
|
||||
145
rebuild/remorse/src/file_system.cpp
Normal file
145
rebuild/remorse/src/file_system.cpp
Normal file
|
|
@ -0,0 +1,145 @@
|
|||
#include "remorse/file_system.h"
|
||||
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
|
||||
namespace remorse {
|
||||
namespace {
|
||||
|
||||
bool pathExists(const std::filesystem::path& path)
|
||||
{
|
||||
return std::filesystem::exists(path);
|
||||
}
|
||||
|
||||
std::optional<std::filesystem::path> parentPath(const std::filesystem::path& path)
|
||||
{
|
||||
const std::filesystem::path parent = path.parent_path();
|
||||
if (parent.empty() || parent == path) {
|
||||
return std::nullopt;
|
||||
}
|
||||
return parent;
|
||||
}
|
||||
|
||||
char toLowerAscii(char value)
|
||||
{
|
||||
if (value < 'A' || value > 'Z') {
|
||||
return value;
|
||||
}
|
||||
return static_cast<char>(value - 'A' + 'a');
|
||||
}
|
||||
|
||||
std::string lowerAscii(std::string value)
|
||||
{
|
||||
for (char& character : value) {
|
||||
character = toLowerAscii(character);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
bool equalAsciiCaseInsensitive(const std::filesystem::path& left, const std::filesystem::path& right)
|
||||
{
|
||||
return lowerAscii(left.filename().string()) == lowerAscii(right.filename().string());
|
||||
}
|
||||
|
||||
std::optional<std::filesystem::path> resolveChildCaseInsensitive(const std::filesystem::path& parent, const std::filesystem::path& child)
|
||||
{
|
||||
const std::filesystem::path exact = parent / child;
|
||||
if (pathExists(exact)) {
|
||||
return exact;
|
||||
}
|
||||
|
||||
if (!std::filesystem::is_directory(parent)) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
for (const std::filesystem::directory_entry& entry : std::filesystem::directory_iterator(parent)) {
|
||||
if (equalAsciiCaseInsensitive(entry.path(), child)) {
|
||||
return entry.path();
|
||||
}
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
GameDataRoot inspectGameDataRoot(const std::filesystem::path& root)
|
||||
{
|
||||
GameDataRoot result;
|
||||
result.path = root;
|
||||
result.hasExecutable = pathExists(root / "CRUSADER.EXE");
|
||||
result.hasConfig = pathExists(root / "CRUSADER.CFG");
|
||||
result.hasStaticData = pathExists(root / "STATIC");
|
||||
result.hasUsecode = pathExists(root / "USECODE");
|
||||
return result;
|
||||
}
|
||||
|
||||
std::optional<GameDataRoot> findGameDataRoot(const std::filesystem::path& start)
|
||||
{
|
||||
std::filesystem::path cursor = std::filesystem::absolute(start);
|
||||
if (std::filesystem::is_regular_file(cursor)) {
|
||||
cursor = cursor.parent_path();
|
||||
}
|
||||
|
||||
while (true) {
|
||||
GameDataRoot candidate = inspectGameDataRoot(cursor);
|
||||
if (candidate.hasExecutable && candidate.hasStaticData && candidate.hasUsecode) {
|
||||
return candidate;
|
||||
}
|
||||
|
||||
const auto parent = parentPath(cursor);
|
||||
if (!parent) {
|
||||
return std::nullopt;
|
||||
}
|
||||
cursor = *parent;
|
||||
}
|
||||
}
|
||||
|
||||
std::optional<std::filesystem::path> resolveGameDataPath(const GameDataRoot& root, const std::filesystem::path& relativePath)
|
||||
{
|
||||
if (relativePath.empty() || relativePath.is_absolute()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::filesystem::path cursor = root.path;
|
||||
for (const std::filesystem::path& component : relativePath) {
|
||||
if (component == ".") {
|
||||
continue;
|
||||
}
|
||||
if (component == "..") {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
const auto next = resolveChildCaseInsensitive(cursor, component);
|
||||
if (!next) {
|
||||
return std::nullopt;
|
||||
}
|
||||
cursor = *next;
|
||||
}
|
||||
return cursor;
|
||||
}
|
||||
|
||||
std::optional<std::vector<u8>> readBinaryFile(const std::filesystem::path& path)
|
||||
{
|
||||
std::ifstream input(path, std::ios::binary);
|
||||
if (!input) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
input.seekg(0, std::ios::end);
|
||||
const std::streamoff size = input.tellg();
|
||||
if (size < 0) {
|
||||
return std::nullopt;
|
||||
}
|
||||
input.seekg(0, std::ios::beg);
|
||||
|
||||
std::vector<u8> bytes(static_cast<std::size_t>(size));
|
||||
if (!bytes.empty()) {
|
||||
input.read(reinterpret_cast<char*>(bytes.data()), size);
|
||||
if (!input) {
|
||||
return std::nullopt;
|
||||
}
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
|
||||
} // namespace remorse
|
||||
119
rebuild/remorse/src/fixed_map.cpp
Normal file
119
rebuild/remorse/src/fixed_map.cpp
Normal file
|
|
@ -0,0 +1,119 @@
|
|||
#include "remorse/fixed_map.h"
|
||||
|
||||
#include "remorse/file_system.h"
|
||||
|
||||
namespace remorse {
|
||||
namespace {
|
||||
|
||||
constexpr u32 fixedMapCountOffset = 0x54;
|
||||
constexpr u32 fixedMapTableOffset = 0x80;
|
||||
constexpr u32 fixedMapTableRowSize = 8;
|
||||
constexpr u32 fixedMapRecordSize = 16;
|
||||
constexpr u16 crusaderCoordinateScale = 2;
|
||||
|
||||
std::optional<u16> readU16Le(const std::vector<u8>& bytes, u32 offset)
|
||||
{
|
||||
if (offset + 2 > bytes.size()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
return static_cast<u16>(bytes[offset] | (static_cast<u16>(bytes[offset + 1]) << 8));
|
||||
}
|
||||
|
||||
std::optional<u32> readU32Le(const std::vector<u8>& bytes, u32 offset)
|
||||
{
|
||||
if (offset + 4 > bytes.size()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
return static_cast<u32>(bytes[offset]) |
|
||||
(static_cast<u32>(bytes[offset + 1]) << 8) |
|
||||
(static_cast<u32>(bytes[offset + 2]) << 16) |
|
||||
(static_cast<u32>(bytes[offset + 3]) << 24);
|
||||
}
|
||||
|
||||
std::optional<FixedMapEntry> readFixedMapEntry(const std::vector<u8>& bytes, u32 offset)
|
||||
{
|
||||
if (offset + fixedMapRecordSize > bytes.size()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
const auto diskX = readU16Le(bytes, offset);
|
||||
const auto diskY = readU16Le(bytes, offset + 2);
|
||||
const auto shape = readU16Le(bytes, offset + 5);
|
||||
const auto flags = readU16Le(bytes, offset + 8);
|
||||
const auto quality = readU16Le(bytes, offset + 10);
|
||||
const auto next = readU16Le(bytes, offset + 14);
|
||||
if (!diskX || !diskY || !shape || !flags || !quality || !next) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
FixedMapEntry entry;
|
||||
entry.x = static_cast<u16>(*diskX * crusaderCoordinateScale);
|
||||
entry.y = static_cast<u16>(*diskY * crusaderCoordinateScale);
|
||||
entry.z = bytes[offset + 4];
|
||||
entry.shape = *shape;
|
||||
entry.frame = bytes[offset + 7];
|
||||
entry.flags = *flags;
|
||||
entry.quality = *quality;
|
||||
entry.npcNumber = bytes[offset + 12];
|
||||
entry.mapNumber = bytes[offset + 13];
|
||||
entry.next = *next;
|
||||
return entry;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
std::optional<FixedMapArchive> loadFixedMapArchive(const std::filesystem::path& path)
|
||||
{
|
||||
auto bytes = readBinaryFile(path);
|
||||
if (!bytes) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
const auto mapCount = readU16Le(*bytes, fixedMapCountOffset);
|
||||
if (!mapCount) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
const u32 tableEnd = fixedMapTableOffset + (static_cast<u32>(*mapCount) * fixedMapTableRowSize);
|
||||
if (tableEnd > bytes->size()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
FixedMapArchive archive;
|
||||
archive.mapCount = *mapCount;
|
||||
archive.bytes = *bytes;
|
||||
return archive;
|
||||
}
|
||||
|
||||
std::optional<FixedMap> loadFixedMap(const FixedMapArchive& archive, u32 mapIndex)
|
||||
{
|
||||
if (mapIndex >= archive.mapCount) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
const u32 tableOffset = fixedMapTableOffset + (mapIndex * fixedMapTableRowSize);
|
||||
const auto mapOffset = readU32Le(archive.bytes, tableOffset);
|
||||
const auto mapSize = readU32Le(archive.bytes, tableOffset + 4);
|
||||
if (!mapOffset || !mapSize) {
|
||||
return std::nullopt;
|
||||
}
|
||||
if (*mapOffset > archive.bytes.size() || *mapSize > archive.bytes.size() - *mapOffset) {
|
||||
return std::nullopt;
|
||||
}
|
||||
if ((*mapSize % fixedMapRecordSize) != 0) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
FixedMap map;
|
||||
map.index = mapIndex;
|
||||
for (u32 offset = *mapOffset; offset < *mapOffset + *mapSize; offset += fixedMapRecordSize) {
|
||||
const auto entry = readFixedMapEntry(archive.bytes, offset);
|
||||
if (!entry) {
|
||||
return std::nullopt;
|
||||
}
|
||||
map.entries.push_back(*entry);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
} // namespace remorse
|
||||
83
rebuild/remorse/src/flex_archive.cpp
Normal file
83
rebuild/remorse/src/flex_archive.cpp
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
#include "remorse/flex_archive.h"
|
||||
|
||||
#include "remorse/file_system.h"
|
||||
|
||||
namespace remorse {
|
||||
namespace {
|
||||
|
||||
constexpr u32 flexCountOffset = 0x54;
|
||||
constexpr u32 flexTableOffset = 0x80;
|
||||
constexpr u32 flexEntrySize = 8;
|
||||
|
||||
std::optional<u32> readU32Le(const std::vector<u8>& bytes, u32 offset)
|
||||
{
|
||||
if (offset + 4 > bytes.size()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
return static_cast<u32>(bytes[offset]) |
|
||||
(static_cast<u32>(bytes[offset + 1]) << 8) |
|
||||
(static_cast<u32>(bytes[offset + 2]) << 16) |
|
||||
(static_cast<u32>(bytes[offset + 3]) << 24);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
std::optional<FlexArchive> loadFlexArchive(const std::filesystem::path& path)
|
||||
{
|
||||
auto bytes = readBinaryFile(path);
|
||||
if (!bytes) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
const auto entryCount = readU32Le(*bytes, flexCountOffset);
|
||||
if (!entryCount) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
const u32 tableEnd = flexTableOffset + (*entryCount * flexEntrySize);
|
||||
if (tableEnd > bytes->size()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
FlexArchive archive;
|
||||
archive.entryCount = *entryCount;
|
||||
archive.bytes = *bytes;
|
||||
|
||||
for (u32 index = 0; index < archive.entryCount; ++index) {
|
||||
const u32 tableOffset = flexTableOffset + (index * flexEntrySize);
|
||||
const auto dataOffset = readU32Le(archive.bytes, tableOffset);
|
||||
const auto dataSize = readU32Le(archive.bytes, tableOffset + 4);
|
||||
if (!dataOffset || !dataSize) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
if (*dataOffset == 0 && *dataSize == 0) {
|
||||
continue;
|
||||
}
|
||||
if (*dataOffset == 0 || *dataSize == 0) {
|
||||
continue;
|
||||
}
|
||||
if (*dataOffset > archive.bytes.size() || *dataSize > archive.bytes.size() - *dataOffset) {
|
||||
continue;
|
||||
}
|
||||
|
||||
archive.populatedEntries.push_back(FlexEntry{index, *dataOffset, *dataSize});
|
||||
}
|
||||
|
||||
return archive;
|
||||
}
|
||||
|
||||
std::optional<std::vector<u8>> readFlexEntry(const FlexArchive& archive, u32 index)
|
||||
{
|
||||
for (const FlexEntry& entry : archive.populatedEntries) {
|
||||
if (entry.index == index) {
|
||||
const auto begin = archive.bytes.begin() + entry.offset;
|
||||
const auto end = begin + entry.size;
|
||||
return std::vector<u8>(begin, end);
|
||||
}
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
} // namespace remorse
|
||||
56
rebuild/remorse/src/glob_archive.cpp
Normal file
56
rebuild/remorse/src/glob_archive.cpp
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
#include "remorse/glob_archive.h"
|
||||
|
||||
#include "remorse/flex_archive.h"
|
||||
|
||||
namespace remorse {
|
||||
namespace {
|
||||
|
||||
constexpr u32 globCountSize = 2;
|
||||
constexpr u32 globEntrySize = 6;
|
||||
|
||||
u16 readU16Le(const std::vector<u8>& bytes, u32 offset)
|
||||
{
|
||||
return static_cast<u16>(bytes[offset] | (static_cast<u16>(bytes[offset + 1]) << 8));
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
std::optional<std::vector<GlobObject>> loadGlobObjects(const std::filesystem::path& path)
|
||||
{
|
||||
const auto archive = loadFlexArchive(path);
|
||||
if (!archive) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::vector<GlobObject> objects;
|
||||
for (const FlexEntry& flexEntry : archive->populatedEntries) {
|
||||
const auto bytes = readFlexEntry(*archive, flexEntry.index);
|
||||
if (!bytes || bytes->size() < globCountSize) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
const u16 entryCount = readU16Le(*bytes, 0);
|
||||
const u32 expectedSize = globCountSize + static_cast<u32>(entryCount) * globEntrySize;
|
||||
if (expectedSize > bytes->size()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
GlobObject object;
|
||||
object.index = flexEntry.index;
|
||||
for (u32 index = 0; index < entryCount; ++index) {
|
||||
const u32 offset = globCountSize + index * globEntrySize;
|
||||
GlobObjectEntry entry;
|
||||
entry.x = (*bytes)[offset];
|
||||
entry.y = (*bytes)[offset + 1];
|
||||
entry.z = (*bytes)[offset + 2];
|
||||
entry.shape = readU16Le(*bytes, offset + 3);
|
||||
entry.frame = (*bytes)[offset + 5];
|
||||
object.entries.push_back(entry);
|
||||
}
|
||||
objects.push_back(object);
|
||||
}
|
||||
|
||||
return objects;
|
||||
}
|
||||
|
||||
} // namespace remorse
|
||||
69
rebuild/remorse/src/main.cpp
Normal file
69
rebuild/remorse/src/main.cpp
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
#include "remorse/combat_tactics.h"
|
||||
#include "remorse/entity_dispatch_entry.h"
|
||||
#include "remorse/file_system.h"
|
||||
#include "remorse/flex_archive.h"
|
||||
#include "remorse/fixed_map.h"
|
||||
#include "remorse/glob_archive.h"
|
||||
#include "remorse/palette.h"
|
||||
#include "remorse/runtime.h"
|
||||
#include "remorse/type_flags.h"
|
||||
|
||||
#include <filesystem>
|
||||
#include <iostream>
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
const std::filesystem::path manifestPath = argc > 1
|
||||
? std::filesystem::path(argv[1])
|
||||
: remorse::defaultManifestPath(std::filesystem::current_path());
|
||||
|
||||
const auto manifest = remorse::loadExportManifestSummary(manifestPath);
|
||||
if (!manifest) {
|
||||
std::cerr << "Could not read export manifest: " << manifestPath.string() << '\n';
|
||||
return 2;
|
||||
}
|
||||
|
||||
remorse::printStartupReport(*manifest);
|
||||
remorse::EntityDispatchEntryBase entry{};
|
||||
remorse::EntityDispatchEntry::initBase(&entry);
|
||||
remorse::EntityDispatchEntry::setGroupId(entry, 1);
|
||||
|
||||
const auto gameDataRoot = remorse::findGameDataRoot(std::filesystem::current_path());
|
||||
std::cout << "Game data root: " << (gameDataRoot ? gameDataRoot->path.string() : "not found") << '\n';
|
||||
if (gameDataRoot) {
|
||||
const auto palettePath = remorse::resolveGameDataPath(*gameDataRoot, std::filesystem::path("STATIC") / "GAMEPAL.PAL");
|
||||
const auto paletteBytes = palettePath ? remorse::readBinaryFile(*palettePath) : std::nullopt;
|
||||
std::cout << "GAMEPAL.PAL bytes: " << (paletteBytes ? paletteBytes->size() : 0) << '\n';
|
||||
const auto palette = palettePath ? remorse::loadPalette256(*palettePath) : std::nullopt;
|
||||
std::cout << "GAMEPAL.PAL colors: " << (palette ? palette->colors.size() : 0) << '\n';
|
||||
|
||||
const auto combatPath = remorse::resolveGameDataPath(*gameDataRoot, std::filesystem::path("STATIC") / "COMBAT.DAT");
|
||||
const auto combatArchive = combatPath ? remorse::loadFlexArchive(*combatPath) : std::nullopt;
|
||||
std::cout << "COMBAT.DAT populated entries: " << (combatArchive ? combatArchive->populatedEntries.size() : 0) << '\n';
|
||||
const auto combatTactics = combatPath ? remorse::loadCombatTactics(*combatPath) : std::nullopt;
|
||||
std::cout << "COMBAT.DAT tactics: " << (combatTactics ? combatTactics->size() : 0) << '\n';
|
||||
|
||||
const auto fixedPath = remorse::resolveGameDataPath(*gameDataRoot, std::filesystem::path("STATIC") / "FIXED.DAT");
|
||||
const auto fixedArchive = fixedPath ? remorse::loadFixedMapArchive(*fixedPath) : std::nullopt;
|
||||
std::cout << "FIXED.DAT maps: " << (fixedArchive ? fixedArchive->mapCount : 0) << '\n';
|
||||
if (fixedArchive) {
|
||||
for (remorse::u32 index = 0; index < fixedArchive->mapCount; ++index) {
|
||||
const auto map = remorse::loadFixedMap(*fixedArchive, index);
|
||||
if (map && !map->entries.empty()) {
|
||||
std::cout << "FIXED.DAT first non-empty map: " << index << '\n';
|
||||
std::cout << "FIXED.DAT first non-empty entries: " << map->entries.size() << '\n';
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const auto globPath = remorse::resolveGameDataPath(*gameDataRoot, std::filesystem::path("STATIC") / "GLOB.FLX");
|
||||
const auto globObjects = globPath ? remorse::loadGlobObjects(*globPath) : std::nullopt;
|
||||
std::cout << "GLOB.FLX objects: " << (globObjects ? globObjects->size() : 0) << '\n';
|
||||
|
||||
const auto typeFlagPath = remorse::resolveGameDataPath(*gameDataRoot, std::filesystem::path("STATIC") / "TYPEFLAG.DAT");
|
||||
const auto typeFlags = typeFlagPath ? remorse::loadTypeFlags(*typeFlagPath) : std::nullopt;
|
||||
std::cout << "TYPEFLAG.DAT shapes: " << (typeFlags ? typeFlags->size() : 0) << '\n';
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
51
rebuild/remorse/src/map_preview_main.cpp
Normal file
51
rebuild/remorse/src/map_preview_main.cpp
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
#include "remorse/file_system.h"
|
||||
#include "remorse/map_renderer.h"
|
||||
|
||||
#include <filesystem>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
const auto root = remorse::findGameDataRoot(std::filesystem::current_path());
|
||||
if (!root) {
|
||||
std::cerr << "Could not locate Crusader game data root.\n";
|
||||
return 2;
|
||||
}
|
||||
|
||||
remorse::MapRenderOptions options;
|
||||
std::filesystem::path outputPath = std::filesystem::path("rebuild") / "remorse" / "out" / "preview" / "map-preview.bmp";
|
||||
if (argc > 1) {
|
||||
outputPath = argv[1];
|
||||
}
|
||||
if (argc > 2) {
|
||||
options.mapIndex = static_cast<remorse::u32>(std::stoul(argv[2], nullptr, 0));
|
||||
}
|
||||
if (argc == 7) {
|
||||
options.worldRect = remorse::WorldRect{
|
||||
std::stoi(argv[3], nullptr, 0),
|
||||
std::stoi(argv[4], nullptr, 0),
|
||||
std::stoi(argv[5], nullptr, 0),
|
||||
std::stoi(argv[6], nullptr, 0)};
|
||||
}
|
||||
|
||||
const auto image = remorse::renderMapPreview(*root, options);
|
||||
if (!image) {
|
||||
std::cerr << "Could not render map preview.\n";
|
||||
return 1;
|
||||
}
|
||||
if (!remorse::writeBmp24(outputPath, *image)) {
|
||||
std::cerr << "Could not write preview BMP: " << outputPath.string() << '\n';
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::cout << "Preview: " << outputPath.string() << '\n';
|
||||
std::cout << "Map: " << options.mapIndex << '\n';
|
||||
std::cout << "Raw fixed items: " << image->rawItemCount << '\n';
|
||||
std::cout << "Render items after filters/globs: " << image->renderItemCount << '\n';
|
||||
std::cout << "Decoded items: " << image->decodedItemCount << '\n';
|
||||
std::cout << "Painted items in crop: " << image->paintedItemCount << '\n';
|
||||
std::cout << "Image size: " << image->width << "x" << image->height << '\n';
|
||||
std::cout << "Full scene bounds: " << image->fullWidth << "x" << image->fullHeight << '\n';
|
||||
return 0;
|
||||
}
|
||||
362
rebuild/remorse/src/map_renderer.cpp
Normal file
362
rebuild/remorse/src/map_renderer.cpp
Normal file
|
|
@ -0,0 +1,362 @@
|
|||
#include "remorse/map_renderer.h"
|
||||
|
||||
#include "remorse/fixed_map.h"
|
||||
#include "remorse/glob_archive.h"
|
||||
#include "remorse/shape_archive.h"
|
||||
#include "remorse/type_flags.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <fstream>
|
||||
#include <map>
|
||||
|
||||
namespace remorse {
|
||||
namespace {
|
||||
|
||||
constexpr u16 flagInvisible = 0x0010;
|
||||
constexpr u16 flagFlipped = 0x0020;
|
||||
constexpr u32 siRoof = 0x0400;
|
||||
constexpr s32 globCoordMask = ~0x3ff;
|
||||
constexpr s32 globCoordShift = 2;
|
||||
constexpr s32 globCoordOffset = 2;
|
||||
|
||||
struct SceneItem {
|
||||
s32 x = 0;
|
||||
s32 y = 0;
|
||||
s32 z = 0;
|
||||
u16 shape = 0;
|
||||
u8 frame = 0;
|
||||
u16 flags = 0;
|
||||
u16 quality = 0;
|
||||
u8 mapNumber = 0;
|
||||
bool fromFixed = true;
|
||||
};
|
||||
|
||||
struct RenderNode {
|
||||
SceneItem item;
|
||||
DecodedShapeFrame decoded;
|
||||
s32 left = 0;
|
||||
s32 top = 0;
|
||||
s32 right = 0;
|
||||
s32 bottom = 0;
|
||||
};
|
||||
|
||||
bool isEggFamily(u8 family)
|
||||
{
|
||||
return family == 3 || family == 4 || family == 7 || family == 8;
|
||||
}
|
||||
|
||||
bool inWorldRect(const SceneItem& item, const std::optional<WorldRect>& rect)
|
||||
{
|
||||
if (!rect) {
|
||||
return true;
|
||||
}
|
||||
return item.x >= rect->minX && item.x <= rect->maxX && item.y >= rect->minY && item.y <= rect->maxY;
|
||||
}
|
||||
|
||||
RgbColor scalePaletteColor(const RgbColor& color)
|
||||
{
|
||||
RgbColor scaled;
|
||||
scaled.red = static_cast<u8>((static_cast<u32>(color.red) * 255) / 63);
|
||||
scaled.green = static_cast<u8>((static_cast<u32>(color.green) * 255) / 63);
|
||||
scaled.blue = static_cast<u8>((static_cast<u32>(color.blue) * 255) / 63);
|
||||
return scaled;
|
||||
}
|
||||
|
||||
std::optional<GlobObject> findGlobObject(const std::vector<GlobObject>& globs, u32 index)
|
||||
{
|
||||
for (const GlobObject& object : globs) {
|
||||
if (object.index == index) {
|
||||
return object;
|
||||
}
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::vector<SceneItem> collectSceneItems(
|
||||
const FixedMap& fixedMap,
|
||||
const std::vector<ShapeTypeInfo>& typeFlags,
|
||||
const std::vector<GlobObject>& globs,
|
||||
const MapRenderOptions& options)
|
||||
{
|
||||
std::vector<SceneItem> pending;
|
||||
pending.reserve(fixedMap.entries.size());
|
||||
for (const FixedMapEntry& entry : fixedMap.entries) {
|
||||
pending.push_back(SceneItem{
|
||||
static_cast<s32>(entry.x),
|
||||
static_cast<s32>(entry.y),
|
||||
static_cast<s32>(entry.z),
|
||||
entry.shape,
|
||||
entry.frame,
|
||||
entry.flags,
|
||||
entry.quality,
|
||||
entry.mapNumber,
|
||||
true});
|
||||
}
|
||||
|
||||
std::vector<SceneItem> items;
|
||||
for (std::size_t index = 0; index < pending.size(); ++index) {
|
||||
const SceneItem item = pending[index];
|
||||
if (!inWorldRect(item, options.worldRect)) {
|
||||
continue;
|
||||
}
|
||||
if (item.shape >= typeFlags.size()) {
|
||||
continue;
|
||||
}
|
||||
const ShapeTypeInfo& info = typeFlags[item.shape];
|
||||
if ((item.flags & flagInvisible) != 0 && !options.includeHiddenMarkers) {
|
||||
continue;
|
||||
}
|
||||
if ((info.flags & siRoof) != 0 && !options.includeRoofs) {
|
||||
continue;
|
||||
}
|
||||
if (options.expandGlobs && info.family == 3 && item.fromFixed) {
|
||||
const auto glob = findGlobObject(globs, item.quality);
|
||||
if (glob) {
|
||||
for (const GlobObjectEntry& entry : glob->entries) {
|
||||
pending.push_back(SceneItem{
|
||||
(item.x & globCoordMask) + (static_cast<s32>(entry.x) << globCoordShift) + globCoordOffset,
|
||||
(item.y & globCoordMask) + (static_cast<s32>(entry.y) << globCoordShift) + globCoordOffset,
|
||||
item.z + entry.z,
|
||||
entry.shape,
|
||||
entry.frame,
|
||||
0,
|
||||
0,
|
||||
item.mapNumber,
|
||||
false});
|
||||
}
|
||||
}
|
||||
if (!options.includeHiddenMarkers) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (isEggFamily(info.family) && !options.includeHiddenMarkers) {
|
||||
continue;
|
||||
}
|
||||
items.push_back(item);
|
||||
}
|
||||
return items;
|
||||
}
|
||||
|
||||
s32 screenX(const SceneItem& item)
|
||||
{
|
||||
return (item.x - item.y) / 4;
|
||||
}
|
||||
|
||||
s32 screenY(const SceneItem& item)
|
||||
{
|
||||
return (item.x + item.y) / 8 - item.z;
|
||||
}
|
||||
|
||||
std::optional<DecodedShapeFrame> cachedDecode(
|
||||
const ShapeArchive& archive,
|
||||
std::map<std::pair<u32, u32>, DecodedShapeFrame>& cache,
|
||||
u32 shape,
|
||||
u32 frame)
|
||||
{
|
||||
const auto key = std::make_pair(shape, frame);
|
||||
const auto found = cache.find(key);
|
||||
if (found != cache.end()) {
|
||||
return found->second;
|
||||
}
|
||||
const auto decoded = decodeShapeFrame(archive, shape, frame);
|
||||
if (!decoded) {
|
||||
return std::nullopt;
|
||||
}
|
||||
cache.emplace(key, *decoded);
|
||||
return decoded;
|
||||
}
|
||||
|
||||
std::vector<RenderNode> prepareRenderNodes(const std::vector<SceneItem>& items, const ShapeArchive& shapeArchive)
|
||||
{
|
||||
std::map<std::pair<u32, u32>, DecodedShapeFrame> cache;
|
||||
std::vector<RenderNode> nodes;
|
||||
for (const SceneItem& item : items) {
|
||||
const auto decoded = cachedDecode(shapeArchive, cache, item.shape, item.frame);
|
||||
if (!decoded) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const s32 bottomX = screenX(item);
|
||||
const s32 bottomY = screenY(item);
|
||||
const bool flipped = (item.flags & flagFlipped) != 0;
|
||||
RenderNode node;
|
||||
node.item = item;
|
||||
node.decoded = *decoded;
|
||||
node.left = flipped
|
||||
? bottomX + node.decoded.frame.xOffset - static_cast<s32>(node.decoded.frame.width)
|
||||
: bottomX - node.decoded.frame.xOffset;
|
||||
node.top = bottomY - node.decoded.frame.yOffset;
|
||||
node.right = node.left + static_cast<s32>(node.decoded.frame.width);
|
||||
node.bottom = node.top + static_cast<s32>(node.decoded.frame.height);
|
||||
nodes.push_back(node);
|
||||
}
|
||||
|
||||
std::sort(nodes.begin(), nodes.end(), [](const RenderNode& left, const RenderNode& right) {
|
||||
const s32 leftKey = left.item.x + left.item.y;
|
||||
const s32 rightKey = right.item.x + right.item.y;
|
||||
if (leftKey != rightKey) {
|
||||
return leftKey < rightKey;
|
||||
}
|
||||
if (left.item.z != right.item.z) {
|
||||
return left.item.z < right.item.z;
|
||||
}
|
||||
if (left.item.y != right.item.y) {
|
||||
return left.item.y < right.item.y;
|
||||
}
|
||||
return left.item.x < right.item.x;
|
||||
});
|
||||
return nodes;
|
||||
}
|
||||
|
||||
void paintNode(RenderedMapImage& image, const RenderNode& node, const Palette256& palette)
|
||||
{
|
||||
const bool flipped = (node.item.flags & flagFlipped) != 0;
|
||||
for (u32 sourceY = 0; sourceY < node.decoded.frame.height; ++sourceY) {
|
||||
const s32 destY = node.top - image.sourceTop + static_cast<s32>(sourceY);
|
||||
if (destY < 0 || destY >= static_cast<s32>(image.height)) {
|
||||
continue;
|
||||
}
|
||||
const std::size_t sourceRow = static_cast<std::size_t>(sourceY) * node.decoded.frame.width;
|
||||
for (u32 sourceX = 0; sourceX < node.decoded.frame.width; ++sourceX) {
|
||||
const u32 actualSourceX = flipped ? node.decoded.frame.width - 1 - sourceX : sourceX;
|
||||
const s16 colorIndex = node.decoded.pixels[sourceRow + actualSourceX];
|
||||
if (colorIndex < 0 || colorIndex >= 256) {
|
||||
continue;
|
||||
}
|
||||
const s32 destX = node.left - image.sourceLeft + static_cast<s32>(sourceX);
|
||||
if (destX < 0 || destX >= static_cast<s32>(image.width)) {
|
||||
continue;
|
||||
}
|
||||
image.pixels[static_cast<std::size_t>(destY) * image.width + destX] = scalePaletteColor(palette.colors[colorIndex]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void writeLe16(std::ofstream& output, u16 value)
|
||||
{
|
||||
output.put(static_cast<char>(value & 0xff));
|
||||
output.put(static_cast<char>((value >> 8) & 0xff));
|
||||
}
|
||||
|
||||
void writeLe32(std::ofstream& output, u32 value)
|
||||
{
|
||||
output.put(static_cast<char>(value & 0xff));
|
||||
output.put(static_cast<char>((value >> 8) & 0xff));
|
||||
output.put(static_cast<char>((value >> 16) & 0xff));
|
||||
output.put(static_cast<char>((value >> 24) & 0xff));
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
std::optional<RenderedMapImage> renderMapPreview(const GameDataRoot& root, const MapRenderOptions& options)
|
||||
{
|
||||
const auto fixedPath = resolveGameDataPath(root, std::filesystem::path("STATIC") / "FIXED.DAT");
|
||||
const auto globPath = resolveGameDataPath(root, std::filesystem::path("STATIC") / "GLOB.FLX");
|
||||
const auto shapePath = resolveGameDataPath(root, std::filesystem::path("STATIC") / "SHAPES.FLX");
|
||||
const auto palettePath = resolveGameDataPath(root, std::filesystem::path("STATIC") / "GAMEPAL.PAL");
|
||||
const auto typeFlagPath = resolveGameDataPath(root, std::filesystem::path("STATIC") / "TYPEFLAG.DAT");
|
||||
if (!fixedPath || !globPath || !shapePath || !palettePath || !typeFlagPath) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
const auto fixedArchive = loadFixedMapArchive(*fixedPath);
|
||||
const auto fixedMap = fixedArchive ? loadFixedMap(*fixedArchive, options.mapIndex) : std::nullopt;
|
||||
const auto globs = loadGlobObjects(*globPath);
|
||||
const auto shapeArchive = loadShapeArchive(*shapePath);
|
||||
const auto palette = loadPalette256(*palettePath);
|
||||
const auto typeFlags = loadTypeFlags(*typeFlagPath);
|
||||
if (!fixedMap || !globs || !shapeArchive || !palette || !typeFlags) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
const auto items = collectSceneItems(*fixedMap, *typeFlags, *globs, options);
|
||||
auto nodes = prepareRenderNodes(items, *shapeArchive);
|
||||
if (nodes.empty()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
s32 minLeft = nodes[0].left;
|
||||
s32 minTop = nodes[0].top;
|
||||
s32 maxRight = nodes[0].right;
|
||||
s32 maxBottom = nodes[0].bottom;
|
||||
for (const RenderNode& node : nodes) {
|
||||
minLeft = std::min(minLeft, node.left);
|
||||
minTop = std::min(minTop, node.top);
|
||||
maxRight = std::max(maxRight, node.right);
|
||||
maxBottom = std::max(maxBottom, node.bottom);
|
||||
}
|
||||
|
||||
RenderedMapImage image;
|
||||
image.rawItemCount = static_cast<u32>(fixedMap->entries.size());
|
||||
image.renderItemCount = static_cast<u32>(items.size());
|
||||
image.decodedItemCount = static_cast<u32>(nodes.size());
|
||||
image.sourceLeft = nodes[0].left;
|
||||
image.sourceTop = nodes[0].top;
|
||||
image.fullWidth = maxRight - minLeft;
|
||||
image.fullHeight = maxBottom - minTop;
|
||||
image.width = std::min(options.maxWidth, static_cast<u32>(std::max<s32>(1, image.fullWidth)));
|
||||
image.height = std::min(options.maxHeight, static_cast<u32>(std::max<s32>(1, image.fullHeight)));
|
||||
image.pixels.assign(
|
||||
static_cast<std::size_t>(image.width) * image.height,
|
||||
RgbColor{static_cast<u8>(10), static_cast<u8>(12), static_cast<u8>(18)});
|
||||
|
||||
for (const RenderNode& node : nodes) {
|
||||
if (node.right < image.sourceLeft || node.bottom < image.sourceTop ||
|
||||
node.left >= image.sourceLeft + static_cast<s32>(image.width) ||
|
||||
node.top >= image.sourceTop + static_cast<s32>(image.height)) {
|
||||
continue;
|
||||
}
|
||||
paintNode(image, node, *palette);
|
||||
++image.paintedItemCount;
|
||||
}
|
||||
return image;
|
||||
}
|
||||
|
||||
bool writeBmp24(const std::filesystem::path& path, const RenderedMapImage& image)
|
||||
{
|
||||
if (image.width == 0 || image.height == 0 || image.pixels.size() != static_cast<std::size_t>(image.width) * image.height) {
|
||||
return false;
|
||||
}
|
||||
std::filesystem::create_directories(path.parent_path());
|
||||
std::ofstream output(path, std::ios::binary);
|
||||
if (!output) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const u32 rowStride = ((image.width * 3 + 3) / 4) * 4;
|
||||
const u32 pixelBytes = rowStride * image.height;
|
||||
const u32 fileSize = 54 + pixelBytes;
|
||||
|
||||
output.put('B');
|
||||
output.put('M');
|
||||
writeLe32(output, fileSize);
|
||||
writeLe16(output, 0);
|
||||
writeLe16(output, 0);
|
||||
writeLe32(output, 54);
|
||||
writeLe32(output, 40);
|
||||
writeLe32(output, image.width);
|
||||
writeLe32(output, image.height);
|
||||
writeLe16(output, 1);
|
||||
writeLe16(output, 24);
|
||||
writeLe32(output, 0);
|
||||
writeLe32(output, pixelBytes);
|
||||
writeLe32(output, 2835);
|
||||
writeLe32(output, 2835);
|
||||
writeLe32(output, 0);
|
||||
writeLe32(output, 0);
|
||||
|
||||
std::vector<u8> row(rowStride, 0);
|
||||
for (s32 y = static_cast<s32>(image.height) - 1; y >= 0; --y) {
|
||||
std::fill(row.begin(), row.end(), static_cast<u8>(0));
|
||||
for (u32 x = 0; x < image.width; ++x) {
|
||||
const RgbColor& pixel = image.pixels[static_cast<std::size_t>(y) * image.width + x];
|
||||
row[x * 3] = pixel.blue;
|
||||
row[x * 3 + 1] = pixel.green;
|
||||
row[x * 3 + 2] = pixel.red;
|
||||
}
|
||||
output.write(reinterpret_cast<const char*>(row.data()), row.size());
|
||||
}
|
||||
return static_cast<bool>(output);
|
||||
}
|
||||
|
||||
} // namespace remorse
|
||||
23
rebuild/remorse/src/palette.cpp
Normal file
23
rebuild/remorse/src/palette.cpp
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
#include "remorse/palette.h"
|
||||
|
||||
#include "remorse/file_system.h"
|
||||
|
||||
namespace remorse {
|
||||
|
||||
std::optional<Palette256> loadPalette256(const std::filesystem::path& path)
|
||||
{
|
||||
const auto bytes = readBinaryFile(path);
|
||||
if (!bytes || bytes->size() != 256 * 3) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
Palette256 palette;
|
||||
for (std::size_t index = 0; index < palette.colors.size(); ++index) {
|
||||
palette.colors[index].red = (*bytes)[index * 3];
|
||||
palette.colors[index].green = (*bytes)[index * 3 + 1];
|
||||
palette.colors[index].blue = (*bytes)[index * 3 + 2];
|
||||
}
|
||||
return palette;
|
||||
}
|
||||
|
||||
} // namespace remorse
|
||||
79
rebuild/remorse/src/runtime.cpp
Normal file
79
rebuild/remorse/src/runtime.cpp
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
#include "remorse/runtime.h"
|
||||
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <regex>
|
||||
#include <sstream>
|
||||
|
||||
namespace remorse {
|
||||
namespace {
|
||||
|
||||
std::string readTextFile(const std::filesystem::path& path)
|
||||
{
|
||||
std::ifstream input(path, std::ios::binary);
|
||||
if (!input) {
|
||||
return {};
|
||||
}
|
||||
|
||||
std::ostringstream buffer;
|
||||
buffer << input.rdbuf();
|
||||
return buffer.str();
|
||||
}
|
||||
|
||||
std::optional<std::string> readJsonString(const std::string& json, const char* key)
|
||||
{
|
||||
const std::regex pattern(std::string("\\\"") + key + "\\\"\\s*:\\s*\\\"([^\\\"]*)\\\"");
|
||||
std::smatch match;
|
||||
if (!std::regex_search(json, match, pattern)) {
|
||||
return std::nullopt;
|
||||
}
|
||||
return match[1].str();
|
||||
}
|
||||
|
||||
u32 readJsonU32(const std::string& json, const char* key)
|
||||
{
|
||||
const std::regex pattern(std::string("\\\"") + key + "\\\"\\s*:\\s*([0-9]+)");
|
||||
std::smatch match;
|
||||
if (!std::regex_search(json, match, pattern)) {
|
||||
return 0;
|
||||
}
|
||||
return static_cast<u32>(std::stoul(match[1].str()));
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
std::optional<ExportManifestSummary> loadExportManifestSummary(const std::filesystem::path& manifestPath)
|
||||
{
|
||||
const std::string json = readTextFile(manifestPath);
|
||||
if (json.empty()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
ExportManifestSummary summary;
|
||||
summary.program = readJsonString(json, "program").value_or("unknown");
|
||||
summary.outputRoot = readJsonString(json, "outputRoot").value_or("");
|
||||
summary.functionFiles = readJsonU32(json, "functionFiles");
|
||||
summary.fallbackFunctionFiles = readJsonU32(json, "fallbackFunctionFiles");
|
||||
summary.globalBuckets = readJsonU32(json, "globalBuckets");
|
||||
summary.globalDefinitions = readJsonU32(json, "globalDefinitions");
|
||||
return summary;
|
||||
}
|
||||
|
||||
std::filesystem::path defaultManifestPath(const std::filesystem::path& currentPath)
|
||||
{
|
||||
return currentPath / "exports" / "src_remorse" / "export_manifest.json";
|
||||
}
|
||||
|
||||
void printStartupReport(const ExportManifestSummary& manifest)
|
||||
{
|
||||
std::cout << "Crusader: No Remorse rebuild harness\n";
|
||||
std::cout << "Program: " << manifest.program << '\n';
|
||||
std::cout << "Export root: " << manifest.outputRoot.string() << '\n';
|
||||
std::cout << "Function files: " << manifest.functionFiles << '\n';
|
||||
std::cout << "Fallback function files: " << manifest.fallbackFunctionFiles << '\n';
|
||||
std::cout << "Global buckets: " << manifest.globalBuckets << '\n';
|
||||
std::cout << "Global definitions: " << manifest.globalDefinitions << '\n';
|
||||
std::cout << "Status: host runtime scaffold is alive; exported game code is not linked yet.\n";
|
||||
}
|
||||
|
||||
} // namespace remorse
|
||||
205
rebuild/remorse/src/shape_archive.cpp
Normal file
205
rebuild/remorse/src/shape_archive.cpp
Normal file
|
|
@ -0,0 +1,205 @@
|
|||
#include "remorse/shape_archive.h"
|
||||
|
||||
namespace remorse {
|
||||
namespace {
|
||||
|
||||
std::optional<u16> readU16Le(const std::vector<u8>& bytes, u32 offset)
|
||||
{
|
||||
if (offset + 2 > bytes.size()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
return static_cast<u16>(bytes[offset] | (static_cast<u16>(bytes[offset + 1]) << 8));
|
||||
}
|
||||
|
||||
std::optional<u32> readU24Le(const std::vector<u8>& bytes, u32 offset)
|
||||
{
|
||||
if (offset + 3 > bytes.size()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
return static_cast<u32>(bytes[offset]) |
|
||||
(static_cast<u32>(bytes[offset + 1]) << 8) |
|
||||
(static_cast<u32>(bytes[offset + 2]) << 16);
|
||||
}
|
||||
|
||||
std::optional<u32> readU32Le(const std::vector<u8>& bytes, u32 offset)
|
||||
{
|
||||
if (offset + 4 > bytes.size()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
return static_cast<u32>(bytes[offset]) |
|
||||
(static_cast<u32>(bytes[offset + 1]) << 8) |
|
||||
(static_cast<u32>(bytes[offset + 2]) << 16) |
|
||||
(static_cast<u32>(bytes[offset + 3]) << 24);
|
||||
}
|
||||
|
||||
std::optional<s32> readS32Le(const std::vector<u8>& bytes, u32 offset)
|
||||
{
|
||||
const auto value = readU32Le(bytes, offset);
|
||||
if (!value) {
|
||||
return std::nullopt;
|
||||
}
|
||||
return static_cast<s32>(*value);
|
||||
}
|
||||
|
||||
std::optional<ShapeFrame> parseFrame(const std::vector<u8>& bytes, u32 frameOffset, u32 frameSize)
|
||||
{
|
||||
if (frameOffset > bytes.size() || frameSize > bytes.size() - frameOffset || frameSize < 28) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::vector<u8> frameBytes(bytes.begin() + frameOffset, bytes.begin() + frameOffset + frameSize);
|
||||
const auto compressed = readU32Le(frameBytes, 8);
|
||||
const auto width = readU32Le(frameBytes, 12);
|
||||
const auto height = readU32Le(frameBytes, 16);
|
||||
const auto xOffset = readS32Le(frameBytes, 20);
|
||||
const auto yOffset = readS32Le(frameBytes, 24);
|
||||
if (!compressed || !width || !height || !xOffset || !yOffset) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
const u32 lineTableSize = *height * 4;
|
||||
if (*height == 0 || *width == 0 || 28 + lineTableSize > frameBytes.size()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
ShapeFrame frame;
|
||||
frame.compressed = *compressed != 0;
|
||||
frame.width = *width;
|
||||
frame.height = *height;
|
||||
frame.xOffset = *xOffset;
|
||||
frame.yOffset = *yOffset;
|
||||
const u32 rleOffset = 28 + lineTableSize;
|
||||
for (u32 row = 0; row < frame.height; ++row) {
|
||||
const auto storedOffset = readU32Le(frameBytes, 28 + row * 4);
|
||||
if (!storedOffset) {
|
||||
return std::nullopt;
|
||||
}
|
||||
const u32 rowFixup = (frame.height - row) * 4;
|
||||
if (*storedOffset < rowFixup) {
|
||||
return std::nullopt;
|
||||
}
|
||||
frame.lineOffsets.push_back(*storedOffset - rowFixup);
|
||||
}
|
||||
frame.rleData.assign(frameBytes.begin() + rleOffset, frameBytes.end());
|
||||
return frame;
|
||||
}
|
||||
|
||||
std::optional<std::vector<s16>> decodePixels(const ShapeFrame& frame)
|
||||
{
|
||||
if (frame.width == 0 || frame.height == 0 || frame.width > 4096 || frame.height > 4096) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::vector<s16> pixels(static_cast<std::size_t>(frame.width) * frame.height, -1);
|
||||
for (u32 row = 0; row < frame.height; ++row) {
|
||||
if (row >= frame.lineOffsets.size()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
u32 pos = frame.lineOffsets[row];
|
||||
u32 x = 0;
|
||||
while (x < frame.width) {
|
||||
if (pos >= frame.rleData.size()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
x += frame.rleData[pos++];
|
||||
if (x == frame.width) {
|
||||
break;
|
||||
}
|
||||
if (x > frame.width || pos >= frame.rleData.size()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
u32 runLength = frame.rleData[pos++];
|
||||
u32 runType = 0;
|
||||
if (frame.compressed) {
|
||||
runType = runLength & 1;
|
||||
runLength >>= 1;
|
||||
}
|
||||
if (runLength == 0 || x + runLength > frame.width) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
const std::size_t rowBase = static_cast<std::size_t>(row) * frame.width + x;
|
||||
if (runType == 0) {
|
||||
if (pos + runLength > frame.rleData.size()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
for (u32 index = 0; index < runLength; ++index) {
|
||||
pixels[rowBase + index] = frame.rleData[pos + index];
|
||||
}
|
||||
pos += runLength;
|
||||
} else {
|
||||
if (pos >= frame.rleData.size()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
const s16 color = frame.rleData[pos++];
|
||||
for (u32 index = 0; index < runLength; ++index) {
|
||||
pixels[rowBase + index] = color;
|
||||
}
|
||||
}
|
||||
x += runLength;
|
||||
}
|
||||
}
|
||||
return pixels;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
std::optional<ShapeArchive> loadShapeArchive(const std::filesystem::path& path)
|
||||
{
|
||||
const auto flex = loadFlexArchive(path);
|
||||
if (!flex) {
|
||||
return std::nullopt;
|
||||
}
|
||||
ShapeArchive shapeArchive;
|
||||
shapeArchive.archive = *flex;
|
||||
return shapeArchive;
|
||||
}
|
||||
|
||||
std::optional<std::vector<ShapeFrame>> loadShapeFrames(const ShapeArchive& archive, u32 shapeIndex)
|
||||
{
|
||||
const auto bytes = readFlexEntry(archive.archive, shapeIndex);
|
||||
if (!bytes || bytes->size() < 6) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
const auto frameCount = readU16Le(*bytes, 4);
|
||||
if (!frameCount || 6 + static_cast<u32>(*frameCount) * 8 > bytes->size()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::vector<ShapeFrame> frames;
|
||||
for (u32 index = 0; index < *frameCount; ++index) {
|
||||
const u32 headerOffset = 6 + index * 8;
|
||||
const auto frameOffset = readU24Le(*bytes, headerOffset);
|
||||
const auto frameSize = readU32Le(*bytes, headerOffset + 4);
|
||||
if (!frameOffset || !frameSize) {
|
||||
return std::nullopt;
|
||||
}
|
||||
const auto frame = parseFrame(*bytes, *frameOffset, *frameSize);
|
||||
if (!frame) {
|
||||
return std::nullopt;
|
||||
}
|
||||
frames.push_back(*frame);
|
||||
}
|
||||
return frames;
|
||||
}
|
||||
|
||||
std::optional<DecodedShapeFrame> decodeShapeFrame(const ShapeArchive& archive, u32 shapeIndex, u32 frameIndex)
|
||||
{
|
||||
const auto frames = loadShapeFrames(archive, shapeIndex);
|
||||
if (!frames || frameIndex >= frames->size()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
const auto pixels = decodePixels((*frames)[frameIndex]);
|
||||
if (!pixels) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
DecodedShapeFrame decoded;
|
||||
decoded.frame = (*frames)[frameIndex];
|
||||
decoded.pixels = *pixels;
|
||||
return decoded;
|
||||
}
|
||||
|
||||
} // namespace remorse
|
||||
38
rebuild/remorse/src/type_flags.cpp
Normal file
38
rebuild/remorse/src/type_flags.cpp
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
#include "remorse/type_flags.h"
|
||||
|
||||
#include "remorse/file_system.h"
|
||||
|
||||
namespace remorse {
|
||||
|
||||
std::optional<std::vector<ShapeTypeInfo>> loadTypeFlags(const std::filesystem::path& path)
|
||||
{
|
||||
const auto bytes = readBinaryFile(path);
|
||||
if (!bytes || (bytes->size() % 9) != 0) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::vector<ShapeTypeInfo> infos;
|
||||
for (u32 offset = 0; offset < bytes->size(); offset += 9) {
|
||||
const u8 byte0 = (*bytes)[offset];
|
||||
const u8 byte1 = (*bytes)[offset + 1];
|
||||
const u8 byte2 = (*bytes)[offset + 2];
|
||||
const u8 byte3 = (*bytes)[offset + 3];
|
||||
const u8 byte4 = (*bytes)[offset + 4];
|
||||
const u8 byte6 = (*bytes)[offset + 6];
|
||||
|
||||
ShapeTypeInfo info;
|
||||
info.flags = static_cast<u32>(byte0) |
|
||||
(static_cast<u32>(byte1 & 0x0f) << 8) |
|
||||
(static_cast<u32>(byte6) << 12);
|
||||
info.family = static_cast<u8>((byte1 >> 4) + ((byte2 & 1) << 4));
|
||||
info.x = static_cast<u8>(((byte3 << 3) | (byte2 >> 5)) & 0x1f);
|
||||
info.y = static_cast<u8>((byte3 >> 2) & 0x1f);
|
||||
info.z = static_cast<u8>(((byte4 << 1) | (byte3 >> 7)) & 0x1f);
|
||||
info.animationType = static_cast<u8>(byte4 >> 4);
|
||||
infos.push_back(info);
|
||||
}
|
||||
|
||||
return infos;
|
||||
}
|
||||
|
||||
} // namespace remorse
|
||||
23
rebuild/remorse/tests/combat_tactics_tests.cpp
Normal file
23
rebuild/remorse/tests/combat_tactics_tests.cpp
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
#include "remorse/combat_tactics.h"
|
||||
#include "remorse/file_system.h"
|
||||
|
||||
#include <cassert>
|
||||
#include <filesystem>
|
||||
|
||||
int main()
|
||||
{
|
||||
const auto root = remorse::findGameDataRoot(std::filesystem::current_path());
|
||||
assert(root.has_value());
|
||||
|
||||
const auto combatPath = remorse::resolveGameDataPath(*root, std::filesystem::path("STATIC") / "COMBAT.DAT");
|
||||
assert(combatPath.has_value());
|
||||
|
||||
const auto tactics = remorse::loadCombatTactics(*combatPath);
|
||||
assert(tactics.has_value());
|
||||
assert(tactics->size() == 14);
|
||||
assert((*tactics)[0].name == "Dumb");
|
||||
assert((*tactics)[0].blockOffsets[0] == 0x002c);
|
||||
assert((*tactics)[0].blockOffsets[1] == 0x0031);
|
||||
assert(!(*tactics)[0].bytecode.empty());
|
||||
return 0;
|
||||
}
|
||||
26
rebuild/remorse/tests/entity_dispatch_entry_tests.cpp
Normal file
26
rebuild/remorse/tests/entity_dispatch_entry_tests.cpp
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
#include "remorse/entity_dispatch_entry.h"
|
||||
|
||||
#include <cassert>
|
||||
|
||||
int main()
|
||||
{
|
||||
remorse::EntityDispatchEntryBase entry{};
|
||||
remorse::EntityDispatchEntry::initBase(&entry);
|
||||
|
||||
remorse::EntityDispatchEntry::setSourceType(entry, 0x1234);
|
||||
assert(entry.bytes[0x04] == 0x34);
|
||||
assert(entry.bytes[0x05] == 0x12);
|
||||
|
||||
remorse::EntityDispatchEntry::setEventTypeChecked(entry, 0x00f2);
|
||||
assert(entry.bytes[0x06] == 0xf2);
|
||||
assert(entry.bytes[0x07] == 0x00);
|
||||
|
||||
entry.bytes[0x08] = 0xe0;
|
||||
remorse::EntityDispatchEntry::setGroupId(entry, 0x23);
|
||||
assert(entry.bytes[0x08] == 0xe3);
|
||||
|
||||
remorse::EntityDispatchEntry::incrementGroupId(entry);
|
||||
assert(entry.bytes[0x08] == 0xe4);
|
||||
|
||||
return 0;
|
||||
}
|
||||
24
rebuild/remorse/tests/file_system_tests.cpp
Normal file
24
rebuild/remorse/tests/file_system_tests.cpp
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
#include "remorse/file_system.h"
|
||||
|
||||
#include <cassert>
|
||||
#include <filesystem>
|
||||
|
||||
int main()
|
||||
{
|
||||
const auto root = remorse::findGameDataRoot(std::filesystem::current_path());
|
||||
assert(root.has_value());
|
||||
assert(root->hasExecutable);
|
||||
assert(root->hasStaticData);
|
||||
assert(root->hasUsecode);
|
||||
|
||||
const auto palettePath = remorse::resolveGameDataPath(*root, std::filesystem::path("static") / "gamepal.pal");
|
||||
assert(palettePath.has_value());
|
||||
|
||||
const auto paletteBytes = remorse::readBinaryFile(*palettePath);
|
||||
assert(paletteBytes.has_value());
|
||||
assert(!paletteBytes->empty());
|
||||
|
||||
const auto blockedParent = remorse::resolveGameDataPath(*root, std::filesystem::path("..") / "CRUSADER.EXE");
|
||||
assert(!blockedParent.has_value());
|
||||
return 0;
|
||||
}
|
||||
37
rebuild/remorse/tests/fixed_map_tests.cpp
Normal file
37
rebuild/remorse/tests/fixed_map_tests.cpp
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
#include "remorse/file_system.h"
|
||||
#include "remorse/fixed_map.h"
|
||||
|
||||
#include <cassert>
|
||||
#include <filesystem>
|
||||
|
||||
int main()
|
||||
{
|
||||
const auto root = remorse::findGameDataRoot(std::filesystem::current_path());
|
||||
assert(root.has_value());
|
||||
|
||||
const auto fixedPath = remorse::resolveGameDataPath(*root, std::filesystem::path("STATIC") / "FIXED.DAT");
|
||||
assert(fixedPath.has_value());
|
||||
|
||||
const auto archive = remorse::loadFixedMapArchive(*fixedPath);
|
||||
assert(archive.has_value());
|
||||
assert(archive->mapCount > 0);
|
||||
|
||||
const auto firstMap = remorse::loadFixedMap(*archive, 0);
|
||||
assert(firstMap.has_value());
|
||||
assert(firstMap->index == 0);
|
||||
|
||||
bool foundNonEmptyMap = false;
|
||||
for (remorse::u32 index = 0; index < archive->mapCount; ++index) {
|
||||
const auto map = remorse::loadFixedMap(*archive, index);
|
||||
assert(map.has_value());
|
||||
if (!map->entries.empty()) {
|
||||
foundNonEmptyMap = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
assert(foundNonEmptyMap);
|
||||
|
||||
const auto outOfRange = remorse::loadFixedMap(*archive, archive->mapCount);
|
||||
assert(!outOfRange.has_value());
|
||||
return 0;
|
||||
}
|
||||
27
rebuild/remorse/tests/flex_archive_tests.cpp
Normal file
27
rebuild/remorse/tests/flex_archive_tests.cpp
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
#include "remorse/file_system.h"
|
||||
#include "remorse/flex_archive.h"
|
||||
|
||||
#include <cassert>
|
||||
#include <filesystem>
|
||||
|
||||
int main()
|
||||
{
|
||||
const auto root = remorse::findGameDataRoot(std::filesystem::current_path());
|
||||
assert(root.has_value());
|
||||
|
||||
const auto combatPath = remorse::resolveGameDataPath(*root, std::filesystem::path("STATIC") / "COMBAT.DAT");
|
||||
assert(combatPath.has_value());
|
||||
|
||||
const auto archive = remorse::loadFlexArchive(*combatPath);
|
||||
assert(archive.has_value());
|
||||
assert(archive->populatedEntries.size() == 14);
|
||||
|
||||
const auto firstEntry = remorse::readFlexEntry(*archive, 0);
|
||||
assert(firstEntry.has_value());
|
||||
assert(firstEntry->size() == 0x4f);
|
||||
assert((*firstEntry)[0] == 'D');
|
||||
assert((*firstEntry)[1] == 'u');
|
||||
assert((*firstEntry)[2] == 'm');
|
||||
assert((*firstEntry)[3] == 'b');
|
||||
return 0;
|
||||
}
|
||||
28
rebuild/remorse/tests/glob_archive_tests.cpp
Normal file
28
rebuild/remorse/tests/glob_archive_tests.cpp
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
#include "remorse/file_system.h"
|
||||
#include "remorse/glob_archive.h"
|
||||
|
||||
#include <cassert>
|
||||
#include <filesystem>
|
||||
|
||||
int main()
|
||||
{
|
||||
const auto root = remorse::findGameDataRoot(std::filesystem::current_path());
|
||||
assert(root.has_value());
|
||||
|
||||
const auto globPath = remorse::resolveGameDataPath(*root, std::filesystem::path("STATIC") / "GLOB.FLX");
|
||||
assert(globPath.has_value());
|
||||
|
||||
const auto objects = remorse::loadGlobObjects(*globPath);
|
||||
assert(objects.has_value());
|
||||
assert(!objects->empty());
|
||||
|
||||
bool foundObjectWithEntries = false;
|
||||
for (const remorse::GlobObject& object : *objects) {
|
||||
if (!object.entries.empty()) {
|
||||
foundObjectWithEntries = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
assert(foundObjectWithEntries);
|
||||
return 0;
|
||||
}
|
||||
25
rebuild/remorse/tests/map_renderer_tests.cpp
Normal file
25
rebuild/remorse/tests/map_renderer_tests.cpp
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
#include "remorse/file_system.h"
|
||||
#include "remorse/map_renderer.h"
|
||||
|
||||
#include <cassert>
|
||||
#include <filesystem>
|
||||
|
||||
int main()
|
||||
{
|
||||
const auto root = remorse::findGameDataRoot(std::filesystem::current_path());
|
||||
assert(root.has_value());
|
||||
|
||||
remorse::MapRenderOptions options;
|
||||
options.mapIndex = 1;
|
||||
options.maxWidth = 320;
|
||||
options.maxHeight = 200;
|
||||
|
||||
const auto image = remorse::renderMapPreview(*root, options);
|
||||
assert(image.has_value());
|
||||
assert(image->width > 0);
|
||||
assert(image->height > 0);
|
||||
assert(!image->pixels.empty());
|
||||
assert(image->decodedItemCount > 0);
|
||||
assert(image->paintedItemCount > 0);
|
||||
return 0;
|
||||
}
|
||||
19
rebuild/remorse/tests/palette_tests.cpp
Normal file
19
rebuild/remorse/tests/palette_tests.cpp
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
#include "remorse/file_system.h"
|
||||
#include "remorse/palette.h"
|
||||
|
||||
#include <cassert>
|
||||
#include <filesystem>
|
||||
|
||||
int main()
|
||||
{
|
||||
const auto root = remorse::findGameDataRoot(std::filesystem::current_path());
|
||||
assert(root.has_value());
|
||||
|
||||
const auto palettePath = remorse::resolveGameDataPath(*root, std::filesystem::path("STATIC") / "GAMEPAL.PAL");
|
||||
assert(palettePath.has_value());
|
||||
|
||||
const auto palette = remorse::loadPalette256(*palettePath);
|
||||
assert(palette.has_value());
|
||||
assert(palette->colors.size() == 256);
|
||||
return 0;
|
||||
}
|
||||
30
rebuild/remorse/tests/shape_archive_tests.cpp
Normal file
30
rebuild/remorse/tests/shape_archive_tests.cpp
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
#include "remorse/file_system.h"
|
||||
#include "remorse/shape_archive.h"
|
||||
|
||||
#include <cassert>
|
||||
#include <filesystem>
|
||||
|
||||
int main()
|
||||
{
|
||||
const auto root = remorse::findGameDataRoot(std::filesystem::current_path());
|
||||
assert(root.has_value());
|
||||
|
||||
const auto shapesPath = remorse::resolveGameDataPath(*root, std::filesystem::path("STATIC") / "SHAPES.FLX");
|
||||
assert(shapesPath.has_value());
|
||||
|
||||
const auto archive = remorse::loadShapeArchive(*shapesPath);
|
||||
assert(archive.has_value());
|
||||
|
||||
bool decodedFrame = false;
|
||||
for (remorse::u32 shape = 0; shape < archive->archive.entryCount; ++shape) {
|
||||
const auto frame = remorse::decodeShapeFrame(*archive, shape, 0);
|
||||
if (frame && !frame->pixels.empty()) {
|
||||
assert(frame->frame.width > 0);
|
||||
assert(frame->frame.height > 0);
|
||||
decodedFrame = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
assert(decodedFrame);
|
||||
return 0;
|
||||
}
|
||||
28
rebuild/remorse/tests/type_flags_tests.cpp
Normal file
28
rebuild/remorse/tests/type_flags_tests.cpp
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
#include "remorse/file_system.h"
|
||||
#include "remorse/type_flags.h"
|
||||
|
||||
#include <cassert>
|
||||
#include <filesystem>
|
||||
|
||||
int main()
|
||||
{
|
||||
const auto root = remorse::findGameDataRoot(std::filesystem::current_path());
|
||||
assert(root.has_value());
|
||||
|
||||
const auto typeFlagPath = remorse::resolveGameDataPath(*root, std::filesystem::path("STATIC") / "TYPEFLAG.DAT");
|
||||
assert(typeFlagPath.has_value());
|
||||
|
||||
const auto infos = remorse::loadTypeFlags(*typeFlagPath);
|
||||
assert(infos.has_value());
|
||||
assert(infos->size() == 2048);
|
||||
|
||||
bool foundGlobFamily = false;
|
||||
for (const remorse::ShapeTypeInfo& info : *infos) {
|
||||
if (info.family == 3) {
|
||||
foundGlobFamily = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
assert(foundGlobFamily);
|
||||
return 0;
|
||||
}
|
||||
150
rebuild/remorse/tools/import_export_slice.py
Normal file
150
rebuild/remorse/tools/import_export_slice.py
Normal file
|
|
@ -0,0 +1,150 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import dataclasses
|
||||
import pathlib
|
||||
import re
|
||||
import shutil
|
||||
from collections.abc import Iterable
|
||||
|
||||
|
||||
CALLING_CONVENTION_REPLACEMENTS = {
|
||||
"__cdecl16far": "CR_FARCALL",
|
||||
"__cdecl16near": "CR_CDECL",
|
||||
"__stdcall16far": "CR_FARCALL",
|
||||
"__stdcall16near": "CR_STDCALL",
|
||||
"__cdecl": "CR_CDECL",
|
||||
"__stdcall": "CR_STDCALL",
|
||||
"__regcall": "CR_CDECL",
|
||||
}
|
||||
|
||||
EXPORT_HEADER_INCLUDE_RE = re.compile(r'#include\s+"(?:\.\./)*CRUSADER\.EXE\.h"')
|
||||
|
||||
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
class ImportStats:
|
||||
files_read: int = 0
|
||||
files_written: int = 0
|
||||
callconv_replacements: int = 0
|
||||
namespace_qualified_functions: int = 0
|
||||
unresolved_tokens: tuple[str, ...] = ()
|
||||
|
||||
|
||||
def normalize_text(text: str) -> tuple[str, int]:
|
||||
replacement_count = 0
|
||||
text = EXPORT_HEADER_INCLUDE_RE.sub('#include "remorse/export_prelude.h"', text)
|
||||
|
||||
for source, target in CALLING_CONVENTION_REPLACEMENTS.items():
|
||||
text, count = re.subn(rf"\b{re.escape(source)}\b", target, text)
|
||||
replacement_count += count
|
||||
|
||||
return text, replacement_count
|
||||
|
||||
|
||||
def find_unresolved_tokens(text: str) -> tuple[str, ...]:
|
||||
tokens = []
|
||||
for token in ("unaff_SS", "unaff_CS", "__return_storage_ptr__", "CONCAT22", "BADSPACEBASE"):
|
||||
if token in text:
|
||||
tokens.append(token)
|
||||
return tuple(tokens)
|
||||
|
||||
|
||||
def iter_source_files(source_root: pathlib.Path) -> Iterable[pathlib.Path]:
|
||||
for path in sorted(source_root.rglob("*")):
|
||||
if path.is_file() and path.suffix.lower() in {".c", ".h"}:
|
||||
yield path
|
||||
|
||||
|
||||
def import_slice(export_root: pathlib.Path, slice_path: pathlib.Path, output_root: pathlib.Path) -> ImportStats:
|
||||
source_root = export_root / "functions" / slice_path
|
||||
if not source_root.is_dir():
|
||||
raise FileNotFoundError(f"export slice does not exist: {source_root}")
|
||||
|
||||
destination_root = output_root / slice_path
|
||||
if destination_root.exists():
|
||||
shutil.rmtree(destination_root)
|
||||
destination_root.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
files_read = 0
|
||||
files_written = 0
|
||||
callconv_replacements = 0
|
||||
namespace_qualified_functions = 0
|
||||
unresolved_tokens: set[str] = set()
|
||||
|
||||
for source_file in iter_source_files(source_root):
|
||||
files_read += 1
|
||||
relative_path = source_file.relative_to(source_root)
|
||||
destination_name = relative_path.with_suffix(".cpp") if source_file.suffix.lower() == ".c" else relative_path
|
||||
destination_file = destination_root / destination_name
|
||||
destination_file.parent.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
original = source_file.read_text(encoding="utf-8", errors="replace")
|
||||
normalized, replacement_count = normalize_text(original)
|
||||
callconv_replacements += replacement_count
|
||||
namespace_qualified_functions += normalized.count("Remorse::")
|
||||
unresolved_tokens.update(find_unresolved_tokens(normalized))
|
||||
destination_file.write_text(normalized, encoding="utf-8", newline="\n")
|
||||
files_written += 1
|
||||
|
||||
return ImportStats(
|
||||
files_read=files_read,
|
||||
files_written=files_written,
|
||||
callconv_replacements=callconv_replacements,
|
||||
namespace_qualified_functions=namespace_qualified_functions,
|
||||
unresolved_tokens=tuple(sorted(unresolved_tokens)),
|
||||
)
|
||||
|
||||
|
||||
def write_report(path: pathlib.Path, slice_path: pathlib.Path, stats: ImportStats) -> None:
|
||||
lines = [
|
||||
"# Export Slice Import Report",
|
||||
"",
|
||||
f"Slice: `{slice_path.as_posix()}`",
|
||||
f"Files read: {stats.files_read}",
|
||||
f"Files written: {stats.files_written}",
|
||||
f"Calling-convention replacements: {stats.callconv_replacements}",
|
||||
f"Namespace-qualified references: {stats.namespace_qualified_functions}",
|
||||
"",
|
||||
"## Remaining Cleanup Signals",
|
||||
"",
|
||||
]
|
||||
|
||||
if stats.unresolved_tokens:
|
||||
lines.extend(f"- `{token}`" for token in stats.unresolved_tokens)
|
||||
else:
|
||||
lines.append("- No tracked unresolved tokens found by this importer.")
|
||||
|
||||
lines.extend(
|
||||
[
|
||||
"",
|
||||
"This is a staging import only. Do not link the generated files into the host rebuild until their types, globals, and segmented-pointer behavior are reviewed.",
|
||||
"",
|
||||
]
|
||||
)
|
||||
path.write_text("\n".join(lines), encoding="utf-8", newline="\n")
|
||||
|
||||
|
||||
def parse_args() -> argparse.Namespace:
|
||||
parser = argparse.ArgumentParser(description="Stage one Ghidra-exported function slice for manual Remorse rebuild cleanup.")
|
||||
parser.add_argument("slice", help="Path under exports/src_remorse/functions, such as classes/Remorse/EntityDispatchEntry")
|
||||
parser.add_argument("--export-root", type=pathlib.Path, default=pathlib.Path("exports/src_remorse"))
|
||||
parser.add_argument("--output-root", type=pathlib.Path, default=pathlib.Path("rebuild/remorse/imported"))
|
||||
parser.add_argument("--report", type=pathlib.Path, default=pathlib.Path("rebuild/remorse/imported/import_report.md"))
|
||||
return parser.parse_args()
|
||||
|
||||
|
||||
def main() -> int:
|
||||
args = parse_args()
|
||||
slice_path = pathlib.Path(args.slice)
|
||||
stats = import_slice(args.export_root, slice_path, args.output_root)
|
||||
args.report.parent.mkdir(parents=True, exist_ok=True)
|
||||
write_report(args.report, slice_path, stats)
|
||||
print(f"Imported {stats.files_written} files from {slice_path.as_posix()} into {args.output_root / slice_path}")
|
||||
print(f"Report: {args.report}")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
Loading…
Add table
Add a link
Reference in a new issue