diff --git a/CMakeLists.txt b/CMakeLists.txt index 7273e10..42686fb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,7 +15,6 @@ elseif(MSVC) set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS} /Ox") endif() - # Find dependency packages find_package(vmq REQUIRED CONFIG) find_package(dr_wav REQUIRED CONFIG) @@ -26,6 +25,7 @@ find_package(OpenGL REQUIRED) find_package(SDL2 REQUIRED COMPONENTS SDL2::SDL2-static SDL2::SDL2main CONFIG) find_package(OpenAL REQUIRED CONFIG) + # Determine dependencies set(STATIC_LIBS vmq diff --git a/src/application.cpp b/src/application.cpp index 640d966..8bfbf39 100644 --- a/src/application.cpp +++ b/src/application.cpp @@ -39,6 +39,7 @@ // Debug #include "debug/ansi-codes.hpp" +#include "debug/console-commands.hpp" // Resources #include "resources/resource-manager.hpp" @@ -800,6 +801,14 @@ application::application(int argc, char** argv): // Set overworld as active scene active_scene = &overworld_scene; + + // Setup debug CLI + cli.register_command("echo", cc::echo); + cli.register_command("exit", std::function(std::bind(&cc::exit, this))); + + std::string cmd = "echo abc 123"; + logger.log(cmd + "\n"); + logger.log(cli.interpret(cmd) + "\n"); } application::~application() diff --git a/src/application.hpp b/src/application.hpp index cd502ff..702fd16 100644 --- a/src/application.hpp +++ b/src/application.hpp @@ -33,6 +33,7 @@ // Debug #include "debug/logger.hpp" #include "debug/performance-sampler.hpp" +#include "debug/cli.hpp" // Input #include "input/control.hpp" @@ -145,6 +146,8 @@ public: void close(int status); logger* get_logger(); + cli* get_cli(); + resource_manager* get_resource_manager(); fsm::machine* get_state_machine(); const fsm::state& get_loading_state() const; @@ -184,7 +187,8 @@ private: // Debugging std::ofstream log_filestream; - logger logger; + ::logger logger; + ::cli cli; // Paths std::string data_path; @@ -327,6 +331,11 @@ inline logger* application::get_logger() return &logger; } +inline cli* application::get_cli() +{ + return &cli; +} + inline resource_manager* application::get_resource_manager() { return resource_manager; diff --git a/src/debug/cli.cpp b/src/debug/cli.cpp new file mode 100644 index 0000000..260f059 --- /dev/null +++ b/src/debug/cli.cpp @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2020 Christopher J. Howard + * + * This file is part of Antkeeper source code. + * + * Antkeeper source code is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Antkeeper source code is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Antkeeper source code. If not, see . + */ + +#include "cli.hpp" + +std::string cli::interpret(const std::string& line) const +{ + std::istringstream stream(line); + std::string command_name; + stream >> command_name; + + if (auto it = commands.find(command_name); it != commands.end()) + { + return it->second(line.substr(command_name.length() + 1)); + } + + return std::string(); +} + +void cli::unregister_command(const std::string& name) +{ + if (auto it = commands.find(name); it != commands.end()) + commands.erase(it); +} diff --git a/src/debug/cli.hpp b/src/debug/cli.hpp index 8df45dd..8e712be 100644 --- a/src/debug/cli.hpp +++ b/src/debug/cli.hpp @@ -32,9 +32,6 @@ class cli { public: - /// String-wrapped function object - typedef std::function command_type; - /** * Interprets a command line as a function invocation. * @@ -66,6 +63,9 @@ public: void unregister_command(const std::string& name); private: + /// String-wrapped function object + typedef std::function command_type; + /** * Parses a single argument from a string stream. * @@ -86,20 +86,6 @@ private: std::map commands; }; -std::string cli::interpret(const std::string& line) const -{ - std::istringstream stream(line); - std::string command_name; - stream >> command_name; - - if (auto it = commands.find(command_name); it != commands.end()) - { - return it->second(line.substr(command_name.length() + 1)); - } - - return std::string(); -} - template void cli::register_command(const std::string& name, const std::function& function) { @@ -112,12 +98,6 @@ void cli::register_command(const std::string& name, T (*function)(Args...)) commands[name] = wrap(std::function(function)); } -void cli::unregister_command(const std::string& name) -{ - if (auto it = commands.find(name); it != commands.end()) - commands.erase(it); -} - template T cli::parse(std::istringstream& stream) { @@ -150,4 +130,3 @@ typename cli::command_type cli::wrap(const std::function& function) } #endif // ANTKEEPER_CLI_HPP - diff --git a/src/debug/console-commands.cpp b/src/debug/console-commands.cpp new file mode 100644 index 0000000..94f2081 --- /dev/null +++ b/src/debug/console-commands.cpp @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2020 Christopher J. Howard + * + * This file is part of Antkeeper source code. + * + * Antkeeper source code is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Antkeeper source code is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Antkeeper source code. If not, see . + */ + +#include "console-commands.hpp" +#include "application.hpp" + +namespace cc +{ + +std::string echo(std::string text) +{ + return text; +} + +std::string exit(application* app) +{ + app->close(EXIT_SUCCESS); + return std::string(); +} + +} // namespace cc diff --git a/src/debug/console-commands.hpp b/src/debug/console-commands.hpp new file mode 100644 index 0000000..4c634c7 --- /dev/null +++ b/src/debug/console-commands.hpp @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2020 Christopher J. Howard + * + * This file is part of Antkeeper source code. + * + * Antkeeper source code is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Antkeeper source code is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Antkeeper source code. If not, see . + */ + +#ifndef ANTKEEPER_CONSOLE_COMMANDS_HPP +#define ANTKEEPER_CONSOLE_COMMANDS_HPP + +#include + +class application; + +namespace cc +{ + +std::string echo(std::string text); + +std::string exit(application* app); + +} // namespace cc + +#endif // ANTKEEPER_CONSOLE_COMMANDS_HPP diff --git a/src/debug/logger.cpp b/src/debug/logger.cpp index 07c2892..c8a5207 100644 --- a/src/debug/logger.cpp +++ b/src/debug/logger.cpp @@ -54,11 +54,6 @@ void logger::log(const std::string& text) for (std::size_t i = 0; i < tasks.size(); ++i) (*os) << indent; - - for (const std::string& prefix: prefix_stack) - { - (*os) << prefix; - } (*os) << (log_prefix + text + log_postfix); @@ -121,16 +116,6 @@ void logger::set_success_postfix(const std::string& postfix) success_postfix = postfix; } -void logger::push_prefix(const std::string& prefix) -{ - prefix_stack.push_back(prefix); -} - -void logger::pop_prefix() -{ - prefix_stack.pop_back(); -} - void logger::push_task(const std::string& description) { std::string message = description + "...\n"; diff --git a/src/debug/logger.hpp b/src/debug/logger.hpp index 2d83f75..06527b5 100644 --- a/src/debug/logger.hpp +++ b/src/debug/logger.hpp @@ -54,9 +54,6 @@ public: void set_error_postfix(const std::string& postfix); void set_success_prefix(const std::string& prefix); void set_success_postfix(const std::string& postfix); - - void push_prefix(const std::string& prefix); - void pop_prefix(); /** * Pushes a task onto the stack and outputs it to the log. @@ -95,9 +92,7 @@ private: std::string error_prefix; std::string error_postfix; std::string success_prefix; - std::string success_postfix; - std::list prefix_stack; - + std::string success_postfix; std::stack tasks; std::string indent; bool timestamp_enabled;