source:trunk/Documentation/Developer/PluginHowTo/part2.tex@845

Last change on this file since 845 was 845, checked in by Arno Wacker, 12 years ago

PluginHowTo

• Major style update - now looks fancy (chapter 1 demonstrates how it should look)
• Minor exemplary changes in chapter 2 (this is pretty much a construction site here..)
File size: 26.4 KB
Line
1\chapter{Plugin Implementation}
2In this chapter we provide step-by-step instructions for implementing your own CrypTool 2.0 plugin. The given instructions refer to the usage of MS Visual Studio 2008 Professional, hence before starting you should have installed your copy of MS Visual Studio 2008.
3
4
5
6\section{New project}
7\label{sec:CreateANewProjectInVS2008ForYourPlugin}
8Open Visual Studio 2008 and create a new project:
9
10Select ''.NET-Framework 3.5'' as the target framework (the Visual Studio Express edition don't provide this selection because it automatically chooses the actual target framework), and ''Class Library'' as default template to create a DLL file. Give the project a unique and significant name (here: ''Caesar''), and choose a location where to save (the Express edition will ask later for a save location when you close your project or your environment).  Finally confirm by pressing the ''OK'' button.
11
12Now your Visual Studio solution should look like this:
13
14
15\begin{figure}
16    %includegraphics...
17    \caption{Figure 1}\label{fig:figure1}
18\end{figure}
19
20
21\section{Interface selection}
22\label{sec:SelectTheInterfaceYourPluginWantsToServe}
23First we have to add a reference to the CrypTool library called "CrypPluginBase.dll" where all necessary CrypTool plugin interfaces are declared.
24
25\begin{figure}
26    %includegraphics...
27    \caption{Figure 2}\label{fig:figure2}
28\end{figure}
29
30Make a right click in the Solution Explorer on the ''Reference'' item and choose ''Add Reference''.
31
32Now browse to the path where the library file is located (e.g. ''C:\\Documents and Settings\\<Username>\\My Documents\\Visual Studio 2008\\Projects\\CrypPluginBase\\bin\\Debug'')
33
34and select the library by double clicking the file or pressing the "OK" button.
35
36\begin{figure}
37    %includegraphics...
38    \caption{Figure 3}\label{fig:figure3}
39\end{figure}
40
41Besides the CrypPluginBase you need to add three assembly references to provide the necessary "Windows" namespace for your user control functions called "Presentation" and "QuickWatchPresentation". Select the following .NET components:
42
43\begin{itemize}
44    \item PresentationCore
45    \item PresentationFramework
46    \item WindowsBase
47\end{itemize}
48
49Afterwards your reference tree view should look like this:
50
51[IMAGE]
52
53If your plugin will be based on further libraries, you have to add them in the same way.
54
55
56\section{Create the classes for the algorithm and for its settings}\label{sec:CreateTheClassesForTheAlgorithmAndForItsSettings}
57In the next step we have to create two classes. The first class named "Caesar" has to inherit from IEncryption to provide an ecryption plugin. If you want to develop a Hash plugin your class has to inherit from IHash.
58The second class named "CaesarSettings" has to inherit from ISettings.
59\subsection{Create the class for the algorithm (Caesar)}\label{sec:CreateTheClassForTheAlgorithmCaesar}
60Visual Studio automatically creates a class which has the name "Class1.cs".  There are two ways to change the name to "Caesar.cs":
61
62\hspace{20pt}-Rename the existent class
63
64\hspace{20pt}-Delete the existent class and create a new one.
65
66Which one you choose is up to you. We choose the second way as you can see in the next screenshot:
67
68[IMAGE]
69
70Now make a right click on the project item "Caesar" and select "Add->Class...":
71
72[IMAGE]
73
74Now give your class a unique name. We call the class as mentioned above "Caesar.cs" and make it public to be available to other classes.
75
76[IMAGE]
77
78\subsection{Create the class for the settings (MD5Settings)}\label{sec:CreateTheClassForTheSettingsCaesarSettings}
79Add a second public class for ISettings in the same way. We call the class "CaesarSettings". The settings class provides the necessary information about controls, captions and descriptions and default parameters for e.g. key settings, alphabets, key length and action to build the TaskPane in CrypTool. How a TaskPane could look like you can see below for the example of a Caesar encryption.
80
81[IMAGE]
82\subsection{Add namespace for the class MD5 and the place from where to inherit}
84Now open the "Caesar.cs" file by double clicking on it at the Solution Explorer and include the necessary namespaces to the class header by typing in the according "using" statement. The CrypTool 2.0 API provides the following namespaces:
85
86\hspace{20pt}-Cryptool.PluginBase = interfaces like IPlugin, IHash, ISettings, attributes, enumerations, delegates and extensions.
87
88\hspace{20pt}-Cryptool.PluginBase.Analysis = interface for the crypto analysis plugins like "Stream Comparator"
89
90\hspace{20pt}-Cryptool.PluginBase.Cryptography = interface for all encryption and hash algorithms like AES, DES or MD5 hash
91
92\hspace{20pt}-Cryptool.PluginBase.Editor = interface for editors you want to implement for CrypTool 2.0 like the default editor
93
94\hspace{20pt}-Cryptool.PluginBase.Generator = interface for generators like the random input generator
95
96\hspace{20pt}-Cryptool.PluginBase.IO = interface for CryptoolStream, input and output plugins like text input, file input, text output and file output
97
98\hspace{20pt}-Cryptool.PluginBase.Miscellaneous = provides all event helper like GuiLogMessage or PropertyChanged
99
100\hspace{20pt}-Cryptool.PluginBase.Tool = interface for all foreign tools which CrypTool 2.0 has to provide and which does not exactly support the CrypTool 2.0 API
101
102\hspace{20pt}-Cryptool.PluginBase.Validation = interface which provides method for validation like regular expression
103
104In this case we want to implement a Caesar algorithm which means we need to include the following namespaces:
105
106\hspace{20pt}-"Cryptool.PluginBase" to provide "ISettings" for the CaesarSettings class
107
108\hspace{20pt}-"Cryptool.PluginBase.Cryptography" to provide "IEncryption" for the Caesar class
109
110\hspace{20pt}-"Cryptool.PluginBase.Miscellaneous" to use the entire CrypTool event handler
111
112It is important to define a new default namespace of our public class ("Caesar"). In CrypTool the default namespace is presented by "Cryptool.[name of class]". Therefore our namespace has to be defined as follows: "Cryptool.Caesar".
113
114Up to now the source code should look as you can see below:
115
116[IMAGE]
117
118Next let your class "Caesar" inherit from IHash by inserting of the following statement:
119
120[IMAGE]
121
123There is an underscore at the I in IEncryption statement. Move your mouse over it or place the cursor at it and press "Shift+Alt+F10" and you will see the following submenu:
124
125[IMAGE]
126
127Choose the item "Implement interface 'IEncryption'". Visual Studio will now place all available and needed interface members to interact with the CrypTool core (this saves you also a lot of typing code).
128
129Your code will now look like this:
130
131[IMAGE]
132
133
134
136Let's now take a look at the second class "CaesarSettings" by double clicking at the "CaesarSettings.cs" file at the Solution Explorer. First we also have to include the namespace of "Cryptool.PluginBase" to the class header and let the settings class inherit from "ISettings" analogous as seen before at the Caesar class. Visual Studio will here also automatically place code from the CrypTool interface if available.
137
138[IMAGE]
139
141Now we have to implement some kind of controls (like button, text box) if we need them in the CrypTool \textbf{TaskPane} to modify settings of the algorithm.
142
144Before we go back to the code of the Caesar class, we have to add an icon image to our project, which will be shown in the CrypTool ribbon bar or/and navigation pane. As there is no default, using an icon image is mandatory.
145Note: This will be changed in future. A default icon will be used if no icon image has been provided.
146For testing purposes you may create a simple black and white PNG image with MS Paint or Paint.NET. As image size you can use 40x40 pixels for example, but as the image will be scaled when required, any size should do it. Place the image file in your project directory or in a subdirectory. Then make a right click on the project item "Caesar" within the Solution Explorer, and select "Add$\textendash$$\textgreaterExisting Item...": 147 148[IMAGE] 149 150Then select "Image Files" as file type, and choose the icon for your plugin: 151 152[IMAGE] 153 154Finally we have to set the icon as a "Resource" to avoid providing the icon as a separate file. Make a right click on the icon and select the item "Properties": 155 156[IMAGE] 157 158In the "Properties" panel you have to set the "Build Action" to "Resource" (not embedded resource): 159 160[IMAGE] 161 162 163\section{Set the attributes for the class Caesar}\label{sec:SetTheAttributesForTheClassMD5} 164Now let's go back to the code of the Caesar class ("Caesar.cs" file). First we have to set the necessary attributes for our class. This attributes are used to provide additional information for the Cryptool 2.0 environment. If not set, your plugin won't show up in the GUI, even if everything else is implemented correctly. 165 166Attributes are used for declarative programming and provide meta data, that can be attached to the existing .NET meta data , like classes and properties. Cryptool provides a set of custom attributes, that are used to mark the different parts of your plugin. 167 168\textit{[Author]} 169 170The first attribute called "Author" is optional, which means we are not forced to define this attribute. It provides the additional information about the plugin developer. We set this attribute to demonstrate how it has to look in case you want to provide this attribute. 171 172[IMAGE] 173 174As we can see above the author attribute takes four elements of type string. These elements are: 175 176\hspace{20pt}-Author = name of the plugin developer 177 178\hspace{20pt}-Email = email of the plugin developer if he wants to be contact 179 180\hspace{20pt}-Institute = current employment of the developer like University or Company 181 182\hspace{20pt}-Url = the website or homepage of the developer 183 184All this elements are also optional. The developer decides what he wants to publish. Unused elements shall be set to null or a zero-length string (""). 185Our author attribute should look now as you can see below: 186 187[IMAGE] 188 189\textit{[PluginInfo]} 190The second attribute called "PluginInfo" provides the necessary information about the plugin like caption and tool tip. This attribute is mandatory. The attribute has the definition as you can see below: 191 192[IMAGE] 193 194This attribute expects the following elements: 195 196\hspace{20pt}o\hspace{10pt}startable = 197Set this flag to true only if your plugin is some kind of input or generator plugin (probably if your plugin just has outputs and no inputs). In all other cases use false here. This flag is important. Setting this flag to true for a non input/generator plugin will result in unpredictable chain runs. This element is mandatory. 198 199\hspace{20pt}o\hspace{10pt}caption = 200from type string, the name of the plugin (e.g. to provide the button content). This element is mandatory. 201 202\hspace{20pt}o\hspace{10pt}toolTip = from type string, description of the plugin (e.g. to provide the button tool tip). This element is optional. 203 204\hspace{20pt}o\hspace{10pt}descriptionUrl = from type string, define where to find the whole description files (e.g. XAML files). This element is optional. 205o icons = from type string array, which provides all necessary icon paths you want to use in the plugin (e.g. the plugin icon as seen above). This element is mandatory. 206 207Unused optional elements shall be set to null or a zero-length string (""). 208 209\smallNote 1: It is possible to use the plugin without setting a caption though it is not recommended. This will be changed in future and the plugin will fail to load without a caption. 210 211Note 2: Currently a zero-length toolTip string appears as empty box. This will be changed in future. 212 213Note 3: Tooltip and description currently do not support internationalization and localization. This will be changed in future. 214 215In our example the first parameter called "startable" has to be set to "false", because our hash algorithm is neither an input nor generator plugin. 216 217[IMAGE] 218 219The next two parameters are needed to define the plugin's name and its description: 220 221[IMAGE] 222 223The fourth element defines the location path of the description file. The parameter is made up by \guilsinglleftAssembly name\guilsinglright/\guilsinglleftfilename\guilsinglright or \guilsinglleftAssembly name\guilsinglright/\guilsinglleftPath\guilsinglright/\guilsinglleftfile name\guilsinglright if you want to store your description files in a separate folder. The description file has to be of type XAML. In our case we create a folder called "DetailedDescription" and store our XAML file there with the necessary images if needed. How you manage the files and folders is up to you. This folder could now look as you can see below: 224 225[IMAGE] 226 227Accordingly the attribute parameter has to be set to: 228 229[IMAGE] 230 231The detailed description could now look like this in CrypTool (right click plugin icon on workspace and select "Show description"): 232 233[IMAGE] 234 235The last parameter tells CrypTool the names of the provided icons. This parameter is made up by \guilsinglleftAssembly name\guilsinglright/\guilsinglleftfile name\guilsinglright or \guilsinglleftAssembly name\guilsinglright/\guilsinglleftPath\guilsinglright/\guilsinglleftfile name\guilsinglright. 236 237The most important icon is the plugin icon, which will be shown in CrypTool in the ribbon bar or navigation pane (This is the first icon in list, so you have to provide at least one icon for a plugin). As named above how to add an icon to the solution accordingly we have to tell CrypTool where to find the icon by setting this parameter as you can see below: 238 239[IMAGE] 240 241You can define further icon paths if needed, by adding the path string separated by a comma. 242\section{Set the private variables for the settings in the class Caesar} 243\label{sec:SetThePrivateVariablesForTheSettingsInTheClassMD5} 244The next step is to define some private variables needed for the settings, input and output data which could look like this: 245 246[IMAGE] 247 248Please notice the sinuous line at the type "CryptoolStream" of the variable inputData and the list listCryptoolStreamsOut. "CryptoolStream" is a data type for input and output between plugins and is able to handle large data amounts. To use the CrypTool own stream type, include the namespace "Cryptool.PluginBase.IO" with a "using" statement as explained in chapter 3.3. 249 250The following private variables are being used in this example: 251 252\hspace{20pt}-CaesarSettings settings: required to implement the IPlugin interface properly 253 254\hspace{20pt}-CryptoolStream inputData: stream to read the input data from 255 256\hspace{20pt}-byte[] outputData: byte array to save the output hash value 257 258\hspace{20pt}-List\guilsinglleftCryptoolStream\guilsinglright listCryptoolStreamsOut: list of all streams being created by Caesar plugin, required to perform a clean dispose 259 260 261\section{Define the code of the class Caesar to fit the interface}\label{sec:DefineTheCodeOfTheClassMD5ToFitTheInterface} 262Next we have to complete our code to correctly serve the interface. 263 264First we add a constructor to our class where we can create an instance of our settings class: 265 266[IMAGE] 267 268Secondly, we have to implement the property "Settings" defined in the interface: 269 270[IMAGE] 271 272Thirdly we have to define two properties with their according attributes. This step is necessary to tell Cryptool that these properties are input/output properties used for data exchange with other plugins. 273 274The attribute is named "PropertyInfo" and consists of the following elements: 275 276\hspace{20pt}-direction = defines whether this property is an input or output property, i.e. whether it reads input data or writes output data 277 278\hspace{30pt}o\hspace{10pt}Direction.Input 279 280\hspace{30pt}o\hspace{10pt}Direction.Output 281 282\hspace{20pt}-caption = caption of the property (e.g. shown at the input on the dropped icon in the editor), see below: 283 284[IMAGE] 285 286\hspace{20pt}-toolTip = tooltip of the property (e.g. shown at the input arrow on the dropped icon in the editor), see above 287 288\hspace{20pt}-descriptionUrl = not used right now 289 290\hspace{20pt}-mandatory = this flag defines whether an input is required to be connected by the user. If set to true, there has to be an input connection that provides data. If no input data is provided for mandatory input, your plugin will not be executed in the workflow chain. If set to false, connecting the input is optional. This only applies to input properties. If using Direction.Output, this flag is ignored. 291 292\hspace{20pt}-hasDefaultValue = if this flag is set to true, CrypTool treats this plugin as though the input has already input data. 293 294\hspace{20pt}-DisplayLevel = define in which display levels your property will be shown in CrypTool. CrypTool provides the following display levels: 295 296\hspace{30pt}o\hspace{10pt}DisplayLevel.Beginner 297 298\hspace{30pt}o\hspace{10pt}DisplayLevel.Experienced 299 300\hspace{30pt}o\hspace{10pt}DisplayLevel.Expert 301 302\hspace{30pt}o\hspace{10pt}DisplayLevel.Professional 303 304\hspace{20pt}-QuickWatchFormat = defines how the content of the property will be shown in the quick watch. CrypTool accepts the following quick watch formats: 305 306\hspace{30pt}o\hspace{10pt}QuickWatchFormat.Base64 307 308\hspace{30pt}o\hspace{10pt}QuickWatchFormat.Hex 309 310\hspace{30pt}o\hspace{10pt}QuickWatchFormat.None 311 312\hspace{30pt}o\hspace{10pt}QuickWatchFormat.Text 313 314A quick watch in Hex could look like this: 315 316[IMAGE] 317 318\hspace{20pt}-quickWatchConversionMethod = this string points to a conversion method; most plugins can use a "null" value here, because no conversion is necessary. The QuickWatch function uses system "default" encoding to display data. So only if your data is in some other format, like Unicode or UTF8, you have to provide the name of a conversion method as string. The method header has to look like this: 319object YourMethodName(string PropertyNameToConvert) 320 321First we define the "InputData" property getter and setter: 322 323[IMAGE] 324 325In the getter we check if the input data is not null. If input data is filled, we declare a new CryptoolStream to read the input data, open it and add it to our list where all output stream references are stored. Finally the new stream will be returned. 326 327\smallNote 1: It is currently not possible to read directly from the input data stream without creating an intermediate CryptoolStream. 328 329\smallNote 2: The naming may be confusing. The new CryptoolStream is not an output stream, but it is added to the list of output streams to enable a clean dispose afterwards. See chapter 9 below. 330 331The setter sets the new input data and announces the data to the Cryptool 2.0 environment by using the expression "OnPropertyChanged("\guilsinglleftProperty name\guilsinglright"). For input properties this step is necessary to update the quick watch view. 332 333The output data property could look like this: 334 335[IMAGE] 336 337CrypTool does not require implementing output setters, as they will never be called from outside of the plugin. Nevertheless in this example our plugin accesses the property itself, therefore we chose to implement the setter. 338 339You can also provide additional output data types if you like. For example we provide also an output data of type CryptoolStream: 340 341[IMAGE] 342 343This property's setter is not called and therefore not implemented. 344 345Notice the method "GuiLogMessage" in the source codes above. This method is used to send messages to the CrypTool status bar. This is a nice feature to inform the user what your plugin is currently doing. 346 347[IMAGE] 348 349The method takes two parameters which are: 350 351\hspace{20pt}-Message = will be shown in the status bar and is of type string 352 353\hspace{20pt}-NotificationLevel = to group the messages to their alert level 354 355\hspace{30pt}o\hspace{10pt}NotificationLevel.Error 356 357\hspace{30pt}o\hspace{10pt}NotificationLevel.Warning 358 359\hspace{30pt}o\hspace{10pt}NotificationLevel.Info 360 361\hspace{30pt}o\hspace{10pt}NotificationLevel.Debug 362 363As we can recognize we have two methods named "OnPropertyChanged" and "GuiLogMessage" which are not defined. So we have to define these two methods as you can see below: 364 365[IMAGE] 366 367To use the "PropertyChangedEventHandler" you have to include the namespace "System.ComponentModel". 368 369Our whole included namespaces looks now like this: 370 371[IMAGE] 372 373 374\section{Complete the actual code for the class Caesar}\label{sec:CompleteTheActualCodeForTheClassMD5} 375Up to now, the plugin is ready for the CrypTool base application to be accepted and been shown correctly in the CrypTool menu. What we need now, is the implementation of the actual algorithm in the function "Execute()" which is up to you as the plugin developer. 376 377Let us demonstrate the Execute() function, too. Our algorithm is based on the .NET framework: 378 379[IMAGE] 380 381It is important to make sure that all changes of output properties will be announced to the CrypTool environment. In this example this happens by calling the setter of OutputData which in turn calls "OnPropertyChanged" for both output properties "OutputData" and "OutputDataStream". Instead of calling the property's setter you can as well call "OnPropertyChanged" directly within the "Execute()" method. 382 383Certainly you have seen the unknown method "ProgressChanged" which you can use to show the current algorithm process as a progress on the plugin icon. 384To use this method you also have to declare this method to afford a successful compilation: 385 386[IMAGE] 387 388\section{Perform a clean dispose}\label{sec:PerformACleanDispose} 389Be sure you have closed and cleaned all your streams after execution and when CrypTool decides to dispose the plugin instance. Though not required, we run the dispose code before execution as well: 390 391[IMAGE] 392\section{Finish implementation}\label{sec:FinishImplementation} 393When adding plugin instances to the CrypTool workspace, CrypTool checks whether the plugin runs without any exception. If any IPlugin method throws an exception, CrypTool will show an error and prohibit using the plugin. Therefore we have to remove the "NotImplementedException" from the methods "Initialize()", "Pause()" and "Stop()". In our example it's sufficient to provide empty implementations. 394 395[IMAGE] 396 397The methods "Presentation()" and "QuickWatchPresentation()" can be used if a plugin developer wants to provide an own visualization of the plugin algorithm which will be shown in CrypTool. Take a look at the PRESENT plugin to see how a custom visualization can be realized. For our Caesar example we don't want to implement a custom visualization, therefore we return "null": 398 399[IMAGE] 400 401Your plugin should compile without errors at this point. 402\section{Sign the created plugin}\label{sec:SignTheCreatedPlugin} 403 404\section{Import the plugin to Cryptool and test it}\label{sec:ImportThePluginToCryptoolAndTestIt} 405After you have built the plugin, you need to move the newly created plugin DLL to a location, where CrypTool can find it. To do this, there are the following ways: 406 407\hspace{20pt}1. Copy your plugin DLL file in the folder "CrypPlugins" which has to be in the same folder as the CrypTool executable, called "CrypWin.exe". If necessary, create the folder "CrypPlugins". This folder is called "Global storage" in the CrypTool architecture. Changes in this folder will take effect for all users on a multi user Windows. Finally restart CrypTool. 408 409[IMAGE] 410 411\hspace{20pt}2. Copy your plugin DLL file in the folder "CrypPlugins" which is located in your home path in the folder "ApplicationData" and restart CrypTool. This home folder path is called "Custom storage" in the CrypTool architecture. Changes in this folder will only take effect for current user. On a German Windows XP the home folder path could look like: 412"C:\backslashDokumente und Einstellungen\backslash$$\guilsinglleft$User$\guilsinglright$$\backslashAnwendungsdaten\backslashCrypPlugins" and in Vista the path will look like "C:\backslashUsers\backslash$$\guilsinglleft$user$\guilsinglright$$\backslash$Application Data$\backslash$CrypPlugins".
413
414[IMAGE]
415
416\hspace{20pt}3. You can also import new plugins directly from the CrypTool interface. Just execute CrypWin.exe and select the "Download Plugins" button. An "Open File Dialog" will open and ask where the new plugin is located. After selecting the new plugin, CrypTool will automatically import the new plugin in the custom storage folder. With this option you will not have to restart CrypTool. All according menu entries will be updated automatically.
417Notice, that this plugin importing function only accepts signed plugins.
418
419This option is a temporary solution for importing new plugins. In the future this will be done online by a web service.
420
421\hspace{20pt}4. Use post-build in your project properties to copy the DLL automatically after building it in Visual Studio. Right-click on your plugin project and select "Properties":
422
423[IMAGE]
424
425Select "Build Events":
426
427[IMAGE]
428
429Enter the following text snippet into "Post-build event command line":
430
431cd "\$(ProjectDir)" 432 433cd ..$\backslash$..$\backslash$CrypWin$\backslash$\$(OutDir)
434
435if not exist "./CrypPlugins" mkdir "./CrypPlugins"
436
437del /F /S /Q /s /q "Caesar*.*"
438
439copy "\\$(TargetDir)Caesar*.*" "./CrypPlugins"
440
441You need to adapt the yellow marked field to your actual project name.
442
443\section{Source code and source template}\label{sec:SourceCodeAndSourceTemplate}
444Here you can download the whole source code which was presented in this "Howto" as a Visual Studio solution:
445