{************************************************************}
{  Delphi KOL Extensions                                     }
{  Ported of RXVERINFO by Valery Dudashenko aka Abyss® [VMD] }
{  Copyright (c) 2001, 2009 Abyss®                           }
{  mailto: vmd.abyss@gmail.com                               }
{************************************************************}

unit KOLVerInfo;

interface

uses Windows, KOL;

type
  TVersionLanguage = (vlArabic, vlBulgarian, vlCatalan, vlTraditionalChinese,
    vlCzech, vlDanish, vlGerman, vlGreek, vlUSEnglish, vlCastilianSpanish,
    vlFinnish, vlFrench, vlHebrew, vlHungarian, vlIcelandic, vlItalian,
    vlJapanese, vlKorean, vlDutch, vlNorwegianBokmel, vlPolish,
    vlBrazilianPortuguese, vlRhaetoRomanic, vlRomanian, vlRussian,
    vlCroatoSerbian, vlSlovak, vlAlbanian, vlSwedish, vlThai, vlTurkish,
    vlUrdu, vlBahasa, vlSimplifiedChinese, vlSwissGerman, vlUKEnglish,
    vlMexicanSpanish, vlBelgianFrench, vlSwissItalian, vlBelgianDutch,
    vlNorwegianNynorsk, vlPortuguese, vlSerboCroatian, vlCanadianFrench,
    vlSwissFrench, vlUnknown);

  TVersionCharSet = (vcsASCII, vcsJapan, vcsKorea, vcsTaiwan, vcsUnicode,
    vcsEasternEuropean, vcsCyrillic, vcsMultilingual, vcsGreek, vcsTurkish,
    vcsHebrew, vcsArabic, vcsUnknown);

  TLongVersion = record
    case Integer of
    0: (All: array[1..4] of Word);
    1: (MS, LS: LongInt);
  end;

{ TVersionInfo }

  PKolVersionInfo = ^TKolVersionInfo;
  TKolVersionInfo = Object(TObj)
  private
    FFileName: PChar;
    FValid: Boolean;
    FSize: DWORD;
    FBuffer: PChar;
    FHandle: DWORD;
    procedure ReadVersionInfo;
    function GetFileName: string;
    procedure SetFileName(const Value: string);
    function GetTranslation: Pointer;
    function GetFixedFileInfo: PVSFixedFileInfo;
    function GetFileLongVersion: TLongVersion;
    function GetProductLongVersion: TLongVersion;
    function GetTranslationString: string;
    function GetComments: string;
    function GetCompanyName: string;
    function GetFileDescription: string;
    function GetFileVersion: string;
    function GetVersionNum: Longint;
    function GetInternalName: string;
    function GetLegalCopyright: string;
    function GetLegalTrademarks: string;
    function GetOriginalFilename: string;
    function GetProductVersion: string;
    function GetProductName: string;
    function GetSpecialBuild: string;
    function GetPrivateBuild: string;
    function GetVersionLanguage: TVersionLanguage;
    function GetVersionCharSet: TVersionCharSet;
    function GetVerFileDate: TDateTime;
  protected
    constructor Create;
  public
    destructor Destroy; virtual;{Do not use call Destroy directly. Use Free method instead.}

    function GetVerValue(const VerName: string): string;
    property FileName: string read GetFileName write SetFileName;
    { TODO : ReadOnly Propertyes }
    property Valid: Boolean read FValid;
    property FixedFileInfo: PVSFixedFileInfo read GetFixedFileInfo;
    property FileLongVersion: TLongVersion read GetFileLongVersion;
    property ProductLongVersion: TLongVersion read GetProductLongVersion;
    property Translation: Pointer read GetTranslation;
    property VersionLanguage: TVersionLanguage read GetVersionLanguage;
    property VersionCharSet: TVersionCharSet read GetVersionCharSet;
    property VersionNum: Longint read GetVersionNum;
    property Comments: string read GetComments;
    property CompanyName: string read GetCompanyName;
    property FileDescription: string read GetFileDescription;
    property FileVersion: string read GetFileVersion;
    property InternalName: string read GetInternalName;
    property LegalCopyright: string read GetLegalCopyright;
    property LegalTrademarks: string read GetLegalTrademarks;
    property OriginalFilename: string read GetOriginalFilename;
    property ProductVersion: string read GetProductVersion;
    property ProductName: string read GetProductName;
    property SpecialBuild: string read GetSpecialBuild;
    property PrivateBuild: string read GetPrivateBuild;
    property Values[const Name: string]: string read GetVerValue;
    property VerFileDate: TDateTime read GetVerFileDate;
  end;

function NewVersionInfo(const AFileName: string): PKolVersionInfo;
function LongVersionToString(const Version: TLongVersion): string;
function StringToLongVersion(const Str: string): TLongVersion;
function AppFileName: string;
function AppVerInfo: PKOLVersionInfo;

implementation

uses Objects;

function MemAlloc(Size: Longint): Pointer;
begin
  GetMem(Result, Size);
end;

