unit GenPlugServ;

{$WARN SYMBOL_PLATFORM OFF}

interface

uses
  Windows, ActiveX, Classes, ComObj, CM2GenInterface, CM2MMRB,Filter, SysUtils,
   CM2AutoUpdateInterface, CM2EditorSupportInterface;

type
  TCM2GEN_Test = class(TComObject, ICM2GenPlug, ICM2AutoUpdate, ICM2EditorSupport20)
  private
   lasterrorstring: PChar;
   Max: Integer;
   Range: String;
   Procedure Parser(Param: String);
  protected
  // Declare Basics
   Function Name(OUT Name: Pchar): HResult; stdcall;
   Function Version(OUT Version: word): HResult; stdcall; // Using MMRB Notation
   Function RequiredCoreVersion(OUT RCV: Word): HResult; stdcall; // Using MMRB Notation - Please refer to CM2Plugtypes for details
   Function SetParentHandle(Handle: THandle): HResult; stdcall;
  // Declare Error commands
   Function GetLastErrorMsg(OUT Msg: PChar): HResult; stdcall;
  // Declare Info system
   Function GetInfoCount(OUT InfoCount: Integer): HResult; stdcall;
   Function GetInfo(Index: Integer; OUT InfoName: PChar; OUT InfoVar: PChar): HResult; stdcall;
  // Declare Sensor system
   Function GetSensorCount(OUT SensorCount: Integer): HResult; stdcall;
   Function GetSensorNameOnly(Index: Integer; OUT SensorName: PChar; OUT SensorStyle: Byte): HResult; stdcall; // Designed for speed polling at startup
   Function GetSensor(Index: Integer; Parameters: PChar; OUT SensorName: PChar; OUT SensorStyle: byte; OUT SensorString: PChar; OUT SensorValue: Double): HResult; stdcall;
  // Declare "Event" functions
   Function Init: HResult; stdcall;
   Function Terminating: HResult; stdcall;
   Function Setup: HResult; stdcall;
  // Default (Required)
   Function GetDefaultCount(SensorIndex: Integer; OUT DefaultCount: Integer): HResult; stdcall;
   Function GetDefault(SensorIndex: Integer; Index: Integer; OUT Name: Pchar; OUT Value: PChar; OUT Description: PChar): HResult; stdcall;
  // Optional
   Function GetOptionalCount(SensorIndex: Integer; OUT OptionalCount: Integer): HResult; stdcall;
   Function GetOptional(SensorIndex: Integer; Index: Integer; OUT Name: Pchar; OUT Value: PChar; OUT Description: PChar): HResult; stdcall;
  // Declare ICM2AutoUpdate
   Function UpdateFileURL(OUT URL: Pchar): HResult; stdcall;
  // Declare ICM2EditorSupport20
   Function ObjIns(SensorIndex: Integer; ParameterName: PChar; OUT ValueType: Byte; OUT ValueRange: PChar): HResult; stdcall;
   Function OnEditor(SensorIndex: Integer; ParameterName: PChar; FullParameterValues: PChar; OUT Value: PChar): HResult; stdcall;
   Function GetMenuCount(SensorIndex: Integer; OUT MenuCount: Byte): HResult; stdcall;
   Function GetMenuItem(SensorIndex: Integer; Index: Byte; OUT MenuTitle: PChar; OUT MenuHint: PChar): HResult; stdcall;
   Function OnMenu(SensorIndex: Integer; Index: Byte; OUT ResultString: PChar): HResult; stdcall;
   Function ShowSensor(Index: Integer; OUT ShowAt: Byte): HResult; stdcall;
  End;

const
  Class_CM2GEN_Test: TGUID = '{68239528-1274-4A75-B94E-3931D91A4037}';
  AutoUpdateFile = 'http://www.coolmon.org/update/testplugin.upd';
  PlugInVersion: Word = 0800;
  RequiredCV: Word = 0800;

implementation


uses ComServ, RangeDLG;

{ TCM2GEN_Test }

function TCM2GEN_Test.GetInfo(Index: Integer; out InfoName,
  InfoVar: PChar): HResult;
begin
 Result := S_OK;
 Case Index of
  0: Begin
      InfoName := PChar('Programmed By');
      InfoVar := PChar('C. Aaangel');
     End;
  1: Begin
      InfoName := PChar('Created');
      InfoVar := Pchar('13th September 2003');
     End;
  2: Begin
      Infoname := PChar('Updated');
      Infovar := PChar('June, 20th 2006');
     End;
  else begin
        Result := E_FAIL;
        LastErrorString := 'Index out of bounds';
       End;
 End;
end;

function TCM2GEN_Test.GetInfoCount(out InfoCount: Integer): HResult;
begin
 Result := S_OK;
 InfoCount := 3; // The infolist contains 3 items
end;

function TCM2GEN_Test.GetLastErrorMsg(OUT Msg: PChar): HResult;
begin
 If LastErrorString = '' then LastErrorString := 'No error';
 Msg := LastErrorString;
 Result := S_OK;
end;

function TCM2GEN_Test.GetSensor(Index: Integer; Parameters: PChar;
  out SensorName: PChar; out Sensorstyle: byte; out SensorString: PChar;
  out SensorValue: Double): HResult;
begin
 Result := S_OK;
 Parser(Parameters);
 Case Index of
  0: Begin
      SensorName := PChar('Random Numbers');
      SensorStyle := 0;
      SensorString := PChar('');
      SensorValue := Random(Max);
     End;
  1: Begin
      SensorName := PChar('Return Parameters');
      SensorStyle := 1;
      SensorString := Parameters;
      SensorValue := 0;
     End;
  else Begin
        Result := E_FAIL;
        LastErrorString := 'Index out of bounds';
       End;
 End;
