refactor project (1/?)

This commit is contained in:
2023-08-22 15:30:26 +08:00
parent f687297ebb
commit 1b8f2ff0d5
34 changed files with 1805 additions and 1396 deletions

View File

@ -0,0 +1,139 @@
#include "VxMemoryMappedFile.hpp"
#include "VTEncoding.hpp"
namespace LibCmo::VxMath {
VxMemoryMappedFile::VxMemoryMappedFile(const char* u8_filepath) :
// init members
#if defined(LIBCMO_OS_WIN32)
m_hFile(NULL), m_hFileMapping(NULL), m_hFileMapView(NULL),
m_dwFileSizeLow(0), m_dwFileSizeHigh(0),
#else
m_hFile(-1), m_offFileSize(0), m_pFileAddr((void*)-1),
#endif
m_szFilePath(),
m_bIsValid(false), m_pMemoryMappedFileBase(nullptr), m_cbFile(0u) {
// save file path
#if defined(LIBCMO_OS_WIN32)
EncodingHelper::SetStdPathFromU8Path(m_szFilePath, u8_filepath);
#else
this->m_szFilePath = u8_filepath;
#endif
// real mapping work
#if defined(LIBCMO_OS_WIN32)
// open file
this->m_hFile = CreateFileW(
this->m_szFilePath.wstring().c_str(),
GENERIC_READ,
0, // do not share
NULL, // no security
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, // no attr
NULL // no template
);
if (this->m_hFile == INVALID_HANDLE_VALUE) {
return;
}
// get size
m_dwFileSizeLow = ::GetFileSize(this->m_hFile, &this->m_dwFileSizeHigh);
if (m_dwFileSizeLow == INVALID_FILE_SIZE) {
CloseHandle(this->m_hFile);
return;
}
// open mapping
this->m_hFileMapping = CreateFileMappingW(
this->m_hFile,
NULL, // default security
PAGE_READONLY,
0, 0, // expand to file size
NULL // no name
);
if (this->m_hFileMapping == NULL) {
CloseHandle(this->m_hFile);
return;
}
// open map view
this->m_hFileMapView = MapViewOfFile(
this->m_hFileMapping,
FILE_MAP_READ,
0, 0, //no offset
0 // expand to full file size
);
if (this->m_hFileMapView == NULL) {
CloseHandle(m_hFileMapping);
CloseHandle(m_hFile);
}
// write member data
m_pMemoryMappedFileBase = m_hFileMapView;
m_cbFile = static_cast<size_t>(static_cast<uint64_t>(m_dwFileSizeLow) | (static_cast<uint64_t>(m_dwFileSizeHigh) << 32));
#else
// create file
// we do not need provide mode_t, because is served for new created file.
// we are opening a existed file.
this->m_hFile = open(m_szFilePath.string().c_str(), O_RDONLY);
if (m_hFile == -1) {
return;
}
// calculate file size
struct stat sb;
int err = fstat(m_hFile, &sb);
// if failed or not a regular file, exit
if (err == -1 || (sb.st_mode & S_IFMT) != S_IFREG) {
close(m_hFile);
return;
}
// setup size
this->m_offFileSize = sb.st_size;
// map file
this->m_pFileAddr = mmap(
nullptr, // request linux distribute one
this->m_offFileSize, // map the full file
PROT_READ, // only for reading
MAP_PRIVATE,
this->m_hFile,
0 // no offset
);
if (this->m_pFileAddr == MAP_FAILED) {
close(m_hFile);
return;
}
// write member data
m_pMemoryMappedFileBase = m_pFileAddr;
m_cbFile = static_cast<size_t>(this->m_offFileSize);
#endif
// set valid
this->m_bIsValid = true;
}
VxMemoryMappedFile::~VxMemoryMappedFile(void) {
if (this->m_bIsValid) {
// only success mapping need free
this->m_bIsValid = false;
m_cbFile = 0;
m_pMemoryMappedFileBase = nullptr;
#if defined(LIBCMO_OS_WIN32)
UnmapViewOfFile(this->m_hFileMapView);
CloseHandle(m_hFileMapping);
CloseHandle(m_hFile);
#else
munmap(this->m_pFileAddr, this->m_offFileSize);
close(m_hFile);
#endif
}
}
}

View File

