public static Assembly Compile(CompilerInputData compilerInputData) { if (string.IsNullOrWhiteSpace(compilerInputData.CSharpCode)) { throw new Exception("CSharpCode parameter can`t be null or empty."); } string assemblyName; if (string.IsNullOrWhiteSpace(compilerInputData.AssemblyName)) { assemblyName = "CompiledAssembly_" + TextExtensions.Generate(10); } else { assemblyName = compilerInputData.AssemblyName; } SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(compilerInputData.CSharpCode); var references = GetMetadataReferences(compilerInputData.ReferencedAssemblies); OptimizationLevel optLevel = OptimizationLevel.Release; #if DEBUG //optLevel = OptimizationLevel.Debug; #endif CSharpCompilation compilation = CSharpCompilation.Create( assemblyName, syntaxTrees: new[] { syntaxTree }, references: references, options: new CSharpCompilationOptions( OutputKind.DynamicallyLinkedLibrary, optimizationLevel: optLevel ) ); using (var ms = new MemoryStream()) { EmitResult result = compilation.Emit(ms); if (!result.Success) { IEnumerable <Diagnostic> failures = result.Diagnostics.Where(diagnostic => diagnostic.IsWarningAsError || diagnostic.Severity == DiagnosticSeverity.Error); string compileErrors = ""; foreach (Diagnostic diagnostic in failures) { compileErrors += $"\n *{diagnostic.Id}: {diagnostic.GetMessage()};"; } throw new CodeGenException("Exception while trying to compile script." + compileErrors); } else { ms.Seek(0, SeekOrigin.Begin); Assembly assembly = Assembly.Load(ms.ToArray()); return(assembly); } } }
string SaveFileToTempDirectory(Stream stream) { if (!Directory.Exists(TempDirPath)) { Directory.CreateDirectory(TempDirPath); } var resName = TextExtensions.Generate(10); var filePath = Path.Combine(TempDirPath, resName); using (var fileStream = File.Create(filePath)) { stream.Seek(0, SeekOrigin.Begin); stream.CopyTo(fileStream); } return(filePath); }
/// <summary> /// Send message with reply markup and use <see cref="ProcessUpdate(ITelegramBotClient, Update)"/> /// to process updates. /// </summary> /// <returns></returns> IReplyMarkup GenerateReplyMarkup() { if (IsInlineKeyboard) { var dict = new Dictionary <string, ButtonInfo>(); var list = new List <IList <InlineKeyboardButton> >(); var buttonInfoCollection = GetButtons(); foreach (var smallCol in buttonInfoCollection) { var smallList = new List <InlineKeyboardButton>(); foreach (var btnInfo in smallCol) { if (!btnInfo.Visible) { continue; } var key = TextExtensions.Generate(10); dict[key] = btnInfo; dict[btnInfo.Text] = btnInfo; var btn = (btnInfo.Button as InlineKeyboardButton) ?? new InlineKeyboardButton(); btn.Text = btnInfo.Text; btn.CallbackData = key; btnInfo.Button = btn; smallList.Add(btn); } if (smallList.Any()) { list.Add(smallList); } } _buttonsByDataKey = dict; return(new InlineKeyboardMarkup(list)); } else { var dict = new Dictionary <string, ButtonInfo>(); var list = new List <IList <KeyboardButton> >(); var buttonInfoCollection = GetButtons(); foreach (var smallCol in buttonInfoCollection) { var smallList = new List <KeyboardButton>(); foreach (var btnInfo in smallCol) { if (!btnInfo.Visible) { continue; } dict[btnInfo.Text] = btnInfo; var btn = (btnInfo.Button as KeyboardButton) ?? new KeyboardButton(); btn.Text = btnInfo.Text; btnInfo.Button = btn; smallList.Add(btn); } if (smallList.Any()) { list.Add(smallList); } } _buttonsByDataKey = dict; return(new ReplyKeyboardMarkup(list, ResizeKeyboard)); } }
public CommonService() { _randomStr = TextExtensions.Generate(5); }