ALERROR CGameSettings::Load (const CString &sFilespec, CString *retsError) // Load // // Load game settings from a file. If the file does not exist, then we // set settings to default values { ALERROR error; int i; // Initialize from defaults for (i = 0; i < OPTIONS_COUNT; i++) SetValue(i, CString(g_OptionData[i].pszDefaultValue, -1, true), true); // Load XML CFileReadBlock DataFile(sFilespec); CXMLElement *pData; CString sError; if (error = CXMLElement::ParseXML(&DataFile, &pData, retsError)) { // ERR_NOTFOUND means that we couldn't find the Settings.xml // file. In that case, initialize from defaults if (error == ERR_NOTFOUND) { LoadFromRegistry(); m_bModified = true; return NOERROR; } // Otherwise, it means that we got an error parsing the file. // Return the error, but leave the settings initialized to defaults // (We should be OK to continue, even with an error). else { m_bModified = false; return error; } } // Initialize to unmodified (as we load settings we might change this) m_bModified = false; // Loop over all elements for (i = 0; i < pData->GetContentElementCount(); i++) { CXMLElement *pItem = pData->GetContentElement(i); if (strEquals(pItem->GetTag(), OPTION_TAG)) { int iOption = FindOptionData(pItem->GetAttribute(NAME_ATTRIB)); if (iOption == -1) { kernelDebugLogMessage("Unknown option: %s", pItem->GetAttribute(NAME_ATTRIB).GetASCIIZPointer()); continue; } SetValue(iOption, pItem->GetAttribute(VALUE_ATTRIB), true); } else if (strEquals(pItem->GetTag(), KEY_MAP_TAG)) { if (error = m_KeyMap.ReadFromXML(pItem)) return error; } else if (m_pExtra) { bool bModified; if (error = m_pExtra->OnLoadSettings(pItem, &bModified)) return error; if (bModified) m_bModified = true; } } // Done delete pData; return NOERROR; }
ALERROR CGameSettings::Load (const CString &sFilespec, CString *retsError) // Load // // Load game settings from a file. If the file does not exist, then we // set settings to default values { ALERROR error; int i; // Initialize from defaults for (i = 0; i < OPTIONS_COUNT; i++) SetValue(i, CString(g_OptionData[i].pszDefaultValue, -1, true), true); // Look for a file in the current directory and see if it is writable. If // not, then look in AppData. We remember the place where we found a valid // file as our AppData root (and we base other directories off that). if (pathIsWritable(sFilespec)) { // AppData is current directory m_sAppData = NULL_STR; } else { m_sAppData = pathAddComponent(pathGetSpecialFolder(folderAppData), TRANSCENDENCE_APP_DATA); if (!pathCreate(m_sAppData) || !pathIsWritable(m_sAppData)) { *retsError = strPatternSubst(CONSTLIT("Unable to write to AppData folder: %s"), m_sAppData); return ERR_FAIL; } } // Settings file CString sSettingsFilespec = pathAddComponent(m_sAppData, sFilespec); // Load XML CFileReadBlock DataFile(sSettingsFilespec); CXMLElement *pData; CString sError; if (error = CXMLElement::ParseXML(&DataFile, &pData, retsError)) { // ERR_NOTFOUND means that we couldn't find the Settings.xml // file. In that case, initialize from defaults if (error == ERR_NOTFOUND) { LoadFromRegistry(); m_bModified = true; return NOERROR; } // Otherwise, it means that we got an error parsing the file. // Return the error, but leave the settings initialized to defaults // (We should be OK to continue, even with an error). else { m_bModified = false; return error; } } // Initialize to unmodified (as we load settings we might change this) m_bModified = false; // Loop over all elements for (i = 0; i < pData->GetContentElementCount(); i++) { CXMLElement *pItem = pData->GetContentElement(i); if (strEquals(pItem->GetTag(), OPTION_TAG)) { int iOption = FindOptionData(pItem->GetAttribute(NAME_ATTRIB)); if (iOption == -1) { kernelDebugLogMessage("Unknown option: %s", pItem->GetAttribute(NAME_ATTRIB)); continue; } SetValue(iOption, pItem->GetAttribute(VALUE_ATTRIB), true); } else if (strEquals(pItem->GetTag(), KEY_MAP_TAG)) { if (error = m_KeyMap.ReadFromXML(pItem)) return error; } else if (strEquals(pItem->GetTag(), EXTENSION_FOLDER_TAG)) { CString sFolder; if (pItem->FindAttribute(PATH_ATTRIB, &sFolder)) m_ExtensionFolders.Insert(sFolder); } else if (strEquals(pItem->GetTag(), EXTENSIONS_TAG)) { if (error = m_Extensions.ReadFromXML(pItem)) return error; } else if (m_pExtra) { bool bModified; if (error = m_pExtra->OnLoadSettings(pItem, &bModified)) return error; if (bModified) m_bModified = true; } } // Done delete pData; return NOERROR; }