/*
Copyright (C) 2014-2019 de4dot@gmail.com
This file is part of dnSpy
dnSpy is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
dnSpy is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with dnSpy. If not, see .
*/
using System;
using System.Threading;
using System.Windows;
using System.Windows.Threading;
using dnSpy.Contracts.App;
using dnSpy.Contracts.Text;
namespace dnSpy.Contracts.Scripting.Roslyn {
///
/// The script's global class
///
public interface IScriptGlobals : ITextPrinter {
///
/// Returns itself so it can be passed into classes that can't access the globals
///
IScriptGlobals Instance { get; }
///
/// Raised before the script gets reset. Can be used to unregister from events to prevent
/// memory leaks. Raised on the UI thread.
///
event EventHandler? ScriptResetting;
///
/// Cancellation token that gets signalled when the script gets reset
///
CancellationToken Token { get; }
// NOTE: We have to add the same ITextPrinter methods and props here because Roslyn only
// allows global access to everything defined in this interface, and not every public
// method and property the global object defines.
///
/// Print options
///
new IPrintOptions PrintOptions { get; }
///
/// Prints text to the screen
///
/// Text
new void PrintError(string? text);
///
/// Prints text to the screen
///
/// Format
/// Args
new void PrintError(string fmt, params object[] args);
///
/// Prints text followed by a new line to the screen
///
/// Text or null
new void PrintLineError(string? text);
///
/// Prints text followed by a new line to the screen
///
/// Format
/// Args
new void PrintLineError(string fmt, params object[] args);
///
/// Prints text to the screen
///
/// Color
/// Text
new void Print(object? color, string? text);
///
/// Prints text to the screen
///
/// Color
/// Text
new void Print(TextColor color, string? text);
///
/// Prints text to the screen
///
/// Text
new void Print(string? text);
///
/// Prints text to the screen
///
/// Color
/// Format
/// Args
new void Print(object? color, string fmt, params object[] args);
///
/// Prints text to the screen
///
/// Color
/// Format
/// Args
new void Print(TextColor color, string fmt, params object[] args);
///
/// Prints text to the screen
///
/// Format
/// Args
new void Print(string fmt, params object[] args);
///
/// Prints text followed by a new line to the screen
///
/// Color
/// Text or null
new void PrintLine(object? color, string? text);
///
/// Prints text followed by a new line to the screen
///
/// Color
/// Text or null
new void PrintLine(TextColor color, string? text);
///
/// Prints text followed by a new line to the screen
///
/// Text or null
new void PrintLine(string? text = null);
///
/// Prints text followed by a new line to the screen
///
/// Color
/// Format
/// Args
new void PrintLine(object? color, string fmt, params object[] args);
///
/// Prints text followed by a new line to the screen
///
/// Color
/// Format
/// Args
new void PrintLine(TextColor color, string fmt, params object[] args);
///
/// Prints text followed by a new line to the screen
///
/// Format
/// Args
new void PrintLine(string fmt, params object[] args);
///
/// Formats and prints a value to the screen
///
/// Value, can be null
/// Color
new void Print(object value, object? color);
///
/// Formats and prints a value to the screen
///
/// Value, can be null
/// Color
new void Print(object value, TextColor color = TextColor.ReplScriptOutputText);
///
/// Formats and prints a value followed by a new line to the screen
///
/// Value or null
/// Color
new void PrintLine(object value, object? color);
///
/// Formats and prints a value followed by a new line to the screen
///
/// Value or null
/// Color
new void PrintLine(object value, TextColor color = TextColor.ReplScriptOutputText);
///
/// Formats and prints an exception to the screen
///
/// Exception
/// Color
new void Print(Exception ex, object? color);
///
/// Formats and prints an exception to the screen
///
/// Exception
/// Color
new void Print(Exception ex, TextColor color = TextColor.Error);
///
/// Formats and prints an exception followed by a new line to the screen
///
/// Exception
/// Color
new void PrintLine(Exception ex, object? color);
///
/// Formats and prints an exception followed by a new line to the screen
///
/// Exception
/// Color
new void PrintLine(Exception ex, TextColor color = TextColor.Error);
///
/// UI thread dispatcher
///
Dispatcher UIDispatcher { get; }
///
/// Creates a new instance. Useful if your script runs in the
/// background and prints text.
///
///
ICachedWriter CreateWriter();
///
/// Executes on the UI thread
///
/// Code
void UI(Action action);
///
/// Executes on the UI thread
///
/// Return type
/// Code
///
T UI(Func func);
///
/// Calls . Use dnSpy to debug itself
/// (dnSpy --multiple) and then call this method from your script in the debugged dnSpy process.
///
void Break();
///
/// Resolves a service, and throws if it wasn't found
///
/// Type of service
///
T Resolve() where T : class;
///
/// Resolves a service or returns null if not found
///
/// Type of service
///
T? TryResolve() where T : class;
///
/// Shows a message box
///
/// Message to show
/// Buttons that should be present
/// Owner window or null to use the main window
///
MsgBoxButton Show(string message, MsgBoxButton buttons = MsgBoxButton.OK, Window? ownerWindow = null);
///
/// Shows a message box with buttons OK and Cancel
///
/// Message to show
/// Owner window or null to use the main window
///
MsgBoxButton ShowOKCancel(string message, Window? ownerWindow = null);
///
/// Shows a message box with buttons OK and Cancel
///
/// Message to show
/// Owner window or null to use the main window
///
MsgBoxButton ShowOC(string message, Window? ownerWindow = null);
///
/// Shows a message box with buttons Yes and No
///
/// Message to show
/// Owner window or null to use the main window
///
MsgBoxButton ShowYesNo(string message, Window? ownerWindow = null);
///
/// Shows a message box with buttons Yes and No
///
/// Message to show
/// Owner window or null to use the main window
///
MsgBoxButton ShowYN(string message, Window? ownerWindow = null);
///
/// Shows a message box with buttons Yes, No and Cancel
///
/// Message to show
/// Owner window or null to use the main window
///
MsgBoxButton ShowYesNoCancel(string message, Window? ownerWindow = null);
///
/// Shows a message box with buttons Yes, No and Cancel
///
/// Message to show
/// Owner window or null to use the main window
///
MsgBoxButton ShowYNC(string message, Window? ownerWindow = null);
///
/// Asks the user for a value and returns it or the default value (eg. null or 0) if the
/// user canceled the dialog box.
///
/// Type
/// Label
/// Default text to write to the textbox or null
/// Title or null
/// Converts a string to the type, or null to use the default
/// converter.
/// Verifies the typed message. Returns null or an empty string if
/// it's a valid value, else an error message to show to the user.
/// Owner window or null to use the main window
///
T Ask(string labelMessage, string? defaultText = null, string? title = null, Func? converter = null, Func? verifier = null, Window? ownerWindow = null);
///
/// Shows an exception message
///
/// Exception
/// Message to show or null
/// Owner window or null to use the main window
void Show(Exception exception, string? msg = null, Window? ownerWindow = null);
}
}