Repository navigation
Expand file tree
/
Copy pathAssemblyDescriptor.cs
More file actions
960 lines (845 loc) · 35.7 KB
/
Copy pathAssemblyDescriptor.cs
File metadata and controls
960 lines (845 loc) · 35.7 KB
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
using System;
#if NETSTANDARD || NETCOREAPP
using FastReport.Code.CodeDom.Compiler;
#else
using System.CodeDom.Compiler;
#endif
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Concurrent;
using FastReport.Data;
using FastReport.Engine;
using FastReport.Utils;
#if SKIA
using HMACSHA1 = FastReport.Utils.DetravHMACSHA1;
#endif
namespace FastReport.Code
{
partial class AssemblyDescriptor : IDisposable
{
private static readonly ConcurrentDictionary<string, Assembly> FAssemblyCache;
private readonly StringBuilder scriptText;
private readonly List<SourcePosition> sourcePositions;
private int insertLine;
private int insertPos;
private bool needCompile;
private const string shaKey = "FastReportCode";
private readonly static object compileLocker;
private readonly string currentFolder;
#if ASYNC
private readonly SemaphoreSlim semaphoreSlim = new SemaphoreSlim(1);
#endif
public Assembly Assembly { get; private set; }
public object Instance { get; private set; }
public Report Report { get; }
public Hashtable Expressions { get; }
private void InsertItem(string text, string objName)
{
string[] lines = text.Split('\r');
scriptText.Insert(insertPos, text);
SourcePosition pos = new SourcePosition(objName, insertLine, insertLine + lines.Length - 2);
sourcePositions.Add(pos);
insertLine += lines.Length - 1;
insertPos += text.Length;
}
private void InitField(string name, object c)
{
FieldInfo info = Instance.GetType().GetField(name,
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
info.SetValue(Instance, c);
}
private void InitFields()
{
InitField("Report", Report);
InitField("Engine", Report.Engine);
ObjectCollection allObjects = Report.AllObjects;
foreach (Base c in allObjects)
{
if (!String.IsNullOrEmpty(c.Name))
InitField(c.Name, c);
}
}
private string GetErrorObjectName(int errorLine)
{
foreach (SourcePosition pos in sourcePositions)
{
if (errorLine >= pos.start && errorLine <= pos.end)
{
return pos.sourceObject;
}
}
return "";
}
private int GetScriptLine(int errorLine)
{
int start = sourcePositions[0].start;
int end = sourcePositions[sourcePositions.Count - 1].end;
if (errorLine >= start && errorLine <= end)
return -1;
if (errorLine > end)
return errorLine - (end - start + 1);
return errorLine;
}
private string ReplaceDataItems(string expression)
{
FindTextArgs args = new FindTextArgs();
args.Text = new FastString(expression);
args.OpenBracket = "[";
args.CloseBracket = "]";
while (args.StartIndex < args.Text.Length)
{
expression = CodeUtils.GetExpression(args, true);
if (expression == null)
break;
if (DataHelper.IsValidColumn(Report.Dictionary, expression))
{
Type type = DataHelper.GetColumnType(Report.Dictionary, expression);
expression = Report.CodeHelper.ReplaceColumnName(expression, type);
}
else if (DataHelper.IsValidParameter(Report.Dictionary, expression))
{
expression = Report.CodeHelper.ReplaceParameterName(DataHelper.GetParameter(Report.Dictionary, expression));
}
else if (DataHelper.IsValidTotal(Report.Dictionary, expression))
{
expression = Report.CodeHelper.ReplaceTotalName(expression);
}
else
{
expression = "[" + ReplaceDataItems(expression) + "]";
}
args.Text = args.Text.Remove(args.StartIndex, args.EndIndex - args.StartIndex);
args.Text = args.Text.Insert(args.StartIndex, expression);
args.StartIndex += expression.Length;
}
return args.Text.ToString();
}
private static bool ContainsAssembly(StringCollection assemblies, string assembly)
{
string asmName = Path.GetFileName(assembly);
foreach (string a in assemblies)
{
string asmName1 = Path.GetFileName(a);
if (String.Compare(asmName, asmName1, true) == 0)
return true;
}
return false;
}
private static void AddFastReportAssemblies(StringCollection assemblies)
{
foreach (Assembly assembly in RegisteredObjects.Assemblies)
{
string aLocation = assembly.Location;
#if CROSSPLATFORM || COREWIN
if (aLocation == "")
{
// try fix SFA in FastReport.Compat
string fixedReference = CodeDomProvider.TryFixReferenceInSingeFileApp(assembly);
if (!string.IsNullOrEmpty(fixedReference))
aLocation = fixedReference;
}
#endif
if (!ContainsAssembly(assemblies, aLocation))
assemblies.Add(aLocation);
}
}
#if ASYNC
private static async ValueTask AddFastReportAssemblies(StringCollection assemblies, CancellationToken token)
{
foreach (Assembly assembly in RegisteredObjects.Assemblies)
{
string aLocation = assembly.Location;
#if CROSSPLATFORM || COREWIN
if (aLocation == "")
{
// try fix SFA in FastReport.Compat
string fixedReference = await CodeDomProvider.TryFixReferenceInSingeFileAppAsync(assembly, token);
if (!string.IsNullOrEmpty(fixedReference))
aLocation = fixedReference;
}
#endif
if (!ContainsAssembly(assemblies, aLocation))
assemblies.Add(aLocation);
}
}
#endif
private void AddReferencedAssemblies(StringCollection assemblies, string defaultPath)
{
for (int i = 0; i < Report.ReferencedAssemblies.Length; i++)
{
string s = Report.ReferencedAssemblies[i];
#if CROSSPLATFORM
if (s == "System.Windows.Forms.dll")
s = "FastReport.Compat";
#endif
// fix for old reports with "System.Windows.Forms.DataVisualization" in referenced assemblies
if (s.IndexOf("System.Windows.Forms.DataVisualization") != -1)
s = "FastReport.DataVisualization";
#if SKIA
if (s.IndexOf("FastReport.Compat") != -1)
s = "FastReport.Compat.Skia";
if (s.IndexOf("FastReport.DataVisualization") != -1)
s = "FastReport.DataVisualization.Skia";
#endif
AddReferencedAssembly(assemblies, defaultPath, s);
}
#if SKIA
AddReferencedAssembly(assemblies, defaultPath, "FastReport.SkiaDrawing");
#endif
// these two required for "dynamic" type support
AddReferencedAssembly(assemblies, defaultPath, "System.Core");
AddReferencedAssembly(assemblies, defaultPath, "Microsoft.CSharp");
}
private void AddReferencedAssembly(StringCollection assemblies, string defaultPath, string assemblyName)
{
string location = GetFullAssemblyReference(assemblyName, defaultPath);
if (location != "" && !ContainsAssembly(assemblies, location))
assemblies.Add(location);
}
private string GetFullAssemblyReference(string relativeReference, string defaultPath)
{
// in .NET Core we get the AssemblyReference in FR.Compat
#if !(CROSSPLATFORM || COREWIN)
if (relativeReference == null || relativeReference.Trim() == "")
return "";
// Strip off any trailing ".dll" ".exe" if present.
string dllName = relativeReference;
if (string.Compare(relativeReference.Substring(relativeReference.Length - 4), ".dll", true) == 0 ||
string.Compare(relativeReference.Substring(relativeReference.Length - 4), ".exe", true) == 0)
dllName = relativeReference.Substring(0, relativeReference.Length - 4);
// See if the required assembly is already present in our current AppDomain
foreach (Assembly currAssembly in AppDomain.CurrentDomain.GetAssemblies())
{
if (string.Compare(currAssembly.GetName().Name, dllName, true) == 0)
{
// Found it, return the location as the full reference.
return currAssembly.Location;
}
}
// See if the required assembly is present in the ReferencedAssemblies but not yet loaded
foreach (AssemblyName assemblyName in Assembly.GetExecutingAssembly().GetReferencedAssemblies())
{
if (string.Compare(assemblyName.Name, dllName, true) == 0)
{
// Found it, try to load assembly and return the location as the full reference.
try
{
return Assembly.ReflectionOnlyLoad(assemblyName.FullName).Location;
}
catch { }
}
}
// See if the required assembly is present locally
string path = Path.Combine(defaultPath, relativeReference);
if (File.Exists(path))
return path;
#endif
return relativeReference;
}
private void AddExpression(string expression, Base source, bool forceSimpleItems)
{
if (expression.Trim() == "" || Expressions.ContainsKey(expression))
return;
string expr = expression;
if (expr.StartsWith("[") && expr.EndsWith("]"))
expr = expr.Substring(1, expr.Length - 2);
// skip simple items. Report.Calc does this.
if (!forceSimpleItems)
{
if (DataHelper.IsSimpleColumn(Report.Dictionary, expr) ||
DataHelper.IsValidParameter(Report.Dictionary, expr) ||
DataHelper.IsValidTotal(Report.Dictionary, expr))
return;
}
// handle complex expressions, relations
ExpressionDescriptor descriptor = new ExpressionDescriptor(this);
Expressions.Add(expression, descriptor);
descriptor.MethodName = "CalcExpression";
if (DataHelper.IsValidColumn(Report.Dictionary, expr))
expr = "[" + expr + "]";
else
expr = expression;
string expressionCode = ReplaceDataItems(expr);
InsertItem(Report.CodeHelper.AddExpression(expression, expressionCode), source == null ? "" : source.Name);
needCompile = true;
}
public void AddObjects()
{
ObjectCollection allObjects = Report.AllObjects;
SortedList<string, Base> objects = new SortedList<string, Base>();
// add all report objects
InsertItem(Report.CodeHelper.AddField(typeof(Report), "Report") +
Report.CodeHelper.AddField(typeof(ReportEngine), "Engine"), "Report");
foreach (Base c in allObjects)
{
if (!String.IsNullOrEmpty(c.Name) && !objects.ContainsKey(c.Name))
objects.Add(c.Name, c);
}
foreach (Base c in objects.Values)
{
InsertItem(Report.CodeHelper.AddField(c.GetType(), c.Name), c.Name);
}
// add custom script
string processedCode = "";
foreach (Base c in objects.Values)
{
string customCode = c.GetCustomScript();
// avoid custom script duplicates
if (!String.IsNullOrEmpty(customCode) && processedCode.IndexOf(customCode) == -1)
{
InsertItem(customCode, c.Name);
processedCode += customCode;
needCompile = true;
}
}
}
public void AddSingleExpression(string expression)
{
InsertItem(Report.CodeHelper.BeginCalcExpression(), "");
AddExpression(expression, null, true);
InsertItem(Report.CodeHelper.EndCalcExpression(), "");
needCompile = true;
}
public void AddExpressions()
{
// speed up the case: lot of report objects (> 1000) and lot of data columns in the dictionary (> 10000).
Report.Dictionary.CacheAllObjects = true;
InsertItem(Report.CodeHelper.BeginCalcExpression(), "");
ObjectCollection allObjects = Report.AllObjects;
ObjectCollection l = Report.Dictionary.AllObjects;
foreach (Base c in l)
{
allObjects.Add(c);
}
foreach (Base c in allObjects)
{
string[] expressions = c.GetExpressions();
if (expressions != null)
{
foreach (string expr in expressions)
{
AddExpression(expr, c, false);
}
}
}
InsertItem(Report.CodeHelper.EndCalcExpression(), "");
Report.Dictionary.CacheAllObjects = false;
}
public void AddFunctions()
{
List<FunctionInfo> list = new List<FunctionInfo>();
RegisteredObjects.Functions.EnumItems(list);
foreach (FunctionInfo info in list)
{
if (info.Function != null)
{
InsertItem(Report.CodeHelper.GetMethodSignatureAndBody(info.Function), "Function");
}
}
}
public string GenerateReportClass(string className)
{
InsertItem(Report.CodeHelper.GenerateInitializeMethod(), "");
return Report.CodeHelper.ReplaceClassName(scriptText.ToString(), className);
}
public void Compile()
{
if (needCompile)
{
lock (compileLocker)
{
if (needCompile)
InternalCompile();
}
}
}
#if ASYNC
public async Task CompileAsync(CancellationToken token = default)
{
if (needCompile)
{
await semaphoreSlim.WaitAsync(token);
if (needCompile)
{
//await Task.Run(token => InternalCompileAsync(token), token);
await InternalCompileAsync(token);
semaphoreSlim.Release();
}
}
}
#endif
private void InternalCompile()
{
CompilerParameters cp = GetCompilerParameters();
if (Config.WebMode &&
Config.EnableScriptSecurity &&
Config.ScriptSecurityProps.AddStubClasses)
AddStubClasses();
string errors = string.Empty;
CompilerResults cr;
bool exception = !TryInternalCompile(cp, out cr);
for (int i = 0; exception && i < Config.CompilerSettings.RecompileCount; i++)
{
exception = !TryRecompile(cp, ref cr);
}
if (cr != null)
HandleCompileErrors(cr, out errors);
if (exception && errors != string.Empty)
throw new CompilerException(errors);
}
#if ASYNC
private async Task InternalCompileAsync(CancellationToken cancellationToken)
{
CompilerParameters cp = await GetCompilerParametersAsync(cancellationToken);
if (Config.WebMode &&
Config.EnableScriptSecurity &&
Config.ScriptSecurityProps.AddStubClasses)
AddStubClasses();
string errors = string.Empty;
CompilerResults cr = await InternalCompileAsync(cp, cancellationToken);
bool success = CheckCompileResult(cr);
for (int i = 0; !success && i < Config.CompilerSettings.RecompileCount; i++)
{
cr = await TryRecompileAsync(cp, cr, cancellationToken);
success = CheckCompileResult(cr);
}
if (cr != null)
HandleCompileErrors(cr, out errors);
if (!success && errors != string.Empty)
throw new CompilerException(errors);
}
private static bool CheckCompileResult(CompilerResults result)
{
// if result == null => was found in cache
return result == null || result.Errors.Count == 0;
}
#endif
private CompilerParameters GetCompilerParameters()
{
// configure compiler options
CompilerParameters cp = new CompilerParameters();
AddFastReportAssemblies(cp.ReferencedAssemblies); // 2
AddReferencedAssemblies(cp.ReferencedAssemblies, currentFolder); // 9
ReviewReferencedAssemblies(cp.ReferencedAssemblies);
cp.GenerateInMemory = true;
// sometimes the system temp folder is not accessible...
if (Config.TempFolder != null)
cp.TempFiles = new TempFileCollection(Config.TempFolder, false);
return cp;
}
#if ASYNC
private async Task<CompilerParameters> GetCompilerParametersAsync(CancellationToken ct)
{
// configure compiler options
CompilerParameters cp = new CompilerParameters();
await AddFastReportAssemblies(cp.ReferencedAssemblies, ct); // 2
AddReferencedAssemblies(cp.ReferencedAssemblies, currentFolder); // 9
ReviewReferencedAssemblies(cp.ReferencedAssemblies);
cp.GenerateInMemory = true;
// sometimes the system temp folder is not accessible...
if (Config.TempFolder != null)
cp.TempFiles = new TempFileCollection(Config.TempFolder, false);
return cp;
}
#endif
private string GetAssemblyHash(CompilerParameters cp)
{
StringBuilder assemblyHashSB = new StringBuilder();
foreach (string a in cp.ReferencedAssemblies)
assemblyHashSB.Append(a);
assemblyHashSB.Append(scriptText);
byte[] hash;
using (HMACSHA1 hMACSHA1 = new HMACSHA1(Encoding.ASCII.GetBytes(shaKey)))
{
hash = hMACSHA1.ComputeHash(Encoding.Unicode.GetBytes(assemblyHashSB.ToString()));
}
return Convert.ToBase64String(hash);
}
/// <summary>
/// Returns true, if compilation is successful
/// </summary>
private bool TryInternalCompile(CompilerParameters cp, out CompilerResults cr)
{
// find assembly in cache
string assemblyHash = GetAssemblyHash(cp);
Assembly cachedAssembly;
if (FAssemblyCache.TryGetValue(assemblyHash, out cachedAssembly))
{
Assembly = cachedAssembly;
var reportScript = Assembly.CreateInstance("FastReport.ReportScript");
InitInstance(reportScript);
cr = null;
return true;
}
// compile report scripts
using (CodeDomProvider provider = Report.CodeHelper.GetCodeProvider())
{
string script = scriptText.ToString();
ScriptSecurityEventArgs ssea = new ScriptSecurityEventArgs(Report, script, Report.ReferencedAssemblies);
Config.OnScriptCompile(ssea);
#if CROSSPLATFORM || COREWIN
provider.BeforeEmitCompilation += Config.OnBeforeScriptCompilation;
// in .NET Core we use cultureInfo to represent errors
cr = provider.CompileAssemblyFromSource(cp, script, Config.CompilerSettings.CultureInfo);
#else
cr = provider.CompileAssemblyFromSource(cp, script);
#endif
Assembly = null;
Instance = null;
if (cr.Errors.Count != 0) // Compile errors
return false;
FAssemblyCache.TryAdd(assemblyHash, cr.CompiledAssembly);
Assembly = cr.CompiledAssembly;
var reportScript = Assembly.CreateInstance("FastReport.ReportScript");
InitInstance(reportScript);
return true;
}
}
#if ASYNC && (CROSSPLATFORM || COREWIN)
/// <summary>
/// Returns true, if compilation is successful
/// </summary>
private async ValueTask<CompilerResults> InternalCompileAsync(CompilerParameters cp, CancellationToken cancellationToken)
{
CompilerResults cr;
// find assembly in cache
string assemblyHash = GetAssemblyHash(cp);
Assembly cachedAssembly;
if (FAssemblyCache.TryGetValue(assemblyHash, out cachedAssembly))
{
Assembly = cachedAssembly;
var reportScript = Assembly.CreateInstance("FastReport.ReportScript");
InitInstance(reportScript);
cr = null;
return cr; // return true;
}
// compile report scripts
using (CodeDomProvider provider = Report.CodeHelper.GetCodeProvider())
{
string script = scriptText.ToString();
ScriptSecurityEventArgs ssea = new ScriptSecurityEventArgs(Report, script, Report.ReferencedAssemblies);
Config.OnScriptCompile(ssea);
provider.BeforeEmitCompilation += Config.OnBeforeScriptCompilation;
cr = await provider.CompileAssemblyFromSourceAsync(cp, script, Config.CompilerSettings.CultureInfo, cancellationToken);
Assembly = null;
Instance = null;
if (cr.Errors.Count != 0) // Compile errors
return cr; // return false;
FAssemblyCache.TryAdd(assemblyHash, cr.CompiledAssembly);
Assembly = cr.CompiledAssembly;
var reportScript = Assembly.CreateInstance("FastReport.ReportScript");
InitInstance(reportScript);
return cr;
}
}
#endif
private string ReplaceExpression(string error, TextObjectBase text)
{
string result = text.Text;
string[] parts = error.Split('\"');
if (parts.Length == 3)
{
string[] expressions = text.GetExpressions();
foreach (string expr in expressions)
{
if (expr.Contains(parts[1]))
{
if (!DataHelper.IsValidColumn(Report.Dictionary, expr))
{
string replaceString = text.Brackets[0] + expr + text.Brackets[2];
if (Config.CompilerSettings.ExceptionBehaviour == CompilerExceptionBehaviour.ShowExceptionMessage ||
Config.CompilerSettings.ExceptionBehaviour == CompilerExceptionBehaviour.ReplaceExpressionWithPlaceholder)
{
result = result.Replace(replaceString, Config.CompilerSettings.Placeholder);
}
else if (Config.CompilerSettings.ExceptionBehaviour == CompilerExceptionBehaviour.ReplaceExpressionWithExceptionMessage)
{
result = result.Replace(replaceString, error);
}
}
}
}
}
return result;
}
/// <summary>
/// Handle compile errors
/// </summary>
private void HandleCompileErrors(CompilerResults cr, out string errors)
{
errors = string.Empty;
Regex regex;
if (Config.WebMode && Config.EnableScriptSecurity)
{
for (int i = 0; i < cr.Errors.Count;)
{
CompilerError ce = cr.Errors[i];
if (ce.ErrorNumber == "CS1685") // duplicate class
{
cr.Errors.Remove(ce);
continue;
}
else if (ce.ErrorNumber == "CS0436") // user using a forbidden type
{
const string pattern = "[\"'](\\S+)[\"']";
regex = new Regex(pattern, RegexOptions.Compiled);
string typeName = regex.Match(ce.ErrorText).Value;
const string res = "Web,ScriptSecurity,ForbiddenType";
string message = Res.TryGet(res);
if (string.Equals(res, message))
message = "Please don't use the type " + typeName;
else
message = message.Replace("{typeName}", typeName); //$"Please don't use the type {typeName}";
ce.ErrorText = message;
}
else if (ce.ErrorNumber == "CS0117") // user using a forbidden method
{
const string pattern = "[\"'](\\S+)[\"']";
regex = new Regex(pattern, RegexOptions.Compiled);
MatchCollection mathes = regex.Matches(ce.ErrorText);
if (mathes.Count > 1)
{
string methodName = mathes[1].Value;
const string res = "Web,ScriptSecurity,ForbiddenMethod";
string message = Res.TryGet(res);
if (string.Equals(res, message))
message = "Please don't use the method " + methodName;
else
message = message.Replace("{methodName}", methodName); //$"Please don't use the method {methodName}";
ce.ErrorText = message;
}
}
i++;
}
}
foreach (CompilerError ce in cr.Errors)
{
int line = GetScriptLine(ce.Line);
// error is inside own items
if (line == -1)
{
string errObjName = GetErrorObjectName(ce.Line);
if (Config.CompilerSettings.ExceptionBehaviour != CompilerExceptionBehaviour.Default)
{
// handle errors when name does not exist in the current context
if (ce.ErrorNumber == "CS0103")
{
TextObjectBase text = Report.FindObject(errObjName) as TextObjectBase;
text.Text = ReplaceExpression(ce.ErrorText, text);
if (Config.CompilerSettings.ExceptionBehaviour == CompilerExceptionBehaviour.ShowExceptionMessage)
System.Windows.Forms.MessageBox.Show(ce.ErrorText);
continue;
}
}
// handle division by zero errors
if (ce.ErrorNumber == "CS0020")
{
TextObjectBase text = Report.FindObject(errObjName) as TextObjectBase;
text.CanGrow = true;
text.FillColor = Color.Red;
text.Text = "DIVISION BY ZERO!";
continue;
}
else
{
errors += $"({errObjName}): {Res.Get("Messages,Error")} {ce.ErrorNumber}: {ce.ErrorText}\r\n";
ErrorMsg(errObjName, ce);
}
}
else
{
errors += $"({line},{ce.Column}): {Res.Get("Messages,Error")} {ce.ErrorNumber}: {ce.ErrorText}\r\n";
ErrorMsg(ce, line);
}
}
}
/// <summary>
/// Returns true if recompilation is successful
/// </summary>
private bool TryRecompile(CompilerParameters cp, ref CompilerResults cr)
{
List<string> additionalAssemblies = new List<string>(4);
foreach (CompilerError ce in cr.Errors)
{
if (ce.ErrorNumber == "CS0012") // missing reference on assembly
{
// try to add reference
try
{
// in .Net Core compiler will return other quotes
#if CROSSPLATFORM || COREWIN
const string quotes = "\'";
#else
const string quotes = "\"";
#endif
const string pattern = quotes + @"(\S{1,}),";
Regex regex = new Regex(pattern, RegexOptions.Compiled);
string assemblyName = regex.Match(ce.ErrorText).Groups[1].Value; // Groups[1] include string without quotes and , symbols
if (!additionalAssemblies.Contains(assemblyName))
additionalAssemblies.Add(assemblyName);
continue;
}
catch { }
}
}
if (additionalAssemblies.Count > 0) // need recompile
{
// try to load missing assemblies
foreach (string assemblyName in additionalAssemblies)
{
AddReferencedAssembly(cp.ReferencedAssemblies, currentFolder, assemblyName);
}
return TryInternalCompile(cp, out cr);
}
return false;
}
#if ASYNC
/// <summary>
/// Returns true if recompilation is successful
/// </summary>
private async Task<CompilerResults> TryRecompileAsync(CompilerParameters cp, CompilerResults oldResult, CancellationToken ct)
{
List<string> additionalAssemblies = new List<string>(4);
foreach (CompilerError ce in oldResult.Errors)
{
if (ce.ErrorNumber == "CS0012") // missing reference on assembly
{
// try to add reference
try
{
// in .Net Core compiler will return other quotes
#if CROSSPLATFORM || COREWIN
const string quotes = "\'";
#else
const string quotes = "\"";
#endif
const string pattern = quotes + @"(\S{1,}),";
Regex regex = new Regex(pattern, RegexOptions.Compiled);
string assemblyName = regex.Match(ce.ErrorText).Groups[1].Value; // Groups[1] include string without quotes and , symbols
if (!additionalAssemblies.Contains(assemblyName))
additionalAssemblies.Add(assemblyName);
continue;
}
catch { }
}
}
if (additionalAssemblies.Count > 0) // need recompile
{
// try to load missing assemblies
foreach (string assemblyName in additionalAssemblies)
{
AddReferencedAssembly(cp.ReferencedAssemblies, currentFolder, assemblyName);
}
return await InternalCompileAsync(cp, ct);
}
return oldResult;
}
#endif
public void InitInstance(object instance)
{
this.Instance = instance;
InitFields();
}
public bool ContainsExpression(string expr)
{
return Expressions.ContainsKey(expr);
}
public object CalcExpression(string expr, Variant value)
{
ExpressionDescriptor expressionDescriptor = Expressions[expr] as ExpressionDescriptor;
if (expressionDescriptor != null)
return expressionDescriptor.Invoke(new object[] { expr, value });
else
return null;
}
public object InvokeMethod(string name, object[] parms)
{
if (String.IsNullOrEmpty(name))
return null;
string exprName = "method_" + name;
if (!ContainsExpression(exprName))
{
ExpressionDescriptor descriptor = new ExpressionDescriptor(this);
Expressions.Add(exprName, descriptor);
descriptor.MethodName = name;
}
try
{
return (Expressions[exprName] as ExpressionDescriptor).Invoke(parms);
}
catch (TargetInvocationException ex)
{
throw (ex.InnerException); // ex now stores the original success
}
}
public void Dispose()
{
#if ASYNC
semaphoreSlim.Dispose();
#endif
}
public AssemblyDescriptor(Report report, string scriptText)
{
this.Report = report;
this.scriptText = new StringBuilder(scriptText);
Expressions = new Hashtable();
sourcePositions = new List<SourcePosition>();
insertPos = Report.CodeHelper.GetPositionToInsertOwnItems(scriptText);
if (insertPos == -1)
{
string msg = Res.Get("Messages,ClassError");
ErrorMsg(msg);
throw new CompilerException(msg);
}
else
{
string[] lines = scriptText.Substring(0, insertPos).Split('\r');
insertLine = lines.Length;
if (scriptText != Report.CodeHelper.EmptyScript())
needCompile = true;
}
// set the current folder
currentFolder = Config.ApplicationFolder;
if (Config.WebMode)
{
try
{
string bin_directory = Path.Combine(currentFolder, "Bin");
if (Directory.Exists(bin_directory))
currentFolder = bin_directory;
}
catch
{
}
}
// Commented by Samuray
//Directory.SetCurrentDirectory(currentFolder);
}
static AssemblyDescriptor()
{
FAssemblyCache = new ConcurrentDictionary<string, Assembly>();
compileLocker = new object();
}
private sealed class SourcePosition
{
public readonly string sourceObject;
public readonly int start;
public readonly int end;
public SourcePosition(string obj, int start, int end)
{
sourceObject = obj;
this.start = start;
this.end = end;
}
}
}
}