/// <summary> /// Render TextFile control. /// Return true if failed. /// </summary> /// <param name="r.Canvas">Parent r.Canvas</param> /// <param name="uiCmd">UICommand</param> /// <returns>Success = false, Failure = true</returns> public static void RenderTextFile(RenderInfo r, UICommand uiCmd) { Debug.Assert(uiCmd.Info.GetType() == typeof(UIInfo_TextFile)); UIInfo_TextFile info = uiCmd.Info as UIInfo_TextFile; TextBox textBox; using (MemoryStream ms = EncodedFile.ExtractInterfaceEncoded(uiCmd.Addr.Plugin, uiCmd.Text)) using (StreamReader sr = new StreamReader(ms, FileHelper.DetectTextEncoding(ms))) { textBox = new TextBox() { TextWrapping = TextWrapping.Wrap, AcceptsReturn = true, IsReadOnly = true, Text = sr.ReadToEnd(), FontSize = CalcFontPointScale(), }; } ScrollViewer.SetHorizontalScrollBarVisibility(textBox, ScrollBarVisibility.Auto); ScrollViewer.SetVerticalScrollBarVisibility(textBox, ScrollBarVisibility.Auto); ScrollViewer.SetCanContentScroll(textBox, true); SetToolTip(textBox, info.ToolTip); DrawToCanvas(r, textBox, uiCmd.Rect); }
public void ExtractInterfaceEncoded_1() { // Type 1 EngineState s = EngineTests.CreateEngineState(); string pPath = Path.Combine("%TestBench%", "EncodedFile", "ExtractFileTests.script"); pPath = StringEscaper.Preprocess(s, pPath); Plugin p = s.Project.LoadPluginMonkeyPatch(pPath); byte[] extractDigest; using (MemoryStream ms = EncodedFile.ExtractInterfaceEncoded(p, "PEBakeryAlphaMemory.jpg")) { extractDigest = HashHelper.CalcHash(HashType.SHA256, ms); } string originFile = Path.Combine("%TestBench%", "EncodedFile", "PEBakeryAlphaMemory.jpg"); originFile = StringEscaper.Preprocess(s, originFile); byte[] originDigest; using (FileStream fs = new FileStream(originFile, FileMode.Open)) { originDigest = HashHelper.CalcHash(HashType.SHA256, fs); } // Compare Hash Assert.IsTrue(originDigest.SequenceEqual(extractDigest)); }
/// <summary> /// Render Button control. /// Return true if failed. /// </summary> /// <param name="r.Canvas">Parent r.Canvas</param> /// <param name="uiCmd">UICommand</param> /// <returns>Success = false, Failure = true</returns> public static void RenderButton(RenderInfo r, UICommand uiCmd, Logger logger) { Debug.Assert(uiCmd.Info.GetType() == typeof(UIInfo_Button)); UIInfo_Button info = uiCmd.Info as UIInfo_Button; Button button = new Button() { HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center, }; button.Click += (object sender, RoutedEventArgs e) => { if (r.Plugin.Sections.ContainsKey(info.SectionName)) // Only if section exists { SectionAddress addr = new SectionAddress(r.Plugin, r.Plugin.Sections[info.SectionName]); UIRenderer.RunOneSection(addr, $"{r.Plugin.Title} - Button [{uiCmd.Key}]", info.ShowProgress); } else { Application.Current.Dispatcher.Invoke((Action)(() => { MainWindow w = Application.Current.MainWindow as MainWindow; w.Logger.System_Write(new LogInfo(LogState.Error, $"Section [{info.SectionName}] does not exists")); })); } }; if (info.Picture != null && uiCmd.Addr.Plugin.Sections.ContainsKey($"EncodedFile-InterfaceEncoded-{info.Picture}")) { // Has Picture if (ImageHelper.GetImageType(info.Picture, out ImageHelper.ImageType type)) { return; } Image image = new Image() { StretchDirection = StretchDirection.DownOnly, Stretch = Stretch.Uniform, UseLayoutRounding = true, }; RenderOptions.SetBitmapScalingMode(image, BitmapScalingMode.HighQuality); using (MemoryStream ms = EncodedFile.ExtractInterfaceEncoded(uiCmd.Addr.Plugin, info.Picture)) { if (type == ImageHelper.ImageType.Svg) { ImageHelper.GetSvgSize(ms, out double width, out double height); image.Source = ImageHelper.SvgToBitmapImage(ms, width, height); } else { image.Source = ImageHelper.ImageToBitmapImage(ms); } } if (uiCmd.Text.Equals(string.Empty, StringComparison.Ordinal)) { // No text, just image button.Content = image; } else { // Button has text StackPanel panel = new StackPanel() { HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center, Orientation = Orientation.Horizontal, }; TextBlock text = new TextBlock() { Text = uiCmd.Text, FontSize = CalcFontPointScale(), Height = double.NaN, VerticalAlignment = VerticalAlignment.Center, Margin = new Thickness(CalcFontPointScale() / 2, 0, 0, 0), }; panel.Children.Add(image); panel.Children.Add(text); button.Content = panel; } } else { // No picture button.Content = uiCmd.Text; button.FontSize = CalcFontPointScale(); } SetToolTip(button, info.ToolTip); DrawToCanvas(r, button, uiCmd.Rect); }
/// <summary> /// Render Image control. /// Return true if failed. /// </summary> /// <param name="r.Canvas">Parent r.Canvas</param> /// <param name="uiCmd">UICommand</param> /// <returns>Success = false, Failure = true</returns> public static void RenderImage(RenderInfo r, UICommand uiCmd) { Debug.Assert(uiCmd.Info.GetType() == typeof(UIInfo_Image)); UIInfo_Image info = uiCmd.Info as UIInfo_Image; Image image = new Image() { StretchDirection = StretchDirection.DownOnly, Stretch = Stretch.Uniform, UseLayoutRounding = true, }; RenderOptions.SetBitmapScalingMode(image, BitmapScalingMode.HighQuality); Button button; // double width, height; using (MemoryStream ms = EncodedFile.ExtractInterfaceEncoded(uiCmd.Addr.Plugin, uiCmd.Text)) { if (ImageHelper.GetImageType(uiCmd.Text, out ImageHelper.ImageType type)) { return; } button = new Button() { Style = (Style)r.Window.FindResource("ImageButton") }; if (type == ImageHelper.ImageType.Svg) { double width = uiCmd.Rect.Width * r.MasterScale; double height = uiCmd.Rect.Height * r.MasterScale; button.Background = ImageHelper.SvgToImageBrush(ms, width, height); } else { button.Background = ImageHelper.ImageToImageBrush(ms); } } bool hasUrl = false; if (info.URL != null && string.Equals(info.URL, string.Empty, StringComparison.Ordinal) == false) { if (Uri.TryCreate(info.URL, UriKind.Absolute, out Uri unused)) { // Success hasUrl = true; } else { // Failure throw new InvalidUICommandException($"Invalid URL [{info.URL}]", uiCmd); } } if (hasUrl) { // Open URL button.Click += (object sender, RoutedEventArgs e) => { Process.Start(info.URL); }; } else { // Open picture with external viewer button.Click += (object sender, RoutedEventArgs e) => { if (ImageHelper.GetImageType(uiCmd.Text, out ImageHelper.ImageType t)) { return; } string path = System.IO.Path.ChangeExtension(System.IO.Path.GetTempFileName(), "." + t.ToString().ToLower()); using (MemoryStream ms = EncodedFile.ExtractInterfaceEncoded(uiCmd.Addr.Plugin, uiCmd.Text)) using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write)) { ms.Position = 0; ms.CopyTo(fs); } ProcessStartInfo procInfo = new ProcessStartInfo() { Verb = "open", FileName = path, UseShellExecute = true }; Process.Start(procInfo); }; } SetToolTip(button, info.ToolTip); DrawToCanvas(r, button, uiCmd.Rect); }