VarList.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /* Copyright (c) 2002-2012 Croteam Ltd.
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of version 2 of the GNU General Public License as published by
  4. the Free Software Foundation
  5. This program is distributed in the hope that it will be useful,
  6. but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License along
  10. with this program; if not, write to the Free Software Foundation, Inc.,
  11. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
  12. #include "StdH.h"
  13. #include "VarList.h"
  14. CListHead _lhVarSettings;
  15. CTString _strFile;
  16. INDEX _ctLines;
  17. CTString GetNonEmptyLine_t(CTStream &strm)
  18. {
  19. FOREVER {
  20. if(strm.AtEOF()) {
  21. ThrowF_t(TRANS("Unexpected end of file"));
  22. }
  23. CTString str;
  24. _ctLines++;
  25. strm.GetLine_t(str);
  26. str.TrimSpacesLeft();
  27. if (str.RemovePrefix("//")) { // skip comments
  28. continue;
  29. }
  30. if (str!="") {
  31. str.TrimSpacesRight();
  32. return str;
  33. }
  34. }
  35. }
  36. void TranslateLine(CTString &str)
  37. {
  38. str.TrimSpacesLeft();
  39. if (str.RemovePrefix("TTRS")) {
  40. str.TrimSpacesLeft();
  41. str = TranslateConst(str, 0);
  42. }
  43. str.TrimSpacesLeft();
  44. }
  45. void FixupFileName_t(CTString &strFnm)
  46. {
  47. strFnm.TrimSpacesLeft();
  48. strFnm.TrimSpacesRight();
  49. if (!strFnm.RemovePrefix(CTString("TF") +"NM ")) { // must not directly have ids in code
  50. ThrowF_t(TRANS("Expected %s%s before filename"), "TF", "NM");
  51. }
  52. }
  53. void CheckPVS_t(CVarSetting *pvs)
  54. {
  55. if (pvs==NULL) {
  56. ThrowF_t("Gadget expected");
  57. }
  58. }
  59. void ParseCFG_t(CTStream &strm)
  60. {
  61. CVarSetting *pvs = NULL;
  62. // repeat
  63. FOREVER {
  64. // read one line
  65. CTString strLine = GetNonEmptyLine_t(strm);
  66. if (strLine.RemovePrefix("MenuEnd")) {
  67. return;
  68. } else if (strLine.RemovePrefix("Gadget:")) {
  69. pvs = new CVarSetting;
  70. _lhVarSettings.AddTail(pvs->vs_lnNode);
  71. TranslateLine(strLine);
  72. strLine.TrimSpacesLeft();
  73. pvs->vs_strName = strLine;
  74. } else if (strLine.RemovePrefix("Type:")) {
  75. CheckPVS_t(pvs);
  76. strLine.TrimSpacesLeft();
  77. strLine.TrimSpacesRight();
  78. if (strLine=="Toggle") {
  79. pvs->vs_bSeparator = FALSE;
  80. } else if (strLine=="Separator") {
  81. pvs->vs_bSeparator = TRUE;
  82. }
  83. } else if (strLine.RemovePrefix("Schedule:")) {
  84. CheckPVS_t(pvs);
  85. FixupFileName_t(strLine);
  86. pvs->vs_strSchedule = strLine;
  87. } else if (strLine.RemovePrefix("Tip:")) {
  88. CheckPVS_t(pvs);
  89. TranslateLine(strLine);
  90. strLine.TrimSpacesLeft();
  91. strLine.TrimSpacesRight();
  92. pvs->vs_strTip = strLine;
  93. } else if (strLine.RemovePrefix("Var:")) {
  94. CheckPVS_t(pvs);
  95. strLine.TrimSpacesLeft();
  96. strLine.TrimSpacesRight();
  97. pvs->vs_strVar = strLine;
  98. } else if (strLine.RemovePrefix("Filter:")) {
  99. CheckPVS_t(pvs);
  100. strLine.TrimSpacesLeft();
  101. strLine.TrimSpacesRight();
  102. pvs->vs_strFilter = strLine;
  103. } else if (strLine.RemovePrefix("Slider:")) {
  104. CheckPVS_t(pvs);
  105. strLine.TrimSpacesLeft();
  106. strLine.TrimSpacesRight();
  107. if (strLine=="Fill") {
  108. pvs->vs_iSlider = 1;
  109. } else if (strLine=="Ratio") {
  110. pvs->vs_iSlider = 2;
  111. } else {
  112. pvs->vs_iSlider = 0;
  113. }
  114. } else if (strLine.RemovePrefix("InGame:")) {
  115. CheckPVS_t(pvs);
  116. strLine.TrimSpacesLeft();
  117. strLine.TrimSpacesRight();
  118. if( strLine=="No") {
  119. pvs->vs_bCanChangeInGame = FALSE;
  120. } else {
  121. ASSERT( strLine=="Yes");
  122. pvs->vs_bCanChangeInGame = TRUE;
  123. }
  124. } else if (strLine.RemovePrefix("String:")) {
  125. CheckPVS_t(pvs);
  126. TranslateLine(strLine);
  127. strLine.TrimSpacesLeft();
  128. strLine.TrimSpacesRight();
  129. pvs->vs_astrTexts.Push() = strLine;
  130. } else if (strLine.RemovePrefix("Value:")) {
  131. CheckPVS_t(pvs);
  132. strLine.TrimSpacesLeft();
  133. strLine.TrimSpacesRight();
  134. pvs->vs_astrValues.Push() = strLine;
  135. } else {
  136. ThrowF_t(TRANS("unknown keyword"));
  137. }
  138. }
  139. }
  140. void LoadVarSettings(const CTFileName &fnmCfg)
  141. {
  142. FlushVarSettings(FALSE);
  143. try {
  144. CTFileStream strm;
  145. strm.Open_t(fnmCfg);
  146. _ctLines = 0;
  147. _strFile = fnmCfg;
  148. ParseCFG_t(strm);
  149. } catch (char* strError) {
  150. CPrintF("%s (%d) : %s\n", (const char*)_strFile, _ctLines, strError);
  151. }
  152. FOREACHINLIST(CVarSetting, vs_lnNode, _lhVarSettings, itvs) {
  153. CVarSetting &vs = *itvs;
  154. if (!vs.Validate() || vs.vs_bSeparator) {
  155. continue;
  156. }
  157. INDEX ctValues = vs.vs_ctValues;
  158. CTString strValue = _pShell->GetValue(vs.vs_strVar);
  159. vs.vs_bCustom = TRUE;
  160. vs.vs_iOrgValue = vs.vs_iValue = -1;
  161. for(INDEX iValue=0; iValue<ctValues; iValue++) {
  162. if (strValue == vs.vs_astrValues[iValue]) {
  163. vs.vs_iOrgValue = vs.vs_iValue = iValue;
  164. vs.vs_bCustom = FALSE;
  165. break;
  166. }
  167. }
  168. }
  169. }
  170. void FlushVarSettings(BOOL bApply)
  171. {
  172. CStaticStackArray<CTString> astrScheduled;
  173. if (bApply) {
  174. FOREACHINLIST(CVarSetting, vs_lnNode, _lhVarSettings, itvs) {
  175. CVarSetting &vs = *itvs;
  176. if (vs.vs_iValue!=vs.vs_iOrgValue) {
  177. CTString strCmd;
  178. _pShell->SetValue(vs.vs_strVar, vs.vs_astrValues[vs.vs_iValue]);
  179. if (vs.vs_strSchedule!="") {
  180. BOOL bSheduled = FALSE;
  181. for(INDEX i=0; i<astrScheduled.Count(); i++) {
  182. if (astrScheduled[i]==vs.vs_strSchedule) {
  183. bSheduled = TRUE;
  184. break;
  185. }
  186. }
  187. if (!bSheduled) {
  188. astrScheduled.Push() = vs.vs_strSchedule;
  189. }
  190. }
  191. }
  192. }
  193. }
  194. {FORDELETELIST(CVarSetting, vs_lnNode, _lhVarSettings, itvs) {
  195. delete &*itvs;
  196. }}
  197. for(INDEX i=0; i<astrScheduled.Count(); i++) {
  198. CTString strCmd;
  199. strCmd.PrintF("include \"%s\"", astrScheduled[i]);
  200. _pShell->Execute(strCmd);
  201. }
  202. }
  203. CVarSetting::CVarSetting()
  204. {
  205. Clear();
  206. }
  207. void CVarSetting::Clear()
  208. {
  209. vs_iOrgValue = 0;
  210. vs_iValue = 0;
  211. vs_ctValues = 0;
  212. vs_bSeparator = FALSE;
  213. vs_bCanChangeInGame = TRUE;
  214. vs_iSlider = 0;
  215. vs_strName.Clear();
  216. vs_strTip.Clear();
  217. vs_strVar.Clear();
  218. vs_strFilter.Clear();
  219. vs_strSchedule.Clear();
  220. vs_bCustom = FALSE;
  221. }
  222. BOOL CVarSetting::Validate(void)
  223. {
  224. if (vs_bSeparator) {
  225. return TRUE;
  226. }
  227. vs_ctValues = Min(vs_astrValues.Count(), vs_astrTexts.Count());
  228. if (vs_ctValues<=0) {
  229. ASSERT(FALSE);
  230. return FALSE;
  231. }
  232. if (!vs_bCustom) {
  233. vs_iValue = Clamp(vs_iValue, 0L, vs_ctValues-1L);
  234. }
  235. return TRUE;
  236. }