Skip to content
This repository has been archived by the owner on Apr 25, 2022. It is now read-only.

Commit

Permalink
util: introduce PluginManger
Browse files Browse the repository at this point in the history
Managing --plugin option was similar across all the implementations
(there were 3 decoders that used this option), so I extracted a class
that shares common code.
  • Loading branch information
rr- committed Sep 3, 2015
1 parent da3a981 commit 97d06fb
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 165 deletions.
51 changes: 26 additions & 25 deletions src/fmt/ivory/mbl_archive.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "fmt/ivory/wady_converter.h"
#include "util/encoding.h"
#include "util/format.h"
#include "util/plugin_mgr.hh"
#include "util/range.h"
#include "log.h"

Expand Down Expand Up @@ -100,35 +101,41 @@ static std::unique_ptr<File> read_file(

struct MblArchive::Priv
{
util::PluginManager<PluginFunc> plugin_mgr;
PrsConverter prs_converter;
WadyConverter wady_converter;
PluginFunc plugin;
PluginTable all_plugins;
};

MblArchive::MblArchive() : p(new Priv)
{
add_transformer(&p->prs_converter);
add_transformer(&p->wady_converter);

p->all_plugins["candy"] = [](bstr &data)
p->plugin_mgr.add("noop", "Unencrypted games", []()
{
for (auto i : util::range(data.size()))
data[i] = -data[i];
};
return [](bstr &) { };
});

p->all_plugins["wanko"] = [](bstr &data)
p->plugin_mgr.add("candy", "Candy Toys", []()
{
static const bstr key =
"\x82\xED\x82\xF1\x82\xB1"
"\x88\xC3\x8D\x86\x89\xBB"_b;
for (auto i : util::range(data.size()))
data[i] ^= key[i % key.size()];
};

p->all_plugins["noop"] = [](bstr &)
return [](bstr &data)
{
for (auto i : util::range(data.size()))
data[i] = -data[i];
};
});

p->plugin_mgr.add("wanko", "Wanko to Kurasou", []()
{
};
return [](bstr &data)
{
static const bstr key =
"\x82\xED\x82\xF1\x82\xB1\x88\xC3\x8D\x86\x89\xBB"_b;
for (auto i : util::range(data.size()))
data[i] ^= key[i % key.size()];
};
});
}

MblArchive::~MblArchive()
Expand All @@ -137,26 +144,20 @@ MblArchive::~MblArchive()

void MblArchive::register_cli_options(ArgParser &arg_parser) const
{
auto sw = arg_parser.register_switch({"-p", "--plugin"})
->set_value_name("PLUGIN")
->set_description("Specifies plugin for decoding dialog files.");
for (auto &it : p->all_plugins)
sw->add_possible_value(it.first);
p->plugin_mgr.register_cli_options(
arg_parser, "Specifies plugin for decoding dialog files.");
Archive::register_cli_options(arg_parser);
}

void MblArchive::parse_cli_options(const ArgParser &arg_parser)
{
if (arg_parser.has_switch("--plugin"))
set_plugin(arg_parser.get_switch("--plugin"));
p->plugin = p->plugin_mgr.get_from_cli_options(arg_parser, false);
Archive::parse_cli_options(arg_parser);
}

void MblArchive::set_plugin(const std::string &plugin_name)
{
if (p->all_plugins.find(plugin_name) == p->all_plugins.end())
throw std::runtime_error("Unknown plugin: " + plugin_name);
p->plugin = p->all_plugins[plugin_name];
p->plugin = p->plugin_mgr.get_from_string(plugin_name);
}

bool MblArchive::is_recognized_internal(File &arc_file) const
Expand Down
34 changes: 30 additions & 4 deletions src/fmt/kirikiri/xp3_archive.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@

#include "fmt/kirikiri/tlg_converter.h"
#include "fmt/kirikiri/xp3_archive.h"
#include "fmt/kirikiri/xp3_filter_factory.h"
#include "fmt/kirikiri/xp3_filters/cxdec_comyu.h"
#include "fmt/kirikiri/xp3_filters/cxdec_fha.h"
#include "fmt/kirikiri/xp3_filters/cxdec_mahoyoru.h"
#include "fmt/kirikiri/xp3_filters/fsn.h"
#include "fmt/kirikiri/xp3_filters/noop.h"
#include "io/buffered_io.h"
#include "util/encoding.h"
#include "util/pack/zlib.h"
#include "util/plugin_mgr.hh"

using namespace au;
using namespace au::fmt::kirikiri;
Expand All @@ -31,6 +36,11 @@ static const bstr adlr_magic = "adlr"_b;
static const bstr info_magic = "info"_b;
static const bstr segm_magic = "segm"_b;

