home *** CD-ROM | disk | FTP | other *** search
/ Xentax forum attachments archive / xentax.7z / 5257 / source.7z / pc_megaman_x8.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2011-12-13  |  6.4 KB  |  220 lines

  1. #include "xentax.h"
  2. #include "pc_megaman_x8.h"
  3.  
  4. PC_MEGAMAN_X8_BEGIN
  5.  
  6. bool extractWPG(const char* filename);
  7. bool extractWSX(const char* filename);
  8.  
  9. bool extract(void)
  10. {
  11.  char pathname[MAX_PATH];
  12.  GetModulePathname(pathname, MAX_PATH);
  13.  return extract(pathname);
  14. }
  15.  
  16. bool extract(const char* pathname)
  17. {
  18.  // process WPG files
  19.  cout << "Searching for WPG files... please wait." << endl;
  20.  deque<string> namelist;
  21.  if(!BuildFilenameList(namelist, "wpg", pathname)) return false;
  22.  cout << "Found " << namelist.size() << " WPG files." << endl;
  23.  uint32 successful = 0;
  24.  for(size_t i = 0; i < namelist.size(); i++) if(extractWPG(namelist[i].c_str())) successful++;
  25.  cout << "Processed " << successful << " out of " << namelist.size() << " WPG files." << endl;
  26.  
  27.  
  28.  //extractWSX("C:\\Temp\\Megaman X8\\opk\\pl\\PL_COS_X.wsx");
  29.  //extractWSX("C:\\Temp\\Megaman X8\\opk\\st00\\EL00_01.wsx");
  30.  
  31.  return true;
  32. }
  33.  
  34. bool extractWSX(const char* filename)
  35. {
  36.  // open file
  37.  cout << "Processing " << filename << "." << endl;
  38.  ifstream ifile(filename, ios::binary);
  39.  if(!ifile) return error("Error opening WSX file.");
  40.  
  41.  // read number of offsets
  42.  uint32 n_offsets = LE_read_uint32(ifile);
  43.  if(ifile.fail()) return error("Read failure.");
  44.  
  45.  // read data size
  46.  uint32 datasize = LE_read_uint32(ifile);
  47.  if(ifile.fail()) return error("Read failure.");
  48.  
  49.  // read offsets
  50.  boost::shared_array<uint32> offsets(new uint32[n_offsets]);
  51.  LE_read_array(ifile, offsets.get(), n_offsets);
  52.  if(ifile.fail()) return error("Read failure.");
  53.  
  54.  // process data at offsets
  55.  for(size_t i = 0; i < n_offsets; i++)
  56.     {
  57.      // seek offset
  58.      ifile.seekg(offsets[i]);
  59.      if(ifile.fail()) return error("Seek failed.");
  60.  
  61.      cout << offsets[i] << endl;
  62.  
  63.      uint32 v1 = LE_read_uint32(ifile);
  64.  
  65.      if(v1 == 0x0101006C)
  66.        {
  67.        }
  68.      else
  69.       {
  70.        return error("Expecting 0x0101006C.");
  71.       }
  72.     }
  73.  
  74.  
  75.  return true;
  76. }
  77.  
  78. bool extractWPG(const char* filename)
  79. {
  80.  // open file
  81.  cout << "Processing " << filename << "." << endl;
  82.  ifstream ifile(filename, ios::binary);
  83.  if(!ifile) return error("Error opening WPG file.");
  84.  
  85.  // compute filesize
  86.  ifile.seekg(0, ios::end);
  87.  size_t filesize = (size_t)ifile.tellg();
  88.  ifile.seekg(0);
  89.  if(filesize == 32) return true;
  90.  
  91.  // read magic
  92.  uint32 magic = BE_read_uint32(ifile);
  93.  if(magic != 0x77706700) return error("Not a WPG file.");
  94.  
  95.  // move to data
  96.  ifile.seekg(0x20);
  97.  if(ifile.fail()) return error("Failed to move to WPG data.");
  98.  
  99.  // make directory for images
  100.  char c_drive[1024];
  101.  char c_path[1024];
  102.  char c_name[1024];
  103.  _splitpath(filename, c_drive, c_path, c_name, nullptr);
  104.  string pathname(c_drive);
  105.  pathname += c_path;
  106.  pathname += c_name;
  107.  CreateDirectoryA(pathname.c_str(), NULL);
  108.  
  109.  // read multiple image
  110.  size_t image_index = 0;
  111.  for(;;)
  112.     {
  113.      // read until we can't read anymore
  114.      ifile.peek();
  115.      if(ifile.eof()) break;
  116.      if(ifile.fail()) break;
  117.  
  118.      // read file type
  119.      uint32 filetype = LE_read_uint32(ifile);
  120.      if(ifile.fail()) return error("Read failure.");
  121.  
  122.      // read unknown #1
  123.      uint32 unk1 = LE_read_uint32(ifile);
  124.      if(ifile.fail()) return error("Read failure.");
  125.  
  126.      // read unknown #2
  127.      uint32 unk2 = LE_read_uint32(ifile);
  128.      if(ifile.fail()) return error("Read failure.");
  129.  
  130.      // read dimensions
  131.      uint16 dx = LE_read_uint16(ifile);
  132.      uint16 dy = LE_read_uint16(ifile);
  133.      if(dx == 0) return error("Invalid WPG dimensions.");
  134.      if(dy == 0) return error("Invalid WPG dimensions.");
  135.  
  136.      // read unknown #3
  137.      uint16 unk3 = LE_read_uint16(ifile);
  138.      if(ifile.fail()) return error("Read failure.");
  139.  
  140.      // build BMP filename
  141.      stringstream ofilename;
  142.      ofilename << pathname.c_str() << "\\" << setfill('0') << setw(3) << image_index++ << ".bmp" << ends;
  143.      cout << ofilename.str() << endl;
  144.  
  145.      // BGRA
  146.      if(filetype == 0x20000)
  147.        {
  148.         // read BMP data
  149.         size_t datasize = 4*dx*dy;
  150.         boost::shared_array<char> data(new char[datasize]);
  151.         ifile.read(data.get(), datasize);
  152.         if(ifile.fail()) return error("Reading WPG data failed.");
  153.     
  154.         // create BMP headers
  155.         BITMAPFILEHEADER fileHeader;
  156.         BITMAPINFOHEADER infoHeader;
  157.         if(!CreateBMPHeaders(dx, dy, 32, &fileHeader, &infoHeader)) return error("Failed to create BMP headers.");
  158.     
  159.         // open BMP file
  160.         ofstream ofile(ofilename.str().c_str(), ios::binary);
  161.         if(!ofile) return error("Failed to create BMP file.");
  162.     
  163.         // save BMP headers
  164.         ofile.write((char*)&fileHeader, sizeof(fileHeader));
  165.         ofile.write((char*)&infoHeader, sizeof(infoHeader));
  166.     
  167.         // save BMP data
  168.         ofile.write(data.get(), datasize);
  169.        }
  170.      else if(filetype == 0x10100)
  171.        {
  172.         // read palette
  173.         size_t size1 = 768;
  174.         boost::shared_array<char> data1(new char[size1]);
  175.         ifile.read(data1.get(), size1);
  176.         if(ifile.fail()) return error("Reading WPG palette failed.");
  177.     
  178.         // read data
  179.         size_t size2 = dx*dy;
  180.         boost::shared_array<char> data2(new char[size2]);
  181.         ifile.read(data2.get(), size2);
  182.     
  183.         // convert palette
  184.         RGBQUAD palette[256];
  185.         size_t index = 0;
  186.         for(size_t i = 0; i < 256; i++) {
  187.             BYTE b1 = data1[index++];
  188.             BYTE b2 = data1[index++];
  189.             BYTE b3 = data1[index++];
  190.             palette[i].rgbRed = b3;
  191.             palette[i].rgbGreen = b2;
  192.             palette[i].rgbBlue = b1;
  193.             palette[i].rgbReserved = 0;
  194.            }
  195.     
  196.         // create BMP headers
  197.         BITMAPFILEHEADER fileHeader;
  198.         BITMAPINFOHEADER infoHeader;
  199.         if(!CreateBMPHeaders(dx, dy, 8, &fileHeader, &infoHeader)) return error("Failed to create BMP headers.");
  200.     
  201.         // open BMP file
  202.         ofstream ofile(ofilename.str().c_str(), ios::binary);
  203.         if(!ofile) return error("Failed to create BMP file.");
  204.     
  205.         // save BMP headers
  206.         ofile.write((char*)&fileHeader, sizeof(fileHeader));
  207.         ofile.write((char*)&infoHeader, sizeof(infoHeader));
  208.     
  209.         // save BMP data
  210.         ofile.write((char*)&palette[0], 256*sizeof(RGBQUAD));
  211.         ofile.write(data2.get(), size2);
  212.        }
  213.      else
  214.         return error("Unknown WPG file type.");
  215.     }
  216.  
  217.  return true;
  218. }
  219.  
  220. PC_MEGAMAN_X8_END