mirror of https://git.sr.ht/~kikoodx/kble2lua
kble2lua first version.
This commit is contained in:
commit
217e41c168
|
@ -0,0 +1,22 @@
|
|||
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.
|
|
@ -0,0 +1,27 @@
|
|||
# kble2lua - see LICENSE file for copyright and license details.
|
||||
|
||||
SRC = kble2lua.c
|
||||
CC = cc
|
||||
CFLAGS = -Os -ansi -pedantic-errors -Wall -Wextra
|
||||
PREFIX ?= /usr/local
|
||||
MANPREFIX ?= /usr/local/man
|
||||
|
||||
all: kble2lua
|
||||
|
||||
kblegenlua: $(SRC)
|
||||
$(CC) $(CFLAGS) $(SRC) -o kble2lua
|
||||
|
||||
clean:
|
||||
rm -f kble2lua
|
||||
|
||||
install:
|
||||
cp ./kble2lua $(DESTDIR)$(PREFIX)/bin
|
||||
chmod 755 $(DESTDIR)$(PREFIX)/bin/kble2lua
|
||||
cp ./kble2lua.1 $(DESTDIR)$(MANPREFIX)/man1
|
||||
chmod 644 $(DESTDIR)$(MANPREFIX)/man1/kble2lua.1
|
||||
|
||||
uninstall:
|
||||
rm -f $(DESTDIR)$(PREFIX)/bin/kble2lua
|
||||
rm -f $(DESTDIR)$(MANPREFIX)/man1/kble2lua.1
|
||||
|
||||
.PHONY: clean install uninstall
|
|
@ -0,0 +1,28 @@
|
|||
.\" kble2lua
|
||||
.TH "KBLE2LUA" "1" "15 March 2021" "Usage manual"
|
||||
.SH NAME
|
||||
kble2lua
|
||||
.SH SYNOPSIS
|
||||
kble2lua SOURCE
|
||||
.SH DESCRIPTION
|
||||
kble2lua is a conversion tool from
|
||||
.BR kble (7)
|
||||
format to
|
||||
.BR lua (1)
|
||||
script.
|
||||
|
||||
Read SOURCE
|
||||
.BR kble (7)
|
||||
file and output
|
||||
.BR lua (1)
|
||||
code to standard output.
|
||||
|
||||
.SH COPYRIGHT
|
||||
Copyright © 2021 KikooDX: MIT License <https://mitsloan.mit.edu/licensing>.
|
||||
|
||||
This is free software: you are free to change and redistribute it.
|
||||
There is NO WARRANTY, to the extent permitted by law.
|
||||
|
||||
.SH SEE ALSO
|
||||
.BR kble (1),
|
||||
.BR kble (7)
|
|
@ -0,0 +1,111 @@
|
|||
/* 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;
|
||||
}
|
Loading…
Reference in New Issue