| 
									
										
										
										
											2015-05-24 06:55:12 +02:00
										 |  |  | // Copyright 2009 Dolphin Emulator Project
 | 
					
						
							| 
									
										
										
										
											2015-05-18 01:08:10 +02:00
										 |  |  | // Licensed under GPLv2+
 | 
					
						
							| 
									
										
										
										
											2013-04-17 23:09:55 -04:00
										 |  |  | // Refer to the license.txt file included.
 | 
					
						
							| 
									
										
										
										
											2010-02-19 17:05:26 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-10 13:54:46 -05:00
										 |  |  | #pragma once
 | 
					
						
							| 
									
										
										
										
											2010-02-19 17:05:26 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-02 22:47:04 -04:00
										 |  |  | #include <algorithm>
 | 
					
						
							| 
									
										
										
										
											2014-02-20 04:11:52 +01:00
										 |  |  | #include <cstdio>
 | 
					
						
							|  |  |  | #include <cstring>
 | 
					
						
							| 
									
										
										
										
											2010-02-19 17:05:26 +00:00
										 |  |  | #include <string>
 | 
					
						
							|  |  |  | #include <vector>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-17 05:18:15 -05:00
										 |  |  | #include "Common/Common.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-19 17:05:26 +00:00
										 |  |  | // 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; } | 
					
						
							| 
									
										
										
										
											2010-12-22 08:00:08 +00:00
										 |  |  | 	bool GetArrayData(u8* dest, u16 destSize) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		if (dest && destSize >= dataLength) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			memcpy(dest, data, dataLength); | 
					
						
							|  |  |  | 			return true; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	bool SetArrayData(u8* buffer, u16 bufferSize) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2011-08-21 21:10:58 -07:00
										 |  |  | 		if (buffer) | 
					
						
							| 
									
										
										
										
											2010-12-22 08:00:08 +00:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2014-05-02 22:47:04 -04:00
										 |  |  | 			memcpy(data, buffer, std::min<u16>(bufferSize, dataLength)); | 
					
						
							| 
									
										
										
										
											2010-12-22 08:00:08 +00:00
										 |  |  | 			return true; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2010-02-19 17:05:26 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class SysConf | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  | 	SysConf(); | 
					
						
							|  |  |  | 	~SysConf(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	bool IsValid() { return m_IsValid; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	template<class T> | 
					
						
							|  |  |  | 	T GetData(const char* sectionName) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		if (!m_IsValid) | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2011-01-13 02:05:58 +00:00
										 |  |  | 			PanicAlertT("Trying to read from invalid SYSCONF"); | 
					
						
							| 
									
										
										
										
											2010-02-19 17:05:26 +00:00
										 |  |  | 			return 0; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-03 22:47:48 +00:00
										 |  |  | 		std::vector<SSysConfEntry>::iterator index = m_Entries.begin(); | 
					
						
							| 
									
										
										
										
											2014-02-12 16:00:34 +01:00
										 |  |  | 		for (; index < m_Entries.end() - 1; ++index) | 
					
						
							| 
									
										
										
										
											2010-02-19 17:05:26 +00:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2011-03-03 22:47:48 +00:00
										 |  |  | 			if (strcmp(index->name, sectionName) == 0) | 
					
						
							| 
									
										
										
										
											2010-02-19 17:05:26 +00:00
										 |  |  | 				break; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2011-03-03 22:47:48 +00:00
										 |  |  | 		if (index == m_Entries.end() - 1) | 
					
						
							| 
									
										
										
										
											2010-02-19 17:05:26 +00:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2011-01-13 02:05:58 +00:00
										 |  |  | 			PanicAlertT("Section %s not found in SYSCONF", sectionName); | 
					
						
							| 
									
										
										
										
											2010-02-19 17:05:26 +00:00
										 |  |  | 			return 0; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-03 22:47:48 +00:00
										 |  |  | 		return index->GetData<T>(); | 
					
						
							| 
									
										
										
										
											2010-02-19 17:05:26 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-12-22 08:00:08 +00:00
										 |  |  | 	bool GetArrayData(const char* sectionName, u8* dest, u16 destSize) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		if (!m_IsValid) | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2011-01-13 02:05:58 +00:00
										 |  |  | 			PanicAlertT("Trying to read from invalid SYSCONF"); | 
					
						
							| 
									
										
										
										
											2010-12-22 08:00:08 +00:00
										 |  |  | 			return 0; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-03 22:47:48 +00:00
										 |  |  | 		std::vector<SSysConfEntry>::iterator index = m_Entries.begin(); | 
					
						
							| 
									
										
										
										
											2014-02-12 16:00:34 +01:00
										 |  |  | 		for (; index < m_Entries.end() - 1; ++index) | 
					
						
							| 
									
										
										
										
											2010-12-22 08:00:08 +00:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2011-03-03 22:47:48 +00:00
										 |  |  | 			if (strcmp(index->name, sectionName) == 0) | 
					
						
							| 
									
										
										
										
											2010-12-22 08:00:08 +00:00
										 |  |  | 				break; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2011-03-03 22:47:48 +00:00
										 |  |  | 		if (index == m_Entries.end() - 1) | 
					
						
							| 
									
										
										
										
											2010-12-22 08:00:08 +00:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2011-01-13 02:05:58 +00:00
										 |  |  | 			PanicAlertT("Section %s not found in SYSCONF", sectionName); | 
					
						
							| 
									
										
										
										
											2010-12-22 08:00:08 +00:00
										 |  |  | 			return 0; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-03 22:47:48 +00:00
										 |  |  | 		return index->GetArrayData(dest, destSize); | 
					
						
							| 
									
										
										
										
											2010-12-22 08:00:08 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	bool SetArrayData(const char* sectionName, u8* buffer, u16 bufferSize) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		if (!m_IsValid) | 
					
						
							|  |  |  | 			return false; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-03 22:47:48 +00:00
										 |  |  | 		std::vector<SSysConfEntry>::iterator index = m_Entries.begin(); | 
					
						
							| 
									
										
										
										
											2014-02-12 16:00:34 +01:00
										 |  |  | 		for (; index < m_Entries.end() - 1; ++index) | 
					
						
							| 
									
										
										
										
											2010-12-22 08:00:08 +00:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2011-03-03 22:47:48 +00:00
										 |  |  | 			if (strcmp(index->name, sectionName) == 0) | 
					
						
							| 
									
										
										
										
											2010-12-22 08:00:08 +00:00
										 |  |  | 				break; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2011-03-03 22:47:48 +00:00
										 |  |  | 		if (index == m_Entries.end() - 1) | 
					
						
							| 
									
										
										
										
											2010-12-22 08:00:08 +00:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2011-01-13 02:05:58 +00:00
										 |  |  | 			PanicAlertT("Section %s not found in SYSCONF", sectionName); | 
					
						
							| 
									
										
										
										
											2010-12-22 08:00:08 +00:00
										 |  |  | 			return false; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-03 22:47:48 +00:00
										 |  |  | 		return index->SetArrayData(buffer, bufferSize); | 
					
						
							| 
									
										
										
										
											2010-12-22 08:00:08 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-19 17:05:26 +00:00
										 |  |  | 	template<class T> | 
					
						
							|  |  |  | 	bool SetData(const char* sectionName, T newValue) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		if (!m_IsValid) | 
					
						
							|  |  |  | 			return false; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-03 22:47:48 +00:00
										 |  |  | 		std::vector<SSysConfEntry>::iterator index = m_Entries.begin(); | 
					
						
							| 
									
										
										
										
											2014-02-12 16:00:34 +01:00
										 |  |  | 		for (; index < m_Entries.end() - 1; ++index) | 
					
						
							| 
									
										
										
										
											2010-02-19 17:05:26 +00:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2011-03-03 22:47:48 +00:00
										 |  |  | 			if (strcmp(index->name, sectionName) == 0) | 
					
						
							| 
									
										
										
										
											2010-02-19 17:05:26 +00:00
										 |  |  | 				break; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2011-03-03 22:47:48 +00:00
										 |  |  | 		if (index == m_Entries.end() - 1) | 
					
						
							| 
									
										
										
										
											2010-02-19 17:05:26 +00:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2011-01-13 02:05:58 +00:00
										 |  |  | 			PanicAlertT("Section %s not found in SYSCONF", sectionName); | 
					
						
							| 
									
										
										
										
											2010-02-19 17:05:26 +00:00
										 |  |  | 			return false; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-03 22:47:48 +00:00
										 |  |  | 		*(T*)index->data = newValue; | 
					
						
							| 
									
										
										
										
											2010-02-19 17:05:26 +00:00
										 |  |  | 		return true; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	bool Save(); | 
					
						
							| 
									
										
										
										
											2014-03-12 15:33:41 -04:00
										 |  |  | 	bool SaveToFile(const std::string& filename); | 
					
						
							|  |  |  | 	bool LoadFromFile(const std::string& filename); | 
					
						
							| 
									
										
										
										
											2011-03-03 00:07:33 +00:00
										 |  |  | 	bool Reload(); | 
					
						
							| 
									
										
										
										
											2011-05-24 19:12:18 +00:00
										 |  |  | 	// This function is used when the NAND root is changed
 | 
					
						
							|  |  |  | 	void UpdateLocation(); | 
					
						
							| 
									
										
										
										
											2011-03-03 00:07:33 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-19 17:05:26 +00:00
										 |  |  | private: | 
					
						
							| 
									
										
										
										
											2011-03-11 12:56:26 +00:00
										 |  |  | 	bool LoadFromFileInternal(FILE *fh); | 
					
						
							| 
									
										
										
										
											2011-03-06 16:57:58 +00:00
										 |  |  | 	void GenerateSysConf(); | 
					
						
							| 
									
										
										
										
											2011-03-03 00:07:33 +00:00
										 |  |  | 	void Clear(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	std::string m_Filename; | 
					
						
							|  |  |  | 	std::string m_FilenameDefault; | 
					
						
							|  |  |  | 	std::vector<SSysConfEntry> m_Entries; | 
					
						
							|  |  |  | 	bool m_IsValid; | 
					
						
							| 
									
										
										
										
											2010-02-19 17:05:26 +00:00
										 |  |  | }; |