Advertisement
Guest User

NXE2GOD

a guest
Jan 2nd, 2012
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 15.22 KB | None | 0 0
  1. #include <xtl.h>
  2. #include "AtgConsole.h"
  3. #include "AtgUtil.h"
  4. #include "AtgInput.h"
  5. #include <iostream>
  6. #include <iomanip>
  7. #include <sstream>
  8. #include <iostream>
  9. #include <fstream>
  10. #include <sys/stat.h>
  11. #include "xfilecache.h"
  12. #include <string>
  13. #include "xbox.h"
  14. #include <vector>
  15.  
  16. using std::vector;
  17. using std::string;
  18. using std::ifstream;
  19. using std::ofstream;
  20. using std::fstream;
  21.  
  22. typedef struct _STRING {
  23.     USHORT Length;
  24.     USHORT MaximumLength;
  25.     PCHAR Buffer;
  26. } STRING, *PSTRING;
  27.  
  28. extern "C" int __stdcall ObCreateSymbolicLink( STRING*, STRING*);
  29. extern "C" int __stdcall ObDeleteSymbolicLink( STRING* );
  30. extern "C" VOID XeCryptSha(LPVOID DataBuffer1, UINT DataSize1, LPVOID DataBuffer2, UINT DataSize2, LPVOID DataBuffer3, UINT DataSize3, LPVOID DigestBuffer, UINT DigestSize);
  31.  
  32. class NXE {
  33.  
  34.     void readTitle()
  35.     {
  36.         string fullPath;
  37.         fullPath += path;
  38.         fullPath += fileName;
  39.  
  40.         ifstream file;
  41.         file.open ((char*)fullPath.c_str(), ifstream::in);
  42.         if (file.is_open())
  43.         {
  44.             char buffer[_MAX_PATH];
  45.             file.seekg(0x00000411);
  46.             file.read(buffer,_MAX_PATH);
  47.             swprintf_s(title, _MAX_PATH,L"%s", buffer);
  48.             file.close();
  49.         }
  50.     }
  51.  
  52.     public:
  53.         string fileName;
  54.         string path;
  55.         wchar_t title[_MAX_PATH];
  56.         NXE (string, string);
  57.         bool status;   
  58. };
  59.  
  60. NXE::NXE (string strFileName, string strPath) {
  61.     fileName = strFileName;
  62.     path = strPath;
  63.     readTitle();
  64.     status = true;
  65. }
  66.  
  67.  
  68. vector<NXE> allNXE;
  69.  
  70. string sDirAct;
  71. string filePathzzz = "hdd:\\Content\\0000000000000000";
  72. string fileList[5000];
  73. NXE* nxeGames;
  74. int fileCount = 0;
  75. bool debugEnabled = false;
  76.  
  77. ATG::Console console;
  78.  
  79. HRESULT Map( CHAR* szDrive, CHAR* szDevice )
  80. {
  81.     CHAR * szSourceDevice;
  82.     CHAR szDestinationDrive[16];
  83.  
  84.     szSourceDevice = szDevice;
  85.  
  86.     sprintf_s( szDestinationDrive,"\\??\\%s", szDrive );
  87.  
  88.     STRING DeviceName =
  89.     {
  90.         strlen(szSourceDevice),
  91.         strlen(szSourceDevice) + 1,
  92.         szSourceDevice
  93.     };
  94.  
  95.     STRING LinkName =
  96.     {
  97.         strlen(szDestinationDrive),
  98.         strlen(szDestinationDrive) + 1,
  99.         szDestinationDrive
  100.     };
  101.  
  102.     return ( HRESULT )ObCreateSymbolicLink( &LinkName, &DeviceName );
  103. }
  104.  
  105. HRESULT unMap( CHAR* szDrive )
  106. {
  107.     CHAR szDestinationDrive[16];
  108.     sprintf_s( szDestinationDrive,"\\??\\%s", szDrive );
  109.  
  110.     STRING LinkName =
  111.     {
  112.         strlen(szDestinationDrive),
  113.         strlen(szDestinationDrive) + 1,
  114.         szDestinationDrive
  115.     };
  116.  
  117.     return ( HRESULT )ObDeleteSymbolicLink( &LinkName );
  118. }
  119.  
  120. int DeleteDirectory(const std::string &refcstrRootDirectory,
  121.                     bool              bDeleteSubdirectories = true)
  122. {
  123.   bool            bSubdirectory = false;       // Flag, indicating whether
  124.                                                // subdirectories have been found
  125.   HANDLE          hFile;                       // Handle to directory
  126.   std::string     strFilePath;                 // Filepath
  127.   std::string     strPattern;                  // Pattern
  128.   WIN32_FIND_DATA FileInformation;             // File information
  129.  
  130.  
  131.   strPattern = refcstrRootDirectory + "\\*.*";
  132.   hFile = ::FindFirstFile(strPattern.c_str(), &FileInformation);
  133.   if(hFile != INVALID_HANDLE_VALUE)
  134.   {
  135.     do
  136.     {
  137.       if(FileInformation.cFileName[0] != '.')
  138.       {
  139.         strFilePath.erase();
  140.         strFilePath = refcstrRootDirectory + "\\" + FileInformation.cFileName;
  141.  
  142.         if(FileInformation.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  143.         {
  144.           if(bDeleteSubdirectories)
  145.           {
  146.             // Delete subdirectory
  147.             int iRC = DeleteDirectory(strFilePath, bDeleteSubdirectories);
  148.             if(iRC)
  149.               return iRC;
  150.           }
  151.           else
  152.             bSubdirectory = true;
  153.         }
  154.         else
  155.         {
  156.           // Set file attributes
  157.           if(::SetFileAttributes(strFilePath.c_str(),
  158.                                  FILE_ATTRIBUTE_NORMAL) == FALSE)
  159.             return ::GetLastError();
  160.  
  161.           // Delete file
  162.           if(::DeleteFile(strFilePath.c_str()) == FALSE)
  163.             return ::GetLastError();
  164.         }
  165.       }
  166.     } while(::FindNextFile(hFile, &FileInformation) == TRUE);
  167.  
  168.     // Close handle
  169.     ::FindClose(hFile);
  170.  
  171.     DWORD dwError = ::GetLastError();
  172.     if(dwError != ERROR_NO_MORE_FILES)
  173.       return dwError;
  174.     else
  175.     {
  176.       if(!bSubdirectory)
  177.       {
  178.         // Set directory attributes
  179.         if(::SetFileAttributes(refcstrRootDirectory.c_str(),
  180.                                FILE_ATTRIBUTE_NORMAL) == FALSE)
  181.           return ::GetLastError();
  182.  
  183.         // Delete directory
  184.         if(::RemoveDirectory(refcstrRootDirectory.c_str()) == FALSE)
  185.           return ::GetLastError();
  186.       }
  187.     }
  188.   }
  189.  
  190.   return 0;
  191. }
  192.  
  193. void debugLog(char* output)
  194. {
  195.     if (debugEnabled)
  196.     {
  197.         ofstream writeLog;
  198.  
  199.         writeLog.open("game:\\debug.log",ofstream::app);
  200.         if (writeLog.is_open())
  201.         {
  202.           writeLog.write(output,strlen(output));
  203.           writeLog.write("\n",1);
  204.         }
  205.         writeLog.close();
  206.     }
  207. }
  208.  
  209. bool FileExists(char* strFilename) {
  210.   struct stat stFileInfo;
  211.   bool returnValue;
  212.   int intStat;
  213.  
  214.   intStat = stat(strFilename,&stFileInfo);
  215.   if(intStat == 0) {
  216.     returnValue = true;
  217.   } else {
  218.  
  219.     returnValue = false;
  220.   }
  221.  
  222.   return(returnValue);
  223. }
  224.  
  225. void nxetogod(NXE nxeGame)
  226. {
  227.     console.Format("Converting ");
  228.     console.Format(nxeGame.title);
  229.     console.Format(" to GOD...\n");
  230.  
  231.     string goddirectory;
  232.     string backupdirectory;
  233.     string confile;
  234.     string condirectory;
  235.     string datadirectory;
  236.     string olddatadirectory;
  237.  
  238.     goddirectory += nxeGame.path;
  239.     goddirectory.erase(goddirectory.size() - 9, 9);
  240.     goddirectory += "00007000";
  241.  
  242.     olddatadirectory += nxeGame.path;
  243.     olddatadirectory += nxeGame.fileName;
  244.     olddatadirectory += ".data";
  245.  
  246.     datadirectory += goddirectory;
  247.     datadirectory += "\\";
  248.     datadirectory += nxeGame.fileName;
  249.     datadirectory += ".data";
  250.  
  251.     backupdirectory += goddirectory;
  252.     backupdirectory += "\\backup";
  253.  
  254.     confile += nxeGame.path;
  255.     confile += nxeGame.fileName;
  256.  
  257.     condirectory += nxeGame.path;
  258.     condirectory.erase(condirectory.size() - 1, 1);
  259.  
  260.     //Create 00007000 directory if it doens't already exist
  261.     ::CreateDirectory(goddirectory.c_str(),0);
  262.     //Create backup directory to store original CON file if it doesn't already exist
  263.     ::CreateDirectory(backupdirectory.c_str(),0);
  264.  
  265.     //Append CON file name to existing path strings to give the full path
  266.     goddirectory += "\\";
  267.     backupdirectory += "\\";
  268.     goddirectory += nxeGame.fileName;
  269.     backupdirectory += nxeGame.fileName;
  270.  
  271.     //Move CON file from 00004000 to 00007000
  272.     if(FileExists((char*)goddirectory.c_str()))
  273.     {
  274.         //Delete target file if it already exists
  275.         ::DeleteFile(goddirectory.c_str());
  276.     }
  277.     //Then move
  278.     ::MoveFile(confile.c_str(),goddirectory.c_str());
  279.  
  280.     //Move .data directory from 00004000 to 00007000
  281.     if(::CreateDirectory(datadirectory.c_str(),0) == 0)
  282.     {
  283.         //Target directory exists in 00007000 - Delete it to empty it
  284.         DeleteDirectory(datadirectory.c_str(),true);
  285.         //Recreate it
  286.         ::CreateDirectory(datadirectory.c_str(),0);
  287.     }
  288.     //Now move data files there
  289.     HANDLE hFind;
  290.     WIN32_FIND_DATA wfd;
  291.  
  292.     string searchdir;
  293.     searchdir += olddatadirectory;
  294.     searchdir += "\\*";
  295.  
  296.     hFind = FindFirstFile( searchdir.c_str(), &wfd );
  297.    
  298.     if( INVALID_HANDLE_VALUE == hFind )
  299.     {
  300.  
  301.     }
  302.     else
  303.     {
  304.         do
  305.         {      
  306.             string oldFile;
  307.             string newFile;
  308.  
  309.             oldFile += olddatadirectory;
  310.             oldFile += "\\";
  311.             oldFile += wfd.cFileName;
  312.  
  313.             newFile += datadirectory;
  314.             newFile += "\\";
  315.             newFile += wfd.cFileName;
  316.  
  317.             ::MoveFile(oldFile.c_str(), newFile.c_str());
  318.  
  319.         } while( FindNextFile( hFind, &wfd ));
  320.         FindClose( hFind );
  321.     }
  322.    
  323.     //Delete original file from backup directory if it exists
  324.     if(FileExists((char*)backupdirectory.c_str()))
  325.     {
  326.         //Delete target file if it already exists
  327.         ::DeleteFile(backupdirectory.c_str());
  328.     }
  329.    
  330.     //Copy original CON file to the backup directory
  331.     ::CopyFile(goddirectory.c_str(), backupdirectory.c_str(),true);
  332.    
  333.     char* SHA1Buffer[0xACBC];
  334.     char* SHA1Digest[20];
  335.    
  336.     fstream LIVE;
  337.  
  338.     LIVE.open(goddirectory.c_str(), fstream::in | fstream::out | fstream::binary );
  339.     if (LIVE.is_open())
  340.     {
  341.         LIVE.seekp(0x340);
  342.         LIVE.put(0x0);
  343.         LIVE.put(0x0);
  344.         LIVE.put(0xAD);
  345.         LIVE.put(0x0E);
  346.         LIVE.seekp(0x346);
  347.         LIVE.put(0x70);
  348.         LIVE.seekp(0x379);
  349.         LIVE.put(0x24);
  350.         LIVE.put(0x5);
  351.         LIVE.put(0x5);
  352.         LIVE.put(0x11);
  353.         LIVE.seekp(0x3FD);
  354.         LIVE.put(0x0);
  355.         LIVE.put(0x0);
  356.         LIVE.put(0x0);
  357.         LIVE.put(0x0);
  358.         LIVE.put(0x0);
  359.         LIVE.put(0x0);
  360.         LIVE.put(0x0);
  361.         LIVE.put(0x0);
  362.         LIVE.put(0x0);
  363.         LIVE.put(0x0);
  364.         LIVE.put(0x0);
  365.         LIVE.put(0x0);
  366.         LIVE.put(0x0);
  367.         LIVE.put(0x0);
  368.         LIVE.put(0x0);
  369.         LIVE.put(0x0);
  370.         LIVE.put(0x0);
  371.         LIVE.put(0x0);
  372.         LIVE.put(0x0);
  373.         LIVE.put(0x0);
  374.         LIVE.seekp(0x22c);
  375.         LIVE.put(0xFF);
  376.         LIVE.put(0xFF);
  377.         LIVE.put(0xFF);
  378.         LIVE.put(0xFF);
  379.         LIVE.put(0xFF);
  380.         LIVE.put(0xFF);
  381.         LIVE.put(0xFF);
  382.         LIVE.put(0xFF);
  383.         LIVE.seekp(0);
  384.         LIVE.put(0x4C);
  385.         LIVE.put(0x49);
  386.         LIVE.put(0x56);
  387.         LIVE.put(0x45);
  388.         LIVE.flush();
  389.         LIVE.seekg(0x344);
  390.         LIVE.read((char*)SHA1Buffer,0xACBC);
  391.         XeCryptSha(&SHA1Buffer, 0xACBC, NULL,0, NULL,0, &SHA1Digest, 20);
  392.         LIVE.seekp(0x32C);
  393.         LIVE.write((char*) SHA1Digest, 20);
  394.         LIVE.close();
  395.         debugLog("New GOD container created, game should now launch without disk.");
  396.     } else
  397.     {
  398.         debugEnabled = true;
  399.         debugLog("Unable to open new Games On Demand Live file at:");
  400.         debugLog((char*)goddirectory.c_str());
  401.         nxeGame.status = false;
  402.     }
  403.  
  404.     if(nxeGame.status)
  405.     {
  406.         //Delete old data directory
  407.         ::RemoveDirectory(olddatadirectory.c_str());
  408.     }
  409. }
  410.  
  411. bool isNXE(WCHAR* path, string path2)
  412. {
  413.     string pathcheck;
  414.     pathcheck += path2;
  415.     if (!pathcheck.compare(pathcheck.length() - 9 , 8,"00004000") == 0)
  416.     {
  417.         debugLog("Not in 00004000 dir, file is either an original backup made by NXE2GOD or has been moved since being ripped by NXE, not attempting to convert.");
  418.         return false;
  419.     }
  420.  
  421.     ifstream NXE;
  422.     NXE.open (path, ifstream::in);
  423.     if (NXE.is_open())
  424.     {
  425.         char * containerBuffer1;
  426.         char * containerBuffer2;
  427.         containerBuffer1 = new char [4];
  428.         containerBuffer2 = new char [4];
  429.         NXE.seekg(0);
  430.         NXE.seekg(0x344);
  431.         NXE.read(containerBuffer1,4);      
  432.  
  433.         if (int(containerBuffer1[0] == 0))
  434.         {
  435.             if (int(containerBuffer1[1] == 0))
  436.             {
  437.                 if (int(containerBuffer1[2] == 64))
  438.                 {
  439.                     if (int(containerBuffer1[3] == 0))
  440.                     {
  441.                         NXE.close();
  442.                         return true;
  443.                     }
  444.                 }
  445.             }
  446.         }
  447.     }
  448.     NXE.close();
  449.     return false;
  450. }
  451.  
  452. HRESULT ScanDir(string strFind)
  453.     {
  454.         HANDLE hFind;
  455.         WIN32_FIND_DATA wfd;
  456.         LPSTR lpPath = new char[MAX_PATH];
  457.         LPWSTR lpPathW = new wchar_t[MAX_PATH];
  458.         LPSTR lpFileName = new char[MAX_PATH];
  459.         LPWSTR lpFileNameW = new wchar_t[MAX_PATH];
  460.         string sFileName;
  461.  
  462.         sDirAct = strFind;
  463.         strFind+= "\\*";
  464.  
  465.         strFind._Copy_s(lpPath, strFind.length(),strFind.length());
  466.  
  467.         lpPath[strFind.length()]='\0';
  468.  
  469.         ::MultiByteToWideChar( CP_ACP, NULL,lpPath, -1, lpPathW, MAX_PATH);
  470.  
  471.         hFind = FindFirstFile( lpPath, &wfd );
  472.         int nIndex = 0;
  473.         if( INVALID_HANDLE_VALUE == hFind )
  474.         {
  475.             debugLog("Invalid handle type - Directory most likely empty");
  476.             debugLog(lpPath);
  477.             debugLog(lpFileName);
  478.         }
  479.         else
  480.         {
  481.             nIndex = 0;
  482.         do
  483.         {      
  484.             sFileName = wfd.cFileName;
  485.             sFileName._Copy_s(lpFileName, sFileName.length(), sFileName.length());
  486.             lpFileName[sFileName.length()]='\0';
  487.             ::MultiByteToWideChar( CP_ACP, NULL,lpFileName, -1, lpFileNameW, MAX_PATH);
  488.        
  489.             if(FILE_ATTRIBUTE_DIRECTORY == wfd.dwFileAttributes)
  490.             {
  491.                
  492.                 string nextDir;
  493.                 nextDir += lpPath;
  494.                 string nextDir1;
  495.                 nextDir1 += lpFileName;
  496.                 nextDir.erase(nextDir.size() - 1, 1);
  497.                 nextDir += nextDir1;
  498.                 ScanDir(nextDir);
  499.  
  500.             } else
  501.             {
  502.                 string filePath;
  503.                 string filePathX;
  504.                 string fileNameX;
  505.                 filePath += lpPath;
  506.                 string fileName;
  507.                 fileName += lpFileName;
  508.                 filePath.erase(filePath.size() - 1, 1);
  509.                 filePathX += filePath;
  510.                 fileNameX += fileName;
  511.                 filePath += fileName;
  512.  
  513.                 LPSTR FileA = new char[MAX_PATH];
  514.  
  515.                 LPSTR FileNameA = new char[MAX_PATH];
  516.                 LPSTR FilePathA = new char[MAX_PATH];
  517.                 filePathX._Copy_s(FilePathA, filePathX.length(),filePathX.length());
  518.                 fileNameX._Copy_s(FileNameA , fileNameX.length(),fileNameX.length());
  519.                 filePath._Copy_s(FileA, filePath.length(),filePath.length());
  520.                 FileA[filePath.length()]='\0';
  521.                 LPWSTR FileB = new wchar_t[MAX_PATH];
  522.                 ::MultiByteToWideChar( CP_ACP, NULL, FileA, -1, FileB, MAX_PATH);
  523.  
  524.                 if (isNXE(FileB,filePathX))
  525.                 {
  526.                     NXE temp(fileNameX,filePathX);
  527.                     allNXE.push_back(temp);
  528.                 }else
  529.                 {
  530.                 }
  531.             }
  532.  
  533.             nIndex++;
  534.             } while( FindNextFile( hFind, &wfd ));
  535.             FindClose( hFind );
  536.         }
  537.         return S_OK;
  538.     }
  539.  
  540.  
  541. //--------------------------------------------------------------------------------------
  542. // Name: main
  543. // Desc: Entry point to the program
  544. //--------------------------------------------------------------------------------------
  545. VOID __cdecl main()
  546. {
  547.  
  548.     console.Create( "game:\\Media\\Fonts\\Arial_12.xpr", 0x00000000, 0xFFFFFFFF );
  549.    
  550.     if(FileExists("debug.log"))
  551.     {
  552.         debugEnabled = true;
  553.     }
  554.  
  555.     debugLog("\n");
  556.     debugLog("*Fresh Application run*");
  557.  
  558.     unMap("hdd:");
  559.     if (Map("hdd:","\\Device\\Harddisk0\\Partition1") == S_OK)
  560.     {
  561.         debugLog("Drive mounted, Scanning folders");
  562.         console.Format( "NXE2GOD V1.1 by Dstruktiv\n" );
  563.         console.Format("For updates please visit www.digitalreality.co.nz\\xbox360\n\n");
  564.         console.Format( "Scanning folders, please wait...\n" );
  565.         ScanDir(filePathzzz);
  566.         if (allNXE.size() == 0)
  567.         {
  568.             console.Format("\nNo NXE titles found\n");
  569.             console.Format( "\nPush any key to exit to NXE" );
  570.         }
  571.         else
  572.         {
  573.             console.Format("\n");
  574.             console.Format( "NXE Files found:\n" );
  575.             console.Format("\n");
  576.  
  577.             for(unsigned int i = 0; i < allNXE.size(); i++) {
  578.                 console.Format(allNXE[i].title);
  579.                 console.Format(" at location: ");
  580.                 console.Format(allNXE[i].path.c_str());
  581.                 console.Format(allNXE[i].fileName.c_str());
  582.                 console.Format("\n");
  583.             }
  584.  
  585.             console.Format("\nPush A to convert files from NXE to GOD or B to cancel and quit to NXE\n\n");
  586.             bool keypush = false;
  587.             while(!keypush)
  588.             {
  589.                 ATG::GAMEPAD* pGamepad = ATG::Input::GetMergedInput();
  590.                 if( pGamepad->wPressedButtons  & XINPUT_GAMEPAD_A)
  591.                 {
  592.                     keypush = true;
  593.                 }
  594.                 if( pGamepad->wPressedButtons  & XINPUT_GAMEPAD_B)
  595.                 {
  596.                     XLaunchNewImage( XLAUNCH_KEYWORD_DEFAULT_APP, 0 );
  597.                 }
  598.             }
  599.             console.Format("Converting NXE2GOD, please wait...\n\n");
  600.             for(unsigned int i = 0; i < allNXE.size(); i++) {
  601.                 nxetogod(allNXE[i]);
  602.             }      
  603.             console.Format("\n");
  604.             console.Format("Conversion results:\n\n");
  605.             for(unsigned int i = 0; i < allNXE.size(); i++) {
  606.                 if(allNXE[i].status)
  607.                 {
  608.                     console.Format(allNXE[i].title);
  609.                     console.Format(" converted successfully.\n");
  610.                 }
  611.                 else
  612.                 {
  613.                     console.Format(allNXE[i].title);
  614.                     console.Format(" failed to convert, please check debug.log on root of USB stick.\n");
  615.                 }
  616.             }
  617.  
  618.             console.Format( "\nProcessing complete, push any key to exit to NXE" );
  619.         }
  620.     }
  621.    
  622.     bool keypush = false;
  623.     while(!keypush)
  624.     {
  625.         ATG::GAMEPAD* pGamepad = ATG::Input::GetMergedInput();
  626.         if( pGamepad->wPressedButtons)
  627.         {
  628.             keypush = true;
  629.         }
  630.     }
  631. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement