Files
int2ssl/main.cpp
T

239 lines
6.6 KiB
C++
Raw Normal View History

2015-02-13 16:59:11 +03:00
#include "stdafx.h"
#include "FalloutScript.h"
#include "XGetopt.h"
2015-02-15 16:16:45 +03:00
#include "Utility.h"
2015-02-13 16:59:11 +03:00
#include <iostream>
using namespace std;
// Globals
BOOL g_bDump = FALSE;
int g_nFalloutVersion = 2;
2015-02-15 16:16:45 +03:00
std::string g_strIndentFill("\t");
2015-02-13 16:59:11 +03:00
BOOL g_bIgnoreWrongNumOfArgs = FALSE;
BOOL g_bInsOmittedArgsBackward = FALSE;
BOOL g_bStopOnError = FALSE;
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-13 16:59:11 +03:00
BOOL ProcessCommandLine(int argc, char* argv[]);
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-14 13:39:47 +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-14 13:39:47 +03:00
if (g_bStopOnError == TRUE)
{
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
}
}
catch (const UserException& e)
{
// Error message already displayed
if (g_bStopOnError == TRUE)
{
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
}
BOOL ProcessCommandLine(int argc, char* argv[])
{
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':
g_bDump = TRUE;
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':
g_bIgnoreWrongNumOfArgs = TRUE;
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':
g_bInsOmittedArgsBackward = TRUE;
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':
g_bStopOnError = TRUE;
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");
if (g_bStopOnError == TRUE)
{
printf("Press ENTER to continue.\n");
cin.get();
}
return FALSE;
}
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
}
}
return TRUE;
2015-02-13 16:59:11 +03:00
}