//--------------------------------------------------------------------------- #include #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(String n, short w, short h, short s) { Destroy(); Name = n; 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, String n) { DWORD dwRead; TPException ec = EC_NONE; Destroy(); Name = n; 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; SAVE_EXCEPTION(ec); goto fail; } if ((pBitmap = (Byte *)GlobalLock(hBitmap)) == NULL) { ec = EC_MEMORY_LACK; SAVE_EXCEPTION(ec); 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::LoadFromOldFile(HANDLE fh) { DWORD dwRead; TPException ec = EC_NONE; Destroy(); char name[TNameLength]; if (!ReadFile(fh, name, TNameLength, &dwRead, NULL)) goto fail; Name = name; 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; SAVE_EXCEPTION(ec); goto fail; } if ((pBitmap = (Byte *)GlobalLock(hBitmap)) == NULL) { ec = EC_MEMORY_LACK; SAVE_EXCEPTION(ec); 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; SAVE_EXCEPTION(ec); goto fail; } if ((pLiftBitmap = (Byte *)GlobalLock(hLiftBitmap)) == NULL) { ec = EC_MEMORY_LACK; SAVE_EXCEPTION(ec); goto fail; } if (!ReadFile(fh, pLiftBitmap, Shaft * Height, &dwRead, NULL)) goto fail; if ((hDrawBitmap = GlobalAlloc(GHND, Width * Shaft)) == NULL) { ec = EC_MEMORY_LACK; SAVE_EXCEPTION(ec); goto fail; } if ((pDrawBitmap = (Byte *)GlobalLock(hDrawBitmap)) == NULL) { ec = EC_MEMORY_LACK; SAVE_EXCEPTION(ec); 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::LoadFromFile(HANDLE fh) { DWORD dwRead; Destroy(); try { int length = 0; if (!ReadFile(fh, &length, sizeof(int), &dwRead, NULL)) throw EC_FILE_NOT_READ; Name = String(L"", length); if (!ReadFile(fh, Name.c_str(), sizeof(Char) * length, &dwRead, NULL)) throw EC_FILE_NOT_READ; if (!ReadFile(fh, &Information, sizeof(short), &dwRead, NULL)) throw EC_FILE_NOT_READ; if (!ReadFile(fh, &Width, sizeof(short), &dwRead, NULL)) throw EC_FILE_NOT_READ; if (!ReadFile(fh, &Height, sizeof(short), &dwRead, NULL)) throw EC_FILE_NOT_READ; if ((hBitmap = GlobalAlloc(GHND, Width * Height)) == NULL) throw EC_MEMORY_LACK; if ((pBitmap = (Byte *)GlobalLock(hBitmap)) == NULL) throw EC_MEMORY_LACK; if (!ReadFile(fh, pBitmap, Width*Height, &dwRead, NULL)) throw EC_FILE_NOT_READ; if (Information) { if (!ReadFile(fh, &Shaft, sizeof(short), &dwRead, NULL)) throw EC_FILE_NOT_READ; if ((hLiftBitmap = GlobalAlloc(GHND, Shaft * Height)) == NULL) throw EC_MEMORY_LACK; if ((pLiftBitmap = (Byte *)GlobalLock(hLiftBitmap)) == NULL) throw EC_MEMORY_LACK; if (!ReadFile(fh, pLiftBitmap, Shaft * Height, &dwRead, NULL)) throw EC_FILE_NOT_READ; if ((hDrawBitmap = GlobalAlloc(GHND, Width * Shaft)) == NULL) throw EC_MEMORY_LACK; if ((pDrawBitmap = (Byte *)GlobalLock(hDrawBitmap)) == NULL) throw EC_MEMORY_LACK; if (!ReadFile(fh, pDrawBitmap, Width * Shaft, &dwRead, NULL)) throw EC_FILE_NOT_READ; } return EC_NONE; } catch (TPException ec) { Destroy(); SAVE_EXCEPTION(ec); return ec; } } //--------------------------------------------------------------------------- TPException __fastcall TTexBitData::SaveToFile(HANDLE fh) { DWORD dwWrite; TPException ec = EC_NONE; int length = Name.Length(); if (!WriteFile(fh, &length, sizeof(int), &dwWrite, NULL)) goto fail; if (!WriteFile(fh, Name.c_str(), sizeof(Char) * length, &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::CheckOldTextureFile(String PathName) { HANDLE hInfo = INVALID_HANDLE_VALUE, hFile = INVALID_HANDLE_VALUE; DWORD dwRead; int i, j, k; TPException ec = EC_NONE; TTexVersion ver; short cnt, count; String dir, path, backupPath, pts[3] = {L"plain", L"twill", L"satin"}; TTexBitData *ptd = NULL; TTextureItem *aTextureItem = NULL; for (k = 0; k <= 2; k++) { dir = Format("%s\\Found\\%s\\%s.inf", OPENARRAY(TVarRec,(PathName, pts[k], pts[k]))); if (FileExists(dir) == false) continue; backupPath = ExtractFileDir(dir) + "\\OldBackup\\"; ForceDirectories(backupPath); f[k] = new TList; if ((hInfo = CreateFile(dir.c_str(), GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE) { ec = EC_FILE_NOT_OPEN; SAVE_EXCEPTION(ec); goto fail; } if (!ReadFile(hInfo, &cnt, 2, &dwRead, NULL)) goto fail; for (i = 0; i < cnt; i++) { aTextureItem = new TTextureItem(); short size = 0; if (!ReadFile(hInfo, &size, sizeof(short), &dwRead, NULL)) goto fail; aTextureItem->name = IntToStr(size); if (!ReadFile(hInfo, &count, 2, &dwRead, NULL)) goto fail; path = Format("%s\\Found\\%s\\%s.%d", OPENARRAY(TVarRec, (PathName, pts[k], pts[k], aTextureItem->size))); if (FileExists(path) == false) continue; aTextureItem->FilePath = path + TEXTURE_EXT; if ((hFile = CreateFile(path.c_str(), GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE) { ec = EC_FILE_NOT_OPEN; SAVE_EXCEPTION(ec); 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; SAVE_EXCEPTION(ec); goto fail; } if ((ec = ptd->LoadFromOldFile(hFile))!=EC_NONE) goto fail; aTextureItem->data->Add(ptd); } } 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; SAVE_EXCEPTION(ec); goto fail; } if ((ec = ptd->LoadFromOldFile(hFile, String(j+1))) != EC_NONE) goto fail; aTextureItem->data->Add(ptd); } } CloseHandle(hFile); hFile = INVALID_HANDLE_VALUE; f[k]->Add(aTextureItem); SaveTexture(TAFoundation, k, i); MoveFile(path.c_str(), (backupPath + ExtractFileName(path)).c_str()); } CloseHandle(hInfo); hInfo = INVALID_HANDLE_VALUE; MoveFile(dir.c_str(), (backupPath + ExtractFileName(dir)).c_str()); } for (k = 0; k <= 2; k++) { dir = Format("%s\\Deriv\\%s\\%s.inf", OPENARRAY(TVarRec, (PathName, pts[k], pts[k]))); if (FileExists(dir) == false) continue; backupPath = ExtractFileDir(dir) + "\\OldBackup\\"; ForceDirectories(backupPath); d[k] = new TList; if ((hInfo = CreateFile(dir.c_str(), GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE) { ec = EC_FILE_NOT_OPEN; SAVE_EXCEPTION(ec); goto fail; } if (!ReadFile(hInfo, &cnt, 2, &dwRead, NULL)) goto fail; for (i = 0; i < cnt; i++) { TTextureItem *aTextureItem = new TTextureItem(); char name[TNameLength]; if (!ReadFile(hInfo, name, TNameLength, &dwRead, NULL)) goto fail; aTextureItem->name = name; if (!ReadFile(hInfo, &count, 2, &dwRead, NULL)) goto fail; path = Format("%s\\Deriv\\%s\\%s.%d", OPENARRAY(TVarRec, (PathName, pts[k], pts[k], i+1))); if (FileExists(path) == false) continue; aTextureItem->FilePath = path + TEXTURE_EXT; if ((hFile = CreateFile(path.c_str(), GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE) { ec = EC_FILE_NOT_OPEN; SAVE_EXCEPTION(ec); 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; SAVE_EXCEPTION(ec); goto fail; } if ((ec = ptd->LoadFromOldFile(hFile))!=EC_NONE) goto fail; aTextureItem->data->Add(ptd); } } 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; SAVE_EXCEPTION(ec); goto fail; } if ((ec = ptd->LoadFromOldFile(hFile, String(j+1))) != EC_NONE) goto fail; aTextureItem->data->Add(ptd); } } CloseHandle(hFile); hFile = INVALID_HANDLE_VALUE; d[k]->Add(aTextureItem); SaveTexture(TADerivation, k, i); MoveFile(path.c_str(), (backupPath + ExtractFileName(path)).c_str()); } CloseHandle(hInfo); hInfo = INVALID_HANDLE_VALUE; MoveFile(dir.c_str(), (backupPath + ExtractFileName(dir)).c_str()); } dir = Format("%s\\Special\\Special.inf", OPENARRAY(TVarRec, (PathName))); if (FileExists(dir) == false) return true; backupPath = ExtractFileDir(dir) + "\\OldBackup\\"; ForceDirectories(backupPath); s = new TList; if ((hInfo = CreateFile(dir.c_str(), GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE) { ec = EC_FILE_NOT_OPEN; SAVE_EXCEPTION(ec); goto fail; } if (!ReadFile(hInfo, &cnt, 2, &dwRead, NULL)) goto fail; for (i = 0; i < cnt; i++) { TTextureItem *aTextureItem = new TTextureItem(); char name[TNameLength]; if (!ReadFile(hInfo, name, TNameLength, &dwRead, NULL)) goto fail; aTextureItem->name = name; if (!ReadFile(hInfo, &count, 2, &dwRead, NULL)) goto fail; path = Format("%s\\Special\\Special.%d", OPENARRAY(TVarRec, (PathName, i+1))); if (FileExists(path) == false) continue; aTextureItem->FilePath = path + TEXTURE_EXT; if ((hFile = CreateFile(path.c_str(), GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE) { ec = EC_FILE_NOT_OPEN; SAVE_EXCEPTION(ec); 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; SAVE_EXCEPTION(ec); goto fail; } if ((ec = ptd->LoadFromOldFile(hFile))!=EC_NONE) goto fail; aTextureItem->data->Add(ptd); } } 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; SAVE_EXCEPTION(ec); goto fail; } if ((ec = ptd->LoadFromOldFile(hFile, String(j+1))) != EC_NONE) goto fail; aTextureItem->data->Add(ptd); } } CloseHandle(hFile); hFile = INVALID_HANDLE_VALUE; s->Add(aTextureItem); SaveTexture(TASpecial, 0, i); MoveFile(path.c_str(), (backupPath + ExtractFileName(path)).c_str()); } CloseHandle(hInfo); hInfo = INVALID_HANDLE_VALUE; MoveFile(dir.c_str(), (backupPath + ExtractFileName(dir)).c_str()); return true; fail: 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; } //--------------------------------------------------------------------------- bool __fastcall TTexture::Create(String PathName) { CheckOldTextureFile(PathName); Destroy(); // groupIdx : ±âº» Á¶Á÷(Foundation), ÀÀ¿ë Á¶Á÷(Derivation), Ư¼ö Á¶Á÷(Sepciality) for (int groupIdx = 0; groupIdx < 3; groupIdx++) { int typeCnt; if (groupIdx == TASpecial) typeCnt = 1; // Ư¼ö Á÷¹°(Special Texture) else typeCnt = 3; // ÆòÁ÷(Plain), ´ÉÁ÷(Twill), ¼öÀÚÁ÷(Satin) // typeIdx : "ÆòÁ÷(Plain), ´ÉÁ÷(Twill), ¼öÀÚÁ÷(Satin)" or "Ư¼ö Á÷¹°(Special Texture)" for (int typeIdx = 0; typeIdx < typeCnt; typeIdx++) { TList **dstTextureItemList = NULL; if (groupIdx == TAFoundation) dstTextureItemList = &(f[typeIdx]); else if (groupIdx == TADerivation) dstTextureItemList = &(d[typeIdx]); else if (groupIdx == TASpecial) dstTextureItemList = &s; *dstTextureItemList = new TList(); String filePath = PathName + L"\\" + TEXTURE_GROUP_NAME[groupIdx] + L"\\"; if (groupIdx == TAFoundation || groupIdx == TADerivation) filePath += TEXTURE_TYPE_NAME[typeIdx] + L"\\"; String fileName = filePath + L"*" + TEXTURE_EXT; TSearchRec sr; int iAttributes = faAnyFile; if (FindFirst(fileName, iAttributes, sr) == 0) { do { if ((sr.Attr & iAttributes) == sr.Attr && sr.Name != "." && sr.Name != "..") { TTextureItem *aTextureItem = new TTextureItem(); if (aTextureItem->LoadFromFile(filePath + sr.Name) == true) (*dstTextureItemList)->Add(aTextureItem); else delete aTextureItem; } } while (FindNext(sr) == 0); FindClose(sr); } } } return true; } //--------------------------------------------------------------------------- void __fastcall TTexture::Destroy() { // groupIdx : ±âº» Á¶Á÷(Foundation), ÀÀ¿ë Á¶Á÷(Derivation), Ư¼ö Á¶Á÷(Sepciality) for (int groupIdx = 0; groupIdx < 3; groupIdx++) { int typeCnt; if (groupIdx == TASpecial) typeCnt = 1; // Ư¼ö Á÷¹°(Special Texture) else typeCnt = 3; // ÆòÁ÷(Plain), ´ÉÁ÷(Twill), ¼öÀÚÁ÷(Satin) // typeIdx : "ÆòÁ÷(Plain), ´ÉÁ÷(Twill), ¼öÀÚÁ÷(Satin)" or "Ư¼ö Á÷¹°(Special Texture)" for (int typeIdx = 0; typeIdx < typeCnt; typeIdx++) { TList **dstTextureItemList = NULL; if (groupIdx == TAFoundation) dstTextureItemList = &(f[typeIdx]); else if (groupIdx == TADerivation) dstTextureItemList = &(d[typeIdx]); else if (groupIdx == TASpecial) dstTextureItemList = &s; if (*dstTextureItemList != NULL) { for (int idx = 0; idx < (*dstTextureItemList)->Count; idx++) { TTextureItem *aTextureItem = static_cast((*dstTextureItemList)->Items[idx]); delete aTextureItem; } (*dstTextureItemList)->Clear(); delete *dstTextureItemList; *dstTextureItemList = NULL; } } } } //--------------------------------------------------------------------------- void __fastcall TTexture::NewDirectory(int groupIdx, int typeIdx, String rootPath, String name) { TList *dstTextureItemList = NULL; if (groupIdx == TAFoundation) dstTextureItemList = f[typeIdx]; else if (groupIdx == TADerivation) dstTextureItemList = d[typeIdx]; else if (groupIdx == TASpecial) dstTextureItemList = s; String filePath = rootPath + "\\" + TEXTURE_GROUP_NAME[groupIdx] + "\\"; if (groupIdx == TAFoundation || groupIdx == TADerivation) filePath += (TEXTURE_TYPE_NAME[typeIdx] + "\\"); for (int i = 0; i <= dstTextureItemList->Count; i++) { String tempPath; if (groupIdx == TAFoundation || groupIdx == TADerivation) tempPath = filePath + TEXTURE_GROUP_NAME[groupIdx] + "_" + TEXTURE_TYPE_NAME[typeIdx] + "_" + IntToStr(i) + TEXTURE_EXT; else tempPath = filePath + TEXTURE_GROUP_NAME[groupIdx] + "_" + IntToStr(i) + TEXTURE_EXT; if (FileExists(tempPath) == false) { filePath = tempPath; break; } } TTextureItem *aTextureItem = new TTextureItem(filePath, name); dstTextureItemList->Add(aTextureItem); aTextureItem->SaveToFile(); } //--------------------------------------------------------------------------- void __fastcall TTexture::SaveTexture(int groupIdx, int typeIdx, int index, TTexBitData *bip) { TList *dstTextureItemList = NULL; if (groupIdx == TAFoundation) dstTextureItemList = f[typeIdx]; else if (groupIdx == TADerivation) dstTextureItemList = d[typeIdx]; else if (groupIdx == TASpecial) dstTextureItemList = s; TTextureItem *dstTextureItem = static_cast(dstTextureItemList->Items[index]); if (bip != NULL) dstTextureItem->data->Add(bip); dstTextureItem->SaveToFile(); } //--------------------------------------------------------------------------- void __fastcall TTexture::DeleteTextureGroup(int groupIdx, int typeIdx, int index) { TList *dstTextureItemList = NULL; if (groupIdx == TAFoundation) dstTextureItemList = f[typeIdx]; else if (groupIdx == TADerivation) dstTextureItemList = d[typeIdx]; else if (groupIdx == TASpecial) dstTextureItemList = s; TTextureItem *dstTextureItem = static_cast(dstTextureItemList->Items[index]); dstTextureItemList->Remove(dstTextureItem); dstTextureItem->DeleteTextureItemFile(); delete dstTextureItem; } //--------------------------------------------------------------------------- void __fastcall TTexture::DeleteTexture(int groupIdx, int typeIdx, int index, int textureIdx) { TList *dstTextureItemList = NULL; if (groupIdx == TAFoundation) dstTextureItemList = f[typeIdx]; else if (groupIdx == TADerivation) dstTextureItemList = d[typeIdx]; else if (groupIdx == TASpecial) dstTextureItemList = s; TTextureItem *dstTextureItem = static_cast(dstTextureItemList->Items[index]); if (dstTextureItem != NULL) { TTexBitData *aTexBitData = static_cast(dstTextureItem->data->Items[textureIdx]); dstTextureItem->data->Remove(aTexBitData); delete aTexBitData; dstTextureItem->SaveToFile(); } } //--------------------------------------------------------------------------- bool __fastcall TTexture::RenameTextureGroup(int groupIdx, int typeIdx, int index, String newName) { TList *dstTextureItemList = NULL; if (groupIdx == TAFoundation) dstTextureItemList = f[typeIdx]; else if (groupIdx == TADerivation) dstTextureItemList = d[typeIdx]; else if (groupIdx == TASpecial) dstTextureItemList = s; for (int i = 0; i < dstTextureItemList->Count; i++) { TTextureItem *aTextureItem = static_cast(dstTextureItemList->Items[i]); if (aTextureItem->name == newName) { MessageDlg(newName + " " + String(IDS_MESSAGE_DIREXIST), mtWarning, TMsgDlgButtons() << mbOK, 0); return false; } } TTextureItem *dstTextureItem = static_cast(dstTextureItemList->Items[index]); dstTextureItem->name = newName; dstTextureItem->SaveToFile(); return true; } //--------------------------------------------------------------------------- bool __fastcall TTexture::RenameTexture(int groupIdx, int typeIdx, int index, int textureIdx, String newName) { TList *dstTextureItemList = NULL; if (groupIdx == TAFoundation) dstTextureItemList = f[typeIdx]; else if (groupIdx == TADerivation) dstTextureItemList = d[typeIdx]; else if (groupIdx == TASpecial) dstTextureItemList = s; TTextureItem *dstTextureItem = static_cast(dstTextureItemList->Items[index]); for (int i = 0; i < dstTextureItem->data->Count; i++) { TTexBitData *aTexBitData = static_cast(dstTextureItem->data->Items[i]); if (aTexBitData->Name == newName) { MessageDlg(IDS_MESSAGE_FILEEXIST, mtWarning, TMsgDlgButtons()<(dstTextureItem->data->Items[textureIdx]); dstTexBitData->Name = newName; dstTextureItem->SaveToFile(); return true; } //--------------------------------------------------------------------------- //////////////////////////////////////////////////////////////////////////////////////////////////// // Class TTextureItem //////////////////////////////////////////////////////////////////////////////////////////////////// __fastcall TTextureItem::TTextureItem() { m_FilePath = L""; m_Size = 0; m_Data = new TList(); } //--------------------------------------------------------------------------- __fastcall TTextureItem::TTextureItem(String filePath, String name) { m_FilePath = filePath; m_Name = name; m_Size = 0; m_Data = new TList(); } //--------------------------------------------------------------------------- __fastcall TTextureItem::~TTextureItem() { Destroy(); } //--------------------------------------------------------------------------- void __fastcall TTextureItem::Destroy() { if (m_Data != NULL) { for (int idx = 0; idx < m_Data->Count; idx++) { TTexBitData *aTexBitData = static_cast(m_Data->Items[idx]); delete aTexBitData; } m_Data->Clear(); delete m_Data; m_Data = NULL; } } //--------------------------------------------------------------------------- int __fastcall TTextureItem::GetSize() { String sizeStr = m_Name; int size = 0; if (TryStrToInt(sizeStr, size) == true) return size; else return -1; } //--------------------------------------------------------------------------- bool __fastcall TTextureItem::LoadFromFile(String path) { Destroy(); HANDLE hFile = INVALID_HANDLE_VALUE; DWORD dwRead; TTexBitData *aTexBitData = NULL; try { hFile = CreateFile(path.c_str(), GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hFile == INVALID_HANDLE_VALUE) { throw EC_FILE_NOT_OPEN; } m_FilePath = path; // Read name int length = 0; if (!ReadFile(hFile, &length, sizeof(int), &dwRead, NULL)) throw EC_FILE_NOT_READ; m_Name = String(L"", length); if (!ReadFile(hFile, m_Name.c_str(), sizeof(Char) * length, &dwRead, NULL)) throw EC_FILE_NOT_READ; // Read data int textureCnt = 0; if (!ReadFile(hFile, &textureCnt, sizeof(int), &dwRead, NULL)) throw EC_FILE_NOT_READ; if (m_Data == NULL) m_Data = new TList(); for (int textureIdx = 0; textureIdx < textureCnt; textureIdx++) { aTexBitData = new TTexBitData(); if (aTexBitData == NULL) throw EC_MEMORY_LACK; if (aTexBitData->LoadFromFile(hFile) != EC_NONE) throw EC_FILE_NOT_READ; m_Data->Add(aTexBitData); } CloseHandle(hFile); return true; } catch (TPException ec) { if (hFile != INVALID_HANDLE_VALUE) CloseHandle(hFile); if (aTexBitData != NULL) delete aTexBitData; SAVE_EXCEPTION(ec); EXCEPTION_MESSAGE_OK(ec); return false; } } //--------------------------------------------------------------------------- void __fastcall TTextureItem::SaveToFile() { HANDLE hFile = INVALID_HANDLE_VALUE; DWORD dwWrite; try { hFile = CreateFile(m_FilePath.c_str(), GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if (hFile == INVALID_HANDLE_VALUE) throw EC_FILE_NOT_CREATE; // Write name int length = m_Name.Length(); if (!WriteFile(hFile, &length, sizeof(int), &dwWrite, NULL)) throw EC_FILE_NOT_WRITE; if (!WriteFile(hFile, m_Name.c_str(), sizeof(Char) * length, &dwWrite, NULL)) throw EC_FILE_NOT_WRITE; // Write data if (m_Data != NULL) { int textureCnt = m_Data->Count; if (!WriteFile(hFile, &textureCnt, sizeof(int), &dwWrite, NULL)) throw EC_FILE_NOT_WRITE; for (int textureIdx = 0; textureIdx < textureCnt; textureIdx++) { TTexBitData *aTexBitData = static_cast(m_Data->Items[textureIdx]); if (aTexBitData->SaveToFile(hFile) != NULL) throw EC_FILE_NOT_WRITE; } } CloseHandle(hFile); } catch (TPException ec) { if (hFile != INVALID_HANDLE_VALUE) { CloseHandle(hFile); DeleteFile(m_FilePath.c_str()); } SAVE_EXCEPTION(ec); EXCEPTION_MESSAGE_OK(ec); } } //--------------------------------------------------------------------------- void __fastcall TTextureItem::DeleteTextureItemFile() { DeleteFile(m_FilePath); } //--------------------------------------------------------------------------- ////////////////////////////////////////////////////////////////////////////////////////////////////