unit LogDataMain;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics,
  Controls, Forms, Dialogs, StdCtrls, ExtCtrls, IniFiles, IdBaseComponent,
  IdComponent, IdUDPBase, IdUDPServer, IdSocketHandle,IdGlobal, Menus,
  WinSkinData;
type
  TFrmLogData = class(TForm)
    Label3: TLabel;
    Label4: TLabel;
    Timer1: TTimer;
    IdUDPServerLog: TIdUDPServer;
    StartTimer: TTimer;
    MainMenu: TMainMenu;
    MainMenu_LogData: TMenuItem;
    CloseTimer: TTimer;

    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure WriteLogFile();

    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure StartTimerTimer(Sender: TObject);
    procedure MainMenu_LogDataClick(Sender: TObject);
    procedure CloseTimerTimer(Sender: TObject);
    procedure IdUDPServerLogUDPRead(Sender: TObject; AData: TBytes;
      ABinding: TIdSocketHandle);
  private
    LogMsgList: TStringList;
    m_boRemoteClose: Boolean;
    { Private declarations }
  public
    procedure MyMessage(var MsgData: TWmCopyData); message WM_COPYDATA;
    { Public declarations }
  end;

var
  FrmLogData: TFrmLogData;
  {This file is generated by DeDe Ver 3.50.02 Copyright (c) 1999-2002 DaFixer}

implementation

uses LDShare, Grobal2, HUtil32, LogManage;

{$R *.DFM}

procedure TFrmLogData.FormCreate(Sender: TObject);
var
  Conf: TIniFile;
  nX, nY: Integer;
begin
  g_dwGameCenterHandle := Str_ToInt(ParamStr(1), 0);
  nX := Str_ToInt(ParamStr(2), -1);
  nY := Str_ToInt(ParamStr(3), -1);
  if (nX >= 0) or (nY >= 0) then begin
    Left := nX;
    Top := nY;
  end;

  m_boRemoteClose := False;
  SendGameCenterMsg(SG_FORMHANDLE, IntToStr(Self.Handle));
  SendGameCenterMsg(SG_STARTNOW, 'Log Server has started...');
  LogMsgList := TStringList.Create;
  StartTimer.Enabled := True;
end;

procedure TFrmLogData.FormDestroy(Sender: TObject);
begin
  LogMsgList.Free;
end;

procedure TFrmLogData.CloseTimerTimer(Sender: TObject);
begin
  Caption := 'Closing...';
  if SearchStatus then QuitFlag := True else Close;
end;

procedure TFrmLogData.FormCloseQuery(Sender: TObject;
  var CanClose: Boolean);
begin
  if m_boRemoteClose then exit;
  if Application.MessageBox('Are you sure you want to close the Log Server?',
    'Message',
    MB_YESNO + MB_ICONQUESTION) = IDYES then begin
    m_boRemoteClose := True;
    CloseTimer.Enabled := True;
  end else CanClose := False;
end;

procedure TFrmLogData.Timer1Timer(Sender: TObject);
begin
  WriteLogFile();
end;

procedure TFrmLogData.WriteLogFile();
var
  I: Integer;
  Year, Month, Day, Hour, Min, Sec, MSec: Word;
  sLogDir, sLogFile: string;
  s2E8: string;
  F: TextFile;
begin
  if LogMsgList.Count <= 0 then exit;
  DecodeDate(Date, Year, Month, Day);
  DecodeTime(Time, Hour, Min, Sec, MSec); //ExtractFilePath(ParamStr(0)) +
  sLogDir := sBaseDir + IntToStr(Year) + '-' + IntToString(Month) + '-' + IntToString(Day);
  if not DirectoryExists(sLogDir) then begin
    CreateDirectoryA(PChar(sLogDir), nil);
  end;
  sLogFile := sLogDir + '\Log-' + IntToString(Hour) + 'h' + IntToString((Min div 10) * 2) + 'm.txt';
  Label4.Caption := sLogFile;
  try
    AssignFile(F, sLogFile);
    if not FileExists(sLogFile) then Rewrite(F)
    else Append(F);
    for I := 0 to LogMsgList.Count - 1 do begin
      Writeln(F, LogMsgList.Strings[I] + #9 + FormatDateTime('yyyy-mm-dd hh:mm:ss', Now));
      Flush(F)
    end;
    LogMsgList.Clear;
  finally
    CloseFile(F);
  end;
end;

procedure TFrmLogData.MainMenu_LogDataClick(Sender: TObject);
begin
  FrmLogManage.Show;
end;

procedure TFrmLogData.MyMessage(var MsgData: TWmCopyData);
var
  sData: string;
  //ProgramType: TProgamType;
  wIdent: Word;
begin
  wIdent := HiWord(MsgData.From);
  //  ProgramType:=TProgamType(LoWord(MsgData.From));
  sData := StrPas(MsgData.CopyDataStruct^.lpData);
  case wIdent of //
    GS_QUIT: begin
        m_boRemoteClose := True;
        CloseTimer.Enabled := True;
        Close();
      end;
    1: ;
    2: ;
    3: ;
  end; // case
end;

procedure TFrmLogData.StartTimerTimer(Sender: TObject);
var
  Conf: TIniFile;
  boMinimize: Boolean;
begin
  StartTimer.Enabled := False;
  Conf := TIniFile.Create('.\LogData.ini');
  if Conf <> nil then begin
    sBaseDir := Conf.ReadString('Setup', 'BaseDir', sBaseDir);
    sServerName := Conf.ReadString('Setup', 'Caption', sServerName);
    sServerName := Conf.ReadString('Setup', 'ServerName', sServerName);
    nServerPort := Conf.ReadInteger('Setup', 'Port', nServerPort);
    boMinimize := Conf.ReadBool('Setup', 'Minimize', True);
    Conf.Free;
  end;
  Caption := sCaption + ' (' + sServerName + ')';
  IdUDPServerLog.DefaultPort := nServerPort;
  IdUDPServerLog.Active := True;
  if boMinimize then Application.Minimize;
  SendGameCenterMsg(SG_STARTOK, 'Log Server startup complete...');
end;

procedure TFrmLogData.IdUDPServerLogUDPRead(Sender: TObject; AData: TBytes;
  ABinding: TIdSocketHandle);
var
  LogStr: string;
begin
  try
    SetLength(LogStr, Length(AData));
    Move(AData[0], LogStr[1], Length(AData));
    LogMsgList.Add(LogStr);
  except

  end;
end;

end.

