summaryrefslogtreecommitdiffstatshomepage
path: root/src/frontend/mame/pluginopts.cpp
blob: 3828be3c8cd4efaf704cbe97d52d0d0c6119d4e6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// license:BSD-3-Clause
// copyright-holders:Miodrag Milanovic
/***************************************************************************

    pluginopts.cpp

    Plugin options manager.

***************************************************************************/

#include "emu.h"
#include "pluginopts.h"
#include "options.h"

#include <rapidjson/document.h>
#include <rapidjson/error/en.h>
#include <rapidjson/istreamwrapper.h>

#include <fstream>


//**************************************************************************
//  PLUGIN OPTIONS
//**************************************************************************

//-------------------------------------------------
//  plugin_options - constructor
//-------------------------------------------------

plugin_options::plugin_options()
{
}


//-------------------------------------------------
//  scan_directory
//-------------------------------------------------

void plugin_options::scan_directory(const std::string &path, bool recursive)
{
	// first try to open as a directory
	osd::directory::ptr directory = osd::directory::open(path);
	if (directory)
	{
		// iterate over all files in the directory
		for (const osd::directory::entry *entry = directory->read(); entry != nullptr; entry = directory->read())
		{
			if (entry->type == osd::directory::entry::entry_type::FILE && !strcmp(entry->name, "plugin.json"))
			{
				std::string curfile = std::string(path).append(PATH_SEPARATOR).append(entry->name);
				load_plugin(curfile);
			}
			else if (entry->type == osd::directory::entry::entry_type::DIR)
			{
				if (recursive && strcmp(entry->name, ".") && strcmp(entry->name, ".."))
					scan_directory(path + PATH_SEPARATOR + entry->name, recursive);
			}
		}
	}
}


//-------------------------------------------------
//  load_plugin
//-------------------------------------------------

bool plugin_options::load_plugin(const std::string &path)
{
	std::ifstream ifs(path);
	rapidjson::IStreamWrapper isw(ifs);
	rapidjson::Document document;
	document.ParseStream<0>(isw);

	if (document.HasParseError())
	{
		const std::string error(GetParseError_En(document.GetParseError()));
		osd_printf_error("Unable to parse plugin definition file %s. Errors returned:\n%s", path, error);
		return false;
	}

	if (!document["plugin"].IsObject())
	{
		osd_printf_error("Bad plugin definition file %s:\n", path);
		return false;
	}

	size_t last_path_sep = path.find_last_of(PATH_SEPARATOR[0]);
	std::string dir = last_path_sep != std::string::npos
		? path.substr(0, last_path_sep)
		: ".";

	plugin p;
	p.m_name        = document["plugin"]["name"].GetString();
	p.m_description = document["plugin"]["description"].GetString();
	p.m_type        = document["plugin"]["type"].GetString();
	p.m_directory   = std::move(dir);
	p.m_start       = false;
	if (document["plugin"].HasMember("start") && (std::string(document["plugin"]["start"].GetString()) == "true"))
		p.m_start = true;

	m_plugins.push_back(std::move(p));
	return true;
}


//-------------------------------------------------
//  find
//-------------------------------------------------

plugin *plugin_options::find(const std::string &name)
{
	auto iter = std::find_if(
		m_plugins.begin(),
		m_plugins.end(),
		[&name](const plugin &p) { return name == p.m_name; });

	return iter != m_plugins.end()
		? &*iter
		: nullptr;
}


//-------------------------------------------------
//  create_core_options
//-------------------------------------------------

static core_options create_core_options(const plugin_options &plugin_opts)
{
	// we're sort of abusing core_options to just get INI file parsing, so we'll build a
	// core_options structure for the sole purpose of parsing an INI file, and then reflect
	// the data back
	static const options_entry s_option_entries[] =
	{
		{ nullptr, nullptr, OPTION_HEADER, "PLUGINS OPTIONS" },
		{ nullptr }
	};

	core_options opts;
	opts.add_entries(s_option_entries);

	// create an entry for each option
	for (const plugin &p : plugin_opts.plugins())
	{
		opts.add_entry(
			{ p.m_name },
			nullptr,
			core_options::option_type::BOOLEAN,
			p.m_start ? "1" : "0");
	}

	return opts;
}


//-------------------------------------------------
//  parse_ini_file
//-------------------------------------------------

void plugin_options::parse_ini_file(util::core_file &inifile)
{
	core_options opts = create_core_options(*this);

	// parse the INI file
	opts.parse_ini_file(inifile, OPTION_PRIORITY_NORMAL, true, true);

	// and reflect these options back
	for (plugin &p : m_plugins)
		p.m_start = opts.bool_value(p.m_name.c_str());
}


//-------------------------------------------------
//  output_ini
//-------------------------------------------------

std::string plugin_options::output_ini() const
{
	core_options opts = create_core_options(*this);
	return opts.output_ini();
}