namespace
{
using FilterPtr = std::unique_ptr<Xp3Filter>;
}

static int detect_version(io::IO &arc_io)
{
int version = 1;
Expand Down Expand Up @@ -163,7 +173,7 @@ static std::unique_ptr<File> read_file(

struct Xp3Archive::Priv
{
Xp3FilterFactory filter_factory;
util::PluginManager<FilterPtr> plugin_mgr;
TlgConverter tlg_converter;
std::unique_ptr<Xp3Filter> filter;

Expand All @@ -175,6 +185,21 @@ struct Xp3Archive::Priv
Xp3Archive::Xp3Archive() : p(new Priv)
{
add_transformer(&p->tlg_converter);

p->plugin_mgr.add("noop", "Unecrypted games",
[]() { return FilterPtr(new xp3_filters::Noop); });

p->plugin_mgr.add("comyu", "Comyu - Kuroi Ryuu to Yasashii Oukoku",
[]() { return FilterPtr(new xp3_filters::CxdecComyu); });

p->plugin_mgr.add("fsn", "Fate/Stay Night",
[]() { return FilterPtr(new xp3_filters::Fsn); });

p->plugin_mgr.add("fha", "Fate/Hollow Ataraxia",
[]() { return FilterPtr(new xp3_filters::CxdecFha); });

p->plugin_mgr.add("mahoyoru", "Mahou Tsukai no Yoru",
[]() { return FilterPtr(new xp3_filters::CxdecMahoYoru); });
}

Xp3Archive::~Xp3Archive()
Expand All @@ -183,13 +208,14 @@ Xp3Archive::~Xp3Archive()

void Xp3Archive::register_cli_options(ArgParser &arg_parser) const
{
p->filter_factory.register_cli_options(arg_parser);
p->plugin_mgr.register_cli_options(
arg_parser, "Selects XP3 decryption routine.");
Archive::register_cli_options(arg_parser);
}

void Xp3Archive::parse_cli_options(const ArgParser &arg_parser)
{
p->filter = p->filter_factory.get_filter_from_cli_options(arg_parser);
p->filter = p->plugin_mgr.get_from_cli_options(arg_parser, true);
Archive::parse_cli_options(arg_parser);
}

Expand Down
91 changes: 0 additions & 91 deletions src/fmt/kirikiri/xp3_filter_factory.cc

This file was deleted.

23 changes: 0 additions & 23 deletions src/fmt/kirikiri/xp3_filter_factory.h

This file was deleted.

33 changes: 11 additions & 22 deletions src/fmt/nitroplus/npa_archive.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "fmt/nitroplus/npa_filters/chaos_head.h"
#include "util/encoding.h"
#include "util/pack/zlib.h"
#include "util/plugin_mgr.hh"
#include "util/range.h"

using namespace au;
Expand Down Expand Up @@ -174,11 +175,18 @@ static std::unique_ptr<File> read_file(

struct NpaArchive::Priv
{
util::PluginManager<std::unique_ptr<NpaFilter>> plugin_mgr;
std::unique_ptr<NpaFilter> filter;
};

NpaArchive::NpaArchive() : p(new Priv)
{
p->plugin_mgr.add("chaos_head", "ChaoS;HEAd", []()
{
std::unique_ptr<NpaFilter> filter(new NpaFilter);
npa_filters::chaos_head_filter_init(*filter);
return filter;
});
}

NpaArchive::~NpaArchive()
Expand All @@ -187,33 +195,14 @@ NpaArchive::~NpaArchive()

void NpaArchive::register_cli_options(ArgParser &arg_parser) const
{
arg_parser.register_switch({"-p", "--plugin"})
->set_value_name("PLUGIN")
->set_description("Selects NPA decryption routine.")
->add_possible_value("chaos_head");

p->plugin_mgr.register_cli_options(
arg_parser, "Selects NPA decryption routine.");
Archive::register_cli_options(arg_parser);
}

void NpaArchive::parse_cli_options(const ArgParser &arg_parser)
{
const std::string plugin = arg_parser.get_switch("plugin").c_str();
void (*initializer)(NpaFilter&) = nullptr;
if (plugin == "chaos_head")
initializer = &npa_filters::chaos_head_filter_init;
else
throw std::runtime_error("Unrecognized plugin: " + plugin);

if (initializer != nullptr)
{
p->filter.reset(new NpaFilter);
initializer(*p->filter);
}
else
{
p->filter.reset(nullptr);
}

p->filter = p->plugin_mgr.get_from_cli_options(arg_parser, true);
Archive::parse_cli_options(arg_parser);
}

Expand Down
Loading

0 comments on commit 97d06fb

Please sign in to comment.