housekeeping style format etc

This commit is contained in:
scawful
2022-06-09 18:28:50 -04:00
parent 6f95d72d81
commit 3a4b532979
5 changed files with 346 additions and 312 deletions

View File

@@ -30,7 +30,7 @@
#define X_ std::byte { #define X_ std::byte {
#define _X } #define _X }
#define MY_BUILD_HEADER(command, length) (command << 5) + ((length) - 1) #define MY_BUILD_HEADER(command, length) (command << 5) + ((length)-1)
namespace yaze { namespace yaze {
namespace Application { namespace Application {
@@ -41,18 +41,18 @@ char* ALTTPCompression::DecompressGfx(const char* c_data,
unsigned int max_length, unsigned int max_length,
unsigned int* uncompressed_data_size, unsigned int* uncompressed_data_size,
unsigned int* compressed_length) { unsigned int* compressed_length) {
char* toret = char* toret = std_nintendo_.Decompress(c_data, start, max_length,
std_nintendo_.Decompress(c_data, start, max_length, uncompressed_data_size, uncompressed_data_size,
compressed_length, D_NINTENDO_C_MODE2); compressed_length, D_NINTENDO_C_MODE2);
return toret; return toret;
} }
char* ALTTPCompression::DecompressOverworld( char* ALTTPCompression::DecompressOverworld(
const char* c_data, const unsigned int start, unsigned int max_length, const char* c_data, const unsigned int start, unsigned int max_length,
unsigned int* uncompressed_data_size, unsigned int* compressed_length) { unsigned int* uncompressed_data_size, unsigned int* compressed_length) {
char* toret = char* toret = std_nintendo_.Decompress(c_data, start, max_length,
std_nintendo_.Decompress(c_data, start, max_length, uncompressed_data_size, uncompressed_data_size,
compressed_length, D_NINTENDO_C_MODE1); compressed_length, D_NINTENDO_C_MODE1);
return toret; return toret;
} }
@@ -61,7 +61,7 @@ char* ALTTPCompression::CompressGfx(const char* u_data,
const unsigned int length, const unsigned int length,
unsigned int* compressed_size) { unsigned int* compressed_size) {
return std_nintendo_.Compress(u_data, start, length, compressed_size, return std_nintendo_.Compress(u_data, start, length, compressed_size,
D_NINTENDO_C_MODE2); D_NINTENDO_C_MODE2);
} }
char* ALTTPCompression::CompressOverworld(const char* u_data, char* ALTTPCompression::CompressOverworld(const char* u_data,
@@ -69,7 +69,7 @@ char* ALTTPCompression::CompressOverworld(const char* u_data,
const unsigned int length, const unsigned int length,
unsigned int* compressed_size) { unsigned int* compressed_size) {
return std_nintendo_.Compress(u_data, start, length, compressed_size, return std_nintendo_.Compress(u_data, start, length, compressed_size,
D_NINTENDO_C_MODE1); D_NINTENDO_C_MODE1);
} }
/* /*
@@ -79,152 +79,160 @@ char* ALTTPCompression::CompressOverworld(const char* u_data,
* Then you have a new header byte and so on, until you hit a header with the * Then you have a new header byte and so on, until you hit a header with the
* value FF * value FF
*/ */
char* StdNintendoCompression::Decompress(const char* c_data, const unsigned int start, char* StdNintendoCompression::Decompress(const char* c_data,
unsigned int max_length, const unsigned int start,
unsigned int* uncompressed_data_size, unsigned int max_length,
unsigned int* compressed_length, char mode) { unsigned int* uncompressed_data_size,
char* u_data; unsigned int* compressed_length,
unsigned char header; char mode) {
unsigned int c_data_pos; char* u_data;
unsigned int u_data_pos; unsigned char header;
unsigned int allocated_memory; unsigned int c_data_pos;
unsigned int max_offset; unsigned int u_data_pos;
unsigned int allocated_memory;
unsigned int max_offset;
max_offset = 0; max_offset = 0;
if (max_length != 0) if (max_length != 0) max_offset = start + max_length;
max_offset = start + max_length; header = c_data[start];
header = c_data[start]; u_data =
u_data = (char *) malloc(INITIAL_ALLOC_SIZE); // No way to know the final size, we will probably realloc if needed (char*)malloc(INITIAL_ALLOC_SIZE); // No way to know the final size, we
allocated_memory = INITIAL_ALLOC_SIZE; // will probably realloc if needed
u_data_pos = 0; allocated_memory = INITIAL_ALLOC_SIZE;
c_data_pos = start; u_data_pos = 0;
c_data_pos = start;
while (header != 0xFF)
{
unsigned int length;
char command;
command = header >> 5; // 3 hightest bits are the command while (header != 0xFF) {
length = (header & 0x1F); // The rest is the length unsigned int length;
char command;
// Extended header, to allow for bigger length value than 32 command = header >> 5; // 3 hightest bits are the command
if (command == 7) length = (header & 0x1F); // The rest is the length
{
// The command are the next 3 bits
command = (header >> 2 ) & 7;
// 2 bits in the original header are the hight bit for the new length
// the next byte is added to this length
length = ((int)((header & 3) << 8)) + (unsigned char) c_data[c_data_pos + 1]; // Extended header, to allow for bigger length value than 32
c_data_pos++; if (command == 7) {
} // The command are the next 3 bits
command = (header >> 2) & 7;
// 2 bits in the original header are the hight bit for the new length
// the next byte is added to this length
//length value starts at 0, 0 is 1 length =
length++; ((int)((header & 3) << 8)) + (unsigned char)c_data[c_data_pos + 1];
printf("%d[%d]", command, length); c_data_pos++;
printf("header %02X - Command : %d , length : %d\n", header, command, length);
if (c_data_pos >= max_offset && max_offset != 0)
{
decompression_error_ = "Compression string exceed the max_length specified";
goto error;
}
if (u_data_pos + length + 1 > allocated_memory) // Adjust allocated memory
{
printf("Memory get reallocated by %d was %d\n", INITIAL_ALLOC_SIZE, allocated_memory);
u_data = (char*) realloc(u_data, allocated_memory + INITIAL_ALLOC_SIZE);
if (u_data == NULL)
{
decompression_error_ = "Can't realloc memory";
return NULL;
}
allocated_memory += INITIAL_ALLOC_SIZE;
}
switch (command)
{
case D_CMD_COPY: { // No compression, data are copied as
if (max_offset != 0 && c_data_pos + 1 + length > max_offset)
{
//decompression_error_ = vasprintf("A copy command exceed the available data %d > %d (max_length specified)\n", c_data_pos + 1 + length, max_offset);
goto error;
}
memcpy(u_data + u_data_pos, c_data + c_data_pos + 1, length);
c_data_pos += length + 1;
break;
}
case D_CMD_BYTE_REPEAT: { // Copy the same byte length time
memset(u_data + u_data_pos, c_data[c_data_pos + 1], length);
c_data_pos += 2;
break;
}
case D_CMD_WORD_REPEAT: { // Next byte is A, the one after is B, copy the sequence AB length times
char a = c_data[c_data_pos + 1];
char b = c_data[c_data_pos + 2];
for (int i = 0; i < length; i = i + 2)
{
u_data[u_data_pos + i] = a;
if ((i + 1) < length)
u_data[u_data_pos + i + 1] = b;
}
c_data_pos += 3;
break;
}
case D_CMD_BYTE_INC: { // Next byte is copied and incremented length time
for (int i = 0; i < length; i++) {
u_data[u_data_pos + i] = c_data[c_data_pos + 1] + i;
}
c_data_pos += 2;
break;
}
case D_CMD_COPY_EXISTING: { // Next 2 bytes form an offset to pick data from the output
//printf("%02X,%02X\n", (unsigned char) c_data[c_data_pos + 1], (unsigned char) c_data[c_data_pos + 2]);
unsigned short offset;
if (mode == D_NINTENDO_C_MODE2)
offset = (unsigned char)(c_data[c_data_pos + 1]) | ((unsigned char) (c_data[c_data_pos + 2]) << 8);
if (mode == D_NINTENDO_C_MODE1)
offset = (unsigned char)(c_data[c_data_pos + 2]) | ((unsigned char) (c_data[c_data_pos + 1]) << 8);
if (offset > u_data_pos)
{
printf("Offset for command copy existing is larger than the current position (Offset : 0x%04X | Pos : 0x%06X\n", offset, u_data_pos);
goto error;
}
if (u_data_pos + length >= allocated_memory)
{
printf("Memory get reallocated by a copy, %d was %d\n", INITIAL_ALLOC_SIZE, allocated_memory);
u_data = (char*) realloc(u_data, allocated_memory + INITIAL_ALLOC_SIZE);
if (u_data == NULL)
{
decompression_error_ = "Can't realloc memory";
return NULL;
}
allocated_memory += INITIAL_ALLOC_SIZE;
}
memcpy(u_data + u_data_pos, u_data + offset, length);
c_data_pos += 3;
break;
}
default: {
decompression_error_ = "Invalid command in the header for decompression";
goto error;
}
}
u_data_pos += length;
//printf("%d|%d\n", c_data_pos, u_data_pos);
header = c_data[c_data_pos];
} }
*uncompressed_data_size = u_data_pos;
*compressed_length = c_data_pos + 1; // length value starts at 0, 0 is 1
//printf("\n"); length++;
return u_data; printf("%d[%d]", command, length);
// yay goto usage :) printf("header %02X - Command : %d , length : %d\n", header, command,
length);
if (c_data_pos >= max_offset && max_offset != 0) {
decompression_error_ =
"Compression string exceed the max_length specified";
goto error;
}
if (u_data_pos + length + 1 > allocated_memory) // Adjust allocated memory
{
printf("Memory get reallocated by %d was %d\n", INITIAL_ALLOC_SIZE,
allocated_memory);
u_data = (char*)realloc(u_data, allocated_memory + INITIAL_ALLOC_SIZE);
if (u_data == NULL) {
decompression_error_ = "Can't realloc memory";
return NULL;
}
allocated_memory += INITIAL_ALLOC_SIZE;
}
switch (command) {
case D_CMD_COPY: { // No compression, data are copied as
if (max_offset != 0 && c_data_pos + 1 + length > max_offset) {
// decompression_error_ = vasprintf("A copy command exceed the
// available data %d > %d (max_length specified)\n", c_data_pos + 1 +
// length, max_offset);
goto error;
}
memcpy(u_data + u_data_pos, c_data + c_data_pos + 1, length);
c_data_pos += length + 1;
break;
}
case D_CMD_BYTE_REPEAT: { // Copy the same byte length time
memset(u_data + u_data_pos, c_data[c_data_pos + 1], length);
c_data_pos += 2;
break;
}
case D_CMD_WORD_REPEAT: { // Next byte is A, the one after is B, copy the
// sequence AB length times
char a = c_data[c_data_pos + 1];
char b = c_data[c_data_pos + 2];
for (int i = 0; i < length; i = i + 2) {
u_data[u_data_pos + i] = a;
if ((i + 1) < length) u_data[u_data_pos + i + 1] = b;
}
c_data_pos += 3;
break;
}
case D_CMD_BYTE_INC: { // Next byte is copied and incremented length time
for (int i = 0; i < length; i++) {
u_data[u_data_pos + i] = c_data[c_data_pos + 1] + i;
}
c_data_pos += 2;
break;
}
case D_CMD_COPY_EXISTING: { // Next 2 bytes form an offset to pick data
// from the output
// printf("%02X,%02X\n", (unsigned char) c_data[c_data_pos + 1],
// (unsigned char) c_data[c_data_pos + 2]);
unsigned short offset;
if (mode == D_NINTENDO_C_MODE2)
offset = (unsigned char)(c_data[c_data_pos + 1]) |
((unsigned char)(c_data[c_data_pos + 2]) << 8);
if (mode == D_NINTENDO_C_MODE1)
offset = (unsigned char)(c_data[c_data_pos + 2]) |
((unsigned char)(c_data[c_data_pos + 1]) << 8);
if (offset > u_data_pos) {
printf(
"Offset for command copy existing is larger than the current "
"position (Offset : 0x%04X | Pos : 0x%06X\n",
offset, u_data_pos);
goto error;
}
if (u_data_pos + length >= allocated_memory) {
printf("Memory get reallocated by a copy, %d was %d\n",
INITIAL_ALLOC_SIZE, allocated_memory);
u_data =
(char*)realloc(u_data, allocated_memory + INITIAL_ALLOC_SIZE);
if (u_data == NULL) {
decompression_error_ = "Can't realloc memory";
return NULL;
}
allocated_memory += INITIAL_ALLOC_SIZE;
}
memcpy(u_data + u_data_pos, u_data + offset, length);
c_data_pos += 3;
break;
}
default: {
decompression_error_ =
"Invalid command in the header for decompression";
goto error;
}
}
u_data_pos += length;
// printf("%d|%d\n", c_data_pos, u_data_pos);
header = c_data[c_data_pos];
}
*uncompressed_data_size = u_data_pos;
*compressed_length = c_data_pos + 1;
// printf("\n");
return u_data;
// yay goto usage :)
error: error:
free(u_data); free(u_data);
return NULL; return NULL;
} }
void StdNintendoCompression::PrintComponent(CompressionComponent* piece) {
void StdNintendoCompression::PrintComponent(CompressionComponent * piece) {
printf("Command : %d\n", piece->command); printf("Command : %d\n", piece->command);
printf("length : %d\n", piece->length); printf("length : %d\n", piece->length);
printf("Argument length : %d\n", piece->argument_length); printf("Argument length : %d\n", piece->argument_length);
@@ -232,24 +240,25 @@ void StdNintendoCompression::PrintComponent(CompressionComponent * piece) {
auto str = piece->argument; auto str = piece->argument;
char* toret = new char[size * 3 + 1]; char* toret = new char[size * 3 + 1];
unsigned int i; unsigned int i;
for (i = 0; i < size; i++){ for (i = 0; i < size; i++) {
sprintf(toret + i * 3, "%02X ", (unsigned char) str[i]); sprintf(toret + i * 3, "%02X ", (unsigned char)str[i]);
} }
toret[size * 3] = 0; toret[size * 3] = 0;
printf("Argument :%s\n", toret); printf("Argument :%s\n", toret);
} }
StdNintendoCompression::CompressionComponent* StdNintendoCompression::CreateComponent(const char command, StdNintendoCompression::CompressionComponent*
const unsigned int length, StdNintendoCompression::CreateComponent(const char command,
const char* args, const unsigned int length,
const unsigned int argument_length) { const char* args,
const unsigned int argument_length) {
CompressionComponent* toret = CompressionComponent* toret =
(CompressionComponent*) malloc(sizeof(CompressionComponent)); (CompressionComponent*)malloc(sizeof(CompressionComponent));
toret->command = command; toret->command = command;
toret->length = length; toret->length = length;
if (args != NULL) { if (args != NULL) {
toret->argument = (char*) malloc(argument_length); toret->argument = (char*)malloc(argument_length);
memcpy(toret->argument, args, argument_length); memcpy(toret->argument, args, argument_length);
} else } else
toret->argument = NULL; toret->argument = NULL;
@@ -272,7 +281,8 @@ void StdNintendoCompression::DestroyChain(CompressionComponent* piece) {
} }
// Merge consecutive copy if possible // Merge consecutive copy if possible
StdNintendoCompression::CompressionComponent* StdNintendoCompression::merge_copy(CompressionComponent* start) { StdNintendoCompression::CompressionComponent*
StdNintendoCompression::merge_copy(CompressionComponent* start) {
CompressionComponent* piece = start; CompressionComponent* piece = start;
while (piece != NULL) { while (piece != NULL) {
@@ -281,7 +291,7 @@ StdNintendoCompression::CompressionComponent* StdNintendoCompression::merge_copy
if (piece->length + piece->next->length <= D_max_length) { if (piece->length + piece->next->length <= D_max_length) {
unsigned int previous_length = piece->length; unsigned int previous_length = piece->length;
piece->length = piece->length + piece->next->length; piece->length = piece->length + piece->next->length;
//piece->argument = realloc(piece->argument, piece->length); // piece->argument = realloc(piece->argument, piece->length);
piece->argument_length = piece->length; piece->argument_length = piece->length;
memcpy(piece->argument + previous_length, piece->next->argument, memcpy(piece->argument + previous_length, piece->next->argument,
piece->next->argument_length); piece->next->argument_length);
@@ -298,8 +308,8 @@ StdNintendoCompression::CompressionComponent* StdNintendoCompression::merge_copy
return start; return start;
} }
unsigned int StdNintendoCompression::create_compression_string(CompressionComponent* start, char* output, unsigned int StdNintendoCompression::create_compression_string(
char mode) { CompressionComponent* start, char* output, char mode) {
unsigned int pos = 0; unsigned int pos = 0;
CompressionComponent* piece = start; CompressionComponent* piece = start;
@@ -312,7 +322,7 @@ unsigned int StdNintendoCompression::create_compression_string(CompressionCompon
output[pos++] = (7 << 5) | ((unsigned char)piece->command << 2) | output[pos++] = (7 << 5) | ((unsigned char)piece->command << 2) |
(((piece->length - 1) & 0xFF00) >> 8); (((piece->length - 1) & 0xFF00) >> 8);
printf("Building extended header : cmd: %d, length: %d - %02X\n", printf("Building extended header : cmd: %d, length: %d - %02X\n",
piece->command, piece->length, (unsigned char)output[pos - 1]); piece->command, piece->length, (unsigned char)output[pos - 1]);
output[pos++] = (char)((piece->length - 1) & 0x00FF); output[pos++] = (char)((piece->length - 1) & 0x00FF);
} else { // We need to split the command } else { // We need to split the command
unsigned int length_left = piece->length - D_max_length; unsigned int length_left = piece->length - D_max_length;
@@ -320,29 +330,26 @@ unsigned int StdNintendoCompression::create_compression_string(CompressionCompon
CompressionComponent* new_piece = NULL; CompressionComponent* new_piece = NULL;
if (piece->command == D_CMD_BYTE_REPEAT || if (piece->command == D_CMD_BYTE_REPEAT ||
piece->command == D_CMD_WORD_REPEAT) { piece->command == D_CMD_WORD_REPEAT) {
new_piece = new_piece = CreateComponent(piece->command, length_left,
CreateComponent(piece->command, length_left, piece->argument, piece->argument_length);
piece->argument, piece->argument_length);
} }
if (piece->command == D_CMD_BYTE_INC) { if (piece->command == D_CMD_BYTE_INC) {
new_piece = new_piece = CreateComponent(piece->command, length_left,
CreateComponent(piece->command, length_left, piece->argument, piece->argument_length);
piece->argument, piece->argument_length);
new_piece->argument[0] = (char)(piece->argument[0] + D_max_length); new_piece->argument[0] = (char)(piece->argument[0] + D_max_length);
} }
if (piece->command == D_CMD_COPY) { if (piece->command == D_CMD_COPY) {
piece->argument_length = D_max_length; piece->argument_length = D_max_length;
new_piece = CreateComponent(piece->command, length_left, NULL, new_piece =
length_left); CreateComponent(piece->command, length_left, NULL, length_left);
memcpy(new_piece->argument, piece->argument + D_max_length, memcpy(new_piece->argument, piece->argument + D_max_length,
length_left); length_left);
} }
if (piece->command == D_CMD_COPY_EXISTING) { if (piece->command == D_CMD_COPY_EXISTING) {
piece->argument_length = D_max_length; piece->argument_length = D_max_length;
unsigned int offset = piece->argument[0] + (piece->argument[1] << 8); unsigned int offset = piece->argument[0] + (piece->argument[1] << 8);
new_piece = new_piece = CreateComponent(piece->command, length_left,
CreateComponent(piece->command, length_left, piece->argument, piece->argument_length);
piece->argument, piece->argument_length);
if (mode == D_NINTENDO_C_MODE2) { if (mode == D_NINTENDO_C_MODE2) {
new_piece->argument[0] = (offset + D_max_length) & 0xFF; new_piece->argument[0] = (offset + D_max_length) & 0xFF;
new_piece->argument[1] = (offset + D_max_length) >> 8; new_piece->argument[1] = (offset + D_max_length) >> 8;
@@ -382,7 +389,11 @@ unsigned int StdNintendoCompression::create_compression_string(CompressionCompon
// TODO TEST compressed data border for each cmd // TODO TEST compressed data border for each cmd
char* StdNintendoCompression::Compress(const char* u_data, const unsigned int start, const unsigned int length, unsigned int* compressed_size, char mode) { char* StdNintendoCompression::Compress(const char* u_data,
const unsigned int start,
const unsigned int length,
unsigned int* compressed_size,
char mode) {
// we will realloc later // we will realloc later
char* compressed_data = (char*)malloc( char* compressed_data = (char*)malloc(
length + length +
@@ -518,8 +529,8 @@ char* StdNintendoCompression::Compress(const char* u_data, const unsigned int st
memcpy(buffer, u_data + u_data_pos - bytes_since_last_compression, memcpy(buffer, u_data + u_data_pos - bytes_since_last_compression,
bytes_since_last_compression); bytes_since_last_compression);
CompressionComponent* new_comp_piece = CompressionComponent* new_comp_piece =
CreateComponent(D_CMD_COPY, bytes_since_last_compression, CreateComponent(D_CMD_COPY, bytes_since_last_compression, buffer,
buffer, bytes_since_last_compression); bytes_since_last_compression);
compressed_chain->next = new_comp_piece; compressed_chain->next = new_comp_piece;
compressed_chain = new_comp_piece; compressed_chain = new_comp_piece;
bytes_since_last_compression = 0; bytes_since_last_compression = 0;
@@ -539,8 +550,8 @@ char* StdNintendoCompression::Compress(const char* u_data, const unsigned int st
memcpy(copy_buff, u_data + u_data_pos - bytes_since_last_compression, memcpy(copy_buff, u_data + u_data_pos - bytes_since_last_compression,
bytes_since_last_compression); bytes_since_last_compression);
CompressionComponent* copy_chuck = CompressionComponent* copy_chuck =
CreateComponent(D_CMD_COPY, bytes_since_last_compression, CreateComponent(D_CMD_COPY, bytes_since_last_compression, copy_buff,
copy_buff, bytes_since_last_compression); bytes_since_last_compression);
compressed_chain->next = copy_chuck; compressed_chain->next = copy_chuck;
compressed_chain = copy_chuck; compressed_chain = copy_chuck;
} }

View File

@@ -1,8 +1,8 @@
#ifndef YAZE_APPLICATION_UTILS_COMPRESSION_H #ifndef YAZE_APPLICATION_UTILS_COMPRESSION_H
#define YAZE_APPLICATION_UTILS_COMPRESSION_H #define YAZE_APPLICATION_UTILS_COMPRESSION_H
#include <cstdlib>
#include <cstddef> #include <cstddef>
#include <cstdlib>
#include <string> #include <string>
#include <vector> #include <vector>
@@ -27,9 +27,9 @@ class StdNintendoCompression {
*/ */
char* Decompress(const char* c_data, const unsigned int start, char* Decompress(const char* c_data, const unsigned int start,
unsigned int max_length, unsigned int max_length,
unsigned int* uncompressed_data_size, unsigned int* uncompressed_data_size,
unsigned int* compressed_length, char mode); unsigned int* compressed_length, char mode);
/* /*
* This function compress u_data following the compression format used by * This function compress u_data following the compression format used by
@@ -42,15 +42,16 @@ class StdNintendoCompression {
*/ */
char* Compress(const char* u_data, const unsigned int start, char* Compress(const char* u_data, const unsigned int start,
const unsigned int length, const unsigned int length, unsigned int* compressed_size,
unsigned int* compressed_size, char mode); char mode);
private: private:
std::string compression_error_; std::string compression_error_;
std::string decompression_error_; std::string decompression_error_;
bool std_nintendo_compression_sanity_check = false; bool std_nintendo_compression_sanity_check = false;
struct CompressionComponent_; struct CompressionComponent_;
using CompressionComponent = CompressionComponent_; using CompressionComponent = CompressionComponent_;
struct CompressionComponent_ { struct CompressionComponent_ {
char command; char command;
unsigned int length; unsigned int length;
@@ -59,17 +60,16 @@ class StdNintendoCompression {
CompressionComponent* next; CompressionComponent* next;
}; };
void PrintComponent(CompressionComponent * piece); void PrintComponent(CompressionComponent* piece);
CompressionComponent* CreateComponent(const char command, CompressionComponent* CreateComponent(const char command,
const unsigned int length, const unsigned int length,
const char* args, const char* args,
const unsigned int argument_length); const unsigned int argument_length);
void DestroyComponent(CompressionComponent* piece); void DestroyComponent(CompressionComponent* piece);
void DestroyChain(CompressionComponent* piece); void DestroyChain(CompressionComponent* piece);
CompressionComponent* merge_copy(CompressionComponent* start); CompressionComponent* merge_copy(CompressionComponent* start);
unsigned int create_compression_string(CompressionComponent* start, char* output, unsigned int create_compression_string(CompressionComponent* start,
char mode); char* output, char mode);
}; };
class ALTTPCompression { class ALTTPCompression {
@@ -125,7 +125,6 @@ class ALTTPCompression {
std::string decompression_error_; std::string decompression_error_;
}; };
} // namespace Utils } // namespace Utils
} // namespace Application } // namespace Application
} // namespace yaze } // namespace yaze

View File

@@ -4,8 +4,7 @@ namespace yaze {
namespace Application { namespace Application {
namespace Utils { namespace Utils {
void ROM::LoadFromFile(const std::string & path) { void ROM::LoadFromFile(const std::string& path) {
std::cout << "filename: " << path << std::endl; std::cout << "filename: " << path << std::endl;
std::ifstream stream(path, std::ios::in | std::ios::binary); std::ifstream stream(path, std::ios::in | std::ios::binary);
@@ -14,21 +13,26 @@ void ROM::LoadFromFile(const std::string & path) {
return; return;
} }
std::vector<char> contents((std::istreambuf_iterator<char>(stream)), std::istreambuf_iterator<char>()); std::vector<char> contents((std::istreambuf_iterator<char>(stream)),
std::istreambuf_iterator<char>());
for(auto i: contents) { for (auto i : contents) {
int value = i; int value = i;
std::cout << "data: " << value << std::endl; std::cout << "data: " << value << std::endl;
} }
std::cout << "file size: " << contents.size() << std::endl; std::cout << "file size: " << contents.size() << std::endl;
unsigned int uncompressed_data_size = 0; unsigned int uncompressed_data_size = 0;
unsigned int compressed_length = 0; unsigned int compressed_length = 0;
auto gfx_decompressed_data = alttp_compressor_.DecompressGfx(contents.data(), 0, contents.size(), &uncompressed_data_size, &compressed_length); auto gfx_decompressed_data = alttp_compressor_.DecompressGfx(
auto overworld_decompressed = alttp_compressor_.DecompressOverworld(contents.data(), 0, contents.size(), &uncompressed_data_size, &compressed_length); contents.data(), 0, contents.size(), &uncompressed_data_size,
&compressed_length);
auto overworld_decompressed = alttp_compressor_.DecompressOverworld(
contents.data(), 0, contents.size(), &uncompressed_data_size,
&compressed_length);
} }
} } // namespace Utils
} } // namespace Application
} } // namespace yaze

View File

@@ -1,13 +1,14 @@
#ifndef YAZE_APPLICATION_UTILS_ROM_H #ifndef YAZE_APPLICATION_UTILS_ROM_H
#define YAZE_APPLICATION_UTILS_ROM_H #define YAZE_APPLICATION_UTILS_ROM_H
#include <bits/postypes.h>
#include <cstddef>
#include <fstream>
#include <iostream>
#include <memory>
#include <string> #include <string>
#include <vector> #include <vector>
#include <bits/postypes.h>
#include <memory>
#include <iostream>
#include <fstream>
#include <cstddef>
#include "Compression.h" #include "Compression.h"
#include "Core/Constants.h" #include "Core/Constants.h"
@@ -18,7 +19,7 @@ namespace Utils {
class ROM { class ROM {
public: public:
void LoadFromFile(const std::string & path); void LoadFromFile(const std::string& path);
private: private:
std::vector<char*> original_rom_; std::vector<char*> original_rom_;
@@ -27,8 +28,8 @@ class ROM {
ALTTPCompression alttp_compressor_; ALTTPCompression alttp_compressor_;
}; };
} } // namespace Utils
} } // namespace Application
} } // namespace yaze
#endif #endif

View File

@@ -49,21 +49,21 @@ void Editor::DrawYazeMenu() {
std::string filePath = ImGuiFileDialog::Instance()->GetCurrentPath(); std::string filePath = ImGuiFileDialog::Instance()->GetCurrentPath();
rom.LoadFromFile(filePathName); rom.LoadFromFile(filePathName);
} }
// close // close
ImGuiFileDialog::Instance()->Close(); ImGuiFileDialog::Instance()->Close();
} }
} }
void Editor::DrawFileMenu() const { void Editor::DrawFileMenu() const {
if (ImGui::MenuItem("Open", "Ctrl+O")) { if (ImGui::MenuItem("Open", "Ctrl+O")) {
// TODO: Add the ability to open ALTTP ROM // TODO: Add the ability to open ALTTP ROM
ImGuiFileDialog::Instance()->OpenDialog("ChooseFileDlgKey", "Open ROM", ".sfc,.smc", "."); ImGuiFileDialog::Instance()->OpenDialog("ChooseFileDlgKey", "Open ROM",
".sfc,.smc", ".");
} }
if (ImGui::BeginMenu("Open Recent")) { if (ImGui::BeginMenu("Open Recent")) {
ImGui::MenuItem("alttp.sfc"); ImGui::MenuItem("alttp.sfc");
// TODO: Display recently accessed files here // TODO: Display recently accessed files here
ImGui::EndMenu(); ImGui::EndMenu();
} }
if (ImGui::MenuItem("Save", "Ctrl+S")) { if (ImGui::MenuItem("Save", "Ctrl+S")) {
@@ -77,22 +77,20 @@ void Editor::DrawFileMenu() const {
// TODO: Make these options matter // TODO: Make these options matter
if (ImGui::BeginMenu("Options")) { if (ImGui::BeginMenu("Options")) {
static bool enabled = true; static bool enabled = true;
ImGui::MenuItem("Enabled", "", &enabled); ImGui::MenuItem("Enabled", "", &enabled);
ImGui::BeginChild("child", ImVec2(0, 60), true); ImGui::BeginChild("child", ImVec2(0, 60), true);
for (int i = 0; i < 10; i++) for (int i = 0; i < 10; i++) ImGui::Text("Scrolling Text %d", i);
ImGui::Text("Scrolling Text %d", i); ImGui::EndChild();
ImGui::EndChild(); static float f = 0.5f;
static float f = 0.5f; static int n = 0;
static int n = 0; ImGui::SliderFloat("Value", &f, 0.0f, 1.0f);
ImGui::SliderFloat("Value", &f, 0.0f, 1.0f); ImGui::InputFloat("Input", &f, 0.1f);
ImGui::InputFloat("Input", &f, 0.1f); ImGui::Combo("Combo", &n, "Yes\0No\0Maybe\0\0");
ImGui::Combo("Combo", &n, "Yes\0No\0Maybe\0\0"); ImGui::EndMenu();
ImGui::EndMenu();
} }
} }
void Editor::DrawEditMenu() const { void Editor::DrawEditMenu() const {
if (ImGui::MenuItem("Undo", "Ctrl+O")) { if (ImGui::MenuItem("Undo", "Ctrl+O")) {
// TODO: Implement this // TODO: Implement this
@@ -116,110 +114,131 @@ void Editor::DrawEditMenu() const {
} }
} }
// i would not recommend doing dungeon because each objects is drawn separately but you could start with OW // first step would be to decompress all graphics data from the game
// (in alttp that's easy they're all located in the same location all the same
// first step would be to decompress all graphics data from the game (in alttp that's easy they're all located in the same location all the same sheet size 128x32) // sheet size 128x32) have a code that convert PC address to SNES and vice-versa
// have a code that convert PC address to SNES and vice-versa
// 1) find the gfx pointers (you could use ZS constant file) // 1) find the gfx pointers (you could use ZS constant file)
// 2) decompress all the gfx with your lz2 decompressor // 2) decompress all the gfx with your lz2 decompressor
// 3) convert the 3bpp snes data into PC 4bpp (probably the hardest part) // 3) convert the 3bpp snes data into PC 4bpp (probably the hardest part)
// 4) get the tiles32 data // 4) get the tiles32 data
// 5) get the tiles16 data // 5) get the tiles16 data
// 6) get the map32 data (they must be decompressed as well with a lz2 variant not the same as gfx compression but pretty similar) // 6) get the map32 data (they must be decompressed as well with a lz2 variant
// 7) get the gfx data of the map yeah i forgot that one and load 4bpp in a pseudo vram and use that to render tiles on screen // not the same as gfx compression but pretty similar)
// 8) try to render the tiles on the bitmap in black & white to start // 7) get the gfx data of the map
// 9) get the palettes data and try to find how they're loaded in the game that's a big puzzle to solve // yeah i forgot that one and load 4bpp in a pseudo vram and use that to
// then 9 you'll have an overworld map viewer, in less than few hours if are able to understand the data quickly // render tiles on screen
void Editor::DrawOverworldEditor() // 8) try to render the tiles on the bitmap in black & white to start
{ // 9) get the palettes data and try to find how they're loaded in
if (ImGui::BeginTabItem("Overworld")) // the game that's a big puzzle to solve then 9 you'll have an overworld map
{ // viewer, in less than few hours if are able to understand the data quickly
static ImVector<ImVec2> points; void Editor::DrawOverworldEditor() {
static ImVec2 scrolling(0.0f, 0.0f); if (ImGui::BeginTabItem("Overworld")) {
static bool opt_enable_grid = true; static ImVector<ImVec2> points;
static bool opt_enable_context_menu = true; static ImVec2 scrolling(0.0f, 0.0f);
static bool adding_line = false; static bool opt_enable_grid = true;
static bool opt_enable_context_menu = true;
static bool adding_line = false;
ImGui::Checkbox("Enable grid", &opt_enable_grid); ImGui::Checkbox("Enable grid", &opt_enable_grid);
ImGui::Checkbox("Enable context menu", &opt_enable_context_menu); ImGui::Checkbox("Enable context menu", &opt_enable_context_menu);
ImGui::Text("Mouse Left: drag to add lines,\nMouse Right: drag to scroll, click for context menu."); ImGui::Text(
"Mouse Left: drag to add lines,\nMouse Right: drag to scroll, click "
"for context menu.");
ImVec2 canvas_p0 = ImGui::GetCursorScreenPos(); // ImDrawList API uses screen coordinates! ImVec2 canvas_p0 =
ImVec2 canvas_sz = ImGui::GetContentRegionAvail(); // Resize canvas to what's available ImGui::GetCursorScreenPos(); // ImDrawList API uses screen coordinates!
if (canvas_sz.x < 50.0f) canvas_sz.x = 50.0f; ImVec2 canvas_sz =
if (canvas_sz.y < 50.0f) canvas_sz.y = 50.0f; ImGui::GetContentRegionAvail(); // Resize canvas to what's available
ImVec2 canvas_p1 = ImVec2(canvas_p0.x + canvas_sz.x, canvas_p0.y + canvas_sz.y); if (canvas_sz.x < 50.0f) canvas_sz.x = 50.0f;
if (canvas_sz.y < 50.0f) canvas_sz.y = 50.0f;
ImVec2 canvas_p1 =
ImVec2(canvas_p0.x + canvas_sz.x, canvas_p0.y + canvas_sz.y);
// Draw border and background color // Draw border and background color
const ImGuiIO & io = ImGui::GetIO(); const ImGuiIO& io = ImGui::GetIO();
ImDrawList* draw_list = ImGui::GetWindowDrawList(); ImDrawList* draw_list = ImGui::GetWindowDrawList();
draw_list->AddRectFilled(canvas_p0, canvas_p1, IM_COL32(50, 50, 50, 255)); draw_list->AddRectFilled(canvas_p0, canvas_p1, IM_COL32(50, 50, 50, 255));
draw_list->AddRect(canvas_p0, canvas_p1, IM_COL32(255, 255, 255, 255)); draw_list->AddRect(canvas_p0, canvas_p1, IM_COL32(255, 255, 255, 255));
// This will catch our interactions // This will catch our interactions
ImGui::InvisibleButton("canvas", canvas_sz, ImGuiButtonFlags_MouseButtonLeft | ImGuiButtonFlags_MouseButtonRight); ImGui::InvisibleButton(
const bool is_hovered = ImGui::IsItemHovered(); // Hovered "canvas", canvas_sz,
const bool is_active = ImGui::IsItemActive(); // Held ImGuiButtonFlags_MouseButtonLeft | ImGuiButtonFlags_MouseButtonRight);
const ImVec2 origin(canvas_p0.x + scrolling.x, canvas_p0.y + scrolling.y); // Lock scrolled origin const bool is_hovered = ImGui::IsItemHovered(); // Hovered
const ImVec2 mouse_pos_in_canvas(io.MousePos.x - origin.x, io.MousePos.y - origin.y); const bool is_active = ImGui::IsItemActive(); // Held
const ImVec2 origin(canvas_p0.x + scrolling.x,
canvas_p0.y + scrolling.y); // Lock scrolled origin
const ImVec2 mouse_pos_in_canvas(io.MousePos.x - origin.x,
io.MousePos.y - origin.y);
// Add first and second point // Add first and second point
if (is_hovered && !adding_line && ImGui::IsMouseClicked(ImGuiMouseButton_Left)) if (is_hovered && !adding_line &&
{ ImGui::IsMouseClicked(ImGuiMouseButton_Left)) {
points.push_back(mouse_pos_in_canvas); points.push_back(mouse_pos_in_canvas);
points.push_back(mouse_pos_in_canvas); points.push_back(mouse_pos_in_canvas);
adding_line = true; adding_line = true;
}
if (adding_line) {
points.back() = mouse_pos_in_canvas;
if (!ImGui::IsMouseDown(ImGuiMouseButton_Left)) adding_line = false;
}
// Pan (we use a zero mouse threshold when there's no context menu)
// You may decide to make that threshold dynamic based on whether the mouse
// is hovering something etc.
const float mouse_threshold_for_pan =
opt_enable_context_menu ? -1.0f : 0.0f;
if (is_active && ImGui::IsMouseDragging(ImGuiMouseButton_Right,
mouse_threshold_for_pan)) {
scrolling.x += io.MouseDelta.x;
scrolling.y += io.MouseDelta.y;
}
// Context menu (under default mouse threshold)
ImVec2 drag_delta = ImGui::GetMouseDragDelta(ImGuiMouseButton_Right);
if (opt_enable_context_menu && drag_delta.x == 0.0f && drag_delta.y == 0.0f)
ImGui::OpenPopupOnItemClick("context", ImGuiPopupFlags_MouseButtonRight);
if (ImGui::BeginPopup("context")) {
if (adding_line) points.resize(points.size() - 2);
adding_line = false;
if (ImGui::MenuItem("Remove one", NULL, false, points.Size > 0)) {
points.resize(points.size() - 2);
} }
if (adding_line) if (ImGui::MenuItem("Remove all", NULL, false, points.Size > 0)) {
{ points.clear();
points.back() = mouse_pos_in_canvas;
if (!ImGui::IsMouseDown(ImGuiMouseButton_Left))
adding_line = false;
} }
ImGui::EndPopup();
}
// Pan (we use a zero mouse threshold when there's no context menu) // Draw grid + all lines in the canvas
// You may decide to make that threshold dynamic based on whether the mouse is hovering something etc. draw_list->PushClipRect(canvas_p0, canvas_p1, true);
const float mouse_threshold_for_pan = opt_enable_context_menu ? -1.0f : 0.0f; if (opt_enable_grid) {
if (is_active && ImGui::IsMouseDragging(ImGuiMouseButton_Right, mouse_threshold_for_pan)) const float GRID_STEP = 64.0f;
{ for (float x = fmodf(scrolling.x, GRID_STEP); x < canvas_sz.x;
scrolling.x += io.MouseDelta.x; x += GRID_STEP)
scrolling.y += io.MouseDelta.y; draw_list->AddLine(ImVec2(canvas_p0.x + x, canvas_p0.y),
} ImVec2(canvas_p0.x + x, canvas_p1.y),
IM_COL32(200, 200, 200, 40));
for (float y = fmodf(scrolling.y, GRID_STEP); y < canvas_sz.y;
y += GRID_STEP)
draw_list->AddLine(ImVec2(canvas_p0.x, canvas_p0.y + y),
ImVec2(canvas_p1.x, canvas_p0.y + y),
IM_COL32(200, 200, 200, 40));
}
// Context menu (under default mouse threshold) for (int n = 0; n < points.Size; n += 2)
ImVec2 drag_delta = ImGui::GetMouseDragDelta(ImGuiMouseButton_Right); draw_list->AddLine(
if (opt_enable_context_menu && drag_delta.x == 0.0f && drag_delta.y == 0.0f) ImVec2(origin.x + points[n].x, origin.y + points[n].y),
ImGui::OpenPopupOnItemClick("context", ImGuiPopupFlags_MouseButtonRight); ImVec2(origin.x + points[n + 1].x, origin.y + points[n + 1].y),
if (ImGui::BeginPopup("context")) IM_COL32(255, 255, 0, 255), 2.0f);
{
if (adding_line)
points.resize(points.size() - 2);
adding_line = false;
if (ImGui::MenuItem("Remove one", NULL, false, points.Size > 0)) { points.resize(points.size() - 2); }
if (ImGui::MenuItem("Remove all", NULL, false, points.Size > 0)) { points.clear(); }
ImGui::EndPopup();
}
// Draw grid + all lines in the canvas draw_list->PopClipRect();
draw_list->PushClipRect(canvas_p0, canvas_p1, true);
if (opt_enable_grid)
{
const float GRID_STEP = 64.0f;
for (float x = fmodf(scrolling.x, GRID_STEP); x < canvas_sz.x; x += GRID_STEP)
draw_list->AddLine(ImVec2(canvas_p0.x + x, canvas_p0.y), ImVec2(canvas_p0.x + x, canvas_p1.y), IM_COL32(200, 200, 200, 40));
for (float y = fmodf(scrolling.y, GRID_STEP); y < canvas_sz.y; y += GRID_STEP)
draw_list->AddLine(ImVec2(canvas_p0.x, canvas_p0.y + y), ImVec2(canvas_p1.x, canvas_p0.y + y), IM_COL32(200, 200, 200, 40));
}
for (int n = 0; n < points.Size; n += 2)
draw_list->AddLine(ImVec2(origin.x + points[n].x, origin.y + points[n].y), ImVec2(origin.x + points[n + 1].x, origin.y + points[n + 1].y), IM_COL32(255, 255, 0, 255), 2.0f);
draw_list->PopClipRect();
ImGui::EndTabItem(); ImGui::EndTabItem();
} }
} }
} // namespace View } // namespace View
} // namespace Application } // namespace Application
} // namespace yaze } // namespace yaze