Advertisement
Guest User

Untitled

a guest
Jan 18th, 2013
360
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.66 KB | None | 0 0
  1. #include "ALTTPFileWriter.hpp"
  2. #include "ALTTPFile.hpp"
  3. #include "ALTTPQuest.hpp"
  4. #include <iostream>
  5.  
  6. ALTTPFileWriter::ALTTPFileWriter(Uint8* data, Uint64 length)
  7.     : BinaryWriter(data, length)
  8. {
  9. }
  10.  
  11. ALTTPFileWriter::ALTTPFileWriter(const std::string& filename)
  12.     : BinaryWriter(filename)
  13. {
  14. }
  15.  
  16. void ALTTPFileWriter::writeFile(ALTTPFile* file)
  17. {
  18.     ALTTPQuest* quest = NULL;
  19.     for (Uint32 i = 0; i < 6; i++)
  20.     {
  21.         if (i < 3)
  22.             quest = file->quest(i);
  23.         else
  24.             quest = file->quest(i - 3);
  25.  
  26.         for (int j = 0; j < 0x140; j++)
  27.         {
  28.             this->writeRoomFlags(quest->roomFlags(j));
  29.         }
  30.  
  31.         for (int j = 0; j < 0x0C0; j++)
  32.         {
  33.             this->writeOverworldEvent(quest->overworldEvent(j));
  34.         }
  35.  
  36.         this->writeBytes((Int8*)quest->inventory(), sizeof(ALTTPInventory));
  37.         this->writeUInt16(quest->rupeeMax());
  38.         this->writeUInt16(quest->rupeeCurrent());
  39.         this->writeDungeonItems(quest->compasses());
  40.         this->writeDungeonItems(quest->bigKeys());
  41.         this->writeDungeonItems(quest->dungeonMaps());
  42.         this->writeUInt16(quest->wishingPond());
  43.         this->writeByte(quest->healthMax());
  44.         this->writeByte(quest->health());
  45.         this->writeByte(quest->magicPower());
  46.         this->writeByte(quest->keys());
  47.         this->writeByte(quest->bombUpgrades());
  48.         this->writeByte(quest->arrowUpgrades());
  49.         this->writeByte(quest->healthFiller());
  50.         this->writeByte(quest->magicFiller());
  51.         ALTTPPendants pendants = quest->pendants();
  52.         this->writeBit(pendants.Courage);
  53.         this->writeBit(pendants.Wisdom);
  54.         this->writeBit(pendants.Power);
  55.         this->writeByte(quest->bombFiller());
  56.         this->writeByte(quest->arrowFiller());
  57.         this->writeByte(quest->arrows());
  58.         this->seek(1);
  59.         ALTTPAbilities abilities = quest->abilityFlags();
  60.         this->writeBit(abilities.Nothing);
  61.         this->writeBit(abilities.Swim);
  62.         this->writeBit(abilities.Dash);
  63.         this->writeBit(abilities.Pull);
  64.         this->writeBit(abilities.Unknown1);
  65.         this->writeBit(abilities.Talk);
  66.         this->writeBit(abilities.Read);
  67.         this->writeBit(abilities.Unknown2);
  68.         ALTTPCrystals crystals = quest->crystals();
  69.         this->writeBytes((Int8*)&crystals, sizeof(ALTTPCrystals));
  70.         ALTTPMagicUsage magicUsage = quest->magicUsage();
  71.         this->writeBytes((Int8*)&magicUsage, sizeof(ALTTPMagicUsage));
  72.  
  73.         for (int j = 0; j < 0x010; j++)
  74.             this->writeByte(quest->dungeonKeys(j));
  75.  
  76.         seek(0x039);
  77.         this->writeByte((Int8)quest->progressIndicator());
  78.         ALTTPProgressFlags1 progress1 = quest->progressFlags1();
  79.         this->writeBytes((Int8*)&progress1, sizeof(ALTTPProgressFlags1));
  80.         this->writeByte(quest->mapIcon());
  81.         this->writeByte(quest->startLocation());
  82.         ALTTPProgressFlags2 progress2 = quest->progressFlags2();
  83.         this->writeBytes((Int8*)&progress2, sizeof(ALTTPProgressFlags2));
  84.         ALTTPLightDarkWorldIndicator indicator = quest->lightDarkWorldIndicator();
  85.         this->writeBytes((Int8*)&indicator, 1);
  86.         this->seek(1);
  87.         this->writeByte(quest->tagAlong());
  88.  
  89.         for(int j = 0; j < 6; j++)
  90.             this->writeByte(quest->oldManFlag(j));
  91.  
  92.         this->writeByte(quest->bombFlag());
  93.  
  94.         for (int j = 0; j < 5; j++)
  95.             this->writeByte(quest->unknown1(j));
  96.  
  97.         for (int j = 0; j < 6; j++)
  98.             this->writeUInt16(quest->playerName()[j]);
  99.  
  100.         this->writeUInt16((quest->valid() == true ? 0x55AA : 0));
  101.  
  102.         for (int j = 0; j < 0x0D; j++)
  103.             this->writeUInt16(quest->dungeonDeathTotal(j));
  104.  
  105.  
  106.         this->writeUInt16(quest->unknown2());
  107.         this->writeUInt16(quest->deathSaveCount());
  108.         this->writeUInt16(quest->postGameDeathCounter());
  109.  
  110.         this->seek(0xF7);
  111.         this->writeUInt16(calculateChecksum(i));
  112.     }
  113. }
  114.  
  115. void ALTTPFileWriter::writeRoomFlags(ALTTPRoomFlags* flags)
  116. {
  117.     writeBit(flags->Quadrant1);
  118.     writeBit(flags->Quadrant2);
  119.     writeBit(flags->Quadrant3);
  120.     writeBit(flags->Quadrant4);
  121.     writeBit(flags->Chest1);
  122.     writeBit(flags->Chest2);
  123.     writeBit(flags->Chest3);
  124.     writeBit(flags->Chest4);
  125.     writeBit(flags->Door1);
  126.     writeBit(flags->Door2);
  127.     writeBit(flags->Door3);
  128.     writeBit(flags->Door4);
  129.     writeBit(flags->BossBattleWon);
  130.     writeBit(flags->Key);
  131.     writeBit(flags->KeyOrChest);
  132.     writeBit(flags->ChestOrTile);
  133. }
  134.  
  135. void ALTTPFileWriter::writeOverworldEvent(ALTTPOverworldEvent* event)
  136. {
  137.     writeBit(event->Unused1);
  138.     writeBit(event->HeartPiece);
  139.     writeBit(event->Overlay);
  140.     writeBit(event->Unused2);
  141.     writeBit(event->Unused3);
  142.     writeBit(event->Unused4);
  143.     writeBit(event->Set);
  144.     writeBit(event->Unused5);
  145. }
  146.  
  147. void ALTTPFileWriter::writeDungeonItems(ALTTPDungeonItemFlags flags)
  148. {
  149.     writeBit(flags.Unused1);
  150.     writeBit(flags.Unused2);
  151.     writeBit(flags.GanonsTower);
  152.     writeBit(flags.TurtleRock);
  153.     writeBit(flags.TowerOfHera);
  154.     writeBit(flags.IcePalace);
  155.     writeBit(flags.SkullWoods);
  156.     writeBit(flags.MiseryMire);
  157.     writeBit(flags.DarkPalace);
  158.     writeBit(flags.SwampPalace);
  159.     writeBit(flags.HyruleCastle2);
  160.     writeBit(flags.DesertPalace);
  161.     writeBit(flags.EasternPalace);
  162.     writeBit(flags.HyruleCastle);
  163.     writeBit(flags.SewerPassage);
  164. }
  165.  
  166. Uint16 ALTTPFileWriter::calculateChecksum(Uint32 game)
  167. {
  168.     Uint16 sum = 0x5a5a;
  169.     for (Uint32 i = 0; i < 0x4FE; i += 2)
  170.         sum -= *(Uint16*)(m_data + (i + (0x500 * game)));
  171.  
  172.     std::cout << std::hex << sum << std::endl;
  173.     return sum;
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement