/
plugins.cpp
1647 lines (1333 loc) · 52.4 KB
/
plugins.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// plugins.cpp
// Plugins management
#include "stdafx.h"
#include "MUSHclient.h"
#include "doc.h"
#include "dialogs\plugins\PluginWizardSheet.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif
// plugin callback routines - start with OnPlugin so that we can advise
// users not to use that string for their own routines
// install / remove
const string ON_PLUGIN_INSTALL ("OnPluginInstall");
const string ON_PLUGIN_CLOSE ("OnPluginClose");
const string ON_PLUGIN_LIST_CHANGED ("OnPluginListChanged");
// connect / disconnect
const string ON_PLUGIN_CONNECT ("OnPluginConnect");
const string ON_PLUGIN_DISCONNECT ("OnPluginDisconnect");
// saving
const string ON_PLUGIN_SAVE_STATE ("OnPluginSaveState");
const string ON_PLUGIN_WORLD_SAVE ("OnPluginWorldSave");
// enable / disable
const string ON_PLUGIN_ENABLE ("OnPluginEnable");
const string ON_PLUGIN_DISABLE ("OnPluginDisable");
// the focus
const string ON_PLUGIN_GETFOCUS ("OnPluginGetFocus");
const string ON_PLUGIN_LOSEFOCUS ("OnPluginLoseFocus");
// capture stuff
const string ON_PLUGIN_TRACE ("OnPluginTrace");
const string ON_PLUGIN_BROADCAST ("OnPluginBroadcast");
const string ON_PLUGIN_SCREENDRAW ("OnPluginScreendraw");
// sounds
const string ON_PLUGIN_PLAYSOUND ("OnPluginPlaySound");
// stuff received/sent
const string ON_PLUGIN_SEND ("OnPluginSend");
const string ON_PLUGIN_SENT ("OnPluginSent");
const string ON_PLUGIN_PARTIAL_LINE ("OnPluginPartialLine");
const string ON_PLUGIN_LINE_RECEIVED ("OnPluginLineReceived");
const string ON_PLUGIN_PACKET_RECEIVED ("OnPluginPacketReceived");
// telnet negotiation
const string ON_PLUGIN_TELNET_OPTION ("OnPluginTelnetOption");
const string ON_PLUGIN_TELNET_REQUEST ("OnPluginTelnetRequest");
const string ON_PLUGIN_TELNET_SUBNEGOTIATION ("OnPluginTelnetSubnegotiation");
const string ON_PLUGIN_IAC_GA ("OnPlugin_IAC_GA");
// commands
const string ON_PLUGIN_COMMAND ("OnPluginCommand");
const string ON_PLUGIN_COMMAND_ENTERED ("OnPluginCommandEntered");
const string ON_PLUGIN_COMMAND_CHANGED ("OnPluginCommandChanged");
const string ON_PLUGIN_TABCOMPLETE ("OnPluginTabComplete");
// resizing, ticking, moving, rhythm
const string ON_PLUGIN_WORLD_OUTPUT_RESIZED ("OnPluginWorldOutputResized");
const string ON_PLUGIN_TICK ("OnPluginTick");
const string ON_PLUGIN_MOUSE_MOVED ("OnPluginMouseMoved");
// MXP stuff
const string ON_PLUGIN_MXP_START ("OnPluginMXPstart");
const string ON_PLUGIN_MXP_STOP ("OnPluginMXPstop");
const string ON_PLUGIN_MXP_OPENTAG ("OnPluginMXPopenTag");
const string ON_PLUGIN_MXP_CLOSETAG ("OnPluginMXPcloseTag");
const string ON_PLUGIN_MXP_SETVARIABLE ("OnPluginMXPsetVariable");
const string ON_PLUGIN_MXP_SETENTITY ("OnPluginMXPsetEntity");
const string ON_PLUGIN_MXP_ERROR ("OnPluginMXPerror");
// chat stuff
const string ON_PLUGIN_CHAT_ACCEPT ("OnPluginChatAccept");
const string ON_PLUGIN_CHAT_MESSAGE ("OnPluginChatMessage");
const string ON_PLUGIN_CHAT_MESSAGE_OUT ("OnPluginChatMessageOut");
const string ON_PLUGIN_CHAT_DISPLAY ("OnPluginChatDisplay");
const string ON_PLUGIN_CHAT_NEWUSER ("OnPluginChatNewUser");
const string ON_PLUGIN_CHAT_USERDISCONNECT ("OnPluginChatUserDisconnect");
// drawing
const string ON_PLUGIN_DRAW_OUTPUT_WINDOW ("OnPluginDrawOutputWindow");
// possible plugin callbacks
string PluginCallbacksNames [] = {
ON_PLUGIN_BROADCAST,
ON_PLUGIN_CHAT_ACCEPT,
ON_PLUGIN_CHAT_DISPLAY,
ON_PLUGIN_CHAT_MESSAGE,
ON_PLUGIN_CHAT_MESSAGE_OUT,
ON_PLUGIN_CHAT_NEWUSER,
ON_PLUGIN_CHAT_USERDISCONNECT,
ON_PLUGIN_CLOSE,
ON_PLUGIN_COMMAND,
ON_PLUGIN_COMMAND_CHANGED,
ON_PLUGIN_COMMAND_ENTERED,
ON_PLUGIN_CONNECT,
ON_PLUGIN_DISABLE,
ON_PLUGIN_DISCONNECT,
ON_PLUGIN_DRAW_OUTPUT_WINDOW,
ON_PLUGIN_ENABLE,
ON_PLUGIN_GETFOCUS,
ON_PLUGIN_IAC_GA,
ON_PLUGIN_INSTALL,
ON_PLUGIN_LINE_RECEIVED,
ON_PLUGIN_LIST_CHANGED,
ON_PLUGIN_LOSEFOCUS,
ON_PLUGIN_MOUSE_MOVED,
ON_PLUGIN_MXP_CLOSETAG,
ON_PLUGIN_MXP_ERROR,
ON_PLUGIN_MXP_OPENTAG,
ON_PLUGIN_MXP_SETENTITY,
ON_PLUGIN_MXP_SETVARIABLE,
ON_PLUGIN_MXP_START,
ON_PLUGIN_MXP_STOP,
ON_PLUGIN_PACKET_RECEIVED,
ON_PLUGIN_PARTIAL_LINE,
ON_PLUGIN_PLAYSOUND,
ON_PLUGIN_SAVE_STATE,
ON_PLUGIN_SCREENDRAW,
ON_PLUGIN_SEND,
ON_PLUGIN_SENT,
ON_PLUGIN_TABCOMPLETE,
ON_PLUGIN_TELNET_OPTION,
ON_PLUGIN_TELNET_REQUEST,
ON_PLUGIN_TELNET_SUBNEGOTIATION,
ON_PLUGIN_TICK,
ON_PLUGIN_TRACE,
ON_PLUGIN_WORLD_OUTPUT_RESIZED,
ON_PLUGIN_WORLD_SAVE,
"" // end of table marker
}; // end of PluginCallbacksNames
// constructor
CPlugin::CPlugin (CMUSHclientDoc * pDoc)
{
m_pDoc = pDoc;
m_ScriptEngine = NULL;
m_bEnabled = true;
m_VariableMap.InitHashTable (293); // allow for 300 variables in this plugin
m_AliasMap.InitHashTable (293); // ditto for aliases
m_TriggerMap.InitHashTable (293); // ditto for triggers
m_tDateInstalled = CTime::GetCurrentTime(); // when plugin loaded
m_dRequiredVersion = 0.0;
m_dVersion = 0.0;
m_bSendToScriptUsed = false;
m_bGlobal = false;
m_iLoadOrder = 0;
m_iScriptTimeTaken = 0;
m_bSavingStateNow = false;
m_iSequence = DEFAULT_PLUGIN_SEQUENCE;
} // end of constructor
// destructor
CPlugin::~CPlugin ()
{
// change to this plugin, call function, put current plugin back
CPlugin * pSavedPlugin = m_pDoc->m_CurrentPlugin;
m_pDoc->m_CurrentPlugin = this;
CScriptCallInfo callinfo (ON_PLUGIN_CLOSE, m_PluginCallbacks [ON_PLUGIN_CLOSE]);
ExecutePluginScript (callinfo);
m_pDoc->m_CurrentPlugin = pSavedPlugin;
SaveState ();
DELETE_MAP (m_TriggerMap, CTrigger);
DELETE_MAP (m_AliasMap, CAlias);
for (CTimerMapIterator timerIt = m_TimerMap.begin ();
timerIt != m_TimerMap.end ();
timerIt++)
delete timerIt->second;
m_TimerMap.clear ();
m_TimerRevMap.clear ();
DELETE_MAP (m_VariableMap, CVariable);
delete m_ScriptEngine;
// delete our arrays
for (tStringMapOfMaps::iterator it = m_Arrays.begin ();
it != m_Arrays.end ();
it++)
{
tStringToStringMap * m = it->second;
m->clear ();
delete m;
}
} // end of destructor
DISPID CPlugin::GetPluginDispid (const char * sName)
{
if (m_ScriptEngine)
return m_ScriptEngine->GetDispid (sName);
else
return DISPID_UNKNOWN;
} // end of CPlugin::GetPluginDispid
void CPlugin::ExecutePluginScript (CScriptCallInfo & callinfo)
{
DISPID & iRoutine = callinfo._dispid_info._dispid;
if (m_ScriptEngine && iRoutine != DISPID_UNKNOWN)
{
callinfo._dispid_info._count++;
long nInvocationCount = 0;
CString strType = TFormat ("Plugin %s",
(LPCTSTR) m_strName);
CString strReason = TFormat ("Executing plugin %s sub %s",
(LPCTSTR) m_strName,
callinfo._name.c_str () );
if (m_ScriptEngine->IsLua ())
{
list<double> nparams;
list<string> sparams;
m_ScriptEngine->ExecuteLua (iRoutine,
callinfo._name.c_str (),
eDontChangeAction,
strType,
strReason,
nparams,
sparams,
nInvocationCount);
} // end of Lua
else
{
DISPPARAMS params = { NULL, NULL, 0, 0 };
m_ScriptEngine->Execute (iRoutine,
callinfo._name.c_str (),
eDontChangeAction,
strType,
strReason,
params,
nInvocationCount, NULL);
} // end of not Lua
} // end of having a script engine
} // end of CPlugin::ExecutePluginScript
bool CPlugin::ExecutePluginScript (CScriptCallInfo & callinfo,
const char * sText)
{
DISPID & iRoutine = callinfo._dispid_info._dispid;
if (m_ScriptEngine && iRoutine != DISPID_UNKNOWN)
{
callinfo._dispid_info._count++;
long nInvocationCount = 0;
CString strType = TFormat ("Plugin %s",
(LPCTSTR) m_strName);
CString strReason = TFormat ("Executing plugin %s sub %s",
(LPCTSTR) m_strName,
callinfo._name.c_str () );
if (m_ScriptEngine->IsLua ())
{
list<double> nparams;
list<string> sparams;
sparams.push_back (sText);
bool result;
m_ScriptEngine->ExecuteLua (iRoutine,
callinfo._name.c_str (),
eDontChangeAction,
strType,
strReason,
nparams,
sparams,
nInvocationCount,
NULL, NULL, NULL,
&result);
return result;
} // end of Lua
else
{
enum
{
eText,
eArgCount, // this MUST be last
};
COleVariant args [eArgCount];
DISPPARAMS params = { args, NULL, eArgCount, 0 };
args [eText] = sText;
COleVariant result;
m_ScriptEngine->Execute (iRoutine,
callinfo._name.c_str (),
eDontChangeAction,
strType,
strReason,
params,
nInvocationCount,
&result);
// see what result was
if (result.vt != VT_EMPTY)
{
result.ChangeType (VT_I4); // make a long
if (result.vt == VT_I4) // conversion successful
if (result.lVal) // return if non-zero
return true;
else
return false;
} // end of having some sort of result
} // end of not Lua
} // end of having a script engine
return true; // assume they OK'd it
} // end of CPlugin::ExecutePluginScript
bool CPlugin::ExecutePluginScript (CScriptCallInfo & callinfo,
const long arg1,
const string sText)
{
DISPID & iRoutine = callinfo._dispid_info._dispid;
if (m_ScriptEngine && iRoutine != DISPID_UNKNOWN)
{
callinfo._dispid_info._count++;
long nInvocationCount = 0;
CString strType = TFormat ("Plugin %s", (LPCTSTR) m_strName);
CString strReason = TFormat ("Executing plugin %s sub %s",
(LPCTSTR) m_strName,
callinfo._name.c_str () );
if (m_ScriptEngine && m_ScriptEngine->IsLua ())
{
bool bResult;
list<double> nparams;
list<string> sparams;
nparams.push_back (arg1);
sparams.push_back (sText);
m_ScriptEngine->ExecuteLua (iRoutine,
callinfo._name.c_str (),
eDontChangeAction,
strType,
strReason,
nparams,
sparams,
nInvocationCount,
NULL, NULL, NULL,
&bResult);
return bResult;
} // end of Lua
// WARNING - arguments should appear in REVERSE order to what the sub expects them!
enum
{
eText,
eArg1,
eArgCount, // this MUST be last
};
COleVariant args [eArgCount];
DISPPARAMS params = { args, NULL, eArgCount, 0 };
args [eArg1] = arg1;
// this crazy code is to get 0x00 bytes into telnet subnegotiations ;)
VARIANT v;
VariantClear (&v);
v.vt = VT_BSTR;
CString str (sText.c_str (), sText.size ());
v.bstrVal = str.AllocSysString ();
args [eText].Attach (v);
COleVariant result;
m_ScriptEngine->Execute (iRoutine,
callinfo._name.c_str (),
eDontChangeAction,
strType,
strReason,
params,
nInvocationCount,
&result);
// see what result was
if (result.vt != VT_EMPTY)
{
result.ChangeType (VT_I4); // make a long
if (result.vt == VT_I4) // conversion successful
if (result.lVal) // return if non-zero
return true;
else
return false;
} // end of having some sort of result
} // end of having a script engine
return true; // assume they OK'd it
} // end of CPlugin::ExecutePluginScript
bool CPlugin::ExecutePluginScript (CScriptCallInfo & callinfo,
const long arg1,
const long arg2,
const string sText)
{
DISPID & iRoutine = callinfo._dispid_info._dispid;
if (m_ScriptEngine && iRoutine != DISPID_UNKNOWN)
{
callinfo._dispid_info._count++;
long nInvocationCount = 0;
CString strType = TFormat ("Plugin %s", (LPCTSTR) m_strName);
CString strReason = TFormat ("Executing plugin %s sub %s",
(LPCTSTR) m_strName,
callinfo._name.c_str () );
if (m_ScriptEngine && m_ScriptEngine->IsLua ())
{
bool bResult;
list<double> nparams;
list<string> sparams;
nparams.push_back (arg1);
nparams.push_back (arg2);
sparams.push_back (sText);
m_ScriptEngine->ExecuteLua (iRoutine,
callinfo._name.c_str (),
eDontChangeAction,
strType,
strReason,
nparams,
sparams,
nInvocationCount,
NULL, NULL, NULL,
&bResult);
return bResult;
} // end of Lua
// WARNING - arguments should appear in REVERSE order to what the sub expects them!
enum
{
eText,
eArg2,
eArg1,
eArgCount, // this MUST be last
};
COleVariant args [eArgCount];
DISPPARAMS params = { args, NULL, eArgCount, 0 };
args [eArg1] = arg1;
args [eArg2] = arg2;
args [eText] = CString (sText.c_str (), sText.size ()); // yadda yadda, doesn't work with internal 0x00
COleVariant result;
m_ScriptEngine->Execute (iRoutine,
callinfo._name.c_str (),
eDontChangeAction,
strType,
strReason,
params,
nInvocationCount,
&result);
// see what result was
if (result.vt != VT_EMPTY)
{
result.ChangeType (VT_I4); // make a long
if (result.vt == VT_I4) // conversion successful
if (result.lVal) // return if non-zero
return true;
else
return false;
} // end of having some sort of result
} // end of having a script engine
return true; // assume they OK'd it
} // end of CPlugin::ExecutePluginScript
bool CPlugin::ExecutePluginScript (CScriptCallInfo & callinfo,
const long arg1,
const char * arg2,
const char * arg3,
const char * arg4)
{
DISPID & iRoutine = callinfo._dispid_info._dispid;
if (m_ScriptEngine && iRoutine != DISPID_UNKNOWN)
{
callinfo._dispid_info._count++;
long nInvocationCount = 0;
CString strType = TFormat ("Plugin %s", (LPCTSTR) m_strName);
CString strReason = TFormat ("Executing plugin %s sub %s",
(LPCTSTR) m_strName,
callinfo._name.c_str () );
if (m_ScriptEngine && m_ScriptEngine->IsLua ())
{
bool bResult;
list<double> nparams;
list<string> sparams;
nparams.push_back (arg1);
sparams.push_back ((LPCTSTR) arg2);
sparams.push_back ((LPCTSTR) arg3);
sparams.push_back ((LPCTSTR) arg4);
m_ScriptEngine->ExecuteLua (iRoutine,
callinfo._name.c_str (),
eDontChangeAction,
strType,
strReason,
nparams,
sparams,
nInvocationCount,
NULL, NULL, NULL,
&bResult);
return bResult;
} // end of Lua
// WARNING - arguments should appear in REVERSE order to what the sub expects them!
enum
{
eArg4,
eArg3,
eArg2,
eArg1,
eArgCount, // this MUST be last
};
COleVariant args [eArgCount];
DISPPARAMS params = { args, NULL, eArgCount, 0 };
args [eArg1] = arg1;
args [eArg2] = arg2;
args [eArg3] = arg3;
args [eArg4] = arg4;
COleVariant result;
m_ScriptEngine->Execute (iRoutine,
callinfo._name.c_str (),
eDontChangeAction,
strType,
strReason,
params,
nInvocationCount,
&result);
// see what result was
if (result.vt != VT_EMPTY)
{
result.ChangeType (VT_I4); // make a long
if (result.vt == VT_I4) // conversion successful
if (result.lVal) // return if non-zero
return true;
else
return false;
} // end of having some sort of result
} // end of having a script engine
return true; // assume they OK'd it
} // end of CPlugin::ExecutePluginScript
void CPlugin::ExecutePluginScriptRtn (CScriptCallInfo & callinfo,
CString & strText)
{
DISPID & iRoutine = callinfo._dispid_info._dispid;
if (m_ScriptEngine && iRoutine != DISPID_UNKNOWN)
{
callinfo._dispid_info._count++;
long nInvocationCount = 0;
CString strType = TFormat ("Plugin %s",
(LPCTSTR) m_strName);
CString strReason = TFormat ("Executing plugin %s sub %s",
(LPCTSTR) m_strName,
callinfo._name.c_str () );
if (m_ScriptEngine->IsLua ())
{
m_ScriptEngine->ExecuteLua (iRoutine,
callinfo._name.c_str (),
eDontChangeAction,
strType,
strReason,
strText,
nInvocationCount,
strText);
} // end of Lua
else
{
enum
{
eText,
eArgCount, // this MUST be last
};
COleVariant args [eArgCount];
DISPPARAMS params = { args, NULL, eArgCount, 0 };
args [eText] = strText;
COleVariant result;
m_ScriptEngine->Execute (iRoutine,
callinfo._name.c_str (),
eDontChangeAction,
strType,
strReason,
params,
nInvocationCount,
&result);
// if they returned a string, give it back
if (result.vt == VT_BSTR)
strText = result.bstrVal;
} // end of not Lua
} // end of having a script engine
} // end of CPlugin::ExecutePluginScript
void RemoveTrailingBackslash (CString& str);
// function prototypes needed for folder browsing
int __stdcall BrowseCallbackProc(
HWND hwnd,
UINT uMsg,
LPARAM lParam,
LPARAM lpData
);
// The field below is needed to initialise the browse directory dialog
// with the initial directory
extern CString strStartingDirectory;
bool CPlugin::SaveState (const bool bScripted)
{
if (!m_bSaveState && !bScripted)
return true; // not needed
// need a directory
if (App.m_strDefaultStateFilesDirectory.IsEmpty ())
return true;
CString strFilename = CString (Make_Absolute_Path (App.m_strDefaultStateFilesDirectory));
CFile * f = NULL;
CArchive * ar = NULL;
bool bError = true;
// need a world id
if (m_pDoc->m_strWorldID.IsEmpty ())
return true;
// sigh ... check plugin state folder exists
CFileStatus status;
CString strError;
CString strFolder = strFilename;
RemoveTrailingBackslash (strFolder); // trailing slash not wanted, thanks
if (!CFile::GetStatus(strFolder, status))
{
strError = "does not exist";
}
else
{ // file exists, is it a writable folder?
if ((status.m_attribute & CFile::directory) == 0)
strError = "is not a directory";
else if ((status.m_attribute & CFile::readOnly) == 1)
strError = "is not writable";
} // end of checking for save state folder
// if some error, alert the media
if (!strError.IsEmpty ())
{
::AfxMessageBox ((LPCTSTR) CFormat ("The plugins 'save state' folder:\r\n\r\n"
"%s\r\n\r\n"
"%s. Please click OK and then browse for it, or create it.",
(LPCTSTR) strFilename,
(LPCTSTR) strError),
MB_ICONSTOP);
// BROWSE FOR STATE FOLDER
// Gets the Shell's default allocator
LPMALLOC pMalloc;
if (::SHGetMalloc(&pMalloc) == NOERROR)
{
char pszBuffer[MAX_PATH];
BROWSEINFO bi;
LPITEMIDLIST pidl;
// Get help on BROWSEINFO struct - it's got all the bit settings.
bi.hwndOwner = NULL;
bi.pidlRoot = NULL;
bi.pszDisplayName = pszBuffer;
bi.lpszTitle = "Folder for saving plugin state files";
bi.ulFlags = BIF_RETURNFSANCESTORS | BIF_RETURNONLYFSDIRS;
// if possible, let them create one
if (!bWine)
bi.ulFlags |= BIF_NEWDIALOGSTYLE | BIF_EDITBOX; // requires CoInitialize
bi.lpfn = BrowseCallbackProc;
bi.lParam = 0;
strStartingDirectory = App.m_strPluginsDirectory; // really should be under plugins directory
// This next call issues the dialog box.
if ((pidl = ::SHBrowseForFolder(&bi)) != NULL)
{
if (::SHGetPathFromIDList(pidl, pszBuffer))
{
App.m_strDefaultStateFilesDirectory = pszBuffer;
App.m_strDefaultStateFilesDirectory += "\\";
strFilename = App.m_strDefaultStateFilesDirectory;
// save back to database
App.SaveGlobalsToDatabase ();
}
// Free the PIDL allocated by SHBrowseForFolder.
pMalloc->Free(pidl);
}
// Release the shell's allocator.
pMalloc->Release();
}
} // end of no save state folder
CPlugin * oldPlugin = m_pDoc->m_CurrentPlugin;
m_pDoc->m_CurrentPlugin = this;
// prevent infinite loops
m_bSavingStateNow = true;
CScriptCallInfo callinfo (ON_PLUGIN_SAVE_STATE, m_PluginCallbacks [ON_PLUGIN_SAVE_STATE]);
ExecutePluginScript (callinfo);
// are not saving state now
m_bSavingStateNow = false;
strFilename += m_pDoc->m_strWorldID; // world ID
strFilename += "-";
strFilename += m_strID; // plugin ID
strFilename += "-state.xml"; // suffix
try
{
f = new CFile (strFilename,
CFile::modeCreate | CFile::modeReadWrite);
ar = new CArchive(f, CArchive::store);
CString strComment = TFormat ("Plugin state saved. Plugin: \"%s\". World: \"%s\".",
(LPCTSTR) m_strName,
(LPCTSTR) m_pDoc->m_mush_name);
m_pDoc->Save_World_XML (*ar, XML_VARIABLES, strComment);
bError = false;
} // end of try block
catch (CFileException * e)
{
::UMessageBox (
TFormat ("Unable to create the plugin save state file: %s",
(LPCTSTR) strFilename),
MB_ICONEXCLAMATION);
e->Delete ();
} // end of catching a file exception
catch (CMemoryException * e)
{
::UMessageBox (
TFormat ("Insufficient memory to write the plugin save state file: %s",
(LPCTSTR) strFilename),
MB_ICONEXCLAMATION);
e->Delete ();
} // end of catching a memory exception
catch (CArchiveException * e)
{
::UMessageBox (
TFormat ("There was a problem saving the plugin save state file: %s",
(LPCTSTR) strFilename),
MB_ICONEXCLAMATION);
e->Delete ();
} // end of catching an archive exception
delete ar; // delete archive
delete f; // delete file
// put old plugin back
m_pDoc->m_CurrentPlugin = oldPlugin;
return bError;
} // end of CPlugin::SaveState
void CMUSHclientDoc::OnFilePluginwizard()
{
// TODO: The property sheet attached to your project
// via this function is not hooked up to any message
// handler. In order to actually use the property sheet,
// you will need to associate this function with a control
// in your project such as a menu item or tool bar button.
CPluginWizardSheet propSheet;
propSheet.m_Page1.m_doc = this;
propSheet.m_Page2.m_doc = this;
propSheet.m_Page3.m_doc = this;
propSheet.m_Page4.m_doc = this;
propSheet.m_Page5.m_doc = this;
propSheet.m_Page6.m_doc = this;
propSheet.m_Page7.m_doc = this;
propSheet.m_Page8.m_doc = this;
// pre-load some stuff
propSheet.m_Page1.m_strID = ::GetUniqueID ();
CTime date = CTime::GetCurrentTime();
propSheet.m_Page1.m_strDateWritten = date.Format ("%Y-%m-%d %H:%M:%S");
propSheet.m_Page1.m_strVersion = "1.0";
propSheet.m_Page1.m_bRemoveItems = true;
myAtoF (MUSHCLIENT_VERSION, &propSheet.m_Page1.m_fRequires);
POSITION pos;
CString strName;
int iCount = 0;
// select everything
// triggers
for (pos = m_TriggerMap.GetStartPosition(); pos; )
{
CTrigger * t;
m_TriggerMap.GetNextAssoc (pos, strName, t);
t->bSelected = true;
}
// aliases
for (pos = m_AliasMap.GetStartPosition(); pos; )
{
CAlias * a;
m_AliasMap.GetNextAssoc (pos, strName, a);
a->bSelected = true;
}
// timers
for (CTimerMapIterator timerIt = m_TimerMap.begin ();
timerIt != m_TimerMap.end ();
timerIt++)
{
timerIt->second->bSelected = true;
}
// variables
for (pos = m_VariableMap.GetStartPosition(); pos; iCount++)
{
CVariable * v;
m_VariableMap.GetNextAssoc (pos, strName, v);
v->bSelected = true;
}
propSheet.m_Page2.m_bGenerateHelp = true;
propSheet.m_Page6.m_bSaveState = iCount > 0;
propSheet.m_Page7.m_strLanguage = m_strLanguage;
propSheet.m_Page7.m_bStandardConstants = m_bEnableScripts;
// load script file
if (!m_strScriptFilename.IsEmpty ())
{
try
{
CFile f (m_strScriptFilename, CFile::modeRead | CFile::shareDenyWrite);
UINT iLength = f.GetLength ();
if (iLength > 0)
{
char * p = propSheet.m_Page7.m_strScript.GetBuffer (iLength + 1);
if (f.Read (p, iLength) != iLength)
{
propSheet.m_Page7.m_strScript.ReleaseBuffer (0);
ThrowErrorException ("Could not read script file");
}
propSheet.m_Page7.m_strScript.ReleaseBuffer (iLength);
} // end of non zero-length script file
} // end of try block
catch (CException* e)
{
e->ReportError();
e->Delete();
} // end of catch
} // end of having a script file
if (propSheet.DoModal() != IDOK)
return;
// write plugin
#define NL "\r\n"
try
{
CString strFilename = propSheet.m_Page1.m_strName + ".xml";
// now find output file name
CFileDialog dlg(FALSE, // FALSE for FileSave
"xml", // default extension
strFilename, // suggest obvious name
OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT|OFN_PATHMUSTEXIST,
"Plugin files (*.xml)|*.xml||",
NULL);
dlg.m_ofn.lpstrTitle = "Save plugin as";
// use default plugins directory
dlg.m_ofn.lpstrInitialDir = Make_Absolute_Path (App.m_strPluginsDirectory);
ChangeToFileBrowsingDirectory ();
int nResult = dlg.DoModal();
ChangeToStartupDirectory ();
if (nResult != IDOK)
return;
strFilename = dlg.GetPathName();
CFile f (strFilename, CFile::modeCreate|CFile::modeWrite|CFile::shareExclusive);
CArchive ar(&f, CArchive::store);
// xml prolog
ar.WriteString ("<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>" NL);
// document type is world
ar.WriteString ("<!DOCTYPE muclient>" NL);
// when did we save it?
ar.WriteString (CFormat ("<!-- Saved on %s -->" NL,
FixHTMLString (CTime::GetCurrentTime().Format