-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathace.py
executable file
·2283 lines (1841 loc) · 77 KB
/
ace.py
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
#!/usr/bin/env python
# ACE - ASSS C Enricher - Generates full modules from pattern specifications for "a small subspace server"
# version beta 2
# Copyright (C) 2010-2011 Justin M. Schwartz ("Arnk Kilo Dylie")
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/gpl-2.0.html>.
import sys, re
from cStringIO import StringIO
from optparse import OptionParser
class ProcessingException(Exception):
def __init__(self, processor, value):
self.processor = processor
self.value = value
def message(self):
return self.processor.filename + ':' + str(self.processor.current_line) + ': error: ' + self.value
def __str__(self):
return repr(self.message())
class Processor:
DirectiveEx = re.compile(r'^\$#(\w+)(.*)\s*?$')
InlineEx = re.compile(r'^(\s*)\$(\w+)\((.*)\)[;]?(.*)\s*$')
InterfaceUsageEx = re.compile(r'(\w+)->\w+?\(')
StringEx = re.compile(r'\s*(".*")\s*')
CPreprocessorEx = re.compile(r'^#(include|define) (\S+)[ ]?(.+)?\s*')
NoParamEx = re.compile(r'^\s*$')
##directive: adviser
# Defines an adviser and begins an adviser block.
#End the block with $#endadviser
# Inside the block, functions should be specified in the order as they are
#listed in the adviser-type definition (see the relevant header file.)
# For functions that are not implemented, use $null() in place of a function.
# The adviser that is created will automatically be registered and unregistered
#in the module code.
# For more information on advisers, see
#http://bitbucket.org/grelminar/asss/wiki/Adviser
##param: scope: global or arena. with global, registers this adviser on load for
#all arenas. with arena, registers this adviser on attach to an arena.
##param: adviserIdOrType1: either the struct type (for example: Appk) or the adviser
#identifier (for example: A_PPK)
# this parameter expects typenames of the form Afoo or identifiers of the
#form A_FOO.
# if of neither form, this directive will assume that it is a typename
##[param]: adviserIdOrType2: if the struct type is specified for
#adviserIdOrType1, then this is the adviser identifier.
# if the adviser identifier is specified for adviserIdOrType1, then this is the
#struct type.
# if adviserIdOrType1 defaulted from not using the standard naming conventions,
#and adviserIdOrType2 is not provided, it will default to A_FOO, where FOO is
#the uppercase representation of the typename given in adviserIdOrType1
##
AdviserParamEx = re.compile(r'^ (\w+) ([^ ]+)[ ]?([^ ]+)?$')
def handleAdviser(processor, module, params):
paramMatch = Processor.AdviserParamEx.match(params)
if not paramMatch:
raise ProcessingException(processor, 'syntax error in $#adviser')
scope = paramMatch.group(1)
if scope <> 'global' and scope <> 'arena':
raise ProcessingException(processor,
'expected: "global" or "arena" as first parameter to $#adviser')
advIdOrType1 = paramMatch.group(2)
advId = None
advType = None
outOfConvention = False
if advIdOrType1[0:2] == 'A_':
advId = advIdOrType1
elif advIdOrType1[0] == 'A':
advType = advIdOrType1
else:
advType = advIdOrType1
outOfConvention = True
advIdOrType2 = paramMatch.group(3)
if advIdOrType2:
if advId:
advType = advIdOrType2
else:
advId = advIdOrType2
else:
if outOfConvention:
advId = 'A_' + advType.upper()
elif advType:
advId = 'A_' + advType[1:].upper()
elif advId:
advType = 'A' + advId[2:].lower()
processor.active_adviser = module.createAdviser(scope, advType, advId)
processor.active_adviser.file = processor.filename
processor.active_adviser.line_number = processor.current_line
return 'endadviser' # return the expected follow up directive
##directive: endadviser
# Closes an adviser block opened by $#adviser
##
def handleEndadviser(processor, module, params):
if not processor.active_adviser:
raise ProcessingException(processor, 'unexpected $#endadviser')
noParams = Processor.NoParamEx.match(params)
if not noParams:
raise ProcessingException(processor,
'syntax error: unexpected paramaters in $#endadviser')
processor.active_adviser = None
return None
##directive: arenadata
# Defines the per-arena-data structure for the module; starts a structure block.
#End the block with $#endarenadata
# Inside the block, define structure fields normally.
# Only one arena data structure may be defined in a module, multiple arenadata
#directives just add extra members to the same struct.
# To access per-player-data inside of a function, use the $usearenadata()
#expansion function.
##[param]: type: static or dynamic. by default, static (except as below:)
# if arena data is already defined (through use of dependencies or $lock
#included), the default is to not change whether it is static or dynamic.
# dynamic arena data uses a mechanism to only allocate the full size of the
#struct for the arena when the module is attached. the resulting module will be
#somewhat more complicated.
# this method is especially recommended for modules with very large arena data
#structs, because asss will limited the total number of bytes used for all
#per-arena-data (using the undocumented global setting on load
#General:PerArenaBytes, with a default of 10000.) ACE avoids this problem using
#a wrapper struct.
##
ArenadataParamEx = re.compile(r'^[ ]?(\w+)?$')
def handleArenadata(processor, module, params):
if processor.active_structure:
raise ProcessingException(processor, 'unexpected $#arenadata')
paramMatch = Processor.ArenadataParamEx.match(params)
if not paramMatch:
raise ProcessingException(processor, 'syntax error in $#arenadata')
type = paramMatch.group(1)
if type and type <> 'static' and type <> 'dynamic':
raise ProcessingException(processor,
'syntax error in $#arenadata: expected: "static" or "dynamic" or nothing as the only parameter')
if type == 'static' or (not type and not module.per_arena_data):
module.setupArenaData(dynamic=False)
elif type == 'dynamic':
module.setupArenaData(dynamic=True)
processor.active_structure = module.per_arena_data
return 'endarenadata' # return the expected follow up directive
##directive: endarenadata
# Closes a structure block opened by $#arenadata
##
def handleEndarenadata(processor, module, params):
noParams = Processor.NoParamEx.match(params)
if not noParams:
raise ProcessingException(processor,
'syntax error: unexpected paramaters in $#endarenadata')
processor.active_structure = None
return None
##directive: attach
# Defines a code block for extra code to use during MM_ATTACH.
#End the block with $#endattach
##[param]: type: first or last. by default, first.
# "first" code is executed before callbacks, advisers, commands, and dynamic
#per-player-data is set up for the arena. use $failattach() in an "attach-first"
#block only.
# "last" code is executed after callbacks, advisers, commands, and dynamic
#per-player-data is set up for the arena. however, it is still executed before
#interface implementations are registered for the arena. $failattach() is not
#allowed in an "attach-last" block.
##
AttachParamEx = re.compile(r'^[ ]?(\w+)?$')
def handleAttach(processor, module, params):
paramMatch = Processor.AttachParamEx.match(params)
if not paramMatch:
raise ProcessingException(processor,
'syntax error in $#attach: expected "first" or "last" or nothing as the only parameter')
type = paramMatch.group(1)
if type and type == 'last':
processor.active_extrablock = module.extra_attachlast_code
else:
processor.active_extrablock = module.extra_attachfirst_code
processor.active_extrablock.write('\t\t/* extra-attach block */ {\n')
module.force_attach = True
return 'endattach' # return the expected follow up directive
##directive: endattach
# Closes an extra-code block opened by $#attach
##
def handleEndattach(processor, module, params):
if not processor.active_extrablock \
or (processor.active_extrablock <> module.extra_attachlast_code
and processor.active_extrablock <> module.extra_attachfirst_code):
raise ProcessingException(processor, 'unexpected $#endattach')
noParams = Processor.NoParamEx.match(params)
if not noParams:
raise ProcessingException(processor,
'syntax error: unexpected paramaters in $#endattach')
processor.active_extrablock.write('\t\t}\n')
processor.active_extrablock = None
return None
##directive: callback
# Defines a callback and begins an callback block.
#End the block with $#endcallback
# Inside the block, define one function that is to be the registered callback
#function.
# The callback will automatically be registered and unregistered
#in the module code.
# For more information on callbacks, see
#http://bitbucket.org/grelminar/asss/wiki/Callback
##param: scope: global or arena. with global, registers this callback on load
#for all arenas. with arena, registers this callback on attach to an arena.
##param: callbackId: the callback type identifier (for example: CB_PLAYERACTION)
##
CallbackParamEx = re.compile(r'^ (\w+) (\w+)$')
def handleCallback(processor, module, params):
paramMatch = Processor.CallbackParamEx.match(params)
if not paramMatch:
raise ProcessingException(processor, 'syntax error in $#callback')
scope = paramMatch.group(1)
if scope <> 'global' and scope <> 'arena':
raise ProcessingException(processor,
'expected: "global" or "arena" as first parameter to $#callback')
cbId = paramMatch.group(2)
processor.active_callback = module.createCallback(scope, cbId)
processor.active_callback.file = processor.filename
processor.active_callback.line_number = processor.current_line
return 'endcallback' # return the expected follow up directive
##directive: endcallback
# Closes a callback block opened by $#callback
##
def handleEndcallback(processor, module, params):
if not processor.active_callback:
raise ProcessingException(processor, 'unexpected $#endcallback')
noParams = Processor.NoParamEx.match(params)
if not noParams:
raise ProcessingException(processor,
'syntax error: unexpected paramaters in $#endcallback')
if not processor.active_callback.function:
raise ProcessingException(processor, 'empty callback block')
processor.active_callback = None
return None
##directive: command
# Defines a command and begins a command block.
#End the block with $#endcommand
# Inside the block, define one function that is to be the registered callback
#function.
# You may also place string literals outside of the function block, these will
#be added to the help text for the command.
# The command will automatically be registered and unregistered
#in the module code.
##param: scope: global or arena. with global, registers this command on load
#for all arenas. with arena, registers this command on attach to an arena.
##param: names: the names of the command, separated by commas.
# for example the value "money,funds,m" creates one command with the main name
#?money, and aliases ?funds and ?m
##
CommandParamEx = re.compile(r'^ (\w+) (.+)$')
def handleCommand(processor, module, params):
paramMatch = Processor.CommandParamEx.match(params)
if not paramMatch:
raise ProcessingException(processor, 'syntax error in $#command')
scope = paramMatch.group(1)
if scope <> 'global' and scope <> 'arena':
raise ProcessingException(processor,
'expected: "global" or "arena" as first parameter to $#command')
names = paramMatch.group(2)
processor.active_command = module.createCommand(scope, names)
processor.active_command.file = processor.filename
processor.active_command.line_number = processor.current_line
return 'endcommand' # return the expected follow up directive
##directive: endcommand
# Closes a command block opened by $#command
##
def handleEndcommand(processor, module, params):
if not processor.active_command:
raise ProcessingException(processor, 'unexpected $#endcommand')
noParams = Processor.NoParamEx.match(params)
if not noParams:
raise ProcessingException(processor,
'syntax error: unexpected paramaters in $#endcommand')
processor.active_command = None
return None
##directive: detach
# Defines a code block for extra code to use during MM_DETACH.
#End the block with $#enddetach
##[param]: type: first or last. by default, last.
# "first" code is executed after interfaces are unregistered, but before
#any other actions. "first" code is not called after a $failattach() directive.
# "last" code is executed after everything is cleaned up except dependency
#pointers and per-arena-data. dynamic per-player-data will already be freed by
#this point. this code will be called after a $failattach() directive.
##
DetachParamEx = re.compile(r'^[ ]?(\w+)?$')
def handleDetach(processor, module, params):
paramMatch = Processor.DetachParamEx.match(params)
if not paramMatch:
raise ProcessingException(processor,
'syntax error in $#detach: expected "first" or "last" or nothing as the only parameter')
type = paramMatch.group(1)
if type and type == 'first':
processor.active_extrablock = module.extra_detachfirst_code
else:
processor.active_extrablock = module.extra_detachlast_code
processor.active_extrablock.write('\t\t/* extra-detach block */ {\n')
module.force_attach = True
return 'enddetach' # return the expected follow up directive
##directive: enddetach
# Closes an extra-code block opened by $#detach
##
def handleEnddetach(processor, module, params):
if not processor.active_extrablock \
or (processor.active_extrablock <> module.extra_detachlast_code
and processor.active_extrablock <> module.extra_detachfirst_code):
raise ProcessingException(processor,
'unexpected $#enddetach')
noParams = Processor.NoParamEx.match(params)
if not noParams:
raise ProcessingException(processor,
'syntax error: unexpected paramaters in $#enddetach')
processor.active_extrablock.write('\t\t}\n')
processor.active_extrablock = None
return None
##directive: implement
# Defines an interface implementation, and begins an implementation block.
#End the block with $#endimplement
# Inside the block, functions should be specified in the order as they are
#listed in the interface-type definition (see the relevant header file.)
# The interface implementation that is created will automatically be registered
#and unregistered in the module code.
# For more information on interfaces, see
#http://bitbucket.org/grelminar/asss/wiki/Interface
##param: scope: global or arena. with global, registers this interface
#implementation on load for all arenas. with arena, registers this
#implementation on attach to an arena.
##param: interfaceTypeOrId1: either the struct type (for example: Ichat) or the
#interface identifier (for example: I_CHAT)
# this parameter expects typenames of the form Ifoo or identifiers of the
#form I_FOO.
# if of neither form, this directive will assume that it is a typename
##[param]: interfaceTypeOrId2: if the struct type is specified for
#interfaceTypeOrId1, then this is the interface identifier.
# if the interface identifier is specified for interfaceTypeOrId1, then this is
#the struct type.
# if not specified, defaults to typenames of the form I_FOO (where Ifoo is a
#standard type name), and Ifoo (where I_FOO is a standard interface id.)
# if interfaceTypeOrId1 was not a standard type name, it will default to
#I_FOO, where FOO is interfaceTypeOrId1.
##[param]: implementationName: the name of the implementation. this is only
#presently used in asss for the rare instances of mm->GetInterfaceByName.
#by default, the name is foo-moduleName where foo is the the lowercase interface
#type (sans a leading I if of the form Ifoo)
##
ImplementParamEx = re.compile(r'^ (\w+) ([^ ]+)[ ]?([^ ]+)?[ ]?(.+)?$')
def handleImplement(processor, module, params):
paramMatch = Processor.ImplementParamEx.match(params)
if not paramMatch:
raise ProcessingException(processor, 'syntax error in $#implement')
scope = paramMatch.group(1)
if scope <> 'global' and scope <> 'arena':
raise ProcessingException(processor,
'expected: "global" or "arena" as first parameter to $#implement')
intTypeOrId1 = paramMatch.group(2)
if not intTypeOrId1:
raise ProcessingException(processor,
'expected: second paramater for interface type or id in $#implement')
intType = None
intId = None
outOfConvention = False
if intTypeOrId1[0:2] == 'I_':
intId = intTypeOrId1
elif intTypeOrId1[0] == 'I':
intType = intTypeOrId1
else:
outOfConvention = True
intType = intTypeOrId1
intTypeOrId2 = paramMatch.group(3)
if intTypeOrId2:
if intId:
intType = intTypeOrId2
else:
intId = intTypeOrId2
else:
if outOfConvention:
intId = 'I_' + intType.upper()
elif intType:
intId = 'I_' + intType[1:].upper()
else:
intType = 'I' + intId[2:].lower()
intName = paramMatch.group(4)
if not intName:
if outOfConvention:
intName = intType.lower() + '-' + module.name
else:
intName = intType[1:].lower() + '-' + module.name
processor.active_interface = module.createImplementation(scope, \
intType, intId, intName)
processor.active_interface.file = processor.filename
processor.active_interface.line_number = processor.current_line
return 'endimplement' # return the expected follow up directive
##directive: endimplement
# Closes an implementation block opened by $#implement
##
def handleEndimplement(processor, module, params):
if not processor.active_interface:
raise ProcessingException(processor, 'unexpected $#endimplement')
noParams = Processor.NoParamEx.match(params)
if not noParams:
raise ProcessingException(processor,
'syntax error: unexpected paramaters in $#endimplement')
processor.active_interface = None
return None
##directive: load
# Defines a code block for extra code to use during MM_LOAD.
#End the block with $#endload
##[param]: type: first or last. by default, first.
# "first" code is executed before callbacks, advisers, and commands are set up.
#use $failload() in a "load-first" block only.
# "last" code is executed after callbacks, advisers, and commands are set up.
#however, it is still executed before interface implementations are registered.
#$failload() is not allowed in a "load-last" block.
##
LoadParamEx = re.compile(r'^[ ]?(\w+)?$')
def handleLoad(processor, module, params):
paramMatch = Processor.LoadParamEx.match(params)
if not paramMatch:
raise ProcessingException(processor,
'syntax error in $#load: expected "first" or "last" or nothing as the only parameter')
type = paramMatch.group(1)
if type and type == 'last':
processor.active_extrablock = module.extra_loadlast_code
else:
processor.active_extrablock = module.extra_loadfirst_code
processor.active_extrablock.write('\t\t/* extra-load block */ {\n')
return 'endload' # return the expected follow up directive
##directive: endload
# Closes an extra-code block opened by $#load
##
def handleEndload(processor, module, params):
if not processor.active_extrablock \
or (processor.active_extrablock <> module.extra_loadlast_code
and processor.active_extrablock <> module.extra_loadfirst_code):
raise ProcessingException(processor, 'unexpected $#endload')
noParams = Processor.NoParamEx.match(params)
if not noParams:
raise ProcessingException(processor,
'syntax error: unexpected paramaters in $#endload')
processor.active_extrablock.write('\t\t}\n')
processor.active_extrablock = None
return None
##directive: module
# Defines the name of the module. There must be a $#module directive on the
#first line of an ACE module, and there may only be one $#module directive in
#the module.
##param: name: the name of the module. this will appear in log entries generated
#by the module, and also create a constant MODULE_NAME. must use alphanumeric
#characters and _ only.
##
ModuleParamEx = re.compile(r'^ (\w+)$')
def handleModule(processor, module, params):
if module.name:
raise ProcessingException(processor,
'unexpected $#module, $#module is only allowed on first line of module')
paramMatch = Processor.ModuleParamEx.match(params)
if not paramMatch:
raise ProcessingException(processor, 'syntax error in $#module: requires alphanumeric name as only parameter')
module.name = paramMatch.group(1)
return None
##directive: playerdata
# Defines the per-player-data structure for the module; starts a structure
#block. End the block with $#endplayerdata
# Inside the block, define structure fields normally.
# Only one player data structure may be defined in a module, multiple playerdata
#directives just add extra members to the same struct.
# To access per-player-data inside of a function, use the $useplayerdata()
#expansion function.
##[param]: type: static or dynamic. by default, static. if player data is
#already defined, and this parameter is not specified, it will not change
#whether it is static or dynamic.
# dynamic player data uses a mechanism to only allocate the full size of the
#struct for the player when the player is in an arena the module is attached in.
# this method is especially recommended for modules with very large player data
#structs, because asss will limited the total number of bytes used for all
#per-player-data (using the undocumented global setting on load
#General:PerPlayerBytes, with a default of 4000) ACE avoids this problem using
#a wrapper struct.
##
PlayerdataParamEx = re.compile(r'^[ ]?(\w+)?$')
def handlePlayerdata(processor, module, params):
if processor.active_structure:
raise ProcessingException(processor, 'unexpected $#playerdata')
paramMatch = Processor.PlayerdataParamEx.match(params)
if not paramMatch:
raise ProcessingException(processor, 'syntax error in $#playerdata')
type = paramMatch.group(1)
if type and type <> 'static' and type <> 'dynamic':
raise ProcessingException(processor,
'syntax error in $#playerdata: expected: "static" or "dynamic" or nothing as the only parameter')
if type == 'static' or (not type and not module.per_player_data):
module.setupPlayerData(dynamic=False)
else:
module.setupPlayerData(dynamic=True)
processor.active_structure = module.per_player_data
return 'endplayerdata' # return the expected follow up directive
##directive: endplayerdata
# Closes a structure block opened by $#playerdata
##
def handleEndplayerdata(processor, module, params):
noParams = Processor.NoParamEx.match(params)
if not noParams:
raise ProcessingException(processor,
'syntax error: unexpected paramaters in $#endplayerdata')
processor.active_structure = None
return None
##directive: require
# Requires a certain interface to be registered, or a certain interface
#implementation (dependency), and creates a pointer to the interface.
# If the interface is not available, the module will fail to load (or attach.)
# For optional interfaces, the $#use directive should be used.
##param: scope: global or arena.
# with global, requests the interface registered for ALLARENAS, and stores the
#pointer as a global.
# for arena, requests the interface on attach registered to the arena, and
#stores the pointer in per-arena-data (access with $usearenadata())
##param: interfaceType: the struct name of the interface. (for example Ichat)
##[param]: pointerName: the variable name to use for the pointer to the
#interface. by default, is foo for a type named Ifoo, or _bar for any other type
#named bar.
##[param]: interfaceIdOrName: the interface identifier or implementation name.
#(for example I_CHAT).
# by default, is derived from the interface type. I_FOO is assumed for types of
#the form Ifoo, and I_BAR is assumed for any other types of the form bar.
#expects identifiers to be in the form I_FOO, otherwise it will assume this is
#an implementation name.
##[param]: interfaceName: the requested implementation name. this is only for
#advanced uses of interfaces.
# if interfaceIdOrName is a name, using this parameter is not allowed.
##
RequireParamEx = re.compile(r'^ (\w*) (\w*)[ ]?(\w+)?[ ]?(.+)?[ ]?(.+)?$')
def handleRequire(processor, module, params):
paramMatch = Processor.RequireParamEx.match(params)
if not paramMatch:
raise ProcessingException(processor, 'syntax error in $#require')
scope = paramMatch.group(1)
if scope <> 'global' and scope <> 'arena':
raise ProcessingException(processor,
'expected: "global" or "arena" as first parameter to $#require')
intType = paramMatch.group(2)
pointer = paramMatch.group(3)
if not pointer:
pointer = intType[1:]
intIdOrName = paramMatch.group(4)
intId = None
intName = None
if not intIdOrName:
if intType[0] == 'I':
intId = 'I_' + intType[1:].upper()
else:
intId = 'I_' + intType.upper()
else:
if intIdOrName[0:2] == 'I_':
intId = intIdOrName
else:
intName = intIdOrName
intNameParam = paramMatch.group(5)
if intNameParam:
if intName:
raise ProcessingException(processor,
'interface name specified twice in $#require')
intName = intNameParam
dep = module.createDependency(scope, intType, pointer, intId, intName,
False, processor.filename, processor.current_line)
return None
##directive: unload
# Defines a code block for extra code to use during MM_UNLOAD.
#End the block with $#endunload
##[param]: type: first or last. by default, last.
# "first" code is executed after interfaces are unregistered, but before
#any other actions. "first" code is not called after a $failload() directive.
# "last" code is executed after everything is cleaned up except dependency
#pointers, per-player-data and per-arena-data. this code will be called after a
#$failload() directive.
##
UnloadParamEx = re.compile(r'^[ ]?(\w+)?$')
def handleUnload(processor, module, params):
paramMatch = Processor.UnloadParamEx.match(params)
if not paramMatch:
raise ProcessingException(processor,
'syntax error in $#unload: expected "first" or "last" or nothing as the only parameter')
type = paramMatch.group(1)
if type and type == 'first':
processor.active_extrablock = module.extra_unloadfirst_code
else:
processor.active_extrablock = module.extra_unloadlast_code
processor.active_extrablock.write('\t\t/* extra-unload block */ {\n')
return 'endunload' # return the expected follow up directive
##directive: endunload
# Closes an extra-code block opened by $#unload
##
def handleEndunload(processor, module, params):
if not processor.active_extrablock \
or (processor.active_extrablock <> module.extra_unloadlast_code
and processor.active_extrablock <> module.extra_unloadfirst_code):
raise ProcessingException(processor,
'unexpected $#endunload')
noParams = Processor.NoParamEx.match(params)
if not noParams:
raise ProcessingException(processor,
'syntax error: unexpected paramaters in $#endunload')
processor.active_extrablock.write('\t\t}\n')
processor.active_extrablock = None
return None
##directive: use
# Requests a certain interface to be used if registered, or a certain interface
#implementation (dependency), and creates a pointer to the interface.
# If the interface is not
#available, the module will continue loading/attaching as normal, and the
#pointer will be null.
# For interfaces that must be available, use the $#require directive instead.
##param: scope: global or arena.
# with global, requests the interface registered for ALLARENAS, and stores the
#pointer as a global.
# for arena, requests the interface on attach registered to the arena, and
#stores the pointer in per-arena-data (access with $usearenadata())
##param: interfaceType: the struct name of the interface. (for example Ichat)
##[param]: pointerName: the variable name to use for the pointer to the
#interface. by default, is foo for a type named Ifoo, or _bar for any other type
#named bar.
##[param]: interfaceIdOrName: the interface identifier or implementation name.
#(for example I_CHAT).
# by default, is derived from the interface type. I_FOO is assumed for types of
#the form Ifoo, and I_BAR is assumed for any other types of the form bar.
#expects identifiers to be in the form I_FOO, otherwise it will assume this is
#an implementation name.
##[param]: interfaceName: the requested implementation name. this is only for
#advanced uses of interfaces.
# if interfaceIdOrName is a name, using this parameter is not allowed.
##
UseParamEx = re.compile(r'^ (\w*) (\w*)[ ]?(\w+)?[ ]?(.+)?[ ]?(.+)?$')
def handleUse(processor, module, params):
paramMatch = Processor.UseParamEx.match(params)
if not paramMatch:
raise ProcessingException(processor, 'syntax error in $#use')
scope = paramMatch.group(1)
if scope <> 'global' and scope <> 'arena':
raise ProcessingException(processor,
'expected: "global" or "arena" as first parameter to $#use')
intType = paramMatch.group(2)
pointer = paramMatch.group(3)
if not pointer:
pointer = intType[1:]
intIdOrName = paramMatch.group(4)
intId = None
intName = None
if not intIdOrName:
if intType[0] == 'I':
intId = 'I_' + intType[1:].upper()
else:
intId = 'I_' + intType.upper()
else:
if intIdOrName[0:2] == 'I_':
intId = intIdOrName
else:
intName = intIdOrName
intNameParam = paramMatch.group(5)
if intNameParam:
if intName:
raise ProcessingException(processor,
'interface name specified twice in $#use')
intName = intNameParam
dep = module.createDependency(scope, intType, pointer, intId, intName,
True, processor.filename, processor.current_line)
return None
##inline: failattach
# Used inside of an "attach-first" extra-code block. If this point in the code
#is reached, the module will fail to attach and begin cleaning up any resources
#obtained from the arena up to that point.
# Resources obtained during the "attach-first" block should be freed in an
#"detach-last" block.
##[param]: messageFormat: a printf format string that will be used for the
#message recorded to the log for the reason why the module failed to attach.
##[param]: ...: items required to fill in the pieces of the messageFormat string
#(as many as are needed, separated by commas, just like using printf)
##
FailattachParamEx = re.compile(r'^\s*(\"[^"]*"\s*(,.+))?\s*$')
def handleFailattach(processor, module, whitespace, params):
if not processor.active_extrablock \
or processor.active_extrablock <> module.extra_attachfirst_code:
raise ProcessingException(processor,
'$failattach() encountered outside of attach-first block')
paramMatch = Processor.FailattachParamEx.match(params)
if not paramMatch:
raise ProcessingException(processor,
'syntax error in $failattach()')
log = paramMatch.group(1)
result = None
if log:
result = whitespace + 'lm->Log(L_ERROR, "<' + module.name + '> " ' \
+ log + ');\n' + whitespace + 'failedAttach = TRUE;\n' \
+ whitespace + 'goto ace_fail_attach;'
else:
result = whitespace + 'failedAttach = TRUE;\n' + whitespace \
+ 'goto ace_fail_attach;'
module.force_fail_attach_label = True
return result
##inline: failload
# Used inside of a "load-first" extra-code block. If this point in the code is
#reached, the module will fail to load and begin unloading, generally releasing
#any resources it has obtained up to that point.
# Resources obtained during the "load-first" block should be freed in an
#"unload-last" block.
##[param]: messageFormat: a printf format string that will be used for the
#message recorded to the log for the reason why the module failed to load.
##[param]: ...: items required to fill in the pieces of the messageFormat string
#(as many as are needed, separated by commas, just like using printf)
##
FailloadParamEx = re.compile(r'^\s*(\"[^"]*"\s*(,.+))?\s*$')
def handleFailload(processor, module, whitespace, params):
if not processor.active_extrablock \
or processor.active_extrablock <> module.extra_loadfirst_code:
raise ProcessingException(processor,
'$failload() encountered outside of load-first block')
paramMatch = Processor.FailloadParamEx.match(params)
if not paramMatch:
raise ProcessingException(processor,
'syntax error in $failload()')
log = paramMatch.group(1)
result = None
if log:
result = whitespace + 'lm->Log(L_ERROR, "<' + module.name + '> " ' \
+ log + ');\n' + whitespace + 'failedLoad = TRUE;\n' \
+ whitespace + 'goto ace_fail_load;'
else:
result = whitespace + 'failedLoad = TRUE;\n' + whitespace \
+ 'goto ace_fail_load;'
module.force_fail_load_label = True
return result
##inline: lock
# Lock the module's mutex.
# Provides support for an automatic global-level mutex.
# This mutex is recursive, so may be locked multiple times in the same thread
#without blocking. You must call $unlock() an equal number of times to give
#other threads access.
# The mutex is initialized after "load-first" blocks, and may not be accessed
#in load-first blocks. It is destroyed before "unload-last" blocks, and may not
#be accessed in unload-last blocks.
##
def handleLock(processor, module, whitespace, params):
if not processor.function_mode and not processor.active_extrablock:
raise ProcessingException(processor,
'$lock() appeared outside of a function/extra-code block')
if processor.active_extrablock == module.extra_unloadlast_code or \
processor.active_extrablock == module.extra_loadfirst_code:
raise ProcessingException(processor,
'$lock() appeared inside of a load-first or unload-last block')
noParams = Processor.NoParamEx.match(params)
if not noParams:
raise ProcessingException(processor,
'syntax error: unexpected paramaters in $lock()')
module.use_mutex = True
return whitespace + 'pthread_mutex_lock(&ace_mutex);'
##inline: null
# Used to specify an unimplemented adviser function, substituting NULL into the
#adivser struct for the function pointer.
##
def handleNull(processor, module, whitespace, params):
if not processor.active_adviser:
raise ProcessingException(processor,
'$null() appeared outside of an adviser block')
noParams = Processor.NoParamEx.match(params)
if not noParams:
raise ProcessingException(processor,
'syntax error: unexpected paramaters in $null()')
processor.active_adviser.functions.append(None)
return ''
##inline: unlock
# Unlock the module's mutex.
# Provides support for an automatic global-level mutex.
# This mutex is recursive, so may be locked multiple times in the same thread
#without blocking. You must call $unlock() an equal number of times to give
#other threads access.
# The mutex is initialized after "load-first" blocks, and may not be accessed
#in load-first blocks. It is destroyed before "unload-last" blocks, and may not
#be accessed in unload-last blocks.
##
def handleUnlock(processor, module, whitespace, params):
if not processor.function_mode and not processor.active_extrablock:
raise ProcessingException(processor,
'$unlock() appeared outside of a function/extra-code block')
noParams = Processor.NoParamEx.match(params)
if not noParams:
raise ProcessingException(processor,
'syntax error: unexpected paramaters in $unlock()')
module.use_mutex = True
return whitespace + 'pthread_mutex_unlock(&ace_mutex);'
##inline: usearenadata
# Declare a pointer to the arena data struct (defined by $#arenadata, and also
#used to store arena-level interface pointers.)
##param: var: the name of the variable to declare
##param: arena: the pointer to the arena to point to
##
UsearenadataParamEx = re.compile(r'\s*(.*)\s*,\s*(.*)\s*')
def handleUsearenadata(processor, module, whitespace, params):
if not module.per_arena_data:
raise ProcessingException(processor,
'$usearenadata() appeared when per-arena-data is not defined for this module')
paramMatch = Processor.UsearenadataParamEx.match(params)
if not paramMatch:
raise ProcessingException(processor,
'syntax error in $usearenadata()')
return module.per_arena_data.getInvokeCode(paramMatch.group(1),
paramMatch.group(2), whitespace)