Make codebase tabs
This commit is contained in:
parent
eeb69fe0df
commit
fbe24aa0f8
|
@ -0,0 +1,11 @@
|
||||||
|
root = true
|
||||||
|
|
||||||
|
[*]
|
||||||
|
end_of_line = lf
|
||||||
|
insert_final_newline = true
|
||||||
|
indent_style = tab
|
||||||
|
indent_size = 4
|
||||||
|
|
||||||
|
# specifically for YAML
|
||||||
|
[{yml, yaml}]
|
||||||
|
indent_style = space
|
|
@ -7,22 +7,22 @@
|
||||||
|
|
||||||
namespace lightningbolt {
|
namespace lightningbolt {
|
||||||
|
|
||||||
/// A read-only file opened via memory mapping.
|
/// A read-only file opened via memory mapping.
|
||||||
/// On POSIX systems, we use mmap(2). Etc etc.
|
/// On POSIX systems, we use mmap(2). Etc etc.
|
||||||
struct MmapFile {
|
struct MmapFile {
|
||||||
MmapFile();
|
MmapFile();
|
||||||
~MmapFile();
|
~MmapFile();
|
||||||
|
|
||||||
// Opens for read-only mode.
|
// Opens for read-only mode.
|
||||||
ErrorOr<void> Open(const fs::path& path);
|
ErrorOr<void> Open(const fs::path& path);
|
||||||
void Close();
|
void Close();
|
||||||
|
|
||||||
u8* GetMapping() const;
|
u8* GetMapping() const;
|
||||||
usize GetMappingSize() const;
|
usize GetMappingSize() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
struct Impl;
|
struct Impl;
|
||||||
Unique<Impl> impl;
|
Unique<Impl> impl;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
|
@ -10,57 +10,57 @@ namespace lightningbolt {
|
||||||
|
|
||||||
struct MmapFile::Impl {
|
struct MmapFile::Impl {
|
||||||
|
|
||||||
~Impl() {
|
~Impl() {
|
||||||
Close();
|
Close();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Close() {
|
void Close() {
|
||||||
if(mapping) {
|
if(mapping) {
|
||||||
munmap(mapping, mappingSize);
|
munmap(mapping, mappingSize);
|
||||||
mapping = nullptr;
|
mapping = nullptr;
|
||||||
mappingSize = 0;
|
mappingSize = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ErrorOr<void> Open(const fs::path& path) {
|
ErrorOr<void> Open(const fs::path& path) {
|
||||||
int fd = open(path.string().c_str(), O_RDONLY);
|
int fd = open(path.string().c_str(), O_RDONLY);
|
||||||
|
|
||||||
// Error opening file.
|
// Error opening file.
|
||||||
if(fd == -1)
|
if(fd == -1)
|
||||||
return std::error_code{errno, std::system_category()};
|
return std::error_code{errno, std::system_category()};
|
||||||
|
|
||||||
{
|
{
|
||||||
auto last = lseek64(fd, 0, SEEK_END);
|
auto last = lseek64(fd, 0, SEEK_END);
|
||||||
mappingSize = lseek64(fd, 0, SEEK_CUR);
|
mappingSize = lseek64(fd, 0, SEEK_CUR);
|
||||||
lseek64(fd, last, SEEK_SET);
|
lseek64(fd, last, SEEK_SET);
|
||||||
}
|
}
|
||||||
|
|
||||||
mapping = static_cast<u8*>(mmap(nullptr, mappingSize, PROT_READ, MAP_PRIVATE, fd, 0));
|
mapping = static_cast<u8*>(mmap(nullptr, mappingSize, PROT_READ, MAP_PRIVATE, fd, 0));
|
||||||
if(mapping == static_cast<u8*>(MAP_FAILED)) {
|
if(mapping == static_cast<u8*>(MAP_FAILED)) {
|
||||||
mappingSize = 0;
|
mappingSize = 0;
|
||||||
return std::error_code{errno, std::system_category()};
|
return std::error_code{errno, std::system_category()};
|
||||||
}
|
}
|
||||||
|
|
||||||
// Once the mapping has successfully been created
|
// Once the mapping has successfully been created
|
||||||
// we can close the file descriptor instead of needing
|
// we can close the file descriptor instead of needing
|
||||||
// to remember it (the kernel will do so for us.)
|
// to remember it (the kernel will do so for us.)
|
||||||
close(fd);
|
close(fd);
|
||||||
|
|
||||||
// No error.
|
// No error.
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
u8* GetMapping() const {
|
u8* GetMapping() const {
|
||||||
return mapping;
|
return mapping;
|
||||||
}
|
}
|
||||||
|
|
||||||
usize GetMappingSize() const {
|
usize GetMappingSize() const {
|
||||||
return mappingSize;
|
return mappingSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
u8* mapping;
|
u8* mapping;
|
||||||
usize mappingSize;
|
usize mappingSize;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace lightningbolt
|
} // namespace lightningbolt
|
|
@ -1,88 +0,0 @@
|
||||||
// Copyright 2023 The LightningBolt Authors
|
|
||||||
// SPDX-License-Identifier: MIT
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
#include <bit>
|
|
||||||
#include <base/Types.hpp>
|
|
||||||
#include <type_traits>
|
|
||||||
#include <span>
|
|
||||||
|
|
||||||
namespace lightningbolt {
|
|
||||||
|
|
||||||
namespace detail {
|
|
||||||
|
|
||||||
template <class NativeT, class OffsetType>
|
|
||||||
constexpr NativeT* CreatePointerFromAddend(void* BasePointer, OffsetType addend) noexcept {
|
|
||||||
return std::bit_cast<NativeT*>(static_cast<char*>(BasePointer) + addend);
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace detail
|
|
||||||
|
|
||||||
/// An "auto-resolving" semi-sweet (/s) pointer type.
|
|
||||||
/// This is designed to allow resolving offsets in data for
|
|
||||||
/// games written before 64-bit pointers were common/used at all.
|
|
||||||
/// This allows looking up data a lot easier :)
|
|
||||||
///
|
|
||||||
/// [NativeT] is the type of data this would point to
|
|
||||||
/// [OffsetType] is the type of data the "pointer" is repressented as
|
|
||||||
template <class NativeT, class OffsetType = u32>
|
|
||||||
struct OffsetPtr final {
|
|
||||||
using Type = std::remove_cvref_t<NativeT>;
|
|
||||||
using Pointer = Type*;
|
|
||||||
using ConstPointer = const Type*;
|
|
||||||
|
|
||||||
/// Set the offset. Duh!
|
|
||||||
constexpr void Set(OffsetType newOffset) noexcept { rawOffset = newOffset; }
|
|
||||||
|
|
||||||
[[nodiscard]] constexpr OffsetType Raw() const noexcept { return rawOffset; }
|
|
||||||
|
|
||||||
[[nodiscard]] constexpr Pointer operator()(void* baseAddr) const noexcept {
|
|
||||||
// While yucky, it should show problem areas which aren't checking things
|
|
||||||
// immediately rather than read invalid data that might do much worse.
|
|
||||||
if(rawOffset == 0)
|
|
||||||
return nullptr;
|
|
||||||
|
|
||||||
return detail::CreatePointerFromAddend<Type>(baseAddr, rawOffset);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<class NativeU>
|
|
||||||
constexpr OffsetPtr<NativeU, OffsetType>& PtrCast() {
|
|
||||||
// Safety: The data layout of OffsetPtr<> stays
|
|
||||||
// the exact same regardless of the result type, therefore
|
|
||||||
// even though this is *techinically* UB (? using bit_cast it shouldn't be ?),
|
|
||||||
// this isn't problematic
|
|
||||||
return *std::bit_cast<OffsetPtr<NativeU, OffsetType>*>(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
OffsetType rawOffset;
|
|
||||||
};
|
|
||||||
|
|
||||||
/// Like OffsetPtr<T> but for arrays of data
|
|
||||||
template <class NativeT, class OffsetType = u32>
|
|
||||||
struct OffsetArrayPtr final {
|
|
||||||
using Type = std::remove_cvref_t<NativeT>;
|
|
||||||
using Pointer = Type*;
|
|
||||||
using ConstPointer = const Type*;
|
|
||||||
|
|
||||||
using Span = std::span<NativeT>;
|
|
||||||
|
|
||||||
/// Set the offset. Duh!
|
|
||||||
constexpr void Set(OffsetType newOffset) noexcept { rawOffset = newOffset; }
|
|
||||||
|
|
||||||
[[nodiscard]] constexpr OffsetType Raw() const noexcept { return rawOffset; }
|
|
||||||
|
|
||||||
[[nodiscard]] constexpr Span operator()(void* baseAddr, OffsetType length) const noexcept {
|
|
||||||
// While yucky, it should show problem areas which aren't checking things
|
|
||||||
// immediately rather than read invalid data that might do much worse.
|
|
||||||
if(rawOffset == 0 || length == 0)
|
|
||||||
return {};
|
|
||||||
|
|
||||||
return { detail::CreatePointerFromAddend<Type>(baseAddr, rawOffset), length };
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
OffsetType rawOffset;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace ssxtools::core
|
|
|
@ -23,7 +23,7 @@ using usize = std::size_t;
|
||||||
using isize = std::intptr_t;
|
using isize = std::intptr_t;
|
||||||
|
|
||||||
namespace lightningbolt {
|
namespace lightningbolt {
|
||||||
namespace fs = std::filesystem;
|
namespace fs = std::filesystem;
|
||||||
|
|
||||||
template <class T, class Deleter = std::default_delete<T>>
|
template <class T, class Deleter = std::default_delete<T>>
|
||||||
using Unique = std::unique_ptr<T, Deleter>;
|
using Unique = std::unique_ptr<T, Deleter>;
|
||||||
|
|
|
@ -9,7 +9,7 @@ namespace lightningbolt {
|
||||||
|
|
||||||
namespace elf {
|
namespace elf {
|
||||||
|
|
||||||
/// Table entry in the ELF. We use this to create file names.
|
/// Table entry in the ELF. We use this to create file names.
|
||||||
struct [[gnu::packed]] BoltTableEntry {
|
struct [[gnu::packed]] BoltTableEntry {
|
||||||
/// Pointer to filename. Should be adjusted
|
/// Pointer to filename. Should be adjusted
|
||||||
u32 filenamePtr;
|
u32 filenamePtr;
|
||||||
|
@ -17,21 +17,19 @@ namespace lightningbolt {
|
||||||
u16 groupId; // (entryId & 0xff00)
|
u16 groupId; // (entryId & 0xff00)
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Offsets in the ELF to the table.
|
/// Offsets in the ELF to the table.
|
||||||
struct BoltTableOffsets {
|
struct BoltTableOffsets {
|
||||||
u32 usTable;
|
u32 usTable;
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Convert a address to ELF file offset.
|
/// Convert a address to ELF file offset.
|
||||||
constexpr u32 AddressToElfFileOffset(u32 address) {
|
constexpr u32 AddressToElfFileOffset(u32 address) {
|
||||||
constexpr u32 LoadAddress = 0x00100000;
|
constexpr u32 LoadAddress = 0x00100000;
|
||||||
constexpr u32 SectionOffset = 0x80;
|
constexpr u32 SectionOffset = 0x80;
|
||||||
return (address - LoadAddress) + SectionOffset;
|
return (address - LoadAddress) + SectionOffset;
|
||||||
}
|
}
|
||||||
|
|
||||||
static constexpr BoltTableOffsets BoltTableOffsets = {
|
static constexpr BoltTableOffsets BoltTableOffsets = { .usTable = AddressToElfFileOffset(0x0033d400) };
|
||||||
.usTable = AddressToElfFileOffset(0x0033d400)
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace elf
|
} // namespace elf
|
||||||
|
|
||||||
|
@ -51,8 +49,8 @@ namespace lightningbolt {
|
||||||
u32 groupSize;
|
u32 groupSize;
|
||||||
u32 groupOffset;
|
u32 groupOffset;
|
||||||
|
|
||||||
// PS2 pointer padding; bolt library stuffs something here
|
// PS2 pointer padding; bolt library stuffs something here
|
||||||
u32 pad;
|
u32 pad;
|
||||||
|
|
||||||
u32 EntryCount() {
|
u32 EntryCount() {
|
||||||
// Special case: 0x0 == 256 entries.
|
// Special case: 0x0 == 256 entries.
|
||||||
|
|
Loading…
Reference in New Issue