Skip to content

Commit

Permalink
gust_g1t: add .gt1 support
Browse files Browse the repository at this point in the history
Closes #18
Also improve Unicode support for backup file creation
  • Loading branch information
VitaSmith committed Jul 30, 2020
1 parent 2e256b0 commit 81f5214
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 14 deletions.
8 changes: 4 additions & 4 deletions .vs/gust_g1t.vcxproj.user
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LocalDebuggerWorkingDirectory>$(SolutionDir)</LocalDebuggerWorkingDirectory>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
<LocalDebuggerCommandArguments>A11V_PLUS_11.g1t</LocalDebuggerCommandArguments>
<LocalDebuggerCommandArguments>00000000000002e4.gt1</LocalDebuggerCommandArguments>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LocalDebuggerWorkingDirectory>$(SolutionDir)</LocalDebuggerWorkingDirectory>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
<LocalDebuggerCommandArguments>A11V_PLUS_11.g1t</LocalDebuggerCommandArguments>
<LocalDebuggerCommandArguments>00000000000002e4.gt1</LocalDebuggerCommandArguments>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LocalDebuggerCommandArguments>A11V_PLUS_11.g1t</LocalDebuggerCommandArguments>
<LocalDebuggerCommandArguments>00000000000002e4.gt1</LocalDebuggerCommandArguments>
<LocalDebuggerWorkingDirectory>$(SolutionDir)</LocalDebuggerWorkingDirectory>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LocalDebuggerCommandArguments>A11V_PLUS_11.g1t</LocalDebuggerCommandArguments>
<LocalDebuggerCommandArguments>00000000000002e4.gt1</LocalDebuggerCommandArguments>
<LocalDebuggerWorkingDirectory>$(SolutionDir)</LocalDebuggerWorkingDirectory>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
</PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion gust_ebm.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ int main_utf8(int argc, char** argv)
JSON_Value* json = NULL;

if (argc != 2) {
printf("%s %s (c) 2019 VitaSmith\n\n"
printf("%s %s (c) 2019-2020 VitaSmith\n\n"
"Usage: %s <file>\n\n"
"Convert a .ebm file to or from an editable JSON file.\n\n",
appname(argv[0]), GUST_TOOLS_VERSION_STR, appname(argv[0]));
Expand Down
2 changes: 1 addition & 1 deletion gust_elixir.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ int main_utf8(int argc, char** argv)
bool list_only = (argc == 3) && (argv[1][0] == '-') && (argv[1][1] == 'l');

if ((argc != 2) && !list_only) {
printf("%s %s (c) 2019 VitaSmith\n\n"
printf("%s %s (c) 2019-2020 VitaSmith\n\n"
"Usage: %s [-l] <elixir[.gz]> file>\n\n"
"Extracts (file) or recreates (directory) a Gust .elixir archive.\n\n"
"Note: A backup (.bak) of the original is automatically created, when the target\n"
Expand Down
16 changes: 9 additions & 7 deletions gust_g1t.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#include "parson.h"
#include "dds.h"

#define G1TG_MAGIC 0x47315447 // "G1GT"
#define GT1G_MAGIC 0x47315447 // "G1TG"
#define G1T_TEX_EXTRA_FLAG 0x10000000
#define JSON_VERSION 1
#define json_object_get_uint32 (uint32_t)json_object_get_number
Expand Down Expand Up @@ -340,7 +340,7 @@ int main_utf8(int argc, char** argv)
goto out;
}
g1t_header hdr = { 0 };
hdr.magic = G1TG_MAGIC;
hdr.magic = GT1G_MAGIC;
hdr.version = getbe32(version);
hdr.total_size = 0; // To be rewritten when we're done
hdr.nb_textures = json_object_get_uint32(json_object(json), "nb_textures");
Expand Down Expand Up @@ -536,12 +536,14 @@ int main_utf8(int argc, char** argv)
r = 0;
} else {
printf("%s '%s'...\n", list_only ? "Listing" : "Extracting", basename(argv[argc - 1]));
char* g1t_pos = strstr(argv[argc - 1], ".g1t");
if (g1t_pos == NULL) {
fprintf(stderr, "ERROR: File should have a '.g1t' extension\n");
size_t len = strlen(argv[argc - 1]);
if ((len < 4) || (argv[argc - 1][len - 4] != '.') || (argv[argc - 1][len - 3] != 'g') ||
((argv[argc - 1][len - 2] != '1') && (argv[argc - 1][len - 2] != 't')) ||
((argv[argc - 1][len - 1] != '1') && (argv[argc - 1][len - 1] != 't')) ) {
fprintf(stderr, "ERROR: File should have a '.g1t' or 'gt1' extension\n");
goto out;
}

char* g1t_pos = &argv[argc - 1][len - 4];
file = fopen_utf8(argv[argc - 1], "rb");
if (file == NULL) {
fprintf(stderr, "ERROR: Can't open file '%s'", argv[argc - 1]);
Expand All @@ -552,7 +554,7 @@ int main_utf8(int argc, char** argv)
fprintf(stderr, "ERROR: Can't read from '%s'", argv[argc - 1]);
goto out;
}
if (magic != G1TG_MAGIC) {
if (magic != GT1G_MAGIC) {
fprintf(stderr, "ERROR: Not a G1T file (bad magic)");
goto out;
}
Expand Down
11 changes: 11 additions & 0 deletions utf8.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#endif

#include <stdio.h>
#include <wchar.h>
#include <sys/stat.h>
#include <sys/types.h>

Expand Down Expand Up @@ -89,6 +90,16 @@ static __inline FILE* fopen_utf8(const char* filename, const char* mode)
return r;
}

static __inline int rename_utf8(const char* oldname, const char* newname)
{
wchar_t* oldname16 = utf8_to_utf16(oldname);
wchar_t* newname16 = utf8_to_utf16(newname);
int r = _wrename(oldname16, newname16);
free(oldname16);
free(newname16);
return r;
}

static __inline int stat64_utf8(const char* path, struct stat64* buffer)
{
int r;
Expand Down
2 changes: 1 addition & 1 deletion util.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ void create_backup(const char* path)
strcpy(backup_path, path);
strcat(backup_path, ".bak");
if (stat64_utf8(backup_path, &st) != 0) {
if (rename(path, backup_path) == 0)
if (rename_utf8(path, backup_path) == 0)
printf("Saved backup as '%s'\n", backup_path);
else
fprintf(stderr, "WARNING: Could not create backup file '%s\n", backup_path);
Expand Down

0 comments on commit 81f5214

Please sign in to comment.