@ -0,0 +1,48 @@
#pragma once
#include "VTUtils.hpp"
#if defined(LIBCMO_OS_WIN32)
#include <Windows.h>
#else
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#endif
#include <string>
#include <filesystem>
namespace LibCmo::VxMath {
class VxMemoryMappedFile {
private:
#if defined(LIBCMO_OS_WIN32)
HANDLE m_hFile;
DWORD m_dwFileSizeLow, m_dwFileSizeHigh;
HANDLE m_hFileMapping;
LPVOID m_hFileMapView;
#else
int m_hFile;
off_t m_offFileSize;
void* m_pFileAddr;
#endif
std::filesystem::path m_szFilePath;
void* m_pMemoryMappedFileBase;
size_t m_cbFile;
bool m_bIsValid;
public:
VxMemoryMappedFile(const char* u8_filepath);
VxMemoryMappedFile(const VxMemoryMappedFile&) = delete;
VxMemoryMappedFile& operator=(const VxMemoryMappedFile&) = delete;
~VxMemoryMappedFile(void);
inline void* GetBase(void) { return this->m_pMemoryMappedFileBase; }
inline size_t GetFileSize(void) { return this->m_cbFile; }
inline bool IsValid(void) { return this->m_bIsValid; }
};
}

108
LibCmo/VxMath/VxTypes.hpp Normal file
View File

@ -0,0 +1,108 @@
#pragma once
#include <string>
#include <vector>
#include <cstring>
#include <cinttypes>
#include "../CK2/CKTypes.hpp"
/**
* @brief The VxMath part of LibCmo.
* These classes are prefixed with Vx in original Virtools SDK.
*/
namespace LibCmo::VxMath {
// ========== Type Definition ==========
// ========== Class List ==========
//--- Important classes
class VxMemoryMappedFile;
//---- Misc
/**
* @brief Class representation of a Vector in 3 dimensions
*/
struct VxVector {
float x, y, z;
VxVector() : x(0.0f), y(0.0f), z(0.0f) {}
VxVector(float f) : x(f), y(f), z(f) {}
VxVector(float _x, float _y, float _z) : x(_x), y(_y), z(_z) {}
VxVector(const float f[3]) : x(f[0]), y(f[1]), z(f[2]) {}
};
/**
* @brief Class representation of a Vector in 2 dimensions
*/
struct Vx2DVector {
float x, y;
Vx2DVector() : x(0.0f), y(0.0f) {}
Vx2DVector(float f) : x(f), y(f) {}
Vx2DVector(float _x, float _y, float _z) : x(_x), y(_y) {}
Vx2DVector(CK2::CKINT iX, CK2::CKINT iY) : x((float)iX), y((float)iY) {}
Vx2DVector(const float f[2]) : x(f[0]), y(f[1]) {}
};
/**
* @brief Class representation of a Quaternion
*/
struct VxQuaternion {
float x, y, z, w;
VxQuaternion() : x(0.0f), y(0.0f), z(0.0f), w(1.0f) {}
VxQuaternion(float X, float Y, float Z, float W) : x(X), y(Y), z(Z), w(W) {}
};
struct VxMatrix {
float m_Data[4][4];
VxMatrix() : m_Data() {
std::memset(m_Data, 0, sizeof(m_Data));
m_Data[0][0] = m_Data[1][1] = m_Data[2][2] = m_Data[3][3] = 1.0f;
}
VxMatrix(float m[4][4]) : m_Data() { std::memcpy(m_Data, m, sizeof(m_Data)); }
};
/**
* @brief Enhanced Image description
* @remark The VxImageDescEx holds basically an VxImageDesc with additionnal support for
* Colormap, Image pointer and is ready for future enhancements.
*/
struct VxImageDescEx {
CK2::CKINT Size; ///< Size of the structure
CK2::CKDWORD Flags; ///< Reserved for special formats (such as compressed ) 0 otherwise
CK2::CKINT Width; ///< Width in pixel of the image
CK2::CKINT Height; ///< Height in pixel of the image
union {
CK2::CKINT BytesPerLine; ///< Pitch (width in bytes) of the image
CK2::CKINT TotalImageSize; ///< For compressed image (DXT1...) the total size of the image
};
CK2::CKINT BitsPerPixel; ///< Number of bits per pixel
union {
CK2::CKDWORD RedMask; ///< Mask for Red component
CK2::CKDWORD BumpDuMask; ///< Mask for Bump Du component
};
union {
CK2::CKDWORD GreenMask; ///< Mask for Green component
CK2::CKDWORD BumpDvMask; ///< Mask for Bump Dv component
};
union {
CK2::CKDWORD BlueMask; ///< Mask for Blue component
CK2::CKDWORD BumpLumMask; ///< Mask for Luminance component
};
CK2::CKDWORD AlphaMask; ///< Mask for Alpha component
CK2::CKWORD BytesPerColorEntry; ///< ColorMap Stride
CK2::CKWORD ColorMapEntries; ///< If other than 0 image is palletized
CK2::CKBYTE* ColorMap; ///< Palette colors
CK2::CKBYTE* Image; ///< Image
};
}