123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 |
- /* Copyright (c) 2002-2012 Croteam Ltd.
- This program is free software; you can redistribute it and/or modify
- it under the terms of version 2 of the GNU General Public License as published by
- the Free Software Foundation
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License along
- with this program; if not, write to the Free Software Foundation, Inc.,
- 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
- #include "StdH.h"
- #include "VarList.h"
- CListHead _lhVarSettings;
- CTString _strFile;
- INDEX _ctLines;
- CTString GetNonEmptyLine_t(CTStream &strm)
- {
- FOREVER {
- if(strm.AtEOF()) {
- ThrowF_t(TRANS("Unexpected end of file"));
- }
- CTString str;
- _ctLines++;
- strm.GetLine_t(str);
- str.TrimSpacesLeft();
- if (str.RemovePrefix("//")) { // skip comments
- continue;
- }
- if (str!="") {
- str.TrimSpacesRight();
- return str;
- }
- }
- }
- void TranslateLine(CTString &str)
- {
- str.TrimSpacesLeft();
- if (str.RemovePrefix("TTRS")) {
- str.TrimSpacesLeft();
- str = TranslateConst(str, 0);
- }
- str.TrimSpacesLeft();
- }
- void FixupFileName_t(CTString &strFnm)
- {
- strFnm.TrimSpacesLeft();
- strFnm.TrimSpacesRight();
- if (!strFnm.RemovePrefix(CTString("TF") +"NM ")) { // must not directly have ids in code
- ThrowF_t(TRANS("Expected %s%s before filename"), "TF", "NM");
- }
- }
- void CheckPVS_t(CVarSetting *pvs)
- {
- if (pvs==NULL) {
- ThrowF_t("Gadget expected");
- }
- }
- void ParseCFG_t(CTStream &strm)
- {
- CVarSetting *pvs = NULL;
- // repeat
- FOREVER {
- // read one line
- CTString strLine = GetNonEmptyLine_t(strm);
- if (strLine.RemovePrefix("MenuEnd")) {
- return;
- } else if (strLine.RemovePrefix("Gadget:")) {
- pvs = new CVarSetting;
- _lhVarSettings.AddTail(pvs->vs_lnNode);
- TranslateLine(strLine);
- strLine.TrimSpacesLeft();
- pvs->vs_strName = strLine;
- } else if (strLine.RemovePrefix("Type:")) {
- CheckPVS_t(pvs);
- strLine.TrimSpacesLeft();
- strLine.TrimSpacesRight();
- if (strLine=="Toggle") {
- pvs->vs_bSeparator = FALSE;
- } else if (strLine=="Separator") {
- pvs->vs_bSeparator = TRUE;
- }
- } else if (strLine.RemovePrefix("Schedule:")) {
- CheckPVS_t(pvs);
- FixupFileName_t(strLine);
- pvs->vs_strSchedule = strLine;
- } else if (strLine.RemovePrefix("Tip:")) {
- CheckPVS_t(pvs);
- TranslateLine(strLine);
- strLine.TrimSpacesLeft();
- strLine.TrimSpacesRight();
- pvs->vs_strTip = strLine;
- } else if (strLine.RemovePrefix("Var:")) {
- CheckPVS_t(pvs);
- strLine.TrimSpacesLeft();
- strLine.TrimSpacesRight();
- pvs->vs_strVar = strLine;
- } else if (strLine.RemovePrefix("Filter:")) {
- CheckPVS_t(pvs);
- strLine.TrimSpacesLeft();
- strLine.TrimSpacesRight();
- pvs->vs_strFilter = strLine;
- } else if (strLine.RemovePrefix("Slider:")) {
- CheckPVS_t(pvs);
- strLine.TrimSpacesLeft();
- strLine.TrimSpacesRight();
- if (strLine=="Fill") {
- pvs->vs_iSlider = 1;
- } else if (strLine=="Ratio") {
- pvs->vs_iSlider = 2;
- } else {
- pvs->vs_iSlider = 0;
- }
- } else if (strLine.RemovePrefix("InGame:")) {
- CheckPVS_t(pvs);
- strLine.TrimSpacesLeft();
- strLine.TrimSpacesRight();
- if( strLine=="No") {
- pvs->vs_bCanChangeInGame = FALSE;
- } else {
- ASSERT( strLine=="Yes");
- pvs->vs_bCanChangeInGame = TRUE;
- }
- } else if (strLine.RemovePrefix("String:")) {
- CheckPVS_t(pvs);
- TranslateLine(strLine);
- strLine.TrimSpacesLeft();
- strLine.TrimSpacesRight();
- pvs->vs_astrTexts.Push() = strLine;
- } else if (strLine.RemovePrefix("Value:")) {
- CheckPVS_t(pvs);
- strLine.TrimSpacesLeft();
- strLine.TrimSpacesRight();
- pvs->vs_astrValues.Push() = strLine;
- } else {
- ThrowF_t(TRANS("unknown keyword"));
- }
- }
- }
- void LoadVarSettings(const CTFileName &fnmCfg)
- {
- FlushVarSettings(FALSE);
- try {
- CTFileStream strm;
- strm.Open_t(fnmCfg);
- _ctLines = 0;
- _strFile = fnmCfg;
- ParseCFG_t(strm);
- } catch (char* strError) {
- CPrintF("%s (%d) : %s\n", (const char*)_strFile, _ctLines, strError);
- }
- FOREACHINLIST(CVarSetting, vs_lnNode, _lhVarSettings, itvs) {
- CVarSetting &vs = *itvs;
- if (!vs.Validate() || vs.vs_bSeparator) {
- continue;
- }
- INDEX ctValues = vs.vs_ctValues;
- CTString strValue = _pShell->GetValue(vs.vs_strVar);
- vs.vs_bCustom = TRUE;
- vs.vs_iOrgValue = vs.vs_iValue = -1;
- for(INDEX iValue=0; iValue<ctValues; iValue++) {
- if (strValue == vs.vs_astrValues[iValue]) {
- vs.vs_iOrgValue = vs.vs_iValue = iValue;
- vs.vs_bCustom = FALSE;
- break;
- }
- }
- }
- }
- void FlushVarSettings(BOOL bApply)
- {
- CStaticStackArray<CTString> astrScheduled;
- if (bApply) {
- FOREACHINLIST(CVarSetting, vs_lnNode, _lhVarSettings, itvs) {
- CVarSetting &vs = *itvs;
- if (vs.vs_iValue!=vs.vs_iOrgValue) {
- CTString strCmd;
- _pShell->SetValue(vs.vs_strVar, vs.vs_astrValues[vs.vs_iValue]);
- if (vs.vs_strSchedule!="") {
- BOOL bSheduled = FALSE;
- for(INDEX i=0; i<astrScheduled.Count(); i++) {
- if (astrScheduled[i]==vs.vs_strSchedule) {
- bSheduled = TRUE;
- break;
- }
- }
- if (!bSheduled) {
- astrScheduled.Push() = vs.vs_strSchedule;
- }
- }
- }
- }
- }
- {FORDELETELIST(CVarSetting, vs_lnNode, _lhVarSettings, itvs) {
- delete &*itvs;
- }}
- for(INDEX i=0; i<astrScheduled.Count(); i++) {
- CTString strCmd;
- strCmd.PrintF("include \"%s\"", astrScheduled[i]);
- _pShell->Execute(strCmd);
- }
- }
- CVarSetting::CVarSetting()
- {
- Clear();
- }
- void CVarSetting::Clear()
- {
- vs_iOrgValue = 0;
- vs_iValue = 0;
- vs_ctValues = 0;
- vs_bSeparator = FALSE;
- vs_bCanChangeInGame = TRUE;
- vs_iSlider = 0;
- vs_strName.Clear();
- vs_strTip.Clear();
- vs_strVar.Clear();
- vs_strFilter.Clear();
- vs_strSchedule.Clear();
- vs_bCustom = FALSE;
- }
- BOOL CVarSetting::Validate(void)
- {
- if (vs_bSeparator) {
- return TRUE;
- }
- vs_ctValues = Min(vs_astrValues.Count(), vs_astrTexts.Count());
- if (vs_ctValues<=0) {
- ASSERT(FALSE);
- return FALSE;
- }
- if (!vs_bCustom) {
- vs_iValue = Clamp(vs_iValue, 0L, vs_ctValues-1L);
- }
- return TRUE;
- }
|