cfgscriptobj.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. #include <precomp.h>
  2. #include "cfgscriptobj.h"
  3. #include <api/config/items/cfgitemi.h>
  4. #ifndef _WASABIRUNTIME
  5. BEGIN_SERVICES(ConfigObject_Svc);
  6. DECLARE_SERVICE(ScriptObjectCreator<ConfigScriptObjectSvc>);
  7. END_SERVICES(ConfigObject_Svc, _ConfigObject_Svc);
  8. #ifdef _X86_
  9. extern "C" { int _link_ConfigObjectSvc; }
  10. #else
  11. extern "C" { int __link_ConfigObjectSvc; }
  12. #endif
  13. #endif
  14. static ConfigScriptObjectController _configController;
  15. ScriptObjectController *configController = &_configController;
  16. static ConfigItemScriptObjectController _configItemController;
  17. ScriptObjectController *configItemController = &_configItemController;
  18. static ConfigAttributeScriptObjectController _configAttributeController;
  19. ScriptObjectController *configAttributeController = &_configAttributeController;
  20. // -----------------------------------------------------------------------------------------------------
  21. // Service
  22. ScriptObjectController *ConfigScriptObjectSvc::getController(int n)
  23. {
  24. switch (n) {
  25. case 0:
  26. return configController;
  27. case 1:
  28. return configItemController;
  29. case 2:
  30. return configAttributeController;
  31. }
  32. return NULL;
  33. }
  34. // -----------------------------------------------------------------------------------------------------
  35. // ConfigObject
  36. ConfigObject::ConfigObject() {
  37. numobjects++;
  38. getScriptObject()->vcpu_setInterface(CONFIG_SCRIPTOBJECT_GUID, (void *)static_cast<ConfigObject *>(this));
  39. getScriptObject()->vcpu_setClassName(L"Config");
  40. getScriptObject()->vcpu_setController(configController);
  41. }
  42. ConfigObject::~ConfigObject() {
  43. numobjects--;
  44. mylist.deleteAll();
  45. if (numobjects == 0) {
  46. foreach(ouraddeditems)
  47. WASABI_API_CONFIG->config_deregisterCfgItem(ouraddeditems.getfor());
  48. endfor
  49. ouraddeditems.deleteAll();
  50. }
  51. }
  52. PtrList<CfgItemI> ConfigObject::ouraddeditems;
  53. int ConfigObject::numobjects=0;
  54. function_descriptor_struct ConfigScriptObjectController::exportedFunction[] = {
  55. {L"getItem", 1, (void*)ConfigObject::config_getItem},
  56. {L"getItemByGuid", 1, (void*)ConfigObject::config_getItemByGuid},
  57. {L"newItem", 2, (void*)ConfigObject::config_newItem},
  58. };
  59. int ConfigScriptObjectController::getNumFunctions() {
  60. return sizeof(exportedFunction) / sizeof(function_descriptor_struct);
  61. }
  62. scriptVar ConfigObject::config_newItem(SCRIPT_FUNCTION_PARAMS, ScriptObject *o, scriptVar configitem_name, scriptVar configitem_guid)
  63. {
  64. SCRIPT_FUNCTION_INIT
  65. ConfigObject *co = static_cast<ConfigObject*>(o->vcpu_getInterface(CONFIG_SCRIPTOBJECT_GUID));
  66. ConfigItemObject *ret=getItem(GET_SCRIPT_STRING(configitem_name), co);
  67. if (!ret)
  68. ret = getItem(GET_SCRIPT_STRING(configitem_guid), co);
  69. if (ret)
  70. return MAKE_SCRIPT_OBJECT(ret->getScriptObject());
  71. CfgItemI *item = new CfgItemI(GET_SCRIPT_STRING(configitem_name), nsGUID::fromCharW(GET_SCRIPT_STRING(configitem_guid)));
  72. ConfigObject::ouraddeditems.addItem(item);
  73. WASABI_API_CONFIG->config_registerCfgItem(item);
  74. ret = new ConfigItemObject(item);
  75. co->mylist.addItem(ret);
  76. return MAKE_SCRIPT_OBJECT(ret->getScriptObject());
  77. }
  78. ConfigItemObject *ConfigObject::getItem(const wchar_t *nameorguid, ConfigObject *co) {
  79. int i=0;
  80. ConfigItemObject *ret=NULL;
  81. GUID g = nsGUID::fromCharW(nameorguid);
  82. for (i=0;;i++) {
  83. CfgItem *item = WASABI_API_CONFIG->config_enumCfgItem(i);
  84. if (!item) break;
  85. GUID ig = item->getGuid();
  86. if (g == ig || WCSCASEEQLSAFE(nameorguid, item->getName()))
  87. {
  88. ret = new ConfigItemObject(item);
  89. co->mylist.addItem(ret);
  90. break;
  91. }
  92. #if 0//CUT
  93. for (int j=0;j<item->getNumChildren();j++) {
  94. if (STRCASEEQL(nameorguid, item->enumChild(j)->getName())) {
  95. ret = new ConfigItemObject(item->enumChild(j));
  96. co->mylist.addItem(ret);
  97. break;
  98. }
  99. }
  100. #endif
  101. }
  102. return ret;
  103. }
  104. scriptVar ConfigObject::config_getItem(SCRIPT_FUNCTION_PARAMS, ScriptObject *o, scriptVar cfgitem_name)
  105. {
  106. SCRIPT_FUNCTION_INIT
  107. ConfigObject *co = static_cast<ConfigObject*>(o->vcpu_getInterface(CONFIG_SCRIPTOBJECT_GUID));
  108. ConfigItemObject *ret=getItem(GET_SCRIPT_STRING(cfgitem_name), co);
  109. return MAKE_SCRIPT_OBJECT(ret ? ret->getScriptObject() : NULL);
  110. }
  111. scriptVar ConfigObject::config_getItemByGuid(SCRIPT_FUNCTION_PARAMS, ScriptObject *o, scriptVar cfgitem_guid)
  112. {
  113. SCRIPT_FUNCTION_INIT
  114. // GUID g = nsGUID::fromChar(GET_SCRIPT_STRING(guid));
  115. // api->
  116. int i=0;
  117. ConfigItemObject *ret=NULL;
  118. ConfigObject *co = static_cast<ConfigObject*>(o->vcpu_getInterface(CONFIG_SCRIPTOBJECT_GUID));
  119. const wchar_t *p = GET_SCRIPT_STRING(cfgitem_guid);
  120. if (p == NULL) {
  121. RETURN_SCRIPT_ZERO;
  122. }
  123. GUID g = nsGUID::fromCharW(p);
  124. for (i=0;;i++) {
  125. CfgItem *item = WASABI_API_CONFIG->config_enumCfgItem(i);
  126. if (!item) break;
  127. if (g == item->getGuid()) {
  128. ret = new ConfigItemObject(item);
  129. co->mylist.addItem(ret);
  130. break;
  131. }
  132. #if 0//CUT
  133. for (int j=0;j<item->getNumChildren();j++) {
  134. if (g == item->enumChild(j)->getName()) {
  135. ret = new ConfigItemObject(item->enumChild(j));
  136. co->mylist.addItem(ret);
  137. break;
  138. }
  139. }
  140. #endif
  141. }
  142. return MAKE_SCRIPT_OBJECT(ret ? ret->getScriptObject() : NULL);
  143. }
  144. ScriptObject *ConfigScriptObjectController::instantiate() {
  145. ConfigObject *c = new ConfigObject;
  146. if (!c) return NULL;
  147. return c->getScriptObject();
  148. }
  149. void ConfigScriptObjectController::destroy(ScriptObject *o) {
  150. ConfigObject *obj = static_cast<ConfigObject *>(o->vcpu_getInterface(CONFIG_SCRIPTOBJECT_GUID));
  151. ASSERT(obj != NULL);
  152. delete obj;
  153. }
  154. void *ConfigScriptObjectController::encapsulate(ScriptObject *o) {
  155. return NULL; // no encapsulation for config
  156. }
  157. void ConfigScriptObjectController::deencapsulate(void *) {
  158. }
  159. // -----------------------------------------------------------------------------------------------------
  160. // ConfigItem
  161. function_descriptor_struct ConfigItemScriptObjectController::exportedFunction[] = {
  162. {L"getAttribute", 1, (void*)ConfigItemObject::configItem_getAttribute},
  163. {L"getGuid", 0, (void*)ConfigItemObject::configItem_getGuid},
  164. {L"newAttribute", 2, (void*)ConfigItemObject::configItem_newAttribute},
  165. };
  166. int ConfigItemScriptObjectController::getNumFunctions() {
  167. return sizeof(exportedFunction) / sizeof(function_descriptor_struct);
  168. }
  169. ConfigItemObject::ConfigItemObject(CfgItem *_item)
  170. {
  171. getScriptObject()->vcpu_setInterface(CONFIGITEM_SCRIPTOBJECT_GUID, (void *)static_cast<ConfigItemObject *>(this));
  172. getScriptObject()->vcpu_setClassName(L"ConfigItem");
  173. getScriptObject()->vcpu_setController(configItemController);
  174. wchar_t strguid[256];
  175. nsGUID::toCharW(_item->getGuid(), strguid);
  176. guid = strguid;
  177. item = _item;
  178. }
  179. ConfigAttributeObject *ConfigItemObject::getAttribute(const wchar_t *name)
  180. {
  181. if (!item) return NULL;
  182. for (int i=0;i<item->getNumAttributes();i++)
  183. {
  184. if (!WCSICMP(item->enumAttribute(i), name))
  185. {
  186. ConfigAttributeObject *o = new ConfigAttributeObject(item, name, this);
  187. mylist.addItem(o);
  188. return o;
  189. }
  190. }
  191. return NULL;
  192. }
  193. ConfigAttributeObject *ConfigItemObject::newAttribute(const wchar_t *name, const wchar_t *defaultvalue)
  194. {
  195. if (!item) return NULL;
  196. ConfigAttributeObject *o = getAttribute(name);
  197. if (o != NULL) return o;
  198. item->addAttribute(name, defaultvalue);
  199. ConfigAttributeObject *cao = getAttribute(name);
  200. cao->setAutoDelete();
  201. return cao;
  202. }
  203. ConfigItemObject::~ConfigItemObject()
  204. {
  205. mylist.deleteAll();
  206. }
  207. scriptVar ConfigItemObject::configItem_getAttribute(SCRIPT_FUNCTION_PARAMS, ScriptObject *o, scriptVar name)
  208. {
  209. SCRIPT_FUNCTION_INIT
  210. ConfigItemObject *cio = static_cast<ConfigItemObject *>(o->vcpu_getInterface(CONFIGITEM_SCRIPTOBJECT_GUID));
  211. ConfigAttributeObject *cao = NULL;
  212. if (cio)
  213. cao = cio->getAttribute(GET_SCRIPT_STRING(name));
  214. return MAKE_SCRIPT_OBJECT(cao ? cao->getScriptObject() : NULL);
  215. }
  216. scriptVar ConfigItemObject::configItem_getGuid(SCRIPT_FUNCTION_PARAMS, ScriptObject *o)
  217. {
  218. SCRIPT_FUNCTION_INIT
  219. ConfigItemObject *cio = static_cast<ConfigItemObject *>(o->vcpu_getInterface(CONFIGITEM_SCRIPTOBJECT_GUID));
  220. if (cio) return MAKE_SCRIPT_STRING(cio->getGuid());
  221. return MAKE_SCRIPT_STRING(L"");
  222. }
  223. scriptVar ConfigItemObject::configItem_newAttribute(SCRIPT_FUNCTION_PARAMS, ScriptObject *o, scriptVar name, scriptVar defval) {
  224. SCRIPT_FUNCTION_INIT
  225. ConfigItemObject *cio = static_cast<ConfigItemObject *>(o->vcpu_getInterface(CONFIGITEM_SCRIPTOBJECT_GUID));
  226. ConfigAttributeObject *cao = NULL;
  227. if (cio)
  228. cao = cio->newAttribute(GET_SCRIPT_STRING(name), GET_SCRIPT_STRING(defval));
  229. return MAKE_SCRIPT_OBJECT(cao ? cao->getScriptObject() : NULL);
  230. }
  231. // -----------------------------------------------------------------------------------------------------
  232. // ConfigAttribute
  233. function_descriptor_struct ConfigAttributeScriptObjectController::exportedFunction[] = {
  234. {L"getData", 0, (void*)ConfigAttributeObject::configAttr_getData},
  235. {L"setData", 1, (void*)ConfigAttributeObject::configAttr_setData},
  236. {L"onDataChanged", 0, (void*)ConfigAttributeObject::configAttr_onDataChanged},
  237. {L"getParentItem", 0, (void*)ConfigAttributeObject::configAttr_getParentItem},
  238. {L"getAttributeName", 0, (void*)ConfigAttributeObject::configAttr_getAttributeName},
  239. };
  240. int ConfigAttributeScriptObjectController::getNumFunctions() {
  241. return sizeof(exportedFunction) / sizeof(function_descriptor_struct);
  242. }
  243. void ConfigAttributeObject::setData(const wchar_t *data)
  244. {
  245. if (!item || !attr) return;
  246. item->setData(attr, data);
  247. }
  248. const wchar_t *ConfigAttributeObject::getData()
  249. {
  250. if (!item || !attr) return NULL;
  251. static wchar_t t[WA_MAX_PATH];
  252. item->getData(attr, t, WA_MAX_PATH);
  253. return t;
  254. }
  255. ConfigAttributeObject::ConfigAttributeObject(CfgItem *_item, const wchar_t *_attr, ConfigItemObject *_parent)
  256. {
  257. getScriptObject()->vcpu_setInterface(CONFIGATTRIBUTE_SCRIPTOBJECT_GUID, (void *)static_cast<ConfigAttributeObject *>(this));
  258. getScriptObject()->vcpu_setClassName(L"ConfigAttribute");
  259. getScriptObject()->vcpu_setController(configAttributeController);
  260. attr = _attr;
  261. item = _item;
  262. parent = _parent;
  263. viewer_addViewItem(item);
  264. autodelete = 0;
  265. }
  266. ConfigAttributeObject::~ConfigAttributeObject() {
  267. if (autodelete) getParentItem()->getCfgItem()->delAttribute(attr);
  268. }
  269. int ConfigAttributeObject::viewer_onEvent(CfgItem *item, int event, intptr_t param, void *ptr, size_t ptrlen)
  270. {
  271. if (event == CfgItem::Event_ATTRIBUTE_CHANGED)
  272. {
  273. const wchar_t *_attr = reinterpret_cast<const wchar_t *>(ptr);
  274. if (!WCSICMP(attr, _attr))
  275. configAttr_onDataChanged(SCRIPT_CALL, getScriptObject());
  276. }
  277. return 1;
  278. }
  279. scriptVar ConfigAttributeObject::configAttr_setData(SCRIPT_FUNCTION_PARAMS, ScriptObject *o, scriptVar val)
  280. {
  281. SCRIPT_FUNCTION_INIT
  282. ConfigAttributeObject *cao = static_cast<ConfigAttributeObject *>(o->vcpu_getInterface(CONFIGATTRIBUTE_SCRIPTOBJECT_GUID));
  283. if (cao)
  284. cao->setData(GET_SCRIPT_STRING(val));
  285. RETURN_SCRIPT_VOID;
  286. }
  287. scriptVar ConfigAttributeObject::configAttr_getData(SCRIPT_FUNCTION_PARAMS, ScriptObject *o) {
  288. SCRIPT_FUNCTION_INIT
  289. ConfigAttributeObject *cao = static_cast<ConfigAttributeObject *>(o->vcpu_getInterface(CONFIGATTRIBUTE_SCRIPTOBJECT_GUID));
  290. if (cao)
  291. return MAKE_SCRIPT_STRING(cao->getData());
  292. return MAKE_SCRIPT_STRING(L"");
  293. }
  294. scriptVar ConfigAttributeObject::configAttr_onDataChanged(SCRIPT_FUNCTION_PARAMS, ScriptObject *o) {
  295. SCRIPT_FUNCTION_INIT
  296. PROCESS_HOOKS0(o, configAttributeController);
  297. SCRIPT_FUNCTION_CHECKABORTEVENT;
  298. SCRIPT_EXEC_EVENT0(o);
  299. }
  300. scriptVar ConfigAttributeObject::configAttr_getParentItem(SCRIPT_FUNCTION_PARAMS, ScriptObject *o) {
  301. SCRIPT_FUNCTION_INIT
  302. ConfigAttributeObject *cao = static_cast<ConfigAttributeObject *>(o->vcpu_getInterface(CONFIGATTRIBUTE_SCRIPTOBJECT_GUID));
  303. if (cao) return MAKE_SCRIPT_OBJECT(cao->getParentItem()->getScriptObject());
  304. return MAKE_SCRIPT_OBJECT(NULL);
  305. }
  306. scriptVar ConfigAttributeObject::configAttr_getAttributeName(SCRIPT_FUNCTION_PARAMS, ScriptObject *o) {
  307. SCRIPT_FUNCTION_INIT
  308. ConfigAttributeObject *cao = static_cast<ConfigAttributeObject *>(o->vcpu_getInterface(CONFIGATTRIBUTE_SCRIPTOBJECT_GUID));
  309. if (cao)
  310. return MAKE_SCRIPT_STRING(cao->getAttributeName());
  311. return MAKE_SCRIPT_STRING(L"");
  312. }