#include "Plugin.h"
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <cstring>

#define SRPMAX 1000

//函数指针声明

extern char g_gameName[9+1];
void InitGMC(HINSTANCE hDLLDrv);
void InitSRP(HINSTANCE hDLLDrv,char *ingameName);
//void GetFromFile(char * infilename);

char * Plugin_GetLIBName();

BytesToHBitmap GMCBytesToHBitmap = 0;
BytesToHBitmapTrans GMCBytesToHBitmapTrans =0;
BytesToHBitmapMask GMCBytesToHBitmapMask =0;
LoadALibhead GMCLoadALibhead =0;
GetSuffix GMCGetSuffix =0;
GetFromFile SRPGetFromFile = 0;
SGetSuffix SRPGetSuffix =0;
GetText SRPGetText = 0;
SetText SRPSetText = 0;
SShowSayBox SRPShowSayBox = 0;

void InitGMC(HINSTANCE hDLLDrv)
{



	GMCBytesToHBitmap = (BytesToHBitmap)GetProcAddress(hDLLDrv, "BytesToHBitmap");
	GMCBytesToHBitmapTrans =(BytesToHBitmapTrans)GetProcAddress(hDLLDrv, "BytesToHBitmapTrans");
	GMCBytesToHBitmapMask =(BytesToHBitmapMask)GetProcAddress(hDLLDrv, "BytesToHBitmapMask");
	GMCLoadALibhead =(LoadALibhead)GetProcAddress(hDLLDrv, "LoadALibhead");
	GMCGetSuffix =(GetSuffix)GetProcAddress(hDLLDrv, "GetSuffix");

}




char * Plugin_GetLIBName()
{
	
	char tPath[MAX_PATH];
	sprintf(tPath,"%s%s",g_gameName,(char *)GMCGetSuffix());
	return tPath;
}



//FreeLibrary 

