Skip to content
onepiecefreak3 edited this page Dec 16, 2018 · 6 revisions

Kore

Namespace: Kore

  1. Terminology
  2. Description
  3. Constructors
  4. Properties
  5. Methods
  6. Events
  7. KoreFileInfo
  8. How to use

Terminology

Adapter/Plugin: Those are synonymous and refer to the same instance of an object that manages a specific file type.

Description

Kore is a main library for a user interface to manage files and file relevant actions. This way a UI doesn't need to manage loading plugins, file formats and any action not specific to the UI itself.

This library is dependant on Kontract.

Constructors

There are 2 constructors for Kore. One that just creates an instance of Kore and one that also takes in another plugin directory. By default the plugin directoy is "plugins" in the same folder as the executable.

var kore = new Kore("otherPluginFolder");    //now all plugins get read from the folder "otherPluginFolder"

Properties

  1. OpenFiles
  2. FileFilters

OpenFiles

This returns a list of KoreFileInfos that were opened using LoadFile.

If no file was loaded yet, an empty List is returned.

FileFilters

This returns an already concatted string of all file extensions found in the loaded plugins. It's mainly meant to be used in OpenFileDialogs to populate its filter property.
The returned string can be directly used in the extension filter of OpenFileDialog.

It contains an entry "All Supported Files" to select all files with the extension a plugin is written for.
And the well-known "All Files" entry that just selects every file with .

The syntax of the returned string looks like this, if 2 plugins with ext1 and ext2 were found:

All Supported Files (*.ext1;*.ext2)|*.ext1;*.ext2|Plugin 1 (*.ext1)|*.ext1|Plugin 2 (*.ext2)|*.ext2|All Files (*.*)|*.*

Methods

  1. LoadFile
  2. SaveFile
  3. CloseFile
  4. FileFiltersByType
  5. FileExtensionsByType
  6. Dispose

LoadFile

This method takes in a file name, to load the file with a specific plugin. It returns an initialized KoreFileInfo or null, if no plugin could open the file.

It can also optionally take in a boolean to tell Kore that it should track the file. By default it's true and will be used in the future by a feature not finished yet.

For a plugin to load a file, it needs to inherit at least from ILoadFiles.

Notice that there is an event IdentificationFailed which gets executed, if no plugin could distinctly identify the given file. That means if all plugins that inherit from IIdentifyFiles returned false, the event is executed.

SaveFile

This method takes in a KoreFileInfo and optionally a filename to save the file to. If saved successfully, the KoreFileInfo gets updated accordingly.

For a plugin to save a file, it needs to inherit at least from ISaveFiles.

CloseFile

This method takes in a KoreFileInfo. It returns a boolean, indicating if closing the file was successful.

The Adapter of KoreFileInfo will get disposed and the KoreFileInfo itself removed from OpenFiles.

For a plugin to close a file, it needs to inherit at least from ICloseFiles.

FileFiltersByType

This method will return a partly FileFilters, depending on the given interface type.
It has to be an interface type from Kontract.

Optionally it can take in a name for the "All Supported Formats"-filter, and a boolean to determine if the "All Files"-filter should be included.

This method returns a concatted string with the same syntax as in FileFilters.

Example:

var kore = new Kore.Kore();
var loadfilters = kore.FileFiltersByType<ILoadFiles>();
//this will return a concatted string only containing the filters for plugins that are able to load files

FileExtensionsByType

This method returns an IEnumerable containing all extensions in the format ".ext", depending on the given interface type.
It has to be an interface type from Kontract.

Example:

var kore = new Kore.Kore();
var loadfilters = kore.FileExtensionsByType<ILoadFiles>();
//this will return an IEnumerable<string> only containing the extensions for plugins that are able to load files

Dispose

Since Kore inherits from IDisposable, it can be disposed either by calling Dispose manually or by being used in a using directive.

All files tracked by Kore with OpenFiles get closed with CloseFile.

Events

  1. IdentificationFailed

IdentificationFailed

This event is toggled when LoadFile couldn't distinctly identify a given file with the loaded adapters.

A UI can use this event to retrieve a list of adapters that don't inherit from IIdentifyFiles and for example give the user the possibility to choose one of those "blind" adapters on their own behalf.

When toggled, the UI retrieves an object IdentificationFailedEventArgs, which contains a List<ILoadFiles> representing said blind adapters and a property SelectedAdapter from type ILoadFiles that ultimately contains the selected adapter by the user.

Example:

static void Main(string[] args)
{
    var kore = new Kore.Kore();
    kore.IdentificationFailed += 

    var kfi = kore.LoadFile("test_file.bin");
}

static void On_IdentificationFailed(object sender, IdentificationFailedEventArgs args)
{
    var blinds = args.BlindAdapters;

    //do something with those blind adapters

    args.SelectedAdapter = blinds.ElementAt(0);    //or anything else to set an adapter inheriting from ILoadFiles here
}

KoreFileInfo

  1. Description
  2. Properties

Description

This class holds information about one file opened by Kore. It inherits from the System.ComponentModel.INotifyPropertyChanged, which describes an event to notify if a property was changed.

Properties

  1. FileInfo
  2. Adapter
  3. HasChanges
  4. Filter
  5. Extension
  6. DisplayName

FileInfo

KoreFileInfo contains a plain System.IO.FileInfo without any changes, from the file it's loaded for.

Adapter

KoreFileInfo contains an adapter with type ILoadFiles which was used to load this file. It can be cast to any other interface from Kontract to access other functionality specific to text, images, archives, etc.

HasChanges

Declares if there are changes made to KoreFileInfo. It has to be set manually and toggles the PropertyChanged event for HasChanges and DisplayName

Filter

Gets the filter from the adapter.

Extension

Gets the extension from the adapter.

DisplayName

Gets the name of the file and if HasChanges is true, it also has a "*" attached.

How to use

static void Main(string[] args)
{
    //initialize the Kore object
    var kore = new Kore.Kore();

    //load the file testfile.bin
    var kfi = kore.LoadFile("testfile.bin");
    if (kfi == null)
        throw new Exception("File couldn't be loaded");

    //cast the KoreFileInfo adapter to an image adapter to make image specific changes
    var imageAdapter = kfi.Adapter as IImageAdapter;

    //add a new image to the image list
    imageAdapter.Bitmaps.Add(new Bitmap());

    //saves the file opened in kfi to new_testfile.bin
    kore.SaveFile(kfi, "new_testfile.bin");

    //Closes and disposes the opened file again
    kore.CloseFile(kfi);
}
Clone this wiki locally