OpenRaider  0.1.4-dev
Open Source Tomb Raider Game Engine implementation
Command.cpp
Go to the documentation of this file.
1 
8 #include <fstream>
9 #include <iomanip>
10 
11 #include "global.h"
12 #include "Log.h"
13 #include "utils/strings.h"
14 #include "commands/Command.h"
15 #include "commands/CommandBind.h"
16 #include "commands/CommandEngine.h"
17 #include "commands/CommandSet.h"
18 
19 std::vector<std::shared_ptr<Command>> Command::commands;
20 
22 }
23 
25  Log::get(LOG_USER) << "No help available!" << Log::endl;
26 }
27 
29  commands.clear();
30  commands.push_back(std::shared_ptr<Command>(new CommandBind()));
31  commands.push_back(std::shared_ptr<Command>(new CommandLoad()));
32  commands.push_back(std::shared_ptr<Command>(new CommandSet()));
33  commands.push_back(std::shared_ptr<Command>(new CommandGet()));
34  commands.push_back(std::shared_ptr<Command>(new CommandScreenshot()));
35  commands.push_back(std::shared_ptr<Command>(new CommandQuit()));
36 }
37 
38 int Command::command(std::string c) {
39  // Remove comment, if any
40  size_t comment = c.find_first_of('#');
41  if (comment != std::string::npos)
42  c.erase(comment);
43 
44  // Execute command
45  std::stringstream command(c);
46  std::string cmd;
47  command >> cmd;
48  command >> std::boolalpha >> std::ws;
49 
50  if (cmd.length() == 0)
51  return 0;
52 
53  // Print help
54  if (cmd == "help") {
55  std::string arg;
56  command >> arg;
57  if (arg.length() == 0) {
58  // List all available commands
59  Log::get(LOG_USER) << "Available commands:" << Log::endl;
60  Log::get(LOG_USER) << std::right << std::setw(11);
61  Log::get(LOG_USER) << "help" << " - print command help" << Log::endl;
62  for (auto& x : commands) {
63  if (x) {
64  Log::get(LOG_USER) << std::right << std::setw(11);
65  Log::get(LOG_USER) << x->name() << " - " << x->brief() << Log::endl;
66  }
67  }
68  Log::get(LOG_USER) << "Use help COMMAND to get additional info" << Log::endl;
69  Log::get(LOG_USER) << "Pass BOOLs as true or false" << Log::endl;
70  return 0;
71  } else {
72  // Show help for a specific command
73  for (auto& x : commands) {
74  if (x) {
75  if (x->name() == arg) {
76  x->printHelp();
77  return 0;
78  }
79  }
80  }
81  Log::get(LOG_USER) << "Unknown command: \"" << arg << "\"" << Log::endl;
82  return -1;
83  }
84  }
85 
86  // Execute command
87  for (auto& x : commands) {
88  if (x) {
89  if (x->name() == cmd) {
90  return x->execute(command);
91  }
92  }
93  }
94 
95  Log::get(LOG_USER) << "Unknown command: \"" << cmd << "\"" << Log::endl;
96  return -1;
97 }
98 
99 int Command::executeFile(std::string file) {
100  std::string configFile = expandHomeDirectory(file);
101  Log::get(LOG_INFO) << "Loading config from \"" << configFile << "\"..." << Log::endl;
102 
103  std::ifstream f(configFile);
104  if (!f) {
105  Log::get(LOG_ERROR) << "Could not open file!" << Log::endl;
106  return -1;
107  }
108 
109  for (std::string line; std::getline(f, line);) {
110  if (line.length() == 0)
111  continue;
112 
113  int error = Command::command(line);
114  if (error != 0)
115  Log::get(LOG_ERROR) << "Error Code: " << error << Log::endl;
116  }
117 
118  f.close();
119  return 0;
120 }
121 
122 std::string Command::autoComplete(std::string begin) {
123  std::vector<std::string> candidates;
124 
125  std::string help("help");
126  if (begin.size() <= help.size()) {
127  if (begin.compare(0, begin.size(), help, 0, begin.size()) == 0) {
128  candidates.push_back(help);
129  }
130  }
131 
132  for (auto& x : commands) {
133  if (x) {
134  std::string name = x->name();
135  if (begin.size() <= name.size()) {
136  if (begin.compare(0, begin.size(), name, 0, begin.size()) == 0) {
137  candidates.push_back(name);
138  }
139  }
140  }
141  }
142 
143  if (candidates.size() == 0) {
144  return "";
145  } else if (candidates.size() == 1) {
146  return candidates.at(0);
147  } else {
148  std::string common = candidates.at(0);
149  for (int i = 0; i < candidates.size(); i++) {
150  Log::get(LOG_USER) << candidates.at(i);
151  if (i < (candidates.size() - 1))
152  Log::get(LOG_USER) << "/";
153 
154  for (int c = 0; (c < common.size()) && (c < candidates.at(i).size()); c++) {
155  if (common.at(c) != candidates.at(i).at(c)) {
156  common.erase(c);
157  break;
158  }
159  }
160  }
162  return common;
163  }
164 }
165 
Get/Set Commands.
Engine Commands.
#define LOG_USER
Definition: Log.h:18
virtual ~Command()
Definition: Command.cpp:21
String handling utilities.
static std::string autoComplete(std::string begin)
Definition: Command.cpp:122
static std::vector< std::shared_ptr< Command > > commands
Definition: Command.h:30
static int command(std::string c)
Definition: Command.cpp:38
Commands.
virtual void printHelp()
Definition: Command.cpp:24
Included everywhere.
static LogLevel & get(int level)
Definition: Log.cpp:14
static void fillCommandList()
Definition: Command.cpp:28
Global Logging Utility.
static void error(char *msg)
Definition: commander.c:19
#define LOG_INFO
Definition: Log.h:21
virtual std::string name()=0
std::string expandHomeDirectory(std::string s)
Definition: strings.cpp:24
static const char endl
Definition: Log.h:35
static int executeFile(std::string file)
Definition: Command.cpp:99
#define LOG_ERROR
Definition: Log.h:19
Bind Command.