////////////////////////////////////////////////////////////////////// // // // tools.pas: Plugin management // // // // The contents of this file are subject to the Bottled Light // // Public License Version 1.0 (the "License"); you may not use this // // file except in compliance with the License. You may obtain a // // copy of the License at http://www.bottledlight.com/BLPL/ // // // // Software distributed under the License is distributed on an // // "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or // // implied. See the License for the specific language governing // // rights and limitations under the License. // // // // The Original Code is the Mappy VM User Interface, released // // April 1st, 2003. The Initial Developer of the Original Code is // // Bottled Light, Inc. Portions created by Bottled Light, Inc. are // // Copyright (C) 2001-2003 Bottled Light, Inc. All Rights Reserved. // // // // Author(s): // // Michael Noland (joat), michael@bottledlight.com // // // // Changelog: // // 1.0: First public release (April 1st, 2003) // // // // Notes: // // Plugin support is completly untested. // // // ////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////// unit tools; ////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////// interface //////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////// uses Windows, Classes, SysUtils, Contnrs, nexus, AddressSpace; ////////////////////////////////////////////////////////////////////// type TPlugin = class public header: PvmPluginHeader; constructor Create(filename: PChar); destructor Destroy; override; procedure Trigger; private DLLHandle: uint32; loaded: boolean; end; ////////////////////////////////////////////////////////////////////// var plugins: TObjectList; ////////////////////////////////////////////////////////////////////// procedure EnumeratePlugins; procedure TriggerPlugin(index: integer); ////////////////////////////////////////////////////////////////////// implementation /////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////// procedure EnumeratePlugins; var f: TSearchRec; p: TPlugin; begin if FindFirst(ExtractFilePath(ParamStr(0)) + 'plugins/*.dll', faReadOnly + faHidden + faArchive, f) = 0 then repeat // Try to load a DLL as a plugin p := TPlugin.Create(PChar(f.name)); if p.loaded then plugins.Add(p) else p.Free; until FindNext(f) <> 0; FindClose(f); end; ////////////////////////////////////////////////////////////////////// procedure TriggerPlugin(index: integer); begin if (index >= 0) and (index < plugins.Count) then TPlugin(plugins[index]).Trigger; end; ////////////////////////////////////////////////////////////////////// // TPlugin /////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////// constructor TPlugin.Create(filename: PChar); var GetHeader: function: PvmPluginHeader; cdecl; begin loaded := false; DLLHandle := LoadLibrary(filename); if DLLHandle >= 32 then begin GetHeader := GetProcAddress(DLLHandle, 'GetHeader'); if @GetHeader <> nil then begin Header := GetHeader; loaded := true; end; end end; ////////////////////////////////////////////////////////////////////// destructor TPlugin.Destroy; begin if loaded then FreeLibrary(DLLHandle); inherited; end; ////////////////////////////////////////////////////////////////////// procedure TPlugin.Trigger; begin with header^ do if triggerCaption <> nil then begin vmLockMemory(mem); Trigger; vmUnlockMemory(mem); end; end; ////////////////////////////////////////////////////////////////////// initialization plugins := TObjectList.Create; finalization plugins.Free; end. //////////////////////////////////////////////////////////////////////