unit uConflictDirectCostTypeDefs;

interface

uses Classes, SysUtils, {$IFDEF MSWINDOWS} Windows, {$ENDIF} uHCDefines,
  uHCTypeDefs;

const
  CHCConflictDirectCostVersion = '2017.12.03';

type
  THCKillCounts = array [0 .. 9, 0 .. SIDES - 1] of ulong;
  THCHumanCost = array[0 .. SIDES - 1] of UInt64; // ALE 20171203 add up deaths
  THCMonetaryCost = array[0 .. SIDES - 1] of UInt64; // ALE 20171203 add up dollars

  // ALE 20150713 Forward type declarations so we can use them in later types
  PMMFConflictDirectCost = ^TMMFConflictDirectCost;

  // ALE 20150819 Structure for the memory mapped file
  TMMFConflictDirectCost = class
  private
    Fversion: integer; // ALE 20150820 DLL and Viewer Server must have same version
    Fread: Boolean; // ALE 20150819 the consumer will set this to True when the information has been read
    Fgamestartdatetime: THCDateTimeRec;
    Fgametime: long;
    Fgametimeleft: long;
  public
    Fkillcounts: THCKillCounts;
    Fhumancost: THCHumanCost;
    Fmonetarycost: THCMonetaryCost;
    property version: integer read Fversion write Fversion;
    property readit: Boolean read Fread write Fread;
    property gamestartdatetime: THCDateTimeRec read Fgamestartdatetime;
    property gametime: long read Fgametime write Fgametime;
    property gametimeleft: long read Fgametimeleft write Fgametimeleft;
    property killcounts: THCKillCounts read Fkillcounts write Fkillcounts;
    property humancost: THCHumanCost read Fhumancost write Fhumancost;
    property monetarycost: THCMonetaryCost read Fmonetarycost write Fmonetarycost;

  end;

{$IFDEF MSWINDOWS}

  TTGPMemMappedFile = class
  private
    fHMapFile: THandle;
  public
    fPSharedData: Pointer;
    constructor Create; overload;
    destructor Destroy; override;
    function CreateNamedFileMapping(const Name: String;
      const pBufSize: NativeInt; pSharedData: Pointer): THandle;
    function GetSharedData(const Name: String;
      const pBufSize: NativeInt): Pointer;
  end;
{$ENDIF}

implementation


{$IFDEF MSWINDOWS}

constructor TTGPMemMappedFile.Create;
begin
  inherited;
  fHMapFile := CreateNamedFileMapping('HCConflictDirectCost', SizeOf(TMMFConflictDirectCost),
    fPSharedData);
end;

destructor TTGPMemMappedFile.Destroy;
begin
  UnmapViewOfFile(fPSharedData);
  CloseHandle(fHMapFile);
  inherited;
end;

function TTGPMemMappedFile.CreateNamedFileMapping(const Name: String;
  const pBufSize: NativeInt; pSharedData: Pointer): THandle;
begin
  Result := CreateFileMapping(INVALID_HANDLE_VALUE, nil, PAGE_READWRITE, 0,
    pBufSize, PChar(Name));

  Win32Check(Result > 0);

  fPSharedData := MapViewOfFile(Result, FILE_MAP_ALL_ACCESS, 0, 0, pBufSize);

  Win32Check(Assigned(fPSharedData));
end;

function TTGPMemMappedFile.GetSharedData(const Name: String;
  const pBufSize: NativeInt): Pointer;
begin
  fHMapFile := OpenFileMapping(FILE_MAP_ALL_ACCESS, False, PChar(Name));
  Win32Check(fHMapFile > 0);

  Result := MapViewOfFile(fHMapFile, FILE_MAP_ALL_ACCESS, 0, 0, pBufSize);

  Win32Check(Assigned(Result));
end;
{$ENDIF}

end.