/*

typedef struct _ImageData
{
	unsigned short mWidth;			
	unsigned short mHeight;
	unsigned char mNumber;
	unsigned char mTransparent;
	unsigned char *m_ImageData;	//-- BGRA --//
}ImageData;

int LoadALibhead(char* Libpath,int mod);//预读取文件头
int ResGetAddress(int m,int n,int id);//查找资源地址 （0为无效地址）

typedef struct libkeytru{
	unsigned char Res_Field;
	unsigned char Res_Type;
	unsigned char Res_Id;
} Libkey;



typedef struct libAddrtru{
	unsigned char block;//0x4000=16k
	unsigned short offset;
} LibAddr;

typedef struct headstru{
	char FileLabel[3];
	char gamename[9];
	unsigned short indexsize;
	unsigned short fillnull0;
	Libkey mkeys[2725];
	unsigned char fillnull1;
	union
	{
		int GMCaddrs[2725];
		LibAddr LIBaddrs[2725];
	}PublicAddress;
} Publichead;

Publichead mhead;
int IsALIbLoaded,mFileType;

//lib mFileType==0:
//gmc mFileType==1:

static int getAddressReal(LibAddr* addr);

char * GMCGetSuffix()
{
	return ".gmc";
}

HBITMAP GMCBytesToHBitmap(char *libfile,int type,int restype,int index,int no)
{

	GMCLoadALibhead(libfile,1);
	FILE *fp = fopen(libfile,"rb");
	int Picoffset=ResGetAddress(type,restype,index);
	fseek(fp, Picoffset, SEEK_SET);
	ImageData* im=(ImageData*)malloc(sizeof(ImageData));
	memset(im,0,sizeof(ImageData));
	fread((void*)im,6,1,fp);

	int pixsize=im->mWidth*im->mHeight<<2;
	//int pixsize=im->mWidth*im->mHeight* im->mNumber<<2;
	im->m_ImageData=(unsigned char *)malloc(pixsize+1);
	fseek(fp, Picoffset+6+pixsize*no, SEEK_SET);
	fread((void*)im->m_ImageData,pixsize,1,fp);// 一次性读取Pic全部像素内容 
	fclose(fp);
	fp = NULL;
	BITMAPINFOHEADER bmih;
	bmih.biSize                  = sizeof (BITMAPINFOHEADER) ;
	bmih.biWidth                 = im->mWidth ;
	bmih.biHeight                = im->mHeight;
	bmih.biPlanes                = 1 ;
	bmih.biBitCount              = 32 ;
	bmih.biCompression           = BI_RGB ;
	bmih.biSizeImage             = 0 ;
	bmih.biXPelsPerMeter         = 0 ;
	bmih.biYPelsPerMeter         = 0 ;
	bmih.biClrUsed               = 0 ;
	bmih.biClrImportant          = 0 ;
	LPBYTE   lpBits=NULL; 

	struct BitMAPINFO
	{ 
		BITMAPINFOHEADER bmiHeader; 
		//RGBQUAD          bmiColors[256]; 
	} bi;
	memset(&bi, 0, sizeof(bi));
	bi.bmiHeader.biSize                  = sizeof (BITMAPINFOHEADER) ;
	bi.bmiHeader.biWidth                 = im->mWidth ;
	bi.bmiHeader.biHeight                = im->mHeight;
	bi.bmiHeader.biPlanes                = 1 ;
	bi.bmiHeader.biBitCount              = 32 ;
	bi.bmiHeader.biCompression           = BI_RGB ;
	bi.bmiHeader.biSizeImage             = 0 ;
	bi.bmiHeader.biXPelsPerMeter         = 0 ;
	bi.bmiHeader.biYPelsPerMeter         = 0 ;
	bi.bmiHeader.biClrUsed               = 0 ;
	bi.bmiHeader.biClrImportant          = 0 ;

	/*for (int i=0; i<256; i++)
	{
		bi.bmiColors[i].rgbRed  = i;
		bi.bmiColors[i].rgbGreen= i;
		bi.bmiColors[i].rgbBlue = i;
	}
	

	
	byte datarow[2000000];
	memset(datarow,0,sizeof(datarow));
	byte data32[2000000];
	memset(data32,0,sizeof(data32));
	byte data24[2000000];
	memset(data24,0,sizeof(data24));
	memcpy(datarow,(void*)im->m_ImageData,pixsize);
	

	for(int i = 0 ; i<pixsize; i+=4)
	{
		int t = im->mWidth * 4*(im->mHeight-(i/4)/im->mWidth-1)+i%(im->mWidth*4);
		
		data32[i]= datarow[t];
		data32[i+1]= datarow[t+1];
		data32[i+2]= datarow[t+2];
		data32[i+3]=0;
		//if(data32[i] == 0)data32[i] = 255;
		//else if(data32[i] == 255)data32[i] = 0;
		//if(data32[i+1] == 0)data32[i+1] = 255;
		//else if(data32[i+1] == 255)data32[i+1] = 0;
		//if(data32[i+2] == 0)data32[i+2] = 255;
		//else if(data32[i+2] == 255)data32[i+2] = 0;
		
	}

	for (int j=0;j<im->mHeight;j++)
	{
		for(int i=0;i<im->mWidth*3;i++)

		{
			data24[i+im->mWidth*3*j]=data32[im->mWidth*4*j+i+i/3];
		}
	}

	//HBITMAP hBmp = CreateDIBitmap(NULL, &bi.bmiHeader, CBM_INIT, data24, (BITMAPINFO*)&bi, DIB_RGB_COLORS);
	HBITMAP   hBmp=::CreateDIBSection(NULL,(BITMAPINFO *) &bmih,DIB_RGB_COLORS,  (void **) &lpBits,NULL,0);
	memcpy((void*)lpBits,(void*)data32,pixsize);
	free(im->m_ImageData);
	free(im);
	
	return hBmp;
}

HBITMAP GMCBytesToHBitmapMask(char *libfile,int type,int restype,int index,int no)
{

	GMCLoadALibhead(libfile,1);
	FILE *fp = fopen(libfile,"rb");
	int Picoffset=ResGetAddress(type,restype,index);
	fseek(fp, Picoffset, SEEK_SET);
	ImageData* im=(ImageData*)malloc(sizeof(ImageData));
	memset(im,0,sizeof(ImageData));
	fread((void*)im,6,1,fp);

	int pixsize=im->mWidth*im->mHeight<<2;
	//int pixsize=im->mWidth*im->mHeight* im->mNumber<<2;
	im->m_ImageData=(unsigned char *)malloc(pixsize+1);
	fseek(fp, Picoffset+6+pixsize*no, SEEK_SET);
	fread((void*)im->m_ImageData,pixsize,1,fp);// 一次性读取Pic全部像素内容 
	fclose(fp);
	fp = NULL;
	BITMAPINFOHEADER bmih;
	bmih.biSize                  = sizeof (BITMAPINFOHEADER) ;
	bmih.biWidth                 = im->mWidth ;
	bmih.biHeight                = im->mHeight;
	bmih.biPlanes                = 1 ;
	bmih.biBitCount              = 32 ;
	bmih.biCompression           = BI_RGB ;
	bmih.biSizeImage             = 0 ;
	bmih.biXPelsPerMeter         = 0 ;
	bmih.biYPelsPerMeter         = 0 ;
	bmih.biClrUsed               = 0 ;
	bmih.biClrImportant          = 0 ;
	LPBYTE   lpBits=NULL; 

	struct BitMAPINFO
	{ 
		BITMAPINFOHEADER bmiHeader; 
		//RGBQUAD          bmiColors[256]; 
	} bi;
	memset(&bi, 0, sizeof(bi));
	bi.bmiHeader.biSize                  = sizeof (BITMAPINFOHEADER) ;
	bi.bmiHeader.biWidth                 = im->mWidth ;
	bi.bmiHeader.biHeight                = im->mHeight;
	bi.bmiHeader.biPlanes                = 1 ;
	bi.bmiHeader.biBitCount              = 32 ;
	bi.bmiHeader.biCompression           = BI_RGB ;
	bi.bmiHeader.biSizeImage             = 0 ;
	bi.bmiHeader.biXPelsPerMeter         = 0 ;
	bi.bmiHeader.biYPelsPerMeter         = 0 ;
	bi.bmiHeader.biClrUsed               = 0 ;
	bi.bmiHeader.biClrImportant          = 0 ;

	/*for (int i=0; i<256; i++)
	{
		bi.bmiColors[i].rgbRed  = i;
		bi.bmiColors[i].rgbGreen= i;
		bi.bmiColors[i].rgbBlue = i;
	}
	

	
	byte datarow[2000000];
	memset(datarow,0,sizeof(datarow));
	byte data32[2000000];
	memset(data32,0,sizeof(data32));
	byte data24[2000000];
	memset(data24,0,sizeof(data24));
	memcpy(datarow,(void*)im->m_ImageData,pixsize);
	

	for(int i = 0 ; i<pixsize; i+=4)
	{
		int t = im->mWidth * 4*(im->mHeight-(i/4)/im->mWidth-1)+i%(im->mWidth*4);
		
		data32[i]= datarow[t];
		data32[i+1]= datarow[t+1];
		data32[i+2]= datarow[t+2];
		data32[i+3]=0;
		//if(data32[i] == 0)data32[i] = 255;
		//else if(data32[i] == 255)data32[i] = 0;
		//if(data32[i+1] == 0)data32[i+1] = 255;
		//else if(data32[i+1] == 255)data32[i+1] = 0;
		//if(data32[i+2] == 0)data32[i+2] = 255;
		//else if(data32[i+2] == 255)data32[i+2] = 0;
		
	}

	for (int j=0;j<im->mHeight;j++)
	{
		for(int i=0;i<im->mWidth*3;i++)

		{
			data24[i+im->mWidth*3*j]=data32[im->mWidth*4*j+i+i/3];
		}
	}

	//HBITMAP hBmp = CreateDIBitmap(NULL, &bi.bmiHeader, CBM_INIT, data24, (BITMAPINFO*)&bi, DIB_RGB_COLORS);
	HBITMAP   hBmp=::CreateDIBSection(NULL,(BITMAPINFO *) &bmih,DIB_RGB_COLORS,  (void **) &lpBits,NULL,0);
	memcpy((void*)lpBits,(void*)data32,pixsize);
	free(im->m_ImageData);
	free(im);
	
	return hBmp;
}

HBITMAP GMCBytesToHBitmapTrans(char *libfile,int type,int restype,int index,int no)
{

	GMCLoadALibhead(libfile,1);
	FILE *fp = fopen(libfile,"rb");
	int Picoffset=ResGetAddress(type,restype,index);
	fseek(fp, Picoffset, SEEK_SET);
	ImageData* im=(ImageData*)malloc(sizeof(ImageData));
	memset(im,0,sizeof(ImageData));
	fread((void*)im,6,1,fp);

	int pixsize=im->mWidth*im->mHeight<<2;
	//int pixsize=im->mWidth*im->mHeight* im->mNumber<<2;
	im->m_ImageData=(unsigned char *)malloc(pixsize+1);
	fseek(fp, Picoffset+6+pixsize*no, SEEK_SET);
	fread((void*)im->m_ImageData,pixsize,1,fp);// 一次性读取Pic全部像素内容 
	fclose(fp);
	fp = NULL;
	BITMAPINFOHEADER bmih;
	bmih.biSize                  = sizeof (BITMAPINFOHEADER) ;
	bmih.biWidth                 = im->mWidth ;
	bmih.biHeight                = im->mHeight;
	bmih.biPlanes                = 1 ;
	bmih.biBitCount              = 32 ;
	bmih.biCompression           = BI_RGB ;
	bmih.biSizeImage             = 0 ;
	bmih.biXPelsPerMeter         = 0 ;
	bmih.biYPelsPerMeter         = 0 ;
	bmih.biClrUsed               = 0 ;
	bmih.biClrImportant          = 0 ;
	LPBYTE   lpBits=NULL; 

	struct BitMAPINFO
	{ 
		BITMAPINFOHEADER bmiHeader; 
		//RGBQUAD          bmiColors[256]; 
	} bi;
	memset(&bi, 0, sizeof(bi));
	bi.bmiHeader.biSize                  = sizeof (BITMAPINFOHEADER) ;
	bi.bmiHeader.biWidth                 = im->mWidth ;
	bi.bmiHeader.biHeight                = im->mHeight;
	bi.bmiHeader.biPlanes                = 1 ;
	bi.bmiHeader.biBitCount              = 32 ;
	bi.bmiHeader.biCompression           = BI_RGB ;
	bi.bmiHeader.biSizeImage             = 0 ;
	bi.bmiHeader.biXPelsPerMeter         = 0 ;
	bi.bmiHeader.biYPelsPerMeter         = 0 ;
	bi.bmiHeader.biClrUsed               = 0 ;
	bi.bmiHeader.biClrImportant          = 0 ;

	/*for (int i=0; i<256; i++)
	{
		bi.bmiColors[i].rgbRed  = i;
		bi.bmiColors[i].rgbGreen= i;
		bi.bmiColors[i].rgbBlue = i;
	}
	

	
	byte datarow[2000000];
	memset(datarow,0,sizeof(datarow));
	byte data32[2000000];
	memset(data32,0,sizeof(data32));
	byte data24[2000000];
	memset(data24,0,sizeof(data24));
	memcpy(datarow,(void*)im->m_ImageData,pixsize);
	

	for(int i = 0 ; i<pixsize; i+=4)
	{
		int t = im->mWidth * 4*(im->mHeight-(i/4)/im->mWidth-1)+i%(im->mWidth*4);
		
		if(datarow[t+3] == 255)
		{
			data32[i]= datarow[t];
			data32[i+1]= datarow[t+1];
			data32[i+2]= datarow[t+2];
			data32[i+3]= 0;
		}
		else
		{
			
			data32[i]= 255;
			data32[i+1]= 255;
			data32[i+2]= 255;
			data32[i+3]= 0;
		}
		
		
	}

	for (int j=0;j<im->mHeight;j++)
	{
		for(int i=0;i<im->mWidth*3;i++)

		{
			data24[i+im->mWidth*3*j]=data32[im->mWidth*4*j+i+i/3];
		}
	}

	//HBITMAP hBmp = CreateDIBitmap(NULL, &bi.bmiHeader, CBM_INIT, data24, (BITMAPINFO*)&bi, DIB_RGB_COLORS);
	HBITMAP   hBmp=::CreateDIBSection(NULL,(BITMAPINFO *) &bmih,DIB_RGB_COLORS,  (void **) &lpBits,NULL,0);
	memcpy((void*)lpBits,(void*)data32,pixsize);
	free(im->m_ImageData);
	free(im);

	return hBmp;
}



static int getAddressReal(LibAddr* addr)
{
	return addr->block*0x4000+addr->offset;
}


int GMCLoadALibhead(char* Libpath,int mod)
{
	FILE *fp;
	mFileType= mod;
	if((fp=fopen(Libpath,"r")) == NULL)
	{  

		return -1;
	}
	IsALIbLoaded=1;

	fread(&mhead,sizeof(Publichead),1,fp); // 一次性读取文件头内容 

	fclose(fp);
	return 1;
}

int ResGetAddress(int m,int n,int id)
{
	int i=mhead.indexsize/3;
	for(int j=0;j<i;j++){
		if(mhead.mkeys[j].Res_Field==m&&mhead.mkeys[j].Res_Type==n&&mhead.mkeys[j].Res_Id==id){
			if(mFileType==0)
				return getAddressReal(&mhead.PublicAddress.LIBaddrs[j]);
			else
				return mhead.PublicAddress.GMCaddrs[j];

		}
	}
	return 0;
}*/


