Lifecycle & Capabilities

TonCut Desktop discovers plugins once at startup, keeps each loaded plugin for the whole session, and calls it only from user actions. This page states what the host does at each step, what it reads from the plugin, and how it reacts to every result.

Host behaviour

Discovery

TonCut scans the Plugins folder next to TonCut.exe, including every subfolder, for files with the extension .plg. The test is case-sensitive: only lowercase .plg is loaded.

For each file TonCut calls LoadLibrary and resolves the export TcpInitializeV2. A failed LoadLibrary (for example a 64-bit DLL), a missing export, or an exception during the call shows a plugin load error, and the plugin is not listed. The binary rules for the export are on ABI & Marshalling.

Initialization

TonCut calls TcpInitializeV2(aRegRoot, aLanguageId, aTonCut) and keeps the returned ITcPlugin. TonCut never calls ITcPlugin.Initialize. Your entry point creates the plugin, calls Initialize itself with the same arguments, and returns the plugin only when that succeeded. Returning nil is reported as a plugin load error.

  • aRegRoot is the plugin's own registry key, relative to HKEY_CURRENT_USER: \SOFTWARE\Codeton\TonCut\<BaseVersion>\Plugins\<file>.plg. It is a UTF-16 string valid only during the call; copy it.
  • aLanguageId is the current user-interface language.
  • aTonCut is the host service object. You may keep it until Finalize.

Load sequence

After the entry point returns, TonCut reads and pushes, in this order:

  1. EngineVersion, compared with the host's engine version (Version gate).
  2. UiScale, pushed once.
  3. Name, Title, and Version.
  4. LoadConfig, with the plugin's stored configuration. TonCut skips it when the plugin has no registry key yet (its first run) and for incompatible plugins.
  5. EnumExtensions three times, once each for , , and , for active plugins.

LanguageId is pushed again on every plugin state change and whenever the user switches the interface language. UiScale is not pushed again.

Lifecycle-sensitive members

function TCsvImportExportPlugin.GetEngineVersion(): Integer;
begin
  Result := cEngineVersion;
end;

procedure TCsvImportExportPlugin.SetLanguageId(aLanguageId: Integer);
begin
  FLanguageId := aLanguageId;
end;

procedure TCsvImportExportPlugin.SetUiScale(aScale: Single);
begin
  FUiScale := aScale;
end;

Version gate

A plugin is compatible only when its EngineVersion is exactly equal to the host's cEngineVersion. The current SDK value is cEngineVersion = 25. Version 25 adds collection update scopes and OnChange to IMaterials, IBands, IResults, and ITemplates. Return the constant from the SDK you compiled against; do not compute or range-check it.

A mismatch is silent: TonCut lists the plugin as Not compatible in Options > Plugins and never activates it. It still calls UiScale, Name, Title, and Version to show the entry, but no other method. When TonCut Desktop raises its engine version, rebuild the plugin against the matching SDK.

Plugin version

TTcpVersion is a UInt64 that packs four 16-bit fields, from the most significant: major, minor, build, revision. TonCut shows it as major.minor.build.revision and compares that text when it checks for plugin updates.

Encode a plugin version

function MakeTcpVersion(aMajor, aMinor, aBuild, aRevision: Word): TTcpVersion;
begin
  Result :=
    (TTcpVersion(aMajor) shl 48) or
    (TTcpVersion(aMinor) shl 32) or
    (TTcpVersion(aBuild) shl 16) or
    TTcpVersion(aRevision);
end;

function TCsvImportExportPlugin.GetVersion(): TTcpVersion;
begin
  Result := MakeTcpVersion(1, 0, 0, 0); // shown as 1.0.0.0
end;

MakeTcpVersion is defined in TonCutPluginSdk.hpp; the Pascal interface units do not define it, so a Delphi plugin declares it as above.

Plugin identity

  • Name must be unique among all installed plugins. A duplicate name raises an error during the startup scan and stops TonCut from loading the remaining plugins. TonCut stores Name and Title as ANSI short strings (at most 255 characters), so keep Name plain ASCII. Name is also the value a device's Driver field refers to, and the configuration backup file is named <Name>Config.json. Never change it between releases.
  • Title is the human-readable label in Options > Plugins and in dialogs.
  • Professional: when True, import, export, save-result, plot, and accept-solution export through this plugin require the Professional edition. In other editions TonCut shows that the function is available only in the Professional version and does not call the plugin.

Features and host commands

tells TonCut which commands to enable. File dialog filters, however, come only from EnumExtensions.

FlagWhat TonCut does
Lets IsMyFile take part in automatic detection. Your filters appear in the Import dialog either way, and choosing one of them calls LoadFile directly.
Not read by TonCut. Your filters appear in the export dialogs either way. Set it to describe the plugin truthfully.
Enables Save one in the Save results dialog when a single unsaved page is selected. Save one calls SaveResult.
Enables Save one. Without , Save one calls SavePlot.
, Enable Save all, which calls BatchSaveResult.
, Enable Save all. Without a batch-result flag, Save all calls BatchSavePlot.
Enables the main Plot action, which calls Plot.
The Plot action calls BatchPlot instead of Plot.
Enables the Settings button for the plugin in Options, Save results, and Plot. Configure is called only then.
Enables the About button in the same places. About is called only then.

