AssemblyDescriptor.cs 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  1. using System;
  2. #if NETSTANDARD || NETCOREAPP
  3. using FastReport.Code.CodeDom.Compiler;
  4. #else
  5. using System.CodeDom.Compiler;
  6. #endif
  7. using System.Collections;
  8. using System.Collections.Generic;
  9. using System.Collections.Specialized;
  10. using System.Drawing;
  11. using System.IO;
  12. using System.Reflection;
  13. using System.Security.Cryptography;
  14. using System.Text;
  15. using System.Text.RegularExpressions;
  16. using System.Threading;
  17. using System.Threading.Tasks;
  18. using System.Collections.Concurrent;
  19. using FastReport.Data;
  20. using FastReport.Engine;
  21. using FastReport.Utils;
  22. #if SKIA
  23. using HMACSHA1 = FastReport.Utils.DetravHMACSHA1;
  24. #endif
  25. namespace FastReport.Code
  26. {
  27. partial class AssemblyDescriptor : IDisposable
  28. {
  29. private static readonly ConcurrentDictionary<string, Assembly> FAssemblyCache;
  30. private readonly StringBuilder scriptText;
  31. private readonly List<SourcePosition> sourcePositions;
  32. private int insertLine;
  33. private int insertPos;
  34. private bool needCompile;
  35. private const string shaKey = "FastReportCode";
  36. private readonly static object compileLocker;
  37. private readonly string currentFolder;
  38. #if ASYNC
  39. private readonly SemaphoreSlim semaphoreSlim = new SemaphoreSlim(1);
  40. #endif
  41. public Assembly Assembly { get; private set; }
  42. public object Instance { get; private set; }
  43. public Report Report { get; }
  44. public Hashtable Expressions { get; }
  45. private void InsertItem(string text, string objName)
  46. {
  47. string[] lines = text.Split('\r');
  48. scriptText.Insert(insertPos, text);
  49. SourcePosition pos = new SourcePosition(objName, insertLine, insertLine + lines.Length - 2);
  50. sourcePositions.Add(pos);
  51. insertLine += lines.Length - 1;
  52. insertPos += text.Length;
  53. }
  54. private void InitField(string name, object c)
  55. {
  56. FieldInfo info = Instance.GetType().GetField(name,
  57. BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
  58. info.SetValue(Instance, c);
  59. }
  60. private void InitFields()
  61. {
  62. InitField("Report", Report);
  63. InitField("Engine", Report.Engine);
  64. ObjectCollection allObjects = Report.AllObjects;
  65. foreach (Base c in allObjects)
  66. {
  67. if (!String.IsNullOrEmpty(c.Name))
  68. InitField(c.Name, c);
  69. }
  70. }
  71. private string GetErrorObjectName(int errorLine)
  72. {
  73. foreach (SourcePosition pos in sourcePositions)
  74. {
  75. if (errorLine >= pos.start && errorLine <= pos.end)
  76. {
  77. return pos.sourceObject;
  78. }
  79. }
  80. return "";
  81. }
  82. private int GetScriptLine(int errorLine)
  83. {
  84. int start = sourcePositions[0].start;
  85. int end = sourcePositions[sourcePositions.Count - 1].end;
  86. if (errorLine >= start && errorLine <= end)
  87. return -1;
  88. if (errorLine > end)
  89. return errorLine - (end - start + 1);
  90. return errorLine;
  91. }
  92. internal static string ReplaceDataItems(Report report, string expression)
  93. {
  94. FindTextArgs args = new FindTextArgs();
  95. args.Text = new FastString(expression);
  96. args.OpenBracket = "[";
  97. args.CloseBracket = "]";
  98. while (args.StartIndex < args.Text.Length)
  99. {
  100. expression = CodeUtils.GetExpression(args, true);
  101. if (expression == null)
  102. break;
  103. if (DataHelper.IsValidColumn(report.Dictionary, expression))
  104. {
  105. Type type = DataHelper.GetColumnType(report.Dictionary, expression);
  106. expression = report.CodeHelper.ReplaceColumnName(expression, type);
  107. }
  108. else if (DataHelper.IsValidParameter(report.Dictionary, expression))
  109. {
  110. expression = report.CodeHelper.ReplaceParameterName(DataHelper.GetParameter(report.Dictionary, expression));
  111. }
  112. else if (DataHelper.IsValidTotal(report.Dictionary, expression))
  113. {
  114. expression = report.CodeHelper.ReplaceTotalName(expression);
  115. }
  116. else
  117. {
  118. expression = "[" + ReplaceDataItems(report, expression) + "]";
  119. }
  120. args.Text = args.Text.Remove(args.StartIndex, args.EndIndex - args.StartIndex);
  121. args.Text = args.Text.Insert(args.StartIndex, expression);
  122. args.StartIndex += expression.Length;
  123. }
  124. return args.Text.ToString();
  125. }
  126. private static bool ContainsAssembly(StringCollection assemblies, string assembly)
  127. {
  128. string asmName = Path.GetFileName(assembly);
  129. foreach (string a in assemblies)
  130. {
  131. string asmName1 = Path.GetFileName(a);
  132. if (String.Compare(asmName, asmName1, true) == 0)
  133. return true;
  134. }
  135. return false;
  136. }
  137. private static void AddFastReportAssemblies(StringCollection assemblies)
  138. {
  139. foreach (Assembly assembly in RegisteredObjects.Assemblies)
  140. {
  141. string aLocation = assembly.Location;
  142. #if CROSSPLATFORM || COREWIN
  143. if (aLocation == "")
  144. {
  145. // try fix SFA in FastReport.Compat
  146. string fixedReference = CodeDomProvider.TryFixReferenceInSingeFileApp(assembly);
  147. if (!string.IsNullOrEmpty(fixedReference))
  148. aLocation = fixedReference;
  149. }
  150. #endif
  151. if (!ContainsAssembly(assemblies, aLocation))
  152. assemblies.Add(aLocation);
  153. }
  154. }
  155. #if ASYNC
  156. private static async ValueTask AddFastReportAssemblies(StringCollection assemblies, CancellationToken token)
  157. {
  158. foreach (Assembly assembly in RegisteredObjects.Assemblies)
  159. {
  160. string aLocation = assembly.Location;
  161. #if CROSSPLATFORM || COREWIN
  162. if (aLocation == "")
  163. {
  164. // try fix SFA in FastReport.Compat
  165. string fixedReference = await CodeDomProvider.TryFixReferenceInSingeFileAppAsync(assembly, token);
  166. if (!string.IsNullOrEmpty(fixedReference))
  167. aLocation = fixedReference;
  168. }
  169. #endif
  170. if (!ContainsAssembly(assemblies, aLocation))
  171. assemblies.Add(aLocation);
  172. }
  173. }
  174. #endif
  175. private void AddReferencedAssemblies(StringCollection assemblies, string defaultPath)
  176. {
  177. for (int i = 0; i < Report.ReferencedAssemblies.Length; i++)
  178. {
  179. string s = Report.ReferencedAssemblies[i];
  180. #if CROSSPLATFORM
  181. if (s == "System.Windows.Forms.dll")
  182. s = "FastReport.Compat";
  183. #endif
  184. // fix for old reports with "System.Windows.Forms.DataVisualization" in referenced assemblies
  185. if (s.IndexOf("System.Windows.Forms.DataVisualization") != -1)
  186. s = "FastReport.DataVisualization";
  187. #if (SKIA && !AVALONIA)
  188. if (s.IndexOf("FastReport.Compat") != -1)
  189. s = "FastReport.Compat.Skia";
  190. if (s.IndexOf("FastReport.DataVisualization") != -1)
  191. s = "FastReport.DataVisualization.Skia";
  192. #endif
  193. AddReferencedAssembly(assemblies, defaultPath, s);
  194. }
  195. #if SKIA
  196. AddReferencedAssembly(assemblies, defaultPath, "FastReport.SkiaDrawing");
  197. #endif
  198. // these two required for "dynamic" type support
  199. AddReferencedAssembly(assemblies, defaultPath, "System.Core");
  200. AddReferencedAssembly(assemblies, defaultPath, "Microsoft.CSharp");
  201. }
  202. private void AddReferencedAssembly(StringCollection assemblies, string defaultPath, string assemblyName)
  203. {
  204. string location = GetFullAssemblyReference(assemblyName, defaultPath);
  205. if (location != "" && !ContainsAssembly(assemblies, location))
  206. assemblies.Add(location);
  207. }
  208. private string GetFullAssemblyReference(string relativeReference, string defaultPath)
  209. {
  210. // in .NET Core we get the AssemblyReference in FR.Compat
  211. #if !(CROSSPLATFORM || COREWIN)
  212. if (relativeReference == null || relativeReference.Trim() == "")
  213. return "";
  214. // Strip off any trailing ".dll" ".exe" if present.
  215. string dllName = relativeReference;
  216. if (string.Compare(relativeReference.Substring(relativeReference.Length - 4), ".dll", true) == 0 ||
  217. string.Compare(relativeReference.Substring(relativeReference.Length - 4), ".exe", true) == 0)
  218. dllName = relativeReference.Substring(0, relativeReference.Length - 4);
  219. // See if the required assembly is already present in our current AppDomain
  220. foreach (Assembly currAssembly in AppDomain.CurrentDomain.GetAssemblies())
  221. {
  222. if (string.Compare(currAssembly.GetName().Name, dllName, true) == 0)
  223. {
  224. // Found it, return the location as the full reference.
  225. return currAssembly.Location;
  226. }
  227. }
  228. // See if the required assembly is present in the ReferencedAssemblies but not yet loaded
  229. foreach (AssemblyName assemblyName in Assembly.GetExecutingAssembly().GetReferencedAssemblies())
  230. {
  231. if (string.Compare(assemblyName.Name, dllName, true) == 0)
  232. {
  233. // Found it, try to load assembly and return the location as the full reference.
  234. try
  235. {
  236. return Assembly.ReflectionOnlyLoad(assemblyName.FullName).Location;
  237. }
  238. catch { }
  239. }
  240. }
  241. // See if the required assembly is present locally
  242. string path = Path.Combine(defaultPath, relativeReference);
  243. if (File.Exists(path))
  244. return path;
  245. #endif
  246. return relativeReference;
  247. }
  248. private void AddExpression(string expression, Base source, bool forceSimpleItems)
  249. {
  250. if (expression.Trim() == "" || Expressions.ContainsKey(expression))
  251. return;
  252. string expr = expression;
  253. if (expr.StartsWith("[") && expr.EndsWith("]"))
  254. expr = expr.Substring(1, expr.Length - 2);
  255. // skip simple items. Report.Calc does this.
  256. if (!forceSimpleItems)
  257. {
  258. if (DataHelper.IsSimpleColumn(Report.Dictionary, expr) ||
  259. DataHelper.IsValidParameter(Report.Dictionary, expr) ||
  260. DataHelper.IsValidTotal(Report.Dictionary, expr))
  261. return;
  262. }
  263. // generate an error if a column that has been disabled is used
  264. Column column = DataHelper.GetColumn(Report.Dictionary, expr);
  265. if (column != null && !column.Enabled)
  266. expr += "Disable";
  267. // handle complex expressions, relations
  268. ExpressionDescriptor descriptor = new ExpressionDescriptor(this);
  269. Expressions.Add(expression, descriptor);
  270. descriptor.MethodName = "CalcExpression";
  271. if (DataHelper.IsValidColumn(Report.Dictionary, expr))
  272. expr = "[" + expr + "]";
  273. else
  274. expr = expression;
  275. string expressionCode = ReplaceDataItems(Report, expr);
  276. InsertItem(Report.CodeHelper.AddExpression(expression, expressionCode), source == null ? "" : source.Name);
  277. needCompile = true;
  278. }
  279. public void AddObjects()
  280. {
  281. ObjectCollection allObjects = Report.AllObjects;
  282. SortedList<string, Base> objects = new SortedList<string, Base>();
  283. // add all report objects
  284. InsertItem(Report.CodeHelper.AddField(typeof(Report), "Report") +
  285. Report.CodeHelper.AddField(typeof(ReportEngine), "Engine"), "Report");
  286. foreach (Base c in allObjects)
  287. {
  288. if (!String.IsNullOrEmpty(c.Name) && !objects.ContainsKey(c.Name))
  289. objects.Add(c.Name, c);
  290. }
  291. foreach (Base c in objects.Values)
  292. {
  293. InsertItem(Report.CodeHelper.AddField(c.GetType(), c.Name), c.Name);
  294. }
  295. // add custom script
  296. string processedCode = "";
  297. foreach (Base c in objects.Values)
  298. {
  299. string customCode = c.GetCustomScript();
  300. // avoid custom script duplicates
  301. if (!String.IsNullOrEmpty(customCode) && processedCode.IndexOf(customCode) == -1)
  302. {
  303. InsertItem(customCode, c.Name);
  304. processedCode += customCode;
  305. needCompile = true;
  306. }
  307. }
  308. }
  309. public void AddSingleExpression(string expression)
  310. {
  311. InsertItem(Report.CodeHelper.BeginCalcExpression(), "");
  312. AddExpression(expression, null, true);
  313. InsertItem(Report.CodeHelper.EndCalcExpression(), "");
  314. needCompile = true;
  315. }
  316. public void AddExpressions()
  317. {
  318. // speed up the case: lot of report objects (> 1000) and lot of data columns in the dictionary (> 10000).
  319. Report.Dictionary.CacheAllObjects = true;
  320. InsertItem(Report.CodeHelper.BeginCalcExpression(), "");
  321. ObjectCollection allObjects = Report.AllObjects;
  322. ObjectCollection l = Report.Dictionary.AllObjects;
  323. foreach (Base c in l)
  324. {
  325. allObjects.Add(c);
  326. }
  327. foreach (Base c in allObjects)
  328. {
  329. string[] expressions = c.GetExpressions();
  330. if (expressions != null)
  331. {
  332. foreach (string expr in expressions)
  333. {
  334. AddExpression(expr, c, false);
  335. }
  336. }
  337. }
  338. InsertItem(Report.CodeHelper.EndCalcExpression(), "");
  339. Report.Dictionary.CacheAllObjects = false;
  340. }
  341. public void AddFunctions()
  342. {
  343. List<FunctionInfo> list = new List<FunctionInfo>();
  344. RegisteredObjects.Functions.EnumItems(list);
  345. foreach (FunctionInfo info in list)
  346. {
  347. if (info.Function != null)
  348. {
  349. InsertItem(Report.CodeHelper.GetMethodSignatureAndBody(info.Function), "Function");
  350. }
  351. }
  352. }
  353. public string GenerateReportClass(string className)
  354. {
  355. InsertItem(Report.CodeHelper.GenerateInitializeMethod(), "");
  356. return Report.CodeHelper.ReplaceClassName(scriptText.ToString(), className);
  357. }
  358. public void Compile()
  359. {
  360. if (needCompile)
  361. {
  362. lock (compileLocker)
  363. {
  364. if (needCompile)
  365. InternalCompile();
  366. }
  367. }
  368. }
  369. #if ASYNC
  370. public async Task CompileAsync(CancellationToken token = default)
  371. {
  372. if (needCompile)
  373. {
  374. await semaphoreSlim.WaitAsync(token);
  375. if (needCompile)
  376. {
  377. try
  378. {
  379. await InternalCompileAsync(token);
  380. }
  381. finally
  382. {
  383. semaphoreSlim.Release();
  384. }
  385. }
  386. }
  387. }
  388. #endif
  389. private void InternalCompile()
  390. {
  391. CompilerParameters cp = GetCompilerParameters();
  392. if (Config.WebMode &&
  393. Config.EnableScriptSecurity &&
  394. Config.ScriptSecurityProps.AddStubClasses)
  395. AddStubClasses();
  396. string errors = string.Empty;
  397. CompilerResults cr;
  398. bool exception = !TryInternalCompile(cp, out cr);
  399. for (int i = 0; exception && i < Config.CompilerSettings.RecompileCount; i++)
  400. {
  401. exception = !TryRecompile(cp, ref cr);
  402. }
  403. if (cr != null)
  404. HandleCompileErrors(cr, out errors);
  405. if (exception && errors != string.Empty)
  406. throw new CompilerException(errors);
  407. }
  408. #if ASYNC
  409. private async Task InternalCompileAsync(CancellationToken cancellationToken)
  410. {
  411. CompilerParameters cp = await GetCompilerParametersAsync(cancellationToken);
  412. if (Config.WebMode &&
  413. Config.EnableScriptSecurity &&
  414. Config.ScriptSecurityProps.AddStubClasses)
  415. AddStubClasses();
  416. string errors = string.Empty;
  417. CompilerResults cr = await InternalCompileAsync(cp, cancellationToken);
  418. bool success = CheckCompileResult(cr);
  419. for (int i = 0; !success && i < Config.CompilerSettings.RecompileCount; i++)
  420. {
  421. cr = await TryRecompileAsync(cp, cr, cancellationToken);
  422. success = CheckCompileResult(cr);
  423. }
  424. if (cr != null)
  425. HandleCompileErrors(cr, out errors);
  426. if (!success && errors != string.Empty)
  427. throw new CompilerException(errors);
  428. }
  429. private static bool CheckCompileResult(CompilerResults result)
  430. {
  431. // if result == null => was found in cache
  432. return result == null || result.Errors.Count == 0;
  433. }
  434. #endif
  435. private CompilerParameters GetCompilerParameters()
  436. {
  437. // configure compiler options
  438. CompilerParameters cp = new CompilerParameters();
  439. AddFastReportAssemblies(cp.ReferencedAssemblies); // 2
  440. AddReferencedAssemblies(cp.ReferencedAssemblies, currentFolder); // 9
  441. ReviewReferencedAssemblies(cp.ReferencedAssemblies);
  442. cp.GenerateInMemory = true;
  443. // sometimes the system temp folder is not accessible...
  444. if (Config.TempFolder != null)
  445. cp.TempFiles = new TempFileCollection(Config.TempFolder, false);
  446. return cp;
  447. }
  448. #if ASYNC
  449. private async Task<CompilerParameters> GetCompilerParametersAsync(CancellationToken ct)
  450. {
  451. // configure compiler options
  452. CompilerParameters cp = new CompilerParameters();
  453. await AddFastReportAssemblies(cp.ReferencedAssemblies, ct); // 2
  454. AddReferencedAssemblies(cp.ReferencedAssemblies, currentFolder); // 9
  455. ReviewReferencedAssemblies(cp.ReferencedAssemblies);
  456. cp.GenerateInMemory = true;
  457. // sometimes the system temp folder is not accessible...
  458. if (Config.TempFolder != null)
  459. cp.TempFiles = new TempFileCollection(Config.TempFolder, false);
  460. return cp;
  461. }
  462. #endif
  463. private string GetAssemblyHash(CompilerParameters cp)
  464. {
  465. StringBuilder assemblyHashSB = new StringBuilder();
  466. foreach (string a in cp.ReferencedAssemblies)
  467. assemblyHashSB.Append(a);
  468. assemblyHashSB.Append(scriptText);
  469. byte[] hash;
  470. using (HMACSHA1 hMACSHA1 = new HMACSHA1(Encoding.ASCII.GetBytes(shaKey)))
  471. {
  472. hash = hMACSHA1.ComputeHash(Encoding.Unicode.GetBytes(assemblyHashSB.ToString()));
  473. }
  474. return Convert.ToBase64String(hash);
  475. }
  476. /// <summary>
  477. /// Returns true, if compilation is successful
  478. /// </summary>
  479. private bool TryInternalCompile(CompilerParameters cp, out CompilerResults cr)
  480. {
  481. // find assembly in cache
  482. string assemblyHash = GetAssemblyHash(cp);
  483. Assembly cachedAssembly;
  484. if (FAssemblyCache.TryGetValue(assemblyHash, out cachedAssembly))
  485. {
  486. Assembly = cachedAssembly;
  487. var reportScript = Assembly.CreateInstance("FastReport.ReportScript");
  488. InitInstance(reportScript);
  489. cr = null;
  490. return true;
  491. }
  492. // compile report scripts
  493. using (CodeDomProvider provider = Report.CodeHelper.GetCodeProvider())
  494. {
  495. string script = scriptText.ToString();
  496. ScriptSecurityEventArgs ssea = new ScriptSecurityEventArgs(Report, script, Report.ReferencedAssemblies);
  497. Config.OnScriptCompile(ssea);
  498. #if CROSSPLATFORM || COREWIN
  499. provider.BeforeEmitCompilation += Config.OnBeforeScriptCompilation;
  500. // in .NET Core we use cultureInfo to represent errors
  501. cr = provider.CompileAssemblyFromSource(cp, script, Config.CompilerSettings.CultureInfo);
  502. #else
  503. cr = provider.CompileAssemblyFromSource(cp, script);
  504. #endif
  505. Assembly = null;
  506. Instance = null;
  507. if (cr.Errors.Count != 0) // Compile errors
  508. return false;
  509. FAssemblyCache.TryAdd(assemblyHash, cr.CompiledAssembly);
  510. Assembly = cr.CompiledAssembly;
  511. var reportScript = Assembly.CreateInstance("FastReport.ReportScript");
  512. InitInstance(reportScript);
  513. return true;
  514. }
  515. }
  516. #if ASYNC && (CROSSPLATFORM || COREWIN)
  517. /// <summary>
  518. /// Returns true, if compilation is successful
  519. /// </summary>
  520. private async ValueTask<CompilerResults> InternalCompileAsync(CompilerParameters cp, CancellationToken cancellationToken)
  521. {
  522. CompilerResults cr;
  523. // find assembly in cache
  524. string assemblyHash = GetAssemblyHash(cp);
  525. Assembly cachedAssembly;
  526. if (FAssemblyCache.TryGetValue(assemblyHash, out cachedAssembly))
  527. {
  528. Assembly = cachedAssembly;
  529. var reportScript = Assembly.CreateInstance("FastReport.ReportScript");
  530. InitInstance(reportScript);
  531. cr = null;
  532. return cr; // return true;
  533. }
  534. // compile report scripts
  535. using (CodeDomProvider provider = Report.CodeHelper.GetCodeProvider())
  536. {
  537. string script = scriptText.ToString();
  538. ScriptSecurityEventArgs ssea = new ScriptSecurityEventArgs(Report, script, Report.ReferencedAssemblies);
  539. Config.OnScriptCompile(ssea);
  540. provider.BeforeEmitCompilation += Config.OnBeforeScriptCompilation;
  541. cr = await provider.CompileAssemblyFromSourceAsync(cp, script, Config.CompilerSettings.CultureInfo, cancellationToken);
  542. Assembly = null;
  543. Instance = null;
  544. if (cr.Errors.Count != 0) // Compile errors
  545. return cr; // return false;
  546. FAssemblyCache.TryAdd(assemblyHash, cr.CompiledAssembly);
  547. Assembly = cr.CompiledAssembly;
  548. var reportScript = Assembly.CreateInstance("FastReport.ReportScript");
  549. InitInstance(reportScript);
  550. return cr;
  551. }
  552. }
  553. #endif
  554. private string ReplaceExpression(string error, TextObjectBase text)
  555. {
  556. string result = text.Text;
  557. string[] parts = error.Split('\"');
  558. if (parts.Length == 3)
  559. {
  560. string[] expressions = text.GetExpressions();
  561. foreach (string expr in expressions)
  562. {
  563. if (expr.Contains(parts[1]))
  564. {
  565. if (!DataHelper.IsValidColumn(Report.Dictionary, expr))
  566. {
  567. string replaceString = text.Brackets[0] + expr + text.Brackets[2];
  568. if (Config.CompilerSettings.ExceptionBehaviour == CompilerExceptionBehaviour.ShowExceptionMessage ||
  569. Config.CompilerSettings.ExceptionBehaviour == CompilerExceptionBehaviour.ReplaceExpressionWithPlaceholder)
  570. {
  571. result = result.Replace(replaceString, Config.CompilerSettings.Placeholder);
  572. }
  573. else if (Config.CompilerSettings.ExceptionBehaviour == CompilerExceptionBehaviour.ReplaceExpressionWithExceptionMessage)
  574. {
  575. result = result.Replace(replaceString, error);
  576. }
  577. }
  578. }
  579. }
  580. }
  581. return result;
  582. }
  583. /// <summary>
  584. /// Handle compile errors
  585. /// </summary>
  586. private void HandleCompileErrors(CompilerResults cr, out string errors)
  587. {
  588. errors = string.Empty;
  589. Regex regex;
  590. if (Config.WebMode && Config.EnableScriptSecurity)
  591. {
  592. for (int i = 0; i < cr.Errors.Count;)
  593. {
  594. CompilerError ce = cr.Errors[i];
  595. if (ce.ErrorNumber == "CS1685") // duplicate class
  596. {
  597. cr.Errors.Remove(ce);
  598. continue;
  599. }
  600. else if (ce.ErrorNumber == "CS0436") // user using a forbidden type
  601. {
  602. const string pattern = "[\"'](\\S+)[\"']";
  603. regex = new Regex(pattern, RegexOptions.Compiled);
  604. string typeName = regex.Match(ce.ErrorText).Value;
  605. const string res = "Web,ScriptSecurity,ForbiddenType";
  606. string message = Res.TryGet(res);
  607. if (string.Equals(res, message))
  608. message = "Please don't use the type " + typeName;
  609. else
  610. message = message.Replace("{typeName}", typeName); //$"Please don't use the type {typeName}";
  611. ce.ErrorText = message;
  612. }
  613. else if (ce.ErrorNumber == "CS0117") // user using a forbidden method
  614. {
  615. const string pattern = "[\"'](\\S+)[\"']";
  616. regex = new Regex(pattern, RegexOptions.Compiled);
  617. MatchCollection mathes = regex.Matches(ce.ErrorText);
  618. if (mathes.Count > 1)
  619. {
  620. string methodName = mathes[1].Value;
  621. const string res = "Web,ScriptSecurity,ForbiddenMethod";
  622. string message = Res.TryGet(res);
  623. if (string.Equals(res, message))
  624. message = "Please don't use the method " + methodName;
  625. else
  626. message = message.Replace("{methodName}", methodName); //$"Please don't use the method {methodName}";
  627. ce.ErrorText = message;
  628. }
  629. }
  630. i++;
  631. }
  632. }
  633. foreach (CompilerError ce in cr.Errors)
  634. {
  635. int line = GetScriptLine(ce.Line);
  636. // error is inside own items
  637. if (line == -1)
  638. {
  639. string errObjName = GetErrorObjectName(ce.Line);
  640. if (Config.CompilerSettings.ExceptionBehaviour != CompilerExceptionBehaviour.Default)
  641. {
  642. // handle errors when name does not exist in the current context
  643. if (ce.ErrorNumber == "CS0103")
  644. {
  645. TextObjectBase text = Report.FindObject(errObjName) as TextObjectBase;
  646. text.Text = ReplaceExpression(ce.ErrorText, text);
  647. if (Config.CompilerSettings.ExceptionBehaviour == CompilerExceptionBehaviour.ShowExceptionMessage)
  648. System.Windows.Forms.MessageBox.Show(ce.ErrorText);
  649. continue;
  650. }
  651. }
  652. // handle division by zero errors
  653. if (ce.ErrorNumber == "CS0020")
  654. {
  655. TextObjectBase text = Report.FindObject(errObjName) as TextObjectBase;
  656. text.CanGrow = true;
  657. text.FillColor = Color.Red;
  658. text.Text = "DIVISION BY ZERO!";
  659. continue;
  660. }
  661. else
  662. {
  663. errors += $"({errObjName}): {Res.Get("Messages,Error")} {ce.ErrorNumber}: {ce.ErrorText}\r\n";
  664. ErrorMsg(errObjName, ce);
  665. }
  666. }
  667. else
  668. {
  669. errors += $"({line},{ce.Column}): {Res.Get("Messages,Error")} {ce.ErrorNumber}: {ce.ErrorText}\r\n";
  670. ErrorMsg(ce, line);
  671. }
  672. }
  673. }
  674. /// <summary>
  675. /// Returns true if recompilation is successful
  676. /// </summary>
  677. private bool TryRecompile(CompilerParameters cp, ref CompilerResults cr)
  678. {
  679. List<string> additionalAssemblies = new List<string>(4);
  680. foreach (CompilerError ce in cr.Errors)
  681. {
  682. if (ce.ErrorNumber == "CS0012") // missing reference on assembly
  683. {
  684. // try to add reference
  685. try
  686. {
  687. // in .Net Core compiler will return other quotes
  688. #if CROSSPLATFORM || COREWIN
  689. const string quotes = "\'";
  690. #else
  691. const string quotes = "\"";
  692. #endif
  693. const string pattern = quotes + @"(\S{1,}),";
  694. Regex regex = new Regex(pattern, RegexOptions.Compiled);
  695. string assemblyName = regex.Match(ce.ErrorText).Groups[1].Value; // Groups[1] include string without quotes and , symbols
  696. if (!additionalAssemblies.Contains(assemblyName))
  697. additionalAssemblies.Add(assemblyName);
  698. continue;
  699. }
  700. catch { }
  701. }
  702. }
  703. if (additionalAssemblies.Count > 0) // need recompile
  704. {
  705. // try to load missing assemblies
  706. foreach (string assemblyName in additionalAssemblies)
  707. {
  708. AddReferencedAssembly(cp.ReferencedAssemblies, currentFolder, assemblyName);
  709. }
  710. return TryInternalCompile(cp, out cr);
  711. }
  712. return false;
  713. }
  714. #if ASYNC
  715. /// <summary>
  716. /// Returns true if recompilation is successful
  717. /// </summary>
  718. private async Task<CompilerResults> TryRecompileAsync(CompilerParameters cp, CompilerResults oldResult, CancellationToken ct)
  719. {
  720. List<string> additionalAssemblies = new List<string>(4);
  721. foreach (CompilerError ce in oldResult.Errors)
  722. {
  723. if (ce.ErrorNumber == "CS0012") // missing reference on assembly
  724. {
  725. // try to add reference
  726. try
  727. {
  728. // in .Net Core compiler will return other quotes
  729. #if CROSSPLATFORM || COREWIN
  730. const string quotes = "\'";
  731. #else
  732. const string quotes = "\"";
  733. #endif
  734. const string pattern = quotes + @"(\S{1,}),";
  735. Regex regex = new Regex(pattern, RegexOptions.Compiled);
  736. string assemblyName = regex.Match(ce.ErrorText).Groups[1].Value; // Groups[1] include string without quotes and , symbols
  737. if (!additionalAssemblies.Contains(assemblyName))
  738. additionalAssemblies.Add(assemblyName);
  739. continue;
  740. }
  741. catch { }
  742. }
  743. }
  744. if (additionalAssemblies.Count > 0) // need recompile
  745. {
  746. // try to load missing assemblies
  747. foreach (string assemblyName in additionalAssemblies)
  748. {
  749. AddReferencedAssembly(cp.ReferencedAssemblies, currentFolder, assemblyName);
  750. }
  751. return await InternalCompileAsync(cp, ct);
  752. }
  753. return oldResult;
  754. }
  755. #endif
  756. public void InitInstance(object instance)
  757. {
  758. this.Instance = instance;
  759. InitFields();
  760. }
  761. public bool ContainsExpression(string expr)
  762. {
  763. return Expressions.ContainsKey(expr);
  764. }
  765. public object CalcExpression(string expr, Variant value)
  766. {
  767. ExpressionDescriptor expressionDescriptor = Expressions[expr] as ExpressionDescriptor;
  768. if (expressionDescriptor != null)
  769. return expressionDescriptor.Invoke(new object[] { expr, value });
  770. else
  771. return null;
  772. }
  773. public object InvokeMethod(string name, object[] parms)
  774. {
  775. if (String.IsNullOrEmpty(name))
  776. return null;
  777. string exprName = "method_" + name;
  778. if (!ContainsExpression(exprName))
  779. {
  780. ExpressionDescriptor descriptor = new ExpressionDescriptor(this);
  781. Expressions.Add(exprName, descriptor);
  782. descriptor.MethodName = name;
  783. }
  784. try
  785. {
  786. return (Expressions[exprName] as ExpressionDescriptor).Invoke(parms);
  787. }
  788. catch (TargetInvocationException ex)
  789. {
  790. throw (ex.InnerException); // ex now stores the original success
  791. }
  792. }
  793. public void Dispose()
  794. {
  795. #if ASYNC
  796. semaphoreSlim.Dispose();
  797. #endif
  798. }
  799. public AssemblyDescriptor(Report report, string scriptText)
  800. {
  801. this.Report = report;
  802. this.scriptText = new StringBuilder(scriptText);
  803. Expressions = new Hashtable();
  804. sourcePositions = new List<SourcePosition>();
  805. insertPos = Report.CodeHelper.GetPositionToInsertOwnItems(scriptText);
  806. if (insertPos == -1)
  807. {
  808. string msg = Res.Get("Messages,ClassError");
  809. ErrorMsg(msg);
  810. throw new CompilerException(msg);
  811. }
  812. else
  813. {
  814. string[] lines = scriptText.Substring(0, insertPos).Split('\r');
  815. insertLine = lines.Length;
  816. if (scriptText != Report.CodeHelper.EmptyScript())
  817. needCompile = true;
  818. }
  819. // set the current folder
  820. currentFolder = Config.ApplicationFolder;
  821. if (Config.WebMode)
  822. {
  823. try
  824. {
  825. string bin_directory = Path.Combine(currentFolder, "Bin");
  826. if (Directory.Exists(bin_directory))
  827. currentFolder = bin_directory;
  828. }
  829. catch
  830. {
  831. }
  832. }
  833. // Commented by Samuray
  834. //Directory.SetCurrentDirectory(currentFolder);
  835. }
  836. static AssemblyDescriptor()
  837. {
  838. FAssemblyCache = new ConcurrentDictionary<string, Assembly>();
  839. compileLocker = new object();
  840. }
  841. private sealed class SourcePosition
  842. {
  843. public readonly string sourceObject;
  844. public readonly int start;
  845. public readonly int end;
  846. public SourcePosition(string obj, int start, int end)
  847. {
  848. sourceObject = obj;
  849. this.start = start;
  850. this.end = end;
  851. }
  852. }
  853. }
  854. }