| 641 | |
| 642 | |
| 643 | bool FCEUSS_LoadFP(EMUFILE* is, ENUM_SSLOADPARAMS params) |
| 644 | { |
| 645 | if(!is) return false; |
| 646 | |
| 647 | //maybe make a backup savestate |
| 648 | bool backup = (params == SSLOADPARAM_BACKUP); |
| 649 | EMUFILE_MEMORY msBackupSavestate; |
| 650 | if(backup) |
| 651 | { |
| 652 | FCEUSS_SaveMS(&msBackupSavestate,Z_NO_COMPRESSION); |
| 653 | } |
| 654 | |
| 655 | uint8 header[16]; |
| 656 | //read and analyze the header |
| 657 | is->fread((char*)&header,16); |
| 658 | if(memcmp(header,"FCSX",4)) { |
| 659 | //its not an fceux save file.. perhaps it is an fceu savefile |
| 660 | is->fseek(0,SEEK_SET); |
| 661 | FCEU_state_loading_old_format = true; |
| 662 | bool ret = FCEUSS_LoadFP_old(is,params)!=0; |
| 663 | FCEU_state_loading_old_format = false; |
| 664 | if(!ret && backup) FCEUSS_LoadFP(&msBackupSavestate,SSLOADPARAM_NOBACKUP); |
| 665 | return ret; |
| 666 | } |
| 667 | |
| 668 | size_t totalsize = FCEU_de32lsb(header + 4); |
| 669 | int stateversion = FCEU_de32lsb(header + 8); |
| 670 | uint32_t comprlen = FCEU_de32lsb(header + 12); |
| 671 | |
| 672 | // reinit memory_savestate |
| 673 | // memory_savestate is global variable which already has its vector of bytes, so no need to allocate memory every time we use save/loadstate |
| 674 | if ((memory_savestate.get_vec())->size() < totalsize) |
| 675 | (memory_savestate.get_vec())->resize(totalsize); |
| 676 | memory_savestate.set_len(totalsize); |
| 677 | memory_savestate.unfail(); |
| 678 | memory_savestate.fseek(0, SEEK_SET); |
| 679 | |
| 680 | if(comprlen != ~0u) |
| 681 | { |
| 682 | // the savestate is compressed: read from is to compressed_buf, then decompress from compressed_buf to memory_savestate.vec |
| 683 | if (compressed_buf.size() < comprlen) compressed_buf.resize(comprlen); |
| 684 | is->fread(&compressed_buf[0], comprlen); |
| 685 | |
| 686 | uLongf uncomprlen = totalsize; |
| 687 | int error = uncompress(memory_savestate.buf(), &uncomprlen, &compressed_buf[0], comprlen); |
| 688 | if(error != Z_OK || uncomprlen != totalsize) |
| 689 | return false; // we dont need to restore the backup here because we havent messed with the emulator state yet |
| 690 | } else |
| 691 | { |
| 692 | // the savestate is not compressed: just read from is to memory_savestate.vec |
| 693 | is->fread(memory_savestate.buf(), totalsize); |
| 694 | } |
| 695 | |
| 696 | FCEUMOV_PreLoad(); |
| 697 | |
| 698 | bool x = (ReadStateChunks(&memory_savestate, totalsize) != 0); |
| 699 | |
| 700 | //mbg 5/24/08 - we don't support old states, so this shouldnt matter. |
no test coverage detected