-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
Grammar.cs
1151 lines (1007 loc) · 42.5 KB
/
Grammar.cs
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Runtime.ExceptionServices;
using System.Speech.Internal;
using System.Speech.Internal.SrgsCompiler;
using System.Speech.Recognition.SrgsGrammar;
using System.Text;
#pragma warning disable 56500 // Remove all the catch all statements warnings used by the interop layer
namespace System.Speech.Recognition
{
// Class for grammars which are to be loaded from SRGS or CFG.
// In contrast to dictation grammars which inherit from this.
[DebuggerDisplay("Grammar = {(_uri != null ? \"uri=\" + _uri.ToString () + \" \" : \"\") + \"rule=\" + _ruleName }")]
public class Grammar
{
#region Constructors
#pragma warning disable 6504
#pragma warning disable 6507
internal Grammar(Uri uri, string ruleName, object[] parameters)
{
Helpers.ThrowIfNull(uri, nameof(uri));
_uri = uri;
InitialGrammarLoad(ruleName, parameters, false);
}
public Grammar(string path)
: this(path, (string)null, null)
{
}
public Grammar(string path, string ruleName)
: this(path, ruleName, null)
{
}
public Grammar(string path, string ruleName, object[] parameters)
{
try
{
_uri = new Uri(path, UriKind.Relative);
}
catch (UriFormatException e)
{
throw new ArgumentException(SR.Get(SRID.RecognizerGrammarNotFound), nameof(path), e);
}
InitialGrammarLoad(ruleName, parameters, false);
}
public Grammar(SrgsDocument srgsDocument)
: this(srgsDocument, null, null, null)
{
}
public Grammar(SrgsDocument srgsDocument, string ruleName)
: this(srgsDocument, ruleName, null, null)
{
}
public Grammar(SrgsDocument srgsDocument, string ruleName, object[] parameters)
: this(srgsDocument, ruleName, null, parameters)
{
}
[EditorBrowsable(EditorBrowsableState.Never)]
public Grammar(SrgsDocument srgsDocument, string ruleName, Uri baseUri)
: this(srgsDocument, ruleName, baseUri, null)
{
}
[EditorBrowsable(EditorBrowsableState.Never)]
public Grammar(SrgsDocument srgsDocument, string ruleName, Uri baseUri, object[] parameters)
{
Helpers.ThrowIfNull(srgsDocument, nameof(srgsDocument));
_srgsDocument = srgsDocument;
_isSrgsDocument = srgsDocument != null;
_baseUri = baseUri;
InitialGrammarLoad(ruleName, parameters, false);
}
public Grammar(Stream stream)
: this(stream, null, null, null)
{
}
public Grammar(Stream stream, string ruleName)
: this(stream, ruleName, null, null)
{
}
public Grammar(Stream stream, string ruleName, object[] parameters)
: this(stream, ruleName, null, parameters)
{
}
[EditorBrowsable(EditorBrowsableState.Never)]
public Grammar(Stream stream, string ruleName, Uri baseUri)
: this(stream, ruleName, baseUri, null)
{
}
[EditorBrowsable(EditorBrowsableState.Never)]
public Grammar(Stream stream, string ruleName, Uri baseUri, object[] parameters)
{
Helpers.ThrowIfNull(stream, nameof(stream));
if (!stream.CanRead)
{
throw new ArgumentException(SR.Get(SRID.StreamMustBeReadable), nameof(stream));
}
_appStream = stream;
_baseUri = baseUri;
InitialGrammarLoad(ruleName, parameters, false);
}
public Grammar(GrammarBuilder builder)
{
Helpers.ThrowIfNull(builder, nameof(builder));
_grammarBuilder = builder;
InitialGrammarLoad(null, null, false);
}
private Grammar(string onInitParameters, Stream stream, string ruleName)
{
_appStream = stream;
_onInitParameters = onInitParameters;
InitialGrammarLoad(ruleName, null, true);
}
protected Grammar()
{
}
protected void StgInit(object[] parameters)
{
_parameters = parameters;
LoadAndCompileCfgData(false, true);
}
#pragma warning restore 6504
#pragma warning restore 6507
#endregion
#region Public Methods
public static Grammar LoadLocalizedGrammarFromType(Type type, params object[] onInitParameters)
{
Helpers.ThrowIfNull(type, nameof(type));
if (type == typeof(Grammar) || !type.IsSubclassOf(typeof(Grammar)))
{
throw new ArgumentException(SR.Get(SRID.StrongTypedGrammarNotAGrammar), nameof(type));
}
Assembly assembly = Assembly.GetAssembly(type);
foreach (Type typeTarget in assembly.GetTypes())
{
string cultureId = null;
if (typeTarget == type || typeTarget.IsSubclassOf(type))
{
if (typeTarget.GetField("__cultureId") != null)
{
// Get the association table
try
{
cultureId = (string)typeTarget.InvokeMember("__cultureId", BindingFlags.GetField, null, null, null, null);
}
catch (Exception e)
{
if (!(e is System.MissingFieldException))
{
throw;
}
}
if (Helpers.CompareInvariantCulture(new CultureInfo(int.Parse(cultureId, CultureInfo.InvariantCulture)), CultureInfo.CurrentUICulture))
{
try
{
return (Grammar)assembly.CreateInstance(typeTarget.FullName, false, BindingFlags.CreateInstance, null, onInitParameters, null, null);
}
catch (MissingMemberException)
{
throw new ArgumentException(SR.Get(SRID.RuleScriptInvalidParameters, typeTarget.Name, typeTarget.Name));
}
}
}
}
}
return null;
}
#endregion
#region public Properties
// Standard properties to control grammar:
// Controls whether this grammar is actually included in the recognition. True by default. Can be set at any point.
public bool Enabled
{
get { return _enabled; }
set
{
// Note: you can still set or get this property regardless of whether the Grammar is loaded or not.
// In theory we could throw in certain scenarios but this is probably simplest.
if (_grammarState != GrammarState.Unloaded && _enabled != value)
{
_recognizer.SetGrammarState(this, value);
}
_enabled = value; // Only on success
}
}
// Relative weight of this Grammar/Rule.
public float Weight
{
get { return _weight; }
set
{
if (value < 0.0 || value > 1.0)
{
throw new ArgumentOutOfRangeException(nameof(value), SR.Get(SRID.GrammarInvalidWeight));
}
// Note: you can still set or get this property regardless of whether the Grammar is loaded or not.
// In theory we could throw in certain scenarios but this is probably simplest.
if (_grammarState != GrammarState.Unloaded && !_weight.Equals(value))
{
_recognizer.SetGrammarWeight(this, value);
}
_weight = value; // Only on success
}
}
// Priority of this Grammar/Rule.
// If different grammars have paths which match the same words,
// then the result will be returned for the grammar with the highest priority.
// Default value zero {lowest value}.
public int Priority
{
get { return _priority; }
set
{
if (value < -128 || value > 127)
{
// We could have used sbyte in the signature of this property but int is probably simpler.
throw new ArgumentOutOfRangeException(nameof(value), SR.Get(SRID.GrammarInvalidPriority));
}
if (_grammarState != GrammarState.Unloaded && _priority != value)
{
_recognizer.SetGrammarPriority(this, value);
}
_priority = value; // Only on success.
}
}
// Simple property that allows a name to be attached to the Grammar.
// This has no effect but could be convenient for certain apps.
public string Name
{
get { return _grammarName; }
set { _grammarName = value ?? string.Empty; }
}
public string RuleName
{
get { return _ruleName; }
}
public bool Loaded
{
get { return _grammarState == GrammarState.Loaded; }
}
internal Uri Uri
{
get { return _uri; }
}
#endregion
#region public Events
// The event fired upon a recognition.
public event EventHandler<SpeechRecognizedEventArgs> SpeechRecognized;
#endregion
#region Internal Properties
internal IRecognizerInternal Recognizer
{
get { return _recognizer; }
set { _recognizer = value; }
}
// The load-state of the grammar.
// - Set to New by constructor and also kept as New if a synchronous load fails.
// - Set to Loaded when any grammar load completes.
// - Set to Unloaded when a grammar is unloaded from the Recognizer.
// There are two additional states used for async grammar loading:
// - Set to Loading when an Async load is in progress.
// - Set to LoadFailed when an async load fails but the grammar is still in the Grammars collection.
internal GrammarState State
{
get { return _grammarState; }
set
{
Debug.Assert(value >= GrammarState.Unloaded && value <= GrammarState.LoadFailed);
// Check state diagram for State. Possible paths:
// Unloaded -> Loaded -> Unloaded {LoadGrammar succeeded}.
// Unloaded {LoadGrammar failed}.
// Unloaded -> Loading -> Loaded -> Unloaded {LoadGrammarAsync succeeded}.
// Unloaded -> Loading -> Unloaded {LoadGrammarAsync cancelled}.
// Unloaded -> Loading -> LoadFailed -> Unloaded {LoadGrammarAsync failed}.
Debug.Assert((_grammarState == GrammarState.Unloaded && (value == GrammarState.Unloaded || value == GrammarState.Loading || value == GrammarState.Loaded)) ||
(_grammarState == GrammarState.Loading && (value == GrammarState.LoadFailed || value == GrammarState.Loaded || value == GrammarState.Unloaded)) ||
(_grammarState == GrammarState.Loaded && value == GrammarState.Unloaded) ||
(_grammarState == GrammarState.LoadFailed && value == GrammarState.Unloaded)
);
// If we are unloaded also reset these parameters.
if (value == GrammarState.Unloaded)
{
// Remove references to these objects so they can be garbage collected.
_loadException = null;
_recognizer = null;
// Don't reset _uri and _ruleName - allows re-use.
// Don't reset _internalData - leave this to the recognizer.
// Note: After a Grammar is unloaded you can still get and set the Weight, Enabled etc.
}
else if (value == GrammarState.Loaded || value == GrammarState.LoadFailed)
{
Debug.Assert(_recognizer != null); // Must be set before changing state.
// Don't update any properties - the recognizer owns pulling this data from the Grammar.
}
_grammarState = value; // On success
}
}
internal Exception LoadException
{
get { return _loadException; }
set { _loadException = value; }
}
// There properties are read-only:
internal byte[] CfgData
{
get { return _cfgData; }
}
internal Uri BaseUri
{
get { return _baseUri; }
}
internal bool Sapi53Only
{
get { return _sapi53Only; }
}
internal uint SapiGrammarId
{
get { return _sapiGrammarId; }
set { _sapiGrammarId = value; }
}
/// <summary>
/// Is the grammar a strongly typed grammar?
/// </summary>
protected internal virtual bool IsStg
{
get { return _isStg; }
}
/// <summary>
/// Is the grammar built from an srgs document?
/// </summary>
internal bool IsSrgsDocument
{
get { return _isSrgsDocument; }
}
// Arbitrary data that is attached and removed by the RecognizerBase.
// This allow RecognizerBase.Grammars to be a simple list without the extra data being stored separately.
internal InternalGrammarData InternalData
{
get { return _internalData; }
set { _internalData = value; }
}
#endregion
#region Internal Methods
/// <summary>
/// Called by the grammar resource loader to load ruleref. Ruleref have a name, a rule name et eventually
/// parameters.
///
/// The grammar name can be either pointing to a CFG, an Srgs or DLL (stand alone or GAC).
/// </summary>
internal static Grammar Create(string grammarName, string ruleName, string onInitParameter, out Uri redirectUri)
{
redirectUri = null;
// Look for tell-tell sign that it is an assembly
grammarName = grammarName.Trim();
// Get an Uri for the grammar. Could fail for GACed values.
Uri uriGrammar;
bool hasUri = Uri.TryCreate(grammarName, UriKind.Absolute, out uriGrammar);
int posDll = grammarName.IndexOf(".dll", StringComparison.OrdinalIgnoreCase);
if (!hasUri || (posDll > 0 && posDll == grammarName.Length - 4))
{
Assembly assembly;
if (hasUri)
{
// regular dll, should use LoadFrom ()
if (uriGrammar.IsFile)
{
assembly = Assembly.LoadFrom(uriGrammar.LocalPath);
}
else
{
throw new InvalidOperationException();
}
}
else
{
// Dll in the GAC use Load ()
assembly = Assembly.Load(grammarName);
}
return LoadGrammarFromAssembly(assembly, ruleName, onInitParameter);
}
try
{
// Standard Srgs or CFG, just create the grammar
string localPath;
using (Stream stream = s_resourceLoader.LoadFile(uriGrammar, out localPath, out redirectUri))
{
try
{
return new Grammar(onInitParameter, stream, ruleName);
}
finally
{
s_resourceLoader.UnloadFile(localPath);
}
}
}
catch
{
// It was not a CFG or an Srgs, try again as dll
Assembly assembly = Assembly.LoadFrom(grammarName);
return LoadGrammarFromAssembly(assembly, ruleName, onInitParameter);
}
}
// Method called from the recognizer when a recognition has occurred.
// Only called for SpeechRecognition events, not SpeechRecognitionRejected.
internal void OnRecognitionInternal(SpeechRecognizedEventArgs eventArgs)
{
Debug.Assert(eventArgs.Result.Grammar == this);
SpeechRecognized?.Invoke(this, eventArgs);
}
// Helper method used to indicate if this grammar has a dictation Uri or not.
// This is here because the functionality needs to be a common place.
internal static bool IsDictationGrammar(Uri uri)
{
// Note that must check IsAbsoluteUri before Scheme because Uri.Scheme may throw on a relative Uri
if (uri == null || !uri.IsAbsoluteUri || uri.Scheme != "grammar" ||
!string.IsNullOrEmpty(uri.Host) || !string.IsNullOrEmpty(uri.Authority) ||
!string.IsNullOrEmpty(uri.Query) || uri.PathAndQuery != "dictation")
{
return false;
}
return true;
}
// Helper method used to indicate if this grammar has a dictation Uri or not.
// This is here because the functionality needs to be a common place.
internal bool IsDictation(Uri uri)
{
bool isDictationGrammar = IsDictationGrammar(uri);
// Note that must check IsAbsoluteUri before Scheme because Uri.Scheme may throw on a relative Uri
if (!isDictationGrammar && this is DictationGrammar)
{
throw new ArgumentException(SR.Get(SRID.DictationInvalidTopic), nameof(uri));
}
return isDictationGrammar;
}
/// <summary>
/// Find a grammar in a tree or rule refs grammar from the SAPI grammar Id
/// </summary>
/// <param name="grammarId">SAPI id</param>
/// <returns>null if not found</returns>
internal Grammar Find(long grammarId)
{
if (_ruleRefs != null)
{
foreach (Grammar ruleRef in _ruleRefs)
{
Grammar found;
if (grammarId == ruleRef._sapiGrammarId)
{
return ruleRef;
}
if ((found = ruleRef.Find(grammarId)) != null)
{
return found;
}
}
}
return null;
}
/// <summary>
/// Find a grammar in a tree or rule refs grammar from a rule name
/// </summary>
/// <returns>null if not found</returns>
internal Grammar Find(string ruleName)
{
if (_ruleRefs != null)
{
foreach (Grammar ruleRef in _ruleRefs)
{
Grammar found;
if (ruleName == ruleRef.RuleName)
{
return ruleRef;
}
if ((found = ruleRef.Find(ruleName)) != null)
{
return found;
}
}
}
return null;
}
/// <summary>
/// Add a rule ref grammar to a grammar.
/// </summary>
internal void AddRuleRef(Grammar ruleRef, uint grammarId)
{
_ruleRefs ??= new Collection<Grammar>();
_ruleRefs.Add(ruleRef);
_sapiGrammarId = grammarId;
}
internal MethodInfo MethodInfo(string method)
{
return GetType().GetMethod(method, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
}
#endregion
#region Internal Fields
internal GrammarOptions _semanticTag;
internal System.Speech.Internal.SrgsCompiler.AppDomainGrammarProxy _proxy;
internal ScriptRef[] _scripts;
#endregion
#region Protected Methods
protected string ResourceName
{
get
{
return _resources;
}
set
{
Helpers.ThrowIfEmptyOrNull(value, nameof(value));
_resources = value;
}
}
#endregion
#region Private Methods
// Called to initialize the grammar from the passed in data.
// In SpeechFX this is called at construction time.
// In MSS this is {currently} called when GetCfg is called.
// The cfg data is stored in the _cfgData field, which is not currently reset to null ever.
// After calling this method the passed in Stream / SrgsDocument are set to null.
private void LoadAndCompileCfgData(bool isImportedGrammar, bool stgInit)
{
#if DEBUG
Debug.Assert(!_loaded);
_loaded = true;
#endif
// If strongly typed grammar, load the cfg from the resources otherwise load the IL from within the CFG
Stream stream = IsStg ? LoadCfgFromResource(stgInit) : LoadCfg(isImportedGrammar, stgInit);
// Check if the grammar needs to be rebuilt
SrgsRule[] extraRules = RunOnInit(IsStg); // list of extra rule to append to the current CFG
if (extraRules != null)
{
MemoryStream streamCombined = CombineCfg(_ruleName, stream, extraRules);
// Release the old stream since a new one contains the CFG
stream.Close();
stream = streamCombined;
}
// Note LoadCfg, LoadCfgFromResource and CombineCfg all reset Stream position to zero.
_cfgData = Helpers.ReadStreamToByteArray(stream, (int)stream.Length);
stream.Close();
// Reset these - no longer needed
_srgsDocument = null;
_appStream = null;
}
/// <summary>
/// Returns a stream object for a grammar.
/// </summary>
private MemoryStream LoadCfg(bool isImportedGrammar, bool stgInit)
{
// No parameters to the constructors
Uri uriGrammar = Uri;
MemoryStream stream = new();
if (uriGrammar != null)
{
throw new PlatformNotSupportedException();
}
else if (_srgsDocument != null)
{
// If srgs, compile to a stream
SrgsGrammarCompiler.Compile(_srgsDocument, stream);
if (_baseUri == null && _srgsDocument.BaseUri != null)
{
// If we loaded the SrgsDocument from a file then that should be used as the base path.
// But it should not override any baseUri supplied directly to the Grammar constructor or in the xmlBase attribute in the xml.
_baseUri = _srgsDocument.BaseUri;
// So the priority order for getting the base path is:
// 1. The xml:base attribute in the xml.
// 2. The baseUri passed to the Grammar constructor.
// 3. The path the xml was originally loaded from.
}
}
else if (_grammarBuilder != null)
{
// If GrammarBuilder, compile to a stream
_grammarBuilder.Compile(stream);
}
else
{
// If stream, load
SrgsGrammarCompiler.CompileXmlOrCopyCfg(_appStream, stream, null);
}
stream.Position = 0;
// Update the rule name
_ruleName = CheckRuleName(stream, _ruleName, isImportedGrammar, stgInit, out _sapi53Only, out _semanticTag);
// Create an app domain for the grammar code if any
CreateSandbox(stream);
stream.Position = 0;
return stream;
}
/// <summary>
/// Look for a grammar by rule name in a loaded assembly.
///
/// The search goes over the base type for the grammar "rule name" and all of its derived language
/// dependent classes.
/// The matching algorithm pick a class that match the culture.
/// </summary>
private static Grammar LoadGrammarFromAssembly(Assembly assembly, string ruleName, string onInitParameters)
{
Type grammarType = typeof(Grammar);
Type matchingType = null;
foreach (Type typeTarget in assembly.GetTypes())
{
// must be a grammar object
if (typeTarget.IsSubclassOf(grammarType))
{
string cultureId = null;
// Set the base class for this rule
if (typeTarget.Name == ruleName)
{
matchingType = typeTarget;
}
// Pick a class that derives from rulename
if (typeTarget == matchingType || (matchingType != null && typeTarget.IsSubclassOf(matchingType)))
{
// Check if the language match
if (typeTarget.GetField("__cultureId") != null)
{
// Get the association table
try
{
cultureId = (string)typeTarget.InvokeMember("__cultureId", BindingFlags.GetField, null, null, null, null);
}
catch (Exception e)
{
if (!(e is System.MissingFieldException))
{
throw;
}
}
// Check for the current culture or any compatible culture (parent en-us or en for e.g.)
if (Helpers.CompareInvariantCulture(new CultureInfo(int.Parse(cultureId, CultureInfo.InvariantCulture)), CultureInfo.CurrentUICulture))
{
try
{
object[] initParams = MatchInitParameters(typeTarget, onInitParameters, assembly.GetName().Name, ruleName);
// The CLR does the match for the right constructor based on the onInitParameters types
return (Grammar)assembly.CreateInstance(typeTarget.FullName, false, BindingFlags.CreateInstance, null, initParams, null, null);
}
catch (MissingMemberException)
{
throw new ArgumentException(SR.Get(SRID.RuleScriptInvalidParameters, typeTarget.Name, typeTarget.Name));
}
}
}
}
}
}
return null;
}
/// <summary>
/// Construct a list of parameters from a sapi:params string.
/// </summary>
private static object[] MatchInitParameters(Type type, string onInitParameters, string grammar, string rule)
{
ConstructorInfo[] cis = type.GetConstructors();
NameValuePair[] pairs = ParseInitParams(onInitParameters);
object[] values = new object[pairs.Length];
bool foundConstructor = false;
for (int iCtor = 0; iCtor < cis.Length && !foundConstructor; iCtor++)
{
ParameterInfo[] paramInfo = cis[iCtor].GetParameters();
// Check if enough parameters are provided.
if (paramInfo.Length > pairs.Length)
{
continue;
}
foundConstructor = true;
for (int i = 0; i < pairs.Length && foundConstructor; i++)
{
NameValuePair pair = pairs[i];
// anonymous
if (pair._name == null)
{
values[i] = pair._value;
}
else
{
bool foundParameter = false;
for (int j = 0; j < paramInfo.Length; j++)
{
if (paramInfo[j].Name == pair._name)
{
values[j] = ParseValue(paramInfo[j].ParameterType, pair._value);
foundParameter = true;
break;
}
}
if (!foundParameter)
{
foundConstructor = false;
}
}
}
}
if (!foundConstructor)
{
throw new FormatException(SR.Get(SRID.CantFindAConstructor, grammar, rule, FormatConstructorParameters(cis)));
}
return values;
}
/// <summary>
/// Parse the value for a type from a string to a strong type.
/// If the type does not support the Parse method then the operation fails.
/// </summary>
private static object ParseValue(Type type, string value)
{
if (type == typeof(string))
{
return value;
}
return type.InvokeMember("Parse", BindingFlags.InvokeMethod, null, null, new object[] { value }, CultureInfo.InvariantCulture);
}
/// <summary>
/// Returns the list of the possible parameter names and type for a grammar
/// </summary>
private static string FormatConstructorParameters(ConstructorInfo[] cis)
{
StringBuilder sb = new();
for (int iCtor = 0; iCtor < cis.Length; iCtor++)
{
sb.Append(iCtor > 0 ? " or sapi:parms=\"" : "sapi:parms=\"");
ParameterInfo[] pis = cis[iCtor].GetParameters();
for (int i = 0; i < pis.Length; i++)
{
if (i > 0)
{
sb.Append(';');
}
ParameterInfo pi = pis[i];
sb.Append(pi.Name);
sb.Append(':');
sb.Append(pi.ParameterType.Name);
}
sb.Append('"');
}
return sb.ToString();
}
/// <summary>
/// Split the init parameter strings into an array of name/values
/// The format must be "name:value". If the ':' then parameter is anonymous.
/// </summary>
private static NameValuePair[] ParseInitParams(string initParameters)
{
if (string.IsNullOrEmpty(initParameters))
{
return Array.Empty<NameValuePair>();
}
string[] parameters = initParameters.Split(';', StringSplitOptions.None);
NameValuePair[] pairs = new NameValuePair[parameters.Length];
for (int i = 0; i < parameters.Length; i++)
{
string parameter = parameters[i];
int posColon = parameter.IndexOf(':');
if (posColon >= 0)
{
pairs[i]._name = parameter.Substring(0, posColon);
pairs[i]._value = parameter.Substring(posColon + 1);
}
else
{
pairs[i]._value = parameter;
}
}
return pairs;
}
private void InitialGrammarLoad(string ruleName, object[] parameters, bool isImportedGrammar)
{
_ruleName = ruleName;
_parameters = parameters;
// Bail out if it is a dictation grammar
if (!IsDictation(_uri))
{
LoadAndCompileCfgData(isImportedGrammar, false);
}
}
private void CreateSandbox(MemoryStream stream)
{
// Checks if it contains .NET Semantic code
byte[] assemblyContent;
byte[] assemblyDebugSymbols;
ScriptRef[] scripts;
stream.Position = 0;
// This must be before the SAPI load to avoid some conflict with SAPI server when getting at the
// the stream
if (System.Speech.Internal.SrgsCompiler.CfgGrammar.LoadIL(stream, out assemblyContent, out assemblyDebugSymbols, out scripts))
{
// Check all methods referenced in the rule; availability, public and arguments
Assembly executingAssembly = Assembly.GetExecutingAssembly();
_proxy = new AppDomainGrammarProxy();
_proxy.Init(_ruleName, assemblyContent, assemblyDebugSymbols);
_scripts = scripts;
}
}
// Loads a strongly typed grammar from a resource in the Assembly.
private Stream LoadCfgFromResource(bool stgInit)
{
// Strongly typed grammar get the Cfg data
Assembly assembly = Assembly.GetAssembly(GetType());
Stream stream = assembly.GetManifestResourceStream(ResourceName);
if (stream == null)
{
throw new FormatException(SR.Get(SRID.RecognizerInvalidBinaryGrammar));
}
try
{
ScriptRef[] scripts = CfgGrammar.LoadIL(stream);
if (scripts == null)
{
throw new ArgumentException(SR.Get(SRID.CannotLoadDotNetSemanticCode));
}
_scripts = scripts;
}
catch (Exception e)
{
throw new ArgumentException(SR.Get(SRID.CannotLoadDotNetSemanticCode), e);
}
stream.Position = 0;
// Update the rule name
_ruleName = CheckRuleName(stream, GetType().Name, false, stgInit, out _sapi53Only, out _semanticTag);
_isStg = true;
return stream;
}
private static MemoryStream CombineCfg(string rule, Stream stream, SrgsRule[] extraRules)
{
using (MemoryStream streamExtra = new())
{
// Create an SrgsDocument from the set of rules
SrgsDocument sgrsDocument = new();
sgrsDocument.TagFormat = SrgsTagFormat.KeyValuePairs;
foreach (SrgsRule srgsRule in extraRules)
{
sgrsDocument.Rules.Add(srgsRule);
}
SrgsGrammarCompiler.Compile(sgrsDocument, streamExtra);
using (StreamMarshaler streamMarshaler = new(stream))
{
long endSeekPosition = stream.Position;
Backend backend = new(streamMarshaler);
stream.Position = endSeekPosition;
streamExtra.Position = 0;
MemoryStream streamCombined = new();
using (StreamMarshaler streamExtraMarshaler = new(streamExtra))
{
Backend extra = new(streamExtraMarshaler);
Backend combined = Backend.CombineGrammar(rule, backend, extra);
using (StreamMarshaler streamCombinedMarshaler = new(streamCombined))
{
combined.Commit(streamCombinedMarshaler);
streamCombined.Position = 0;
return streamCombined;
}
}
}
}
}
#pragma warning disable 56507 // check for null or empty strings
private SrgsRule[] RunOnInit(bool stg)
{
SrgsRule[] extraRules = null;
bool onInitInvoked = false;
// Get the name of the onInit method to run
string methodName = ScriptRef.OnInitMethod(_scripts, _ruleName);
if (methodName != null)
{
if (_proxy != null)
{
Exception appDomainException;
extraRules = _proxy.OnInit(methodName, _parameters, _onInitParameters, out appDomainException);
onInitInvoked = true;
if (appDomainException != null)
{
ExceptionDispatchInfo.Throw(appDomainException);
}
}
else
{