|
- // stormlib.cpp : 定义控制台应用程序的入口点。
- //
- #include <windows.h>
- #include <iostream>
- typedef char * mLPCSTR;
- namespace _MPQ{
- BOOL MPQInitialized = FALSE;
- BOOL (WINAPI * SFileOpenArchive)(mLPCSTR lpFileName, DWORD dwPriority, DWORD dwFlags, HANDLE *hMPQ);
- BOOL (WINAPI * SFileCloseArchive)(HANDLE hMPQ);
- BOOL (WINAPI * SFileGetArchiveName)(HANDLE hMPQ, mLPCSTR lpBuffer, DWORD dwBufferLength);
- BOOL (WINAPI * SFileOpenFile)(mLPCSTR lpFileName, HANDLE *hFile);
- BOOL (WINAPI * SFileOpenFileEx)(HANDLE hMPQ, mLPCSTR lpFileName, DWORD dwSearchScope, HANDLE *hFile);
- BOOL (WINAPI * SFileCloseFile)(HANDLE hFile);
- DWORD (WINAPI * SFileGetFileSize)(HANDLE hFile, LPDWORD lpFileSizeHigh);
- BOOL (WINAPI * SFileGetFileArchive)(HANDLE hFile, HANDLE *hMPQ);
- BOOL (WINAPI * SFileGetFileName)(HANDLE hFile, mLPCSTR lpBuffer, DWORD dwBufferLength);
- DWORD (WINAPI * SFileSetFilePointer)(HANDLE hFile, LONG lDistanceToMove, PLONG lplDistanceToMoveHigh, DWORD dwMoveMethod);
- BOOL (WINAPI * SFileReadFile)(HANDLE hFile,LPVOID lpBuffer,DWORD nNumberOfBytesToRead,LPDWORD lpNumberOfBytesRead,LPOVERLAPPED lpOverlapped);
- LCID (WINAPI * SFileSetLocale)(LCID nNewLocale);
- BOOL (WINAPI * SFileGetBasePath)(mLPCSTR lpBuffer, DWORD dwBufferLength);
- BOOL (WINAPI * SFileSetBasePath)(mLPCSTR lpNewBasePath);
- BOOL (WINAPI * SFileDestroy)();
- void MPQInitialize(void)
- {
- //Load storm.dll and set function
- HMODULE stormdll;
- stormdll = LoadLibraryA("storm.dll");
- (FARPROC&)SFileCloseArchive = GetProcAddress(stormdll, (mLPCSTR)0x0FC);
- (FARPROC&)SFileCloseFile = GetProcAddress(stormdll, (mLPCSTR)0x0FD);
- (FARPROC&)SFileDestroy = GetProcAddress(stormdll, (mLPCSTR)0x106);
- (FARPROC&)SFileGetFileArchive = GetProcAddress(stormdll, (mLPCSTR)0x108);
- (FARPROC&)SFileGetFileSize = GetProcAddress(stormdll, (mLPCSTR)0x109);
- (FARPROC&)SFileOpenArchive = GetProcAddress(stormdll, (mLPCSTR)0x10A);
- (FARPROC&)SFileOpenFile = GetProcAddress(stormdll, (mLPCSTR)0x10B);
- (FARPROC&)SFileOpenFileEx = GetProcAddress(stormdll, (mLPCSTR)0x10C);
- (FARPROC&)SFileReadFile = GetProcAddress(stormdll, (mLPCSTR)0x10D);
- (FARPROC&)SFileSetBasePath = GetProcAddress(stormdll, (mLPCSTR)0x10E);
- (FARPROC&)SFileSetFilePointer = GetProcAddress(stormdll, (mLPCSTR)0x10F);
- (FARPROC&)SFileSetLocale = GetProcAddress(stormdll, (mLPCSTR)0x110);
- (FARPROC&)SFileGetBasePath = GetProcAddress(stormdll, (mLPCSTR)0x111);
- (FARPROC&)SFileGetArchiveName = GetProcAddress(stormdll, (mLPCSTR)0x113);
- (FARPROC&)SFileGetFileName = GetProcAddress(stormdll, (mLPCSTR)0x114);
- /*
- (FARPROC&)SFileSetLastError = GetProcAddress(stormdll, (mLPCSTR)0x1D1);
- (FARPROC&)SFileCompress = GetProcAddress(stormdll, (mLPCSTR)0x227);
- (FARPROC&)SFileDecompress = GetProcAddress(stormdll, (mLPCSTR)0x228);
- */
- SFileDestroy();
- SFileSetLocale(0x656E5553);
- MPQInitialized = TRUE;
- }
- BOOL MPQExtract(mLPCSTR MPQArchive, mLPCSTR MPQFile, mLPCSTR Path, BOOL UseMPQPath)
- {
- //3 method to use
- //MPQExtract("d:\\UCD3.3.scx", "staredit\\scenario.chk", "d:\\", true);
- //MPQExtract("d:\\UCD3.3.scx", "staredit\\scenario.chk", "d:\\", false);
- //MPQExtract("d:\\UCD3.3.scx", "staredit\\scenario.chk", "d:\\scenario.chk", false);
- HANDLE mpq,scenario;
- DWORD filesize,readed;
- BYTE * buffer;
- FILE *f1;
- BOOL usePathName;
- mLPCSTR FileName = 0;
- mLPCSTR FinalPath = 0;
- if('\\' == Path[strlen(Path) - 1])
- {
- usePathName = FALSE;
- FileName = (mLPCSTR)malloc(strlen(MPQFile) + 1);
- if(UseMPQPath)
- strcpy(FileName, MPQFile);
- else
- {
- //getPathFileName(MPQFile, FileName);
- //void getPathFileName(mLPCSTR Path, mLPCSTR out)
- {
- DWORD len = strlen(MPQFile), l = len;
- while(l)
- {
- if('\\' == MPQFile[l])
- break;
- l--;
- }
- memmove(FileName, MPQFile + l + 1, len - l);
- }
- }
- }
- else
- usePathName = TRUE;
- if(!MPQInitialized) MPQInitialize();
- if(!SFileOpenArchive(MPQArchive, 0, 0, &mpq)) return FALSE;
- if(!SFileOpenFileEx(mpq, MPQFile, 0, &scenario))
- {
- SFileCloseArchive(mpq);
- return FALSE;
- }
- filesize = SFileGetFileSize(scenario, &readed);
- buffer = (BYTE*)malloc(filesize);
- if(!buffer)
- {
- SFileCloseFile(scenario);
- SFileCloseArchive(mpq);
- return FALSE;
- }
- if(!SFileReadFile(scenario, buffer, filesize, &readed, NULL))
- {
- SFileCloseFile(scenario);
- SFileCloseArchive(mpq);
- return FALSE;
- }
- SFileCloseFile(scenario);
- SFileCloseArchive(mpq);
-
- if(usePathName)
- f1 = fopen(Path, "wb");
- else
- {
- FinalPath = (mLPCSTR)malloc(strlen(Path) + strlen(FileName) + 1);
- strcpy(FinalPath, Path);
- strcat(FinalPath, FileName);
- //makedir(FinalPath);
- //void makedir(mLPCSTR FinalPath)
- {
- DWORD i = 1;
- DWORD len = strlen(FinalPath);
- mLPCSTR T = (mLPCSTR) malloc(len + 1);
- strcpy(T, FinalPath);
- while(i <= len)
- {
- if('\\' == FinalPath[i-1])
- {
- T[i] = 0;
- if(0xFFFFFFFF == GetFileAttributesA(T))
- CreateDirectoryA(T, NULL);
- T[i] = FinalPath[i];
- }
- i++;
- }
- }
- f1 = fopen(FinalPath, "wb");
- }
- if(!f1) return FALSE;
- fwrite(buffer, filesize, 1, f1);
- fclose(f1);
- free(buffer);
- if(FinalPath) free(FinalPath);
- if(FileName) free(FileName);
- return TRUE;
- }
- DWORD MPQGetFileSize(mLPCSTR MPQ, mLPCSTR MPQFile)
- {
- //get mpq file size
- //Error return 0xFFFFFFFF
- HANDLE mpq,scenario;
- DWORD readed;
- if(!MPQInitialized) MPQInitialize();
- if(!SFileOpenArchive(MPQ, 0, 0, &mpq)) return 0xFFFFFFFF;
- if(!SFileOpenFileEx(mpq, MPQFile, 0, &scenario))
- {
- SFileCloseArchive(mpq);
- return 0xFFFFFFFF;
- }
- return SFileGetFileSize(scenario, &readed);
- }
- BOOL MPQReadFile(mLPCSTR MPQArchive, mLPCSTR MPQFile, void * Buffer, DWORD LengthOfBuffer)
- {
- //This function is used to extract MPQ file to memory
- //buffer must be alloced before use this function
-
- HANDLE mpq,scenario;
- DWORD filesize,readed;
- BYTE * buffer;
- if(!MPQInitialized) MPQInitialize();
- if(!SFileOpenArchive(MPQArchive, 0, 0, &mpq)) return FALSE;
- if(!SFileOpenFileEx(mpq, MPQFile, 0, &scenario))
- {
- SFileCloseArchive(mpq);
- return FALSE;
- }
- filesize = SFileGetFileSize(scenario, &readed);
- buffer = (BYTE *)Buffer;
- if(LengthOfBuffer < filesize || !buffer)
- {
- SFileCloseFile(scenario);
- SFileCloseArchive(mpq);
- return FALSE;
- }
- if(!SFileReadFile(scenario, buffer, filesize, &readed, NULL))
- {
- SFileCloseFile(scenario);
- SFileCloseArchive(mpq);
- return FALSE;
- }
- SFileCloseFile(scenario);
- SFileCloseArchive(mpq);
-
- return TRUE;
- }
- BOOL MPQFileExist(mLPCSTR MPQ, mLPCSTR MPQFile)
- {
- //Exist return TRUE
- //Error or doesn`t exist return FALSE
- HANDLE mpq,scenario;
- DWORD readed;
- if(!MPQInitialized) MPQInitialize();
- if(!SFileOpenArchive(MPQ, 0, 0, &mpq)) return FALSE;
- if(!SFileOpenFileEx(mpq, MPQFile, 0, &scenario))
- {
- SFileCloseArchive(mpq);
- return FALSE;
- }
- return TRUE;
- }
- };
- using namespace _MPQ;
- void main(void)
- {
- MPQExtract("d:\\UCD3.3.scx", "staredit\\scenario.chk", "d:\\", false);
- }
复制代码 |
|