/
lua_methods.cpp
7436 lines (6373 loc) · 235 KB
/
lua_methods.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
#include "stdafx.h"
#include "..\mainfrm.h"
#include "..\MUSHclient.h"
#include "..\doc.h"
#include "..\scripting\errors.h"
#include "..\winplace.h"
#include "..\TextDocument.h"
#include "..\TextView.h"
#include "..\sendvw.h"
#include "..\dialogs\DebugLuaDlg.h"
#ifdef USE_LUA
const char mushclient_typename[] = "mushclient.world";
/*------------------------------------------------------------------------
This whole file is basically "glue" routines to bind the Lua calls to the
normal MUSHclient script calls. As far as possible the calls will be compatible
with existing ones.
A couple of exceptions will be things like GetAlias which return results
by reference, these are changed to return multiple results.
Written: 20th November 2004 by Nick Gammon
Multiple-world support
----------------------
The functions will automatically check if the first argument is a userdatum,
and if so, check that it is a mushclient.world userdatum.
This lets you direct commands to other worlds, like this:
other = world.GetWorld ("my other world")
world.Note (other, "hello there")
world.AddMapperComment (other, "this is a comment")
------------------------------------------------------------------------ */
// same as luaL_checkudata but just returns NULL if not correct type
LUALIB_API void *isudata (lua_State *L, int ud, const char *tname) {
void *p = lua_touserdata(L, ud);
if (p != NULL) { /* value is a userdata? */
if (lua_getmetatable(L, ud)) { /* does it have a metatable? */
lua_getfield(L, LUA_REGISTRYINDEX, tname); /* get correct metatable */
int bCorrectType = lua_rawequal(L, -1, -2);
lua_pop(L, 2); /* remove both metatables */
if (bCorrectType) /* does it have the correct mt? */
return p;
} // has metatable
} // is userdata
return NULL; /* to avoid warnings */
}
// checks user data is mushclient.doc, and returns pointer to world
static CMUSHclientDoc *doc (lua_State *L)
{
CMUSHclientDoc **ud = (CMUSHclientDoc **) isudata (L, 1, mushclient_typename);
// if first argument is a world userdatum, take that as our world
if (ud)
{
CMUSHclientDoc * pWantedDoc = *ud;
luaL_argcheck(L, ud != NULL, 1, "'world' expected");
lua_remove (L, 1); // remove the userdata, we don't need it now
// for safety, check that world still exists
for (POSITION docPos = App.m_pWorldDocTemplate->GetFirstDocPosition();
docPos != NULL; )
{
CMUSHclientDoc * pDoc = (CMUSHclientDoc *) App.m_pWorldDocTemplate->GetNextDoc(docPos);
if (pDoc == pWantedDoc)
return pDoc;
} // end of doing each document
luaL_error (L, "world is no longer available");
}
// retrieve our state
CMUSHclientDoc * pDoc;
/* retrieve the document */
lua_pushstring(L, DOCUMENT_STATE); /* push address */
lua_gettable(L, LUA_ENVIRONINDEX); /* retrieve value */
pDoc = (CMUSHclientDoc *) lua_touserdata(L, -1); /* convert to data */
lua_pop(L, 1); /* pop result */
return pDoc;
}
// helper function for pushing results returned by normal MUSHclient methods
// (stored in a variant)
int pushVariant (lua_State *L, VARIANT & v)
{
switch (v.vt)
{
case VT_BSTR:
lua_pushstring (L, CString (v.bstrVal));
VariantClear (&v);
break;
case VT_I2:
lua_pushnumber (L, v.iVal);
break;
case VT_I4:
lua_pushnumber (L, v.lVal);
break;
case VT_R4:
lua_pushnumber (L, v.fltVal);
break;
case VT_R8:
lua_pushnumber (L, v.dblVal);
break;
case VT_BOOL:
lua_pushboolean (L, v.boolVal);
break;
case VT_DATE:
{
// maybe not, let the user format the date
// COleDateTime d (v.date);
// lua_pushstring (L, d.Format (0, 0));
// lua_pushnumber (L, v.date);
COleDateTime dt (v.date); // convert to COleDateTime
SYSTEMTIME sysTime;
dt.GetAsSystemTime (sysTime); // now get as SYSTEMTIME
CTime tm (sysTime); // import into CTime
lua_pushnumber (L, tm.GetTime ()); // return as Lua-style system time
break;
}
case (VT_ARRAY + VT_VARIANT): // array of variants
{
lua_newtable(L);
SAFEARRAY * psa = v.parray;
long lLBound, lUBound;
SafeArrayGetLBound(psa, 1, &lLBound);
SafeArrayGetUBound(psa, 1, &lUBound);
VARIANT val;
int extra = 0;
if (lLBound == 0)
extra = 1;
for (int i = lLBound; i <= lUBound; i++)
{
long index[2];
index [0] = i;
index [1] = 0;
SafeArrayGetElement(psa, index, &val);
pushVariant (L, val); // recurse to push the element value
lua_rawseti(L, -2, i + extra); // make 1-relative
} // end of doing each row
VariantClear (&v);
} // end of VT_ARRAY
break;
default:
lua_pushnil (L);
break;
} // end of switch on type
return 1; // number of result fields
} // end of pushVariant
// helper function for pushing results returned by normal MUSHclient methods
// (stored in a BSTR)
int pushBstr (lua_State *L, BSTR & str)
{
lua_pushstring (L, CString (str));
SysFreeString (str);
return 1; // number of result fields
} // end of pushBstr
// helper function for things like print, note, send etc. to combine all arguments
// into a string
string concatArgs (lua_State *L, const char * delimiter = "", const int first = 1)
{
int n = lua_gettop(L); /* number of arguments */
int i;
string sOutput;
lua_getglobal(L, "tostring");
for (i = first; i <= n; i++)
{
const char *s;
lua_pushvalue(L, -1); /* function to be called */
lua_pushvalue(L, i); /* value to print */
lua_call(L, 1, 1);
s = lua_tostring(L, -1); /* get result */
if (s == NULL)
luaL_error(L, "'tostring' must return a string to be concatenated");
// add delimiter every time except first time through the loop
if (i > first)
sOutput += delimiter;
sOutput += s;
lua_pop(L, 1); /* pop result */
}
lua_pop(L, 1); /* pop tostring function */
return sOutput;
} // end of concatArgs
static void GetVariableListHelper (lua_State *L, CMUSHclientDoc *pDoc)
{
lua_newtable(L);
for (POSITION pos = pDoc->GetVariableMap ().GetStartPosition(); pos; )
{
CString strVariableName;
CVariable * variable_item;
pDoc->GetVariableMap ().GetNextAssoc (pos, strVariableName, variable_item);
lua_pushstring (L, strVariableName);
lua_pushstring (L, variable_item->strContents);
lua_rawset(L, -3);
} // end of looping through each Variable
} // end of GetVariableListHelper
//----- My checking routines that do not witter on about "bad self" -----
LUALIB_API int my_argerror (lua_State *L, int narg, const char *extramsg) {
lua_Debug ar;
if (!lua_getstack(L, 0, &ar)) /* no stack frame? */
return luaL_error(L, "bad argument #%d (%s)", narg, extramsg);
lua_getinfo(L, "n", &ar);
if (ar.name == NULL)
ar.name = "?";
return luaL_error(L, "bad argument #%d to " LUA_QS " (%s)",
narg, ar.name, extramsg);
}
LUALIB_API int my_typerror (lua_State *L, int narg, const char *tname) {
const char *msg = lua_pushfstring(L, "%s expected, got %s",
tname, luaL_typename(L, narg));
return my_argerror(L, narg, msg);
}
static void my_tag_error (lua_State *L, int narg, int tag) {
my_typerror(L, narg, lua_typename(L, tag));
}
LUALIB_API const char *my_checklstring (lua_State *L, int narg, size_t *len) {
const char *s = lua_tolstring(L, narg, len);
if (!s) my_tag_error(L, narg, LUA_TSTRING);
return s;
}
LUALIB_API const char *my_optlstring (lua_State *L, int narg,
const char *def, size_t *len) {
if (lua_isnoneornil(L, narg)) {
if (len)
*len = (def ? strlen(def) : 0);
return def;
}
else return my_checklstring(L, narg, len);
}
LUALIB_API lua_Number my_checknumber (lua_State *L, int narg) {
lua_Number d = lua_tonumber(L, narg);
if (d == 0 && !lua_isnumber(L, narg)) /* avoid extra test when d is not 0 */
my_tag_error(L, narg, LUA_TNUMBER);
return d;
}
LUALIB_API lua_Number my_optnumber (lua_State *L, int narg, lua_Number def) {
return luaL_opt(L, my_checknumber, narg, def);
}
#define my_checkstring(L,n) (my_checklstring(L, (n), NULL))
#define my_optstring(L,n,d) (my_optlstring(L, (n), (d), NULL))
//-------------- end of special checks --------------------
const bool optboolean (lua_State *L, const int narg, const int def)
{
// that argument not present, take default
if (lua_gettop (L) < narg)
return def;
// nil will default to the default
if (lua_isnil (L, narg))
return def;
// try to convert boolean
if (lua_isboolean (L, narg))
return lua_toboolean (L, narg);
return my_checknumber (L, narg) != 0;
}
//----------------------------------------
// world.Accelerator
//----------------------------------------
static int L_Accelerator (lua_State *L)
{
CMUSHclientDoc *pDoc = doc (L);
lua_pushnumber (L, pDoc->Accelerator (
my_checkstring (L, 1), // Key
my_checkstring (L, 2) // Send
));
return 1; // number of result fields
} // end of L_Accelerator
//----------------------------------------
// world.AcceleratorTo
//----------------------------------------
static int L_AcceleratorTo (lua_State *L)
{
CMUSHclientDoc *pDoc = doc (L);
lua_pushnumber (L, pDoc->AcceleratorTo (
my_checkstring (L, 1), // Key
my_checkstring (L, 2), // Send
my_checknumber (L, 3) // SendTo
));
return 1; // number of result fields
} // end of L_AcceleratorTo
//----------------------------------------
// world.AcceleratorList
//----------------------------------------
static int L_AcceleratorList (lua_State *L)
{
VARIANT v = doc (L)->AcceleratorList ();
return pushVariant (L, v);
} // end of L_AcceleratorList
//----------------------------------------
// world.Activate
//----------------------------------------
static int L_Activate (lua_State *L)
{
doc (L)->Activate ();
return 0; // number of result fields
} // end of L_Activate
//----------------------------------------
// world.ActivateClient
//----------------------------------------
static int L_ActivateClient (lua_State *L)
{
doc (L)->ActivateClient ();
return 0; // number of result fields
} // end of L_ActivateClient
//----------------------------------------
// world.ActivateNotepad
//----------------------------------------
static int L_ActivateNotepad (lua_State *L)
{
CMUSHclientDoc *pDoc = doc (L);
lua_pushboolean (L, pDoc->ActivateNotepad (my_checkstring (L, 1)));
return 1; // number of result fields
} // end of L_ActivateNotepad
//----------------------------------------
// world.AddAlias
//----------------------------------------
static int L_AddAlias (lua_State *L)
{
CMUSHclientDoc *pDoc = doc (L);
lua_pushnumber (L, pDoc->AddAlias (
my_checkstring (L, 1), // AliasName
my_checkstring (L, 2), // MatchText
my_checkstring (L, 3), // ResponseText
my_checknumber (L, 4), // Flags
my_optstring (L, 5, "") // ScriptName - optional
));
return 1; // number of result fields
} // end of L_AddAlias
//----------------------------------------
// world.AddFont
//----------------------------------------
static int L_AddFont (lua_State *L)
{
CMUSHclientDoc *pDoc = doc (L);
lua_pushnumber (L, pDoc->AddFont (
my_checkstring (L, 1) // PathName
));
return 1; // number of result fields
} // end of L_AddFont
//----------------------------------------
// world.AddMapperComment
//----------------------------------------
static int L_AddMapperComment (lua_State *L)
{
CMUSHclientDoc *pDoc = doc (L);
lua_pushnumber (L, pDoc->AddMapperComment (my_checkstring (L, 1)));
return 1; // number of result fields
} // end of L_AddMapperComment
//----------------------------------------
// world.AddSpellCheckWord
//----------------------------------------
static int L_AddSpellCheckWord (lua_State *L)
{
CMUSHclientDoc *pDoc = doc (L);
lua_pushnumber (L, pDoc->AddSpellCheckWord (
my_checkstring (L, 1), // Original word
my_checkstring (L, 2), // Action
my_optstring (L, 3, "") // Replacement word - optional
));
return 1; // number of result fields
} // end of L_AddSpellCheckWord
//----------------------------------------
// world.AddTimer
//----------------------------------------
static int L_AddTimer (lua_State *L)
{
CMUSHclientDoc *pDoc = doc (L);
lua_pushnumber (L, pDoc->AddTimer (
my_checkstring (L, 1), // TimerName
my_checknumber (L, 2), // Hour
my_checknumber (L, 3), // Minute
my_checknumber (L, 4), // Second
my_checkstring (L, 5), // ResponseText
my_checknumber (L, 6), // Flags
my_optstring (L, 7, "") // ScriptName - optional
));
return 1; // number of result fields
} // end of L_AddTimer
//----------------------------------------
// world.AddToMapper
//----------------------------------------
static int L_AddToMapper (lua_State *L)
{
CMUSHclientDoc *pDoc = doc (L);
lua_pushnumber (L, pDoc->AddToMapper (
my_checkstring (L, 1), // Direction
my_optstring (L, 2, "") // Reverse - optional
));
return 1; // number of result fields
} // end of L_AddToMapper
//----------------------------------------
// world.AddTrigger
//----------------------------------------
static int L_AddTrigger (lua_State *L)
{
CMUSHclientDoc *pDoc = doc (L);
lua_pushnumber (L, pDoc->AddTrigger (
my_checkstring (L, 1), // TriggerName
my_checkstring (L, 2), // MatchText
my_checkstring (L, 3), // ResponseText
my_checknumber (L, 4), // Flags
my_checknumber (L, 5), // Colour
my_checknumber (L, 6), // Wildcard
my_checkstring (L, 7), // SoundFileName
my_optstring (L, 8, "") // ScriptName - optional
));
return 1; // number of result fields
} // end of L_AddTrigger
//----------------------------------------
// world.AddTriggerEx
//----------------------------------------
static int L_AddTriggerEx (lua_State *L)
{
CMUSHclientDoc *pDoc = doc (L);
lua_pushnumber (L, pDoc->AddTriggerEx (
my_checkstring (L, 1), // TriggerName
my_checkstring (L, 2), // MatchText
my_checkstring (L, 3), // ResponseText
my_checknumber (L, 4), // Flags
my_checknumber (L, 5), // Colour
my_checknumber (L, 6), // Wildcard
my_checkstring (L, 7), // SoundFileName
my_checkstring (L, 8), // ScriptName
my_checknumber (L, 9), // SendTo
my_optnumber (L, 10, DEFAULT_TRIGGER_SEQUENCE) // Sequence - optional
));
return 1; // number of result fields
} // end of L_AddTriggerEx
//----------------------------------------
// world.AdjustColour
//----------------------------------------
static int L_AdjustColour (lua_State *L)
{
CMUSHclientDoc *pDoc = doc (L);
lua_pushnumber (L, pDoc->AdjustColour (
my_checknumber (L, 1), // Colour
my_checknumber (L, 2) // Method
));
return 1; // number of result fields
} // end of L_AdjustColour
//----------------------------------------
// world.ANSI
//----------------------------------------
static int L_ANSI (lua_State *L)
{
CMUSHclientDoc * pDoc = doc (L); // must do this first
int n = lua_gettop(L); /* number of arguments */
int i;
CString sOutput = "\x1B[";
for (i= 1; i <= n; i++)
{
sOutput += CFormat ("%i", (int) my_checknumber (L, i));
if (i < n)
sOutput += ";";
}
sOutput += "m";
lua_pushstring (L, sOutput);
return 1;
} // end of L_ANSI
//----------------------------------------
// world.AnsiNote
//----------------------------------------
static int L_AnsiNote (lua_State *L)
{
CMUSHclientDoc * pDoc = doc (L); // must do this first
pDoc->AnsiNote (concatArgs (L).c_str ());
// lua_concat (L, lua_gettop (L));
// pDoc->AnsiNote (my_checkstring (L, 1));
return 0; // number of result fields
} // end of L_AnsiNote
//----------------------------------------
// world.AppendToNotepad
//----------------------------------------
static int L_AppendToNotepad (lua_State *L)
{
CMUSHclientDoc * pDoc = doc (L); // must do this first
lua_pushboolean (L, pDoc->AppendToNotepad (
my_checkstring (L, 1), // title
concatArgs (L, "", 2).c_str () // contents
));
// lua_concat (L, lua_gettop (L) - 1);
// lua_pushboolean (L, pDoc->AppendToNotepad (
// my_checkstring (L, 1), // title
// my_checkstring (L, 2) // contents
// ));
return 1; // number of result fields
} // end of L_AppendToNotepad
//----------------------------------------
// world.ArrayClear
//----------------------------------------
static int L_ArrayClear (lua_State *L)
{
CMUSHclientDoc *pDoc = doc (L);
lua_pushnumber (L, pDoc->ArrayClear (my_checkstring (L, 1)));
return 1; // number of result fields
} // end of L_ArrayClear
//----------------------------------------
// world.ArrayCount
//----------------------------------------
static int L_ArrayCount (lua_State *L)
{
lua_pushnumber (L, doc (L)->ArrayCount ());
return 1; // number of result fields
} // end of L_ArrayCount
//----------------------------------------
// world.ArrayCreate
//----------------------------------------
static int L_ArrayCreate (lua_State *L)
{
CMUSHclientDoc *pDoc = doc (L);
lua_pushnumber (L, pDoc->ArrayCreate (my_checkstring (L, 1)));
return 1; // number of result fields
} // end of L_ArrayCreate
//----------------------------------------
// world.ArrayDelete
//----------------------------------------
static int L_ArrayDelete (lua_State *L)
{
CMUSHclientDoc *pDoc = doc (L);
lua_pushnumber (L, pDoc->ArrayDelete (my_checkstring (L, 1)));
return 1; // number of result fields
} // end of L_ArrayDelete
//----------------------------------------
// world.ArrayDeleteKey
//----------------------------------------
static int L_ArrayDeleteKey (lua_State *L)
{
CMUSHclientDoc *pDoc = doc (L);
lua_pushnumber (L, pDoc->ArrayDeleteKey (
my_checkstring (L, 1), // Name
my_checkstring (L, 2) // Key
));
return 1; // number of result fields
} // end of L_ArrayDeleteKey
//----------------------------------------
// world.ArrayExists
//----------------------------------------
static int L_ArrayExists (lua_State *L)
{
CMUSHclientDoc *pDoc = doc (L);
lua_pushboolean (L, pDoc->ArrayExists (my_checkstring (L, 1)));
return 1; // number of result fields
} // end of L_ArrayExists
//----------------------------------------
// world.ArrayExport
//----------------------------------------
static int L_ArrayExport (lua_State *L)
{
CMUSHclientDoc *pDoc = doc (L);
VARIANT v = pDoc->ArrayExport (
my_checkstring (L, 1), // Name
my_optstring (L, 2, ",") // Delimiter
);
return pushVariant (L, v);
} // end of L_ArrayExport
//----------------------------------------
// world.ArrayExportKeys
//----------------------------------------
static int L_ArrayExportKeys (lua_State *L)
{
CMUSHclientDoc *pDoc = doc (L);
VARIANT v = pDoc->ArrayExportKeys (
my_checkstring (L, 1), // Name
my_optstring (L, 2, ",") // Delimiter
);
return pushVariant (L, v);
} // end of L_ArrayExportKeys
//----------------------------------------
// world.ArrayGet
//----------------------------------------
static int L_ArrayGet (lua_State *L)
{
CMUSHclientDoc *pDoc = doc (L);
VARIANT v = pDoc->ArrayGet (
my_checkstring (L, 1), // Name
my_checkstring (L, 2) // Key
);
return pushVariant (L, v);
} // end of L_ArrayGet
//----------------------------------------
// world.ArrayGetFirstKey
//----------------------------------------
static int L_ArrayGetFirstKey (lua_State *L)
{
CMUSHclientDoc *pDoc = doc (L);
VARIANT v = pDoc->ArrayGetFirstKey (my_checkstring (L, 1));
return pushVariant (L, v);
} // end of L_ArrayGetFirstKey
//----------------------------------------
// world.ArrayGetLastKey
//----------------------------------------
static int L_ArrayGetLastKey (lua_State *L)
{
CMUSHclientDoc *pDoc = doc (L);
VARIANT v = pDoc->ArrayGetLastKey (my_checkstring (L, 1));
return pushVariant (L, v);
} // end of L_ArrayGetLastKey
//----------------------------------------
// world.ArrayImport
// - extension - takes an optional table as 2nd argument
//----------------------------------------
static int L_ArrayImport (lua_State *L)
{
CMUSHclientDoc * pDoc = doc (L); // must do this first
string name = my_checkstring (L, 1);
const int table = 2; // 2nd argument may be table
// extension here - accept table as 2nd argument
if (lua_istable (L, table))
{
tStringMapOfMaps::iterator it = pDoc->GetArrayMap ().find (name);
// find table
if (it == pDoc->GetArrayMap ().end ())
{
lua_pushnumber (L, eArrayDoesNotExist);
return 1;
}
int iDuplicates = 0;
// standard Lua table iteration
for (lua_pushnil (L); lua_next (L, table) != 0; lua_pop (L, 1))
{
if (lua_type (L, -2) != LUA_TSTRING)
luaL_error (L, "table must have string keys");
// get key and value
string sKey = lua_tostring (L, -2);
string sValue = lua_tostring (L, -1);
// insert into array
pair<tStringToStringMap::iterator, bool> status =
it->second->insert (make_pair (sKey, sValue));
// check if already there
if (!status.second)
{
status.first->second = sValue;
iDuplicates++;
}
} // end of looping through table
if (iDuplicates)
lua_pushnumber (L, eImportedWithDuplicates);
else
lua_pushnumber (L, eOK);
return 1; // one result, which is error code
} // end of table for argument 2
// not a table - normal string with delimiters
lua_pushnumber (L, pDoc->ArrayImport (
name.c_str (), // Name
my_checkstring (L, 2), // Values
my_optstring (L, 3, ",") // Delimiter
));
return 1; // number of result fields
} // end of L_ArrayImport
//----------------------------------------
// world.ArrayKeyExists
//----------------------------------------
static int L_ArrayKeyExists (lua_State *L)
{
CMUSHclientDoc *pDoc = doc (L);
lua_pushboolean (L, pDoc->ArrayKeyExists (
my_checkstring (L, 1), // Name
my_checkstring (L, 2) // Key
));
return 1; // number of result fields
} // end of L_ArrayKeyExists
//----------------------------------------
// world.ArrayListAll
//----------------------------------------
static int L_ArrayListAll (lua_State *L)
{
VARIANT v = doc (L)->ArrayListAll ();
return pushVariant (L, v);
} // end of L_ArrayListAll
//----------------------------------------
// world.ArrayListKeys
//----------------------------------------
static int L_ArrayListKeys (lua_State *L)
{
CMUSHclientDoc *pDoc = doc (L);
VARIANT v = pDoc->ArrayListKeys (my_checkstring (L, 1));
return pushVariant (L, v);
} // end of L_ArrayListKeys
//----------------------------------------
// world.ArrayListValues
//----------------------------------------
static int L_ArrayListValues (lua_State *L)
{
CMUSHclientDoc *pDoc = doc (L);
VARIANT v = pDoc->ArrayListValues (my_checkstring (L, 1));
return pushVariant (L, v);
} // end of L_ArrayListValues
//----------------------------------------
// world.ArrayList - Lua only
//----------------------------------------
static int L_ArrayList (lua_State *L)
{
CMUSHclientDoc * pDoc = doc (L); // must do this first
tStringMapOfMaps::iterator it = pDoc->GetArrayMap ().find (my_checkstring (L, 1));
if (it == pDoc->GetArrayMap ().end ())
return 0; // not found, no results
lua_newtable(L);
for (tStringToStringMap::iterator i = it->second->begin ();
i != it->second->end ();
i++)
{
lua_pushstring (L, i->first.c_str ());
lua_pushstring (L, i->second.c_str ());
lua_rawset(L, -3);
}
return 1; // one table
} // end of L_ArrayList
//----------------------------------------
// world.ArraySet
//----------------------------------------
static int L_ArraySet (lua_State *L)
{
CMUSHclientDoc *pDoc = doc (L);
lua_pushnumber (L, pDoc->ArraySet (
my_checkstring (L, 1), // Name
my_checkstring (L, 2), // Key
my_checkstring (L, 3) // Value
));
return 1; // number of result fields
} // end of L_ArraySet
//----------------------------------------
// world.ArraySize
//----------------------------------------
static int L_ArraySize (lua_State *L)
{
CMUSHclientDoc *pDoc = doc (L);
lua_pushnumber (L, pDoc->ArraySize (my_checkstring (L, 1)));
return 1; // number of result fields
} // end of L_ArraySize
//----------------------------------------
// world.Base64Decode
//----------------------------------------
static int L_Base64Decode (lua_State *L)
{
CMUSHclientDoc *pDoc = doc (L);
VARIANT v = pDoc->Base64Decode (my_checkstring (L, 1));
return pushVariant (L, v);
} // end of L_Base64Decode
//----------------------------------------
// world.Base64Encode
//----------------------------------------
static int L_Base64Encode (lua_State *L)
{
CMUSHclientDoc *pDoc = doc (L);
VARIANT v = pDoc->Base64Encode (
my_checkstring (L, 1), // Text
optboolean (L, 2, 0) // MultiLine
);
return pushVariant (L, v);
} // end of L_Base64Encode
//----------------------------------------
// world.BlendPixel
//----------------------------------------
static int L_BlendPixel (lua_State *L)
{
CMUSHclientDoc *pDoc = doc (L);
lua_pushnumber (L, pDoc->BlendPixel (
my_checknumber (L, 1), // Blend
my_checknumber (L, 2), // Base
my_checknumber (L, 3), // Mode
my_optnumber (L, 4, 1) // Opacity
));
return 1; // number of result fields
} // end of L_BlendPixel
//----------------------------------------
// world.GetBoldColour
//----------------------------------------
static int L_GetBoldColour (lua_State *L)
{
CMUSHclientDoc *pDoc = doc (L);
lua_pushnumber (L, pDoc->GetBoldColour (my_checknumber (L, 1)));
return 1; // number of result fields
} // end of GetBoldColour
//----------------------------------------
// world.SetBoldColour
//----------------------------------------
static int L_SetBoldColour (lua_State *L)
{
CMUSHclientDoc *pDoc = doc (L);
pDoc->SetBoldColour (
my_checknumber (L, 1), // WhichColour
my_checknumber (L, 2) // nNewValue
);
return 0; // number of result fields
} // end of SetBoldColour
//----------------------------------------
// world.BroadcastPlugin
//----------------------------------------
static int L_BroadcastPlugin (lua_State *L)
{
CMUSHclientDoc *pDoc = doc (L);
lua_pushnumber (L, pDoc->BroadcastPlugin (
my_checknumber (L, 1), // PluginID
my_optstring (L, 2, "") // Argument - optional
));
return 1; // number of result fields
} // end of L_BroadcastPlugin
//----------------------------------------
// world.CallPlugin
//----------------------------------------
static int L_CallPlugin (lua_State *L)
{
CMUSHclientDoc *pDoc = doc (L);
lua_pushnumber (L, pDoc->CallPlugin (
my_checkstring (L, 1), // PluginID
my_checkstring (L, 2), // Routine
my_optstring (L, 3, "") // Argument - optional
));
return 1; // number of result fields
} // end of L_CallPlugin
//----------------------------------------
// world.ChangeDir
//----------------------------------------
static int L_ChangeDir (lua_State *L)
{
CMUSHclientDoc *pDoc = doc (L);
lua_pushnumber (L, pDoc->ChangeDir (
my_checkstring (L, 1) // Directory
));
return 1; // number of result fields
} // end of L_ChangeDir
//----------------------------------------
// world.ChatAcceptCalls
//----------------------------------------
static int L_ChatAcceptCalls (lua_State *L)
{
CMUSHclientDoc *pDoc = doc (L);
lua_pushnumber (L, pDoc->ChatAcceptCalls (my_optnumber (L, 1, DEFAULT_CHAT_PORT)));
return 1; // number of result fields
} // end of L_ChatAcceptCalls
//----------------------------------------
// world.ChatCall
//----------------------------------------
static int L_ChatCall (lua_State *L)
{
CMUSHclientDoc *pDoc = doc (L);
lua_pushnumber (L, pDoc->ChatCall (
my_checkstring (L, 1), // Server
my_optnumber (L, 2, DEFAULT_CHAT_PORT)
));
return 1; // number of result fields
} // end of L_ChatCall