-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathCCommunicationsHandler.cpp
323 lines (232 loc) · 7.06 KB
/
CCommunicationsHandler.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
// CCommunicationsHandler.cpp
//
// CCommunicationsHandler class
// Copyright (c) 2017 Kronosaur Productions, LLC. All Rights Reserved.
#include "PreComp.h"
#define ON_SHOW_TAG CONSTLIT("OnShow")
#define CODE_TAG CONSTLIT("Code")
#define INVOKE_TAG CONSTLIT("Invoke")
#define ID_ATTRIB CONSTLIT("id")
#define KEY_ATTRIB CONSTLIT("key")
#define NAME_ATTRIB CONSTLIT("name")
CCommunicationsHandler::CCommunicationsHandler (void)
// CCommunicationsHandler constructor
{
}
CCommunicationsHandler::~CCommunicationsHandler (void)
// CCommunicationsHandler destructor
{
DeleteAll();
}
CCommunicationsHandler &CCommunicationsHandler::operator= (const CCommunicationsHandler &Src)
// CCommunicationsHandler operator =
{
int i;
DeleteAll();
m_Messages = Src.m_Messages;
for (i = 0; i < GetCount(); i++)
{
if (m_Messages[i].InvokeEvent.pCode)
m_Messages[i].InvokeEvent.pCode = m_Messages[i].InvokeEvent.pCode->Reference();
if (m_Messages[i].OnShowEvent.pCode)
m_Messages[i].OnShowEvent.pCode = m_Messages[i].OnShowEvent.pCode->Reference();
}
return *this;
}
void CCommunicationsHandler::DeleteAll (void)
// DeleteAll
//
// Delete all messages
{
for (int i = 0; i < GetCount(); i++)
{
if (m_Messages[i].InvokeEvent.pCode)
m_Messages[i].InvokeEvent.pCode->Discard(&g_pUniverse->GetCC());
if (m_Messages[i].OnShowEvent.pCode)
m_Messages[i].OnShowEvent.pCode->Discard(&g_pUniverse->GetCC());
}
m_Messages.DeleteAll();
}
bool CCommunicationsHandler::FindMergePos (const SMessage &Msg, int *retiPos)
// FindMergePos
//
// If the given message does not already exist (by ID) then we return
// the position at which it should be inserted.
{
int i;
// If we already have this message, then we don't need to insert it.
for (i = 0; i < GetCount(); i++)
if (strEquals(m_Messages[i].sID, Msg.sID))
return false;
// Figure out where to insert it (by shortcut order)
if (retiPos)
{
i = 0;
while (i < GetCount() && strCompareAbsolute(Msg.sShortcut, m_Messages[i].sShortcut) != 1)
i++;
*retiPos = i;
}
return true;
}
bool CCommunicationsHandler::FindMessage (const CString &sID, const SMessage **retpMessage) const
// FindMessage
//
// Finds the message by ID
{
int i;
for (i = 0; i < GetCount(); i++)
if (strEquals(m_Messages[i].sID, sID))
{
if (retpMessage)
*retpMessage = &m_Messages[i];
return true;
}
return false;
}
int CCommunicationsHandler::FindMessageByID (const CString &sID) const
// FindMessageByID
//
// Finds the message by ID
{
for (int i = 0; i < GetCount(); i++)
if (strEquals(m_Messages[i].sID, sID))
return i;
return -1;
}
int CCommunicationsHandler::FindMessageByName (const CString &sMessage) const
// FindMessageByName
//
// Finds the message by name
{
for (int i = 0; i < GetCount(); i++)
if (strEquals(m_Messages[i].sMessage, sMessage))
return i;
return -1;
}
void CCommunicationsHandler::FireInvoke (const CString &sID, CSpaceObject *pObj, CSovereign *pSender, ICCItem *pData)
// FireInvoke
//
// Invoke the message
{
// Get the code
const SMessage *pMsg;
if (!FindMessage(sID, &pMsg))
return;
if (pMsg->InvokeEvent.pCode == NULL)
return;
// Run
CCodeChainCtx Ctx;
// Define parameters
Ctx.DefineContainingType(pObj);
Ctx.SaveAndDefineSourceVar(pObj);
Ctx.SaveAndDefineDataVar(pData);
Ctx.DefineInteger(CONSTLIT("aPlayer"), pSender->GetUNID());
// Execute
ICCItem *pResult = Ctx.Run(pMsg->InvokeEvent);
if (pResult->IsError())
pSender->MessageFromObj(pObj, pResult->GetStringValue());
Ctx.Discard(pResult);
}
ALERROR CCommunicationsHandler::InitFromXML (CXMLElement *pDesc, CString *retsError)
// InitFromXML
//
// Load from an XML element
{
int i, j;
CString sError;
// Allocate the structure
int iCount = pDesc->GetContentElementCount();
if (iCount == 0)
return NOERROR;
m_Messages.InsertEmpty(iCount);
for (i = 0; i < iCount; i++)
{
CXMLElement *pMessage = pDesc->GetContentElement(i);
// ID
m_Messages[i].sID = pMessage->GetAttribute(ID_ATTRIB);
// Get the name
m_Messages[i].sMessage = pMessage->GetAttribute(NAME_ATTRIB);
m_Messages[i].sShortcut = pMessage->GetAttribute(KEY_ATTRIB);
// If the ID is blank, then use the shortcut; if that's missing, use the ordinal value.
if (m_Messages[i].sID.IsBlank())
{
if (!m_Messages[i].sShortcut.IsBlank())
m_Messages[i].sID = m_Messages[i].sShortcut;
else
m_Messages[i].sID = strFromInt(i);
}
// If no sub elements, just get the code from the content
if (pMessage->GetContentElementCount() == 0)
{
m_Messages[i].InvokeEvent.pExtension = NULL;
m_Messages[i].InvokeEvent.pCode = g_pUniverse->GetCC().Link(pMessage->GetContentText(0));
m_Messages[i].OnShowEvent.pExtension = NULL;
m_Messages[i].OnShowEvent.pCode = NULL;
}
// If we've got sub elements, then load the different code blocks
else
{
m_Messages[i].InvokeEvent.pExtension = NULL;
m_Messages[i].InvokeEvent.pCode = NULL;
m_Messages[i].OnShowEvent.pExtension = NULL;
m_Messages[i].OnShowEvent.pCode = NULL;
for (j = 0; j < pMessage->GetContentElementCount(); j++)
{
CXMLElement *pItem = pMessage->GetContentElement(j);
// OnShow
if (strEquals(pItem->GetTag(), ON_SHOW_TAG))
{
m_Messages[i].OnShowEvent.pExtension = NULL;
m_Messages[i].OnShowEvent.pCode = g_pUniverse->GetCC().Link(pItem->GetContentText(0));
}
else if (strEquals(pItem->GetTag(), INVOKE_TAG) || strEquals(pItem->GetTag(), CODE_TAG))
{
m_Messages[i].InvokeEvent.pExtension = NULL;
m_Messages[i].InvokeEvent.pCode = g_pUniverse->GetCC().Link(pItem->GetContentText(0));
}
else
{
*retsError = strPatternSubst(CONSTLIT("Unknown element: <%s>"), pItem->GetTag());
return ERR_FAIL;
}
}
}
// Deal with error
if (m_Messages[i].InvokeEvent.pCode && m_Messages[i].InvokeEvent.pCode->IsError())
sError = strPatternSubst(CONSTLIT("Communications: %s <Invoke>: %s"),
m_Messages[i].sID,
m_Messages[i].InvokeEvent.pCode->GetStringValue());
else if (m_Messages[i].OnShowEvent.pCode && m_Messages[i].OnShowEvent.pCode->IsError())
sError = strPatternSubst(CONSTLIT("Communications: %s <OnShow>: %s"),
m_Messages[i].sID,
m_Messages[i].OnShowEvent.pCode->GetStringValue());
}
// Done
if (retsError)
*retsError = sError;
return (sError.IsBlank() ? NOERROR : ERR_FAIL);
}
void CCommunicationsHandler::Merge (CCommunicationsHandler &New)
// Merge
//
// Merges messages from the new handler into this handler
{
int i;
for (i = 0; i < New.GetCount(); i++)
{
int iInsert;
if (FindMergePos(New.m_Messages[i], &iInsert))
{
SMessage *pMsg = m_Messages.InsertAt(iInsert);
pMsg->sID = New.m_Messages[i].sID;
pMsg->sMessage = New.m_Messages[i].sMessage;
pMsg->sShortcut = New.m_Messages[i].sShortcut;
pMsg->InvokeEvent = New.m_Messages[i].InvokeEvent;
if (pMsg->InvokeEvent.pCode)
pMsg->InvokeEvent.pCode->Reference();
pMsg->OnShowEvent = New.m_Messages[i].OnShowEvent;
if (pMsg->OnShowEvent.pCode)
pMsg->OnShowEvent.pCode->Reference();
}
}
}