2015-05-23 22:55:12 -06:00
|
|
|
// Copyright 2009 Dolphin Emulator Project
|
2015-05-17 17:08:10 -06:00
|
|
|
// Licensed under GPLv2+
|
2013-04-17 21:09:55 -06:00
|
|
|
// Refer to the license.txt file included.
|
2010-02-19 10:05:26 -07:00
|
|
|
|
2017-03-03 12:43:52 -07:00
|
|
|
#include "Common/SysConf.h"
|
|
|
|
|
2017-02-25 15:59:31 -07:00
|
|
|
#include <algorithm>
|
2017-03-10 10:11:45 -07:00
|
|
|
#include <array>
|
2014-02-19 20:11:52 -07:00
|
|
|
#include <cstdio>
|
2013-10-26 03:55:41 -06:00
|
|
|
|
2014-11-17 18:59:14 -07:00
|
|
|
#include "Common/CommonPaths.h"
|
2014-09-07 19:06:58 -06:00
|
|
|
#include "Common/CommonTypes.h"
|
2017-01-15 13:46:32 -07:00
|
|
|
#include "Common/File.h"
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "Common/FileUtil.h"
|
2017-03-10 10:11:45 -07:00
|
|
|
#include "Common/Logging/Log.h"
|
2017-03-03 12:43:52 -07:00
|
|
|
#include "Common/Swap.h"
|
2016-02-28 17:42:15 -07:00
|
|
|
#include "Core/Movie.h"
|
|
|
|
|
2017-03-10 10:11:45 -07:00
|
|
|
constexpr size_t SYSCONF_SIZE = 0x4000;
|
|
|
|
|
|
|
|
static size_t GetNonArrayEntrySize(SysConf::Entry::Type type)
|
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case SysConf::Entry::Type::Byte:
|
|
|
|
case SysConf::Entry::Type::ByteBool:
|
|
|
|
return 1;
|
|
|
|
case SysConf::Entry::Type::Short:
|
|
|
|
return 2;
|
|
|
|
case SysConf::Entry::Type::Long:
|
|
|
|
return 4;
|
|
|
|
case SysConf::Entry::Type::LongLong:
|
|
|
|
return 8;
|
|
|
|
default:
|
|
|
|
_assert_(false);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2017-01-06 13:59:02 -07:00
|
|
|
SysConf::SysConf(const Common::FromWhichRoot root_type)
|
2010-02-19 10:05:26 -07:00
|
|
|
{
|
2017-03-10 10:11:45 -07:00
|
|
|
m_file_name = Common::RootUserPath(root_type) + DIR_SEP WII_SYSCONF_DIR DIR_SEP WII_SYSCONF;
|
|
|
|
Load();
|
2010-02-19 10:05:26 -07:00
|
|
|
}
|
2010-02-02 14:56:29 -07:00
|
|
|
|
2010-02-19 10:05:26 -07:00
|
|
|
SysConf::~SysConf()
|
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
Save();
|
2011-03-02 17:07:33 -07:00
|
|
|
}
|
2010-02-19 11:50:01 -07:00
|
|
|
|
2011-03-02 17:07:33 -07:00
|
|
|
void SysConf::Clear()
|
|
|
|
{
|
2017-03-10 10:11:45 -07:00
|
|
|
m_entries.clear();
|
2010-02-19 10:05:26 -07:00
|
|
|
}
|
|
|
|
|
2017-03-10 10:11:45 -07:00
|
|
|
void SysConf::Load()
|
2010-02-19 10:05:26 -07:00
|
|
|
{
|
2017-03-10 10:11:45 -07:00
|
|
|
Clear();
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2017-06-29 03:20:38 -06:00
|
|
|
if (File::GetSize(m_file_name) != SYSCONF_SIZE || !LoadFromFile(m_file_name))
|
2016-06-24 02:43:46 -06:00
|
|
|
{
|
2017-03-10 10:11:45 -07:00
|
|
|
WARN_LOG(CORE, "No valid SYSCONF detected. Creating a new one.");
|
|
|
|
InsertDefaultEntries();
|
2016-06-24 02:43:46 -06:00
|
|
|
}
|
|
|
|
|
2017-03-10 10:11:45 -07:00
|
|
|
ApplySettingsFromMovie();
|
2010-02-19 10:05:26 -07:00
|
|
|
}
|
|
|
|
|
2017-03-10 10:11:45 -07:00
|
|
|
bool SysConf::LoadFromFile(const std::string& file_name)
|
2017-01-06 13:59:02 -07:00
|
|
|
{
|
2017-03-10 10:11:45 -07:00
|
|
|
File::IOFile file(file_name, "rb");
|
|
|
|
file.Seek(4, SEEK_SET);
|
|
|
|
u16 number_of_entries;
|
|
|
|
file.ReadBytes(&number_of_entries, sizeof(number_of_entries));
|
|
|
|
number_of_entries = Common::swap16(number_of_entries);
|
|
|
|
|
|
|
|
std::vector<u16> offsets(number_of_entries);
|
|
|
|
for (u16& offset : offsets)
|
2016-06-24 02:43:46 -06:00
|
|
|
{
|
2017-03-10 10:11:45 -07:00
|
|
|
file.ReadBytes(&offset, sizeof(offset));
|
|
|
|
offset = Common::swap16(offset);
|
2016-06-24 02:43:46 -06:00
|
|
|
}
|
|
|
|
|
2017-03-10 10:11:45 -07:00
|
|
|
for (const u16 offset : offsets)
|
2016-06-24 02:43:46 -06:00
|
|
|
{
|
2017-03-10 10:11:45 -07:00
|
|
|
file.Seek(offset, SEEK_SET);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2017-03-10 10:11:45 -07:00
|
|
|
// Metadata
|
2016-06-24 02:43:46 -06:00
|
|
|
u8 description = 0;
|
2017-03-10 10:11:45 -07:00
|
|
|
file.ReadBytes(&description, sizeof(description));
|
|
|
|
const Entry::Type type = static_cast<Entry::Type>((description & 0xe0) >> 5);
|
|
|
|
const u8 name_length = (description & 0x1f) + 1;
|
|
|
|
std::string name(name_length, '\0');
|
|
|
|
file.ReadBytes(&name[0], name.size());
|
|
|
|
|
|
|
|
// Data
|
|
|
|
std::vector<u8> data;
|
|
|
|
switch (type)
|
2016-06-24 02:43:46 -06:00
|
|
|
{
|
2017-03-10 10:11:45 -07:00
|
|
|
case Entry::Type::BigArray:
|
2016-06-24 02:43:46 -06:00
|
|
|
{
|
2017-03-10 10:11:45 -07:00
|
|
|
u16 data_length = 0;
|
|
|
|
file.ReadBytes(&data_length, sizeof(data_length));
|
2017-07-04 08:09:31 -06:00
|
|
|
// The stored u16 is length - 1, not length.
|
|
|
|
data.resize(Common::swap16(data_length) + 1);
|
2016-06-24 02:43:46 -06:00
|
|
|
break;
|
|
|
|
}
|
2017-03-10 10:11:45 -07:00
|
|
|
case Entry::Type::SmallArray:
|
|
|
|
{
|
|
|
|
u8 data_length = 0;
|
|
|
|
file.ReadBytes(&data_length, sizeof(data_length));
|
2017-07-04 08:09:31 -06:00
|
|
|
data.resize(data_length + 1);
|
2016-06-24 02:43:46 -06:00
|
|
|
break;
|
2017-03-10 10:11:45 -07:00
|
|
|
}
|
|
|
|
case Entry::Type::Byte:
|
|
|
|
case Entry::Type::ByteBool:
|
|
|
|
case Entry::Type::Short:
|
|
|
|
case Entry::Type::Long:
|
|
|
|
case Entry::Type::LongLong:
|
|
|
|
data.resize(GetNonArrayEntrySize(type));
|
2016-10-02 03:52:57 -06:00
|
|
|
break;
|
2016-06-24 02:43:46 -06:00
|
|
|
default:
|
2017-03-10 10:11:45 -07:00
|
|
|
ERROR_LOG(CORE, "Unknown entry type %d in SYSCONF for %s (offset %u)", static_cast<u8>(type),
|
|
|
|
name.c_str(), offset);
|
2016-06-24 02:43:46 -06:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-10 10:11:45 -07:00
|
|
|
file.ReadBytes(data.data(), data.size());
|
|
|
|
AddEntry({type, name, std::move(data)});
|
|
|
|
}
|
|
|
|
return true;
|
2010-02-19 10:05:26 -07:00
|
|
|
}
|
|
|
|
|
2017-03-10 10:11:45 -07:00
|
|
|
template <typename T>
|
|
|
|
static void AppendToBuffer(std::vector<u8>* vector, T value)
|
2011-03-06 09:57:58 -07:00
|
|
|
{
|
2017-03-10 10:11:45 -07:00
|
|
|
const T swapped_value = Common::FromBigEndian(value);
|
|
|
|
vector->resize(vector->size() + sizeof(T));
|
|
|
|
std::memcpy(&*(vector->end() - sizeof(T)), &swapped_value, sizeof(T));
|
2011-03-06 09:57:58 -07:00
|
|
|
}
|
|
|
|
|
2017-03-10 10:11:45 -07:00
|
|
|
bool SysConf::Save() const
|
2011-03-06 09:57:58 -07:00
|
|
|
{
|
2017-03-10 10:11:45 -07:00
|
|
|
std::vector<u8> buffer;
|
|
|
|
buffer.reserve(SYSCONF_SIZE);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2017-03-10 10:11:45 -07:00
|
|
|
// Header
|
|
|
|
constexpr std::array<u8, 4> version{{'S', 'C', 'v', '0'}};
|
|
|
|
buffer.insert(buffer.end(), version.cbegin(), version.cend());
|
|
|
|
AppendToBuffer<u16>(&buffer, static_cast<u16>(m_entries.size()));
|
2011-03-06 09:57:58 -07:00
|
|
|
|
2017-03-10 10:11:45 -07:00
|
|
|
const size_t entries_begin_offset = buffer.size() + sizeof(u16) * (m_entries.size() + 1);
|
|
|
|
std::vector<u8> entries;
|
|
|
|
for (const auto& item : m_entries)
|
2016-06-24 02:43:46 -06:00
|
|
|
{
|
2017-03-10 10:11:45 -07:00
|
|
|
// Offset
|
|
|
|
AppendToBuffer<u16>(&buffer, static_cast<u16>(entries_begin_offset + entries.size()));
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2017-03-10 10:11:45 -07:00
|
|
|
// Entry metadata (type and name)
|
|
|
|
entries.insert(entries.end(),
|
|
|
|
(static_cast<u8>(item.type) << 5) | (static_cast<u8>(item.name.size()) - 1));
|
|
|
|
entries.insert(entries.end(), item.name.cbegin(), item.name.cend());
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2017-03-10 10:11:45 -07:00
|
|
|
// Entry data
|
2016-08-06 14:16:41 -06:00
|
|
|
switch (item.type)
|
2016-06-24 02:43:46 -06:00
|
|
|
{
|
2017-03-10 10:11:45 -07:00
|
|
|
case Entry::Type::BigArray:
|
2016-06-24 02:43:46 -06:00
|
|
|
{
|
2017-03-10 10:11:45 -07:00
|
|
|
const u16 data_size = static_cast<u16>(item.bytes.size());
|
2017-07-04 08:09:31 -06:00
|
|
|
// length - 1 is stored, not length.
|
|
|
|
AppendToBuffer<u16>(&entries, data_size - 1);
|
2017-03-10 10:11:45 -07:00
|
|
|
entries.insert(entries.end(), item.bytes.cbegin(), item.bytes.cbegin() + data_size);
|
|
|
|
break;
|
2016-06-24 02:43:46 -06:00
|
|
|
}
|
|
|
|
|
2017-03-10 10:11:45 -07:00
|
|
|
case Entry::Type::SmallArray:
|
|
|
|
{
|
|
|
|
const u8 data_size = static_cast<u8>(item.bytes.size());
|
2017-07-04 08:09:31 -06:00
|
|
|
AppendToBuffer<u8>(&entries, data_size - 1);
|
2017-03-10 10:11:45 -07:00
|
|
|
entries.insert(entries.end(), item.bytes.cbegin(), item.bytes.cbegin() + data_size);
|
2016-06-24 02:43:46 -06:00
|
|
|
break;
|
2017-03-10 10:11:45 -07:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
|
|
|
default:
|
2017-03-10 10:11:45 -07:00
|
|
|
entries.insert(entries.end(), item.bytes.cbegin(), item.bytes.cend());
|
2016-06-24 02:43:46 -06:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-03-10 10:11:45 -07:00
|
|
|
// Offset for the dummy past-the-end entry.
|
|
|
|
AppendToBuffer<u16>(&buffer, static_cast<u16>(entries_begin_offset + entries.size()));
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2017-03-10 10:11:45 -07:00
|
|
|
// Main data.
|
|
|
|
buffer.insert(buffer.end(), entries.cbegin(), entries.cend());
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2017-03-10 10:11:45 -07:00
|
|
|
// Make sure the buffer size is 0x4000 bytes now and write the footer.
|
|
|
|
buffer.resize(SYSCONF_SIZE);
|
|
|
|
constexpr std::array<u8, 4> footer = {{'S', 'C', 'e', 'd'}};
|
|
|
|
std::copy(footer.cbegin(), footer.cend(), buffer.end() - footer.size());
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2017-03-10 10:11:45 -07:00
|
|
|
// Write the new data.
|
|
|
|
const std::string temp_file = File::GetTempFilenameForAtomicWrite(m_file_name);
|
|
|
|
File::CreateFullPath(temp_file);
|
|
|
|
{
|
|
|
|
File::IOFile file(temp_file, "wb");
|
|
|
|
if (!file.WriteBytes(buffer.data(), buffer.size()))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return File::RenameSync(temp_file, m_file_name);
|
2011-03-06 09:57:58 -07:00
|
|
|
}
|
|
|
|
|
2017-03-10 10:11:45 -07:00
|
|
|
SysConf::Entry::Entry(Type type_, const std::string& name_) : type(type_), name(name_)
|
2010-02-19 10:05:26 -07:00
|
|
|
{
|
2017-03-10 10:11:45 -07:00
|
|
|
if (type != Type::SmallArray && type != Type::BigArray)
|
|
|
|
bytes.resize(GetNonArrayEntrySize(type));
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2017-03-10 10:11:45 -07:00
|
|
|
SysConf::Entry::Entry(Type type_, const std::string& name_, const std::vector<u8>& bytes_)
|
|
|
|
: type(type_), name(name_), bytes(bytes_)
|
|
|
|
{
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2017-03-10 10:11:45 -07:00
|
|
|
SysConf::Entry::Entry(Type type_, const std::string& name_, std::vector<u8>&& bytes_)
|
|
|
|
: type(type_), name(name_), bytes(std::move(bytes_))
|
|
|
|
{
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2017-03-10 10:11:45 -07:00
|
|
|
void SysConf::AddEntry(Entry&& entry)
|
|
|
|
{
|
|
|
|
m_entries.emplace_back(std::move(entry));
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2017-03-10 10:11:45 -07:00
|
|
|
SysConf::Entry* SysConf::GetEntry(const std::string& key)
|
|
|
|
{
|
|
|
|
const auto iterator = std::find_if(m_entries.begin(), m_entries.end(),
|
|
|
|
[&key](const auto& entry) { return entry.name == key; });
|
|
|
|
return iterator != m_entries.end() ? &*iterator : nullptr;
|
2010-02-19 10:05:26 -07:00
|
|
|
}
|
|
|
|
|
2017-03-10 10:11:45 -07:00
|
|
|
const SysConf::Entry* SysConf::GetEntry(const std::string& key) const
|
2010-02-19 10:05:26 -07:00
|
|
|
{
|
2017-03-10 10:11:45 -07:00
|
|
|
const auto iterator = std::find_if(m_entries.begin(), m_entries.end(),
|
|
|
|
[&key](const auto& entry) { return entry.name == key; });
|
|
|
|
return iterator != m_entries.end() ? &*iterator : nullptr;
|
|
|
|
}
|
2014-03-12 13:33:41 -06:00
|
|
|
|
2017-03-10 10:11:45 -07:00
|
|
|
SysConf::Entry* SysConf::GetOrAddEntry(const std::string& key, Entry::Type type)
|
|
|
|
{
|
|
|
|
if (Entry* entry = GetEntry(key))
|
|
|
|
return entry;
|
|
|
|
AddEntry({type, key});
|
|
|
|
return GetEntry(key);
|
2010-02-02 14:56:29 -07:00
|
|
|
}
|
2011-03-02 17:07:33 -07:00
|
|
|
|
2017-03-10 10:11:45 -07:00
|
|
|
void SysConf::RemoveEntry(const std::string& key)
|
2011-05-24 13:12:18 -06:00
|
|
|
{
|
2017-03-10 10:11:45 -07:00
|
|
|
m_entries.erase(std::remove_if(m_entries.begin(), m_entries.end(),
|
|
|
|
[&key](const auto& entry) { return entry.name == key; }),
|
|
|
|
m_entries.end());
|
2011-05-24 13:12:18 -06:00
|
|
|
}
|
|
|
|
|
2017-03-10 10:11:45 -07:00
|
|
|
// Apply Wii settings from normal SYSCONF on Movie recording/playback
|
|
|
|
void SysConf::ApplySettingsFromMovie()
|
2011-03-02 17:07:33 -07:00
|
|
|
{
|
2017-03-10 10:11:45 -07:00
|
|
|
if (!Movie::IsMovieActive())
|
|
|
|
return;
|
2011-03-02 17:07:33 -07:00
|
|
|
|
2017-03-10 10:11:45 -07:00
|
|
|
SetData<u8>("IPL.LNG", Entry::Type::Byte, Movie::GetLanguage());
|
|
|
|
SetData<u8>("IPL.E60", Entry::Type::Byte, Movie::IsPAL60());
|
|
|
|
SetData<u8>("IPL.PGS", Entry::Type::Byte, Movie::IsProgressive());
|
|
|
|
}
|
|
|
|
|
|
|
|
void SysConf::InsertDefaultEntries()
|
|
|
|
{
|
2017-07-04 08:09:31 -06:00
|
|
|
AddEntry({Entry::Type::BigArray, "BT.DINF", std::vector<u8>(0x460 + 1)});
|
|
|
|
AddEntry({Entry::Type::BigArray, "BT.CDIF", std::vector<u8>(0x204 + 1)});
|
2017-03-10 10:11:45 -07:00
|
|
|
AddEntry({Entry::Type::Long, "BT.SENS", {0, 0, 0, 3}});
|
|
|
|
AddEntry({Entry::Type::Byte, "BT.BAR", {1}});
|
|
|
|
AddEntry({Entry::Type::Byte, "BT.SPKV", {0x58}});
|
|
|
|
AddEntry({Entry::Type::Byte, "BT.MOT", {1}});
|
|
|
|
|
2017-07-04 08:09:31 -06:00
|
|
|
std::vector<u8> console_nick = {0, 'd', 0, 'o', 0, 'l', 0, 'p', 0, 'h', 0, 'i', 0, 'n'};
|
|
|
|
// 22 bytes: 2 bytes per character (10 characters maximum),
|
|
|
|
// 1 for a null terminating character, 1 for the string length
|
|
|
|
console_nick.resize(22);
|
|
|
|
console_nick[21] = static_cast<u8>(strlen("dolphin"));
|
2017-03-10 10:11:45 -07:00
|
|
|
AddEntry({Entry::Type::SmallArray, "IPL.NIK", std::move(console_nick)});
|
|
|
|
|
|
|
|
AddEntry({Entry::Type::Byte, "IPL.LNG", {1}});
|
2017-07-04 08:09:31 -06:00
|
|
|
std::vector<u8> ipl_sadr(0x1007 + 1);
|
2017-03-10 10:11:45 -07:00
|
|
|
ipl_sadr[0] = 0x6c;
|
|
|
|
AddEntry({Entry::Type::BigArray, "IPL.SADR", std::move(ipl_sadr)});
|
|
|
|
|
2017-07-04 08:09:31 -06:00
|
|
|
std::vector<u8> ipl_pc(0x49 + 1);
|
2017-03-10 10:11:45 -07:00
|
|
|
ipl_pc[1] = 0x04;
|
|
|
|
ipl_pc[2] = 0x14;
|
|
|
|
AddEntry({Entry::Type::SmallArray, "IPL.PC", std::move(ipl_pc)});
|
|
|
|
|
|
|
|
AddEntry({Entry::Type::Long, "IPL.CB", {0x0f, 0x11, 0x14, 0xa6}});
|
|
|
|
AddEntry({Entry::Type::Byte, "IPL.AR", {1}});
|
|
|
|
AddEntry({Entry::Type::Byte, "IPL.SSV", {1}});
|
|
|
|
|
2017-06-16 14:38:08 -06:00
|
|
|
AddEntry({Entry::Type::ByteBool, "IPL.CD", {0}});
|
|
|
|
AddEntry({Entry::Type::ByteBool, "IPL.CD2", {0}});
|
2017-03-10 10:11:45 -07:00
|
|
|
AddEntry({Entry::Type::ByteBool, "IPL.EULA", {1}});
|
|
|
|
AddEntry({Entry::Type::Byte, "IPL.UPT", {2}});
|
|
|
|
AddEntry({Entry::Type::Byte, "IPL.PGS", {0}});
|
|
|
|
AddEntry({Entry::Type::Byte, "IPL.E60", {1}});
|
|
|
|
AddEntry({Entry::Type::Byte, "IPL.DH", {0}});
|
|
|
|
AddEntry({Entry::Type::Long, "IPL.INC", {0, 0, 0, 8}});
|
|
|
|
AddEntry({Entry::Type::Long, "IPL.FRC", {0, 0, 0, 0x28}});
|
2017-07-04 08:09:31 -06:00
|
|
|
AddEntry({Entry::Type::SmallArray, "IPL.IDL", {0, 1}});
|
2017-03-10 10:11:45 -07:00
|
|
|
|
|
|
|
AddEntry({Entry::Type::Long, "NET.WCFG", {0, 0, 0, 1}});
|
|
|
|
AddEntry({Entry::Type::Long, "NET.CTPC", std::vector<u8>(4)});
|
|
|
|
AddEntry({Entry::Type::Byte, "WWW.RST", {0}});
|
|
|
|
|
|
|
|
AddEntry({Entry::Type::ByteBool, "MPLS.MOVIE", {1}});
|
2011-03-02 17:07:33 -07:00
|
|
|
}
|