end;

function TCM2GEN_Test.GetSensorCount(out SensorCount: Integer): HResult;
begin
 SensorCount := 2; // This server holds 2 sensors
 Result := S_OK;
end;

function TCM2GEN_Test.GetSensorNameOnly(Index: Integer; OUT SensorName: PChar; OUT SensorStyle: Byte): HResult;
begin
 Result := S_OK;
 Case Index of
  0: begin
      SensorName := Pchar('Random Numbers');
      SensorStyle := 0;
     End;
  1: Begin
      SensorName := PChar('Return Parameters');
      SensorStyle := 1;
     End;
  else begin
        Result := E_FAIL;
        LastErrorString := 'Index out of bounds';
       End;
 End;
end;

function TCM2GEN_Test.Init: HResult;
begin
 Randomize; // Is used in conjuction with the Random command in the "Random Numbers" sensor
 Max := 255;
 Result := S_OK;
end;

function TCM2GEN_Test.Name(out Name: Pchar): HResult;
begin
 Name := PChar('Test');
 Result := S_OK;
end;

procedure TCM2GEN_Test.Parser(Param: String);
Var
 I: Integer;
 N, V: String;
begin
 For I := 1 to countfilt(Param, '|') do begin
  N := GetFilt(Param, I, '|');
  V := GetFilt(N, 2, '=');
  N := uppercase(GetFilt(N, 1, '='));
  If (N = 'MAX') or (N = 'RANGE') then
  try
   Max := StrToInt(V);
  except
   Max := 255;
  End;

 End;
end;

function TCM2GEN_Test.RequiredCoreVersion(out RCV: Word): HResult;
begin
 RCV := RequiredCV;
 Result := S_OK;
end;

function TCM2GEN_Test.SetParentHandle(Handle: THandle): HResult;
begin
 // We don't need this handle so do nothing
 Result := S_OK;
end;

function TCM2GEN_Test.Setup: HResult;
begin
 Result := E_NOTIMPL;
end;

function TCM2GEN_Test.Terminating: HResult;
begin
 Result := S_OK; // We dont need to free any memory
end;

function TCM2GEN_Test.Version(out Version: word): HResult;
begin
 Version := PlugInVersion;
 Result := S_OK;
end;

function TCM2GEN_Test.GetDefault(SensorIndex, Index: Integer; out Name,
  Value, Description: PChar): HResult;
begin
 // No required tags for this
 Result := S_OK;
end;

function TCM2GEN_Test.GetDefaultCount(SensorIndex: Integer;
  out DefaultCount: Integer): HResult;
begin
 DefaultCount := 0;
 Result := S_OK;
end;

function TCM2GEN_Test.GetOptional(SensorIndex, Index: Integer;
  out Name, Value, Description: PChar): HResult;
begin
 Result := S_OK;
 If SensorIndex = 0 then begin
  Case Index of
   0: Begin
       Name := 'Range';
       Value := '255';
       Description := 'Generate random number between 0 - "range", default value is 255 - Range must be between 0 - 65535';
      End;
  End;
 End;
end;

function TCM2GEN_Test.GetOptionalCount(SensorIndex: Integer;
  out OptionalCount: Integer): HResult;
begin
 If SensorIndex = 0 Then OptionalCount := 1
  else OptionalCount := 0;
 Result := S_OK;
end;

function TCM2GEN_Test.UpdateFileURL(out URL: Pchar): HResult;
begin
 URL := PChar(AutoUpdateFile);
 Result := S_OK;
end;

function TCM2GEN_Test.ObjIns(SensorIndex: Integer; ParameterName: PChar;
  out ValueType: Byte; out ValueRange: PChar): HResult;
begin
 Result := S_OK;
 Case SensorIndex of
  0: Begin
      If uppercase(ParameterName) = 'RANGE' then begin
       // Range := '1|65535';
       ValueType := vtEditor;
       ValueRange := '';
      end;
     end;
 end;
end;

function TCM2GEN_Test.ShowSensor(Index: Integer;
  out ShowAt: Byte): HResult;
begin
 Result := S_OK;
 ShowAt := saDesignTime;
 Case Index of
  1: ShowAt := saRuntimeOnly;
 end;
end;

function TCM2GEN_Test.GetMenuCount(SensorIndex: Integer;
  out MenuCount: Byte): HResult;
begin
 MenuCount := 0;
 Result := S_OK;
end;

function TCM2GEN_Test.GetMenuItem(SensorIndex: Integer; Index: Byte;
  out MenuTitle, MenuHint: PChar): HResult;
begin
 MenuTitle := '';
 MenuHint := '';
 Result := S_OK;
end;

function TCM2GEN_Test.OnEditor(SensorIndex: Integer; ParameterName,
  FullParameterValues: PChar; out Value: PChar): HResult;
Var
 RD: TRangeDialog;
 Val: Integer;
begin
 Result := S_OK;
 Case SensorIndex of
  0: Begin
      // Random numbers
      If uppercase(ParameterName) = 'RANGE' then begin
       RD := TRangeDialog.Create(nil);
       Try
        Val := StrToInt(Value);
       except
        Val := 1000;
       end;
       If RD.Execute(Val) then begin
        Range := IntToStr(Val);
        Value := Pchar(Range);
       end;
       FreeAndNil(RD);
      end;
     end;
 end;
end;

function TCM2GEN_Test.OnMenu(SensorIndex: Integer; Index: Byte;
  out ResultString: PChar): HResult;
begin
 Result := S_OK;
end;

initialization
  TComObjectFactory.Create(ComServer, TCM2GEN_Test, Class_CM2GEN_Test,
    'CM2GEN_Test', 'CM2 General Plugin - Test', ciMultiInstance, tmApartment);
end.
