Files
int2ssl/main.cpp
T

252 lines
6.9 KiB
C++
Raw Normal View History

2015-02-15 18:53:04 +03:00
/**
*
* Copyright (c) 2005-2009 Anchorite (TeamX), <anchorite2001@yandex.ru>
2015-02-15 21:33:27 +03:00
* Copyright (c) 2014-2015 Nirran, phobos2077
* Copyright (c) 2015 alexeevdv <mail@alexeevdv.ru>
2015-02-15 18:53:04 +03:00
* Distributed under the GNU GPL v3. For full terms see the file license.txt
*
*/
2015-02-15 16:31:38 +03:00
// C++ standard includes
#include <iostream>
// int2ssl includes
2015-02-13 16:59:11 +03:00
#include "FalloutScript.h"
#include "XGetopt.h"
2015-02-15 16:16:45 +03:00
#include "Utility.h"
2015-02-15 16:31:38 +03:00
2015-02-15 18:53:04 +03:00
// Third party includes
2015-02-13 16:59:11 +03:00
using namespace std;
// Globals
2015-02-15 16:59:52 +03:00
bool g_bDump = false;
2015-02-13 16:59:11 +03:00
int g_nFalloutVersion = 2;
2015-02-15 16:16:45 +03:00
std::string g_strIndentFill("\t");
2015-02-15 16:59:52 +03:00
bool g_bIgnoreWrongNumOfArgs = false;
bool g_bInsOmittedArgsBackward = false;
bool g_bStopOnError = false;
2015-02-13 16:59:11 +03:00
2015-02-15 16:16:45 +03:00
std::string g_inputFileName;
std::string g_outputFileName;
2015-02-13 16:59:11 +03:00
// Functions
2015-02-15 16:16:45 +03:00
void PrintUsage(std::string filename);
2015-02-15 16:59:52 +03:00
bool ProcessCommandLine(int argc, char* argv[]);
2015-02-13 16:59:11 +03:00
int main(int argc, char* argv[])
{
2015-02-15 16:16:45 +03:00
std::cout << "Fallout script decompiler, version 8.3.0 (sfall edition)" << std::endl
<< "Copyright (C) Anchorite (TeamX), 2005-2009" << std::endl
<< "anchorite2001@yandex.ru" << std::endl
<< "Continued by Nirran, phobos2077 (2014-2015)" << std::endl
<< "Crossplatformed by alexeevdv (2015)" << std::endl;
2015-02-13 16:59:11 +03:00
2015-02-15 16:16:45 +03:00
if (argc < 2 || !ProcessCommandLine(argc, argv))
2015-02-14 23:18:15 +03:00
{
PrintUsage(argv[0]);
cin.get();
return 1;
}
2015-02-13 16:59:11 +03:00
2015-02-14 13:39:47 +03:00
CFile fileInput;
CFile fileOutput;
2015-02-13 16:59:11 +03:00
2015-02-15 16:16:45 +03:00
if (!fileInput.Open(g_inputFileName, CFile::modeRead | CFile::shareDenyWrite))
2015-02-14 13:39:47 +03:00
{
2015-02-15 16:16:45 +03:00
printf("Error: Unable open input file %s.\n", g_inputFileName.c_str());
2015-02-15 16:59:52 +03:00
if (g_bStopOnError == true)
2015-02-13 22:31:52 +03:00
{
2015-02-14 13:39:47 +03:00
printf("Press ENTER to continue.\n");
2015-02-13 16:59:11 +03:00
cin.get();
2015-02-14 13:39:47 +03:00
}
return 1;
}
2015-02-15 16:16:45 +03:00
if (!fileOutput.Open(g_outputFileName, CFile::modeCreate | CFile::modeWrite | CFile::typeText | CFile::shareDenyWrite))
2015-02-14 13:39:47 +03:00
{
2015-02-15 16:16:45 +03:00
std::cout << format("Error: Unable open output file %s", g_outputFileName) << std::endl;
2015-02-15 16:59:52 +03:00
if (g_bStopOnError == true)
2015-02-14 13:39:47 +03:00
{
2015-02-15 16:16:45 +03:00
std::cout << "Press ENTER to continue." << std::endl;
2015-02-14 13:39:47 +03:00
cin.get();
}
return 1;
}
try
{
CArchive arInput(&fileInput, CArchive::load);
CFalloutScript Script;
2015-02-15 16:16:45 +03:00
std::cout << format("Loading file %s...", g_inputFileName) << std::endl;
2015-02-14 13:39:47 +03:00
Script.Serialize(arInput);
2015-02-15 16:16:45 +03:00
std::cout << format("File %s loaded successfuly", g_inputFileName) << std::endl
<< std::endl;
2015-02-14 13:39:47 +03:00
CArchive arOutput(&fileOutput, CArchive::store);
if (g_bDump)
{
2015-02-15 16:16:45 +03:00
std::cout << format("Dumping %s...", g_inputFileName) << std::endl;
2015-02-14 13:39:47 +03:00
Script.Dump(arOutput);
2015-02-15 16:16:45 +03:00
std::cout << format("File %s dumped successfuly", g_inputFileName) << std::endl;
2015-02-14 13:39:47 +03:00
}
else
{
2015-02-15 16:16:45 +03:00
std::cout << "Decompiling..." << std::endl;
2015-02-14 13:39:47 +03:00
printf(" Init definitions\n");
Script.InitDefinitions();
printf(" Processing code\n");
Script.ProcessCode();
printf(" Storing sources\n");
Script.StoreSource(arOutput);
2015-02-15 16:16:45 +03:00
std::cout << format("File %s decompiled successfuly\n", g_inputFileName) << std::endl;
2015-02-14 13:39:47 +03:00
}
}
2015-02-15 19:14:18 +03:00
catch (const std::exception& e)
2015-02-14 13:39:47 +03:00
{
// Error message already displayed
2015-02-15 16:59:52 +03:00
if (g_bStopOnError == true)
2015-02-14 13:39:47 +03:00
{
printf("Press ENTER to continue.\n");
cin.get();
}
return 1;
}
catch(...)
{
return 1;
// const int c_nErrMsgLen = 1024;
// TCHAR lpszErrMsg[c_nErrMsgLen];
// e->GetErrorMessage(lpszErrMsg, c_nErrMsgLen);
// printf("Error: %s.\n", lpszErrMsg);
// if (g_bStopOnError == TRUE)
// {
// printf("Press ENTER to continue.\n");
// cin.get();
// }
// return 1;
}
return 0;
2015-02-13 16:59:11 +03:00
}
2015-02-15 16:16:45 +03:00
void PrintUsage(std::string filename)
2015-02-13 16:59:11 +03:00
{
2015-02-15 16:16:45 +03:00
std::cout << "Usage: " << filename << " [options] [-s value] file.int [file.ssl]" << std::endl
<< "Example: " << filename << " -d-1-a-b-e-s3 random.int" << std::endl
<< std::endl
<< "Options" << std::endl
<< " -d: dump file" << std::endl
<< " -1: input file is Fallout 1 script" << std::endl
<< " -a: ignore wrong number of arguments" << std::endl
<< " -b: insert omitted arguments backward" << std::endl
<< " -s: use Space instead of tab to indent" << std::endl
<< " -e: stop decompiling on error" << std::endl
<< " --: end of options" << std::endl;
2015-02-13 16:59:11 +03:00
}
2015-02-15 16:59:52 +03:00
bool ProcessCommandLine(int argc, char* argv[])
2015-02-13 16:59:11 +03:00
{
2015-02-14 13:39:47 +03:00
int c;
int nIndentWidth;
2015-02-13 16:59:11 +03:00
2015-02-14 13:39:47 +03:00
while((c = getopt(argc, argv, "d1abes:")) != EOF)
{
switch (c)
{
case 'd':
2015-02-15 16:59:52 +03:00
g_bDump = true;
2015-02-14 13:39:47 +03:00
printf("dump file is on.\n");
break;
2015-02-13 16:59:11 +03:00
2015-02-14 13:39:47 +03:00
case '1':
g_nFalloutVersion = 1;
printf("int2ssl is using Fallout 1 code.\n");
break;
2015-02-13 16:59:11 +03:00
2015-02-14 13:39:47 +03:00
case 'a':
2015-02-15 16:59:52 +03:00
g_bIgnoreWrongNumOfArgs = true;
2015-02-14 13:39:47 +03:00
printf("ignore wrong number of arguments is on.\n");
break;
2015-02-13 16:59:11 +03:00
2015-02-14 13:39:47 +03:00
case 'b':
2015-02-15 16:59:52 +03:00
g_bInsOmittedArgsBackward = true;
2015-02-14 13:39:47 +03:00
printf("insert omitted arguments backward is on.\n");
break;
2015-02-13 16:59:11 +03:00
2015-02-14 13:39:47 +03:00
case 'e':
2015-02-15 16:59:52 +03:00
g_bStopOnError = true;
2015-02-14 13:39:47 +03:00
printf("stop decompiling on error is on.\n");
break;
2015-02-13 16:59:11 +03:00
2015-02-14 13:39:47 +03:00
case 's':
nIndentWidth = atoi(optarg);
2015-02-13 16:59:11 +03:00
2015-02-14 13:39:47 +03:00
if (nIndentWidth <= 0)
{
printf("Warning: Invalid indent width. Indent set 3\n");
nIndentWidth = 3;
}
2015-02-13 16:59:11 +03:00
2015-02-14 13:39:47 +03:00
g_strIndentFill = "";
2015-02-13 16:59:11 +03:00
2015-02-14 13:39:47 +03:00
for(; nIndentWidth > 0; nIndentWidth--)
{
g_strIndentFill += " ";
}
break;
2015-02-13 16:59:11 +03:00
}
2015-02-14 13:39:47 +03:00
}
2015-02-13 16:59:11 +03:00
2015-02-14 13:39:47 +03:00
if (optind == argc)
{
printf("Error: Input file name omitted\n");
2015-02-15 16:59:52 +03:00
if (g_bStopOnError == true)
2015-02-14 13:39:47 +03:00
{
printf("Press ENTER to continue.\n");
cin.get();
}
2015-02-15 16:59:52 +03:00
return false;
2015-02-14 13:39:47 +03:00
}
2015-02-15 16:16:45 +03:00
g_inputFileName = argv[optind];
2015-02-14 13:39:47 +03:00
optind++;
if (optind < argc)
{
2015-02-15 16:16:45 +03:00
g_outputFileName = argv[optind];
2015-02-14 13:39:47 +03:00
}
else
{
2015-02-15 16:16:45 +03:00
g_outputFileName = g_inputFileName;
std::string extension = g_outputFileName.substr(g_outputFileName.length() - 4);
2015-02-14 13:39:47 +03:00
2015-02-14 23:34:03 +03:00
if (extension == ".int")
{
2015-02-15 16:16:45 +03:00
g_outputFileName = g_outputFileName.substr(0, g_outputFileName.length() - 4);
2015-02-14 23:34:03 +03:00
}
2015-02-14 13:39:47 +03:00
if (g_bDump)
{
2015-02-15 16:16:45 +03:00
g_outputFileName += ".dump";
2015-02-14 13:39:47 +03:00
}
else
{
2015-02-15 16:16:45 +03:00
g_outputFileName += ".ssl";
2015-02-14 13:39:47 +03:00
}
}
2015-02-15 16:59:52 +03:00
return true;
2015-02-13 16:59:11 +03:00
}