Skip to content

Commit

Permalink
NCSDECOMP: Add initial ncsdecomp tool
Browse files Browse the repository at this point in the history
  • Loading branch information
Nostritius authored and DrMcCoy committed Mar 24, 2019
1 parent 61c11b4 commit 5f2cef4
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ Makefile.in
/src/cbgt2tga
/src/cdpth2tga
/src/ncsdis
/src/ncsdecomp
/src/erf
/src/untws
/src/tws
Expand Down Expand Up @@ -138,6 +139,7 @@ Makefile.in
/src/cbgt2tga.exe
/src/cdpth2tga.exe
/src/ncsdis.exe
/src/ncsdecomp.exe
/src/erf.exe
/src/untws.exe
/src/tws.exe
Expand Down
127 changes: 127 additions & 0 deletions src/ncsdecomp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/* xoreos-tools - Tools to help with xoreos development
*
* xoreos-tools is the legal property of its developers, whose names
* can be found in the AUTHORS file distributed with this source
* distribution.
*
* xoreos-tools is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 3
* of the License, or (at your option) any later version.
*
* xoreos-tools is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with xoreos-tools. If not, see <http://www.gnu.org/licenses/>.
*/

/** @file
* Tool to decompiling NWScript bytecode.
*/

#include "src/version/version.h"

#include "src/common/scopedptr.h"
#include "src/common/ustring.h"
#include "src/common/util.h"
#include "src/common/error.h"
#include "src/common/platform.h"
#include "src/common/readfile.h"
#include "src/common/writefile.h"
#include "src/common/stdoutstream.h"
#include "src/common/cli.h"

#include "src/aurora/types.h"

#include "src/nwscript/decompiler.h"

#include "src/util.h"

bool parseCommandLine(const std::vector<Common::UString> &argv, int &returnValue,
Common::UString &inFile, Common::UString &outFile,
Aurora::GameID &game);

void decNCS(const Common::UString &inFile, const Common::UString &outFile,
Aurora::GameID &game);

int main(int argc, char **argv) {
initPlatform();

try {
std::vector<Common::UString> args;
Common::Platform::getParameters(argc, argv, args);

Aurora::GameID game = Aurora::kGameIDUnknown;

int returnValue = 1;
Common::UString inFile, outFile;

if (!parseCommandLine(args, returnValue, inFile, outFile, game))
return returnValue;

decNCS(inFile, outFile, game);
} catch (...) {
Common::exceptionDispatcherError();
}

return 0;
}

bool parseCommandLine(const std::vector<Common::UString> &argv, int &returnValue,
Common::UString &inFile, Common::UString &outFile,
Aurora::GameID &game) {
using Common::CLI::NoOption;
using Common::CLI::kContinueParsing;
using Common::CLI::Parser;
using Common::CLI::ValGetter;
using Common::CLI::Callback;
using Common::CLI::ValAssigner;
using Common::CLI::makeEndArgs;
using Common::CLI::makeAssigners;
using Aurora::GameID;

Common::UString encodingStr;
NoOption inFileOpt(false, new ValGetter<Common::UString &>(inFile, "input files"));
NoOption outFileOpt(true, new ValGetter<Common::UString &>(outFile, "output files"));
Parser parser(argv[0], "BioWare NWScript bytecode decompiler",
"\nIf no output file is given, the output is written to stdout.",
returnValue,
makeEndArgs(&inFileOpt, &outFileOpt));

parser.addSpace();
parser.addOption("nwn", "This is a Neverwinter Nights script", kContinueParsing,
makeAssigners(new ValAssigner<GameID>(Aurora::kGameIDNWN, game)));
parser.addOption("nwn2", "This is a Neverwinter Nights 2 script", kContinueParsing,
makeAssigners(new ValAssigner<GameID>(Aurora::kGameIDNWN2, game)));
parser.addOption("kotor", "This is a Knights of the Old Republic script", kContinueParsing,
makeAssigners(new ValAssigner<GameID>(Aurora::kGameIDKotOR, game)));
parser.addOption("kotor2", "This is a Knights of the Old Republic II script", kContinueParsing,
makeAssigners(new ValAssigner<GameID>(Aurora::kGameIDKotOR2, game)));
parser.addOption("jade", "This is a Jade Empire script", kContinueParsing,
makeAssigners(new ValAssigner<GameID>(Aurora::kGameIDJade, game)));
parser.addOption("witcher", "This is a The Witcher script", kContinueParsing,
makeAssigners(new ValAssigner<GameID>(Aurora::kGameIDWitcher, game)));
parser.addOption("dragonage", "This is a Dragon Age script", kContinueParsing,
makeAssigners(new ValAssigner<GameID>(Aurora::kGameIDDragonAge, game)));
parser.addOption("dragonage2", "This is a Dragon Age II script", kContinueParsing,
makeAssigners(new ValAssigner<GameID>(Aurora::kGameIDDragonAge2, game)));

return parser.process(argv);
}

void decNCS(const Common::UString &inFile, const Common::UString &outFile, Aurora::GameID &game) {
Common::ScopedPtr<Common::SeekableReadStream> ncs(new Common::ReadFile(inFile));
Common::ScopedPtr<Common::WriteStream> out(openFileOrStdOut(outFile));

status("Decompiling script...");
NWScript::Decompiler decompiler(*ncs, game);
decompiler.createNSS(*out);

out->flush();

if (!outFile.empty())
status("Deccompiled \"%s\" into \"%s\"", inFile.c_str(), outFile.c_str());
}
13 changes: 13 additions & 0 deletions src/rules.mk
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,19 @@ src_keybif_LDADD = \
$(LDADD) \
$(EMPTY)

bin_PROGRAMS += src/ncsdecomp
src_ncsdecomp_SOURCES = \
src/ncsdecomp.cpp \
src/util.cpp \
$(EMPTY)
src_ncsdecomp_LDADD = \
src/nwscript/libnwscript.la \
src/aurora/libaurora.la \
src/common/libcommon.la \
src/version/libversion.la \
$(LDADD) \
$(EMPTY)

# Subdirectories

include src/version/rules.mk
Expand Down

0 comments on commit 5f2cef4

Please sign in to comment.