mirror of
https://github.com/rwengine/openrw.git
synced 2024-11-22 18:32:44 +01:00
clang-format files in rwengine/src
This commit is contained in:
parent
7b7e65e002
commit
36a2c57d91
@ -1,89 +1,94 @@
|
||||
#include <BinaryStream.hpp>
|
||||
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
|
||||
namespace RW
|
||||
{
|
||||
namespace RW {
|
||||
|
||||
std::unique_ptr<BinaryStream> BinaryStream::parse(const std::string& filename)
|
||||
{
|
||||
std::ifstream dfile(filename, std::ios_base::binary);
|
||||
if ( ! dfile.is_open()) {
|
||||
std::cerr << "Error opening file " << filename << std::endl;
|
||||
return nullptr;
|
||||
}
|
||||
std::unique_ptr<BinaryStream> BinaryStream::parse(const std::string &filename) {
|
||||
std::ifstream dfile(filename, std::ios_base::binary);
|
||||
if (!dfile.is_open()) {
|
||||
std::cerr << "Error opening file " << filename << std::endl;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
dfile.seekg(0, std::ios_base::end);
|
||||
size_t length = dfile.tellg();
|
||||
dfile.seekg(0);
|
||||
char *data = new char[length];
|
||||
dfile.read(data, length);
|
||||
// std::cout << "File is " << length << " bytes" << std::endl << std::endl;
|
||||
dfile.seekg(0, std::ios_base::end);
|
||||
size_t length = dfile.tellg();
|
||||
dfile.seekg(0);
|
||||
char *data = new char[length];
|
||||
dfile.read(data, length);
|
||||
// std::cout << "File is " << length << " bytes" << std::endl << std::endl;
|
||||
|
||||
auto BS = std::unique_ptr<BinaryStream>(new BinaryStream);
|
||||
auto BS = std::unique_ptr<BinaryStream>(new BinaryStream);
|
||||
|
||||
// Set file's ACTUAL length
|
||||
auto header = reinterpret_cast<nativeSectionHeader_t *>(data);
|
||||
length = header->size + sizeof(nativeSectionHeader_t);
|
||||
// Set file's ACTUAL length
|
||||
auto header = reinterpret_cast<nativeSectionHeader_t *>(data);
|
||||
length = header->size + sizeof(nativeSectionHeader_t);
|
||||
|
||||
sectionHeader_t *prevHeader = nullptr;
|
||||
sectionHeader_t *prevHeader = nullptr;
|
||||
|
||||
size_t offset = 0;
|
||||
while (offset < length) {
|
||||
nativeSectionHeader_t *sectionHeader = reinterpret_cast<nativeSectionHeader_t *>(data + offset);
|
||||
sectionHeader_t *sec = new sectionHeader_t;
|
||||
sec->ID = sectionHeader->ID;
|
||||
sec->size = sectionHeader->size;
|
||||
sec->version = sectionHeader->version;
|
||||
if (prevHeader == nullptr)
|
||||
BS->rootHeader = sec;
|
||||
else
|
||||
prevHeader->next = sec;
|
||||
size_t offset = 0;
|
||||
while (offset < length) {
|
||||
nativeSectionHeader_t *sectionHeader =
|
||||
reinterpret_cast<nativeSectionHeader_t *>(data + offset);
|
||||
sectionHeader_t *sec = new sectionHeader_t;
|
||||
sec->ID = sectionHeader->ID;
|
||||
sec->size = sectionHeader->size;
|
||||
sec->version = sectionHeader->version;
|
||||
if (prevHeader == nullptr)
|
||||
BS->rootHeader = sec;
|
||||
else
|
||||
prevHeader->next = sec;
|
||||
|
||||
if (sectionHeader->ID == 0) {
|
||||
std::cout << "Section ID is ZERO! Abort!" << std::endl;
|
||||
break;
|
||||
}
|
||||
if (sectionHeader->ID == 0) {
|
||||
std::cout << "Section ID is ZERO! Abort!" << std::endl;
|
||||
break;
|
||||
}
|
||||
|
||||
std::cout << "Section " << std::hex << sectionHeader->ID
|
||||
<< " (" << sectionIdString(sectionHeader->ID) << ")"
|
||||
<< " - " << std::dec << sectionHeader->size << " bytes" << std::endl;
|
||||
/*
|
||||
std::cout << "Offset " << std::hex << offset << std::endl;
|
||||
*/
|
||||
std::cout << "Section " << std::hex << sectionHeader->ID << " ("
|
||||
<< sectionIdString(sectionHeader->ID) << ")"
|
||||
<< " - " << std::dec << sectionHeader->size << " bytes"
|
||||
<< std::endl;
|
||||
/*
|
||||
std::cout << "Offset " << std::hex << offset << std::endl;
|
||||
*/
|
||||
|
||||
size_t bytesOfData = 0;
|
||||
switch (sectionHeader->ID) {
|
||||
case STRUCT:
|
||||
bytesOfData = sectionHeader->size;
|
||||
sec->data = new uint8_t[bytesOfData];
|
||||
memcpy(sec->data, data + offset + sizeof(nativeSectionHeader_t), bytesOfData);
|
||||
break;
|
||||
}
|
||||
// std::cout << "It has " << std::dec << bytesOfData << " bytes of data!" << std::endl;
|
||||
offset += sizeof(nativeSectionHeader_t) + bytesOfData;
|
||||
size_t bytesOfData = 0;
|
||||
switch (sectionHeader->ID) {
|
||||
case STRUCT:
|
||||
bytesOfData = sectionHeader->size;
|
||||
sec->data = new uint8_t[bytesOfData];
|
||||
memcpy(sec->data, data + offset + sizeof(nativeSectionHeader_t),
|
||||
bytesOfData);
|
||||
break;
|
||||
}
|
||||
// std::cout << "It has " << std::dec << bytesOfData << " bytes of
|
||||
// data!" << std::endl;
|
||||
offset += sizeof(nativeSectionHeader_t) + bytesOfData;
|
||||
|
||||
// std::cout << std::endl;
|
||||
// std::cout << std::endl;
|
||||
|
||||
prevHeader = sec;
|
||||
}
|
||||
prevHeader = sec;
|
||||
}
|
||||
|
||||
delete[] data;
|
||||
delete[] data;
|
||||
|
||||
return BS;
|
||||
return BS;
|
||||
}
|
||||
|
||||
std::string BinaryStream::sectionIdString(uint32_t id)
|
||||
{
|
||||
switch (id) {
|
||||
case STRUCT: return "STRUCT";
|
||||
case EXTENSION: return "EXTENSION";
|
||||
case TEXTURE_NATIVE: return "TEXTURE_NATIVE";
|
||||
case TEXTURE_DICTIONARY: return "TEXTURE_DICTIONARY";
|
||||
default: return "UNKNOWN";
|
||||
}
|
||||
std::string BinaryStream::sectionIdString(uint32_t id) {
|
||||
switch (id) {
|
||||
case STRUCT:
|
||||
return "STRUCT";
|
||||
case EXTENSION:
|
||||
return "EXTENSION";
|
||||
case TEXTURE_NATIVE:
|
||||
return "TEXTURE_NATIVE";
|
||||
case TEXTURE_DICTIONARY:
|
||||
return "TEXTURE_DICTIONARY";
|
||||
default:
|
||||
return "UNKNOWN";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -2,43 +2,40 @@
|
||||
#ifndef _BINARYSTREAM_HPP_
|
||||
#define _BINARYSTREAM_HPP_
|
||||
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
namespace RW
|
||||
{
|
||||
namespace RW {
|
||||
|
||||
class BinaryStream
|
||||
{
|
||||
class BinaryStream {
|
||||
private:
|
||||
struct nativeSectionHeader_t
|
||||
{
|
||||
uint32_t ID;
|
||||
uint32_t size;
|
||||
uint32_t version;
|
||||
};
|
||||
struct nativeSectionHeader_t {
|
||||
uint32_t ID;
|
||||
uint32_t size;
|
||||
uint32_t version;
|
||||
};
|
||||
|
||||
public:
|
||||
enum {
|
||||
STRUCT = 0x0001,
|
||||
EXTENSION = 0x0003,
|
||||
TEXTURE_NATIVE = 0x0015,
|
||||
TEXTURE_DICTIONARY = 0x0016,
|
||||
};
|
||||
enum {
|
||||
STRUCT = 0x0001,
|
||||
EXTENSION = 0x0003,
|
||||
TEXTURE_NATIVE = 0x0015,
|
||||
TEXTURE_DICTIONARY = 0x0016,
|
||||
};
|
||||
|
||||
struct sectionHeader_t {
|
||||
uint32_t ID;
|
||||
uint32_t size;
|
||||
uint32_t version;
|
||||
struct sectionHeader_t {
|
||||
uint32_t ID;
|
||||
uint32_t size;
|
||||
uint32_t version;
|
||||
|
||||
uint8_t *data = nullptr;
|
||||
sectionHeader_t *next = nullptr;
|
||||
} *rootHeader;
|
||||
uint8_t *data = nullptr;
|
||||
sectionHeader_t *next = nullptr;
|
||||
} * rootHeader;
|
||||
|
||||
static std::unique_ptr<BinaryStream> parse(const std::string& filename);
|
||||
static std::unique_ptr<BinaryStream> parse(const std::string &filename);
|
||||
|
||||
static std::string sectionIdString(uint32_t id);
|
||||
static std::string sectionIdString(uint32_t id);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
@ -1,50 +1,52 @@
|
||||
#include <TextureArchive.hpp>
|
||||
|
||||
#include <cassert>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <cassert>
|
||||
|
||||
namespace RW
|
||||
{
|
||||
namespace RW {
|
||||
|
||||
std::unique_ptr<TextureArchive> TextureArchive::create(BinaryStream &binaryStream)
|
||||
{
|
||||
auto textureArchive = std::unique_ptr<TextureArchive>(new TextureArchive);
|
||||
std::unique_ptr<TextureArchive> TextureArchive::create(
|
||||
BinaryStream &binaryStream) {
|
||||
auto textureArchive = std::unique_ptr<TextureArchive>(new TextureArchive);
|
||||
|
||||
auto section = binaryStream.rootHeader;
|
||||
auto section = binaryStream.rootHeader;
|
||||
|
||||
assert(
|
||||
section->ID == BinaryStream::TEXTURE_DICTIONARY
|
||||
&& "BinaryStream passed to this function must be a TEXTURE DICTIONARY"
|
||||
);
|
||||
assert(section->ID == BinaryStream::TEXTURE_DICTIONARY &&
|
||||
"BinaryStream passed to this function must be a TEXTURE DICTIONARY");
|
||||
|
||||
// Struct
|
||||
section = section->next;
|
||||
textureArchive->numTextures = ((uint16_t *) section->data)[0];
|
||||
// Struct
|
||||
section = section->next;
|
||||
textureArchive->numTextures = ((uint16_t *)section->data)[0];
|
||||
|
||||
for (size_t i = 0; i < textureArchive->numTextures; i++) {
|
||||
section = section->next; // Texture Native
|
||||
section = section->next; // Struct
|
||||
for (size_t i = 0; i < textureArchive->numTextures; i++) {
|
||||
section = section->next; // Texture Native
|
||||
section = section->next; // Struct
|
||||
|
||||
Texture texture = *reinterpret_cast<Texture *>(section->data);
|
||||
Texture texture = *reinterpret_cast<Texture *>(section->data);
|
||||
|
||||
if (texture.header.rasterFormat & 0x2000) {
|
||||
memcpy(texture.body.palette, section->data + sizeof(TextureHeader) - 4, 1024);
|
||||
|
||||
texture.body.pixels = new uint8_t[texture.header.width * texture.header.height];
|
||||
memcpy(texture.body.pixels, section->data + sizeof(TextureHeader) + 1024, texture.header.width * texture.header.height);
|
||||
} else {
|
||||
size_t bufSize = texture.header.width * texture.header.height * texture.header.bpp/8;
|
||||
texture.body.pixels = new uint8_t[bufSize];
|
||||
memcpy(texture.body.pixels, section->data + sizeof(TextureHeader), bufSize);
|
||||
}
|
||||
if (texture.header.rasterFormat & 0x2000) {
|
||||
memcpy(texture.body.palette,
|
||||
section->data + sizeof(TextureHeader) - 4, 1024);
|
||||
|
||||
textureArchive->textures.push_back(texture);
|
||||
texture.body.pixels =
|
||||
new uint8_t[texture.header.width * texture.header.height];
|
||||
memcpy(texture.body.pixels,
|
||||
section->data + sizeof(TextureHeader) + 1024,
|
||||
texture.header.width * texture.header.height);
|
||||
} else {
|
||||
size_t bufSize = texture.header.width * texture.header.height *
|
||||
texture.header.bpp / 8;
|
||||
texture.body.pixels = new uint8_t[bufSize];
|
||||
memcpy(texture.body.pixels, section->data + sizeof(TextureHeader),
|
||||
bufSize);
|
||||
}
|
||||
|
||||
section = section->next; // Extension
|
||||
}
|
||||
textureArchive->textures.push_back(texture);
|
||||
|
||||
return textureArchive;
|
||||
section = section->next; // Extension
|
||||
}
|
||||
|
||||
return textureArchive;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -6,45 +6,41 @@
|
||||
|
||||
#include <vector>
|
||||
|
||||
namespace RW
|
||||
{
|
||||
namespace RW {
|
||||
|
||||
class TextureArchive
|
||||
{
|
||||
class TextureArchive {
|
||||
public:
|
||||
struct TextureHeader {
|
||||
uint32_t platformID;
|
||||
uint16_t filterFlags;
|
||||
uint8_t textureWrapV;
|
||||
uint8_t textureWrapU;
|
||||
uint8_t diffuseName[32];
|
||||
uint8_t alphaName[32];
|
||||
uint32_t rasterFormat;
|
||||
uint32_t alpha;
|
||||
uint16_t width;
|
||||
uint16_t height;
|
||||
uint8_t bpp;
|
||||
uint8_t numMipmap;
|
||||
uint8_t rasterType;
|
||||
uint8_t compression;
|
||||
uint32_t dataSize;
|
||||
};
|
||||
struct TextureBody {
|
||||
uint8_t palette[1024];
|
||||
uint8_t *pixels;
|
||||
};
|
||||
struct Texture {
|
||||
TextureHeader header;
|
||||
TextureBody body;
|
||||
};
|
||||
struct TextureHeader {
|
||||
uint32_t platformID;
|
||||
uint16_t filterFlags;
|
||||
uint8_t textureWrapV;
|
||||
uint8_t textureWrapU;
|
||||
uint8_t diffuseName[32];
|
||||
uint8_t alphaName[32];
|
||||
uint32_t rasterFormat;
|
||||
uint32_t alpha;
|
||||
uint16_t width;
|
||||
uint16_t height;
|
||||
uint8_t bpp;
|
||||
uint8_t numMipmap;
|
||||
uint8_t rasterType;
|
||||
uint8_t compression;
|
||||
uint32_t dataSize;
|
||||
};
|
||||
struct TextureBody {
|
||||
uint8_t palette[1024];
|
||||
uint8_t *pixels;
|
||||
};
|
||||
struct Texture {
|
||||
TextureHeader header;
|
||||
TextureBody body;
|
||||
};
|
||||
|
||||
size_t numTextures;
|
||||
std::vector<Texture> textures;
|
||||
|
||||
size_t numTextures;
|
||||
std::vector<Texture> textures;
|
||||
|
||||
static std::unique_ptr<TextureArchive> create(BinaryStream &binaryStream);
|
||||
static std::unique_ptr<TextureArchive> create(BinaryStream &binaryStream);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user