diff options
Diffstat (limited to 'src/dmtool.cc')
-rw-r--r-- | src/dmtool.cc | 140 |
1 files changed, 90 insertions, 50 deletions
diff --git a/src/dmtool.cc b/src/dmtool.cc index f9d32ab..4aee1cf 100644 --- a/src/dmtool.cc +++ b/src/dmtool.cc @@ -1,78 +1,112 @@ -#include "creature.h" +#include "entry.h" #include "settings.h" #include <iostream> #include <vector> #include <string> #include <filesystem> +#include <system_error> using namespace std; namespace fs = std::filesystem; void usage(string exename) { - cout << "Usage:\n"; + cout << "Usage:" << endl; string indOpt = " " + exename + " "; string indDesc = " "; - cout << indOpt << "[ls] [subfolder]\n"; - cout << indDesc << "List creatures and objects.\n"; - cout << indOpt << "cp old-path new-path\n"; - cout << indDesc << "Copy old-path to new-path.\n"; - cout << indOpt << "mv old-path new-path\n"; - cout << indDesc << "Move old-path to new-path.\n"; - cout << indOpt << "rm path\n"; - cout << indDesc << "Remove existing creature, object, or directory.\n"; - cout << indOpt << "roll path name\n"; - cout << indDesc << "Roll a skill check, save, or attack.\n"; - cout << indOpt << "damage path amount [type]\n"; - cout << indDesc << "Damage creature by amount. Type defaults to \"force\".\n"; - cout << indOpt << "heal path amount\n"; - cout << indDesc << "Heal creature by amount.\n"; - cout << indOpt << "reset path\n"; - cout << indDesc << "Reset creature to full health (as if completing a long rest).\n"; - cout << indOpt << "set path field value\n"; - cout << indDesc << "Set a field to a new value, where field is any of:\n"; - cout << indDesc << " ability (str, dex, con, int, wis, cha); value is new ability score\n"; - cout << indDesc << " skill (athletics, \"sleight of hand\", etc.); value is (none|proficient|expert)\n"; - cout << indDesc << " name; value is new given name.\n"; - cout << indOpt << "add path entry\n"; - cout << indDesc << "Add entry to creature, where entry is an item or spell.\n"; - cout << indOpt << "help\n"; - cout << indDesc << "Show this help.\n"; + cout << indOpt << "[ls] [subfolder]" << endl; + cout << indDesc << "List creatures and objects." << endl; + cout << indOpt << "cp old-path new-path" << endl; + cout << indDesc << "Copy old-path to new-path." << endl; + cout << indOpt << "mv old-path new-path" << endl; + cout << indDesc << "Move old-path to new-path." << endl; + cout << indOpt << "rm path" << endl; + cout << indDesc << "Remove existing creature, object, or directory." << endl; + cout << indOpt << "roll path name" << endl; + cout << indDesc << "Roll a skill check, save, or attack." << endl; + cout << indOpt << "damage path amount [type]" << endl; + cout << indDesc << "Damage creature by amount. Type defaults to \"force\"." << endl; + cout << indOpt << "heal path amount" << endl; + cout << indDesc << "Heal creature by amount." << endl; + cout << indOpt << "reset path" << endl; + cout << indDesc << "Reset creature to full health (as if completing a long rest)." << endl; + cout << indOpt << "set path field value" << endl; + cout << indDesc << "Set a field to a new value, where field is any of:" << endl; + cout << indDesc << " ability (str, dex, con, int, wis, cha); value is new ability score" << endl; + cout << indDesc << " skill (athletics, \"sleight of hand\", etc.); value is (none|proficient|expert)" << endl; + cout << indDesc << " name; value is new given name." << endl; + cout << indOpt << "add path entry" << endl; + cout << indDesc << "Add entry to creature, where entry is an item or spell." << endl; + cout << indOpt << "help" << endl; + cout << indDesc << "Show this help." << endl; } -void print(string path) { - creature::Creature c(utils::loadJson(path)); - cout << genText(c); +void print(const fs::path& path) { + auto e = entry::Entry::create(utils::loadJson(path)); + cout << e->getText() << endl; +} + +fs::path getBaseDir() { + return settings::getString("savedir"); +} + +fs::path getTruePath(const fs::path& virtPath) { + fs::path p = getBaseDir() / virtPath; + if(fs::directory_entry(p.string() + ".json").is_regular_file()) return p.string() + ".json"; + return p; +} + +// Ensure the system is set up correctly +void initFS() { + fs::directory_entry de = fs::directory_entry(getBaseDir()); + if(! de.exists()) { + fs::create_directories(de); + fs::copy(settings::getString("weapon"), de.path() / "weapons"); + fs::copy(settings::getString("armor"), de.path() / "armor"); + fs::copy(settings::getString("spellcasting"), de.path() / "spells"); + fs::copy(settings::getString("monsters"), de.path() / "creatures"); + } +} + +void list(const fs::path& p) { + fs::path truePath = getTruePath(p); + if(fs::directory_entry(truePath).is_regular_file()) { + print(truePath); + } + else if(fs::directory_entry(truePath).is_directory()) { + for(fs::directory_entry de : filesystem::directory_iterator(truePath)) { + if(de.is_directory()) { + cout << de.path().filename().string() << "/"; + } else { + cout << de.path().stem().string(); + } + cout << "" << endl; + } + } + else { + cerr << "Unknown path " << p << endl; + } } void list(vector<string> args) { - string baseDir = settings::getString("savedir"); - vector<string> listPaths; if(args.empty()) { - listPaths.push_back(baseDir); + list(""); } else { - for(auto dir : args) { - listPaths.push_back(baseDir + "/" + dir); + for(string dir : args) { + list(dir); } } - for(auto listPath : listPaths) { - if(fs::is_regular_file(fs::status(listPath))) { - // Try loading and printing stuff about it - print(listPath); - } - else if(fs::is_directory(fs::status(listPath))) { - for(fs::directory_entry path : filesystem::directory_iterator(listPath)) { - cout << path.path().filename().string(); - if(path.is_directory()) { - cout << "/"; - } - cout << "\n"; - } - } +} + +void cp(vector<string> args) { + if(args.size() != 2) { + cerr << "Subcommand 'cp' expected 2 arguments but got " << args.size() << endl; } + fs::path src = getTruePath(args[0]); + fs::path dest = getTruePath(args[1]); + fs::copy(src, dest); } -void cp(vector<string> args) {} void mv(vector<string> args) {} void rm(vector<string> args) {} void roll(vector<string> args) {} @@ -85,6 +119,12 @@ void add(vector<string> args) {} int main(int argc, char *argv[]) { string exename = argv[0]; vector<string> args(&argv[1], &argv[argc]); + try { + initFS(); + } catch (fs::filesystem_error& e) { + cerr << e.what() << endl; + return 1; + } if(args.empty()) { list(args); return 0; |