update tools

This commit is contained in:
jiangzhengwenjz 2020-05-01 02:04:33 +08:00
parent 19bee53d96
commit 06e360fada
13 changed files with 89 additions and 23 deletions

View File

@ -171,7 +171,7 @@ endif
$(C_BUILDDIR)/%.o : $(C_SUBDIR)/%.c $$(c_dep)
@$(CPP) $(CPPFLAGS) $< -o $(C_BUILDDIR)/$*.i
$(PREPROC) $(C_BUILDDIR)/$*.i | $(CC1) $(CFLAGS) -o $(C_BUILDDIR)/$*.s
@$(PREPROC) $(C_BUILDDIR)/$*.i | $(CC1) $(CFLAGS) -o $(C_BUILDDIR)/$*.s
@echo -e "\t.text\n\t.align\t2, 0 @ Don't pad with nop\n" >> $(C_BUILDDIR)/$*.s
$(AS) $(ASFLAGS) -o $@ $(C_BUILDDIR)/$*.s

View File

@ -1,6 +1,6 @@
CC = gcc
CFLAGS = -Wall -Wextra -Wno-switch -Werror -std=c11 -O2
CFLAGS = -Wall -Wextra -Wno-switch -std=gnu11 -O3
LIBS = -lm

View File

@ -1,6 +1,6 @@
CC = gcc
CFLAGS = -Wall -Wextra -Werror -std=c11 -O2
CFLAGS = -Wall -Wextra -std=gnu11 -O3
.PHONY: all clean

View File

@ -1,4 +1,5 @@
CC = gcc
.PHONY: all clean
SRCS = gbafix.c

View File

@ -175,27 +175,31 @@ int main(int argc, char *argv[])
}
uint32_t sh_offset = 0;
size_t s;
// read file
infile = fopen(argfile, "r+b");
if (!infile) { fprintf(stderr, "Error opening input file!\n"); return -1; }
fseek(infile, sh_offset, SEEK_SET);
fread(&header, sizeof(header), 1, infile);
s = fread(&header, sizeof(header), 1, infile);
if (s != 1) { fprintf(stderr, "Error reading elf header!\n"); return 1; }
// elf check
Elf32_Shdr secHeader;
if (memcmp(&header, ELFMAG, 4) == 0) {
Elf32_Ehdr *elfHeader = (Elf32_Ehdr *)&header;
fseek(infile, elfHeader->e_shoff, SEEK_SET);
int i;
for (i = 0; i < elfHeader->e_shnum; i++) {
fread(&secHeader, sizeof(Elf32_Shdr), 1, infile);
s = fread(&secHeader, sizeof(Elf32_Shdr), 1, infile);
if (s != 1) { fprintf(stderr, "Error reading section header!\n"); return 1; }
if (secHeader.sh_type == SHT_PROGBITS && secHeader.sh_addr == elfHeader->e_entry) break;
}
if (i == elfHeader->e_shnum) { fprintf(stderr, "Error finding entry point!\n"); return 1; }
fseek(infile, secHeader.sh_offset, SEEK_SET);
sh_offset = secHeader.sh_offset;
fread(&header, sizeof(header), 1, infile);
s = fread(&header, sizeof(header), 1, infile);
if (s != 1) { fprintf(stderr, "Error reading elf header!\n"); return 1; }
}
// fix some data

View File

@ -1,6 +1,6 @@
CC = gcc
CFLAGS = -Wall -Wextra -Werror -Wno-sign-compare -std=c11 -O2 -DPNG_SKIP_SETJMP_CHECK
CFLAGS = -Wall -Wextra -Wno-sign-compare -std=gnu11 -O3 -DPNG_SKIP_SETJMP_CHECK
LIBS = -lpng -lz

View File

