Skip to content

Commit

Permalink
Fixed 'use cue name' behavior and used smarter naming
Browse files Browse the repository at this point in the history
  • Loading branch information
hozuki committed Apr 16, 2020
1 parent 2925b81 commit 8ec2eee
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 139 deletions.
22 changes: 11 additions & 11 deletions src/apps/acb2hcas/acb2hcas.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include <string>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <cinttypes>

Expand All @@ -24,7 +23,7 @@ struct Acb2HcasOptions {

static void PrintHelp();

static int ParseArgs(int argc, const char *argv[], const char **input, Acb2HcasOptions &options);
static int ParseArgs(int argc, const char *argv[], string &inputFile, Acb2HcasOptions &options);

static int DoWork(const string &inputFile, const Acb2HcasOptions &options);

Expand All @@ -33,22 +32,22 @@ static int ProcessHca(AcbWalkCallbackParams *params);
static void WriteHcaKeyFile(const string &fileName, uint64_t key, uint16_t modifier = 0);

int main(int argc, const char *argv[]) {
if (argc < 2) {
PrintHelp();
return 0;
}

const char *inputFile;
string inputFile;
Acb2HcasOptions options = {0};

const auto parsed = ParseArgs(argc, argv, &inputFile, options);
const auto parsed = ParseArgs(argc, argv, inputFile, options);

if (parsed < 0) {
return 0;
} else if (parsed > 0) {
return parsed;
}

if (!cgssHelperFileExists(inputFile.c_str())) {
fprintf(stderr, "File '%s' does not exist or cannot be opened.\n", inputFile.c_str());
return -1;
}

return DoWork(inputFile, options);
}

Expand All @@ -61,13 +60,13 @@ static void PrintHelp() {
cout << "\t-n\tUse cue names for output waveforms" << endl;
}

static int ParseArgs(int argc, const char *argv[], const char **input, Acb2HcasOptions &options) {
static int ParseArgs(int argc, const char *argv[], string &inputFile, Acb2HcasOptions &options) {
if (argc < 2) {
PrintHelp();
return -1;
}

*input = argv[1];
inputFile = argv[1];

for (int i = 2; i < argc; ++i) {
if (argv[i][0] == '-' || argv[i][0] == '/') {
Expand All @@ -91,6 +90,7 @@ static int ParseArgs(int argc, const char *argv[], const char **input, Acb2HcasO
options.useCueName = TRUE;
break;
default:
fprintf(stderr, "Unknown option: %s\n", argv[i]);
return 2;
}
}
Expand Down
25 changes: 13 additions & 12 deletions src/apps/acb2wavs/acb2wavs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
#include "../common/common.h"
#include "../common/acbextract.h"

using namespace cgss;
using namespace std;
using namespace cgss;

struct Acb2WavsOptions {
HCA_DECODER_CONFIG decoderConfig;
Expand All @@ -18,7 +18,7 @@ struct Acb2WavsOptions {

static void PrintHelp();

static int ParseArgs(int argc, const char *argv[], const char **input, Acb2WavsOptions &options);
static int ParseArgs(int argc, const char *argv[], string &inputFile, Acb2WavsOptions &options);

static int DoWork(const string &inputFile, const Acb2WavsOptions &options);

Expand All @@ -27,22 +27,22 @@ static int ProcessHca(AcbWalkCallbackParams *params);
static int DecodeHca(IStream *hcaDataStream, IStream *waveStream, const HCA_DECODER_CONFIG &dc);

int main(int argc, const char *argv[]) {
if (argc < 2) {
PrintHelp();
return 0;
}

const char *inputFile;
string inputFile;
Acb2WavsOptions options = {0};

const auto parsed = ParseArgs(argc, argv, &inputFile, options);
const auto parsed = ParseArgs(argc, argv, inputFile, options);

if (parsed < 0) {
return 0;
} else if (parsed > 0) {
return parsed;
}

if (!cgssHelperFileExists(inputFile.c_str())) {
fprintf(stderr, "File '%s' does not exist or cannot be opened.\n", inputFile.c_str());
return -1;
}

return DoWork(inputFile, options);
}

Expand All @@ -52,13 +52,13 @@ static void PrintHelp() {
cout << "\t-n\tUse cue names for output waveforms" << endl;
}

static int ParseArgs(int argc, const char *argv[], const char **input, Acb2WavsOptions &options) {
static int ParseArgs(int argc, const char *argv[], string &inputFile, Acb2WavsOptions &options) {
if (argc < 2) {
PrintHelp();
return -1;
}

*input = argv[1];
inputFile = argv[1];

options.decoderConfig.waveHeaderEnabled = TRUE;
options.decoderConfig.decodeFunc = CDefaultWaveGenerator::Decode16BitS;
Expand All @@ -82,6 +82,7 @@ static int ParseArgs(int argc, const char *argv[], const char **input, Acb2WavsO
options.useCueName = TRUE;
break;
default:
fprintf(stderr, "Unknown option: %s\n", argv[i]);
return 2;
}
}
Expand Down Expand Up @@ -113,7 +114,7 @@ static int ProcessHca(AcbWalkCallbackParams *params) {
HCA_DECODER_CONFIG decoderConfig = params->walkOptions->decoderConfig;

const string extractFilePath = common_utils::ReplaceAnyExtension(params->extractPathHint, ".wav");
fprintf(stdout, "Decoding to %s...\n", extractFilePath.c_str());
fprintf(stdout, ": decoding to %s...\n", extractFilePath.c_str());

try {
CFileStream fs(extractFilePath.c_str(), FileMode::Create, FileAccess::Write);
Expand Down
148 changes: 50 additions & 98 deletions src/apps/acbunpack/acbunpack.cpp
Original file line number Diff line number Diff line change
@@ -1,139 +1,91 @@
#include <cstdio>
#include <cstdint>
#include <cstring>
#include <cinttypes>

#if defined(_WIN32) || defined(WIN32)

#define WIN32_LEAN_AND_MEAN

#include <windows.h>

#else
#include <sys/stat.h>
#include <unistd.h>
#endif
#include <string>

#include "../../lib/cgss_api.h"
#include "../common/common.h"
#include "../common/acbextract.h"

using namespace std;
using namespace cgss;

static void print_help();
struct AcbUnpackOptions {
bool_t useCueName;
};

static void MakeDirectories(const std::string &s);
static void PrintHelp();

static std::string GetDirectoryFromPath(const std::string &s);
static int ParseArgs(int argc, const char *argv[], string &inputFile, AcbUnpackOptions &options);

static std::string GetFileNameFromPath(const std::string &s);
static int DoWork(const string &inputFile, const AcbUnpackOptions &options);

static int ExtractFile(AcbWalkCallbackParams *params);

int main(int argc, const char *argv[]) {
if (argc == 1) {
print_help();
string inputFile;
AcbUnpackOptions options = {0};

const auto parsed = ParseArgs(argc, argv, inputFile, options);

if (parsed < 0) {
return 0;
} else if (parsed > 0) {
return parsed;
}

const char *filePath = argv[1];

if (!cgssHelperFileExists(filePath)) {
fprintf(stderr, "File '%s' does not exist or cannot be opened.\n", filePath);
if (!cgssHelperFileExists(inputFile.c_str())) {
fprintf(stderr, "File '%s' does not exist or cannot be opened.\n", inputFile.c_str());
return -1;
}

AcbWalkOptions options;
options.useCueName = true;
options.callback = ExtractFile;
return DoWork(inputFile, options);
}

const auto r = AcbWalk(filePath, &options);
static int DoWork(const string &inputFile, const AcbUnpackOptions &options) {
AcbWalkOptions o;
o.useCueName = options.useCueName;
o.callback = ExtractFile;

return r;
return AcbWalk(inputFile, &o);
}

static int ExtractFile(AcbWalkCallbackParams *params) {
fprintf(stdout, "Unpacking: %s\n", params->extractPathHint.c_str());

CFileStream fs(params->extractPathHint.c_str(), FileMode::Create, FileAccess::Write);
common_utils::CopyStream(params->entryDataStream, &fs);

return 0;
}

static void print_help() {
fprintf(stderr, "Usage:\n\n\tacbunpack <file>\n");
}

static std::string GetDirectoryFromPath(const std::string &s) {
const char sep1 = '/', sep2 = '\\';

size_t i1 = s.rfind(sep1, s.length());
size_t i2 = s.rfind(sep2, s.length());

if (i1 != std::string::npos) {
if (i2 != std::string::npos) {
auto i = i1 > i2 ? i1 : i2;
return (s.substr(0, i));
} else {
return (s.substr(0, i1));
}
} else {
if (i2 != std::string::npos) {
return (s.substr(0, i2));
} else {
return std::string("");
}
}
static void PrintHelp() {
static const char *helpMessage = "Usage:\n"
"\n"
"\tacbunpack <file> [-n]\n"
"\n"
"\t-n Use cue names for output waveforms\n";
fprintf(stderr, "%s", helpMessage);
}

static std::string GetFileNameFromPath(const std::string &s) {
const char sep1 = '/', sep2 = '\\';

size_t i1 = s.rfind(sep1, s.length());
size_t i2 = s.rfind(sep2, s.length());

if (i1 != std::string::npos) {
if (i2 != std::string::npos) {
auto i = i1 > i2 ? i1 : i2;
return (s.substr(i + 1));
} else {
return (s.substr(i1 + 1));
}
} else {
if (i2 != std::string::npos) {
return (s.substr(i2 + 1));
} else {
return std::string("");
}
static int ParseArgs(int argc, const char *argv[], string &inputFile, AcbUnpackOptions &options) {
if (argc < 2) {
PrintHelp();
return -1;
}
}

#if defined(WIN32) || defined(_WIN32)

static void MakeDirectories(const std::string &s) {
CreateDirectory(s.c_str(), nullptr);
}

#else

static void MakeDirectories(const std::string &s) {
char str[512] = {0};

strncpy(str, s.c_str(), 512);
auto len = strlen(str);

for (auto i = 0; i < len; i++) {
if (str[i] == '/') {
str[i] = '\0';
if (access(str, 0) != 0) {
mkdir(str, 0777);
inputFile = argv[1];

for (int i = 2; i < argc; ++i) {
if (argv[i][0] == '-' || argv[i][0] == '/') {
switch (argv[i][1]) {
case 'n':
options.useCueName = TRUE;
break;
default:
fprintf(stderr, "Unknown option: %s\n", argv[i]);
return 2;
}
str[i] = '/';
}
}

if (len > 0 && access(str, 0) != 0) {
mkdir(str, 0777);
}
return 0;
}

#endif
Loading

0 comments on commit 8ec2eee

Please sign in to comment.