2018-02-09 17:23:58 +00:00
|
|
|
#include "MameNames.h"
|
|
|
|
|
|
|
|
#include "resources/ResourceManager.h"
|
|
|
|
#include "utils/FileSystemUtil.h"
|
|
|
|
#include "Log.h"
|
|
|
|
#include <pugixml/src/pugixml.hpp>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
MameNames* MameNames::sInstance = nullptr;
|
|
|
|
|
|
|
|
void MameNames::init()
|
|
|
|
{
|
|
|
|
if(!sInstance)
|
|
|
|
sInstance = new MameNames();
|
|
|
|
|
|
|
|
} // init
|
|
|
|
|
|
|
|
void MameNames::deinit()
|
|
|
|
{
|
|
|
|
if(sInstance)
|
|
|
|
{
|
|
|
|
delete sInstance;
|
|
|
|
sInstance = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // deinit
|
|
|
|
|
|
|
|
MameNames* MameNames::getInstance()
|
|
|
|
{
|
|
|
|
if(!sInstance)
|
|
|
|
sInstance = new MameNames();
|
|
|
|
|
|
|
|
return sInstance;
|
|
|
|
|
|
|
|
} // getInstance
|
|
|
|
|
|
|
|
MameNames::MameNames()
|
|
|
|
{
|
|
|
|
std::string xmlpath = ResourceManager::getInstance()->getResourcePath(":/mamenames.xml");
|
|
|
|
|
|
|
|
if(!Utils::FileSystem::exists(xmlpath))
|
|
|
|
return;
|
|
|
|
|
|
|
|
LOG(LogInfo) << "Parsing XML file \"" << xmlpath << "\"...";
|
|
|
|
|
|
|
|
pugi::xml_document doc;
|
|
|
|
pugi::xml_parse_result result = doc.load_file(xmlpath.c_str());
|
|
|
|
|
|
|
|
if(!result)
|
|
|
|
{
|
|
|
|
LOG(LogError) << "Error parsing XML file \"" << xmlpath << "\"!\n " << result.description();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(pugi::xml_node gameNode = doc.child("game"); gameNode; gameNode = gameNode.next_sibling("game"))
|
|
|
|
{
|
|
|
|
NamePair namePair = { gameNode.child("mamename").text().get(), gameNode.child("realname").text().get() };
|
2018-02-15 14:03:42 +00:00
|
|
|
mNamePairs.push_back(namePair);
|
2018-02-09 17:23:58 +00:00
|
|
|
}
|
2019-08-25 15:23:02 +00:00
|
|
|
|
2018-05-10 01:29:46 +00:00
|
|
|
// Read bios
|
|
|
|
xmlpath = ResourceManager::getInstance()->getResourcePath(":/mamebioses.xml");
|
2019-08-25 15:23:02 +00:00
|
|
|
|
2018-05-10 01:29:46 +00:00
|
|
|
if(!Utils::FileSystem::exists(xmlpath))
|
|
|
|
return;
|
2019-08-25 15:23:02 +00:00
|
|
|
|
2018-05-10 01:29:46 +00:00
|
|
|
LOG(LogInfo) << "Parsing XML file \"" << xmlpath << "\"...";
|
2019-08-25 15:23:02 +00:00
|
|
|
|
2018-05-10 01:29:46 +00:00
|
|
|
result = doc.load_file(xmlpath.c_str());
|
2019-08-25 15:23:02 +00:00
|
|
|
|
2018-05-10 01:29:46 +00:00
|
|
|
if(!result)
|
|
|
|
{
|
|
|
|
LOG(LogError) << "Error parsing XML file \"" << xmlpath << "\"!\n " << result.description();
|
|
|
|
return;
|
|
|
|
}
|
2019-08-25 15:23:02 +00:00
|
|
|
|
2018-05-10 01:29:46 +00:00
|
|
|
for(pugi::xml_node biosNode = doc.child("bios"); biosNode; biosNode = biosNode.next_sibling("bios"))
|
|
|
|
{
|
|
|
|
std::string bios = biosNode.text().get();
|
|
|
|
mMameBioses.push_back(bios);
|
|
|
|
}
|
2019-08-25 15:23:02 +00:00
|
|
|
|
2018-05-10 01:29:46 +00:00
|
|
|
// Read devices
|
|
|
|
xmlpath = ResourceManager::getInstance()->getResourcePath(":/mamedevices.xml");
|
2019-08-25 15:23:02 +00:00
|
|
|
|
2018-05-10 01:29:46 +00:00
|
|
|
if(!Utils::FileSystem::exists(xmlpath))
|
|
|
|
return;
|
2019-08-25 15:23:02 +00:00
|
|
|
|
2018-05-10 01:29:46 +00:00
|
|
|
LOG(LogInfo) << "Parsing XML file \"" << xmlpath << "\"...";
|
2019-08-25 15:23:02 +00:00
|
|
|
|
2018-05-10 01:29:46 +00:00
|
|
|
result = doc.load_file(xmlpath.c_str());
|
2019-08-25 15:23:02 +00:00
|
|
|
|
2018-05-10 01:29:46 +00:00
|
|
|
if(!result)
|
|
|
|
{
|
|
|
|
LOG(LogError) << "Error parsing XML file \"" << xmlpath << "\"!\n " << result.description();
|
|
|
|
return;
|
|
|
|
}
|
2019-08-25 15:23:02 +00:00
|
|
|
|
2018-05-10 01:29:46 +00:00
|
|
|
for(pugi::xml_node deviceNode = doc.child("device"); deviceNode; deviceNode = deviceNode.next_sibling("device"))
|
|
|
|
{
|
|
|
|
std::string device = deviceNode.text().get();
|
|
|
|
mMameDevices.push_back(device);
|
|
|
|
}
|
2018-02-09 17:23:58 +00:00
|
|
|
|
|
|
|
} // MameNames
|
|
|
|
|
|
|
|
MameNames::~MameNames()
|
|
|
|
{
|
|
|
|
|
|
|
|
} // ~MameNames
|
|
|
|
|
|
|
|
std::string MameNames::getRealName(const std::string& _mameName)
|
|
|
|
{
|
|
|
|
size_t start = 0;
|
2018-02-15 14:03:42 +00:00
|
|
|
size_t end = mNamePairs.size();
|
2018-02-09 17:23:58 +00:00
|
|
|
|
|
|
|
while(start < end)
|
|
|
|
{
|
|
|
|
const size_t index = (start + end) / 2;
|
2018-02-15 14:03:42 +00:00
|
|
|
const int compare = strcmp(mNamePairs[index].mameName.c_str(), _mameName.c_str());
|
2018-02-09 17:23:58 +00:00
|
|
|
|
|
|
|
if(compare < 0) start = index + 1;
|
|
|
|
else if( compare > 0) end = index;
|
2018-02-15 14:03:42 +00:00
|
|
|
else return mNamePairs[index].realName;
|
2018-02-09 17:23:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return _mameName;
|
|
|
|
|
|
|
|
} // getRealName
|
2018-05-10 01:29:46 +00:00
|
|
|
|
|
|
|
const bool MameNames::isBios(const std::string& _biosName)
|
|
|
|
{
|
|
|
|
return MameNames::find(mMameBioses, _biosName);
|
|
|
|
|
|
|
|
} // isBios
|
|
|
|
|
|
|
|
const bool MameNames::isDevice(const std::string& _deviceName)
|
|
|
|
{
|
|
|
|
return MameNames::find(mMameDevices, _deviceName);
|
2019-08-25 15:23:02 +00:00
|
|
|
|
2018-05-10 01:29:46 +00:00
|
|
|
} // isDevice
|
|
|
|
|
|
|
|
const bool MameNames::find(std::vector<std::string> devices, const std::string& name)
|
|
|
|
{
|
|
|
|
size_t start = 0;
|
|
|
|
size_t end = devices.size();
|
|
|
|
|
|
|
|
while(start < end)
|
|
|
|
{
|
|
|
|
const size_t index = (start + end) / 2;
|
|
|
|
const int compare = strcmp(devices[index].c_str(), name.c_str());
|
|
|
|
|
|
|
|
if(compare < 0) start = index + 1;
|
|
|
|
else if( compare > 0) end = index;
|
|
|
|
else return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2019-08-25 15:23:02 +00:00
|
|
|
|
2018-05-10 01:29:46 +00:00
|
|
|
}
|