2 #include "mainloop.hpp"
5 #include "moviedata.hpp"
9 #include "keymapper.hpp"
13 #include <snes/snes.hpp>
14 #include <ui-libsnes/libsnes.hpp>
15 #include "framerate.hpp"
16 #if defined(_WIN32) || defined(_WIN64) || defined(TEST_WIN32_CODE) || defined(__APPLE__)
21 class my_interfaced
: public SNES::Interface
23 string
path(SNES::Cartridge::Slot slot
, const string
&hint
)
29 struct moviefile
generate_movie_template(std::vector
<std::string
> cmdline
, loaded_rom
& r
)
31 struct moviefile movie
;
32 movie
.coreversion
= bsnes_core_version
;
33 movie
.projectid
= get_random_hexstring(40);
34 movie
.gametype
= gtype::togametype(r
.rtype
, r
.region
);
35 movie
.rom_sha256
= r
.rom
.sha256
;
36 movie
.romxml_sha256
= r
.rom_xml
.sha256
;
37 movie
.slota_sha256
= r
.slota
.sha256
;
38 movie
.slotaxml_sha256
= r
.slota_xml
.sha256
;
39 movie
.slotb_sha256
= r
.slotb
.sha256
;
40 movie
.slotbxml_sha256
= r
.slotb_xml
.sha256
;
41 movie
.movie_sram
= load_sram_commandline(cmdline
);
42 for(auto i
= cmdline
.begin(); i
!= cmdline
.end(); i
++) {
44 if(o
.length() >= 8 && o
.substr(0, 8) == "--port1=")
45 movie
.port1
= port_type::lookup(o
.substr(8), false).ptype
;
46 if(o
.length() >= 8 && o
.substr(0, 8) == "--port2=")
47 movie
.port2
= port_type::lookup(o
.substr(8), true).ptype
;
48 if(o
.length() >= 11 && o
.substr(0, 11) == "--gamename=")
49 movie
.gamename
= o
.substr(11);
50 if(o
.length() >= 9 && o
.substr(0, 9) == "--author=") {
51 std::string line
= o
.substr(9);
52 auto g
= split_author(line
);
53 movie
.authors
.push_back(g
);
55 if(o
.length() >= 13 && o
.substr(0, 13) == "--rtc-second=") {
56 movie
.rtc_second
= movie
.movie_rtc_second
= parse_value
<int64_t>(o
.substr(13));
58 if(o
.length() >= 16 && o
.substr(0, 16) == "--rtc-subsecond=") {
59 movie
.rtc_subsecond
= movie
.movie_rtc_subsecond
= parse_value
<int64_t>(o
.substr(16));
60 if(movie
.rtc_subsecond
< 0 || movie
.rtc_subsecond
> 3462619485019ULL)
61 throw std::runtime_error("Bad RTC subsecond value (range is 0-3462619485019)");
72 void run_extra_scripts(const std::vector
<std::string
>& cmdline
)
74 for(auto i
= cmdline
.begin(); i
!= cmdline
.end(); i
++) {
76 if(o
.length() >= 6 && o
.substr(0, 6) == "--run=") {
77 std::string file
= o
.substr(6);
78 messages
<< "--- Running " << file
<< " --- " << std::endl
;
79 command::invokeC("run-script " + file
);
80 messages
<< "--- End running " << file
<< " --- " << std::endl
;
86 int main(int argc
, char** argv
)
88 std::vector
<std::string
> cmdline
;
89 for(int i
= 1; i
< argc
; i
++)
90 cmdline
.push_back(argv
[i
]);
92 SNES::system
.interface
= &intrf
;
98 x
<< snes_library_id() << " (" << SNES::Info::Profile
<< " core)";
99 bsnes_core_version
= x
.str();
104 messages
<< "BSNES version: " << bsnes_core_version
<< std::endl
;
105 messages
<< "lsnes version: lsnes rr" << lsnes_version
<< std::endl
;
106 messages
<< "Command line is: ";
107 for(auto k
= cmdline
.begin(); k
!= cmdline
.end(); k
++)
108 messages
<< "\"" << *k
<< "\" ";
109 messages
<< std::endl
;
111 std::string cfgpath
= get_config_path();
113 messages
<< "Saving per-user data to: " << get_config_path() << std::endl
;
114 messages
<< "--- Running lsnesrc --- " << std::endl
;
115 command::invokeC("run-script " + cfgpath
+ "/lsnes.rc");
116 messages
<< "--- End running lsnesrc --- " << std::endl
;
118 run_extra_scripts(cmdline
);
120 messages
<< "--- Loading ROM ---" << std::endl
;
123 r
= load_rom_from_commandline(cmdline
);
125 } catch(std::bad_alloc
& e
) {
127 } catch(std::exception
& e
) {
128 messages
<< "FATAL: Can't load ROM: " << e
.what() << std::endl
;
132 messages
<< "Detected region: " << gtype::tostring(r
.rtype
, r
.region
) << std::endl
;
133 if(r
.region
== REGION_PAL
)
134 set_nominal_framerate(322445.0/6448.0);
135 else if(r
.region
== REGION_NTSC
)
136 set_nominal_framerate(10738636.0/178683.0);
138 messages
<< "--- Internal memory mappings ---" << std::endl
;
140 messages
<< "--- End of Startup --- " << std::endl
;
142 movie
.force_corrupt
= true;
146 for(auto i
= cmdline
.begin(); i
!= cmdline
.end(); i
++)
147 if(i
->length() > 0 && (*i
)[0] != '-') {
150 movie
= moviefile(*i
);
152 } catch(std::bad_alloc
& e
) {
154 } catch(std::exception
& e
) {
155 messages
<< "Error loading '" << *i
<< "': " << e
.what() << std::endl
;
159 movie
= generate_movie_template(cmdline
, r
);
161 } catch(std::bad_alloc
& e
) {
163 } catch(std::exception
& e
) {
164 messages
<< "FATAL: " << e
.what() << std::endl
;