ChaiScriptXml.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 = CTextConvert::UnicodeToUTF8(values);
  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 = CTextConvert::UnicodeToUTF8(listItem.m_name);
  69. friendElement->SetAttribute("name", name);
  70. CStringA desc = CTextConvert::UnicodeToUTF8(listItem.m_description);
  71. friendElement->SetAttribute("description", desc);
  72. CStringA script = CTextConvert::UnicodeToUTF8(listItem.m_script);
  73. friendElement->SetAttribute("script", script);
  74. CStringA guid = CTextConvert::UnicodeToUTF8(listItem.m_guid);
  75. friendElement->SetAttribute("guid", guid);
  76. CStringA version = CTextConvert::UnicodeToUTF8(listItem.m_version);
  77. friendElement->SetAttribute("version", version);
  78. friendOuter->LinkEndChild(friendElement);
  79. }
  80. TiXmlPrinter printer;
  81. printer.SetLineBreak("");
  82. doc.Accept(&printer);
  83. CString cs = printer.CStr();
  84. return cs;
  85. }
  86. void CChaiScriptXml::AddToMenu(CMenu *pMenu, CAccels *actions)
  87. {
  88. if (m_list.size() > 0)
  89. {
  90. pMenu->AppendMenu(MF_SEPARATOR);
  91. bool addedItem = false;
  92. int id = 0;
  93. for (auto & element : m_list)
  94. {
  95. if (addedItem == false)
  96. {
  97. addedItem = true;
  98. }
  99. CString cs;
  100. if (element.m_description != _T(""))
  101. {
  102. cs.Format(_T("(%s) - %s"), element.m_name, element.m_description);
  103. }
  104. else
  105. {
  106. cs.Format(_T("%s"), element.m_name);
  107. }
  108. if (actions != NULL)
  109. {
  110. CString shortcutText = actions->GetCmdKeyText(ActionEnums::PASTE_SCRIPT, element.m_guid);
  111. if (shortcutText != _T(""))
  112. {
  113. cs += "\t";
  114. cs += shortcutText;
  115. }
  116. }
  117. pMenu->AppendMenuW(MF_STRING, (ChaiScriptMenuStartId + id), cs);
  118. id++;
  119. if (id > MaxChaiScripts)
  120. {
  121. break;
  122. }
  123. }
  124. }
  125. }