Skip to content

Commit

Permalink
DynRPG TextPlugin: Update to latest Player code
Browse files Browse the repository at this point in the history
  • Loading branch information
Ghabry committed Oct 2, 2020
1 parent ae75648 commit 337ac23
Show file tree
Hide file tree
Showing 11 changed files with 298 additions and 356 deletions.
7 changes: 0 additions & 7 deletions src/dynrpg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,12 @@
#include "player.h"

#include "dynrpg_easyrpg.h"

#include <cstring>
#include <fstream>

#include "dynrpg_textplugin.h"

#include <cstring>
#include <fstream>
#include <map>

#include "dynrpg_textplugin.h"

enum DynRpg_ParseMode {
ParseMode_Function,
ParseMode_WaitForComma,
Expand Down Expand Up @@ -191,7 +185,6 @@ static std::string ParseToken(const std::string& token, const std::string& funct

void create_all_plugins() {
plugins.emplace_back(new DynRpg::EasyRpgPlugin());

plugins.emplace_back(new DynRpg::TextPlugin());

for (auto& plugin : plugins) {
Expand Down
25 changes: 14 additions & 11 deletions src/dynrpg.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ using dynfunc = bool(*)(dyn_arg_list);
namespace DynRpg {
namespace detail {
template <typename T>
inline bool parse_arg(StringView, dyn_arg_list, const int, T& value, bool& parse_okay) {
inline bool parse_arg(StringView, dyn_arg_list, const int, T&, bool&, bool) {
static_assert(sizeof(T) == -1, "Only parsing int, float and std::string supported");
}

// FIXME: Extracting floats that are followed by chars behaviour varies depending on the C++ library
// see https://bugs.llvm.org/show_bug.cgi?id=17782
template <>
inline bool parse_arg(StringView func_name, dyn_arg_list args, const int i, float& value, bool& parse_okay) {
inline bool parse_arg(StringView func_name, dyn_arg_list args, const int i, float& value, bool& parse_okay, bool warn) {
if (!parse_okay) return false;
value = 0.0;
if (args[i].empty()) {
Expand All @@ -61,14 +61,15 @@ namespace DynRpg {
iss >> value;
parse_okay = !iss.fail();
if (!parse_okay) {
Output::Warning("{}: Arg {} ({}) is not numeric", func_name, i, args[i]);
if (warn)
Output::Warning("{}: Arg {} ({}) is not numeric", func_name, i, args[i]);
parse_okay = false;
}
return parse_okay;
}

template <>
inline bool parse_arg(StringView func_name, dyn_arg_list args, const int i, int& value, bool& parse_okay) {
inline bool parse_arg(StringView func_name, dyn_arg_list args, const int i, int& value, bool& parse_okay, bool warn) {
if (!parse_okay) return false;
value = 0;
if (args[i].empty()) {
Expand All @@ -79,23 +80,24 @@ namespace DynRpg {
iss >> value;
parse_okay = !iss.fail();
if (!parse_okay) {
Output::Warning("{}: Arg {} ({}) is not an integer", func_name, i, args[i]);
if (warn)
Output::Warning("{}: Arg {} ({}) is not an integer", func_name, i, args[i]);
parse_okay = false;
}
return parse_okay;
}

template <>
inline bool parse_arg(StringView, dyn_arg_list args, const int i, std::string& value, bool& parse_okay) {
inline bool parse_arg(StringView, dyn_arg_list args, const int i, std::string& value, bool& parse_okay, bool) {
if (!parse_okay) return false;
value = args[i];
parse_okay = true;
return parse_okay;
}

template <typename Tuple, std::size_t... I>
inline void parse_args(StringView func_name, dyn_arg_list in, Tuple& value, bool& parse_okay, std::index_sequence<I...>) {
(void)std::initializer_list<bool>{parse_arg(func_name, in, I, std::get<I>(value), parse_okay)...};
inline void parse_args(StringView func_name, dyn_arg_list in, Tuple& value, bool& parse_okay, bool warn, std::index_sequence<I...>) {
(void)std::initializer_list<bool>{parse_arg(func_name, in, I, std::get<I>(value), parse_okay, warn)...};
}
}

Expand All @@ -111,16 +113,17 @@ namespace DynRpg {
void Save(int slot);

template <typename... Targs>
std::tuple<Targs...> ParseArgs(StringView func_name, dyn_arg_list args, bool* parse_okay = nullptr) {
std::tuple<Targs...> ParseArgs(StringView func_name, dyn_arg_list args, bool* parse_okay = nullptr, bool warn = true) {
std::tuple<Targs...> t;
if (args.size() < sizeof...(Targs)) {
if (parse_okay)
*parse_okay = false;
Output::Warning("{}: Got {} args (needs {} or more)", func_name, args.size(), sizeof...(Targs));
if (warn)
Output::Warning("{}: Got {} args (needs {} or more)", func_name, args.size(), sizeof...(Targs));
return t;
}
bool okay = true;
detail::parse_args(func_name, args, t, okay, std::make_index_sequence<sizeof...(Targs)>{});
detail::parse_args(func_name, args, t, okay, warn, std::make_index_sequence<sizeof...(Targs)>{});
if (parse_okay)
*parse_okay = okay;
return t;
Expand Down
Loading

0 comments on commit 337ac23

Please sign in to comment.