/*typedef struct _SRP
{
	int Max;			
	int scriptType[SRPMAX];
	int scriptIndex[SRPMAX];
	int startAddress[SRPMAX];
	int textLen[SRPMAX];
	char text[SRPMAX][1000];
}SRP;

SRP sRP;*/

void InitSRP(HINSTANCE hDLLDrv,char *ingameName)
{

	SRPGetFromFile = (GetFromFile)GetProcAddress(hDLLDrv, "GetFromFile");
	SRPGetSuffix =(GetSuffix)GetProcAddress(hDLLDrv, "SGetSuffix");//获取数据存储文件
	SRPGetText = (GetText)GetProcAddress(hDLLDrv, "GetText");
	SRPSetText = (SetText)GetProcAddress(hDLLDrv, "SetText");
	SRPShowSayBox = (SShowSayBox)GetProcAddress(hDLLDrv, "SShowSayBox");

	SRPGetFromFile(ingameName);
}

/*char * SRPGetSuffix()
{
	return ".srp";
}



void GetFromFile(char * infilename)
{
	char name[200];
	char name1[200];
	memset(name,0,sizeof(name));
	GetModuleFileName(NULL,name1,200); 
	(strrchr(name1, '\\'))[1] = 0;

	char tmpname[255];
	sprintf_s(tmpname,"%s\\Res\\%s.srp",name1,infilename);
	FILE *fp = fopen(tmpname,"r");
	if(!fp)
	{
		return;
	}
	fscanf(fp,"%d",&sRP.Max);
	for (int i = 0 ; i < sRP.Max ; i ++)
	{
		fscanf(fp,"%d %d %d %d %s",&sRP.scriptType[i],&sRP.scriptIndex[i],&sRP.startAddress[i],&sRP.textLen[i],&sRP.text[i]);//stdin为键盘输入  
	}
	fclose(fp);
	fp = NULL;
}




int SRPSetText(char * text,int scriptType,int scriptIndex,int startAddress)
{
		for(int i = 0 ; i < sRP.Max ; i ++)
		{
			if(sRP.scriptType[i] == scriptType && sRP.scriptIndex[i] == scriptIndex && sRP.startAddress[i] == startAddress)
			{
				memset(text,0,sizeof(text));
				int textLen = sRP.textLen[i];
				strcpy(text,sRP.text[i]);
				return textLen;
			}
		}
		return 0;
}*/

