summaryrefslogtreecommitdiffstatshomepage
path: root/src/frontend/mame/pluginopts.cpp
blob: cfb04af21a4f927a862bbb3f1e256349dedc41c0 (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
// license:BSD-3-Clause
// copyright-holders:Miodrag Milanovic
/***************************************************************************

    pluginopts.cpp

    Plugin options manager.

***************************************************************************/
#include "emu.h"
#include "pluginopts.h"

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

#include <fstream>


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

const options_entry plugin_options::s_option_entries[] =
{
	{ nullptr,  nullptr,  OPTION_HEADER,  "PLUGINS OPTIONS" },
	{ nullptr }
};

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

plugin_options::plugin_options()
: core_options()
{
	add_entries(plugin_options::s_option_entries);
}


void plugin_options::parse_json(std::string path)
{
	// first try to open as a directory
	osd_subst_env(path, path);
	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)
			{
				std::string name = entry->name;
				if (name == "plugin.json")
				{
					std::string curfile = std::string(path).append(PATH_SEPARATOR).append(entry->name);
					std::ifstream ifs(curfile);
					rapidjson::IStreamWrapper isw(ifs);
					rapidjson::Document document;
					document.ParseStream<0>(isw);

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

					if (document["plugin"].IsObject())
					{
						std::string plugin_name = document["plugin"]["name"].GetString();
						std::string description = document["plugin"]["description"].GetString();
						std::string type = document["plugin"]["type"].GetString();
						bool start = false;
						if (document["plugin"].HasMember("start") && (std::string(document["plugin"]["start"].GetString()) == "true"))
							start = true;

						if (type=="plugin")
						{
							auto const it = m_descriptions.emplace(m_descriptions.end(), std::move(description));
							add_entry({ std::move(plugin_name) }, it->c_str(), option_type::BOOLEAN, start ? "1" : "0");
						}
					}

				}
			}
			else if (entry->type == osd::directory::entry::entry_type::DIR)
			{
				std::string name = entry->name;
				if (!(name == "." || name == ".."))
				{
					parse_json(path + PATH_SEPARATOR + name);
				}
			}
		}
	}
}