const
  LanguageValues: array[TVersionLanguage] of Word = ($0401, $0402, $0403,
    $0404, $0405, $0406, $0407, $0408, $0409, $040A, $040B, $040C, $040D,
    $040E, $040F, $0410, $0411, $0412, $0413, $0414, $0415, $0416, $0417,
    $0418, $0419, $041A, $041B, $041C, $041D, $041E, $041F, $0420, $0421,
    $0804, $0807, $0809, $080A, $080C, $0810, $0813, $0814, $0816, $081A,
    $0C0C, $100C, $0000);

const
  CharacterSetValues: array[TVersionCharSet] of Integer = (0, 932, 949, 950,
    1200, 1250, 1251, 1252, 1253, 1254, 1255, 1256, -1);

{ TODO : Func's Ported of StrUtils, SysUtils, FileUtils(RX) }
type
  LongRec = packed record
    case Integer of
      0: (Lo, Hi: Word);
      1: (Words: array [0..1] of Word);
      2: (Bytes: array [0..3] of Byte);
  end;

const
  NullDate: TDateTime = 0;


function StrAlloc(Size: Cardinal): PChar;
begin
  Inc(Size, SizeOf(Cardinal));
  GetMem(Result, Size);
  Cardinal(Pointer(Result)^) := Size;
  Inc(Result, SizeOf(Cardinal));
end;

procedure StrDispose(Str: PChar);
begin
  if Str <> nil then
  begin
    Dec(Str, SizeOf(Cardinal));
    FreeMem(Str, Cardinal(Pointer(Str)^));
  end;
end;

function StrPas(const Str: PChar): string;
begin
  Result := Str;
end;

function FileAge(const FileName: string): Integer;
var
  Handle: THandle;
  FindData: TWin32FindData;
  LocalFileTime: TFileTime;
begin
  Handle := FindFirstFile(PChar(FileName), FindData);
  if Handle <> INVALID_HANDLE_VALUE then
  begin
    Windows.FindClose(Handle);
    if (FindData.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) = 0 then
    begin
      FileTimeToLocalFileTime(FindData.ftLastWriteTime, LocalFileTime);
      if FileTimeToDosDateTime(LocalFileTime, LongRec(Result).Hi,
        LongRec(Result).Lo) then Exit;
    end;
  end;
  Result := -1;
end;


function FileDateToDateTime(FileDate: Integer): TDateTime;
var
 SummDate: TDateTime;
 SummTime: TDateTime;
begin
    EncodeDate(
      (LongRec(FileDate).Hi shr 9 + 1980),
      (LongRec(FileDate).Hi shr 5 and 15),
      (LongRec(FileDate).Hi and 31), SummDate);
    EncodeDate(
      (LongRec(FileDate).Lo shr 11),
      (LongRec(FileDate).Lo shr 5 and 63),
      (LongRec(FileDate).Lo and 31 shl 1), SummTime);
    Result := SummDate + SummTime;
end;


function FileDateTime(const FileName: string): System.TDateTime;
var
  Age: Longint;
begin
  Age := FileAge(FileName);
  if Age = -1 then
    Result := NullDate
  else
    Result := FileDateToDateTime(Age);
end;
{ TODO : END by Func's Ported of StrUtils, SysUtils, FileUtils(RX)}


{ TVersionInfo }
function NewVersionInfo(const AFileName: string): PKolVersionInfo;
begin
  New(Result, Create);
  Result.FFileName := StrPCopy(StrAlloc(Length(AFileName) + 1), AFileName);
  Result.ReadVersionInfo;
end;

constructor TKolVersionInfo.Create;
begin
  inherited;
end;

destructor TKolVersionInfo.Destroy;
begin
  if FBuffer <> nil then FreeMem(FBuffer, FSize);
  StrDispose(FFileName);
  inherited;
end;

procedure TKolVersionInfo.ReadVersionInfo;
begin
  FValid := False;
  FSize := KOL.GetFileVersionInfoSize(FFileName, FHandle);
  if FSize > 0 then
    try
      FBuffer := MemAlloc(FSize);
      FValid := KOL.GetFileVersionInfo(FFileName, FHandle, FSize, FBuffer);
    except
      FValid := False;
      raise;
    end;
end;

function TKolVersionInfo.GetFileName: string;
begin
  Result := StrPas(FFileName);
end;

procedure TKolVersionInfo.SetFileName(const Value: string);
begin
  if FBuffer <> nil then FreeMem(FBuffer, FSize);
  FBuffer := nil;
  StrDispose(FFileName);
  FFileName := StrPCopy(StrAlloc(Length(Value) + 1), Value);
  ReadVersionInfo;
end;

function TKolVersionInfo.GetTranslation: Pointer;
var
  Len: Cardinal;
begin
  Result := nil;
  if Valid then KOL.VerQueryValue(FBuffer, '\VarFileInfo\Translation', Result, Len)
  else Result := nil;
end;

function TKolVersionInfo.GetTranslationString: string;
var
  P: Pointer;
begin
  Result := '';
  P := GetTranslation;
  if P <> nil then
    Result := Int2Hex(MakeLong(HiWord(Longint(P^)), LoWord(Longint(P^))), 8);
end;

function TKolVersionInfo.GetVersionLanguage: TVersionLanguage;
var
  P: Pointer;
begin
  P := GetTranslation;
  for Result := vlArabic to vlUnknown do
    if LoWord(Longint(P^)) = LanguageValues[Result] then Break;
end;

function TKolVersionInfo.GetVersionCharSet: TVersionCharSet;
var
  P: Pointer;
begin
  P := GetTranslation;
  for Result := vcsASCII to vcsUnknown do
    if HiWord(Longint(P^)) = CharacterSetValues[Result] then Break;
end;

function TKolVersionInfo.GetFixedFileInfo: PVSFixedFileInfo;
var
  Len: Cardinal;
begin
  Result := nil;
  if Valid then VerQueryValue(FBuffer, '\', Pointer(Result), Len)
  else Result := nil;
end;

function TKolVersionInfo.GetProductLongVersion: TLongVersion;
begin
  Result.MS := FixedFileInfo^.dwProductVersionMS;
  Result.LS := FixedFileInfo^.dwProductVersionLS;
end;

function TKolVersionInfo.GetFileLongVersion: TLongVersion;
begin
  Result.MS := FixedFileInfo^.dwFileVersionMS;
  Result.LS := FixedFileInfo^.dwFileVersionLS;
end;

function TKolVersionInfo.GetVersionNum: Longint;
begin
  if Valid then Result := FixedFileInfo^.dwFileVersionMS
  else Result := 0;
end;

function TKolVersionInfo.GetVerValue(const VerName: string): string;
var
  szName: array[0..255] of Char;
  Value: Pointer;
  Len: Cardinal;
begin
  Result := '';
  if Valid then begin
    StrPCopy(szName, '\StringFileInfo\' + GetTranslationString + '\' + VerName);
    if KOL.VerQueryValue(FBuffer, szName, Value, Len) then
      Result := StrPas(PChar(Value));
  end;
end;

function TKolVersionInfo.GetComments: string;
begin
  Result := GetVerValue('Comments');
end;

function TKolVersionInfo.GetCompanyName: string;
begin
  Result := GetVerValue('CompanyName');
end;

function TKolVersionInfo.GetFileDescription: string;
begin
  Result := GetVerValue('FileDescription');
end;

function TKolVersionInfo.GetFileVersion: string;
begin
  Result := GetVerValue('FileVersion');
  if (Result = '') and Valid then
    Result := LongVersionToString(FileLongVersion);
end;

function TKolVersionInfo.GetInternalName: string;
begin
  Result := GetVerValue('InternalName');
end;

function TKolVersionInfo.GetLegalCopyright: string;
begin
  Result := GetVerValue('LegalCopyright');
end;

function TKolVersionInfo.GetLegalTrademarks: string;
begin
  Result := GetVerValue('LegalTrademarks');
end;

function TKolVersionInfo.GetOriginalFilename: string;
begin
  Result := GetVerValue('OriginalFilename');
end;

function TKolVersionInfo.GetProductVersion: string;
begin
  Result := GetVerValue('ProductVersion');
  if (Result = '') and Valid then
    Result := LongVersionToString(ProductLongVersion);
end;

function TKolVersionInfo.GetProductName: string;
begin
  Result := GetVerValue('ProductName');
end;

function TKolVersionInfo.GetSpecialBuild: string;
begin
  Result := GetVerValue('SpecialBuild');
end;

function TKolVersionInfo.GetPrivateBuild: string;
begin
  Result := GetVerValue('PrivateBuild');
end;

function TKolVersionInfo.GetVerFileDate: TDateTime;
begin
  if FileExists(FileName) then
    Result := FileDateTime(FileName)
  else Result := NullDate;
end;

{ Long version string routines }

function LongVersionToString(const Version: TLongVersion): string;
begin
  with Version do
    Result := Format('%d.%d.%d.%d', [All[2], All[1], All[4], All[3]]);
end;

function StringToLongVersion(const Str: string): TLongVersion;
var
  Sep: Integer;
  Tmp, Fragment: string;
  I: Word;
begin
  Tmp := Str;
  for I := 1 to 4 do begin
    Sep := Pos('.', Tmp);
    if Sep = 0 then Sep := Pos(',', Tmp);
    if Sep = 0 then Fragment := Tmp
    else begin
      Fragment := Copy(Tmp, 1, Sep - 1);
      Tmp := Copy(Tmp, Sep + 1, MaxInt);
    end;
    if Fragment = '' then Result.All[I] := 0
    else Result.All[I] := Str2Int(Fragment);
  end;
  I := Result.All[1];
  Result.All[1] := Result.All[2];
  Result.All[2] := I;
  I := Result.All[3];
  Result.All[3] := Result.All[4];
  Result.All[4] := I;
end;

function AppFileName: string;
var
  FileName: array[0..255] of Char;
begin
  if IsLibrary then begin
    GetModuleFileName(HInstance, FileName, SizeOf(FileName) - 1);
    Result := StrPas(FileName);
  end
  else Result := ParamStr(0);
end;

function AppVerInfo: PKOLVersionInfo;
begin
  Result := NewVersionInfo(AppFileName);
end;

end.
