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
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
|
||||
Loading…
Add table
Add a link
Reference in a new issue