summaryrefslogtreecommitdiffstatshomepage
path: root/src/frontend/mame/ui/systemlist.cpp
blob: d7450aac162c8378ea41f9ce242b8d34b2af7947 (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
// license:BSD-3-Clause
// copyright-holders:Vas Crabb
/***************************************************************************

    ui/systemlist.h

    Persistent system list data.

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

#include "emu.h"
#include "ui/systemlist.h"

#include "ui/moptions.h"

#include "drivenum.h"
#include "fileio.h"

#include "util/corestr.h"
#include "util/unicode.h"

#include <algorithm>
#include <cassert>
#include <functional>
#include <locale>
#include <string_view>


namespace ui {

void system_list::cache_data(ui_options const &options)
{
	std::unique_lock<std::mutex> lock(m_mutex);
	if (!m_started)
	{
		m_started = true;
#if defined(__EMSCRIPTEN__)
		std::invoke(
#else
		m_thread = std::make_unique<std::thread>(
#endif
				[this, datpath = std::string(options.history_path()), titles = std::string(options.system_names())]
				{
					do_cache_data(datpath, titles);
				});
	}
}


void system_list::reset_cache()
{
	std::unique_lock<std::mutex> lock(m_mutex);
	if (m_thread)
		m_thread->join();
	m_thread.reset();
	m_started = false;
	m_available = AVAIL_NONE;
	m_systems.clear();
	m_sorted_list.clear();
	m_filter_data = machine_filter_data();
	m_bios_count = 0;
}


void system_list::wait_available(available desired)
{
	if (!is_available(desired))
	{
		assert(m_started);
		std::unique_lock<std::mutex> lock(m_mutex);
		if (!is_available(desired))
			m_condition.wait(lock, [this, desired] () { return is_available(desired); });
	}
}


system_list &system_list::instance()
{
	static system_list data;
	return data;
}


system_list::system_list()
	: m_started(false)
	, m_available(AVAIL_NONE)
	, m_bios_count(0)
{
}


system_list::~system_list()
{
	if (m_thread)
		m_thread->join();
}


void system_list::notify_available(available value)
{
	std::unique_lock<std::mutex> lock(m_mutex);
	m_available.fetch_or(value, std::memory_order_release);
	m_condition.notify_all();
}


void system_list::do_cache_data(std::string const &datpath, std::string const &titles)
{
	// try to open the titles file for optimisation reasons
	emu_file titles_file(datpath, OPEN_FLAG_READ);
	bool const try_titles(!titles.empty() && !titles_file.open(titles));

	// generate full list - initially ordered by shortname
	populate_list(!try_titles);

	// notify that BIOS count is valie
	notify_available(AVAIL_BIOS_COUNT);

	// try to load localised descriptions
	if (try_titles)
	{
		load_titles(titles_file);

		// populate parent descriptions while still ordered by shortname
		// already done on the first pass if built-in titles are used
		populate_parents();
	}

	// system names are finalised now
	notify_available(AVAIL_SYSTEM_NAMES);

	// get rid of the "empty" driver - we don't need positions to line up any more
	m_sorted_list.reserve(m_systems.size() - 1);
	auto const empty(driver_list::find(GAME_NAME(___empty)));
	for (ui_system_info &info : m_systems)
	{
		if (info.index != empty)
			m_sorted_list.emplace_back(info);
	}

	// sort drivers and notify
	std::locale const lcl;
	std::collate<wchar_t> const &coll = std::use_facet<std::collate<wchar_t> >(lcl);
	auto const compare_names =
			[&coll] (std::wstring const &wx, std::wstring const &wy) -> bool
			{
				return 0 > coll.compare(wx.data(), wx.data() + wx.size(), wy.data(), wy.data() + wy.size());
			};
	std::stable_sort(
			m_sorted_list.begin(),
			m_sorted_list.end(),
			[&compare_names] (ui_system_info const &lhs, ui_system_info const &rhs)
			{
				game_driver const &x(*lhs.driver);
				game_driver const &y(*rhs.driver);

				if (!lhs.is_clone && !rhs.is_clone)
				{
					return compare_names(
							lhs.reading_description.empty() ? wstring_from_utf8(lhs.description) : lhs.reading_description,
							rhs.reading_description.empty() ? wstring_from_utf8(rhs.description) : rhs.reading_description);
				}
				else if (lhs.is_clone && rhs.is_clone)
				{
					if (!std::strcmp(x.parent, y.parent))
					{
						return compare_names(
								lhs.reading_description.empty() ? wstring_from_utf8(lhs.description) : lhs.reading_description,
								rhs.reading_description.empty() ? wstring_from_utf8(rhs.description) : rhs.reading_description);
					}
					else
					{
						return compare_names(
								lhs.reading_parent.empty() ? wstring_from_utf8(lhs.parent) : lhs.reading_parent,
								rhs.reading_parent.empty() ? wstring_from_utf8(rhs.parent) : rhs.reading_parent);
					}
				}
				else if (!lhs.is_clone && rhs.is_clone)
				{
					if (!std::strcmp(x.name, y.parent))
					{
						return true;
					}
					else
					{
						return compare_names(
								lhs.reading_description.empty() ? wstring_from_utf8(lhs.description) : lhs.reading_description,
								rhs.reading_parent.empty() ? wstring_from_utf8(rhs.parent) : rhs.reading_parent);
					}
				}
				else
				{
					if (!std::strcmp(x.parent, y.name))
					{
						return false;
					}
					else
					{
						return compare_names(
								lhs.reading_parent.empty() ? wstring_from_utf8(lhs.parent) : lhs.reading_parent,
								rhs.reading_description.empty() ? wstring_from_utf8(rhs.description) : rhs.reading_description);
					}
				}
			});
	notify_available(AVAIL_SORTED_LIST);

	// sort manufacturers and years
	m_filter_data.finalise();
	notify_available(AVAIL_FILTER_DATA);

	// convert shortnames to UCS-4
	for (ui_system_info &info : m_sorted_list)
		info.ucs_shortname = ustr_from_utf8(normalize_unicode(info.driver->name, unicode_normalization_form::D, true));
	notify_available(AVAIL_UCS_SHORTNAME);

	// convert descriptions to UCS-4
	for (ui_system_info &info : m_sorted_list)
		info.ucs_description = ustr_from_utf8(normalize_unicode(info.description, unicode_normalization_form::D, true));
	notify_available(AVAIL_UCS_DESCRIPTION);

	// convert "<manufacturer> <description>" to UCS-4
	std::string buf;
	for (ui_system_info &info : m_sorted_list)
	{
		buf.assign(info.driver->manufacturer);
		buf.append(1, ' ');
		buf.append(info.description);
		info.ucs_manufacturer_description = ustr_from_utf8(normalize_unicode(buf, unicode_normalization_form::D, true));
	}
	notify_available(AVAIL_UCS_MANUF_DESC);

	// convert default descriptions to UCS-4
	if (try_titles)
	{
		for (ui_system_info &info : m_sorted_list)
		{
			std::string_view const fullname(info.driver->type.fullname());
			if (info.description != fullname)
				info.ucs_default_description = ustr_from_utf8(normalize_unicode(fullname, unicode_normalization_form::D, true));
		}
	}
	notify_available(AVAIL_UCS_DFLT_DESC);

	// convert "<manufacturer> <default description>" to UCS-4
	if (try_titles)
	{
		for (ui_system_info &info : m_sorted_list)
		{
			std::string_view const fullname(info.driver->type.fullname());
			if (info.description != fullname)
			{
				buf.assign(info.driver->manufacturer);
				buf.append(1, ' ');
				buf.append(fullname);
				info.ucs_manufacturer_default_description = ustr_from_utf8(normalize_unicode(buf, unicode_normalization_form::D, true));
			}
		}
	}
	notify_available(AVAIL_UCS_MANUF_DFLT_DESC);
}


void system_list::populate_list(bool copydesc)
{
	m_systems.reserve(driver_list::total());
	std::unordered_set<std::string> manufacturers, years;
	for (int x = 0; x < driver_list::total(); ++x)
	{
		game_driver const &driver(driver_list::driver(x));
		ui_system_info &ins(m_systems.emplace_back(driver, x, false));
		if (&driver != &GAME_NAME(___empty))
		{
			if (driver.flags & machine_flags::IS_BIOS_ROOT)
				++m_bios_count;

			if ((driver.parent[0] != '0') || driver.parent[1])
			{
				auto const parentindex(driver_list::find(driver.parent));
				if (copydesc)
				{
					if (0 <= parentindex)
					{
						game_driver const &parentdriver(driver_list::driver(parentindex));
						ins.is_clone = !(parentdriver.flags & machine_flags::IS_BIOS_ROOT);
						ins.parent = parentdriver.type.fullname();
					}
					else
					{
						ins.is_clone = false;
						ins.parent = driver.parent;
					}
				}
				else
				{
					ins.is_clone = (0 <= parentindex) && !(driver_list::driver(parentindex).flags & machine_flags::IS_BIOS_ROOT);
				}
			}

			if (copydesc)
				ins.description = driver.type.fullname();

			m_filter_data.add_manufacturer(driver.manufacturer);
			m_filter_data.add_year(driver.year);
			m_filter_data.add_source_file(driver.type.source());
		}
	}
}


void system_list::load_titles(util::core_file &file)
{
	char readbuf[1024];
	std::string convbuf;
	while (file.gets(readbuf, std::size(readbuf)))
	{
		// shortname, description, and description reading separated by tab
		auto const eoln(
				std::find_if(
					std::begin(readbuf),
					std::end(readbuf),
					[] (char ch) { return !ch || ('\n' == ch) || ('\r' == ch); }));
		auto const split(std::find(std::begin(readbuf), eoln, '\t'));
		if (eoln == split)
			continue;
		std::string_view const shortname(readbuf, split - readbuf);

		// find matching system - still sorted by shortname at this point
		auto const found(
				std::lower_bound(
					m_systems.begin(),
					m_systems.end(),
					shortname,
					[] (ui_system_info const &a, std::string_view const &b)
					{
						return a.driver->name < b;
					}));
		if ((m_systems.end() == found) || (shortname != found->driver->name))
		{
			//osd_printf_verbose("System '%s' not found\n", shortname); very spammy for single-driver builds
			continue;
		}

		// find the end of the description
		auto const descstart(std::next(split));
		auto const descend(std::find(descstart, eoln, '\t'));
		auto const description(strtrimspace(std::string_view(descstart, descend - descstart)));
		if (description.empty())
		{
			osd_printf_warning("Empty translated description for system '%s'\n", shortname);
		}
		else if (!found->description.empty())
		{
			osd_printf_warning(
					"Multiple translated descriptions for system '%s' ('%s' and '%s')\n",
					shortname,
					found->description,
					description);
		}
		else
		{
			found->description = description;
		}

		// populate the reading if it's present
		if (eoln == descend)
			continue;
		auto const readstart(std::next(descend));
		auto const readend(std::find(readstart, eoln, '\t'));
		auto const reading(strtrimspace(std::string_view(readstart, readend - readstart)));
		if (reading.empty())
		{
			osd_printf_warning("Empty translated description reading for system '%s'\n", shortname);
		}
		else
		{
			found->reading_description = wstring_from_utf8(reading);
			found->ucs_reading_description = ustr_from_utf8(normalize_unicode(reading, unicode_normalization_form::D, true));
			convbuf.assign(found->driver->manufacturer);
			convbuf.append(1, ' ');
			convbuf.append(reading);
			found->ucs_manufacturer_reading_description = ustr_from_utf8(normalize_unicode(convbuf, unicode_normalization_form::D, true));
		}
	}

	// fill in untranslated descriptions
	for (ui_system_info &info : m_systems)
	{
		if (info.description.empty())
			info.description = info.driver->type.fullname();
	}
}


void system_list::populate_parents()
{
	for (ui_system_info &info : m_systems)
	{
		if ((info.driver->parent[0] != '0') || info.driver->parent[1])
		{
			auto const found(
					std::lower_bound(
						m_systems.begin(),
						m_systems.end(),
						std::string_view(info.driver->parent),
						[] (ui_system_info const &a, std::string_view const &b)
						{
							return a.driver->name < b;
						}));
			if (m_systems.end() != found)
			{
				info.parent = found->description;
				info.reading_parent = found->reading_description;
			}
			else
			{
				info.parent = info.driver->parent;
			}
		}
	}
}

} // namespace ui