/**********************************************************************
 *<
	FILE: patchmaker.h

	DESCRIPTION:

	CREATED BY: Kwak CJ

	HISTORY: Created 08/11/19

 *>	Copyright (c) 2008, All Rights Reserved.
 **********************************************************************/
#ifndef __PATCHMAKER_H__
#define __PATCHMAKER_H__

#ifndef WINVER
#define WINVER          0x0500
#endif

#ifndef _WIN32_WINNT
#define _WIN32_WINNT    0x0500
#endif

#pragma once
#include <windows.h>        // 윈도우즈 헤더
#include <commctrl.h>
#include "resource.h"

const char* const SCRIPT_DIRECTORY = "script";
const char* const SCRIPT_SUB_DIRECTORY[] = { "script\\direct", "script\\interface", "script\\language", "script\\resource" };
const char* const ZIP_PASSWORD = "dkdlfltmehdwjq10aks";
const char* const BACKUP_DIRECTORY_NAME = "oldfile backup";

enum eScriptSubDirectory
{
	   eDIRECT_DIRECTORY = 0,
	eINTERFACE_DIRECTORY,
	 eLANGUAGE_DIRECTORY,
	 eRESOURCE_DIRECTORY,
	eSUB_DIRECTORY_MAX_COUNT,
};

struct Packfile
{
	char directory[ 1024 ];			// 디렉토리
};

struct Packlist
{
	Packfile* pack;
	ULONG     offset;				// 오프셋
	ULONG     length;				// 전체갯수
};

struct Patchfile
{
	char     fileName[ 1024 ];		// 파일이름
	DWORD    sizeHigh;				// 파일크기
	DWORD    sizeLow;				// 파일크기
	FILETIME writeTime;				// 파일날짜
};

struct Patchlist
{
	Patchfile* file;				// 업데이트 파일 구조체
	ULONG      offset;				// 오프셋
	ULONG      length;				// 전체갯수
};

class Patchmaker
{
private:
	HWND       m_wnd;
	HICON      m_icon;
	TCHAR*     m_windowName;
	bool       m_endApp;
	char       m_currentDirectory[ MAX_PATH ];

	char       m_sourceDirectory[ 1024 ];
	char       m_targetDirectory[ 1024 ];
	char       m_workerDirectory[ 1024 ];
	char       m_backupDirectory[ 1024 ]; // 새로 패치 되면서 덮어씌우기 될 기존의 파일을 백업시킬 폴더. taget폴더 안에 생성한다.


	Packlist   m_packlist;
	Patchlist  m_patchlist;
	HANDLE     m_workerThread;

	void       EventWrite      ( LPCTSTR format, ... );

	bool       GetFileTimeUTC  ( char* filename, SYSTEMTIME* utc );
	bool       SetFileTimeUTC  ( char* filename, SYSTEMTIME* utc );

	void       FileTimeToUTC   ( FILETIME* filetime );
	void       FileTimeToDOS   ( FILETIME* filetime );
	long       FileTimeCompare ( char* arg1, char* arg2, int* error );

	bool       Pack            ( char* source, char* target, SYSTEMTIME* utc=NULL,  bool bReverseWrite = true );
	bool       Unpack          ( char* source, char* target, SYSTEMTIME* utc=NULL );

	int        Verify          ( char* root, char* sub, int& files, int& directories );
	int        Verify          ( char* filename );

	bool       StartPatch      ( );
	int        Xcopy           ( char* sub );
	int        Search          ( char* root, char* sub, int& files, int& directories, bool opcode=false );

public:
	Patchmaker(void);

	int        Run             ( HINSTANCE instance, HINSTANCE prevInstance, LPSTR cmdLine, int cmdShow );
	LRESULT    MsgProc         ( HWND wnd, UINT msg, WPARAM wParam, LPARAM lParam );

	HWND       GetHwnd         ( ) { return m_wnd; }
	TCHAR*     GetWindowName   ( ) { return m_windowName; }

	bool       CommandLine     ( LPSTR cmdLine );
	BOOL       SetStatus       ( HWND wnd, char* msg );
	void       Progress        ( DWORD length, DWORD offset );

	DWORD      WorkerThread    ( );
	void	   ErrorPrint(char *msg);
private:
	LRESULT    Destroy         ( HWND wnd, WPARAM wParam, LPARAM lParam );
	LRESULT    InitDialog      ( HWND wnd, WPARAM wParam, LPARAM lParam );
	LRESULT    Command         ( HWND wnd, WPARAM wParam, LPARAM lParam );
	LRESULT    DropFiles       ( HWND wnd, WPARAM wParam, LPARAM lParam );
	LRESULT    PatchMsg        ( HWND wnd, WPARAM wParam, LPARAM lParam );

public:
	virtual ~Patchmaker(void);
};

#endif // __PATCHMAKER_H__
