mirror of https://git.sr.ht/~kikoodx/kble2lua
112 lines
2.9 KiB
C
112 lines
2.9 KiB
C
/* The MIT License
|
|
*
|
|
* Copyright (c) 2021 KikooDX
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
* a copy of this software and associated documentation files (the
|
|
* "Software"), to deal in the Software without restriction, including
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
* the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be
|
|
* included in all copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
* SOFTWARE. */
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
static const int kble_fmt_version = 0;
|
|
|
|
static int read_byte(FILE *file);
|
|
static int read_byte_group(FILE *file, int number);
|
|
|
|
int main(int argc, char **argv) {
|
|
FILE *file = NULL;
|
|
int byte = 0;
|
|
int width = 0;
|
|
int height = 0;
|
|
int cell_size = 0;
|
|
int i = 0;
|
|
|
|
if (argc != 2) {
|
|
fprintf(stderr, "ERROR: Wrong argument count. "
|
|
"Expected 1, got %d.\n", argc - 1);
|
|
return EXIT_FAILURE;
|
|
}
|
|
|
|
file = fopen(argv[1], "rb");
|
|
if (file == NULL) {
|
|
fprintf(stderr, "ERROR: Cannot open input file.\n");
|
|
return EXIT_FAILURE;
|
|
}
|
|
|
|
/* Check KBLE format ID. */
|
|
byte = read_byte(file);
|
|
if (byte != kble_fmt_version) {
|
|
fprintf(stderr, "ERROR: KBLE format ID doesn't match. "
|
|
"Expected %d, got %d.\n", kble_fmt_version, byte);
|
|
return EXIT_FAILURE;
|
|
}
|
|
|
|
/* Get cell size (in bytes). */
|
|
cell_size = read_byte(file);
|
|
|
|
/* Get width and height of level. */
|
|
width = read_byte_group(file, 2);
|
|
height = read_byte_group(file, 2);
|
|
|
|
/* Output Lua code. */
|
|
printf(
|
|
"return {\n"
|
|
" width = %d,\n"
|
|
" height = %d,\n"
|
|
" data = {\n ", width, height);
|
|
for (i = 0; i < width * height; i++) {
|
|
byte = read_byte_group(file, cell_size);
|
|
printf("%d", byte);
|
|
printf(", ");
|
|
if (i % width == width - 1 && i != width * height - 1)
|
|
printf("\n ");
|
|
}
|
|
printf("\n"
|
|
" }\n"
|
|
"}\n");
|
|
/* End output Lua code. */
|
|
|
|
fclose(file);
|
|
|
|
return EXIT_SUCCESS;
|
|
}
|
|
|
|
static int read_byte(FILE *file) {
|
|
const int byte = getc(file);
|
|
if (byte == EOF) {
|
|
fprintf(stderr, "ERROR: Unexpected EOF.\n");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
return byte;
|
|
}
|
|
|
|
static int read_byte_group(FILE *file, int number) {
|
|
int byte = 0;
|
|
int merged = 0;
|
|
int i = 0;
|
|
int shift = number * 8;
|
|
for (i = 0; i < number; i += 1) {
|
|
shift -= 8;
|
|
byte = read_byte(file);
|
|
merged |= byte << shift;
|
|
}
|
|
return merged;
|
|
}
|