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