SHARE
TWEET

DeMMU.cpp

HertzDevil Jul 21st, 2013 119 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. PUT this in the same directory as the asset files of MegaMan Unlimited
  3. PRAISE be to lodepng http://lodev.org/lodepng/
  4. */
  5.  
  6. #include <iostream>
  7. #include <fstream>
  8. #include "lodepng.h"
  9.  
  10. using namespace std;
  11.  
  12. int cutscenesCount = 0, gameobjectsCount = 0, levelartCount = 0, menusCount = 0, miscCount = 0, gunsCount;
  13.  
  14. void encodeOneStep(const char* filename, vector<unsigned char>& image, unsigned width, unsigned height){
  15.     unsigned error = lodepng::encode(filename, image, width, height);
  16.     if (error) cout << "encoder error " << error << ": "<< lodepng_error_text(error) << endl;}
  17.  
  18. unsigned int get2(fstream* bin){
  19.     int ret = 0;
  20.     for (int shift = 0;;shift += 7){
  21.         int byte = bin->get();
  22.         ret += byte<<shift;
  23.         if (byte >= 0x80) ret -= 128<<shift;
  24.         else break;}
  25.     return ret;}
  26.  
  27. void dumpImage8(fstream* bin, unsigned char* pal){
  28.     for(int i = 0; i < 256; i++){
  29.         bin->seekg(2, ios_base::cur);
  30.         for(int j = 0; j < 3; j++){
  31.             bin->seekg(1, ios_base::cur);
  32.             pal[3*i + j] = get2(bin);}}
  33.     bin->seekg(0x1D, ios_base::cur);
  34.     unsigned width = get2(bin);
  35.     bin->seekg(0x1, ios_base::cur);
  36.     unsigned height = get2(bin);
  37.     bin->seekg(0x7, ios_base::cur);
  38.     vector<unsigned char> image;
  39.     image.resize(width*height*4);
  40.     for(unsigned y = 0; y < height; y++)
  41.     for(unsigned x = 0; x < width; x++){
  42.         unsigned char pixel = bin->get();
  43.         image[4*width*y + 4*x + 0] = pal[pixel*3 + 0];
  44.         image[4*width*y + 4*x + 1] = pal[pixel*3 + 1];
  45.         image[4*width*y + 4*x + 2] = pal[pixel*3 + 2];
  46.         image[4*width*y + 4*x + 3] = 255;}
  47.     bin->seekg(0x6, ios_base::cur);
  48.     int namelen = bin->get();
  49.     char* filename = new char[namelen+1]; filename[namelen] = 0x00;
  50.     bin->read(filename,namelen);
  51.     cout << dec << noshowbase << width << "x" << height << " "
  52.          << hex << showbase << uppercase << bin->tellg() << " " << filename << endl;
  53.     for (int i = 0; i < namelen; i++)
  54.         if (filename[i] == '\\' || filename[i] == '/')
  55.         filename[i] = '-';
  56.     encodeOneStep(filename, image, width, height);
  57.     delete[] filename;}
  58.  
  59. void extractImages8(const char* filename, int* inc){
  60.     fstream asset(filename, ios::in|ios::binary);
  61.     asset.seekg(0, ios_base::beg);
  62.     unsigned char* palette = new unsigned char[768];
  63.     for (;;){
  64.         if (!asset) break;
  65.         if (asset.get() != static_cast<unsigned char>(0x10)) continue;
  66.         if (asset.get() != static_cast<unsigned char>(0x08)) continue;
  67.         if (asset.get() != static_cast<unsigned char>(0x80)) continue;
  68.         if (asset.get() != static_cast<unsigned char>(0x02)) continue;
  69.         dumpImage8(&asset, palette);
  70.         *inc += 1;}
  71.     asset.close();
  72.     delete[] palette;}
  73.  
  74. void dumpImage32(fstream* bin, int* inc, bool hasName = true){
  75.     bin->seekg(1, ios_base::cur);
  76.     unsigned width = get2(bin);
  77.     bin->seekg(1, ios_base::cur);
  78.     unsigned height = get2(bin);
  79.     bin->seekg(1, ios_base::cur); get2(bin);
  80.     bin->seekg(1, ios_base::cur); get2(bin);
  81.     vector<unsigned char> image;
  82.     image.resize(width*height*4);
  83.     for(unsigned y = 0; y < height; y++)
  84.     for(unsigned x = 0; x < width; x++)
  85.     for(int channel = 0; channel < 4; channel++)
  86.         image[4*width*y + 4*x + channel] = bin->get();
  87.     if (hasName == true){
  88.         bin->seekg(1, ios_base::cur); get2(bin);
  89.         bin->seekg(1, ios_base::cur); get2(bin);
  90.         bin->seekg(1, ios_base::cur);
  91.         int namelen = bin->get();
  92.         char* filename = new char[namelen+1];
  93.         filename[namelen] = 0x00;
  94.         bin->read(filename,namelen);
  95.         cout << dec << noshowbase << width << "x" << height << " "
  96.              << hex << showbase << uppercase << bin->tellg() << " " << filename << endl;
  97.         for (int i = 0; i < namelen; i++)
  98.             if (filename[i] == '\\' || filename[i] == '/')
  99.             filename[i] = '-';
  100.         encodeOneStep(filename, image, width, height);
  101.         delete[] filename;}
  102.     else{
  103.         char filename[6] = {49 + *inc, '.', 'p', 'n', 'g', '\0'};
  104.         encodeOneStep(filename, image, width, height);}}
  105.  
  106. void extractImages32(const char* filename, int* inc, bool hasName = true){
  107.     fstream asset(filename, ios::in|ios::binary);
  108.     asset.seekg(0, ios_base::beg);
  109.     for (;;){
  110.         if (!asset) break;
  111.         if (asset.get() != static_cast<unsigned char>(0x80)) continue;
  112.         if (asset.get() != static_cast<unsigned char>(0xF8)) continue;
  113.         if (asset.get() != static_cast<unsigned char>(0x0F)) continue;
  114.         dumpImage32(&asset, inc, hasName);
  115.         *inc += 1;}
  116.     asset.close();}
  117.  
  118. void extractSound(const char* filename, int* inc, unsigned init = 0, unsigned atrimoden = 0xFFFFFFFF){
  119.     fstream asset(filename, ios::in|ios::binary);
  120.     asset.seekg(init, ios_base::beg);
  121.     for (;;){
  122.         if (!asset) break;
  123.         if (asset.tellg() >= atrimoden) return;
  124.         cout << hex << asset.tellg() << " ";
  125.         asset.seekg(1, ios_base::cur); get2(&asset);
  126.         asset.seekg(1, ios_base::cur); get2(&asset);
  127.         asset.seekg(1, ios_base::cur);
  128.         int namelen = get2(&asset);
  129.         char* outname = new char[namelen+1];
  130.         outname[namelen] = 0x00;
  131.         asset.read(outname,namelen);
  132.         for (int i = 0; i < namelen; i++)
  133.             if (outname[i] == '\\' || outname[i] == '/')
  134.             outname[i] = '-';
  135.         fstream out(outname, ios::out|ios::binary);
  136.         cout << outname << endl;
  137.         int isWav = outname[namelen-1] == 'v';
  138.         if (isWav){
  139.             asset.seekg(1, ios_base::cur); get2(&asset);}
  140.         asset.seekg(1, ios_base::cur);
  141.         int songSize = get2(&asset);
  142.         if (isWav){
  143.             long temp = 0x46464952; out.write(reinterpret_cast<char*>(&temp), 4);
  144.             temp = songSize + 36; out.write(reinterpret_cast<char*>(&temp), 4);
  145.             temp = 0x45564157; out.write(reinterpret_cast<char*>(&temp), 4);
  146.             temp = 0x20746D66; out.write(reinterpret_cast<char*>(&temp), 4);
  147.             temp = 0x00000010; out.write(reinterpret_cast<char*>(&temp), 4);
  148.             temp = 0x00020001; out.write(reinterpret_cast<char*>(&temp), 4);
  149.             temp = 44100; out.write(reinterpret_cast<char*>(&temp), 4);
  150.             temp *= 4; out.write(reinterpret_cast<char*>(&temp), 4);
  151.             temp = 0x00100004; out.write(reinterpret_cast<char*>(&temp), 4);
  152.             temp = 0x61746164; out.write(reinterpret_cast<char*>(&temp), 4);
  153.             temp = songSize; out.write(reinterpret_cast<char*>(&temp), 4);}
  154.         for (; out.tellp() < songSize + isWav*44;)
  155.             out.put(asset.get());
  156.         out.close();
  157.         asset.seekg(1, ios_base::cur); get2(&asset);
  158.         if (isWav){
  159.             asset.seekg(1, ios_base::cur); get2(&asset);}
  160.         *inc += 1;}
  161.     asset.close();}
  162.  
  163. int main(int argc, char** argv){
  164.     extractImages32("cutscenes.mmu", &cutscenesCount);
  165.     extractImages32("gameobjects.mmu", &gameobjectsCount);
  166.     extractImages32("menus.mmu", &menusCount);
  167.     extractImages32("misc.mmu", &miscCount, false);
  168.     extractImages8("levelart.mmu", &levelartCount);
  169.     extractSound("guns.mmu", &gunsCount, 0xB, 0x4000000);
  170.     cout << dec;
  171.     cout << cutscenesCount << " cutscene images exported." << endl;
  172.     cout << gameobjectsCount << " object images exported." << endl;
  173.     cout << levelartCount << " level images exported." << endl;
  174.     cout << menusCount << " menu images exported." << endl;
  175.     cout << miscCount << " miscellaneous images exported." << endl;
  176.     cout << gunsCount << " sounds exported." << endl;
  177.     return EXIT_SUCCESS;}
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top