ChaiScriptXml.cpp 3.4 KB

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