@ -27,7 +27,17 @@ void ConvertGbaToPng(char *inputPath, char *outputPath, struct GbaToPngOptions *
if (options->paletteFilePath != NULL)
{
ReadGbaPalette(options->paletteFilePath, &image.palette);
char *paletteFileExtension = GetFileExtensionAfterDot(options->paletteFilePath);
if (strcmp(paletteFileExtension, "gbapal") == 0)
{
ReadGbaPalette(options->paletteFilePath, &image.palette);
}
else
{
ReadJascPalette(options->paletteFilePath, &image.palette);
}
image.hasPalette = true;
}
else
@ -59,7 +69,7 @@ void ConvertPngToGba(char *inputPath, char *outputPath, struct PngToGbaOptions *
void HandleGbaToPngCommand(char *inputPath, char *outputPath, int argc, char **argv)
{
char *inputFileExtension = GetFileExtension(inputPath);
char *inputFileExtension = GetFileExtensionAfterDot(inputPath);
struct GbaToPngOptions options;
options.paletteFilePath = NULL;
options.bitDepth = inputFileExtension[0] - '0';
@ -138,7 +148,7 @@ void HandleGbaToPngCommand(char *inputPath, char *outputPath, int argc, char **a
void HandlePngToGbaCommand(char *inputPath, char *outputPath, int argc, char **argv)
{
char *outputFileExtension = GetFileExtension(outputPath);
char *outputFileExtension = GetFileExtensionAfterDot(outputPath);
int bitDepth = outputFileExtension[0] - '0';
struct PngToGbaOptions options;
options.numTiles = 0;
@ -198,9 +208,17 @@ void HandlePngToGbaCommand(char *inputPath, char *outputPath, int argc, char **a
ConvertPngToGba(inputPath, outputPath, &options);
}
void HandlePngToJascPaletteCommand(char *inputPath, char *outputPath, int argc UNUSED, char **argv UNUSED)
{
struct Palette palette = {};
ReadPngPalette(inputPath, &palette);
WriteJascPalette(outputPath, &palette);
}
void HandlePngToGbaPaletteCommand(char *inputPath, char *outputPath, int argc UNUSED, char **argv UNUSED)
{
struct Palette palette;
struct Palette palette = {};
ReadPngPalette(inputPath, &palette);
WriteGbaPalette(outputPath, &palette);
@ -208,7 +226,7 @@ void HandlePngToGbaPaletteCommand(char *inputPath, char *outputPath, int argc UN
void HandleGbaToJascPaletteCommand(char *inputPath, char *outputPath, int argc UNUSED, char **argv UNUSED)
{
struct Palette palette;
struct Palette palette = {};
ReadGbaPalette(inputPath, &palette);
WriteJascPalette(outputPath, &palette);
@ -241,7 +259,7 @@ void HandleJascToGbaPaletteCommand(char *inputPath, char *outputPath, int argc,
}
}
struct Palette palette;
struct Palette palette = {};
ReadJascPalette(inputPath, &palette);
@ -483,6 +501,8 @@ void HandleHuffDecompressCommand(char *inputPath, char *outputPath, int argc UNU
int main(int argc, char **argv)
{
char converted = 0;
if (argc < 3)
FATAL_ERROR("Usage: gbagfx INPUT_PATH OUTPUT_PATH [options...]\n");
@ -495,6 +515,7 @@ int main(int argc, char **argv)
{ "png", "4bpp", HandlePngToGbaCommand },
{ "png", "8bpp", HandlePngToGbaCommand },
{ "png", "gbapal", HandlePngToGbaPaletteCommand },
{ "png", "pal", HandlePngToJascPaletteCommand },
{ "gbapal", "pal", HandleGbaToJascPaletteCommand },
{ "pal", "gbapal", HandleJascToGbaPaletteCommand },
{ "latfont", "png", HandleLatinFontToPngCommand },
@ -514,14 +535,39 @@ int main(int argc, char **argv)
char *inputPath = argv[1];
char *outputPath = argv[2];
char *inputFileExtension = GetFileExtension(inputPath);
char *outputFileExtension = GetFileExtension(outputPath);
char *inputFileExtension = GetFileExtensionAfterDot(inputPath);
char *outputFileExtension = GetFileExtensionAfterDot(outputPath);
if (inputFileExtension == NULL)
FATAL_ERROR("Input file \"%s\" has no extension.\n", inputPath);
if (outputFileExtension == NULL)
FATAL_ERROR("Output file \"%s\" has no extension.\n", outputPath);
{
outputFileExtension = GetFileExtension(outputPath);
if (*outputFileExtension == '.')
outputFileExtension++;
if (*outputFileExtension == 0)
FATAL_ERROR("Output file \"%s\" has no extension.\n", outputPath);
size_t newOutputPathSize = strlen(inputPath) - strlen(inputFileExtension) + strlen(outputFileExtension);
outputPath = malloc(newOutputPathSize);
if (outputPath == NULL)
FATAL_ERROR("Failed to allocate memory for new output path.\n");
for (int i = 0; i < newOutputPathSize; i++)
{
outputPath[i] = inputPath[i];
if (outputPath[i] == '.')
{
strcpy(&outputPath[i + 1], outputFileExtension);
break;
}
}
}
for (int i = 0; handlers[i].function != NULL; i++)
{
@ -529,9 +575,16 @@ int main(int argc, char **argv)
&& (handlers[i].outputFileExtension == NULL || strcmp(handlers[i].outputFileExtension, outputFileExtension) == 0))
{
handlers[i].function(inputPath, outputPath, argc, argv);
return 0;
converted = 1;
break;
}
}
FATAL_ERROR("Don't know how to convert \"%s\" to \"%s\".\n", inputPath, outputPath);
if (outputPath != argv[2])
free(outputPath);
if (!converted)
FATAL_ERROR("Don't know how to convert \"%s\" to \"%s\".\n", argv[1], argv[2]);
return 0;
}

View File

@ -47,6 +47,13 @@ char *GetFileExtension(char *path)
while (extension > path && *extension != '.')
extension--;
return extension;
}
char *GetFileExtensionAfterDot(char *path)
{
char *extension = GetFileExtension(path);
if (extension == path)
return NULL;

View File

@ -7,6 +7,7 @@
bool ParseNumber(char *s, char **end, int radix, int *intValue);
char *GetFileExtension(char *path);
char *GetFileExtensionAfterDot(char *path);
unsigned char *ReadWholeFile(char *path, int *size);
unsigned char *ReadWholeFileZeroPadded(char *path, int *size, int padAmount);
void WriteWholeFile(char *path, void *buffer, int bufferSize);

View File

@ -1,6 +1,6 @@
CXX := g++
CXXFLAGS := -std=c++11 -O2 -Wall -Wno-switch -Werror
CXXFLAGS := -std=c++11 -O3 -Wall -Wno-switch
SRCS := agb.cpp error.cpp main.cpp midi.cpp tables.cpp

View File

@ -1,6 +1,6 @@
CXX := g++
CXXFLAGS := -std=c++11 -O2 -Wall -Wno-switch -Werror
CXXFLAGS := -std=c++11 -O3 -Wall -Wno-switch
SRCS := asm_file.cpp c_file.cpp charmap.cpp preproc.cpp string_parser.cpp \
utf8.cpp

View File

@ -1,6 +1,6 @@
CC = gcc
CFLAGS = -Wall -Wextra -Werror -std=c11 -O2 -DPNG_SKIP_SETJMP_CHECK
CFLAGS = -Wall -Wextra -std=gnu11 -O3 -DPNG_SKIP_SETJMP_CHECK
LIBS = -lpng -lz

View File

@ -1,6 +1,6 @@
CXX = g++
CXXFLAGS = -Wall -Werror -std=c++11 -O2
CXXFLAGS = -Wall -std=c++11 -O3
SRCS = scaninc.cpp c_file.cpp asm_file.cpp source_file.cpp