PageRenderTime 43ms CodeModel.GetById 5ms RepoModel.GetById 0ms app.codeStats 0ms

/engines/wintermute/base/scriptables/script_ext_date.cpp

http://github.com/scummvm/scummvm
C++ | 310 lines | 190 code | 30 blank | 90 comment | 77 complexity | 85f51189b91e5142ba60ba2adc1db0ae MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1, GPL-2.0
  1. /* ScummVM - Graphic Adventure Engine
  2. *
  3. * ScummVM is the legal property of its developers, whose names
  4. * are too numerous to list here. Please refer to the COPYRIGHT
  5. * file distributed with this source distribution.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  20. *
  21. */
  22. /*
  23. * This file is based on WME Lite.
  24. * http://dead-code.org/redir.php?target=wmelite
  25. * Copyright (c) 2011 Jan Nedoma
  26. */
  27. #include "engines/wintermute/base/scriptables/script_stack.h"
  28. #include "engines/wintermute/base/scriptables/script_value.h"
  29. #include "engines/wintermute/base/scriptables/script_ext_date.h"
  30. namespace Wintermute {
  31. IMPLEMENT_PERSISTENT(SXDate, false)
  32. BaseScriptable *makeSXDate(BaseGame *inGame, ScStack *stack) {
  33. return new SXDate(inGame, stack);
  34. }
  35. //////////////////////////////////////////////////////////////////////////
  36. SXDate::SXDate(BaseGame *inGame, ScStack *stack) : BaseScriptable(inGame) {
  37. stack->correctParams(6);
  38. memset(&_tm, 0, sizeof(_tm));
  39. ScValue *valYear = stack->pop();
  40. _tm.tm_year = valYear->getInt() - 1900;
  41. _tm.tm_mon = stack->pop()->getInt() - 1;
  42. _tm.tm_mday = stack->pop()->getInt();
  43. _tm.tm_hour = stack->pop()->getInt();
  44. _tm.tm_min = stack->pop()->getInt();
  45. _tm.tm_sec = stack->pop()->getInt();
  46. if (valYear->isNULL()) {
  47. g_system->getTimeAndDate(_tm);
  48. }
  49. }
  50. //////////////////////////////////////////////////////////////////////////
  51. SXDate::~SXDate() {
  52. }
  53. //////////////////////////////////////////////////////////////////////////
  54. const char *SXDate::scToString() {
  55. // TODO: Make this more stringy, and less ISO 8601-like
  56. _strRep.format("%04d-%02d-%02d - %02d:%02d:%02d", _tm.tm_year, _tm.tm_mon, _tm.tm_mday, _tm.tm_hour, _tm.tm_min, _tm.tm_sec);
  57. return _strRep.c_str();
  58. //return asctime(&_tm);
  59. }
  60. //////////////////////////////////////////////////////////////////////////
  61. bool SXDate::scCallMethod(ScScript *script, ScStack *stack, ScStack *thisStack, const char *name) {
  62. //////////////////////////////////////////////////////////////////////////
  63. // GetYear
  64. //////////////////////////////////////////////////////////////////////////
  65. if (strcmp(name, "GetYear") == 0) {
  66. stack->correctParams(0);
  67. stack->pushInt(_tm.tm_year + 1900);
  68. return STATUS_OK;
  69. }
  70. //////////////////////////////////////////////////////////////////////////
  71. // GetMonth
  72. //////////////////////////////////////////////////////////////////////////
  73. else if (strcmp(name, "GetMonth") == 0) {
  74. stack->correctParams(0);
  75. stack->pushInt(_tm.tm_mon + 1);
  76. return STATUS_OK;
  77. }
  78. //////////////////////////////////////////////////////////////////////////
  79. // GetDate
  80. //////////////////////////////////////////////////////////////////////////
  81. else if (strcmp(name, "GetDate") == 0) {
  82. stack->correctParams(0);
  83. stack->pushInt(_tm.tm_mday);
  84. return STATUS_OK;
  85. }
  86. //////////////////////////////////////////////////////////////////////////
  87. // GetHours
  88. //////////////////////////////////////////////////////////////////////////
  89. else if (strcmp(name, "GetHours") == 0) {
  90. stack->correctParams(0);
  91. stack->pushInt(_tm.tm_hour);
  92. return STATUS_OK;
  93. }
  94. //////////////////////////////////////////////////////////////////////////
  95. // GetMinutes
  96. //////////////////////////////////////////////////////////////////////////
  97. else if (strcmp(name, "GetMinutes") == 0) {
  98. stack->correctParams(0);
  99. stack->pushInt(_tm.tm_min);
  100. return STATUS_OK;
  101. }
  102. //////////////////////////////////////////////////////////////////////////
  103. // GetSeconds
  104. //////////////////////////////////////////////////////////////////////////
  105. else if (strcmp(name, "GetSeconds") == 0) {
  106. stack->correctParams(0);
  107. stack->pushInt(_tm.tm_sec);
  108. return STATUS_OK;
  109. }
  110. //////////////////////////////////////////////////////////////////////////
  111. // GetWeekday
  112. //////////////////////////////////////////////////////////////////////////
  113. else if (strcmp(name, "GetWeekday") == 0) {
  114. stack->correctParams(0);
  115. stack->pushInt(_tm.tm_wday);
  116. return STATUS_OK;
  117. }
  118. //////////////////////////////////////////////////////////////////////////
  119. // SetYear
  120. //////////////////////////////////////////////////////////////////////////
  121. else if (strcmp(name, "SetYear") == 0) {
  122. stack->correctParams(1);
  123. _tm.tm_year = stack->pop()->getInt() - 1900;
  124. stack->pushNULL();
  125. return STATUS_OK;
  126. }
  127. //////////////////////////////////////////////////////////////////////////
  128. // SetMonth
  129. //////////////////////////////////////////////////////////////////////////
  130. else if (strcmp(name, "SetMonth") == 0) {
  131. stack->correctParams(1);
  132. _tm.tm_mon = stack->pop()->getInt() - 1;
  133. stack->pushNULL();
  134. return STATUS_OK;
  135. }
  136. //////////////////////////////////////////////////////////////////////////
  137. // SetDate
  138. //////////////////////////////////////////////////////////////////////////
  139. else if (strcmp(name, "SetDate") == 0) {
  140. stack->correctParams(1);
  141. _tm.tm_mday = stack->pop()->getInt();
  142. stack->pushNULL();
  143. return STATUS_OK;
  144. }
  145. //////////////////////////////////////////////////////////////////////////
  146. // SetHours
  147. //////////////////////////////////////////////////////////////////////////
  148. else if (strcmp(name, "SetHours") == 0) {
  149. stack->correctParams(1);
  150. _tm.tm_hour = stack->pop()->getInt();
  151. stack->pushNULL();
  152. return STATUS_OK;
  153. }
  154. //////////////////////////////////////////////////////////////////////////
  155. // SetMinutes
  156. //////////////////////////////////////////////////////////////////////////
  157. else if (strcmp(name, "SetMinutes") == 0) {
  158. stack->correctParams(1);
  159. _tm.tm_min = stack->pop()->getInt();
  160. stack->pushNULL();
  161. return STATUS_OK;
  162. }
  163. //////////////////////////////////////////////////////////////////////////
  164. // SetSeconds
  165. //////////////////////////////////////////////////////////////////////////
  166. else if (strcmp(name, "SetSeconds") == 0) {
  167. stack->correctParams(1);
  168. _tm.tm_sec = stack->pop()->getInt();
  169. stack->pushNULL();
  170. return STATUS_OK;
  171. }
  172. //////////////////////////////////////////////////////////////////////////
  173. // SetCurrentTime
  174. //////////////////////////////////////////////////////////////////////////
  175. else if (strcmp(name, "SetCurrentTime") == 0) {
  176. stack->correctParams(0);
  177. g_system->getTimeAndDate(_tm);
  178. stack->pushNULL();
  179. return STATUS_OK;
  180. } else {
  181. return STATUS_FAILED;
  182. }
  183. }
  184. //////////////////////////////////////////////////////////////////////////
  185. ScValue *SXDate::scGetProperty(const Common::String &name) {
  186. _scValue->setNULL();
  187. //////////////////////////////////////////////////////////////////////////
  188. // Type
  189. //////////////////////////////////////////////////////////////////////////
  190. if (name == "Type") {
  191. _scValue->setString("date");
  192. return _scValue;
  193. } else {
  194. return _scValue;
  195. }
  196. }
  197. //////////////////////////////////////////////////////////////////////////
  198. bool SXDate::scSetProperty(const char *name, ScValue *value) {
  199. /*
  200. //////////////////////////////////////////////////////////////////////////
  201. // Name
  202. //////////////////////////////////////////////////////////////////////////
  203. if (name == "Name")==0) {
  204. setName(value->getString());
  205. return STATUS_OK;
  206. }
  207. else*/ return STATUS_FAILED;
  208. }
  209. //////////////////////////////////////////////////////////////////////////
  210. bool SXDate::persist(BasePersistenceManager *persistMgr) {
  211. BaseScriptable::persist(persistMgr);
  212. int32 year = _tm.tm_year;
  213. int32 mon = _tm.tm_mon;
  214. int32 mday = _tm.tm_mday;
  215. int32 hour = _tm.tm_hour;
  216. int32 min = _tm.tm_min;
  217. int32 sec = _tm.tm_sec;
  218. persistMgr->transferSint32(TMEMBER(year));
  219. persistMgr->transferSint32(TMEMBER(mon));
  220. persistMgr->transferSint32(TMEMBER(mday));
  221. persistMgr->transferSint32(TMEMBER(hour));
  222. persistMgr->transferSint32(TMEMBER(min));
  223. persistMgr->transferSint32(TMEMBER(sec));
  224. if (persistMgr->checkVersion(1, 2, 1)) {
  225. int32 wday = _tm.tm_wday;
  226. persistMgr->transferSint32(TMEMBER(wday));
  227. _tm.tm_wday = wday;
  228. }
  229. _tm.tm_year = year;
  230. _tm.tm_mon = mon;
  231. _tm.tm_mday = mday;
  232. _tm.tm_hour = hour;
  233. _tm.tm_min = min;
  234. _tm.tm_sec = sec;
  235. return STATUS_OK;
  236. }
  237. //////////////////////////////////////////////////////////////////////////
  238. int SXDate::scCompare(BaseScriptable *Value) {
  239. TimeDate time1 = _tm;
  240. TimeDate time2 = ((SXDate *)Value)->_tm;
  241. if (time1.tm_year < time2.tm_year) {
  242. return -1;
  243. } else if (time1.tm_year == time2.tm_year) {
  244. if (time1.tm_mon < time2.tm_mon) {
  245. return -1;
  246. } else if (time1.tm_mon == time2.tm_mon) {
  247. if (time1.tm_mday < time2.tm_mday) {
  248. return -1;
  249. } else if (time1.tm_mday == time2.tm_mday) {
  250. if (time1.tm_hour < time2.tm_hour) {
  251. return -1;
  252. } else if (time1.tm_hour == time2.tm_hour) {
  253. if (time1.tm_min < time2.tm_min) {
  254. return -1;
  255. } else if (time1.tm_min == time2.tm_min) {
  256. if (time1.tm_sec < time2.tm_sec) {
  257. return -1;
  258. } else if (time1.tm_sec == time2.tm_sec) {
  259. return 0; // Equal
  260. } else {
  261. return 1; // Sec
  262. }
  263. } else {
  264. return 1; // Minute
  265. }
  266. } else {
  267. return 1; // Hour
  268. }
  269. } else {
  270. return 1; // Day
  271. }
  272. } else {
  273. return 1; // Month
  274. }
  275. } else {
  276. return 1; // Year
  277. }
  278. }
  279. } // End of namespace Wintermute