2006-07-29 05:46:15 +00:00
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
2008-05-22 07:43:48 +00:00
# include <assert.h>
2008-06-03 05:01:07 +00:00
# include <zlib.h>
2008-06-06 08:23:12 +00:00
# include <iomanip>
2008-06-17 06:55:07 +00:00
# include <fstream>
2008-06-17 08:32:24 +00:00
# include <limits.h>
2008-12-01 00:37:40 +00:00
# include <stdarg.h>
2008-05-22 07:43:48 +00:00
2006-07-29 05:46:15 +00:00
# include "types.h"
2006-08-01 05:50:19 +00:00
# include "utils/endian.h"
2006-07-29 05:46:15 +00:00
# include "palette.h"
# include "input.h"
# include "fceu.h"
# include "driver.h"
# include "state.h"
2006-08-01 05:50:19 +00:00
# include "file.h"
2006-07-29 05:46:15 +00:00
# include "video.h"
# include "movie.h"
2008-09-24 06:06:31 +00:00
# ifdef _S9XLUA_H
2008-07-23 06:54:14 +00:00
# include "fceulua.h"
2008-09-24 06:06:31 +00:00
# endif
2008-06-17 06:55:07 +00:00
# include "utils/guid.h"
2008-05-23 09:58:38 +00:00
# include "utils/memory.h"
2008-06-03 04:04:04 +00:00
# include "utils/memorystream.h"
2008-05-23 09:58:38 +00:00
# include "utils/xstring.h"
2008-11-23 21:28:22 +00:00
# include <sstream>
2008-05-24 17:53:14 +00:00
2008-10-25 12:36:03 +00:00
# ifdef CREATE_AVI
# include "drivers/videolog/nesvideos-piece.h"
# endif
2008-08-18 20:34:59 +00:00
# ifdef WIN32
# include <windows.h>
2009-05-25 15:42:24 +00:00
extern void AddRecentMovieFile ( const char * filename ) ;
2008-08-18 20:34:59 +00:00
# endif
2008-06-03 04:04:04 +00:00
using namespace std ;
2008-06-22 06:53:49 +00:00
# define MOVIE_VERSION 3
2006-07-29 05:46:15 +00:00
extern char FileBase [ ] ;
2008-12-24 21:26:09 +00:00
extern bool AutoSS ; //Declared in fceu.cpp, keeps track if a auto-savestate has been made
2008-05-26 01:10:13 +00:00
2008-11-24 00:45:25 +00:00
std : : vector < int > subtitleFrames ; //Frame numbers for subtitle messages
std : : vector < string > subtitleMessages ; //Messages of subtitles
bool subtitlesOnAVI = false ;
2008-12-23 01:07:55 +00:00
bool autoMovieBackup = false ; //Toggle that determines if movies should be backed up automatically before altering them
bool freshMovie = false ; //True when a movie loads, false when movie is altered. Used to determine if a movie has been altered since opening
// Function declarations------------------------
2006-07-29 05:46:15 +00:00
2008-05-23 09:58:38 +00:00
//TODO - remove the synchack stuff from the replay gui and require it to be put into the fm2 file
//which the user would have already converted from fcm
//also cleanup the whole emulator version bullshit in replay. we dont support that old stuff anymore
2008-05-26 00:03:20 +00:00
//todo - better error handling for the compressed savestate
2006-07-29 05:46:15 +00:00
2008-05-26 00:03:20 +00:00
//todo - consider a MemoryBackedFile class..
//..a lot of work.. instead lets just read back from the current fcm
2006-07-29 05:46:15 +00:00
2008-05-26 00:03:20 +00:00
//todo - handle case where read+write is requested, but the file is actually readonly (could be confusing)
2006-07-29 05:46:15 +00:00
2008-06-20 06:52:20 +00:00
//todo - could we, given a field size, over-read from an inputstream and then parse out an integer?
//that would be faster than several reads, perhaps.
2008-05-23 09:58:38 +00:00
//sometimes we accidentally produce movie stop signals while we're trying to do other things with movies..
bool suppressMovieStop = false ;
2006-07-29 05:46:15 +00:00
2008-05-26 00:03:20 +00:00
//----movie engine main state
2008-05-23 09:58:38 +00:00
2008-06-05 06:49:11 +00:00
EMOVIEMODE movieMode = MOVIEMODE_INACTIVE ;
2006-07-29 05:46:15 +00:00
2008-05-23 09:58:38 +00:00
//this should not be set unless we are in MOVIEMODE_RECORD!
2008-06-06 08:23:12 +00:00
//FILE* fpRecordingMovie = 0;
2008-10-25 12:36:03 +00:00
std : : ostream * osRecordingMovie = 0 ;
2008-05-26 00:03:20 +00:00
2008-05-23 09:58:38 +00:00
int currFrameCounter ;
2008-05-26 00:03:20 +00:00
uint32 cur_input_display = 0 ;
2008-06-21 07:24:01 +00:00
int pauseframe = - 1 ;
2008-05-26 00:03:20 +00:00
bool movie_readonly = true ;
int input_display = 0 ;
int frame_display = 0 ;
2008-05-23 09:58:38 +00:00
2008-05-24 22:18:16 +00:00
SFORMAT FCEUMOV_STATEINFO [ ] = {
{ & currFrameCounter , 4 | FCEUSTATE_RLSB , " FCNT " } ,
{ 0 }
} ;
2008-05-26 07:34:40 +00:00
char curMovieFilename [ 512 ] = { 0 } ;
2008-05-28 07:41:49 +00:00
MovieData currMovieData ;
2008-06-27 04:39:14 +00:00
int currRerecordCount ;
2008-05-23 09:58:38 +00:00
2008-05-28 07:41:49 +00:00
void MovieData : : clearRecordRange ( int start , int len )
2006-07-29 05:46:15 +00:00
{
2008-05-28 07:41:49 +00:00
for ( int i = 0 ; i < len ; i + + )
records [ i + start ] . clear ( ) ;
}
2008-05-26 00:03:20 +00:00
2008-06-27 07:16:08 +00:00
void MovieData : : insertEmpty ( int at , int frames )
{
if ( at = = - 1 )
{
int currcount = records . size ( ) ;
records . resize ( records . size ( ) + frames ) ;
clearRecordRange ( currcount , frames ) ;
}
else
{
records . insert ( records . begin ( ) + at , frames , MovieRecord ( ) ) ;
clearRecordRange ( at , frames ) ;
}
}
2008-05-28 07:41:49 +00:00
void MovieData : : TryDumpIncremental ( )
{
2008-06-05 06:49:11 +00:00
if ( movieMode = = MOVIEMODE_TASEDIT )
2008-05-26 00:03:20 +00:00
{
2008-05-28 07:41:49 +00:00
//only log the savestate if we are appending to the green zone
if ( currFrameCounter = = currMovieData . greenZoneCount )
2008-05-26 00:03:20 +00:00
{
2008-06-28 22:03:59 +00:00
if ( currFrameCounter = = ( int ) currMovieData . records . size ( ) | | currMovieData . records . size ( ) = = 0 )
2008-05-26 00:03:20 +00:00
{
2008-06-28 22:03:59 +00:00
currMovieData . insertEmpty ( - 1 , 1 ) ;
2008-05-26 00:03:20 +00:00
}
2008-06-28 22:03:59 +00:00
MovieData : : dumpSavestateTo ( & currMovieData . records [ currFrameCounter ] . savestate , Z_DEFAULT_COMPRESSION ) ;
currMovieData . greenZoneCount + + ;
2008-05-26 00:03:20 +00:00
}
}
2008-05-28 07:41:49 +00:00
}
2006-07-29 05:46:15 +00:00
2008-06-27 07:16:08 +00:00
void MovieRecord : : clear ( )
{
2008-06-29 16:24:37 +00:00
commands = 0 ;
2008-06-27 07:16:08 +00:00
* ( uint32 * ) & joysticks = 0 ;
memset ( zappers , 0 , sizeof ( zappers ) ) ;
}
2008-05-28 07:41:49 +00:00
const char MovieRecord : : mnemonics [ 8 ] = { ' A ' , ' B ' , ' S ' , ' T ' , ' U ' , ' D ' , ' L ' , ' R ' } ;
2008-06-09 08:15:20 +00:00
void MovieRecord : : dumpJoy ( std : : ostream * os , uint8 joystate )
{
//these are mnemonics for each joystick bit.
//since we usually use the regular joypad, these will be more helpful.
2008-07-04 21:09:49 +00:00
//but any character other than ' ' or '.' should count as a set bit
2008-06-09 08:15:20 +00:00
//maybe other input types will need to be encoded another way..
for ( int bit = 7 ; bit > = 0 ; bit - - )
{
int bitmask = ( 1 < < bit ) ;
char mnemonic = mnemonics [ bit ] ;
//if the bit is set write the mnemonic
if ( joystate & bitmask )
os - > put ( mnemonic ) ;
2008-07-04 21:09:49 +00:00
else //otherwise write an unset bit
os - > put ( ' . ' ) ;
2008-06-09 08:15:20 +00:00
}
}
void MovieRecord : : parseJoy ( std : : istream * is , uint8 & joystate )
{
2008-06-12 07:10:46 +00:00
char buf [ 8 ] ;
is - > read ( buf , 8 ) ;
2008-06-09 08:15:20 +00:00
joystate = 0 ;
2008-06-12 07:10:46 +00:00
for ( int i = 0 ; i < 8 ; i + + )
2008-06-09 08:15:20 +00:00
{
2008-06-12 07:10:46 +00:00
joystate < < = 1 ;
2008-07-04 21:09:49 +00:00
joystate | = ( ( buf [ i ] = = ' . ' | | buf [ i ] = = ' ' ) ? 0 : 1 ) ;
2008-06-09 08:15:20 +00:00
}
}
void MovieRecord : : parse ( MovieData * md , std : : istream * is )
{
//by the time we get in here, the initial pipe has already been extracted
2008-06-12 05:07:30 +00:00
//extract the commands
2008-06-20 06:52:20 +00:00
commands = uint32DecFromIstream ( is ) ;
2008-06-12 07:20:15 +00:00
//*is >> commands;
2008-06-12 05:07:30 +00:00
is - > get ( ) ; //eat the pipe
2008-06-09 08:15:20 +00:00
//a special case: if fourscore is enabled, parse four gamepads
if ( md - > fourscore )
{
parseJoy ( is , joysticks [ 0 ] ) ; is - > get ( ) ; //eat the pipe
parseJoy ( is , joysticks [ 1 ] ) ; is - > get ( ) ; //eat the pipe
parseJoy ( is , joysticks [ 2 ] ) ; is - > get ( ) ; //eat the pipe
parseJoy ( is , joysticks [ 3 ] ) ; is - > get ( ) ; //eat the pipe
}
else
{
for ( int port = 0 ; port < 2 ; port + + )
{
if ( md - > ports [ port ] = = SI_GAMEPAD )
parseJoy ( is , joysticks [ port ] ) ;
else if ( md - > ports [ port ] = = SI_ZAPPER )
{
2008-06-20 06:52:20 +00:00
zappers [ port ] . x = uint32DecFromIstream ( is ) ;
zappers [ port ] . y = uint32DecFromIstream ( is ) ;
zappers [ port ] . b = uint32DecFromIstream ( is ) ;
zappers [ port ] . bogo = uint32DecFromIstream ( is ) ;
zappers [ port ] . zaphit = uint64DecFromIstream ( is ) ;
2008-06-09 08:15:20 +00:00
}
is - > get ( ) ; //eat the pipe
}
}
//(no fcexp data is logged right now)
is - > get ( ) ; //eat the pipe
//should be left at a newline
}
2008-06-20 22:15:30 +00:00
bool MovieRecord : : parseBinary ( MovieData * md , std : : istream * is )
{
commands = ( uint8 ) is - > get ( ) ;
//check for eof
if ( is - > gcount ( ) ! = 1 ) return false ;
if ( md - > fourscore )
{
is - > read ( ( char * ) & joysticks , 4 ) ;
}
else
{
for ( int port = 0 ; port < 2 ; port + + )
{
if ( md - > ports [ port ] = = SI_GAMEPAD )
joysticks [ port ] = ( uint8 ) is - > get ( ) ;
else if ( md - > ports [ port ] = = SI_ZAPPER )
{
zappers [ port ] . x = ( uint8 ) is - > get ( ) ;
zappers [ port ] . y = ( uint8 ) is - > get ( ) ;
zappers [ port ] . b = ( uint8 ) is - > get ( ) ;
zappers [ port ] . bogo = ( uint8 ) is - > get ( ) ;
2008-06-28 20:20:40 +00:00
read64le ( & zappers [ port ] . zaphit , is ) ;
2008-06-20 22:15:30 +00:00
}
}
}
return true ;
}
void MovieRecord : : dumpBinary ( MovieData * md , std : : ostream * os , int index )
{
os - > put ( commands ) ;
if ( md - > fourscore )
{
os - > put ( joysticks [ 0 ] ) ;
os - > put ( joysticks [ 1 ] ) ;
os - > put ( joysticks [ 2 ] ) ;
os - > put ( joysticks [ 3 ] ) ;
}
else
{
for ( int port = 0 ; port < 2 ; port + + )
{
if ( md - > ports [ port ] = = SI_GAMEPAD )
os - > put ( joysticks [ port ] ) ;
else if ( md - > ports [ port ] = = SI_ZAPPER )
{
os - > put ( zappers [ port ] . x ) ;
os - > put ( zappers [ port ] . y ) ;
os - > put ( zappers [ port ] . b ) ;
os - > put ( zappers [ port ] . bogo ) ;
write64le ( zappers [ port ] . zaphit , os ) ;
}
}
}
}
2008-06-09 08:15:20 +00:00
void MovieRecord : : dump ( MovieData * md , std : : ostream * os , int index )
2008-06-03 04:04:04 +00:00
{
2008-06-12 05:07:30 +00:00
//dump the misc commands
2008-06-20 06:52:20 +00:00
//*os << '|' << setw(1) << (int)commands;
os - > put ( ' | ' ) ;
putdec < uint8 , 1 , true > ( os , commands ) ;
2008-06-12 05:07:30 +00:00
2008-06-09 08:15:20 +00:00
//a special case: if fourscore is enabled, dump four gamepads
if ( md - > fourscore )
{
os - > put ( ' | ' ) ;
dumpJoy ( os , joysticks [ 0 ] ) ; os - > put ( ' | ' ) ;
dumpJoy ( os , joysticks [ 1 ] ) ; os - > put ( ' | ' ) ;
dumpJoy ( os , joysticks [ 2 ] ) ; os - > put ( ' | ' ) ;
dumpJoy ( os , joysticks [ 3 ] ) ; os - > put ( ' | ' ) ;
}
else
2008-06-03 04:04:04 +00:00
{
2008-06-09 08:15:20 +00:00
for ( int port = 0 ; port < 2 ; port + + )
2008-06-03 04:04:04 +00:00
{
2008-06-09 08:15:20 +00:00
os - > put ( ' | ' ) ;
if ( md - > ports [ port ] = = SI_GAMEPAD )
dumpJoy ( os , joysticks [ port ] ) ;
else if ( md - > ports [ port ] = = SI_ZAPPER )
2008-06-20 06:52:20 +00:00
{
putdec < uint8 , 3 , true > ( os , zappers [ port ] . x ) ; os - > put ( ' ' ) ;
putdec < uint8 , 3 , true > ( os , zappers [ port ] . y ) ; os - > put ( ' ' ) ;
putdec < uint8 , 1 , true > ( os , zappers [ port ] . b ) ; os - > put ( ' ' ) ;
putdec < uint8 , 1 , true > ( os , zappers [ port ] . bogo ) ; os - > put ( ' ' ) ;
putdec < uint64 , 20 , false > ( os , zappers [ port ] . zaphit ) ;
}
2008-06-03 04:04:04 +00:00
}
2008-06-06 08:23:12 +00:00
os - > put ( ' | ' ) ;
2008-06-03 04:04:04 +00:00
}
2008-06-09 08:15:20 +00:00
//(no fcexp data is logged right now)
os - > put ( ' | ' ) ;
2006-07-29 05:46:15 +00:00
2008-05-28 07:41:49 +00:00
//each frame is on a new line
2008-06-06 08:23:12 +00:00
os - > put ( ' \n ' ) ;
2008-05-28 07:41:49 +00:00
}
2008-05-23 09:58:38 +00:00
2008-05-28 07:41:49 +00:00
MovieData : : MovieData ( )
: version ( MOVIE_VERSION )
, emuVersion ( FCEU_VERSION_NUMERIC )
, palFlag ( false )
2008-06-22 06:53:49 +00:00
, rerecordCount ( 1 )
2008-10-25 12:36:03 +00:00
, binaryFlag ( false )
2008-05-28 07:41:49 +00:00
, greenZoneCount ( 0 )
{
memset ( & romChecksum , 0 , sizeof ( MD5DATA ) ) ;
}
2006-07-29 05:46:15 +00:00
2008-05-28 07:41:49 +00:00
void MovieData : : truncateAt ( int frame )
{
records . resize ( frame ) ;
}
2008-05-23 09:58:38 +00:00
2008-06-09 08:15:20 +00:00
void MovieData : : installValue ( std : : string & key , std : : string & val )
{
//todo - use another config system, or drive this from a little data structure. because this is gross
if ( key = = " version " )
installInt ( val , version ) ;
else if ( key = = " emuVersion " )
installInt ( val , emuVersion ) ;
2008-06-22 06:53:49 +00:00
else if ( key = = " rerecordCount " )
installInt ( val , rerecordCount ) ;
2008-06-09 08:15:20 +00:00
else if ( key = = " palFlag " )
installBool ( val , palFlag ) ;
else if ( key = = " romFilename " )
romFilename = val ;
else if ( key = = " romChecksum " )
StringToBytes ( val , & romChecksum , MD5DATA : : size ) ;
else if ( key = = " guid " )
guid = FCEU_Guid : : fromString ( val ) ;
else if ( key = = " fourscore " )
installBool ( val , fourscore ) ;
else if ( key = = " port0 " )
installInt ( val , ports [ 0 ] ) ;
else if ( key = = " port1 " )
installInt ( val , ports [ 1 ] ) ;
else if ( key = = " port2 " )
installInt ( val , ports [ 2 ] ) ;
2008-06-20 22:15:30 +00:00
else if ( key = = " binary " )
installBool ( val , binaryFlag ) ;
2008-06-22 23:41:24 +00:00
else if ( key = = " comment " )
2008-08-10 08:59:23 +00:00
comments . push_back ( mbstowcs ( val ) ) ;
2008-11-23 21:28:22 +00:00
else if ( key = = " subtitle " )
subtitles . push_back ( val ) ; //mbstowcs(val));
2008-06-09 08:15:20 +00:00
else if ( key = = " savestate " )
2008-05-23 09:58:38 +00:00
{
2008-07-24 03:01:39 +00:00
int len = Base64StringToBytesLength ( val ) ;
if ( len = = - 1 ) len = HexStringToBytesLength ( val ) ; // wasn't base64, try hex
2008-05-28 07:41:49 +00:00
if ( len > = 1 )
2008-05-23 09:58:38 +00:00
{
2008-05-28 07:41:49 +00:00
savestate . resize ( len ) ;
2008-07-24 03:01:39 +00:00
StringToBytes ( val , & savestate [ 0 ] , len ) ; // decodes either base64 or hex
2008-05-23 09:58:38 +00:00
}
2006-07-29 05:46:15 +00:00
}
2008-05-28 07:41:49 +00:00
}
2006-07-29 05:46:15 +00:00
2008-06-20 22:15:30 +00:00
int MovieData : : dump ( std : : ostream * os , bool binary )
2008-06-02 14:02:02 +00:00
{
2008-06-17 07:55:27 +00:00
int start = os - > tellp ( ) ;
2008-06-03 04:04:04 +00:00
* os < < " version " < < version < < endl ;
* os < < " emuVersion " < < emuVersion < < endl ;
2008-06-22 06:53:49 +00:00
* os < < " rerecordCount " < < rerecordCount < < endl ;
2008-06-03 04:04:04 +00:00
* os < < " palFlag " < < ( palFlag ? 1 : 0 ) < < endl ;
* os < < " romFilename " < < romFilename < < endl ;
* os < < " romChecksum " < < BytesToString ( romChecksum . data , MD5DATA : : size ) < < endl ;
2008-06-03 05:06:54 +00:00
* os < < " guid " < < guid . toString ( ) < < endl ;
2008-06-09 08:15:20 +00:00
* os < < " fourscore " < < ( fourscore ? 1 : 0 ) < < endl ;
* os < < " port0 " < < ports [ 0 ] < < endl ;
* os < < " port1 " < < ports [ 1 ] < < endl ;
* os < < " port2 " < < ports [ 2 ] < < endl ;
2008-06-22 22:25:30 +00:00
2008-06-22 23:41:24 +00:00
for ( uint32 i = 0 ; i < comments . size ( ) ; i + + )
2008-08-10 08:59:23 +00:00
* os < < " comment " < < wcstombs ( comments [ i ] ) < < endl ;
2008-11-23 21:28:22 +00:00
for ( uint32 i = 0 ; i < subtitles . size ( ) ; i + + )
* os < < " subtitle " < < subtitles [ i ] < < endl ;
2008-06-20 22:15:30 +00:00
if ( binary )
* os < < " binary 1 " < < endl ;
2008-06-03 05:06:54 +00:00
2008-06-03 04:04:04 +00:00
if ( savestate . size ( ) ! = 0 )
* os < < " savestate " < < BytesToString ( & savestate [ 0 ] , savestate . size ( ) ) < < endl ;
2008-06-20 22:15:30 +00:00
if ( binary )
{
//put one | to start the binary dump
os - > put ( ' | ' ) ;
for ( int i = 0 ; i < ( int ) records . size ( ) ; i + + )
records [ i ] . dumpBinary ( this , os , i ) ;
}
else
for ( int i = 0 ; i < ( int ) records . size ( ) ; i + + )
records [ i ] . dump ( this , os , i ) ;
2008-06-17 07:55:27 +00:00
int end = os - > tellp ( ) ;
return end - start ;
2008-06-02 14:02:02 +00:00
}
2008-05-26 00:03:20 +00:00
int FCEUMOV_GetFrame ( void )
{
return currFrameCounter ;
}
2008-05-24 22:18:16 +00:00
2009-03-07 19:26:07 +00:00
int FCEUI_GetLagCount ( void )
{
return lagCounter ;
}
2009-03-08 19:14:37 +00:00
bool FCEUI_GetLagged ( void )
{
if ( lagFlag ) return true ;
else return false ;
}
2008-05-26 00:03:20 +00:00
bool FCEUMOV_ShouldPause ( void )
2008-05-23 09:58:38 +00:00
{
2009-05-04 13:38:41 +00:00
if ( pauseframe & & currFrameCounter = = ( pauseframe - 1 ) ) //adelikat: changed to pauseframe -1 to prevent an off by 1 error. THis is probably the hackiest solution but I think it would cause some major restructuring to fix it properly.
2008-05-26 00:03:20 +00:00
{
pauseframe = 0 ; //only pause once!
return true ;
}
else
{
return false ;
}
}
2008-06-05 06:49:11 +00:00
EMOVIEMODE FCEUMOV_Mode ( )
{
return movieMode ;
}
bool FCEUMOV_Mode ( EMOVIEMODE modemask )
2008-05-26 00:03:20 +00:00
{
2008-06-05 06:49:11 +00:00
return ( movieMode & modemask ) ! = 0 ;
2008-05-26 00:03:20 +00:00
}
2008-06-05 06:49:11 +00:00
bool FCEUMOV_Mode ( int modemask )
2008-05-26 00:03:20 +00:00
{
2008-06-05 06:49:11 +00:00
return FCEUMOV_Mode ( ( EMOVIEMODE ) modemask ) ;
2006-07-29 05:46:15 +00:00
}
2008-06-20 22:15:30 +00:00
static void LoadFM2_binarychunk ( MovieData & movieData , std : : istream * fp , int size )
{
int recordsize = 1 ; //1 for the command
if ( movieData . fourscore )
recordsize + = 4 ; //4 joysticks
else
{
for ( int i = 0 ; i < 2 ; i + + )
{
switch ( movieData . ports [ i ] )
{
case SI_GAMEPAD : recordsize + + ; break ;
2008-06-28 20:20:40 +00:00
case SI_ZAPPER : recordsize + = 12 ; break ;
2008-06-20 22:15:30 +00:00
}
}
}
//find out how much remains in the file
int curr = fp - > tellg ( ) ;
fp - > seekg ( 0 , std : : ios : : end ) ;
int end = fp - > tellg ( ) ;
int flen = end - curr ;
fp - > seekg ( curr , std : : ios : : beg ) ;
//the amount todo is the min of the limiting size we received and the remaining contents of the file
int todo = std : : min ( size , flen ) ;
int numRecords = todo / recordsize ;
movieData . records . resize ( numRecords ) ;
for ( int i = 0 ; i < numRecords ; i + + )
{
movieData . records [ i ] . parseBinary ( & movieData , fp ) ;
}
}
2008-05-23 09:58:38 +00:00
//yuck... another custom text parser.
2009-04-23 21:34:30 +00:00
bool LoadFM2 ( MovieData & movieData , std : : istream * fp , int size , bool stopAfterHeader )
2006-07-29 05:46:15 +00:00
{
2008-08-12 06:58:52 +00:00
//first, look for an fcm signature
char fcmbuf [ 3 ] ;
std : : ios : : pos_type curr = fp - > tellg ( ) ;
fp - > read ( fcmbuf , 3 ) ;
fp - > seekg ( curr ) ;
if ( ! strncmp ( fcmbuf , " FCM " , 3 ) ) {
FCEU_PrintError ( " FCM File format is no longer supported. Please use Tools > Convert FCM " ) ;
return false ;
}
2008-08-10 02:33:14 +00:00
//movie must start with "version 3"
char buf [ 9 ] ;
2008-08-12 06:58:52 +00:00
curr = fp - > tellg ( ) ;
2008-08-10 02:33:14 +00:00
fp - > read ( buf , 9 ) ;
2008-08-10 04:03:50 +00:00
fp - > seekg ( curr ) ;
2008-08-10 02:33:14 +00:00
if ( fp - > fail ( ) ) return false ;
if ( memcmp ( buf , " version 3 " , 9 ) )
return false ;
2008-05-23 09:58:38 +00:00
std : : string key , value ;
enum {
2008-11-23 21:28:22 +00:00
NEWLINE , KEY , SEPARATOR , VALUE , RECORD , COMMENT , SUBTITLE
2008-05-23 09:58:38 +00:00
} state = NEWLINE ;
bool bail = false ;
for ( ; ; )
2007-02-06 18:59:15 +00:00
{
2008-06-19 03:26:37 +00:00
bool iswhitespace , isrecchar , isnewline ;
2008-06-17 16:59:16 +00:00
int c ;
2008-06-17 08:32:24 +00:00
if ( size - - < = 0 ) goto bail ;
2008-06-17 16:59:16 +00:00
c = fp - > get ( ) ;
2008-05-23 09:58:38 +00:00
if ( c = = - 1 )
goto bail ;
2008-05-26 00:33:27 +00:00
iswhitespace = ( c = = ' ' | | c = = ' \t ' ) ;
isrecchar = ( c = = ' | ' ) ;
isnewline = ( c = = 10 | | c = = 13 ) ;
2008-06-20 22:15:30 +00:00
if ( isrecchar & & movieData . binaryFlag & & ! stopAfterHeader )
{
LoadFM2_binarychunk ( movieData , fp , size ) ;
2008-08-10 02:33:14 +00:00
return true ;
2008-06-20 22:15:30 +00:00
}
2008-05-23 09:58:38 +00:00
switch ( state )
{
case NEWLINE :
if ( isnewline ) goto done ;
2008-05-24 22:18:16 +00:00
if ( iswhitespace ) goto done ;
if ( isrecchar )
goto dorecord ;
//must be a key
key = " " ;
value = " " ;
goto dokey ;
2008-05-23 09:58:38 +00:00
break ;
2008-06-06 08:23:12 +00:00
case RECORD :
{
2008-05-23 09:58:38 +00:00
dorecord :
2008-08-10 02:33:14 +00:00
if ( stopAfterHeader ) return true ;
2008-06-20 22:15:30 +00:00
int currcount = movieData . records . size ( ) ;
movieData . records . resize ( currcount + 1 ) ;
2008-06-17 08:32:24 +00:00
int preparse = fp - > tellg ( ) ;
2008-06-20 22:15:30 +00:00
movieData . records [ currcount ] . parse ( & movieData , fp ) ;
2008-06-17 08:32:24 +00:00
int postparse = fp - > tellg ( ) ;
size - = ( postparse - preparse ) ;
2008-05-23 09:58:38 +00:00
state = NEWLINE ;
break ;
}
2008-06-06 08:23:12 +00:00
2008-05-23 09:58:38 +00:00
case KEY :
dokey : //dookie
state = KEY ;
if ( iswhitespace ) goto doseparator ;
if ( isnewline ) goto commit ;
key + = c ;
break ;
case SEPARATOR :
doseparator :
state = SEPARATOR ;
if ( isnewline ) goto commit ;
if ( ! iswhitespace ) goto dovalue ;
break ;
case VALUE :
dovalue :
state = VALUE ;
if ( isnewline ) goto commit ;
value + = c ;
}
goto done ;
bail :
bail = true ;
if ( state = = VALUE ) goto commit ;
2008-06-19 16:19:48 +00:00
goto done ;
2008-05-23 09:58:38 +00:00
commit :
2008-06-09 08:15:20 +00:00
movieData . installValue ( key , value ) ;
2008-05-23 09:58:38 +00:00
state = NEWLINE ;
done : ;
2008-06-19 16:19:48 +00:00
if ( bail ) break ;
2007-02-06 18:59:15 +00:00
}
2008-08-10 02:33:14 +00:00
return true ;
2008-05-23 09:58:38 +00:00
}
2007-02-06 18:59:15 +00:00
2008-06-22 06:53:49 +00:00
static void closeRecordingMovie ( )
{
if ( osRecordingMovie )
{
delete osRecordingMovie ;
osRecordingMovie = 0 ;
}
}
2007-02-06 18:59:15 +00:00
2008-05-23 09:58:38 +00:00
/// Stop movie playback.
2008-06-22 06:53:49 +00:00
static void StopPlayback ( )
2008-05-23 09:58:38 +00:00
{
2008-05-27 04:40:03 +00:00
FCEU_DispMessageOnMovie ( " Movie playback stopped. " ) ;
2008-05-23 09:58:38 +00:00
movieMode = MOVIEMODE_INACTIVE ;
}
/// Stop movie recording
2008-06-22 06:53:49 +00:00
static void StopRecording ( )
2008-05-23 09:58:38 +00:00
{
FCEU_DispMessage ( " Movie recording stopped. " ) ;
2007-02-06 18:59:15 +00:00
2008-05-26 00:03:20 +00:00
movieMode = MOVIEMODE_INACTIVE ;
2008-06-22 06:53:49 +00:00
closeRecordingMovie ( ) ;
2006-07-29 05:46:15 +00:00
}
2008-05-23 09:58:38 +00:00
2007-02-06 18:59:15 +00:00
void FCEUI_StopMovie ( )
2006-07-29 05:46:15 +00:00
{
2008-05-23 09:58:38 +00:00
if ( suppressMovieStop )
return ;
if ( movieMode = = MOVIEMODE_PLAY )
2007-02-06 18:59:15 +00:00
StopPlayback ( ) ;
2008-05-23 09:58:38 +00:00
else if ( movieMode = = MOVIEMODE_RECORD )
2007-02-06 18:59:15 +00:00
StopRecording ( ) ;
2008-05-26 07:34:40 +00:00
2008-12-24 21:26:09 +00:00
curMovieFilename [ 0 ] = 0 ; //No longer a current movie filename
freshMovie = false ; //No longer a fresh movie loaded
if ( bindSavestate ) AutoSS = false ; //If bind movies to savestates is true, then there is no longer a valid auto-save to load
2006-07-29 05:46:15 +00:00
}
2008-06-05 06:49:11 +00:00
static void poweron ( bool shouldDisableBatteryLoading )
{
2008-06-25 06:34:08 +00:00
//// make a for-movie-recording power-on clear the game's save data, too
2008-08-12 06:52:48 +00:00
//extern char lastLoadedGameName [2048];
//extern int disableBatteryLoading, suppressAddPowerCommand;
//suppressAddPowerCommand=1;
//if(shouldDisableBatteryLoading) disableBatteryLoading=1;
//suppressMovieStop=true;
//{
// //we need to save the pause state through this process
// int oldPaused = EmulationPaused;
// // NOTE: this will NOT write an FCEUNPCMD_POWER into the movie file
// FCEUGI* gi = FCEUI_LoadGame(lastLoadedGameName, 0);
// assert(gi);
// PowerNES();
// EmulationPaused = oldPaused;
//}
//suppressMovieStop=false;
//if(shouldDisableBatteryLoading) disableBatteryLoading=0;
//suppressAddPowerCommand=0;
2008-06-28 23:31:02 +00:00
extern int disableBatteryLoading ;
2008-08-12 06:52:48 +00:00
disableBatteryLoading = 1 ;
2008-06-25 06:34:08 +00:00
PowerNES ( ) ;
2008-08-12 06:52:48 +00:00
disableBatteryLoading = 0 ;
2008-06-05 06:49:11 +00:00
}
void FCEUMOV_EnterTasEdit ( )
{
//stop any current movie activity
FCEUI_StopMovie ( ) ;
//clear the current movie
currFrameCounter = 0 ;
currMovieData = MovieData ( ) ;
currMovieData . guid . newGuid ( ) ;
currMovieData . palFlag = FCEUI_GetCurrentVidSystem ( 0 , 0 ) ! = 0 ;
currMovieData . romChecksum = GameInfo - > MD5 ;
currMovieData . romFilename = FileBase ;
//reset the rom
poweron ( false ) ;
//todo - think about this
2008-06-09 08:15:20 +00:00
//ResetInputTypes();
2008-06-21 07:24:01 +00:00
//todo - maybe this instead
//FCEUD_SetInput(currMovieData.fourscore,(ESI)currMovieData.ports[0],(ESI)currMovieData.ports[1],(ESIFC)currMovieData.ports[2]);
2008-06-05 06:49:11 +00:00
//pause the emulator
FCEUI_SetEmulationPaused ( 1 ) ;
//and enter tasedit mode
movieMode = MOVIEMODE_TASEDIT ;
2008-06-27 07:16:08 +00:00
currMovieData . TryDumpIncremental ( ) ;
2008-06-05 06:49:11 +00:00
FCEU_DispMessage ( " Tasedit engaged " ) ;
}
void FCEUMOV_ExitTasEdit ( )
{
movieMode = MOVIEMODE_INACTIVE ;
FCEU_DispMessage ( " Tasedit disengaged " ) ;
currMovieData = MovieData ( ) ;
}
2008-06-03 05:01:07 +00:00
bool MovieData : : loadSavestateFrom ( std : : vector < char > * buf )
2008-05-28 07:41:49 +00:00
{
2008-06-17 06:55:07 +00:00
memorystream ms ( buf ) ;
return FCEUSS_LoadFP ( & ms , SSLOADPARAM_BACKUP ) ;
2008-05-28 07:41:49 +00:00
}
2008-06-03 05:01:07 +00:00
void MovieData : : dumpSavestateTo ( std : : vector < char > * buf , int compressionLevel )
2008-05-28 07:41:49 +00:00
{
2008-06-03 05:01:07 +00:00
memorystream ms ( buf ) ;
FCEUSS_SaveMS ( & ms , compressionLevel ) ;
2008-06-03 05:50:58 +00:00
ms . trim ( ) ;
2008-05-28 07:41:49 +00:00
}
2006-07-29 05:46:15 +00:00
2008-05-23 09:58:38 +00:00
//begin playing an existing movie
2009-05-25 18:17:19 +00:00
bool FCEUI_LoadMovie ( const char * fname , bool _read_only , bool tasedit , int _pauseframe )
2006-07-29 05:46:15 +00:00
{
2008-06-21 07:24:01 +00:00
if ( ! tasedit & & ! FCEU_IsValidUI ( FCEUI_PLAYMOVIE ) )
2009-05-25 18:17:19 +00:00
return true ; //adelikat: file did not fail to load, so let's return true here, just do nothing
2008-06-05 06:49:11 +00:00
2008-05-23 09:58:38 +00:00
assert ( fname ) ;
2008-05-22 07:43:48 +00:00
2008-06-11 04:54:33 +00:00
//mbg 6/10/08 - we used to call StopMovie here, but that cleared curMovieFilename and gave us crashes...
if ( movieMode = = MOVIEMODE_PLAY )
StopPlayback ( ) ;
else if ( movieMode = = MOVIEMODE_RECORD )
StopRecording ( ) ;
//--------------
2008-05-22 07:43:48 +00:00
2008-05-23 09:58:38 +00:00
currMovieData = MovieData ( ) ;
2008-05-26 00:03:20 +00:00
strcpy ( curMovieFilename , fname ) ;
2008-08-10 02:33:14 +00:00
FCEUFILE * fp = FCEU_fopen ( fname , 0 , " rb " , 0 ) ;
2009-05-25 18:17:19 +00:00
if ( ! fp ) return false ;
2008-08-10 02:33:14 +00:00
if ( fp - > isArchive ( ) & & ! _read_only ) {
FCEU_PrintError ( " Cannot open a movie in read+write from an archive. " ) ;
2009-05-25 18:17:19 +00:00
return true ; //adelikat: file did not fail to load, so return true (false is only for file not exist/unable to open errors
2008-08-10 02:33:14 +00:00
}
2009-05-25 15:42:24 +00:00
# ifdef WIN32
//Add to the recent movie menu
AddRecentMovieFile ( fname ) ;
# endif
2008-08-10 02:33:14 +00:00
LoadFM2 ( currMovieData , fp - > stream , INT_MAX , false ) ;
2009-04-23 21:34:30 +00:00
LoadSubtitles ( currMovieData ) ;
2008-06-09 08:15:20 +00:00
delete fp ;
2008-05-22 07:43:48 +00:00
2008-12-23 01:07:55 +00:00
freshMovie = true ; //Movie has been loaded, so it must be unaltered
2008-12-24 21:26:09 +00:00
if ( bindSavestate ) AutoSS = false ; //If bind savestate to movie is true, then their isn't a valid auto-save to load, so flag it
2008-06-22 06:53:49 +00:00
//fully reload the game to reinitialize everything before playing any movie
poweron ( true ) ;
2008-05-22 07:43:48 +00:00
2008-05-23 09:58:38 +00:00
//WE NEED TO LOAD A SAVESTATE
2008-06-21 07:24:01 +00:00
if ( currMovieData . savestate . size ( ) ! = 0 )
2008-05-22 07:43:48 +00:00
{
2008-05-28 07:41:49 +00:00
bool success = MovieData : : loadSavestateFrom ( & currMovieData . savestate ) ;
2009-05-25 18:17:19 +00:00
if ( ! success ) return true ; //adelikat: I guess return true here? False is only for a bad movie filename, if it got this far the file was god?
2008-05-22 07:43:48 +00:00
}
2008-05-26 07:34:40 +00:00
//if there is no savestate, we won't have this crucial piece of information at the start of the movie.
//so, we have to include it with the movie
2008-05-23 09:58:38 +00:00
if ( currMovieData . palFlag )
2008-05-22 07:43:48 +00:00
FCEUI_SetVidSystem ( 1 ) ;
else
FCEUI_SetVidSystem ( 0 ) ;
2008-06-09 08:15:20 +00:00
//force the input configuration stored in the movie to apply
FCEUD_SetInput ( currMovieData . fourscore , ( ESI ) currMovieData . ports [ 0 ] , ( ESI ) currMovieData . ports [ 1 ] , ( ESIFC ) currMovieData . ports [ 2 ] ) ;
2008-05-22 07:43:48 +00:00
2008-05-26 00:03:20 +00:00
//stuff that should only happen when we're ready to positively commit to the replay
2008-06-21 07:24:01 +00:00
if ( tasedit )
{
currFrameCounter = 0 ;
pauseframe = _pauseframe ;
2008-05-28 07:41:49 +00:00
2008-06-21 07:24:01 +00:00
currMovieData . TryDumpIncremental ( ) ;
}
2008-05-26 00:03:20 +00:00
else
2008-06-21 07:24:01 +00:00
{
currFrameCounter = 0 ;
pauseframe = _pauseframe ;
movie_readonly = _read_only ;
movieMode = MOVIEMODE_PLAY ;
2008-06-27 04:39:14 +00:00
currRerecordCount = currMovieData . rerecordCount ;
2008-06-21 07:24:01 +00:00
if ( movie_readonly )
FCEU_DispMessage ( " Replay started Read-Only. " ) ;
else
FCEU_DispMessage ( " Replay started Read+Write. " ) ;
}
2008-10-25 12:36:03 +00:00
# ifdef CREATE_AVI
if ( LoggingEnabled )
{
FCEU_DispMessage ( " Video recording enabled. \n " ) ;
LoggingEnabled = 2 ;
}
# endif
2009-05-25 18:17:19 +00:00
return true ;
2008-05-23 09:58:38 +00:00
}
2008-05-22 07:43:48 +00:00
2008-05-26 00:03:20 +00:00
static void openRecordingMovie ( const char * fname )
{
2008-06-06 08:23:12 +00:00
osRecordingMovie = FCEUD_UTF8_fstream ( fname , " wb " ) ;
if ( ! osRecordingMovie )
2008-05-26 00:03:20 +00:00
FCEU_PrintError ( " Error opening movie output file: %s " , fname ) ;
strcpy ( curMovieFilename , fname ) ;
}
2008-05-22 07:43:48 +00:00
2008-06-05 06:49:11 +00:00
2008-05-23 09:58:38 +00:00
//begin recording a new movie
2008-06-21 07:24:01 +00:00
//TODO - BUG - the record-from-another-savestate doesnt work.
2008-08-10 08:59:23 +00:00
void FCEUI_SaveMovie ( const char * fname , EMOVIE_FLAG flags , std : : wstring author )
2006-07-29 05:46:15 +00:00
{
2008-06-05 06:49:11 +00:00
if ( ! FCEU_IsValidUI ( FCEUI_RECORDMOVIE ) )
return ;
2008-05-23 09:58:38 +00:00
assert ( fname ) ;
2008-05-22 07:43:48 +00:00
FCEUI_StopMovie ( ) ;
2008-05-26 00:03:20 +00:00
openRecordingMovie ( fname ) ;
2008-05-22 07:43:48 +00:00
2008-05-26 02:20:56 +00:00
currFrameCounter = 0 ;
2008-08-07 04:56:40 +00:00
LagCounterReset ( ) ;
2008-05-26 02:20:56 +00:00
2008-05-23 09:58:38 +00:00
currMovieData = MovieData ( ) ;
2008-05-26 00:03:20 +00:00
currMovieData . guid . newGuid ( ) ;
2008-05-22 07:43:48 +00:00
2008-08-10 08:59:23 +00:00
if ( author ! = L " " ) currMovieData . comments . push_back ( L " author " + author ) ;
2008-05-26 00:03:20 +00:00
currMovieData . palFlag = FCEUI_GetCurrentVidSystem ( 0 , 0 ) ! = 0 ;
2008-05-23 09:58:38 +00:00
currMovieData . romChecksum = GameInfo - > MD5 ;
currMovieData . romFilename = FileBase ;
2008-06-09 08:15:20 +00:00
currMovieData . fourscore = FCEUI_GetInputFourscore ( ) ;
currMovieData . ports [ 0 ] = joyports [ 0 ] . type ;
currMovieData . ports [ 1 ] = joyports [ 1 ] . type ;
currMovieData . ports [ 2 ] = portFC . type ;
2008-05-22 07:43:48 +00:00
2008-06-21 07:24:01 +00:00
if ( flags & MOVIE_FLAG_FROM_POWERON )
2008-05-22 07:43:48 +00:00
{
2008-06-05 06:49:11 +00:00
poweron ( true ) ;
2008-05-22 07:43:48 +00:00
}
else
{
2008-06-03 05:01:07 +00:00
MovieData : : dumpSavestateTo ( & currMovieData . savestate , Z_BEST_COMPRESSION ) ;
2008-05-22 07:43:48 +00:00
}
2008-05-23 09:58:38 +00:00
//we are going to go ahead and dump the header. from now on we will only be appending frames
2008-06-20 22:15:30 +00:00
currMovieData . dump ( osRecordingMovie , false ) ;
2008-05-22 07:43:48 +00:00
2008-05-26 00:03:20 +00:00
movieMode = MOVIEMODE_RECORD ;
2008-05-26 03:13:16 +00:00
movie_readonly = false ;
2008-06-27 04:39:14 +00:00
currRerecordCount = 0 ;
2008-05-26 00:03:20 +00:00
2008-05-23 09:58:38 +00:00
FCEU_DispMessage ( " Movie recording started. " ) ;
2006-07-29 05:46:15 +00:00
}
2008-11-02 18:21:44 +00:00
static int _currCommand = 0 ;
2008-05-22 07:43:48 +00:00
//the main interaction point between the emulator and the movie system.
//either dumps the current joystick state or loads one state from the movie
2008-06-08 10:22:33 +00:00
void FCEUMOV_AddInputState ( )
2006-07-29 05:46:15 +00:00
{
2008-06-05 06:49:11 +00:00
//todo - for tasedit, either dump or load depending on whether input recording is enabled
//or something like that
//(input recording is just like standard read+write movie recording with input taken from gamepad)
//otherwise, it will come from the tasedit data.
2008-06-28 22:03:59 +00:00
if ( movieMode = = MOVIEMODE_TASEDIT )
{
MovieRecord * mr = & currMovieData . records [ currFrameCounter ] ;
if ( movie_readonly )
{
//reset if necessary
if ( mr - > command_reset ( ) )
ResetNES ( ) ;
joyports [ 0 ] . load ( mr ) ;
joyports [ 1 ] . load ( mr ) ;
}
else
{
joyports [ 0 ] . log ( mr ) ;
joyports [ 1 ] . log ( mr ) ;
mr - > commands = 0 ;
}
}
else if ( movieMode = = MOVIEMODE_PLAY )
2008-05-22 07:43:48 +00:00
{
2008-05-23 09:58:38 +00:00
//stop when we run out of frames
if ( currFrameCounter = = currMovieData . records . size ( ) )
2008-05-22 07:43:48 +00:00
{
2008-05-26 22:45:17 +00:00
StopPlayback ( ) ;
2008-05-23 09:58:38 +00:00
}
else
{
2008-06-08 10:22:33 +00:00
MovieRecord * mr = & currMovieData . records [ currFrameCounter ] ;
2008-06-12 05:07:30 +00:00
2008-11-02 18:21:44 +00:00
//reset and power cycle if necessary
if ( mr - > command_power ( ) )
PowerNES ( ) ;
2008-06-12 05:07:30 +00:00
if ( mr - > command_reset ( ) )
ResetNES ( ) ;
2008-06-08 10:22:33 +00:00
joyports [ 0 ] . load ( mr ) ;
joyports [ 1 ] . load ( mr ) ;
2008-05-23 09:58:38 +00:00
}
2008-05-26 22:45:17 +00:00
//if we are on the last frame, then pause the emulator if the player requested it
if ( currFrameCounter = = currMovieData . records . size ( ) - 1 )
{
if ( FCEUD_PauseAfterPlayback ( ) )
{
FCEUI_ToggleEmulationPause ( ) ;
}
}
2008-05-23 09:58:38 +00:00
//pause the movie at a specified frame
if ( FCEUMOV_ShouldPause ( ) & & FCEUI_EmulationPaused ( ) = = 0 )
{
FCEUI_ToggleEmulationPause ( ) ;
FCEU_DispMessage ( " Paused at specified movie frame " ) ;
2008-05-22 07:43:48 +00:00
}
}
2008-05-23 09:58:38 +00:00
else if ( movieMode = = MOVIEMODE_RECORD )
2008-05-22 07:43:48 +00:00
{
2008-05-26 00:03:20 +00:00
MovieRecord mr ;
2008-05-24 22:18:16 +00:00
2008-06-08 10:22:33 +00:00
joyports [ 0 ] . log ( & mr ) ;
joyports [ 1 ] . log ( & mr ) ;
2008-11-02 18:21:44 +00:00
mr . commands = _currCommand ;
_currCommand = 0 ;
2008-06-06 08:23:12 +00:00
2008-06-09 08:15:20 +00:00
mr . dump ( & currMovieData , osRecordingMovie , currMovieData . records . size ( ) ) ;
2008-05-26 00:03:20 +00:00
currMovieData . records . push_back ( mr ) ;
2008-05-22 07:43:48 +00:00
}
2008-05-26 02:20:56 +00:00
currFrameCounter + + ;
2008-06-08 10:22:33 +00:00
extern uint8 joy [ 4 ] ;
memcpy ( & cur_input_display , joy , 4 ) ;
2006-07-29 05:46:15 +00:00
}
2008-06-08 10:22:33 +00:00
2008-05-24 22:18:16 +00:00
//TODO
2006-07-29 05:46:15 +00:00
void FCEUMOV_AddCommand ( int cmd )
{
2008-05-24 22:18:16 +00:00
// do nothing if not recording a movie
if ( movieMode ! = MOVIEMODE_RECORD )
return ;
2006-07-29 05:46:15 +00:00
2008-11-02 18:21:44 +00:00
//NOTE: EMOVIECMD matches FCEUNPCMD_RESET and FCEUNPCMD_POWER
//we are lucky (well, I planned it that way)
_currCommand | = cmd ;
2006-07-29 05:46:15 +00:00
}
void FCEU_DrawMovies ( uint8 * XBuf )
{
2008-12-03 01:33:05 +00:00
if ( frame_display & & movieMode ! = MOVIEMODE_TASEDIT )
2008-11-11 22:58:29 +00:00
{
2008-12-03 01:33:05 +00:00
char counterbuf [ 32 ] = { 0 } ;
if ( movieMode = = MOVIEMODE_PLAY )
sprintf ( counterbuf , " %d/%d " , currFrameCounter , currMovieData . records . size ( ) ) ;
else if ( movieMode = = MOVIEMODE_RECORD )
sprintf ( counterbuf , " %d " , currMovieData . records . size ( ) ) ;
2008-11-14 13:35:01 +00:00
else
sprintf ( counterbuf , " %d (no movie) " , currFrameCounter ) ;
2008-12-03 01:33:05 +00:00
if ( counterbuf [ 0 ] )
DrawTextTrans ( ClipSidesOffset + XBuf + FCEU_TextScanlineOffsetFromBottom ( 30 ) + 1 , 256 , ( uint8 * ) counterbuf , 0x20 + 0x80 ) ;
2008-05-26 01:55:45 +00:00
}
2006-07-29 05:46:15 +00:00
}
2008-07-10 02:17:42 +00:00
void FCEU_DrawLagCounter ( uint8 * XBuf )
{
2008-07-10 03:21:04 +00:00
uint8 color ;
if ( lagFlag ) color = 0x16 + 0x80 ; //If currently lagging display red
2008-12-03 01:33:05 +00:00
else color = 0x2A + 0x80 ; //else display green
2008-07-10 03:21:04 +00:00
2008-07-10 02:17:42 +00:00
if ( lagCounterDisplay )
{
char counterbuf [ 32 ] = { 0 } ;
2008-07-10 04:55:52 +00:00
sprintf ( counterbuf , " %d " , lagCounter ) ;
2008-07-10 02:17:42 +00:00
if ( counterbuf [ 0 ] )
2008-12-03 01:33:05 +00:00
DrawTextTrans ( ClipSidesOffset + XBuf + FCEU_TextScanlineOffsetFromBottom ( 40 ) + 1 , 256 , ( uint8 * ) counterbuf , color ) ; //0x20+0x80
2008-07-10 02:17:42 +00:00
}
}
2008-06-02 14:02:02 +00:00
int FCEUMOV_WriteState ( std : : ostream * os )
{
//we are supposed to dump the movie data into the savestate
if ( movieMode = = MOVIEMODE_RECORD | | movieMode = = MOVIEMODE_PLAY )
2008-06-20 22:15:30 +00:00
return currMovieData . dump ( os , true ) ;
2008-06-02 14:02:02 +00:00
else return 0 ;
}
2008-05-24 22:18:16 +00:00
static bool load_successful ;
2006-07-29 05:46:15 +00:00
2008-06-17 06:55:07 +00:00
bool FCEUMOV_ReadState ( std : : istream * is , uint32 size )
2006-07-29 05:46:15 +00:00
{
2008-05-24 22:18:16 +00:00
load_successful = false ;
2008-08-10 02:33:14 +00:00
//a little rule: cant load states in read+write mode with a movie from an archive.
//so we are going to switch it to readonly mode in that case
if ( ! movie_readonly & & FCEU_isFileInArchive ( curMovieFilename ) ) {
FCEU_PrintError ( " Cannot loadstate in Read+Write with movie from archive. Movie is now Read-Only. " ) ;
movie_readonly = true ;
}
2008-05-26 00:03:20 +00:00
MovieData tempMovieData = MovieData ( ) ;
2008-08-10 04:03:50 +00:00
std : : ios : : pos_type curr = is - > tellg ( ) ;
if ( ! LoadFM2 ( tempMovieData , is , size , false ) ) {
is - > seekg ( ( uint32 ) curr + size ) ;
extern bool FCEU_state_loading_old_format ;
if ( FCEU_state_loading_old_format ) {
if ( movieMode = = MOVIEMODE_PLAY | | movieMode = = MOVIEMODE_RECORD ) {
FCEUI_StopMovie ( ) ;
FCEU_PrintError ( " You have tried to use an old savestate while playing a movie. This is unsupported (since the old savestate has old-format movie data in it which can't be converted on the fly) " ) ;
}
}
2008-08-10 03:19:59 +00:00
return false ;
2008-08-10 04:03:50 +00:00
}
2008-05-24 22:18:16 +00:00
2008-05-24 22:31:58 +00:00
//complex TAS logic for when a savestate is loaded:
2008-05-26 00:03:20 +00:00
//----------------
//if we are playing or recording and toggled read-only:
// then, the movie we are playing must match the guid of the one stored in the savestate or else error.
// the savestate is assumed to be in the same timeline as the current movie.
// if the current movie is not long enough to get to the savestate's frame#, then it is an error.
// the movie contained in the savestate will be discarded.
// the emulator will be put into play mode.
//if we are playing or recording and toggled read+write
// then, the movie we are playing must match the guid of the one stored in the savestate or else error.
// the movie contained in the savestate will be loaded into memory
// the frames in the movie after the savestate frame will be discarded
2008-05-26 05:54:21 +00:00
// the in-memory movie will have its rerecord count incremented
2008-05-26 00:03:20 +00:00
// the in-memory movie will be dumped to disk as fcm.
// the emulator will be put into record mode.
2008-05-24 22:31:58 +00:00
//if we are doing neither:
2008-05-26 00:03:20 +00:00
// then, we must discard this movie and just load the savestate
2008-05-24 22:31:58 +00:00
2008-05-24 22:18:16 +00:00
2008-05-26 00:03:20 +00:00
if ( movieMode = = MOVIEMODE_PLAY | | movieMode = = MOVIEMODE_RECORD )
{
//handle moviefile mismatch
if ( tempMovieData . guid ! = currMovieData . guid )
{
2008-08-18 20:34:59 +00:00
//mbg 8/18/08 - this code can be used to turn the error message into an OK/CANCEL
2008-08-19 04:48:45 +00:00
# ifdef WIN32
std : : string msg = " There is a mismatch between savestate's movie and current movie. \n current: " + currMovieData . guid . toString ( ) + " \n savestate: " + tempMovieData . guid . toString ( ) + " \n \n This means that you have loaded a savestate belonging to a different movie than the one you are playing now. \n \n Continue loading this savestate anyway? " ;
extern HWND pwindow ;
int result = MessageBox ( pwindow , msg . c_str ( ) , " Error loading savestate " , MB_OKCANCEL ) ;
if ( result = = IDCANCEL )
return false ;
# else
2008-08-18 20:34:59 +00:00
FCEU_PrintError ( " Mismatch between savestate's movie and current movie. \n current: %s \n savestate: %s \n " , currMovieData . guid . toString ( ) . c_str ( ) , tempMovieData . guid . toString ( ) . c_str ( ) ) ;
return false ;
2008-08-19 04:48:45 +00:00
# endif
2008-05-26 00:03:20 +00:00
}
2008-05-23 09:58:38 +00:00
2008-05-26 00:03:20 +00:00
closeRecordingMovie ( ) ;
if ( movie_readonly )
{
//if the frame counter is longer than our current movie, then error
2008-05-26 23:29:00 +00:00
if ( currFrameCounter > ( int ) currMovieData . records . size ( ) )
2008-05-26 00:03:20 +00:00
{
FCEU_PrintError ( " Savestate is from a frame (%d) after the final frame in the movie (%d). This is not permitted. " , currFrameCounter , currMovieData . records . size ( ) - 1 ) ;
return false ;
}
movieMode = MOVIEMODE_PLAY ;
}
else
{
//truncate before we copy, just to save some time
tempMovieData . truncateAt ( currFrameCounter ) ;
currMovieData = tempMovieData ;
2008-07-23 06:54:14 +00:00
2008-09-24 06:06:31 +00:00
# ifdef _S9XLUA_H
2008-07-23 06:54:14 +00:00
if ( ! FCEU_LuaRerecordCountSkip ( ) )
currRerecordCount + + ;
2008-09-24 06:06:31 +00:00
# endif
2008-07-23 06:54:14 +00:00
currMovieData . rerecordCount = currRerecordCount ;
2008-05-23 09:58:38 +00:00
2008-05-26 00:03:20 +00:00
openRecordingMovie ( curMovieFilename ) ;
2008-06-20 22:15:30 +00:00
currMovieData . dump ( osRecordingMovie , false ) ;
2008-05-26 00:03:20 +00:00
movieMode = MOVIEMODE_RECORD ;
}
}
load_successful = true ;
2008-05-23 09:58:38 +00:00
//// Maximus: Show the last input combination entered from the
//// movie within the state
//if(current!=0) // <- mz: only if playing or recording a movie
// memcpy(&cur_input_display, joop, 4);
2008-05-22 07:43:48 +00:00
2008-05-26 00:03:20 +00:00
return true ;
2006-07-29 05:46:15 +00:00
}
void FCEUMOV_PreLoad ( void )
{
load_successful = 0 ;
}
2008-06-05 06:49:11 +00:00
bool FCEUMOV_PostLoad ( void )
2006-07-29 05:46:15 +00:00
{
2008-06-05 06:49:11 +00:00
if ( movieMode = = MOVIEMODE_INACTIVE | | movieMode = = MOVIEMODE_TASEDIT )
return true ;
2006-07-29 05:46:15 +00:00
else
2008-06-05 06:49:11 +00:00
return load_successful ;
}
2006-07-29 05:46:15 +00:00
void FCEUI_MovieToggleFrameDisplay ( void )
{
2008-05-26 01:55:45 +00:00
frame_display = ! frame_display ;
2006-07-29 05:46:15 +00:00
}
void FCEUI_ToggleInputDisplay ( void )
{
2008-05-26 02:05:01 +00:00
switch ( input_display )
{
case 0 :
input_display = 1 ;
break ;
case 1 :
input_display = 2 ;
break ;
case 2 :
input_display = 4 ;
break ;
default :
input_display = 0 ;
break ;
}
2006-07-29 05:46:15 +00:00
}
2009-03-15 21:12:15 +00:00
int FCEUI_GetMovieLength ( )
{
return currMovieData . records . size ( ) ;
}
int FCEUI_GetMovieRerecordCount ( )
{
return currMovieData . rerecordCount ;
}
2008-05-26 00:03:20 +00:00
bool FCEUI_GetMovieToggleReadOnly ( )
2006-07-29 05:46:15 +00:00
{
2008-05-26 00:03:20 +00:00
return movie_readonly ;
}
2009-03-15 23:21:52 +00:00
void FCEUI_SetMovieToggleReadOnly ( bool which )
{
if ( which ) //If set to readonly
{
if ( ! movie_readonly ) //If not already set
{
movie_readonly = true ;
FCEU_DispMessage ( " Movie is now Read-Only. " ) ;
}
else //Else restate message
FCEU_DispMessage ( " Movie is Read-Only. " ) ;
}
else //If set to read+write
{
if ( movie_readonly ) //If not already set
{
movie_readonly = false ;
FCEU_DispMessage ( " Movie is now Read+Write. " ) ;
}
else //Else restate message
FCEU_DispMessage ( " Movie is Read+Write. " ) ;
}
}
2008-05-26 00:03:20 +00:00
void FCEUI_MovieToggleReadOnly ( )
{
if ( movie_readonly )
FCEU_DispMessage ( " Movie is now Read+Write. " ) ;
2006-07-29 05:46:15 +00:00
else
2008-05-26 00:03:20 +00:00
FCEU_DispMessage ( " Movie is now Read-Only. " ) ;
movie_readonly = ! movie_readonly ;
2006-07-29 05:46:15 +00:00
}
2008-06-04 00:53:09 +00:00
void FCEUI_MoviePlayFromBeginning ( void )
{
if ( movieMode ! = MOVIEMODE_INACTIVE )
{
char * fname = strdup ( curMovieFilename ) ;
2008-06-21 07:24:01 +00:00
FCEUI_LoadMovie ( fname , true , false , 0 ) ;
2008-06-04 00:53:09 +00:00
FCEU_DispMessage ( " Movie is now Read-Only. Playing from beginning. " ) ;
free ( fname ) ;
}
}
2009-03-15 23:46:57 +00:00
string FCEUI_GetMovieName ( void )
{
return curMovieFilename ;
}
2008-06-04 00:53:09 +00:00
2008-11-11 00:02:22 +00:00
bool FCEUI_MovieGetInfo ( FCEUFILE * fp , MOVIE_INFO & info , bool skipFrameCount )
2006-07-29 05:46:15 +00:00
{
2008-05-23 09:58:38 +00:00
MovieData md ;
2008-08-10 02:33:14 +00:00
if ( ! LoadFM2 ( md , fp - > stream , INT_MAX , skipFrameCount ) )
return false ;
2008-06-09 08:15:20 +00:00
2008-11-11 00:02:22 +00:00
info . movie_version = md . version ;
info . poweron = md . savestate . size ( ) = = 0 ;
info . pal = md . palFlag ;
info . nosynchack = true ;
info . num_frames = md . records . size ( ) ;
info . md5_of_rom_used = md . romChecksum ;
info . emu_version_used = md . emuVersion ;
info . name_of_rom_used = md . romFilename ;
info . rerecord_count = md . rerecordCount ;
info . comments = md . comments ;
2008-11-23 21:28:22 +00:00
info . subtitles = md . subtitles ;
2008-05-22 07:43:48 +00:00
2008-06-09 08:15:20 +00:00
return true ;
2008-05-23 09:58:38 +00:00
}
2008-11-23 21:28:22 +00:00
//This function creates an array of frame numbers and corresponding strings for displaying subtitles
2009-04-23 21:34:30 +00:00
void LoadSubtitles ( MovieData moviedata )
2008-11-23 21:28:22 +00:00
{
extern std : : vector < string > subtitles ;
2009-04-23 21:34:30 +00:00
for ( uint32 i = 0 ; i < moviedata . subtitles . size ( ) ; i + + )
2008-11-23 21:28:22 +00:00
{
2009-04-23 21:34:30 +00:00
std : : string & subtitle = moviedata . subtitles [ i ] ;
2008-11-23 21:28:22 +00:00
size_t splitat = subtitle . find_first_of ( ' ' ) ;
std : : string key , value ;
//If we can't split them, then don't process this one
if ( splitat = = std : : string : : npos )
{
}
//Else split the subtitle into the int and string arrays
else
{
key = subtitle . substr ( 0 , splitat ) ;
value = subtitle . substr ( splitat + 1 ) ;
subtitleFrames . push_back ( atoi ( key . c_str ( ) ) ) ;
subtitleMessages . push_back ( value ) ;
}
}
}
//Every frame, this will be called to determine if a subtitle should be displayed, which one, and then to display it
void ProcessSubtitles ( void )
{
if ( movieMode = = MOVIEMODE_INACTIVE ) return ;
for ( uint32 i = 0 ; i < currMovieData . subtitles . size ( ) ; i + + )
{
if ( currFrameCounter = = subtitleFrames [ i ] )
2008-11-23 23:49:15 +00:00
FCEU_DisplaySubtitles ( " %s " , subtitleMessages [ i ] . c_str ( ) ) ;
2008-11-23 21:28:22 +00:00
}
2008-11-23 23:49:15 +00:00
}
void FCEU_DisplaySubtitles ( char * format , . . . )
{
va_list ap ;
va_start ( ap , format ) ;
vsnprintf ( subtitleMessage . errmsg , sizeof ( subtitleMessage . errmsg ) , format , ap ) ;
va_end ( ap ) ;
subtitleMessage . howlong = 300 ;
2008-11-24 00:45:25 +00:00
subtitleMessage . isMovieMessage = subtitlesOnAVI ;
2008-12-01 00:37:40 +00:00
}
2008-12-23 01:07:55 +00:00
2009-05-25 20:30:32 +00:00
void FCEUI_CreateMovieFile ( std : : string fn )
{
MovieData md = currMovieData ; //Get current movie data
std : : fstream * outf = FCEUD_UTF8_fstream ( fn , " wb " ) ; //open/create file
md . dump ( outf , false ) ; //dump movie data
delete outf ; //clean up, delete file object
}
2008-12-23 01:07:55 +00:00
void FCEUI_MakeBackupMovie ( bool dispMessage )
{
//This function generates backup movie files
string currentFn ; //Current movie fillename
string backupFn ; //Target backup filename
string tempFn ; //temp used in back filename creation
stringstream stream ;
int x ; //Temp variable for string manip
bool exist = false ; //Used to test if filename exists
bool overflow = false ; //Used for special situation when backup numbering exceeds limit
currentFn = curMovieFilename ; //Get current moviefilename
backupFn = curMovieFilename ; //Make backup filename the same as current moviefilename
x = backupFn . find_last_of ( " . " ) ; //Find file extension
backupFn = backupFn . substr ( 0 , x ) ; //Remove extension
tempFn = backupFn ; //Store the filename at this point
for ( unsigned int backNum = 0 ; backNum < 999 ; backNum + + ) //999 = arbituary limit to backup files
{
stream . str ( " " ) ; //Clear stream
if ( backNum > 99 )
stream < < " - " < < backNum ; //assign backNum to stream
else if ( backNum < = 99 & & backNum > = 10 )
stream < < " -0 " ; //Make it 010, etc if two digits
else
stream < < " -00 " < < backNum ; //Make it 001, etc if single digit
backupFn . append ( stream . str ( ) ) ; //add number to bak filename
backupFn . append ( " .bak " ) ; //add extension
exist = CheckFileExists ( backupFn . c_str ( ) ) ; //Check if file exists
if ( ! exist )
break ; //Yeah yeah, I should use a do loop or something
else
{
backupFn = tempFn ; //Before we loop again, reset the filename
if ( backNum = = 999 ) //If 999 exists, we have overflowed, let's handle that
{
backupFn . append ( " -001.bak " ) ; //We are going to simply overwrite 001.bak
overflow = true ; //Flag that we have exceeded limit
break ; //Just in case
}
}
}
2009-05-25 20:30:32 +00:00
FCEUI_CreateMovieFile ( backupFn ) ;
2008-12-23 01:07:55 +00:00
//TODO, decide if fstream successfully opened the file and print error message if it doesn't
if ( dispMessage ) //If we should inform the user
{
if ( overflow )
2008-12-24 19:14:04 +00:00
FCEUI_DispMessage ( " Backup overflow, overwriting %s " , backupFn . c_str ( ) ) ; //Inform user of overflow
2008-12-23 01:07:55 +00:00
else
FCEUI_DispMessage ( " %s created " , backupFn . c_str ( ) ) ; //Inform user of backup filename
}
}
bool CheckFileExists ( const char * filename )
{
//This function simply checks to see if the given filename exists
2008-12-23 03:54:31 +00:00
string checkFilename ;
2008-12-23 20:29:27 +00:00
2008-12-23 03:54:31 +00:00
if ( filename )
checkFilename = filename ;
2008-12-23 21:28:06 +00:00
2008-12-23 01:07:55 +00:00
//Check if this filename exists
fstream test ;
test . open ( checkFilename . c_str ( ) , fstream : : in ) ;
if ( test . fail ( ) )
{
test . close ( ) ;
2008-12-23 20:29:27 +00:00
return false ;
2008-12-23 01:07:55 +00:00
}
else
{
test . close ( ) ;
2008-12-23 20:29:27 +00:00
return true ;
2008-12-23 01:07:55 +00:00
}
}