Any save flag (result or plot, single or batch) also lists the plugin in the Save results dialog's plugin list.

Extensions

EnumExtensions(aType, aEnumCb) registers one dialog filter per aEnumCb.Invoke(aType, aExtension, aName) call. TonCut builds the filter as aName (*aExtension)|*aExtension, so the extension must include the leading dot ('.csv', not 'csv'). Each type feeds different dialogs:

TypeDialogs
Import
Save data, Export project, and the export offered when a solution is accepted
The file filter of Save results

Call aEnumCb only while EnumExtensions runs, and pass the same aType you received.

Batch targets

BatchSaveResult and BatchSavePlot receive aSaveTarget and aPath:

  • If your features contain or , TonCut passes stFolder and the folder the user chose. The plugin chooses the file names.
  • Otherwise TonCut passes stFile and the file the user chose. TonCut has already appended your extension if the user typed none, and has already asked before overwriting.

Saved flags

After BatchSaveResult returns , TonCut marks as saved only the sheets on which the plugin set ISolutionSheet.IsSaved := True. After SaveResult returns , TonCut marks the sheet itself, but enabling Save one and selecting the next unsaved page read the flag, so set IsSaved there too. The flags are not persisted.

Advertise features

function TCsvImportExportPlugin.GetFeatures(): TTcpFeatures;
begin
  // feSaveResultToFile requires GetPlotterInfo to return a valid object.
  Result := [feImport, feExport, feSaveResultToFile, feAbout];
end;

Result codes

TonCut tests only three values of : , (only from LoadFile), and (only from IsMyFile). Every other value, including and , simply means "not OK". cprNothing, cprNotInitialized, cprMoreData, and cprNo have no meaning to the host; cprNo is still a clear way to say "not my file".

CallOther results
IsMyFileThe plugin is chosen at once.: the plugin becomes a candidate; if no plugin returns OK, the last candidate in scan order wins. Anything else: the plugin is skipped.
LoadFileTonCut opens its Import dialog with the staged data.: nothing happens, silently. Anything else: an "Error reading file" message. The staged data is discarded in both cases.
SaveFileSuccess.An "Error writing file" message, also for .
SaveResult, SavePlot, BatchSaveResult, BatchSavePlotThe sheets are marked saved (see Saved flags).Silent. Show your own message if the user needs one.
Configure, About, EnumExtensions, LoadConfig, SaveConfigIgnored.Ignored.

IsMyFile is consulted only for the Open dialog's All files filter, drag-and-drop, temporary-file import, and files named on the command line, and only for plugins with . When the user picks one of your own filters, TonCut calls LoadFile without asking IsMyFile.

Return conservative match results

function TCsvImportExportPlugin.IsMyFile(const aFileName: WideString): TTcpResult;
begin
  try
    Result := ValidateHeader(aFileName); // cprOK, cprMaybe, or cprNo
  except
    Result := cprNo; // never let an exception leave a plugin method
  end;
end;

Exceptions and failures

An exception that escapes a Delphi method, or a failed HRESULT returned by a C++ method, is raised inside TonCut. TonCut catches it, sets the plugin's state to failure (removing it from the active lists and disabling it), and shows a generic plugin error naming the plugin. The exception text is discarded. A few calls, such as reading Features while saving results, BackupConfig, and RestoreConfig, are not protected this way (observed in code); an exception from them may reach TonCut's default error handler instead.

Therefore:

  • Wrap the body of every plugin method in try ... except (Delphi) or try { ... } catch (...) (C++).
  • Report the outcome through . In C++, return S_OK unless an argument pointer is invalid.
  • If the user needs details, show your own message box owned by the aWindow you received.

Threading and windows

Every call site observed in TonCut's code is a user-interface handler, so plugin methods are called on the main user-interface thread, one call at a time. Do not rely on more than that. If your plugin pumps messages (a modal dialog, a progress window), TonCut can call back into it before the current call returns; guard state you are in the middle of changing.

aWindow is the window to own any dialog you show. It is not always the main form:

CallaWindow
LoadFileThe input frame
SaveFileThe main form
SaveResult, BatchSaveResult, SavePlot, BatchSavePlotThe modal Save results dialog
Plot, BatchPlotThe modal Plot dialog
Configure, AboutThe dialog whose button was pressed

Unloading

TonCut unloads plugins at shutdown and before a plugin auto-update. For each plugin it calls Finalize, releases its final reference, and calls FreeLibrary immediately. Stopping a plugin in Options does not unload it.

In Finalize, release ITonCut and everything else you hold. Every object your plugin created, including any ITcPlotterInfo you returned, must have reached a reference count of zero before FreeLibrary; code that runs afterwards would run in an unloaded module.

Was this page helpful?