ChaiScriptXml.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #include "stdafx.h"
  2. #include "ChaiScriptXml.h"
  3. #include "tinyxml\tinyxml.h"
  4. #include "Shared\TextConvert.h"
  5. #include "Misc.h"
  6. CChaiScriptXml::CChaiScriptXml()
  7. {
  8. m_assignedGuidOnLoad = false;
  9. }
  10. CChaiScriptXml::~CChaiScriptXml()
  11. {
  12. }
  13. CString CChaiScriptXml::GetScript(CString name, BOOL &active)
  14. {
  15. CString script;
  16. for (auto & listItem : m_list)
  17. {
  18. if (listItem.m_name == name)
  19. {
  20. active = listItem.m_active;
  21. script = listItem.m_name;
  22. break;
  23. }
  24. }
  25. return script;
  26. }
  27. void CChaiScriptXml::Load(CString values)
  28. {
  29. m_assignedGuidOnLoad = false;
  30. m_list.clear();
  31. TiXmlDocument doc;
  32. CStringA xmlA;
  33. CTextConvert::ConvertToUTF8(values, xmlA);
  34. doc.Parse(xmlA);
  35. TiXmlElement *ItemHeader = doc.FirstChildElement("ChaiScripts");
  36. if (ItemHeader != NULL)
  37. {
  38. TiXmlElement *ItemElement = ItemHeader->FirstChildElement();
  39. while (ItemElement)
  40. {
  41. CDittoChaiScriptXmlItem array_item;
  42. ItemElement->Attribute("active", &array_item.m_active);
  43. array_item.m_name = ItemElement->Attribute("name");
  44. array_item.m_description = ItemElement->Attribute("description");
  45. array_item.m_script = ItemElement->Attribute("script");
  46. array_item.m_guid = ItemElement->Attribute("guid");
  47. array_item.m_version = ItemElement->Attribute("vesion");
  48. if (array_item.m_guid == "")
  49. {
  50. array_item.m_guid = NewGuidString();
  51. m_assignedGuidOnLoad = true;
  52. }
  53. m_list.push_back(array_item);
  54. ItemElement = ItemElement->NextSiblingElement();
  55. }
  56. }
  57. }
  58. CString CChaiScriptXml::Save()
  59. {
  60. m_assignedGuidOnLoad = false;
  61. TiXmlDocument doc;
  62. TiXmlElement* friendOuter = new TiXmlElement("ChaiScripts");
  63. doc.LinkEndChild(friendOuter);
  64. for (auto & listItem : m_list)
  65. {
  66. TiXmlElement* friendElement = new TiXmlElement("ChaiScriptItem");
  67. friendElement->SetAttribute("active", listItem.m_active);
  68. CStringA name;
  69. CTextConvert::ConvertToUTF8(listItem.m_name, name);
  70. friendElement->SetAttribute("name", name);
  71. CStringA desc;
  72. CTextConvert::ConvertToUTF8(listItem.m_description, desc);
  73. friendElement->SetAttribute("description", desc);
  74. CStringA script;
  75. CTextConvert::ConvertToUTF8(listItem.m_script, script);
  76. friendElement->SetAttribute("script", script);
  77. CStringA guid;
  78. CTextConvert::ConvertToUTF8(listItem.m_guid, guid);
  79. friendElement->SetAttribute("guid", guid);
  80. CStringA version;
  81. CTextConvert::ConvertToUTF8(listItem.m_version, version);
  82. friendElement->SetAttribute("version", version);
  83. friendOuter->LinkEndChild(friendElement);
  84. }
  85. TiXmlPrinter printer;
  86. printer.SetLineBreak("");
  87. doc.Accept(&printer);
  88. CString cs = printer.CStr();
  89. return cs;
  90. }
  91. void CChaiScriptXml::AddToMenu(CMenu *pMenu)
  92. {
  93. if (m_list.size() > 0)
  94. {
  95. pMenu->AppendMenu(MF_SEPARATOR);
  96. bool addedItem = false;
  97. int id = 0;
  98. for (auto & element : m_list)
  99. {
  100. if (addedItem == false)
  101. {
  102. addedItem = true;
  103. }
  104. CString cs;
  105. if (element.m_description != _T(""))
  106. {
  107. cs.Format(_T("(%s) - %s"), element.m_name, element.m_description);
  108. }
  109. else
  110. {
  111. cs.Format(_T("%s"), element.m_name);
  112. }
  113. pMenu->AppendMenuW(MF_STRING, (ChaiScriptMenuStartId + id), cs);
  114. id++;
  115. if (id > MaxChaiScripts)
  116. {
  117. break;
  118. }
  119. }
  120. }
  121. }