diff --git a/FalloutScript.cpp b/FalloutScript.cpp index 68b6be2..b09289a 100644 --- a/FalloutScript.cpp +++ b/FalloutScript.cpp @@ -1,20 +1,8 @@ -// FalloutScript.cpp : implementation file -// - #include "stdafx.h" #include "FalloutScript.h" #include -// Constants -//c_strBogusProcedureName = ".............."; -//c_strArgumentTemplate = "arg%u"; -//c_strGlobalVarTemplate = "GVar%u"; -//c_strLocalVarTemplate = "LVar%u"; - - -// CFalloutScript - CFalloutScript::CFalloutScript() { } @@ -118,37 +106,45 @@ void CFalloutScript::Serialize(CArchive& ar) CString strNumOfArgsWarning("Warning: Omitted \"# of argument to \'start\' procedure\"\n"); - if (!HeaderTail.IsEmpty()) { + if (!HeaderTail.IsEmpty()) + { opcode = HeaderTail.at(HeaderTail.GetUpperBound()); - if (opcode.GetOperator() == COpcode::O_CRITICAL_DONE) { + if (opcode.GetOperator() == COpcode::O_CRITICAL_DONE) + { HeaderTail.RemoveAt(HeaderTail.GetUpperBound()); - if (!HeaderTail.IsEmpty()) { + if (!HeaderTail.IsEmpty()) + { opcode = HeaderTail.at(HeaderTail.GetUpperBound()); - if (opcode.HasArgument()) { + if (opcode.HasArgument()) + { HeaderTail.RemoveAt(HeaderTail.GetUpperBound()); } - else { + else + { printf("\n"); printf("%s", strNumOfArgsWarning.c_str()); printf("\n"); } } - else { + else + { printf("\n"); printf("%s", strNumOfArgsWarning.c_str()); printf("\n"); } } - else { + else + { printf("\n"); printf("Warning: Omitted expected opcode 0x8003. Opcode with # of arguments will be not analysed\n"); printf("\n"); } } - else { + else + { printf("\n"); printf("%s", strNumOfArgsWarning.c_str()); printf("\n"); @@ -169,12 +165,14 @@ void CFalloutScript::Serialize(CArchive& ar) // Global variables printf(" Extract \"Global variables\" section\n"); - for(INT_PTR i = 0; i < HeaderTail.GetSize(); i++) { + for(INT_PTR i = 0; i < HeaderTail.GetSize(); i++) + { WORD wGlobalVarOperator = HeaderTail.at(i).GetOperator(); if ((wGlobalVarOperator != COpcode::O_STRINGOP) && (wGlobalVarOperator != COpcode::O_FLOATOP) && - (wGlobalVarOperator != COpcode::O_INTOP)) { + (wGlobalVarOperator != COpcode::O_INTOP)) + { printf("Error: Malformed \"Global variables\" section\n"); AfxThrowUserException(); } @@ -213,27 +211,35 @@ void CFalloutScript::ExtractCodeElements(COpcodeArray& Source, COpcodeArray& Des { INT_PTR i = 0; - for(; i < Source.GetSize(); i++) { - if (Source.at(i).GetOperator() == wDelimeter) { + for(; i < Source.GetSize(); i++) + { + if (Source.at(i).GetOperator() == wDelimeter) + { break; } } - if (i < Source.GetSize()) { - if (i < nSizeOfCodeItem - 1) { + if (i < Source.GetSize()) + { + if (i < nSizeOfCodeItem - 1) + { printf(lpszErrorMessage); AfxThrowUserException(); } - while(Source.at(i).GetOperator() == wDelimeter) { - for(INT_PTR j = 0; j < nSizeOfCodeItem - 1; j++) { - if (!((this->*pCheckFunc)(Source.at(i - nSizeOfCodeItem + 1 + j).GetOperator(), j))) { + while(Source.at(i).GetOperator() == wDelimeter) + { + for(INT_PTR j = 0; j < nSizeOfCodeItem - 1; j++) + { + if (!((this->*pCheckFunc)(Source.at(i - nSizeOfCodeItem + 1 + j).GetOperator(), j))) + { printf(lpszErrorMessage); AfxThrowUserException(); } } - for(INT_PTR j = 0; j < nSizeOfCodeItem - 1; j++) { + for(INT_PTR j = 0; j < nSizeOfCodeItem - 1; j++) + { Destination.Add(Source.at(i - nSizeOfCodeItem + 1)); Source.RemoveAt(i - nSizeOfCodeItem + 1); } @@ -241,7 +247,8 @@ void CFalloutScript::ExtractCodeElements(COpcodeArray& Source, COpcodeArray& Des Source.RemoveAt(i - nSizeOfCodeItem + 1); // Delimeter - if (i > Source.GetUpperBound()) { + if (i > Source.GetUpperBound()) + { break; } } @@ -255,7 +262,8 @@ BOOL CFalloutScript::CheckExportVarCode(WORD wOperator, INT_PTR nIndex) BOOL CFalloutScript::CheckSetExportedVarValueCode(WORD wOperator, INT_PTR nIndex) { - switch(nIndex) { + switch(nIndex) + { case 0: return (wOperator == COpcode::O_STRINGOP) || (wOperator == COpcode::O_FLOATOP) || @@ -279,3 +287,4 @@ bool CFalloutScript::ArgNeedParens( const CNode& node, const CNode& argument, CF && ((GetPriority(argument.m_Opcode.GetOperator()) != GetPriority(node.m_Opcode.GetOperator())) || (GetAssociation(node.m_Opcode.GetOperator()) != assoc))); } + diff --git a/FalloutScript.h b/FalloutScript.h index 7b2fb97..180edfe 100644 --- a/FalloutScript.h +++ b/FalloutScript.h @@ -1,5 +1,3 @@ -#pragma once - #ifndef FALLOUT_SCRIPT_H #define FALLOUT_SCRIPT_H @@ -8,11 +6,8 @@ #include "Namespace.h" #include "Node.h" - -// Constants - -// CFalloutScript -class CFalloutScript : public CObject { +class CFalloutScript +{ public: CFalloutScript(); virtual ~CFalloutScript(); @@ -73,8 +68,9 @@ private: Assoc GetAssociation(WORD wOperator); private: - // CDefObject - class CDefObject { + + class CDefObject + { public: enum ObjectType { OBJECT_VARIABLE, diff --git a/FalloutScriptDecompile.cpp b/FalloutScriptDecompile.cpp index 9db1914..fc47ef7 100644 --- a/FalloutScriptDecompile.cpp +++ b/FalloutScriptDecompile.cpp @@ -1,6 +1,3 @@ -// FalloutScriptDecompile.cpp : implementation file -// - #include "stdafx.h" #include "FalloutScript.h" #include "ObjectAttributes.h" @@ -10,7 +7,7 @@ // Globals extern BOOL g_bIgnoreWrongNumOfArgs; extern BOOL g_bInsOmittedArgsBackward; -// CFalloutScript + void CFalloutScript::InitDefinitions() { ULONG ulNameOffset; @@ -60,26 +57,33 @@ void CFalloutScript::ProcessCode() printf(" Extracting and reducing conditions\n"); - for(INT_PTR i = 0; i < m_ProcTable.GetSize(); i++) { - if (m_ProcTable[i].m_ulType & P_CONDITIONAL) { + for(INT_PTR i = 0; i < m_ProcTable.GetSize(); i++) + { + if (m_ProcTable[i].m_ulType & P_CONDITIONAL) + { ExtractAndReduceCondition(m_ProcBodies.at(i), m_Conditions.at(i), 0); } - for(INT_PTR j = 0; j < m_ProcBodies.at(i).GetSize(); j++) { - if (m_ProcBodies.at(i).at(j).m_Opcode.GetOperator() == COpcode::O_CALL_CONDITION) { + for(INT_PTR j = 0; j < m_ProcBodies.at(i).GetSize(); j++) + { + if (m_ProcBodies.at(i).at(j).m_Opcode.GetOperator() == COpcode::O_CALL_CONDITION) + { CNodeArray Condition; CNode node = m_ProcBodies.at(i).at(j).m_Arguments.at(0); - if (node.m_Opcode.GetOperator() != COpcode::O_INTOP) { + if (node.m_Opcode.GetOperator() != COpcode::O_INTOP) + { printf("Error: Invalid opcode for start address of condition\n"); AfxThrowUserException(); } ULONG ulCondStartAddress = node.m_Opcode.GetArgument(); - do { + do + { node = m_ProcBodies.at(i).at(j = NextNodeIndex(m_ProcBodies.at(i), j, -1)); - } while(node.m_ulOffset != ulCondStartAddress); + } + while(node.m_ulOffset != ulCondStartAddress); j = NextNodeIndex(m_ProcBodies.at(i), j, -1); // For O_JMP opcode @@ -92,7 +96,8 @@ void CFalloutScript::ProcessCode() printf(" Setting borders of blocks\n"); - for(INT_PTR i = 0; i < m_ProcTable.GetSize(); i++) { + for(INT_PTR i = 0; i < m_ProcTable.GetSize(); i++) + { printf(" Procedure: %d\r", i); SetBordersOfBlocks(m_ProcBodies.at(i)); } @@ -112,11 +117,13 @@ INT_PTR CFalloutScript::GetIndexOfProc(const char* lpszName) strName.MakeLower(); - for(INT_PTR i = 0; i < m_ProcTable.GetSize(); i++) { + for(INT_PTR i = 0; i < m_ProcTable.GetSize(); i++) + { strTestName = m_Namespace[m_ProcTable[i].m_ulNameOffset]; strTestName.MakeLower(); - if (strTestName == strName) { + if (strTestName == strName) + { nResult = i; break; } @@ -145,8 +152,10 @@ INT_PTR CFalloutScript::GetIndexOfExportedVariable(ULONG ulNameOffset) { INT_PTR nResult = -1; - for(INT_PTR i = 0; i < m_ExportedVarValue.GetSize(); i += 2) { - if (m_ExportedVarValue.at(i + 1).GetArgument() == ulNameOffset) { + for(INT_PTR i = 0; i < m_ExportedVarValue.GetSize(); i += 2) + { + if (m_ExportedVarValue.at(i + 1).GetArgument() == ulNameOffset) + { nResult = i; break; } @@ -159,7 +168,8 @@ void CFalloutScript::SetExternalVariable(ULONG ulNameOffset) { CDefObject defObject; - if (!m_Definitions.Lookup(ulNameOffset, defObject)) { + if (!m_Definitions.Lookup(ulNameOffset, defObject)) + { m_Definitions.SetAt(ulNameOffset, CDefObject(CDefObject::OBJECT_VARIABLE, V_IMPORT)); } } @@ -173,9 +183,12 @@ void CFalloutScript::TryRenameGlobalVariables() CDefObject defObject; INT_PTR nGlobalVarIndex = 0; - if (nNamesCount - nDefinitionsCount == nGlobalVarCount) { - for(INT_PTR i = 0; i < m_Namespace.GetSize(); i++) { - if (!m_Definitions.Lookup(m_Namespace.GetOffsetByIndex(i), defObject)) { + if (nNamesCount - nDefinitionsCount == nGlobalVarCount) + { + for(INT_PTR i = 0; i < m_Namespace.GetSize(); i++) + { + if (!m_Definitions.Lookup(m_Namespace.GetOffsetByIndex(i), defObject)) + { defObject.m_ObjectType = CDefObject::OBJECT_VARIABLE; defObject.m_ulAttributes = V_GLOBAL | m_GlobalVar.at(nGlobalVarIndex).GetOperator(); defObject.m_ulVarValue = m_GlobalVar.at(nGlobalVarIndex).GetArgument(); @@ -193,11 +206,14 @@ void CFalloutScript::TryRenameImportedVariables() CDefObject defObject; ULONG ulNameOffset; - if (m_GlobalVar.GetSize() == 0) { - for(INT_PTR i = 0; i < m_Namespace.GetSize(); i++) { + if (m_GlobalVar.GetSize() == 0) + { + for(INT_PTR i = 0; i < m_Namespace.GetSize(); i++) + { ulNameOffset = m_Namespace.GetOffsetByIndex(i); - if (!m_Definitions.Lookup(ulNameOffset, defObject)) { + if (!m_Definitions.Lookup(ulNameOffset, defObject)) + { m_Definitions.SetAt(ulNameOffset, CDefObject(CDefObject::OBJECT_VARIABLE, V_IMPORT)); } } @@ -208,8 +224,7 @@ INT_PTR CFalloutScript::NextNodeIndex( CNodeArray& NodeArray, INT_PTR nCurrentIn { INT_PTR nResult = nCurrentIndex + nStep; -// if ((nResult < 0) || (nResult > NodeArray.GetUpperBound())) -if ((nResult < 0) || (nResult > NodeArray.GetUpperBound())) + if ((nResult < 0) || (nResult > NodeArray.GetUpperBound())) { printf("Error: Index of node out of range\n"); AfxThrowUserException(); @@ -227,10 +242,12 @@ BOOL CFalloutScript::RemoveSequenceOfNodes(CNodeArray& NodeArray, INT_PTR nStart { INT_PTR nCurrentNodeIndex = nStartIndex - 1; - for(INT_PTR i = 0; i < nSequenceLen; i++) { + for(INT_PTR i = 0; i < nSequenceLen; i++) + { nCurrentNodeIndex = NextNodeIndex(NodeArray, nCurrentNodeIndex, 1); - if (NodeArray.at(nCurrentNodeIndex).m_Opcode.GetOperator() != wSequence[nCurrentNodeIndex - nStartIndex]) { + if (NodeArray.at(nCurrentNodeIndex).m_Opcode.GetOperator() != wSequence[nCurrentNodeIndex - nStartIndex]) + { return FALSE; } } @@ -305,42 +322,54 @@ void CFalloutScript::InitialReduce() WORD* pwCode; INT_PTR nCount; - for(INT_PTR i = 0 ; i < m_ProcBodies.GetSize(); i++) { + for(INT_PTR i = 0 ; i < m_ProcBodies.GetSize(); i++) + { // Tail - if (!m_ProcBodies.at(i).IsEmpty()) { + if (!m_ProcBodies.at(i).IsEmpty()) + { pwCode = (m_ProcTable[i].m_ulType & P_CRITICAL) ? awTailOfCriticalProc : awTailOfProc; nCount = (m_ProcTable[i].m_ulType & P_CRITICAL) ? 4 : 3; - if (!RemoveSequenceOfNodes(m_ProcBodies.at(i), m_ProcBodies.at(i).GetSize() - nCount, nCount, pwCode, nCount)) { + if (!RemoveSequenceOfNodes(m_ProcBodies.at(i), m_ProcBodies.at(i).GetSize() - nCount, nCount, pwCode, nCount)) + { printf("Error: Invalid tail of procedure\'s body\n"); AfxThrowUserException(); } } // Body - for(INT_PTR j = 0; j < m_ProcBodies.at(i).GetSize(); j++) { - switch(m_ProcBodies.at(i).at(j).m_Opcode.GetOperator()) { + for(INT_PTR j = 0; j < m_ProcBodies.at(i).GetSize(); j++) + { + switch(m_ProcBodies.at(i).at(j).m_Opcode.GetOperator()) + { case COpcode::O_DUP: // 'Check procedure's arguments count' statement - if (!RemoveSequenceOfNodes(m_ProcBodies.at(i), j, 3, awCheckArgCount, 3)) { + if (!RemoveSequenceOfNodes(m_ProcBodies.at(i), j, 3, awCheckArgCount, 3)) + { // short circuit AND UINT actualOperator = CheckSequenceOfNodes(m_ProcBodies.at(i), j, awShortCircuitAnd, 5) ? COpcode::O_AND : (CheckSequenceOfNodes(m_ProcBodies.at(i), j, awShortCircuitOr, 6) ? COpcode::O_OR : 0); - if (actualOperator) { + if (actualOperator) + { UINT k, skipOffset = m_ProcBodies.at(i).at(j+1).m_Opcode.GetArgument(); CNode node; k = j - 1; - do { + do + { k = NextNodeIndex(m_ProcBodies.at(i), k, 1); - } while (skipOffset > m_ProcBodies.at(i).at(k).m_ulOffset); + } + while (skipOffset > m_ProcBodies.at(i).at(k).m_ulOffset); + m_ProcBodies.at(i).InsertAt(k, m_ProcBodies.at(i).at(j)); m_ProcBodies.at(i).at(k).m_Opcode.SetOperator(actualOperator); // place AND/OR here, so BuildTree() will treat it as a regular binary operator m_ProcBodies.at(i).at(k).m_ulOffset = m_ProcBodies.at(i).at(k-1).m_ulOffset + COpcode::OPERATOR_SIZE; // adjust offset m_ProcBodies.at(i).RemoveAt(j, (actualOperator == COpcode::O_AND) ? 5 : 6); // reduce - } else { + } + else + { printf("Error: Unknown sequence of opcodes\n"); AfxThrowUserException(); } @@ -354,8 +383,10 @@ void CFalloutScript::InitialReduce() pwCode = (m_ProcTable[i].m_ulType & P_CRITICAL) ? awCriticalReturn : awReturn; nCount = (m_ProcTable[i].m_ulType & P_CRITICAL) ? 7 : 6; - if (!RemoveSequenceOfNodes(m_ProcBodies.at(i), j, nCount - 1, pwCode, nCount)) { - if (!RemoveSequenceOfNodes(m_ProcBodies.at(i), j - 1, 2, awStoreReturnAdress, 2)) { + if (!RemoveSequenceOfNodes(m_ProcBodies.at(i), j, nCount - 1, pwCode, nCount)) + { + if (!RemoveSequenceOfNodes(m_ProcBodies.at(i), j - 1, 2, awStoreReturnAdress, 2)) + { printf("Error: Unknown sequence of opcodes\n"); AfxThrowUserException(); } @@ -385,59 +416,56 @@ ULONG CFalloutScript::BuildTreeBranch(CNodeArray& NodeArray, ULONG nStartIndex, opcodeAttributes = NodeArray.at(j).m_Opcode.GetAttributes(); nNumOfArgs = INT_PTR(opcodeAttributes.m_ulNumArgs); -std::cout << std::hex << wOperator << std::endl; - switch(wOperator) { case COpcode::O_FETCH_EXTERNAL: case COpcode::O_STORE_EXTERNAL: { - INT_PTR nExtVarNameNodeIndex = NextNodeIndex(NodeArray, j, -1); - WORD wOpeartor = NodeArray.at(nExtVarNameNodeIndex).m_Opcode.GetOperator(); - ULONG ulArgument = NodeArray.at(nExtVarNameNodeIndex).m_Opcode.GetArgument(); + INT_PTR nExtVarNameNodeIndex = NextNodeIndex(NodeArray, j, -1); + WORD wOpeartor = NodeArray.at(nExtVarNameNodeIndex).m_Opcode.GetOperator(); + ULONG ulArgument = NodeArray.at(nExtVarNameNodeIndex).m_Opcode.GetArgument(); - if ((wOpeartor != COpcode::O_STRINGOP) && (wOpeartor != COpcode::O_INTOP)) - { - printf("Error: Invalid reference to external variable\n"); - AfxThrowUserException(); - } - - SetExternalVariable(ulArgument); + if ((wOpeartor != COpcode::O_STRINGOP) && (wOpeartor != COpcode::O_INTOP)) + { + printf("Error: Invalid reference to external variable\n"); + AfxThrowUserException(); } + SetExternalVariable(ulArgument); break; + } case COpcode::O_CALL: + { + INT_PTR nProcNumOfArgsNodeIndex = NextNodeIndex(NodeArray, j, -2); + WORD wProcNumOfArgsOperator = NodeArray.at(nProcNumOfArgsNodeIndex).m_Opcode.GetOperator(); + ULONG ulProcNumOfArgs = NodeArray.at(nProcNumOfArgsNodeIndex).m_Opcode.GetArgument(); + + if (wProcNumOfArgsOperator != COpcode::O_INTOP) { - INT_PTR nProcNumOfArgsNodeIndex = NextNodeIndex(NodeArray, j, -2); - WORD wProcNumOfArgsOperator = NodeArray.at(nProcNumOfArgsNodeIndex).m_Opcode.GetOperator(); - ULONG ulProcNumOfArgs = NodeArray.at(nProcNumOfArgsNodeIndex).m_Opcode.GetArgument(); - - if (wProcNumOfArgsOperator != COpcode::O_INTOP) { - printf("Error: Invalid opcode for procedure\'s number of arguments\n"); - AfxThrowUserException(); - } - - nNumOfArgs = INT_PTR(ulProcNumOfArgs) + 2; + printf("Error: Invalid opcode for procedure\'s number of arguments\n"); + AfxThrowUserException(); } + nNumOfArgs = INT_PTR(ulProcNumOfArgs) + 2; break; + } case COpcode::O_ADDREGION: + { + INT_PTR nAddRegionNumOfArgsNodeIndex = NextNodeIndex(NodeArray, j, -1); + WORD wAddRegionNumOfArgsOperator = NodeArray.at(nAddRegionNumOfArgsNodeIndex).m_Opcode.GetOperator(); + ULONG ulAddRegionNumOfArgs = NodeArray.at(nAddRegionNumOfArgsNodeIndex).m_Opcode.GetArgument(); + + if (wAddRegionNumOfArgsOperator != COpcode::O_INTOP) { - INT_PTR nAddRegionNumOfArgsNodeIndex = NextNodeIndex(NodeArray, j, -1); - WORD wAddRegionNumOfArgsOperator = NodeArray.at(nAddRegionNumOfArgsNodeIndex).m_Opcode.GetOperator(); - ULONG ulAddRegionNumOfArgs = NodeArray.at(nAddRegionNumOfArgsNodeIndex).m_Opcode.GetArgument(); - - if (wAddRegionNumOfArgsOperator != COpcode::O_INTOP) { - printf("Error: Invalid opcode for addRegion number of arguments\n"); - AfxThrowUserException(); - } - - nNumOfArgs = INT_PTR(ulAddRegionNumOfArgs) + 1; + printf("Error: Invalid opcode for addRegion number of arguments\n"); + AfxThrowUserException(); } + nNumOfArgs = INT_PTR(ulAddRegionNumOfArgs) + 1; break; + } } // Check nodes @@ -540,27 +568,32 @@ void CFalloutScript::ExtractAndReduceCondition(CNodeArray& Source, CNodeArray& D node = Source.at(nNodeIndex = NextNodeIndex(Source, nStartIndex - 1, 1)); - if (node.m_Opcode.GetOperator() != COpcode::O_JMP) { + if (node.m_Opcode.GetOperator() != COpcode::O_JMP) + { printf("Error: Invalid startup of condition\n"); AfxThrowUserException(); } CNode nodeJumpAddress = node.m_Arguments.at(0); - if (nodeJumpAddress.m_Opcode.GetOperator() != COpcode::O_INTOP) { + if (nodeJumpAddress.m_Opcode.GetOperator() != COpcode::O_INTOP) + { printf("Error: Invalid startup of condition\n"); AfxThrowUserException(); } ULONG ulJumpOffset = node.m_Arguments.at(0).m_Opcode.GetArgument(); - do { + do + { node = Source.at(nNodeIndex = NextNodeIndex(Source, nNodeIndex, 1)); - } while(node.m_ulOffset < ulJumpOffset); + } + while(node.m_ulOffset < ulJumpOffset); Destination.SetSize(nNodeIndex - nStartIndex); - for(INT_PTR j = 0; j < nNodeIndex - nStartIndex; j++) { + for(INT_PTR j = 0; j < nNodeIndex - nStartIndex; j++) + { Destination.at(j) = Source.at(nStartIndex + j); } @@ -576,24 +609,29 @@ void CFalloutScript::ExtractAndReduceCondition(CNodeArray& Source, CNodeArray& D }; // Startup - if (!RemoveSequenceOfNodes(Destination, 0, 2, awStartupOfCondition, 2)) { + if (!RemoveSequenceOfNodes(Destination, 0, 2, awStartupOfCondition, 2)) + { printf("Error: Invalid startup of condition\n"); AfxThrowUserException(); } // Cleanup - if (!RemoveSequenceOfNodes(Destination, Destination.GetSize() - 2, 2, awCleanupOfCondition, 2)) { + if (!RemoveSequenceOfNodes(Destination, Destination.GetSize() - 2, 2, awCleanupOfCondition, 2)) + { printf("Error: Invalid cleanup of condition\n"); AfxThrowUserException(); } // Check condition - if (Destination.GetSize() != 1) { + if (Destination.GetSize() != 1) + { printf("Error: Invalid condition. Only one expression allowed\n"); AfxThrowUserException(); } - else { - if (Destination.at(0).m_Opcode.GetAttributes().m_Type != COpcode::COpcodeAttributes::TYPE_EXPRESSION) { + else + { + if (Destination.at(0).m_Opcode.GetAttributes().m_Type != COpcode::COpcodeAttributes::TYPE_EXPRESSION) + { printf("Error: Invalid condition. Expression required\n"); AfxThrowUserException(); } @@ -605,14 +643,16 @@ void CFalloutScript::ExtractAndReduceCondition(CNodeArray& Source, CNodeArray& D void CFalloutScript::SetBordersOfBlocks(CNodeArray& NodeArray) { - if (NodeArray.IsEmpty()) { + if (NodeArray.IsEmpty()) + { return; } ULONG ulOffset; // Start of procedure - if (NodeArray.at(0).m_Opcode.GetOperator() == COpcode::O_PUSH_BASE) { + if (NodeArray.at(0).m_Opcode.GetOperator() == COpcode::O_PUSH_BASE) + { ulOffset = NodeArray.at(0).m_ulOffset; NodeArray.at(0) = c_NodeBeginOfBlock; NodeArray.at(0).m_ulOffset = ulOffset; @@ -623,14 +663,15 @@ void CFalloutScript::SetBordersOfBlocks(CNodeArray& NodeArray) if ((NodeArray.at(nLastNodeIndex).m_Opcode.GetOperator() == COpcode::O_POP_RETURN) && (NodeArray.at(nLastNodeIndex).m_Opcode.GetArgument() == 0) && - (NodeArray.at(nLastNodeIndex).m_Arguments.at(0).m_Opcode.GetOperator() == COpcode::O_INTOP)){ + (NodeArray.at(nLastNodeIndex).m_Arguments.at(0).m_Opcode.GetOperator() == COpcode::O_INTOP)) + { ulOffset = NodeArray.at(nLastNodeIndex).m_ulOffset; NodeArray.at(nLastNodeIndex) = c_NodeEndOfBlock; NodeArray.at(nLastNodeIndex).m_ulOffset = ulOffset; - } else /*if ((NodeArray.at(nLastNodeIndex).m_Opcode.GetOperator() == COpcode::O_POP_RETURN) && - (NodeArray.at(nLastNodeIndex).m_Arguments.at(0).m_Opcode.GetOperator() == COpcode::O_INTOP))*/ { + } + else + { ulOffset = NodeArray.at(nLastNodeIndex).m_ulOffset; - //NodeArray.at(nLastNodeIndex) = c_NodeEndOfBlock; NodeArray.InsertAt(nLastNodeIndex + 1, CNode(c_NodeEndOfBlock)); NodeArray.at(nLastNodeIndex).m_ulOffset = ulOffset; } @@ -638,166 +679,200 @@ void CFalloutScript::SetBordersOfBlocks(CNodeArray& NodeArray) // Body CNode node; - for(INT_PTR i = 0; i < NodeArray.GetSize(); i++) { - switch(NodeArray.at(i).m_Opcode.GetOperator()) { + for(INT_PTR i = 0; i < NodeArray.GetSize(); i++) + { + switch(NodeArray.at(i).m_Opcode.GetOperator()) + { case COpcode::O_WHILE: + { + CNode node = NodeArray.at(i).m_Arguments.at(0); + ULONG loopOffset = NodeArray.at(i).m_Arguments.at(1).GetTopOffset(); + + if (node.m_Opcode.GetOperator() != COpcode::O_INTOP) { - CNode node = NodeArray.at(i).m_Arguments.at(0); - ULONG loopOffset = NodeArray.at(i).m_Arguments.at(1).GetTopOffset(); + printf("Error: Invalid opcode for jump-address\n"); + AfxThrowUserException(); + } + + NodeArray.InsertAt(i + 1, CNode(c_NodeBeginOfBlock)); + NodeArray.at(i + 1).m_ulOffset = NodeArray.at(i).m_ulOffset; + ulOffset = node.m_Opcode.GetArgument(); + + INT_PTR nNodeIndex = i + 1; + CArray jumps; - if (node.m_Opcode.GetOperator() != COpcode::O_INTOP) { - printf("Error: Invalid opcode for jump-address\n"); - AfxThrowUserException(); - } - - NodeArray.InsertAt(i + 1, CNode(c_NodeBeginOfBlock)); - NodeArray.at(i + 1).m_ulOffset = NodeArray.at(i).m_ulOffset; - ulOffset = node.m_Opcode.GetArgument(); - - INT_PTR nNodeIndex = i + 1; - CArray jumps; - //CMap elses = CMap (); - //bool unused; - do { - node = NodeArray.at(nNodeIndex = NextNodeIndex(NodeArray, nNodeIndex, 1)); - if (node.m_Opcode.GetOperator() == COpcode::O_JMP && node.m_Type == CNode::TYPE_NORMAL && node.m_Arguments.GetSize() > 0) { - ULONG ofs = node.m_Arguments.at(0).m_Opcode.GetArgument(); - if (ofs == ulOffset) { - NodeArray.at(nNodeIndex).m_Type = CNode::TYPE_BREAK; - } else if (ofs == loopOffset) { - NodeArray.at(nNodeIndex).m_Type = CNode::TYPE_CONTINUE; // continue in "while" loop - } else { - //if (!elses.Lookup(NodeArray[nNodeIndex+1].GetTopOffset(), unused)) { - jumps.Add(nNodeIndex); - //} - } - }/* else if (node.m_Opcode.GetOperator() == COpcode::O_IF && node.m_Arguments.GetSize() > 0) { - elses[node.m_Arguments.at(0).m_Opcode.GetArgument()] = true; - }*/ - } while(node.m_ulOffset < ulOffset); - - bool isForLoop = false; - if (NodeArray.at(nNodeIndex - 2).m_Arguments.GetSize() > 0 && i > 0) { // *might* be a "for" loop - loopOffset = NodeArray.at(nNodeIndex - 2).GetTopOffset(); - for (INT_PTR j=0; j 0) + { + ULONG ofs = node.m_Arguments.at(0).m_Opcode.GetArgument(); + if (ofs == ulOffset) + { + NodeArray.at(nNodeIndex).m_Type = CNode::TYPE_BREAK; + } + else if (ofs == loopOffset) + { + NodeArray.at(nNodeIndex).m_Type = CNode::TYPE_CONTINUE; // continue in "while" loop + } + else + { + jumps.Add(nNodeIndex); } } + } + while(node.m_ulOffset < ulOffset); - NodeArray.InsertAt(nNodeIndex, CNode(c_NodeEndOfBlock)); - NodeArray.at(nNodeIndex).m_ulOffset = NodeArray.at(nNodeIndex + 1).m_ulOffset; - - node = NodeArray.at(nNodeIndex - 1); - - if (node.m_Opcode.GetOperator() != COpcode::O_JMP) { - printf("Error: Invalid tail of \'while\' statement\n"); - AfxThrowUserException(); - } - - if (node.m_Arguments.at(0).m_Opcode.GetOperator() != COpcode::O_INTOP) { - printf("Error: Invalid opcode for jump-address\n"); - AfxThrowUserException(); - } - - if (isForLoop) { - NodeArray.at(i).m_Type = CNode::TYPE_FOR_LOOP; - NodeArray.at(i).m_Arguments.at(0) = NodeArray.at(i - 1); // "for" initializer - NodeArray.at(i).m_Arguments.Add(NodeArray.at(nNodeIndex - 2)); // "for" increment - NodeArray.RemoveAt(i - 1); // eat statement before "while" - NodeArray.RemoveAt(nNodeIndex - 3, 2); // eat last statement in loop along with jmp - } else { - NodeArray.RemoveAt(nNodeIndex - 1); - NodeArray.at(i).m_Arguments.RemoveAt(0); + bool isForLoop = false; + if (NodeArray.at(nNodeIndex - 2).m_Arguments.GetSize() > 0 && i > 0) + { // *might* be a "for" loop + loopOffset = NodeArray.at(nNodeIndex - 2).GetTopOffset(); + for (INT_PTR j=0; j %x", ulOffset, node.m_ulOffset); + if (ulOffset > node.m_ulOffset) + { + NodeArray.RemoveAt(nNodeIndex - 1); // remove jump + NodeArray.InsertAt(nNodeIndex, CNode(c_NodeBeginOfBlock)); + NodeArray.at(nNodeIndex).m_ulOffset = NodeArray.at(nNodeIndex + 1).m_ulOffset; - NodeArray.InsertAt(i + 1, CNode(c_NodeBeginOfBlock)); - NodeArray.at(i + 1).m_ulOffset = NodeArray.at(i + 2).m_ulOffset; - ulOffset = node.m_Opcode.GetArgument(); // offset for jump - - INT_PTR nNodeIndex = i + 1; - - do { - node = NodeArray.at(nNodeIndex = NextNodeIndex(NodeArray, nNodeIndex, 1)); - } while(node.m_ulOffset < ulOffset); - NodeArray.InsertAt(nNodeIndex, CNode(c_NodeEndOfBlock)); - NodeArray.at(nNodeIndex).m_ulOffset = NodeArray.at(nNodeIndex + 1).m_ulOffset; - - node = NodeArray.at(nNodeIndex - 1); - if (node.m_Opcode.GetOperator() == COpcode::O_JMP && node.m_Type != CNode::TYPE_BREAK && node.m_Type != CNode::TYPE_CONTINUE) { // else block - if (node.m_Arguments.at(0).m_Opcode.GetOperator() != COpcode::O_INTOP) { - printf("Error: Invalid opcode for jump-address\n"); - AfxThrowUserException(); - } - //ULONG jumpPastElseOffset = - ulOffset = node.m_Arguments.at(0).m_Opcode.GetArgument(); - //printf("(else) goto %x > %x", ulOffset, node.m_ulOffset); - if (ulOffset > node.m_ulOffset) { - NodeArray.RemoveAt(nNodeIndex - 1); // remove jump - NodeArray.InsertAt(nNodeIndex, CNode(c_NodeBeginOfBlock)); - NodeArray.at(nNodeIndex).m_ulOffset = NodeArray.at(nNodeIndex + 1).m_ulOffset; - - CNode Bnode = NodeArray.at(nNodeIndex - 1); - CNode Cnode = NodeArray.at(nNodeIndex + 1); - if ((Bnode.m_Type == CNode::TYPE_END_OF_BLOCK)/* && - (Cnode.m_Opcode.GetOperator() != COpcode::O_IF)*/) { - do { - node = NodeArray.at(nNodeIndex = NextNodeIndex(NodeArray, nNodeIndex, 1)); - } while(node.m_ulOffset < ulOffset); - NodeArray.InsertAt(nNodeIndex, CNode(c_NodeEndOfBlock)); - NodeArray.at(nNodeIndex).m_ulOffset = NodeArray.at(nNodeIndex + 1).m_ulOffset; + CNode Bnode = NodeArray.at(nNodeIndex - 1); + CNode Cnode = NodeArray.at(nNodeIndex + 1); + if ((Bnode.m_Type == CNode::TYPE_END_OF_BLOCK)/* && + (Cnode.m_Opcode.GetOperator() != COpcode::O_IF)*/) + { + do + { + node = NodeArray.at(nNodeIndex = NextNodeIndex(NodeArray, nNodeIndex, 1)); } + while(node.m_ulOffset < ulOffset); + NodeArray.InsertAt(nNodeIndex, CNode(c_NodeEndOfBlock)); + NodeArray.at(nNodeIndex).m_ulOffset = NodeArray.at(nNodeIndex + 1).m_ulOffset; } } } - NodeArray.at(i).m_Arguments.RemoveAt(0); - i++; + } + NodeArray.at(i).m_Arguments.RemoveAt(0); + i++; - break; + break; } } } void CFalloutScript::ReduceConditionalExpressions(CNodeArray& NodeArray) { - /*if (NodeArray.IsEmpty()) { + /* + if (NodeArray.IsEmpty()) + { return; } CNode node; - for (INT_PTR i = 0; i < NodeArray.GetSize(); i++) { - if (NodeArray.at(i).m_Opcode.GetOperator() == COpcode::O_IF) { - if (ReduceExpressionBlock(NodeArray, i+1)) { + for (INT_PTR i = 0; i < NodeArray.GetSize(); i++) + { + if (NodeArray.at(i).m_Opcode.GetOperator() == COpcode::O_IF) + { + if (ReduceExpressionBlock(NodeArray, i+1)) + { } } - }*/ + } + */ } BOOL CFalloutScript::IsOmittetArgsAllowed(WORD wOpcode) { if (((wOpcode >= COpcode::O_END_CORE) && (wOpcode < COpcode::O_END_OP)) || - (wOpcode == COpcode::O_POP_RETURN)) { + (wOpcode == COpcode::O_POP_RETURN)) + { return TRUE; } - else { + else + { return FALSE; } } diff --git a/FalloutScriptDefObject.cpp b/FalloutScriptDefObject.cpp index 80c9154..19c80cd 100644 --- a/FalloutScriptDefObject.cpp +++ b/FalloutScriptDefObject.cpp @@ -1,16 +1,12 @@ -// FalloutScript.cpp : implementation file -// - #include "stdafx.h" #include "FalloutScript.h" - -// CFalloutScript::CDefObject CFalloutScript::CDefObject::CDefObject(ObjectType type, ULONG ulAttributes, ULONG ulObjectData) { m_ulAttributes = ulAttributes; - switch(m_ObjectType = type) { + switch(m_ObjectType = type) + { case OBJECT_VARIABLE: m_ulVarValue = ulObjectData; break; diff --git a/FalloutScriptDump.cpp b/FalloutScriptDump.cpp index 1ddf1f8..edd03ea 100644 --- a/FalloutScriptDump.cpp +++ b/FalloutScriptDump.cpp @@ -1,7 +1,6 @@ #include "stdafx.h" #include "FalloutScript.h" -// CFalloutScript void CFalloutScript::Dump(CArchive& ar) { ar.WriteString("============== Procedures table ==================\n"); @@ -67,16 +66,20 @@ void CFalloutScript::Dump(CArchive& ar) ar.WriteString("============== Exported variables ==================\n"); - if (m_ExportedVarValue.IsEmpty()) { + if (m_ExportedVarValue.IsEmpty()) + { ar.WriteString("Not found\n"); } - else { - for(INT_PTR i = 0; i < m_ExportedVarValue.GetSize(); i += 2) { + else + { + for(INT_PTR i = 0; i < m_ExportedVarValue.GetSize(); i += 2) + { wOperator = m_ExportedVarValue.at(i).GetOperator(); ulArgument = m_ExportedVarValue.at(i).GetArgument(); ULONG ulNameArgument = m_ExportedVarValue.at(i + 1).GetArgument(); - switch(wOperator) { + switch(wOperator) + { case COpcode::O_STRINGOP: strOutLine.Format("%s := \"%s\"\n", m_Namespace[ulNameArgument].c_str(), @@ -107,19 +110,21 @@ void CFalloutScript::Dump(CArchive& ar) ar.WriteString("============== Procedures ==================\n"); ar.WriteString("\n"); - for(INT_PTR nIndexOfProc = 0; nIndexOfProc < m_ProcTable.GetSize(); nIndexOfProc++) { + for(INT_PTR nIndexOfProc = 0; nIndexOfProc < m_ProcTable.GetSize(); nIndexOfProc++) + { strOutLine.Format("%d: %s (0x%08x)\n", nIndexOfProc, m_Namespace[m_ProcTable[nIndexOfProc].m_ulNameOffset].c_str(), - m_ProcTable[nIndexOfProc].m_ulBodyOffset - ); + m_ProcTable[nIndexOfProc].m_ulBodyOffset); ar.WriteString(strOutLine); ar.WriteString("===============================\n"); - for(INT_PTR i = 0; i < m_ProcBodies.at(nIndexOfProc).GetSize(); i++) { + for(INT_PTR i = 0; i < m_ProcBodies.at(nIndexOfProc).GetSize(); i++) + { wOperator = m_ProcBodies.at(nIndexOfProc).at(i).m_Opcode.GetOperator(); ulArgument = m_ProcBodies.at(nIndexOfProc).at(i).m_Opcode.GetArgument(); - switch(wOperator) { + switch(wOperator) + { case COpcode::O_STRINGOP: case COpcode::O_INTOP: strOutLine.Format("0x%08X: 0x%04X 0x%08x - %s(0x%08x) // %u (%d)\n", diff --git a/FalloutScriptStore.cpp b/FalloutScriptStore.cpp index a86e271..b6ea726 100644 --- a/FalloutScriptStore.cpp +++ b/FalloutScriptStore.cpp @@ -1,6 +1,3 @@ -// FalloutScript.cpp : implementation file -// - #include "stdafx.h" #include "FalloutScript.h" #include "ObjectAttributes.h" @@ -8,7 +5,6 @@ // Globals extern CString g_strIndentFill; -// CFalloutScript void CFalloutScript::StoreTree(CArchive& ar) { CString strOutLine; @@ -16,16 +12,19 @@ void CFalloutScript::StoreTree(CArchive& ar) ar.WriteString("============== Procedures ==================\n"); ar.WriteString("\n"); - for(INT_PTR nIndexOfProc = 0; nIndexOfProc < m_ProcTable.GetSize(); nIndexOfProc++) { + for(INT_PTR nIndexOfProc = 0; nIndexOfProc < m_ProcTable.GetSize(); nIndexOfProc++) + { strOutLine.Format("%d: %s (0x%08x)\n", nIndexOfProc, m_Namespace[m_ProcTable[nIndexOfProc].m_ulNameOffset].c_str(), m_ProcTable[nIndexOfProc].m_ulBodyOffset); ar.WriteString(strOutLine); ar.WriteString("===============================\n"); - if (m_ProcTable[nIndexOfProc].m_ulType & P_CONDITIONAL) { + if (m_ProcTable[nIndexOfProc].m_ulType & P_CONDITIONAL) + { ar.WriteString("Condition\n"); ar.WriteString("===============================\n"); - for(INT_PTR i = 0; i < m_Conditions.at(nIndexOfProc).GetSize(); i++) { + for(INT_PTR i = 0; i < m_Conditions.at(nIndexOfProc).GetSize(); i++) + { strOutLine.Format("0x%08X: ", m_Conditions.at(nIndexOfProc).at(i).m_ulOffset); ar.WriteString(strOutLine); m_Conditions.at(nIndexOfProc).at(i).StoreTree(ar, 0, 0); @@ -36,7 +35,8 @@ void CFalloutScript::StoreTree(CArchive& ar) ar.WriteString("===============================\n"); } - for(INT_PTR i = 0; i < m_ProcBodies.at(nIndexOfProc).GetSize(); i++) { + for(INT_PTR i = 0; i < m_ProcBodies.at(nIndexOfProc).GetSize(); i++) + { strOutLine.Format("0x%08X: ", m_ProcBodies.at(nIndexOfProc).at(i).m_ulOffset); ar.WriteString(strOutLine); m_ProcBodies.at(nIndexOfProc).at(i).StoreTree(ar, 0, 0); @@ -53,11 +53,8 @@ void CFalloutScript::StoreSource(CArchive& ar) ar.WriteString("\n"); ar.WriteString("\n"); StoreDeclarations(ar); -// StoreTree(ar); } - - void CFalloutScript::StoreDefinitions(CArchive& ar) { CString c_strBogusProcedureName(".............."); @@ -71,7 +68,8 @@ void CFalloutScript::StoreDefinitions(CArchive& ar) CString strDefinition; ULONG ulVarValue; - if (nNamesCount != nDefinitionsCount) { + if (nNamesCount != nDefinitionsCount) + { ar.WriteString("/*******************************************************\n"); ar.WriteString("* Some unreferenced imported varables found. *\n"); ar.WriteString("* Because of it it is impossible to specify *\n"); @@ -79,12 +77,14 @@ void CFalloutScript::StoreDefinitions(CArchive& ar) ar.WriteString("*******************************************************/\n"); ar.WriteString("\n"); - for(INT_PTR i = 0; i < m_GlobalVar.GetSize(); i++) { + for(INT_PTR i = 0; i < m_GlobalVar.GetSize(); i++) + { ulVarValue = m_GlobalVar.at(i).GetArgument(); strDefinition += "variable "; strDefinition += m_GlobalVarsNames.at(i); - switch(m_GlobalVar.at(i).GetOperator()) { + switch(m_GlobalVar.at(i).GetOperator()) + { case COpcode::O_STRINGOP: strDefinition.Format(strDefinition + " := \"%s\"", m_Stringspace[ulVarValue].c_str()); break; @@ -94,16 +94,17 @@ void CFalloutScript::StoreDefinitions(CArchive& ar) break; case COpcode::O_INTOP: - if (ulVarValue != 0) { + if (ulVarValue != 0) + { strDefinition.Format(strDefinition + " := %d", (long)ulVarValue); } - break; } strDefinition += ";"; - if ((m_GlobalVar.at(i).GetOperator() == COpcode::O_INTOP) && (ulVarValue & 0x80000000)) { + if ((m_GlobalVar.at(i).GetOperator() == COpcode::O_INTOP) && (ulVarValue & 0x80000000)) + { strDefinition.Format(strDefinition + " /* (%d) */", ulVarValue); } @@ -133,14 +134,18 @@ void CFalloutScript::StoreDefinitions(CArchive& ar) OutDefObject currentOut = OUT_NOTHING; CProcDescriptor procDescriptor; - for(INT_PTR i = 0; i < m_Namespace.GetSize(); i++) { + for(INT_PTR i = 0; i < m_Namespace.GetSize(); i++) + { ulNameOffset = m_Namespace.GetOffsetByIndex(i); - if (i == 0) { - if (m_Namespace[ulNameOffset] == c_strBogusProcedureName.c_str()) { + if (i == 0) + { + if (m_Namespace[ulNameOffset] == c_strBogusProcedureName.c_str()) + { continue; } - else { + else + { std::string msg = "Warning: Bogus procedure with name "; msg += c_strBogusProcedureName; msg += " not found at expected location\n"; @@ -148,36 +153,45 @@ void CFalloutScript::StoreDefinitions(CArchive& ar) } } - if (m_Definitions.Lookup(ulNameOffset, defObject)) { + if (m_Definitions.Lookup(ulNameOffset, defObject)) + { strDefinition = ""; - switch(defObject.m_ObjectType) { + switch(defObject.m_ObjectType) + { case CDefObject::OBJECT_PROCEDURE: procDescriptor = m_ProcTable[defObject.m_ulProcIndex]; - if (procDescriptor.m_ulType & P_IMPORT) { + if (procDescriptor.m_ulType & P_IMPORT) + { strDefinition = "import "; currentOut = OUT_IMPORTED_PROCEDURE; } - else if (procDescriptor.m_ulType & P_EXPORT) { + else if (procDescriptor.m_ulType & P_EXPORT) + { strDefinition = "export "; currentOut = OUT_EXPORTED_PROCEDURE; } - else { + else + { currentOut = OUT_PROCEDURE; } strDefinition += "procedure "; strDefinition += m_Namespace[ulNameOffset]; - if (procDescriptor.m_ulNumArgs != 0) { + if (procDescriptor.m_ulNumArgs != 0) + { strDefinition += "("; - for(ULONG i = 0; i < procDescriptor.m_ulNumArgs; i++) { - if (i == 0) { + for(ULONG i = 0; i < procDescriptor.m_ulNumArgs; i++) + { + if (i == 0) + { strDefinition.Format(strDefinition + "variable " + c_strArgumentTemplate.c_str(), i); } - else { + else + { strDefinition.Format(strDefinition + ", variable " + c_strArgumentTemplate.c_str(), i); } } @@ -187,30 +201,36 @@ void CFalloutScript::StoreDefinitions(CArchive& ar) strDefinition += ";"; - if (procDescriptor.m_ulType & P_NOTIMPLEMENTED) { + if (procDescriptor.m_ulType & P_NOTIMPLEMENTED) + { strDefinition += " /* Prodedure defined, but not implemented */"; } break; case CDefObject::OBJECT_VARIABLE: - if (defObject.m_ulAttributes & V_IMPORT) { + if (defObject.m_ulAttributes & V_IMPORT) + { strDefinition = "import "; currentOut = OUT_IMPORTED_VARIABLE; } - else if (defObject.m_ulAttributes & V_EXPORT) { + else if (defObject.m_ulAttributes & V_EXPORT) + { strDefinition = "export "; currentOut = OUT_EXPORTED_VARIABLE; } - else { + else + { currentOut = OUT_GLOBAL_VARIABLE; } strDefinition += "variable "; strDefinition += m_Namespace[ulNameOffset]; - if (!(defObject.m_ulAttributes & V_IMPORT)) { - switch(defObject.m_ulAttributes & 0xFFFF) { + if (!(defObject.m_ulAttributes & V_IMPORT)) + { + switch(defObject.m_ulAttributes & 0xFFFF) + { case COpcode::O_STRINGOP: strDefinition.Format(strDefinition + " := \"%s\"", m_Stringspace[defObject.m_ulVarValue].c_str()); break; @@ -220,29 +240,27 @@ void CFalloutScript::StoreDefinitions(CArchive& ar) break; case COpcode::O_INTOP: - if (defObject.m_ulVarValue != 0) { + if (defObject.m_ulVarValue != 0) + { strDefinition.Format(strDefinition + " := %d", (long)defObject.m_ulVarValue); } - break; } } strDefinition += ";"; - //if (((defObject.m_ulAttributes & 0xFFFF) == COpcode::O_INTOP) && - // (defObject.m_ulVarValue & 0x80000000)) { - // strDefinition.Format(strDefinition + " /* (%d) */", defObject.m_ulVarValue); - //} } } - else { + else + { currentOut = OUT_UNKNOWN_VARIABLE; strDefinition = "/* ?import? variable "; strDefinition+= m_Namespace.GetStringByIndex(i) + "; */"; } - if ((currentOut != lastOut) && (lastOut != OUT_NOTHING)) { + if ((currentOut != lastOut) && (lastOut != OUT_NOTHING)) + { ar.WriteString("\n"); } @@ -261,23 +279,27 @@ void CFalloutScript::StoreDeclarations(CArchive& ar) CString strOutLine; - for(INT_PTR i = 0; i < m_ProcTable.GetSize(); i++) { + for(INT_PTR i = 0; i < m_ProcTable.GetSize(); i++) + { printf(" Procedure: %d\r", i); // Bogus procedure - if ((i == 0) && (m_Namespace[m_ProcTable[i].m_ulNameOffset] == c_strBogusProcedureName.c_str())) { - continue; + if ((i == 0) && (m_Namespace[m_ProcTable[i].m_ulNameOffset] == c_strBogusProcedureName.c_str())) + { + continue; } // Empty procedure - if (m_ProcTable.GetSizeOfProc(i) == 0) { + if (m_ProcTable.GetSizeOfProc(i) == 0) + { ar.WriteString("/*******************************************************\n"); ar.WriteString("* Found Procedure without body. *\n"); ar.WriteString("* *\n"); strOutLine = "* Name: "; strOutLine+= m_Namespace[m_ProcTable[i].m_ulNameOffset]; - while(strOutLine.GetLength() < 55) { + while(strOutLine.GetLength() < 55) + { strOutLine += " "; } @@ -286,15 +308,19 @@ void CFalloutScript::StoreDeclarations(CArchive& ar) ar.WriteString("* *\n"); - if (!(m_ProcTable[i].m_ulType & P_NOTIMPLEMENTED)) { + if (!(m_ProcTable[i].m_ulType & P_NOTIMPLEMENTED)) + { ar.WriteString("* Other possible name(s): *\n"); - for(INT_PTR j = i + 1; j < m_ProcTable.GetSize(); j++) { - if (m_ProcTable[j].m_ulBodyOffset == m_ProcTable[i].m_ulBodyOffset) { + for(INT_PTR j = i + 1; j < m_ProcTable.GetSize(); j++) + { + if (m_ProcTable[j].m_ulBodyOffset == m_ProcTable[i].m_ulBodyOffset) + { strOutLine = "* "; strOutLine+= m_Namespace[m_ProcTable[j].m_ulNameOffset]; - while(strOutLine.GetLength() < 55) { + while(strOutLine.GetLength() < 55) + { strOutLine += " "; } @@ -304,7 +330,8 @@ void CFalloutScript::StoreDeclarations(CArchive& ar) } } } - else { + else + { ar.WriteString("* Not implemented *\n"); } @@ -317,24 +344,30 @@ void CFalloutScript::StoreDeclarations(CArchive& ar) // 'Normal' procedure CProcDescriptor procDescriptor = m_ProcTable[i]; - if (procDescriptor.m_ulType & P_CRITICAL) { + if (procDescriptor.m_ulType & P_CRITICAL) + { strOutLine = "critical "; } - else { + else + { strOutLine = ""; } strOutLine += "procedure "; strOutLine+= m_Namespace[m_ProcTable[i].m_ulNameOffset]; - if (procDescriptor.m_ulNumArgs != 0) { + if (procDescriptor.m_ulNumArgs != 0) + { strOutLine += "("; - for(ULONG i = 0; i < procDescriptor.m_ulNumArgs; i++) { - if (i == 0) { + for(ULONG i = 0; i < procDescriptor.m_ulNumArgs; i++) + { + if (i == 0) + { strOutLine.Format(strOutLine + "variable " + c_strArgumentTemplate.c_str(), i); } - else { + else + { strOutLine.Format(strOutLine + ", variable " + c_strArgumentTemplate.c_str(), i); } } @@ -342,10 +375,12 @@ void CFalloutScript::StoreDeclarations(CArchive& ar) strOutLine += ")"; } - if (procDescriptor.m_ulType & P_TIMED) { + if (procDescriptor.m_ulType & P_TIMED) + { strOutLine.Format(strOutLine + " in %d", procDescriptor.m_ulTime); } - else if (procDescriptor.m_ulType & P_CONDITIONAL) { + else if (procDescriptor.m_ulType & P_CONDITIONAL) + { strOutLine.Format(strOutLine + " when (%s)", GetSource(m_Conditions.at(i).at(0), FALSE, procDescriptor.m_ulNumArgs).c_str()); } @@ -356,42 +391,47 @@ void CFalloutScript::StoreDeclarations(CArchive& ar) INT_PTR nIndentLevel = 0; CNode::Type prevNodeType = CNode::TYPE_NORMAL; - for(INT_PTR nNodeIndex = 0; nNodeIndex < m_ProcBodies.at(i).GetSize(); nNodeIndex++) { - if (m_ProcBodies.at(i).at(nNodeIndex).m_Type == CNode::TYPE_BEGIN_OF_BLOCK) { - /*if (((nNodeIndex - 1 > 0) && (m_ProcBodies.at(i).at(nNodeIndex - 1).m_Type == CNode::TYPE_END_OF_BLOCK)) && - ((nNodeIndex + 1 <= m_ProcBodies.at(i).GetSize()) && (m_ProcBodies.at(i)[nNodeIndex + 1].m_Opcode.GetOperator() == COpcode::O_IF))) { - ar.WriteString(GetIndentString(nIndentLevel) + "else\n"); - } - else */if ((nNodeIndex - 1 > 0) && (m_ProcBodies.at(i).at(nNodeIndex - 1).m_Type == CNode::TYPE_END_OF_BLOCK)) { + for(INT_PTR nNodeIndex = 0; nNodeIndex < m_ProcBodies.at(i).GetSize(); nNodeIndex++) + { + if (m_ProcBodies.at(i).at(nNodeIndex).m_Type == CNode::TYPE_BEGIN_OF_BLOCK) + { + if ((nNodeIndex - 1 > 0) && (m_ProcBodies.at(i).at(nNodeIndex - 1).m_Type == CNode::TYPE_END_OF_BLOCK)) + { ar.WriteString(GetIndentString(nIndentLevel) + "else begin\n"); - nIndentLevel++; + nIndentLevel++; } - else { + else + { ar.WriteString("begin\n"); nIndentLevel++; } prevNodeType = CNode::TYPE_BEGIN_OF_BLOCK; } - else if (m_ProcBodies.at(i).at(nNodeIndex).m_Type == CNode::TYPE_END_OF_BLOCK) { + else if (m_ProcBodies.at(i).at(nNodeIndex).m_Type == CNode::TYPE_END_OF_BLOCK) + { nIndentLevel--; ar.WriteString(GetIndentString(nIndentLevel) + "end\n"); prevNodeType = CNode::TYPE_END_OF_BLOCK; } - else { + else + { prevNodeType = CNode::TYPE_NORMAL; WORD wOperator = m_ProcBodies.at(i).at(nNodeIndex).m_Opcode.GetOperator(); if ((m_ProcBodies.at(i).at(nNodeIndex).m_Opcode.GetAttributes().m_Type == COpcode::COpcodeAttributes::TYPE_EXPRESSION) && - (wOperator != COpcode::O_STRINGOP) && (wOperator != COpcode::O_FLOATOP) && (wOperator != COpcode::O_INTOP)) { + (wOperator != COpcode::O_STRINGOP) && (wOperator != COpcode::O_FLOATOP) && (wOperator != COpcode::O_INTOP)) + { printf("Warning: Result of expression is left in stack. (Opcode 0x%04X at 0x%08X)\n", wOperator, m_ProcBodies.at(i).at(nNodeIndex).m_ulOffset); } - switch(wOperator) { + switch(wOperator) + { case COpcode::O_STRINGOP: case COpcode::O_FLOATOP: case COpcode::O_INTOP: { - if (!bLocalVar) { + if (!bLocalVar) + { printf("Warning: Free value found. Converted to definition of local variable\n"); } @@ -408,15 +448,22 @@ void CFalloutScript::StoreDeclarations(CArchive& ar) break; case COpcode::O_WHILE: - if (m_ProcBodies.at(i).at(nNodeIndex).m_Type == CNode::TYPE_FOR_LOOP) { + if (m_ProcBodies.at(i).at(nNodeIndex).m_Type == CNode::TYPE_FOR_LOOP) + { CString str = GetIndentString(nIndentLevel) + "for ("; - for (INT_PTR j = 0; j < m_ProcBodies.at(i).at(nNodeIndex).m_Arguments.GetSize(); j++) { - if (j > 0) str += "; "; + for (INT_PTR j = 0; j < m_ProcBodies.at(i).at(nNodeIndex).m_Arguments.GetSize(); j++) + { + if (j > 0) + { + str += "; "; + } str += GetSource(m_ProcBodies.at(i).at(nNodeIndex).m_Arguments.at(j), FALSE, procDescriptor.m_ulNumArgs); } str += ") "; ar.WriteString(str); - } else { + } + else + { ar.WriteString(GetIndentString(nIndentLevel) + GetSource(m_ProcBodies.at(i).at(nNodeIndex), FALSE, procDescriptor.m_ulNumArgs) + " do "); } break; @@ -425,7 +472,8 @@ void CFalloutScript::StoreDeclarations(CArchive& ar) ar.WriteString(GetIndentString(nIndentLevel) + GetSource(m_ProcBodies.at(i).at(nNodeIndex), FALSE, procDescriptor.m_ulNumArgs) + ";\n"); if ((m_ProcBodies.at(i).at(nNodeIndex).m_Type != CNode::TYPE_BEGIN_OF_BLOCK) && - (m_ProcBodies.at(i).at(nNodeIndex).m_Type != CNode::TYPE_END_OF_BLOCK)) { + (m_ProcBodies.at(i).at(nNodeIndex).m_Type != CNode::TYPE_END_OF_BLOCK)) + { bLocalVar = FALSE; } } @@ -443,7 +491,8 @@ CString CFalloutScript::GetSource(CNode& node, BOOL bLabel, ULONG ulNumArgs) CString strResult("Damn!!!"); - if (node.m_Type == CNode::TYPE_OMITTED_ARGUMENT) { + if (node.m_Type == CNode::TYPE_OMITTED_ARGUMENT) + { return CString("/* Omitted argument */"); } @@ -451,27 +500,35 @@ CString CFalloutScript::GetSource(CNode& node, BOOL bLabel, ULONG ulNumArgs) ULONG ulArgument = node.m_Opcode.GetArgument(); // sslc additions: - if (wOperator == COpcode::O_JMP) { - if (node.m_Type == CNode::TYPE_BREAK) { + if (wOperator == COpcode::O_JMP) + { + if (node.m_Type == CNode::TYPE_BREAK) + { return CString("break"); - } else if (node.m_Type == CNode::TYPE_CONTINUE) { + } + else if (node.m_Type == CNode::TYPE_CONTINUE) + { return CString("continue"); } } - switch(wOperator) { + switch(wOperator) + { case COpcode::O_STRINGOP: - try { - if (bLabel) { + try + { + if (bLabel) + { strResult = m_Namespace[ulArgument]; } - else { + else + { strResult = "\""; strResult += m_Stringspace[ulArgument] + "\""; } } catch(UserException& e) - { + { printf("Warning: Restoration after exception. Object name replaced with '/* Fake object name */'\n"); strResult = "/* Fake object name */"; } @@ -483,17 +540,20 @@ CString CFalloutScript::GetSource(CNode& node, BOOL bLabel, ULONG ulNumArgs) break; case COpcode::O_INTOP: - try { - if (bLabel) { + try + { + if (bLabel) + { strResult = m_Namespace[ulArgument]; -// strResult = m_Namespace[m_ProcTable[ulArgument].m_ulNameOffset]; } - else { + else + { strResult.Format("%d", (long)ulArgument); } } - catch(UserException& e) { + catch(UserException& e) + { printf("Warning: Restoration after exception. Object name replaced with '/* Fake object name */'\n"); strResult = "/* Fake object name */"; } @@ -509,12 +569,14 @@ CString CFalloutScript::GetSource(CNode& node, BOOL bLabel, ULONG ulNumArgs) break; case COpcode::O_FETCH_GLOBAL: - if (node.m_Arguments.at(0).m_Opcode.GetOperator() != COpcode::O_INTOP) { + if (node.m_Arguments.at(0).m_Opcode.GetOperator() != COpcode::O_INTOP) + { printf("Error: Invalid argument to O_FETCH_GLOBAL opcode\n"); AfxThrowUserException(); } - if (INT_PTR(node.m_Arguments.at(0).m_Opcode.GetArgument()) > m_GlobalVarsNames.GetUpperBound()) { + if (INT_PTR(node.m_Arguments.at(0).m_Opcode.GetArgument()) > m_GlobalVarsNames.GetUpperBound()) + { printf("Error: Invalid index of global variable\n"); AfxThrowUserException(); } @@ -523,12 +585,14 @@ CString CFalloutScript::GetSource(CNode& node, BOOL bLabel, ULONG ulNumArgs) break; case COpcode::O_STORE_GLOBAL: - if (node.m_Arguments.at(1).m_Opcode.GetOperator() != COpcode::O_INTOP) { + if (node.m_Arguments.at(1).m_Opcode.GetOperator() != COpcode::O_INTOP) + { printf("Error: Invalid argument to O_STORE_GLOBAL opcode\n"); AfxThrowUserException(); } - if (INT_PTR(node.m_Arguments.at(1).m_Opcode.GetArgument()) > m_GlobalVarsNames.GetUpperBound()) { + if (INT_PTR(node.m_Arguments.at(1).m_Opcode.GetArgument()) > m_GlobalVarsNames.GetUpperBound()) + { printf("Error: Invalid index of global variable\n"); AfxThrowUserException(); } @@ -540,7 +604,8 @@ CString CFalloutScript::GetSource(CNode& node, BOOL bLabel, ULONG ulNumArgs) { WORD wOperator = node.m_Arguments.at(0).m_Opcode.GetOperator(); - if ((wOperator != COpcode::O_STRINGOP) && (wOperator != COpcode::O_INTOP)) { + if ((wOperator != COpcode::O_STRINGOP) && (wOperator != COpcode::O_INTOP)) + { printf("Error: Invalid argument to O_FETCH_EXTERNAL opcode\n"); AfxThrowUserException(); } @@ -553,7 +618,8 @@ CString CFalloutScript::GetSource(CNode& node, BOOL bLabel, ULONG ulNumArgs) { WORD wOperator = node.m_Arguments.at(1).m_Opcode.GetOperator(); - if ((wOperator != COpcode::O_STRINGOP) && (wOperator != COpcode::O_INTOP)) { + if ((wOperator != COpcode::O_STRINGOP) && (wOperator != COpcode::O_INTOP)) + { printf("Error: Invalid argument to O_STORE_EXTERNAL opcode\n"); AfxThrowUserException(); } @@ -563,7 +629,8 @@ CString CFalloutScript::GetSource(CNode& node, BOOL bLabel, ULONG ulNumArgs) break; case COpcode::O_FETCH: - if (node.m_Arguments.at(0).m_Opcode.GetOperator() != COpcode::O_INTOP) { + if (node.m_Arguments.at(0).m_Opcode.GetOperator() != COpcode::O_INTOP) + { printf("Error: Invalid argument to O_FETCH opcode\n"); AfxThrowUserException(); } @@ -571,10 +638,12 @@ CString CFalloutScript::GetSource(CNode& node, BOOL bLabel, ULONG ulNumArgs) { ULONG ulVarNum = node.m_Arguments.at(0).m_Opcode.GetArgument(); - if (ulVarNum < ulNumArgs) { + if (ulVarNum < ulNumArgs) + { strResult.Format(c_strArgumentTemplate.c_str(), ulVarNum); } - else { + else + { strResult.Format(c_strLocalVarTemplate.c_str(), ulVarNum); } } @@ -582,16 +651,20 @@ CString CFalloutScript::GetSource(CNode& node, BOOL bLabel, ULONG ulNumArgs) break; case COpcode::O_STORE: - if (node.m_Arguments.at(1).m_Opcode.GetOperator() != COpcode::O_INTOP) { + if (node.m_Arguments.at(1).m_Opcode.GetOperator() != COpcode::O_INTOP) + { printf("Error: Invalid argument to O_STORE opcode\n"); AfxThrowUserException(); } { ULONG ulVarNum = node.m_Arguments.at(1).m_Opcode.GetArgument(); - if (ulVarNum < ulNumArgs) { + if (ulVarNum < ulNumArgs) + { strResult.Format(c_strArgumentTemplate.c_str(), ulVarNum); - } else { + } + else + { strResult.Format(c_strLocalVarTemplate.c_str(), ulVarNum); } strResult += " := "; @@ -603,7 +676,8 @@ CString CFalloutScript::GetSource(CNode& node, BOOL bLabel, ULONG ulNumArgs) case COpcode::O_POP: strResult = GetSource(node.m_Arguments.at(0), FALSE, ulNumArgs); - if (node.m_Arguments.at(0).m_Opcode.GetOperator() == COpcode::O_CALL) { + if (node.m_Arguments.at(0).m_Opcode.GetOperator() == COpcode::O_CALL) + { strResult = "call "; strResult += strResult; } @@ -611,38 +685,43 @@ CString CFalloutScript::GetSource(CNode& node, BOOL bLabel, ULONG ulNumArgs) break; case COpcode::O_CALL: - if (node.m_Arguments.at(node.m_Arguments.GetUpperBound()).m_Opcode.GetOperator() == COpcode::O_INTOP) { + if (node.m_Arguments.at(node.m_Arguments.GetUpperBound()).m_Opcode.GetOperator() == COpcode::O_INTOP) + { strResult = m_Namespace[m_ProcTable[node.m_Arguments.at(node.m_Arguments.GetUpperBound()).m_Opcode.GetArgument()].m_ulNameOffset]; } - else { + else + { strResult = GetSource(node.m_Arguments.at(node.m_Arguments.GetUpperBound()), FALSE, ulNumArgs); } - //if (node.m_Arguments.GetSize() > 2) { - strResult += "("; + strResult += "("; - for(INT_PTR nArgIndex = 0; nArgIndex < node.m_Arguments.GetUpperBound() - 1; nArgIndex++) { - if (nArgIndex == 0) { - strResult += GetSource(node.m_Arguments.at(nArgIndex), FALSE, ulNumArgs); - } - else { - strResult += ", "; - strResult += GetSource(node.m_Arguments.at(nArgIndex), FALSE, ulNumArgs); - } + for(INT_PTR nArgIndex = 0; nArgIndex < node.m_Arguments.GetUpperBound() - 1; nArgIndex++) + { + if (nArgIndex == 0) + { + strResult += GetSource(node.m_Arguments.at(nArgIndex), FALSE, ulNumArgs); } + else + { + strResult += ", "; + strResult += GetSource(node.m_Arguments.at(nArgIndex), FALSE, ulNumArgs); + } + } - strResult += ")"; - //} + strResult += ")"; break; case COpcode::O_CALL_AT: strResult = "call "; - if (node.m_Arguments.at(1).m_Opcode.GetOperator() == COpcode::O_INTOP) { + if (node.m_Arguments.at(1).m_Opcode.GetOperator() == COpcode::O_INTOP) + { strResult += m_Namespace[m_ProcTable[node.m_Arguments.at(1).m_Opcode.GetArgument()].m_ulNameOffset]; } - else { + else + { strResult += GetSource(node.m_Arguments.at(1), FALSE, ulNumArgs); } @@ -653,10 +732,12 @@ CString CFalloutScript::GetSource(CNode& node, BOOL bLabel, ULONG ulNumArgs) case COpcode::O_CALL_CONDITION: strResult = "call "; - if (node.m_Arguments.at(1).m_Opcode.GetOperator() == COpcode::O_INTOP) { + if (node.m_Arguments.at(1).m_Opcode.GetOperator() == COpcode::O_INTOP) + { strResult += m_Namespace[m_ProcTable[node.m_Arguments.at(1).m_Opcode.GetArgument()].m_ulNameOffset]; } - else { + else + { strResult += GetSource(node.m_Arguments.at(1), FALSE, ulNumArgs); } @@ -668,11 +749,14 @@ CString CFalloutScript::GetSource(CNode& node, BOOL bLabel, ULONG ulNumArgs) strResult = "addRegion "; strResult += GetSource(node.m_Arguments.at(0), FALSE, ulNumArgs) + " { "; - for(INT_PTR nArgIndex = 1; nArgIndex < node.m_Arguments.GetUpperBound(); nArgIndex++) { - if (nArgIndex == 1) { + for(INT_PTR nArgIndex = 1; nArgIndex < node.m_Arguments.GetUpperBound(); nArgIndex++) + { + if (nArgIndex == 1) + { strResult += GetSource(node.m_Arguments.at(nArgIndex), FALSE, ulNumArgs); } - else { + else + { strResult += ", "; strResult += GetSource(node.m_Arguments.at(nArgIndex), FALSE, ulNumArgs); } @@ -707,11 +791,13 @@ CString CFalloutScript::GetSource(CNode& node, BOOL bLabel, ULONG ulNumArgs) case COpcode::O_SAYOPTION: { - if (node.m_Arguments.at(1).m_Opcode.GetOperator() == COpcode::O_INTOP) { + if (node.m_Arguments.at(1).m_Opcode.GetOperator() == COpcode::O_INTOP) + { ULONG aulProcArg[2] = { 2 }; strResult = GetSource(node, bLabel, ulNumArgs, aulProcArg, 1); } - else { + else + { strResult = "sayOption("; strResult += GetSource(node.m_Arguments.at(0), FALSE, ulNumArgs) + ", " + GetSource(node.m_Arguments.at(1), FALSE, ulNumArgs) + ")"; } @@ -776,7 +862,8 @@ CString CFalloutScript::GetSource(CNode& node, BOOL bLabel, ULONG ulNumArgs) break; case COpcode::O_CANCEL: - if (node.m_Arguments.at(0).m_Opcode.GetOperator() != COpcode::O_INTOP) { + if (node.m_Arguments.at(0).m_Opcode.GetOperator() != COpcode::O_INTOP) + { printf("Error: Invalid argument to O_CANCEL opcode\n"); AfxThrowUserException(); } @@ -786,49 +873,54 @@ CString CFalloutScript::GetSource(CNode& node, BOOL bLabel, ULONG ulNumArgs) break; case COpcode::O_NEGATE: - if (node.m_Arguments.at(0).m_Opcode.GetAttributes().m_Category == COpcode::COpcodeAttributes::CATEGORY_INFIX) { + if (node.m_Arguments.at(0).m_Opcode.GetAttributes().m_Category == COpcode::COpcodeAttributes::CATEGORY_INFIX) + { strResult = "-("; strResult += GetSource(node.m_Arguments.at(0), bLabel, ulNumArgs) + ")"; } - else { + else + { strResult = "-"; strResult += GetSource(node.m_Arguments.at(0), bLabel, ulNumArgs); } break; -// case COpcode::O_SETGLOBALMOUSEFUNC: -// printf("Warning: Fallout2 don'\t have implemented handler for O_SETGLOBALMOUSEFUNC opcode\n"); -// continue default execution - default: COpcode::COpcodeAttributes attributes = node.m_Opcode.GetAttributes(); ULONG *procArgs = attributes.m_procArgs; ULONG numProcArgs = attributes.m_numProcArgs; - if (numProcArgs > 0) { + if (numProcArgs > 0) + { strResult = GetSource(node, bLabel, ulNumArgs, procArgs, numProcArgs); break; } - if (node.m_Type == CNode::TYPE_CONDITIONAL_EXPRESSION) { - if (node.m_Arguments.GetSize() != 3) { + if (node.m_Type == CNode::TYPE_CONDITIONAL_EXPRESSION) + { + if (node.m_Arguments.GetSize() != 3) + { printf("Error: Invalid number of arguments in conditional expression\n"); AfxThrowUserException(); } CString sPostfix[] = { CString(" if "), CString(" else "), CString("")}; strResult = ""; - for(INT_PTR i = 0; i < node.m_Arguments.GetSize(); i++) { + for(INT_PTR i = 0; i < node.m_Arguments.GetSize(); i++) + { bool bParens = ArgNeedParens(node, node.m_Arguments.at(i), CFalloutScript::RIGHT_ASSOC); strResult += (bParens ? CString("(") : CString("")) + GetSource(node.m_Arguments.at(i), bLabel, ulNumArgs) + (bParens ? ")" : "") + sPostfix[i]; } break; } - switch(attributes.m_Category) { + switch(attributes.m_Category) + { case COpcode::COpcodeAttributes::CATEGORY_INFIX: - if (ArgNeedParens(node, node.m_Arguments.at(0), CFalloutScript::LEFT_ASSOC)) { + if (ArgNeedParens(node, node.m_Arguments.at(0), CFalloutScript::LEFT_ASSOC)) + { strResult = "("; strResult += GetSource(node.m_Arguments.at(0), FALSE, ulNumArgs) + ")"; } - else { + else + { strResult = GetSource(node.m_Arguments.at(0), FALSE, ulNumArgs); } @@ -836,11 +928,13 @@ CString CFalloutScript::GetSource(CNode& node, BOOL bLabel, ULONG ulNumArgs) strResult += attributes.m_strName + " "; - if (ArgNeedParens(node, node.m_Arguments.at(1), CFalloutScript::RIGHT_ASSOC)) { + if (ArgNeedParens(node, node.m_Arguments.at(1), CFalloutScript::RIGHT_ASSOC)) + { strResult += "("; strResult += GetSource(node.m_Arguments.at(1), FALSE, ulNumArgs) + ")"; } - else { + else + { strResult += GetSource(node.m_Arguments.at(1), FALSE, ulNumArgs); } @@ -849,18 +943,23 @@ CString CFalloutScript::GetSource(CNode& node, BOOL bLabel, ULONG ulNumArgs) case COpcode::COpcodeAttributes::CATEGORY_PREFIX: strResult = attributes.m_strName; - if (node.m_Arguments.GetSize() != 0) { - if (wOperator == COpcode::O_IF || wOperator == COpcode::O_WHILE) { + if (node.m_Arguments.GetSize() != 0) + { + if (wOperator == COpcode::O_IF || wOperator == COpcode::O_WHILE) + { strResult += " "; } strResult += "("; - for(INT_PTR i = 0; i < node.m_Arguments.GetSize(); i++) { - if (i == 0) { + for(INT_PTR i = 0; i < node.m_Arguments.GetSize(); i++) + { + if (i == 0) + { strResult += GetSource(node.m_Arguments.at(i), FALSE, ulNumArgs); } - else { + else + { strResult += ", "; strResult += GetSource(node.m_Arguments.at(i), FALSE, ulNumArgs); } @@ -883,41 +982,51 @@ CString CFalloutScript::GetSource( CNode& node, BOOL bLabel, ULONG ulNumArgs, UL CString strArgument; BOOL bIsProcArg; - for(INT_PTR nArgIndex = 0; nArgIndex < node.m_Arguments.GetSize(); nArgIndex++) { + for(INT_PTR nArgIndex = 0; nArgIndex < node.m_Arguments.GetSize(); nArgIndex++) + { bIsProcArg = FALSE; - for(ULONG ulProcArgIndex = 0; ulProcArgIndex < ulProcArgCount; ulProcArgIndex++) { - if (nArgIndex == aulProcArg[ulProcArgIndex] - 1) { + for(ULONG ulProcArgIndex = 0; ulProcArgIndex < ulProcArgCount; ulProcArgIndex++) + { + if (nArgIndex == aulProcArg[ulProcArgIndex] - 1) + { bIsProcArg = TRUE; break; } } - if (bIsProcArg) { - if (node.m_Arguments.at(nArgIndex).m_Opcode.GetOperator() == COpcode::O_INTOP) { + if (bIsProcArg) + { + if (node.m_Arguments.at(nArgIndex).m_Opcode.GetOperator() == COpcode::O_INTOP) + { try { strArgument = m_Namespace[m_ProcTable[node.m_Arguments.at(nArgIndex).m_Opcode.GetArgument()].m_ulNameOffset]; } - catch(UserException& e) { + catch(UserException& e) + { printf("Warning: Restoration after exception. Object name replaced with '/* Fake object name */'\n"); strArgument.Format("/* Fake object name: %u (%d)*/", node.m_Arguments.at(nArgIndex).m_Opcode.GetArgument(), node.m_Arguments.at(nArgIndex).m_Opcode.GetArgument()); } } - else { + else + { strArgument = GetSource(node.m_Arguments.at(nArgIndex), FALSE, ulNumArgs); } } - else { + else + { strArgument = GetSource(node.m_Arguments.at(nArgIndex), FALSE, ulNumArgs); } - if (nArgIndex == 0) { + if (nArgIndex == 0) + { strResult += strArgument; } - else { + else + { strResult += ", "; strResult += strArgument; } @@ -932,7 +1041,8 @@ CString CFalloutScript::GetIndentString(INT_PTR nLevel) { CString strResult; - for(; nLevel > 0; nLevel--) { + for(; nLevel > 0; nLevel--) + { strResult += g_strIndentFill; } @@ -941,7 +1051,8 @@ CString CFalloutScript::GetIndentString(INT_PTR nLevel) int CFalloutScript::GetPriority(WORD wOperator) { - switch(wOperator) { + switch(wOperator) + { case COpcode::O_IF: return 0; case COpcode::O_OR: @@ -976,7 +1087,8 @@ int CFalloutScript::GetPriority(WORD wOperator) CFalloutScript::Assoc CFalloutScript::GetAssociation(WORD wOperator) { - switch(wOperator) { + switch(wOperator) + { case COpcode::O_GREATER: case COpcode::O_LESS: case COpcode::O_GREATER_EQUAL: diff --git a/Namespace.cpp b/Namespace.cpp index ba42e5a..41eda77 100644 --- a/Namespace.cpp +++ b/Namespace.cpp @@ -25,88 +25,87 @@ CNamespace::~CNamespace() void CNamespace::Serialize(CArchive& ar) { - //if (ar.IsStoring()) { - if (false) + m_Map.RemoveAll(); + m_Order.RemoveAll(); + + ULONG ulLength; + + if (ReadMSBULong(ar, ulLength) != 4) { - //ASSERT(FALSE); + printf("Error: Unable read length of namespace\n"); + AfxThrowUserException(); } - else { - m_Map.RemoveAll(); - m_Order.RemoveAll(); - ULONG ulLength; + if (ulLength == 0xFFFFFFFF) return; - if (ReadMSBULong(ar, ulLength) != 4) + ULONG ulTotalRead = 0; + WORD wLengthOfString; + LPTSTR lpszNewString; + + while(ulTotalRead < ulLength) + { + CString strNewString; + + if (ReadMSBWord(ar, wLengthOfString) != 2) { - printf("Error: Unable read length of namespace\n"); + printf("Error: Unable read length of string\n"); + AfxThrowUserException(); + }; + + if ((wLengthOfString < 2) || (wLengthOfString & 0x0001)) + { + printf("Error: Invalid length of string\n"); AfxThrowUserException(); } - if (ulLength != 0xFFFFFFFF) + lpszNewString = strNewString.GetBuffer(wLengthOfString); + + if (ar.Read(lpszNewString, wLengthOfString) != wLengthOfString) { - ULONG ulTotalRead = 0; - WORD wLengthOfString; - LPTSTR lpszNewString; + strNewString.ReleaseBufferSetLength(0); + printf("Error: Unable read string in namespace\n"); + AfxThrowUserException(); + } - while(ulTotalRead < ulLength) - { - CString strNewString; + if ((lpszNewString[wLengthOfString - 1] != '\x00') && (lpszNewString[wLengthOfString - 2] != '\x00')) + { + strNewString.ReleaseBufferSetLength(0); + printf("Error: Invalid end of string in namespace\n"); + AfxThrowUserException(); + } - if (ReadMSBWord(ar, wLengthOfString) != 2) { - printf("Error: Unable read length of string\n"); - AfxThrowUserException(); - }; - - if ((wLengthOfString < 2) || (wLengthOfString & 0x0001)) { - printf("Error: Invalid length of string\n"); - AfxThrowUserException(); - } - - lpszNewString = strNewString.GetBuffer(wLengthOfString); - - if (ar.Read(lpszNewString, wLengthOfString) != wLengthOfString) { - strNewString.ReleaseBufferSetLength(0); - printf("Error: Unable read string in namespace\n"); - AfxThrowUserException(); - } - - if ((lpszNewString[wLengthOfString - 1] != '\x00') && (lpszNewString[wLengthOfString - 2] != '\x00')) { - strNewString.ReleaseBufferSetLength(0); - printf("Error: Invalid end of string in namespace\n"); - AfxThrowUserException(); - } - - std::string tmpString(lpszNewString); - strNewString = tmpString.c_str();//.ReleaseBuffer(); + std::string tmpString(lpszNewString); + strNewString = tmpString.c_str();//.ReleaseBuffer(); - // Convert Nongraphic Characters to Escape sequence - CString strNonGraph("\\\a\b\f\n\r\t\""); - CString strEscape("\\abfnrt\""); + // Convert Nongraphic Characters to Escape sequence + CString strNonGraph("\\\a\b\f\n\r\t\""); + CString strEscape("\\abfnrt\""); - for(int i = 0; i < strNonGraph.GetLength(); i++) - { - strNewString.Replace(CString(strNonGraph[i]), CString("\\" + strEscape[i])); - } + for(int i = 0; i < strNonGraph.GetLength(); i++) + { + strNewString.Replace(CString(strNonGraph[i]), CString("\\" + strEscape[i])); + } - m_Map.SetAt(ulTotalRead + 6, strNewString); - m_Order.Add(ulTotalRead + 6); + m_Map.SetAt(ulTotalRead + 6, strNewString); + m_Order.Add(ulTotalRead + 6); - ulTotalRead += (2 + wLengthOfString); - } + ulTotalRead += (2 + wLengthOfString); + } - ULONG ulTerminator; + ULONG ulTerminator; - if (ReadMSBULong(ar, ulTerminator) != 4) - { - printf("Error: Unable read terminator of namespace\n"); - AfxThrowUserException(); - } + if (ReadMSBULong(ar, ulTerminator) != 4) + { + printf("Error: Unable read terminator of namespace\n"); + AfxThrowUserException(); + } - if (ulTerminator != 0xFFFFFFFF) { - printf("Error: Invalid terminator of namespace\n"); - AfxThrowUserException(); - } + if (ulTerminator != 0xFFFFFFFF) + { + printf("Error: Invalid terminator of namespace\n"); + AfxThrowUserException(); + } // //For debugging only // for(INT_PTR i = 0; i < GetSize(); i++) { @@ -114,8 +113,6 @@ void CNamespace::Serialize(CArchive& ar) // } // // printf("\n"); - } - } } INT_PTR CNamespace::GetSize() const diff --git a/Namespace.h b/Namespace.h index 344cafa..41a139e 100644 --- a/Namespace.h +++ b/Namespace.h @@ -1,13 +1,11 @@ -#pragma once - #ifndef NAMESPACE_H #define NAMESPACE_H #include "Hacks/CArchive.h" #include "Hacks/CMap.h" -// CNamespace -class CNamespace : public CObject { +class CNamespace +{ public: CNamespace(); virtual ~CNamespace(); @@ -33,4 +31,4 @@ private: CDWordArray m_Order; }; -#endif +#endif //NAMESPACE_H diff --git a/Node.cpp b/Node.cpp index f97411a..6755998 100644 --- a/Node.cpp +++ b/Node.cpp @@ -1,12 +1,6 @@ -// Node.cpp : implementation file -// - #include "stdafx.h" #include "Node.h" - -// CNode - CNode::CNode(Type type) : m_ulOffset(0), m_Type(type) @@ -28,28 +22,31 @@ CNode::~CNode() CNode& CNode::operator = (const CNode& node) { - if (&node != this) { + if (&node != this) + { m_ulOffset = node.m_ulOffset; m_Opcode = node.m_Opcode; m_Type = node.m_Type; m_Arguments.Copy(node.m_Arguments); } - return (*this); } void CNode::StoreTree(CArchive& ar, int nIndent, int nIndex) { // Indent - if (nIndex != 0) { + if (nIndex != 0) + { ar.WriteString(" "); - for(int i = 0; i < nIndent; i++) { + for(int i = 0; i < nIndent; i++) + { ar.WriteString(" "); } } - switch(m_Type) { + switch(m_Type) + { case TYPE_BEGIN_OF_BLOCK: ar.WriteString("========= begin of block =========\n"); return; @@ -64,7 +61,8 @@ void CNode::StoreTree(CArchive& ar, int nIndent, int nIndex) WORD wOperator = m_Opcode.GetOperator(); ULONG ulArgument = m_Opcode.GetArgument(); - switch(wOperator) { + switch(wOperator) + { case COpcode::O_STRINGOP: case COpcode::O_INTOP: strOutLine.Format("0x%04X 0x%08x ", @@ -85,12 +83,15 @@ void CNode::StoreTree(CArchive& ar, int nIndent, int nIndex) wOperator); ar.WriteString(strOutLine); } - if (!m_Arguments.IsEmpty()) { - for(int i = 0; i < m_Arguments.GetSize(); i++) { + if (!m_Arguments.IsEmpty()) + { + for(int i = 0; i < m_Arguments.GetSize(); i++) + { m_Arguments.at(i).StoreTree(ar, nIndent + 1, i); } } - else { + else + { ar.WriteString("\n"); } @@ -99,9 +100,13 @@ void CNode::StoreTree(CArchive& ar, int nIndent, int nIndex) ULONG CNode::GetTopOffset() { if (m_Arguments.GetSize() > 0) + { return m_Arguments.at(0).GetTopOffset(); + } else + { return m_ulOffset; + } } bool CNode::IsExpression() const diff --git a/Node.h b/Node.h index 9b4495b..bc9c5fa 100644 --- a/Node.h +++ b/Node.h @@ -1,22 +1,15 @@ -#pragma once - #ifndef NODE_H #define NODE_H #include "Namespace.h" #include "Opcode.h" - -// Forward declaration of CNode class CNode; - - -// CNodeArray typedef CArray CNodeArray; +typedef CArray CArrayOfNodeArray; - -// CNode -class CNode : public CObject { +class CNode +{ public: enum Type { TYPE_NORMAL, @@ -51,15 +44,8 @@ public: CNodeArray m_Arguments; }; - -// CNodeArray -typedef CArray CNodeArray; - -// CArrayOfNodeArray -typedef CArray CArrayOfNodeArray; - // 'Begin' end 'End of block' 'End Else Begin' nodes const CNode c_NodeBeginOfBlock(CNode::TYPE_BEGIN_OF_BLOCK); const CNode c_NodeEndOfBlock(CNode::TYPE_END_OF_BLOCK); -#endif +#endif //NODE_H diff --git a/ObjectAttributes.h b/ObjectAttributes.h index 823f7c1..77e8f5b 100644 --- a/ObjectAttributes.h +++ b/ObjectAttributes.h @@ -1,9 +1,6 @@ -#pragma once - #ifndef OBJECT_ATTRIBUTES_H #define OBJECT_ATTRIBUTES_H - // ProcedureAttributes enum ProcAttributes { P_TIMED = 0x01, @@ -15,7 +12,6 @@ enum ProcAttributes { P_NOTIMPLEMENTED = 0x80000000 }; - // VariableAtributes enum VariableAtributes { V_GLOBAL = 0x00010000, @@ -23,4 +19,4 @@ enum VariableAtributes { V_EXPORT = 0x00040000 }; -#endif +#endif //OBJECT_ATTRIBUTES_H diff --git a/Opcode.cpp b/Opcode.cpp index 6e2c4d4..d761ac4 100644 --- a/Opcode.cpp +++ b/Opcode.cpp @@ -1,19 +1,12 @@ -// Opcode.cpp : implementation file -// - #include "stdafx.h" #include "Opcode.h" #include "Utility.h" - #include // Globals extern int g_nFalloutVersion; - -// COpcode - COpcode::COpcode() : m_wOperator(O_NOOP), m_ulArgument(0) @@ -41,41 +34,47 @@ COpcode& COpcode::operator = (const COpcode& opcode) void COpcode::Serialize(CArchive& ar) { - if (ReadMSBWord(ar, m_wOperator) != OPERATOR_SIZE) { + if (ReadMSBWord(ar, m_wOperator) != OPERATOR_SIZE) + { + printf("Error: Unable read opcode\n"); + AfxThrowUserException(); + } - printf("Error: Unable read opcode\n"); + if ((m_wOperator < O_OPERATOR) || + ((m_wOperator >= O_END_OP) && + (m_wOperator != O_STRINGOP) && + (m_wOperator != O_FLOATOP)&& + (m_wOperator != O_INTOP))) + { + ar.Flush(); + printf("Error: Invalid opcode at 0x%08x\n", ar.GetFile()->GetPosition() - 2); + AfxThrowUserException(); + } + + if ((m_wOperator == O_STRINGOP) || (m_wOperator == O_FLOATOP) || (m_wOperator == O_INTOP)) + { + if (ReadMSBULong(ar, m_ulArgument) != ARGUMENT_SIZE) + { + printf("Error: Unable read opcode argument\n"); AfxThrowUserException(); } - - if ((m_wOperator < O_OPERATOR) || - ((m_wOperator >= O_END_OP) && - (m_wOperator != O_STRINGOP) && - (m_wOperator != O_FLOATOP)&& - (m_wOperator != O_INTOP))) { - ar.Flush(); - printf("Error: Invalid opcode at 0x%08x\n", ar.GetFile()->GetPosition() - 2); - AfxThrowUserException(); - } - - if ((m_wOperator == O_STRINGOP) || (m_wOperator == O_FLOATOP) || (m_wOperator == O_INTOP)) { - if (ReadMSBULong(ar, m_ulArgument) != ARGUMENT_SIZE) { - printf("Error: Unable read opcode argument\n"); - AfxThrowUserException(); - } - } + } } void COpcode::Expect(CArchive& ar, WORD wOperator, BOOL bArgumentFound, ULONG ulArgument) { Serialize(ar); - if (m_wOperator != wOperator) { + if (m_wOperator != wOperator) + { printf("Error: Unexpected opcode (0x%04X expected, but 0x%04X found)\n", wOperator, m_wOperator); AfxThrowUserException(); } - if (bArgumentFound && ((m_wOperator == O_STRINGOP) || (m_wOperator == O_FLOATOP) || (m_wOperator == O_INTOP))) { - if (m_ulArgument != ulArgument) { + if (bArgumentFound && ((m_wOperator == O_STRINGOP) || (m_wOperator == O_FLOATOP) || (m_wOperator == O_INTOP))) + { + if (m_ulArgument != ulArgument) + { printf("Error: Unexpected argument of opcode. (0x%08X expected, but 0x%08X found)\n", ulArgument, m_ulArgument); AfxThrowUserException(); } @@ -87,18 +86,23 @@ void COpcode::Expect(CArchive& ar, int nCount, WORD pwOperators[]) Serialize(ar); BOOL bFound = FALSE; - for(int i = 0; i < nCount; i++) { - if (m_wOperator == pwOperators[i]) { + for(int i = 0; i < nCount; i++) + { + if (m_wOperator == pwOperators[i]) + { bFound = TRUE; break; } } - if (!bFound) { + if (!bFound) + { printf("Error: Unexpected opcode ("); - for(int i = 0; i < nCount; i++) { - if (i != 0) { + for(int i = 0; i < nCount; i++) + { + if (i != 0) + { printf(" or "); } @@ -117,10 +121,12 @@ BOOL COpcode::HasArgument() const int COpcode::GetSize() const { - if ((m_wOperator == O_STRINGOP) || (m_wOperator == O_FLOATOP) || (m_wOperator == O_INTOP)) { + if ((m_wOperator == O_STRINGOP) || (m_wOperator == O_FLOATOP) || (m_wOperator == O_INTOP)) + { return OPERATOR_SIZE + ARGUMENT_SIZE; } - else { + else + { return OPERATOR_SIZE; } } @@ -147,13 +153,16 @@ const COpcode::COpcodeAttributes COpcode::GetAttributes() const COpcodeAttributes result; - if (g_nFalloutVersion == 1) { - if (f1OpcodeAttributes.Lookup(m_wOperator, result)) { + if (g_nFalloutVersion == 1) + { + if (f1OpcodeAttributes.Lookup(m_wOperator, result)) + { return result; } } - if (!f2OpcodeAttributes.Lookup(m_wOperator, result)) { + if (!f2OpcodeAttributes.Lookup(m_wOperator, result)) + { printf("Error: Attempt to obtain attributes of unknown opcode\n"); AfxThrowUserException(); } diff --git a/Opcode.h b/Opcode.h index 5fbd382..a1e3953 100644 --- a/Opcode.h +++ b/Opcode.h @@ -5,7 +5,6 @@ #include "Hacks/CArchive.h" #include "Hacks/CMap.h" -// COpcode class COpcode { public: @@ -21,8 +20,6 @@ public: O_OPERATOR = 0x8000 }; - // const O_IF_EXPR = 1000; - enum O_Core { O_NOOP = O_OPERATOR, O_CONST, @@ -800,13 +797,15 @@ public: }; // CF2OpcodeAttributesMap - class CF2OpcodeAttributesMap : public CMap { + class CF2OpcodeAttributesMap : public CMap + { public: CF2OpcodeAttributesMap(); }; // C12OpcodeAttributesMap - class CF1OpcodeAttributesMap : public CMap { + class CF1OpcodeAttributesMap : public CMap + { public: CF1OpcodeAttributesMap(); }; @@ -838,7 +837,6 @@ private: ULONG m_ulArgument; }; -// COpcodeArray typedef CArray COpcodeArray; #endif diff --git a/OpcodeAttributes.cpp b/OpcodeAttributes.cpp index d423aae..36e8254 100644 --- a/OpcodeAttributes.cpp +++ b/OpcodeAttributes.cpp @@ -1,15 +1,6 @@ -// OpcodeMnemonic.cpp : COpcode class implementation file -// - #include "stdafx.h" #include "Opcode.h" -#ifdef _DEBUG -#define new DEBUG_NEW -#endif - - -// COpcode::COpcodeAttributes COpcode::COpcodeAttributes::COpcodeAttributes(std::string mnemonic, std::string name, ULONG ulNumArgs, @@ -40,7 +31,8 @@ COpcode::COpcodeAttributes::COpcodeAttributes(const COpcodeAttributes& attribute COpcode::COpcodeAttributes& COpcode::COpcodeAttributes::operator = (const COpcodeAttributes& attributes) { - if (&attributes != this) { + if (&attributes != this) + { m_strMnemonic = attributes.m_strMnemonic; m_strName = attributes.m_strName; m_ulNumArgs = attributes.m_ulNumArgs; diff --git a/ProcTable.cpp b/ProcTable.cpp index 6b94f09..0b2564e 100644 --- a/ProcTable.cpp +++ b/ProcTable.cpp @@ -1,6 +1,3 @@ -// ProcTable.cpp : implementation file -// - #include "stdafx.h" #include "ProcTable.h" #include "Utility.h" @@ -9,8 +6,6 @@ #include "Hacks/CString.h" #include -// CProcDescriptor - CProcDescriptor::CProcDescriptor() : m_ulNameOffset(0), m_ulType(0), @@ -38,7 +33,8 @@ CProcDescriptor::~CProcDescriptor() CProcDescriptor& CProcDescriptor::operator = (const CProcDescriptor& Item) { - if (&Item != this) { + if (&Item != this) + { m_ulNameOffset = Item.m_ulNameOffset; m_ulType = Item.m_ulType; m_ulTime = Item.m_ulTime; @@ -52,35 +48,29 @@ CProcDescriptor& CProcDescriptor::operator = (const CProcDescriptor& Item) void CProcDescriptor::Serialize(CArchive& ar) { - //if (ar.IsStoring()) { - if (false) + UINT uiTotalRead; + + uiTotalRead = ReadMSBULong(ar, m_ulNameOffset); + uiTotalRead += ReadMSBULong(ar, m_ulType); + uiTotalRead += ReadMSBULong(ar, m_ulTime); + uiTotalRead += ReadMSBULong(ar, m_ulExpressionOffset); + uiTotalRead += ReadMSBULong(ar, m_ulBodyOffset); + uiTotalRead += ReadMSBULong(ar, m_ulNumArgs); + +// printf("=================\n"); +// printf("uiTotalRead = %u\n", uiTotalRead); +// printf("m_ulNameOffset = 0x%08X\n", m_ulNameOffset); +// printf("m_ulType = 0x%08X\n", m_ulType); +// printf("m_ulTime = 0x%08X\n", m_ulTime); +// printf("m_ulExpressionOffset = 0x%08X\n", m_ulExpressionOffset); +// printf("m_ulBodyOffset = 0x%08X\n", m_ulBodyOffset); +// printf("m_ulNumArgs = 0x%08X\n", m_ulNumArgs); + + + if (uiTotalRead != (sizeof(ULONG) * 6)) { - // ASSERT(FALSE); - } - else { - UINT uiTotalRead; - - uiTotalRead = ReadMSBULong(ar, m_ulNameOffset); - uiTotalRead += ReadMSBULong(ar, m_ulType); - uiTotalRead += ReadMSBULong(ar, m_ulTime); - uiTotalRead += ReadMSBULong(ar, m_ulExpressionOffset); - uiTotalRead += ReadMSBULong(ar, m_ulBodyOffset); - uiTotalRead += ReadMSBULong(ar, m_ulNumArgs); - -// printf("=================\n"); -// printf("uiTotalRead = %u\n", uiTotalRead); -// printf("m_ulNameOffset = 0x%08X\n", m_ulNameOffset); -// printf("m_ulType = 0x%08X\n", m_ulType); -// printf("m_ulTime = 0x%08X\n", m_ulTime); -// printf("m_ulExpressionOffset = 0x%08X\n", m_ulExpressionOffset); -// printf("m_ulBodyOffset = 0x%08X\n", m_ulBodyOffset); -// printf("m_ulNumArgs = 0x%08X\n", m_ulNumArgs); - - - if (uiTotalRead != (sizeof(ULONG) * 6)) { - printf("Error: Unable read procedure descriptor\n"); - AfxThrowUserException(); - } + printf("Error: Unable read procedure descriptor\n"); + AfxThrowUserException(); } } @@ -118,7 +108,8 @@ void CProcDescriptor::Dump(CArchive& ar) strType += ")"; } - else { + else + { strType = "No special types"; } @@ -157,20 +148,26 @@ int compareProcBodyOffsets(const void* elem0, const void* elem1) ProcBodyOffset offset0 = *((ProcBodyOffset*)elem0); ProcBodyOffset offset1 = *((ProcBodyOffset*)elem1); - if (offset0.m_ulBodyOffset < offset1.m_ulBodyOffset) { + if (offset0.m_ulBodyOffset < offset1.m_ulBodyOffset) + { return -1; } - else if (offset0.m_ulBodyOffset > offset1.m_ulBodyOffset) { + else if (offset0.m_ulBodyOffset > offset1.m_ulBodyOffset) + { return 1; } - else { - if (offset0.m_ulProcIndex < offset1.m_ulProcIndex) { + else + { + if (offset0.m_ulProcIndex < offset1.m_ulProcIndex) + { return -1; } - else if (offset0.m_ulProcIndex > offset1.m_ulProcIndex) { + else if (offset0.m_ulProcIndex > offset1.m_ulProcIndex) + { return 1; } - else { + else + { return 0; } } @@ -178,91 +175,95 @@ int compareProcBodyOffsets(const void* elem0, const void* elem1) void CProcTable::Serialize(CArchive& ar) { - //if (ar.IsStoring()) { - if(false) + m_Table.RemoveAll(); + m_ProcSize.RemoveAll(); + + ULONG ulSizeOfTable; + ULONG ulRead; + + ar.Flush(); + ULONG ulSizeOfFile = ULONG(ar.GetFile()->GetLength()); + + ulRead = ReadMSBULong(ar, ulSizeOfTable); + + if (ulRead != sizeof(ulSizeOfTable)) { - // ASSERT(FALSE); + printf("Error: Unable read size of procedures table\n"); + AfxThrowUserException(); } - else { - m_Table.RemoveAll(); - m_ProcSize.RemoveAll(); - ULONG ulSizeOfTable; - ULONG ulRead; + m_Table.SetSize(ulSizeOfTable); + m_ProcSize.SetSize(ulSizeOfTable); - ar.Flush(); - ULONG ulSizeOfFile = ULONG(ar.GetFile()->GetLength()); + ULONG ulIndexOfProcOffset = 0; + ProcBodyOffset* pOffsets = new ProcBodyOffset[ulSizeOfTable + 1]; - ulRead = ReadMSBULong(ar, ulSizeOfTable); + if (pOffsets == NULL) + { +// AfxThrowMemoryException(); + throw 1; + } - if (ulRead != sizeof(ulSizeOfTable)) + for(ULONG i = 0; i < ulSizeOfTable; i++) + { +// printf("======== %u =========\n", i); + m_Table.at(i).Serialize(ar); + m_ProcSize.at(i) = 0; // Initialize size of procedure + + if (!(m_Table.at(i).m_ulType & P_IMPORT)) { - printf("Error: Unable read size of procedures table\n"); + if ((m_Table.at(i).m_ulBodyOffset != 0) && (m_Table.at(i).m_ulBodyOffset != ulSizeOfFile)) + { + pOffsets[ulIndexOfProcOffset].m_ulProcIndex = i; + pOffsets[ulIndexOfProcOffset].m_ulBodyOffset = m_Table.at(i).m_ulBodyOffset; + ulIndexOfProcOffset++; + } + else + { + m_Table.at(i).m_ulType |= P_NOTIMPLEMENTED; + } + } + } + + pOffsets[ulIndexOfProcOffset].m_ulProcIndex = ulIndexOfProcOffset + 1; + pOffsets[ulIndexOfProcOffset].m_ulBodyOffset = ulSizeOfFile; + + qsort(pOffsets, ulIndexOfProcOffset, sizeof(ProcBodyOffset), compareProcBodyOffsets); + + // Sizes of procedures + for(ULONG i = 0; i < ulIndexOfProcOffset; i++) + { + if (pOffsets[i + 1].m_ulBodyOffset >= pOffsets[i].m_ulBodyOffset) + { + m_ProcSize.at(pOffsets[i].m_ulProcIndex) = pOffsets[i + 1].m_ulBodyOffset - pOffsets[i].m_ulBodyOffset; + } + else + { + printf("Error: Procedures are not sorted in ascending address order\n"); AfxThrowUserException(); } - - m_Table.SetSize(ulSizeOfTable); - m_ProcSize.SetSize(ulSizeOfTable); - - ULONG ulIndexOfProcOffset = 0; - ProcBodyOffset* pOffsets = new ProcBodyOffset[ulSizeOfTable + 1]; - - if (pOffsets == NULL) { -// AfxThrowMemoryException(); - throw 1; - } - - for(ULONG i = 0; i < ulSizeOfTable; i++) { -// printf("======== %u =========\n", i); - m_Table.at(i).Serialize(ar); - m_ProcSize.at(i) = 0; // Initialize size of procedure - - if (!(m_Table.at(i).m_ulType & P_IMPORT)) { - if ((m_Table.at(i).m_ulBodyOffset != 0) && (m_Table.at(i).m_ulBodyOffset != ulSizeOfFile)) { - pOffsets[ulIndexOfProcOffset].m_ulProcIndex = i; - pOffsets[ulIndexOfProcOffset].m_ulBodyOffset = m_Table.at(i).m_ulBodyOffset; - ulIndexOfProcOffset++; - } - else { - m_Table.at(i).m_ulType |= P_NOTIMPLEMENTED; - } - } - } - - pOffsets[ulIndexOfProcOffset].m_ulProcIndex = ulIndexOfProcOffset + 1; - pOffsets[ulIndexOfProcOffset].m_ulBodyOffset = ulSizeOfFile; - - qsort(pOffsets, ulIndexOfProcOffset, sizeof(ProcBodyOffset), compareProcBodyOffsets); - - // Sizes of procedures - for(ULONG i = 0; i < ulIndexOfProcOffset; i++) { - if (pOffsets[i + 1].m_ulBodyOffset >= pOffsets[i].m_ulBodyOffset) { - m_ProcSize.at(pOffsets[i].m_ulProcIndex) = pOffsets[i + 1].m_ulBodyOffset - pOffsets[i].m_ulBodyOffset; - } - else { - printf("Error: Procedures are not sorted in ascending address order\n"); - AfxThrowUserException(); - } - } - - delete [] pOffsets; - - for(ULONG i = 0; i < ulSizeOfTable; i++) { - if (m_Table.at(i).m_ulBodyOffset != 0) { - m_ulOffsetOfProcSection = m_Table.at(i).m_ulBodyOffset; - break; - } - } - -// // For debugging only -// printf("Total: %d\n", m_Table.GetSize()); -// -// for(i = 0; i < ulSizeOfTable; i++) { -// printf("Offset: 0x%08X, size: 0x%08X\n", m_Table.at(i).m_ulBodyOffset, m_ProcSize.at(i)); -// } - -// printf("m_ulOffsetOfProcSection: 0x%08X\n", m_ulOffsetOfProcSection); } + + delete [] pOffsets; + + for(ULONG i = 0; i < ulSizeOfTable; i++) + { + if (m_Table.at(i).m_ulBodyOffset != 0) + { + m_ulOffsetOfProcSection = m_Table.at(i).m_ulBodyOffset; + break; + } + } + +// // For debugging only +// printf("Total: %d\n", m_Table.GetSize()); + +// for(i = 0; i < ulSizeOfTable; i++) +// { +// printf("Offset: 0x%08X, size: 0x%08X\n", m_Table.at(i).m_ulBodyOffset, m_ProcSize.at(i)); +// } + +// printf("m_ulOffsetOfProcSection: 0x%08X\n", m_ulOffsetOfProcSection); } INT_PTR CProcTable::GetSize() @@ -272,7 +273,8 @@ INT_PTR CProcTable::GetSize() ULONG CProcTable::GetSizeOfProc(INT_PTR nIndex) { - if ((nIndex < 0) || (nIndex > m_ProcSize.GetUpperBound())) { + if ((nIndex < 0) || (nIndex > m_ProcSize.GetUpperBound())) + { printf("Warning: Invalid index of procedure (%d). Exception will be thrown\n", nIndex); AfxThrowUserException(); } @@ -303,7 +305,8 @@ void CProcTable::Dump(CArchive& ar) CProcDescriptor& CProcTable::operator [] (INT_PTR nIndex) { - if ((nIndex < 0) || (nIndex > m_ProcSize.GetUpperBound())) { + if ((nIndex < 0) || (nIndex > m_ProcSize.GetUpperBound())) + { printf("Warning: Invalid index of procedure (%d). Exception will be thrown\n", nIndex); AfxThrowUserException(); } diff --git a/ProcTable.h b/ProcTable.h index 0d1987b..e30d7aa 100644 --- a/ProcTable.h +++ b/ProcTable.h @@ -6,7 +6,8 @@ #include "Hacks/CArchive.h" // CProcDescriptor -class CProcDescriptor : public CObject { +class CProcDescriptor +{ public: CProcDescriptor(); CProcDescriptor(const CProcDescriptor& Item); @@ -29,9 +30,8 @@ public: ULONG m_ulNumArgs; // Number of args to procedure }; - -// CProcTable -class CProcTable : public CObject { +class CProcTable +{ public: CProcTable(); virtual ~CProcTable(); diff --git a/StartupCode.h b/StartupCode.h index 0aaeba7..73f1147 100644 --- a/StartupCode.h +++ b/StartupCode.h @@ -1,5 +1,3 @@ -#pragma once - #ifndef STARTUP_CODE_H #define STARTUP_CODE_H diff --git a/Utility.h b/Utility.h index 1f20e5a..ee60e68 100644 --- a/Utility.h +++ b/Utility.h @@ -1,5 +1,3 @@ -#pragma once - #ifndef UTILITY_H #define UTILITY_H diff --git a/stdafx.cpp b/stdafx.cpp index c7e3d00..2348535 100644 --- a/stdafx.cpp +++ b/stdafx.cpp @@ -1,9 +1,5 @@ #include "stdafx.h" -CObject::CObject() -{ -} - void AfxThrowUserException() { diff --git a/stdafx.h b/stdafx.h index b349842..9530666 100644 --- a/stdafx.h +++ b/stdafx.h @@ -10,12 +10,6 @@ class CString; -class CObject -{ -public : - CObject(); -}; - class CDWordArray : public CArray {