//--------------------------------------------------------------------------- #include #pragma hdrstop #include "Texture.h" #include "TPUtils.hpp" #include "Exception.h" //--------------------------------------------------------------------------- #pragma package(smart_init) //--------------------------------------------------------------------------- // TTexBitData //--------------------------------------------------------------------------- __fastcall TTexBitData::TTexBitData() { hBitmap = NULL; hLiftBitmap = NULL; hDrawBitmap = NULL; Information = 0; } //--------------------------------------------------------------------------- __fastcall TTexBitData::~TTexBitData() { Destroy(); } //--------------------------------------------------------------------------- bool __fastcall TTexBitData::Create(AnsiString n, short w, short h, short s) { Destroy(); strncpy(Name, n.c_str(), TNameLength-1); Width = w; Height = h; if ((hBitmap = GlobalAlloc(GHND, Width * Height)) == NULL) goto fail; if ((pBitmap = (Byte *)GlobalLock(hBitmap)) == NULL) goto fail; if (s>0) { Information = 1; Shaft = s; if ((hLiftBitmap = GlobalAlloc(GHND, Shaft * Height)) == NULL) goto fail; if ((pLiftBitmap = (Byte *)GlobalLock(hLiftBitmap)) == NULL) goto fail; if ((hDrawBitmap = GlobalAlloc(GHND, Width * Shaft)) == NULL) goto fail; if ((pDrawBitmap = (Byte *)GlobalLock(hDrawBitmap)) == NULL) goto fail; } return true; fail: Destroy(); return false; } //--------------------------------------------------------------------------- void __fastcall TTexBitData::Destroy() { if (hBitmap) { if (pBitmap) { GlobalUnlock(hBitmap); pBitmap = NULL; } GlobalFree(hBitmap); hBitmap = NULL; } if (hLiftBitmap) { if (pLiftBitmap) { GlobalUnlock(hLiftBitmap); pLiftBitmap = NULL; } GlobalFree(hLiftBitmap); hLiftBitmap = NULL; } if (hDrawBitmap) { if (pDrawBitmap) { GlobalUnlock(hDrawBitmap); pDrawBitmap = NULL; } GlobalFree(hDrawBitmap); hDrawBitmap = NULL; } Information = 0; } //--------------------------------------------------------------------------- bool __fastcall TTexBitData::Resize(short w, short h, short s) { HGLOBAL center = NULL, lift = NULL, draw = NULL; if (w==Width && h==Height && s==Shaft) return true; if ((center = GlobalAlloc(GHND, w * h)) == NULL) goto fail; if (s>0) { if ((lift = GlobalAlloc(GHND, s * h)) == NULL) goto fail; if ((draw = GlobalAlloc(GHND, w * s)) == NULL) goto fail; } if (hBitmap) { if (pBitmap) GlobalUnlock(hBitmap); GlobalFree(hBitmap); } if (hLiftBitmap) { if (pLiftBitmap) GlobalUnlock(hLiftBitmap); GlobalFree(hLiftBitmap); } if (hDrawBitmap) { if (pDrawBitmap) GlobalUnlock(hDrawBitmap); GlobalFree(hDrawBitmap); } Information = s>0 ? 1 : 0; Width = w; Height = h; hBitmap = center; pBitmap = (Byte *)GlobalLock(hBitmap); Shaft = s; hLiftBitmap = lift; if (hLiftBitmap) pLiftBitmap = (Byte *)GlobalLock(hLiftBitmap); hDrawBitmap = draw; if (hDrawBitmap) pDrawBitmap = (Byte *)GlobalLock(hDrawBitmap); return true; fail: if (center) GlobalFree(center); if (lift) GlobalFree(lift); if (draw) GlobalFree(draw); return false; } //--------------------------------------------------------------------------- TPException __fastcall TTexBitData::LoadFromOldFile(HANDLE fh, AnsiString n) { DWORD dwRead; TPException ec = EC_NONE; Destroy(); strncpy(Name, n.c_str(), TNameLength-1); if (!ReadFile(fh, &Width, 2, &dwRead, NULL)) goto fail; if (!ReadFile(fh, &Height, 2, &dwRead, NULL)) goto fail; Information = 0; if ((hBitmap = GlobalAlloc(GHND, Width * Height)) == NULL) { ec = EC_MEMORY_LACK; goto fail; } if ((pBitmap = (Byte *)GlobalLock(hBitmap)) == NULL) { ec = EC_MEMORY_LACK; goto fail; } if (!ReadFile(fh, pBitmap, Width*Height, &dwRead, NULL)) goto fail; return EC_NONE; fail: Destroy(); if (ec == EC_NONE) ec = EC_FILE_NOT_READ; return ec; } //--------------------------------------------------------------------------- TPException __fastcall TTexBitData::LoadFromFile(HANDLE fh) { DWORD dwRead; TPException ec = EC_NONE; Destroy(); if (!ReadFile(fh, Name, TNameLength, &dwRead, NULL)) goto fail; if (!ReadFile(fh, &Information, 2, &dwRead, NULL)) goto fail; if (!ReadFile(fh, &Width, 2, &dwRead, NULL)) goto fail; if (!ReadFile(fh, &Height, 2, &dwRead, NULL)) goto fail; if ((hBitmap = GlobalAlloc(GHND, Width * Height)) == NULL) { ec = EC_MEMORY_LACK; goto fail; } if ((pBitmap = (Byte *)GlobalLock(hBitmap)) == NULL) { ec = EC_MEMORY_LACK; goto fail; } if (!ReadFile(fh, pBitmap, Width*Height, &dwRead, NULL)) goto fail; if (Information) { if (!ReadFile(fh, &Shaft, 2, &dwRead, NULL)) goto fail; if ((hLiftBitmap = GlobalAlloc(GHND, Shaft * Height)) == NULL) { ec = EC_MEMORY_LACK; goto fail; } if ((pLiftBitmap = (Byte *)GlobalLock(hLiftBitmap)) == NULL) { ec = EC_MEMORY_LACK; goto fail; } if (!ReadFile(fh, pLiftBitmap, Shaft * Height, &dwRead, NULL)) goto fail; if ((hDrawBitmap = GlobalAlloc(GHND, Width * Shaft)) == NULL) { ec = EC_MEMORY_LACK; goto fail; } if ((pDrawBitmap = (Byte *)GlobalLock(hDrawBitmap)) == NULL) { ec = EC_MEMORY_LACK; goto fail; } if (!ReadFile(fh, pDrawBitmap, Width * Shaft, &dwRead, NULL)) goto fail; } return EC_NONE; fail: Destroy(); if (ec == EC_NONE) ec = EC_FILE_NOT_READ; return ec; } //--------------------------------------------------------------------------- TPException __fastcall TTexBitData::SaveToFile(HANDLE fh) { DWORD dwWrite; TPException ec = EC_NONE; if (!WriteFile(fh, Name, TNameLength, &dwWrite, NULL)) goto fail; if (!WriteFile(fh, &Information, 2, &dwWrite, NULL)) goto fail; if (!WriteFile(fh, &Width, 2, &dwWrite, NULL)) goto fail; if (!WriteFile(fh, &Height, 2, &dwWrite, NULL)) goto fail; if (!WriteFile(fh, pBitmap, Width*Height, &dwWrite, NULL)) goto fail; if (Information) { if (!WriteFile(fh, &Shaft, 2, &dwWrite, NULL)) goto fail; if (!WriteFile(fh, pLiftBitmap, Shaft*Height, &dwWrite, NULL)) goto fail; if (!WriteFile(fh, pDrawBitmap, Width*Shaft, &dwWrite, NULL)) goto fail; } return EC_NONE; fail: if (ec == EC_NONE) ec = EC_FILE_NOT_WRITE; return ec; } //--------------------------------------------------------------------------- // TTexture //--------------------------------------------------------------------------- __fastcall TTexture::TTexture() { f[0] = NULL; f[1] = NULL; f[2] = NULL; d[0] = NULL; d[1] = NULL; d[2] = NULL; s = NULL; } //--------------------------------------------------------------------------- __fastcall TTexture::~TTexture() { Destroy(); } //--------------------------------------------------------------------------- bool __fastcall TTexture::Create(AnsiString PathName) { HANDLE hInfo = INVALID_HANDLE_VALUE, hFile = INVALID_HANDLE_VALUE; DWORD dwRead; int i, j, k, ec = EC_NONE; TTexVersion ver; TFound *fd = NULL; TDeriv *dt = NULL; TSpecial *sp = NULL; short cnt, count; AnsiString dir, pts[3] = {"plain", "twill", "satin"}; TTexBitData *ptd = NULL; for (k = 0; k <= 2; k++) { f[k] = new TList; dir = Format("%s\\Found\\%s\\%s.inf", OPENARRAY(TVarRec,(PathName, pts[k], pts[k]))); if ((hInfo = CreateFile(dir.c_str(), GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE) { ec = EC_FILE_NOT_OPEN; goto fail; } if (!ReadFile(hInfo, &cnt, 2, &dwRead, NULL)) goto fail; for (i = 0; i < cnt; i++) { fd = new TFound; if (!ReadFile(hInfo, &fd->size, 2, &dwRead, NULL)) goto fail; fd->data = new TList; if (!ReadFile(hInfo, &count, 2, &dwRead, NULL)) goto fail; dir = Format("%s\\Found\\%s\\%s.%d", OPENARRAY(TVarRec, (PathName, pts[k], pts[k], fd->size))); if ((hFile = CreateFile(dir.c_str(), GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE) { ec = EC_FILE_NOT_OPEN; goto fail; } if (!ReadFile(hFile, &ver, sizeof(TTexVersion), &dwRead, NULL)) goto fail; if ((ver.Texpia == 'P') && (ver.Method == 'T')) { for (j = 0; j < count; j++) { if ((ptd = new TTexBitData) == NULL) { ec = EC_MEMORY_LACK; goto fail; } if ((ec = ptd->LoadFromFile(hFile))!=EC_NONE) goto fail; fd->data->Add(ptd); ptd = NULL; } } else { if (SetFilePointer(hFile, 0, NULL, FILE_BEGIN) == 0xFFFFFFFF) goto fail; for (j = 0; j < count; j++) { if ((ptd = new TTexBitData) == NULL) { ec = EC_MEMORY_LACK; goto fail; } if ((ec = ptd->LoadFromOldFile(hFile, AnsiString(j+1))) != EC_NONE) goto fail; fd->data->Add(ptd); ptd = NULL; } } CloseHandle(hFile); hFile = INVALID_HANDLE_VALUE; f[k]->Add(fd); fd = NULL; } CloseHandle(hInfo); hInfo = INVALID_HANDLE_VALUE; } for (k = 0; k <= 2; k++) { d[k] = new TList; dir = Format("%s\\Deriv\\%s\\%s.inf", OPENARRAY(TVarRec, (PathName, pts[k], pts[k]))); if ((hInfo = CreateFile(dir.c_str(), GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE) { ec = EC_FILE_NOT_OPEN; goto fail; } if (!ReadFile(hInfo, &cnt, 2, &dwRead, NULL)) goto fail; for (i = 0; i < cnt; i++) { dt = new TDeriv; if (!ReadFile(hInfo, dt->name, TNameLength, &dwRead, NULL)) goto fail; dt->data = new TList; if (!ReadFile(hInfo, &count, 2, &dwRead, NULL)) goto fail; dir = Format("%s\\Deriv\\%s\\%s.%d", OPENARRAY(TVarRec, (PathName, pts[k], pts[k], i+1))); if ((hFile = CreateFile(dir.c_str(), GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE) { ec = EC_FILE_NOT_OPEN; goto fail; } if (!ReadFile(hFile, &ver, sizeof(TTexVersion), &dwRead, NULL)) goto fail; if ((ver.Texpia == 'P') && (ver.Method == 'T')) { for (j = 0; j < count; j++) { if ((ptd = new TTexBitData) == NULL) { ec = EC_MEMORY_LACK; goto fail; } if ((ec = ptd->LoadFromFile(hFile))!=EC_NONE) goto fail; dt->data->Add(ptd); ptd = NULL; } } else { if (SetFilePointer(hFile, 0, NULL, FILE_BEGIN) == 0xFFFFFFFF) goto fail; for (j = 0; j < count; j++) { if ((ptd = new TTexBitData) == NULL) { ec = EC_MEMORY_LACK; goto fail; } if ((ec = ptd->LoadFromOldFile(hFile, AnsiString(j+1))) != EC_NONE) goto fail; dt->data->Add(ptd); ptd = NULL; } } CloseHandle(hFile); hFile = INVALID_HANDLE_VALUE; d[k]->Add(dt); dt = NULL; } CloseHandle(hInfo); hInfo = INVALID_HANDLE_VALUE; } s = new TList; dir = Format("%s\\Special\\Special.inf", OPENARRAY(TVarRec, (PathName))); if ((hInfo = CreateFile(dir.c_str(), GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE) { ec = EC_FILE_NOT_OPEN; goto fail; } if (!ReadFile(hInfo, &cnt, 2, &dwRead, NULL)) goto fail; for (i = 0; i < cnt; i++) { sp = new TSpecial; if (!ReadFile(hInfo, sp->name, TNameLength, &dwRead, NULL)) goto fail; sp->data = new TList; if (!ReadFile(hInfo, &count, 2, &dwRead, NULL)) goto fail; dir = Format("%s\\Special\\Special.%d", OPENARRAY(TVarRec, (PathName, i+1))); if ((hFile = CreateFile(dir.c_str(), GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE) { ec = EC_FILE_NOT_OPEN; goto fail; } if (!ReadFile(hFile, &ver, sizeof(TTexVersion), &dwRead, NULL)) goto fail; if ((ver.Texpia == 'P') && (ver.Method == 'T')) { for (j = 0; j < count; j++) { if ((ptd = new TTexBitData) == NULL) { ec = EC_MEMORY_LACK; goto fail; } if ((ec = ptd->LoadFromFile(hFile))!=EC_NONE) goto fail; sp->data->Add(ptd); ptd = NULL; } } else { if (SetFilePointer(hFile, 0, NULL, FILE_BEGIN) == 0xFFFFFFFF) goto fail; for (j = 0; j < count; j++) { if ((ptd = new TTexBitData) == NULL) { ec = EC_MEMORY_LACK; goto fail; } if ((ec = ptd->LoadFromOldFile(hFile, AnsiString(j+1))) != EC_NONE) goto fail; sp->data->Add(ptd); ptd = NULL; } } CloseHandle(hFile); hFile = INVALID_HANDLE_VALUE; s->Add(sp); sp = NULL; } CloseHandle(hInfo); hInfo = INVALID_HANDLE_VALUE; return true; fail: if (fd) delete fd; if (dt) delete dt; if (sp) delete sp; Destroy(); if (ptd) delete ptd; if (hFile != INVALID_HANDLE_VALUE) CloseHandle(hFile); if (hInfo != INVALID_HANDLE_VALUE) CloseHandle(hInfo); if (ec == EC_NONE) ec = EC_FILE_NOT_READ; EXCEPTION_MESSAGE_OK(ec); return false; } //--------------------------------------------------------------------------- void __fastcall TTexture::Destroy() { TFound *fd; TDeriv *dt; TSpecial *sp; TTexBitData *ptd; int k; for (k = 0; k <= 2; k++) { if (f[k]) { while (f[k]->Count>0) { fd = (TFound *) f[k]->Last(); f[k]->Remove(fd); while (fd->data->Count>0) { ptd = (TTexBitData *) fd->data->Last(); fd->data->Remove(ptd); delete ptd; } delete fd->data; delete fd; } delete f[k]; f[k] = NULL; } } for (k = 0; k <= 2; k++) { if (d[k]) { while (d[k]->Count>0) { dt = (TDeriv *) d[k]->Last(); d[k]->Remove(dt); while (dt->data->Count>0) { ptd = (TTexBitData *) dt->data->Last(); dt->data->Remove(ptd); delete ptd; } delete dt->data; delete dt; } delete d[k]; d[k] = NULL; } } if (s) { while (s->Count>0) { sp = (TSpecial *) s->Last(); s->Remove(sp); while (sp->data->Count>0) { ptd = (TTexBitData *) sp->data->Last(); sp->data->Remove(ptd); delete ptd; } delete sp->data; delete sp; } delete s; s = NULL; } } //--------------------------------------------------------------------------- void __fastcall TTexture::NewDirectory(int fds, int k, short cnt, AnsiString PathName, AnsiString n) { HANDLE hFile = INVALID_HANDLE_VALUE; DWORD dwWrite; int offset; short count = 0, wid; TTexVersion ver = { 'P', 'T', 100 }; char name[TNameLength]; AnsiString pts[3] = {"plain", "twill", "satin"}; AnsiString dir; TPException ec = EC_NONE; if (fds == 0) { dir = Format("%s\\Found\\%s\\%s.inf", OPENARRAY(TVarRec, (PathName, pts[k], pts[k]))); if ((hFile = CreateFile(dir.c_str(), GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE) { ec = EC_FILE_NOT_CREATE; goto fail; } if (!WriteFile(hFile, &cnt, 2, &dwWrite, NULL)) goto fail; offset = 4 * (cnt-1); if (SetFilePointer(hFile, offset, NULL, FILE_CURRENT) == 0xFFFFFFFF) goto fail; wid = StrToInt(n); if (!WriteFile(hFile, &wid, 2, &dwWrite, NULL)) goto fail; if (!WriteFile(hFile, &count, 2, &dwWrite, NULL)) goto fail; CloseHandle(hFile); hFile = INVALID_HANDLE_VALUE; dir = Format("%s\\Found\\%s\\%s.%d", OPENARRAY(TVarRec, (PathName, pts[k], pts[k], wid))); } else if (fds == 1) { dir = Format("%s\\Deriv\\%s\\%s.inf", OPENARRAY(TVarRec, (PathName, pts[k], pts[k]))); if ((hFile = CreateFile(dir.c_str(), GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE) { ec = EC_FILE_NOT_CREATE; goto fail; } if (!WriteFile(hFile, &cnt, 2, &dwWrite, NULL)) goto fail; offset = (TNameLength + 2) * (cnt-1); if (SetFilePointer(hFile, offset, NULL, FILE_CURRENT) == 0xFFFFFFFF) goto fail; strncpy(name, n.c_str(), TNameLength-1); if (!WriteFile(hFile, name, TNameLength, &dwWrite, NULL)) goto fail; if (!WriteFile(hFile, &count, 2, &dwWrite, NULL)) goto fail; CloseHandle(hFile); hFile = INVALID_HANDLE_VALUE; dir = Format("%s\\Deriv\\%s\\%s.%d", OPENARRAY(TVarRec, (PathName, pts[k], pts[k], cnt))); } else { dir = Format("%s\\Special\\Special.inf", OPENARRAY(TVarRec, (PathName))); if ((hFile = CreateFile(dir.c_str(), GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE) { ec = EC_FILE_NOT_CREATE; goto fail; } if (!WriteFile(hFile, &cnt, 2, &dwWrite, NULL)) goto fail; offset = (TNameLength + 2) * (cnt-1); if (SetFilePointer(hFile, offset, NULL, FILE_CURRENT) == 0xFFFFFFFF) goto fail; strncpy(name, n.c_str(), TNameLength-1); if (!WriteFile(hFile, name, TNameLength, &dwWrite, NULL)) goto fail; if (!WriteFile(hFile, &count, 2, &dwWrite, NULL)) goto fail; CloseHandle(hFile); hFile = INVALID_HANDLE_VALUE; dir = Format("%s\\Special\\Special.%d", OPENARRAY(TVarRec, (PathName, cnt))); } if ((hFile = CreateFile(dir.c_str(), GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE) { ec = EC_FILE_NOT_CREATE; goto fail; } if (!WriteFile(hFile, &ver, sizeof(TTexVersion), &dwWrite, NULL)) goto fail; CloseHandle(hFile); return; fail: if (hFile != INVALID_HANDLE_VALUE) CloseHandle(hFile); if (ec == EC_NONE) ec = EC_FILE_NOT_WRITE; EXCEPTION_MESSAGE_OK(ec); } //--------------------------------------------------------------------------- void __fastcall TTexture::SaveTexture(int fds, int k, int ext, int index, AnsiString PathName, bool bInfor) { HANDLE hFile = INVALID_HANDLE_VALUE; DWORD dwWrite; int i, offset; short count; TTexVersion ver = {'P', 'T', 100}; AnsiString dir, pts[3] = {"plain", "twill", "satin"}; TFound *fd; TDeriv *dt; TSpecial *sp; TTexBitData *ptd = NULL; TPException ec = EC_NONE; if (fds == 0) { fd = (TFound *) f[k]->Items[index]; if (bInfor) { dir = Format("%s\\Found\\%s\\%s.inf", OPENARRAY(TVarRec, (PathName, pts[k], pts[k]))); if ((hFile = CreateFile(dir.c_str(), GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE) { ec = EC_FILE_NOT_CREATE; goto fail; } offset = 4 + index*4; if (SetFilePointer(hFile, offset, NULL, FILE_BEGIN) == 0xFFFFFFFF) goto fail; count = fd->data->Count; if (!WriteFile(hFile, &count, 2, &dwWrite, NULL)) goto fail; CloseHandle(hFile); hFile = INVALID_HANDLE_VALUE; } dir = Format("%s\\Found\\%s\\%s.%d", OPENARRAY(TVarRec, (PathName, pts[k], pts[k], ext))); if ((hFile = CreateFile(dir.c_str(), GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE) { ec = EC_FILE_NOT_CREATE; goto fail; } if (!WriteFile(hFile, &ver, sizeof(TTexVersion), &dwWrite, NULL)) goto fail; for (i = 0; i < fd->data->Count; i++) { ptd = (TTexBitData *) fd->data->Items[i]; if ((ec = ptd->SaveToFile(hFile)) != EC_NONE) goto fail; } CloseHandle(hFile); } else if (fds == 1) { dt = (TDeriv *) d[k]->Items[index]; if (bInfor) { dir = Format("%s\\Deriv\\%s\\%s.inf", OPENARRAY(TVarRec, (PathName, pts[k], pts[k]))); if ((hFile = CreateFile(dir.c_str(), GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE) { ec = EC_FILE_NOT_CREATE; goto fail; } offset = 24 + index * 24; if (SetFilePointer(hFile, offset, NULL, FILE_BEGIN) == 0xFFFFFFFF) goto fail; count = dt->data->Count; if (!WriteFile(hFile, &count, 2, &dwWrite, NULL)) goto fail; CloseHandle(hFile); hFile = INVALID_HANDLE_VALUE; } dir = Format("%s\\Deriv\\%s\\%s.%d", OPENARRAY(TVarRec, (PathName, pts[k], pts[k], ext))); if ((hFile = CreateFile(dir.c_str(), GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE) { ec = EC_FILE_NOT_CREATE; goto fail; } if (!WriteFile(hFile, &ver, sizeof(TTexVersion), &dwWrite, NULL)) goto fail; for (i = 0; i < dt->data->Count; i++) { ptd = (TTexBitData *) dt->data->Items[i]; if ((ec = ptd->SaveToFile(hFile)) != EC_NONE) goto fail; } CloseHandle(hFile); } else if (fds == 2) { sp = (TSpecial *) s->Items[index]; if (bInfor) { dir = Format("%s\\Special\\Special.inf", OPENARRAY(TVarRec, (PathName))); if ((hFile = CreateFile(dir.c_str(), GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE) { ec = EC_FILE_NOT_CREATE; goto fail; } offset = 24 + index * 24; if (SetFilePointer(hFile, offset, NULL, FILE_BEGIN) == 0xFFFFFFFF) goto fail; count = sp->data->Count; if (!WriteFile(hFile, &count, 2, &dwWrite, NULL)) { ec = EC_FILE_NOT_WRITE; goto fail; } CloseHandle(hFile); hFile = INVALID_HANDLE_VALUE; } dir = Format("%s\\Special\\Special.%d", OPENARRAY(TVarRec, (PathName, ext))); if ((hFile = CreateFile(dir.c_str(), GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE) { ec = EC_FILE_NOT_CREATE; goto fail; } if (!WriteFile(hFile, &ver, sizeof(TTexVersion), &dwWrite, NULL)) goto fail; for (i = 0; i < sp->data->Count; i++) { ptd = (TTexBitData *) sp->data->Items[i]; if ((ec = ptd->SaveToFile(hFile)) != EC_NONE) goto fail; } CloseHandle(hFile); } return; fail: if (hFile != INVALID_HANDLE_VALUE) CloseHandle(hFile); if (ec == EC_NONE) ec = EC_FILE_NOT_WRITE; EXCEPTION_MESSAGE_OK(ec); } //--------------------------------------------------------------------------- void __fastcall TTexture::SaveTexture(int fds, int k, int ext, int index, int n, int over, TTexBitData *bip, AnsiString PathName) { HANDLE hFile = INVALID_HANDLE_VALUE; DWORD dwWrite; int i, offset; short count; TTexVersion ver = {'P', 'T', 100}; AnsiString dir, pts[3] = {"plain", "twill", "satin"}; TFound *fd; TDeriv *dt; TSpecial *sp; TTexBitData *ptd = NULL; TPException ec = EC_NONE; if (fds == 0) { fd = (TFound *) f[k]->Items[index]; if (over) { // 0: overwrite 1: insert dir = Format("%s\\Found\\%s\\%s.inf", OPENARRAY(TVarRec, (PathName, pts[k], pts[k]))); if ((hFile = CreateFile(dir.c_str(), GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE) { ec = EC_FILE_NOT_CREATE; goto fail; } offset = 4 + index*4; if (SetFilePointer(hFile, offset, NULL, FILE_BEGIN) == 0xFFFFFFFF) goto fail; count = fd->data->Count; if (!WriteFile(hFile, &count, 2, &dwWrite, NULL)) goto fail; CloseHandle(hFile); hFile = INVALID_HANDLE_VALUE; } dir = Format("%s\\Found\\%s\\%s.%d", OPENARRAY(TVarRec, (PathName, pts[k], pts[k], ext))); if ((hFile = CreateFile(dir.c_str(), GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE) { ec = EC_FILE_NOT_CREATE; goto fail; } if (!WriteFile(hFile, &ver, sizeof(TTexVersion), &dwWrite, NULL)) goto fail; for (i = 0; i < fd->data->Count; i++) { if (i != n) ptd = (TTexBitData *) fd->data->Items[i]; else ptd = bip; if ((ec = ptd->SaveToFile(hFile)) != EC_NONE) goto fail; } CloseHandle(hFile); } else if (fds == 1) { dt = (TDeriv *) d[k]->Items[index]; if (over) { // 0: overwrite 1: insert dir = Format("%s\\Deriv\\%s\\%s.inf", OPENARRAY(TVarRec, (PathName, pts[k], pts[k]))); if ((hFile = CreateFile(dir.c_str(), GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE) { ec = EC_FILE_NOT_CREATE; goto fail; } offset = 24 + index * 24; if (SetFilePointer(hFile, offset, NULL, FILE_BEGIN) == 0xFFFFFFFF) goto fail; count = dt->data->Count; if (!WriteFile(hFile, &count, 2, &dwWrite, NULL)) goto fail; CloseHandle(hFile); hFile = INVALID_HANDLE_VALUE; } dir = Format("%s\\Deriv\\%s\\%s.%d", OPENARRAY(TVarRec, (PathName, pts[k], pts[k], ext))); if ((hFile = CreateFile(dir.c_str(), GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE) { ec = EC_FILE_NOT_CREATE; goto fail; } if (!WriteFile(hFile, &ver, sizeof(TTexVersion), &dwWrite, NULL)) goto fail; for (i = 0; i < dt->data->Count; i++) { if (i != n) ptd = (TTexBitData *) dt->data->Items[i]; else ptd = bip; if ((ec = ptd->SaveToFile(hFile)) != EC_NONE) goto fail; } CloseHandle(hFile); } else if (fds == 2) { sp = (TSpecial *) s->Items[index]; if (over) { // 0: overwrite 1: insert dir = Format("%s\\Special\\Special.inf", OPENARRAY(TVarRec, (PathName))); if ((hFile = CreateFile(dir.c_str(), GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE) { ec = EC_FILE_NOT_CREATE; goto fail; } offset = 24 + index * 24; if (SetFilePointer(hFile, offset, NULL, FILE_BEGIN) == 0xFFFFFFFF) goto fail; count = sp->data->Count; if (!WriteFile(hFile, &count, 2, &dwWrite, NULL)) goto fail; CloseHandle(hFile); hFile = INVALID_HANDLE_VALUE; } dir = Format("%s\\Special\\Special.%d", OPENARRAY(TVarRec, (PathName, ext))); if ((hFile = CreateFile(dir.c_str(), GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE) { ec = EC_FILE_NOT_CREATE; goto fail; } if (!WriteFile(hFile, &ver, sizeof(TTexVersion), &dwWrite, NULL)) goto fail; for (i = 0; i < sp->data->Count; i++) { if (i != n) ptd = (TTexBitData *) sp->data->Items[i]; else ptd = bip; if ((ec = ptd->SaveToFile(hFile)) != EC_NONE) goto fail; } CloseHandle(hFile); } return; fail: if (hFile != INVALID_HANDLE_VALUE) CloseHandle(hFile); if (ec == EC_NONE) ec = EC_FILE_NOT_WRITE; EXCEPTION_MESSAGE_OK(ec); } //--------------------------------------------------------------------------- void __fastcall TTexture::DeleteTextureGroup(int fds, int k, int ext, int index, AnsiString PathName) { HANDLE hFile = INVALID_HANDLE_VALUE; DWORD dwWrite; int i, kkk; short count; TFound *fd; TDeriv *dt; TSpecial *sp; AnsiString dir, dir2; AnsiString pts[3] = {"plain", "twill", "satin"}; TPException ec = EC_NONE; if (fds == 0) { dir = Format("%s\\Found\\%s\\%s.inf", OPENARRAY(TVarRec, (PathName, pts[k], pts[k]))); if ((hFile = CreateFile(dir.c_str(), GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE) { ec = EC_FILE_NOT_CREATE; goto fail; } if (!WriteFile(hFile, &(f[k]->Count), 2, &dwWrite, NULL)) goto fail; for (i=0; iCount; i++) { fd = (TFound *)f[k]->Items[i]; if (!WriteFile(hFile, &fd->size, 2, &dwWrite, NULL)) goto fail; count = fd->data->Count; if (!WriteFile(hFile, &count, 2, &dwWrite, NULL)) goto fail; } CloseHandle(hFile); dir = Format("%s\\Found\\%s\\%s.%d", OPENARRAY(TVarRec, (PathName, pts[k], pts[k], ext))); DeleteFile(dir); } else if (fds == 1) { dir = Format("%s\\Deriv\\%s\\%s.inf", OPENARRAY(TVarRec, (PathName, pts[k], pts[k]))); if ((hFile = CreateFile(dir.c_str(), GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE) { ec = EC_FILE_NOT_CREATE; goto fail; } if (!WriteFile(hFile, &(d[k]->Count), 2, &dwWrite, NULL)) goto fail; for (i=0; iCount; i++) { dt = (TDeriv *)d[k]->Items[i]; if (!WriteFile(hFile, &dt->name, TNameLength, &dwWrite, NULL)) goto fail; count = dt->data->Count; if (!WriteFile(hFile, &count, 2, &dwWrite, NULL)) goto fail; } CloseHandle(hFile); dir = Format("%s\\Deriv\\%s\\%s.%d", OPENARRAY(TVarRec, (PathName, pts[k], pts[k], ext))); DeleteFile(dir); for (kkk = ext+1; kkk <= d[k]->Count+1; kkk++) { dir = Format("%s\\Deriv\\%s\\%s.%d", OPENARRAY(TVarRec, (PathName, pts[k], pts[k], kkk))); dir2 = Format("%s\\Deriv\\%s\\%s.%d", OPENARRAY(TVarRec, (PathName, pts[k], pts[k], kkk-1))); RenameFile(dir, dir2); } } else if (fds == 2) { dir = Format("%s\\Special\\Special.inf", OPENARRAY(TVarRec, (PathName))); if ((hFile = CreateFile(dir.c_str(), GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE) { ec = EC_FILE_NOT_CREATE; goto fail; } if (!WriteFile(hFile, &(s->Count), 2, &dwWrite, NULL)) goto fail; for (i=0; iCount; i++) { sp = (TSpecial *)s->Items[i]; if (!WriteFile(hFile, &sp->name, TNameLength, &dwWrite, NULL)) goto fail; count = sp->data->Count; if (!WriteFile(hFile, &count, 2, &dwWrite, NULL)) goto fail; } CloseHandle(hFile); dir = Format("%s\\Special\\Special.%d", OPENARRAY(TVarRec, (PathName, ext))); DeleteFile(dir); for (kkk = ext+1; kkk <= s->Count+1; kkk++) { dir = Format("%s\\Special\\Special.%d", OPENARRAY(TVarRec, (PathName, kkk))); dir2 = Format("%s\\Special\\Special.%d", OPENARRAY(TVarRec, (PathName, kkk-1))); RenameFile(dir, dir2); } } return; fail: if (hFile != INVALID_HANDLE_VALUE) CloseHandle(hFile); if (ec == EC_NONE) ec = EC_FILE_NOT_WRITE; EXCEPTION_MESSAGE_OK(ec); } //--------------------------------------------------------------------------- void __fastcall TTexture::RenameTextureGroup(int fds, int k, int index, char *n, AnsiString PathName) { HANDLE hFile = INVALID_HANDLE_VALUE; DWORD dwWrite; int offset; AnsiString dir, pts[3] = {"plain", "twill", "satin"}; TPException ec = EC_NONE; if (fds == 1) { dir = Format("%s\\Deriv\\%s\\%s.inf", OPENARRAY(TVarRec, (PathName, pts[k], pts[k]))); if ((hFile = CreateFile(dir.c_str(), GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE) { ec = EC_FILE_NOT_CREATE; goto fail; } offset = 2 + index*24; if (SetFilePointer(hFile, offset, NULL, FILE_BEGIN) == 0xFFFFFFFF) goto fail; if (!WriteFile(hFile, n, TNameLength, &dwWrite, NULL)) goto fail; CloseHandle(hFile); } else if (fds == 2) { dir = Format("%s\\Special\\Special.inf", OPENARRAY(TVarRec, (PathName))); if ((hFile = CreateFile(dir.c_str(), GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE) { ec = EC_FILE_NOT_CREATE; goto fail; } offset = 2 + index*24; if (SetFilePointer(hFile, offset, NULL, FILE_BEGIN) == 0xFFFFFFFF) goto fail; if (!WriteFile(hFile, n, TNameLength, &dwWrite, NULL)) goto fail; CloseHandle(hFile); } return; fail: if (hFile != INVALID_HANDLE_VALUE) CloseHandle(hFile); if (ec == EC_NONE) ec = EC_FILE_NOT_WRITE; EXCEPTION_MESSAGE_OK(ec); } //---------------------------------------------------------------------------