From 217e41c168ba7c8e50db1696d90b5bb6da342c7d Mon Sep 17 00:00:00 2001 From: KikooDX Date: Mon, 15 Mar 2021 18:23:55 +0100 Subject: [PATCH] kble2lua first version. --- LICENSE | 22 +++++++++++ Makefile | 27 +++++++++++++ kble2lua.1 | 28 ++++++++++++++ kble2lua.c | 111 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 188 insertions(+) create mode 100644 LICENSE create mode 100644 Makefile create mode 100644 kble2lua.1 create mode 100644 kble2lua.c diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..0c9517d --- /dev/null +++ b/LICENSE @@ -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. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..fe87bae --- /dev/null +++ b/Makefile @@ -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 diff --git a/kble2lua.1 b/kble2lua.1 new file mode 100644 index 0000000..231b522 --- /dev/null +++ b/kble2lua.1 @@ -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 . + +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) diff --git a/kble2lua.c b/kble2lua.c new file mode 100644 index 0000000..6844990 --- /dev/null +++ b/kble2lua.c @@ -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 +#include + +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; +}