Skip to content

Commit

Permalink
Added acbunpack function
Browse files Browse the repository at this point in the history
  • Loading branch information
towa0131 committed Oct 19, 2018
1 parent f38bd51 commit 7db5198
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 2 deletions.
20 changes: 20 additions & 0 deletions acbunpack.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <stdio.h>
#include <stdint.h>
#include <string.h>

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

#include <windows.h>

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

#include "cgss_api.h"

using namespace cgss;

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

void CopyStream(IStream *src, IStream *dst);
71 changes: 71 additions & 0 deletions cgss.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,21 @@ extern "C"{
#include <iostream>

#include "php_cgss.hpp"
#include "acbunpack.hpp"
#include "cgss_api.h"
#include "CAcbFile.h"

static int le_cgss;

using namespace std;

zend_class_entry *cgss_exception;

string GetFileName(const string &s) {
const auto pos = s.rfind(".");
return s.substr(0, pos);
}

PHP_FUNCTION(hca2wav)
{

Expand Down Expand Up @@ -79,6 +86,69 @@ PHP_FUNCTION(hca2wav)
RETURN_TRUE;
}

PHP_FUNCTION(acbunpack)
{
zend_string *acbFile;

const char *filePath;

ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_STR(acbFile)
ZEND_PARSE_PARAMETERS_END();

filePath = reinterpret_cast<const char*>((unsigned char *) ZSTR_VAL(acbFile));

if (!cgssHelperFileExists(filePath)) {
zend_throw_exception_ex(cgss_exception, -1 TSRMLS_CC, "File %s does not exist or cannot be opened.", filePath);
RETURN_FALSE;
}

cgss::CFileStream fileStream(filePath, cgss::FileMode::OpenExisting, cgss::FileAccess::Read);
cgss::CAcbFile acb(&fileStream, filePath);

acb.Initialize();

const string extractDir = "_acb_" + GetFileName(filePath) + "/";
MakeDirectories(extractDir);

const auto &fileNames = acb.GetFileNames();

uint32_t i = 0;

for (const auto &fileName : fileNames) {
auto s = fileName;
auto isCueNonEmpty = !s.empty();

if (!isCueNonEmpty) {
s = CAcbFile::GetSymbolicFileNameFromCueId(i);
}

auto extractPath = extractDir + s;

IStream *stream;

if (isCueNonEmpty) {
stream = acb.OpenDataStream(s.c_str());
} else {
stream = acb.OpenDataStream(i);
}

if (stream) {
cgss::CFileStream fs(extractPath.c_str(), cgss::FileMode::Create, cgss::FileAccess::Write);
CopyStream(stream, &fs);
} else {
zend_throw_exception_ex(cgss_exception, -1 TSRMLS_CC, "Cue #%u (%s) cannot be retrieved.", i + 1, s.c_str());
RETURN_FALSE;
}

delete stream;

++i;
}

RETURN_TRUE;
}

PHP_MINIT_FUNCTION(cgss)
{
REGISTER_MAIN_LONG_CONSTANT("CGSS_HCA_KEY_1", 0xF27E3B22, CONST_PERSISTENT | CONST_CS);
Expand Down Expand Up @@ -114,6 +184,7 @@ PHP_MINFO_FUNCTION(cgss)

const zend_function_entry cgss_functions[] = {
PHP_FE(hca2wav, NULL)
PHP_FE(acbunpack, NULL)
PHP_FE_END
};

Expand Down
3 changes: 2 additions & 1 deletion config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ if test "$PHP_CGSS" != "no"; then
PHP_ADD_LIBRARY(cgss, , CGSS_SHARED_LIBADD)
PHP_ADD_LIBRARY(stdc++, , CGSS_SHARED_LIBADD)
PHP_ADD_INCLUDE([./libcgss/src/lib])
PHP_NEW_EXTENSION(cgss, cgss.cpp, $ext_shared)
PHP_ADD_INCLUDE([./libcgss/src/lib/ichinose])
PHP_NEW_EXTENSION(cgss, cgss.cpp libcgss/src/apps/acbunpack/acbunpack.cpp libcgss/src/lib/ichinose/*.cpp libcgss/src/lib/takamori/*.cpp libcgss/src/lib/takamori/*/*.cpp, $ext_shared)
fi
2 changes: 1 addition & 1 deletion libcgss

0 comments on commit 7db5198

Please sign in to comment.