openMSX
SaveStateCLI.cc
Go to the documentation of this file.
1 #include "SaveStateCLI.hh"
2 #include "CommandLineParser.hh"
3 #include "TclObject.hh"
4 
5 namespace openmsx {
6 
8  : parser(parser_)
9 {
10  parser.registerOption("-savestate", *this);
11  parser.registerFileType({"oms"}, *this);
12 }
13 
14 void SaveStateCLI::parseOption(const std::string& option, std::span<std::string>& cmdLine)
15 {
16  parseFileType(getArgument(option, cmdLine), cmdLine);
17 }
18 
19 std::string_view SaveStateCLI::optionHelp() const
20 {
21  return "Load savestate and start emulation from there";
22 }
23 
24 void SaveStateCLI::parseFileType(const std::string& filename,
25  std::span<std::string>& /*cmdLine*/)
26 {
27  // TODO: this is basically a C++ version of a part of savestate.tcl.
28  // Can that be improved?
29  auto& interp = parser.getInterpreter();
30 
31  TclObject command1 = makeTclList("restore_machine", filename);
32  auto newId = command1.executeCommand(interp);
33 
34  TclObject command2 = makeTclList("machine");
35  auto currentId = command2.executeCommand(interp);
36 
37  if (!currentId.empty()) {
38  TclObject command3 = makeTclList("delete_machine", currentId);
39  command3.executeCommand(interp);
40  }
41 
42  TclObject command4 = makeTclList("activate_machine", newId);
43  command4.executeCommand(interp);
44 }
45 
46 std::string_view SaveStateCLI::fileTypeHelp() const
47 {
48  return "openMSX savestate";
49 }
50 
51 std::string_view SaveStateCLI::fileTypeCategoryName() const
52 {
53  return "savestate";
54 }
55 
56 } // namespace openmsx
static std::string getArgument(const std::string &option, std::span< std::string > &cmdLine)
Definition: CLIOption.cc:9
void registerOption(const char *str, CLIOption &cliOption, ParsePhase phase=PHASE_LAST, unsigned length=2)
void registerFileType(std::initializer_list< std::string_view > extensions, CLIFileType &cliFileType)
Interpreter & getInterpreter() const
void parseFileType(const std::string &filename, std::span< std::string > &cmdLine) override
Definition: SaveStateCLI.cc:24
SaveStateCLI(CommandLineParser &parser)
Definition: SaveStateCLI.cc:7
std::string_view fileTypeCategoryName() const override
Definition: SaveStateCLI.cc:51
void parseOption(const std::string &option, std::span< std::string > &cmdLine) override
Definition: SaveStateCLI.cc:14
std::string_view optionHelp() const override
Definition: SaveStateCLI.cc:19
std::string_view fileTypeHelp() const override
Definition: SaveStateCLI.cc:46
TclObject executeCommand(Interpreter &interp, bool compile=false)
Interpret this TclObject as a command and execute it.
Definition: TclObject.cc:181
This file implemented 3 utility functions:
Definition: Autofire.cc:9
constexpr const char *const filename
TclObject makeTclList(Args &&... args)
Definition: TclObject.hh:281