unit MySkinFilter;

interface

uses Windows, Classes, KsSkinObjects, KsSkinSource;

const

  MySign: array [0..13] of char = 'My skin format';

type

{ TMySkinFilter }

  TMySkinFilter = class(TSeSkinFilter)
  public
    constructor Create; override;
    { }
    class function GetFileExtension: string; override;
    class function GetFilterName: string; override;
    { Filter }
    procedure ReadSkin(Stream: TStream); override;
    procedure WriteSkin(Stream: TStream); override;
  end;

implementation {===============================================================}

uses Lzh;

{ TMySkinFilter =====================================================}

constructor TMySkinFilter.Create;
begin
  inherited;
end;

class function TMySkinFilter.GetFileExtension: string;
begin
  Result := 'myskin';
end;

class function TMySkinFilter.GetFilterName: string;
begin
  Result := 'MySkin Format';
end;

procedure TMySkinFilter.ReadSkin(Stream: TStream);
var
  MemStream: TStream;
  LzhStream: TLZHStream;
  S: array [0..13] of Char;
begin
  { Read sign }
  Stream.Read(S, SizeOf(MySign));
  if S <> MySign then Exit;

  { Read skin }
  MemStream := TMemoryStream.Create;
  try
    { UnCompress}
    LzhStream := TLZHStream.Create(Stream, MemStream);
    try
      LzhStream.UnPack;
    finally
      LzhStream.Free;
    end;
    { Read SkinSource from MemStream }
    MemStream.Position := 0;
    ReadSkinSource(MemStream);
  finally
    MemStream.Free;
  end;
end;

procedure TMySkinFilter.WriteSkin(Stream: TStream);
var
  MemStream: TStream;
  LzhStream: TLZHStream;
  Size: integer;
begin
  { Write sign }
  Stream.Write(MySign, SizeOf(MySign));

  { Write skin }
  MemStream := TMemoryStream.Create;
  try
    { Write SkinSource to MemStream }
    WriteSkinSource(MemStream);
    { Compress}
    LzhStream := TLZHStream.Create(MemStream, Stream);
    try
      MemStream.Position := 0;
      Size := LzhStream.Pack(MemStream.Size);
    finally
      LzhStream.Free;
    end;
  finally
    MemStream.Free;
  end;
end;

initialization
  DefaultFilter := TMySkinFilter;
  RegisterSkinFilter(TMySkinFilter);
finalization
  UnregisterSkinFilter(TMySkinFilter);
end.
