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
|
// Copyright 2009 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.
#pragma once
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include "Common/CommonTypes.h"
#include "Common/MsgHandler.h"
#include "Common/Logging/Log.h"
// This class is meant to edit the values in a given Wii SYSCONF file
// It currently does not add/remove/rearrange sections,
// instead only modifies exiting sections' data
#define SYSCONF_SIZE 0x4000
enum SysconfType
{
Type_BigArray = 1,
Type_SmallArray,
Type_Byte,
Type_Short,
Type_Long,
Type_Unknown,
Type_Bool
};
struct SSysConfHeader
{
char version[4];
u16 numEntries;
};
struct SSysConfEntry
{
u16 offset;
SysconfType type;
u8 nameLength;
char name[32];
u16 dataLength;
u8* data;
template<class T>
T GetData() { return *(T*)data; }
bool GetArrayData(u8* dest, u16 destSize)
{
if (dest && destSize >= dataLength)
{
memcpy(dest, data, dataLength);
return true;
}
return false;
}
bool SetArrayData(u8* buffer, u16 bufferSize)
{
if (buffer)
{
memcpy(data, buffer, std::min<u16>(bufferSize, dataLength));
return true;
}
return false;
}
};
class SysConf
{
public:
SysConf();
~SysConf();
bool IsValid() { return m_IsValid; }
template<class T>
T GetData(const char* sectionName)
{
if (!m_IsValid)
{
PanicAlertT("Trying to read from invalid SYSCONF");
return 0;
}
std::vector<SSysConfEntry>::iterator index = m_Entries.begin();
for (; index < m_Entries.end() - 1; ++index)
{
if (strcmp(index->name, sectionName) == 0)
break;
}
if (index == m_Entries.end() - 1)
{
PanicAlertT("Section %s not found in SYSCONF", sectionName);
return 0;
}
return index->GetData<T>();
}
bool GetArrayData(const char* sectionName, u8* dest, u16 destSize)
{
if (!m_IsValid)
{
PanicAlertT("Trying to read from invalid SYSCONF");
return 0;
}
std::vector<SSysConfEntry>::iterator index = m_Entries.begin();
for (; index < m_Entries.end() - 1; ++index)
{
if (strcmp(index->name, sectionName) == 0)
break;
}
if (index == m_Entries.end() - 1)
{
PanicAlertT("Section %s not found in SYSCONF", sectionName);
return 0;
}
return index->GetArrayData(dest, destSize);
}
bool SetArrayData(const char* sectionName, u8* buffer, u16 bufferSize)
{
if (!m_IsValid)
return false;
std::vector<SSysConfEntry>::iterator index = m_Entries.begin();
for (; index < m_Entries.end() - 1; ++index)
{
if (strcmp(index->name, sectionName) == 0)
break;
}
if (index == m_Entries.end() - 1)
{
PanicAlertT("Section %s not found in SYSCONF", sectionName);
return false;
}
return index->SetArrayData(buffer, bufferSize);
}
template<class T>
bool SetData(const char* sectionName, T newValue)
{
if (!m_IsValid)
return false;
std::vector<SSysConfEntry>::iterator index = m_Entries.begin();
for (; index < m_Entries.end() - 1; ++index)
{
if (strcmp(index->name, sectionName) == 0)
break;
}
if (index == m_Entries.end() - 1)
{
PanicAlertT("Section %s not found in SYSCONF", sectionName);
return false;
}
*(T*)index->data = newValue;
return true;
}
bool Save();
bool SaveToFile(const std::string& filename);
bool LoadFromFile(const std::string& filename);
bool Reload();
// This function is used when the NAND root is changed
void UpdateLocation();
private:
bool LoadFromFileInternal(FILE* fh);
void GenerateSysConf();
void Clear();
std::string m_Filename;
std::string m_FilenameDefault;
std::vector<SSysConfEntry> m_Entries;
bool m_IsValid;
};
|