From 9cf539eb525be0a546905aa1020aeae0d52c4ad8 Mon Sep 17 00:00:00 2001 From: elasota Date: Sun, 29 Dec 2019 22:12:11 -0500 Subject: [PATCH] Refactoring --- GpApp/About.cpp | 4 +- GpApp/AnimCursor.cpp | 35 +------- GpApp/House.cpp | 7 +- GpApp/HouseIO.cpp | 12 ++- GpApp/Map.cpp | 2 +- GpApp/Music.cpp | 4 +- GpApp/ObjectDraw2.cpp | 6 +- GpApp/Play.cpp | 4 +- GpApp/Room.cpp | 4 +- GpApp/RoomGraphics.cpp | 2 +- GpApp/RoomInfo.cpp | 14 +-- GpApp/SelectHouse.cpp | 3 +- GpApp/Sound.cpp | 8 +- GpApp/StructuresInit.cpp | 2 +- GpApp/StructuresInit2.cpp | 2 +- GpApp/Utilities.cpp | 6 +- PortabilityLayer/DisplayDeviceManager.cpp | 2 +- PortabilityLayer/MemoryManager.cpp | 8 +- PortabilityLayer/MenuManager.cpp | 4 +- PortabilityLayer/PLErrorCodes.h | 2 + PortabilityLayer/PLMenus.cpp | 2 +- PortabilityLayer/PLQDraw.cpp | 2 +- PortabilityLayer/PLQDraw.h | 11 +-- PortabilityLayer/PLResourceManager.cpp | 82 ++++++++++++++++- PortabilityLayer/PLResources.cpp | 24 +---- PortabilityLayer/PLResources.h | 13 +-- PortabilityLayer/ResourceFile.cpp | 6 ++ PortabilityLayer/ResourceFile.h | 103 +++++++++++----------- PortabilityLayer/ResourceManager.h | 6 ++ 29 files changed, 203 insertions(+), 177 deletions(-) diff --git a/GpApp/About.cpp b/GpApp/About.cpp index d8bd1f1..a5b4158 100644 --- a/GpApp/About.cpp +++ b/GpApp/About.cpp @@ -109,7 +109,7 @@ static void HiLiteOkayButton (void) if (thePict != nil) { DrawPicture(thePict, &okayButtonBounds); - ReleaseResource((Handle)thePict); + DisposeHandle((Handle)thePict); okayButtIsHiLit = true; } @@ -131,7 +131,7 @@ static void UnHiLiteOkayButton (void) if (thePict != nil) { DrawPicture(thePict, &okayButtonBounds); - ReleaseResource((Handle)thePict); + DisposeHandle((Handle)thePict); okayButtIsHiLit = false; } diff --git a/GpApp/AnimCursor.cpp b/GpApp/AnimCursor.cpp index a638c08..a29084d 100644 --- a/GpApp/AnimCursor.cpp +++ b/GpApp/AnimCursor.cpp @@ -36,7 +36,6 @@ typedef struct } compiledAcurRec, *compiledAcurPtr, **compiledAcurHandle; -Boolean GetMonoCursors (acurHandle, compiledAcurHandle); Boolean GetColorCursors (acurHandle, compiledAcurHandle); void InitAnimatedCursor (acurHandle); @@ -47,36 +46,6 @@ Boolean useColorCursor = false; //============================================================== Functions -//-------------------------------------------------------------- GetMonoCursors - -// Loads b&w cursors (for animated beach ball). - -Boolean GetMonoCursors (acurHandle ballCursH, compiledAcurHandle compiledBallCursH) -{ - short i, j; - CursHandle cursHdl; - - if (ballCursH) // Were we passed a legit acur handle? - { - j = (*ballCursH)->n; // Get number of 'frames' in the acur - for (i = 0; i < j; i++) // Start walking the frames - { - cursHdl = GetCursor((*ballCursH)->frame[i].resID); - if (cursHdl == nil) // Did the cursor load? It didn't?... - { // Well then, toss what we got. - for (j = 0; j < i; j++) - DisposeHandle((*compiledBallCursH)->frame[j].cursorHdl); - return(false); // And report this to mother. - } // However!... - else // If cursor loaded ok... - { // Detach it from the resource map... - DetachResource((Handle)cursHdl); // And assign to our struct - (*compiledBallCursH)->frame[i].cursorHdl = (Handle)cursHdl; - } - } - } - return(true); -} //-------------------------------------------------------------- GetColorCursors @@ -133,8 +102,6 @@ void InitAnimatedCursor (acurHandle ballCursH) if (useColor) useColor = GetColorCursors(ballCursH, compiledBallCursorH); - if (!useColor && !GetMonoCursors(ballCursH, compiledBallCursorH)) - RedAlert(kErrFailedResourceLoad); DisposCursors(); animCursorH = ballCursH; @@ -189,7 +156,7 @@ void DisposCursors (void) if (animCursorH != nil) { - ReleaseResource((Handle)animCursorH); + DisposeHandle((Handle)animCursorH); animCursorH = nil; } } diff --git a/GpApp/House.cpp b/GpApp/House.cpp index a458bb0..37b697c 100644 --- a/GpApp/House.cpp +++ b/GpApp/House.cpp @@ -93,9 +93,10 @@ Boolean CreateNewHouse (void) theErr = fm->CreateFileAtCurrentTime(theSpec.m_dir, theSpec.m_name, 'ozm5', 'gliH'); if (!CheckFileError(theErr, PSTR("New House"))) return (false); - HCreateResFile(theSpec.m_dir, theSpec.m_name); - if (ResError() != PLErrors::kNone) - YellowAlert(kYellowFailedResCreate, ResError()); + + theErr = HCreateResFile(theSpec.m_dir, theSpec.m_name); + if (theErr != PLErrors::kNone) + YellowAlert(kYellowFailedResCreate, theErr); PasStringCopy(theSpec.m_name, thisHouseName); AddExtraHouse(theSpec); diff --git a/GpApp/HouseIO.cpp b/GpApp/HouseIO.cpp index 83f6b59..414c194 100644 --- a/GpApp/HouseIO.cpp +++ b/GpApp/HouseIO.cpp @@ -17,6 +17,7 @@ #include "House.h" #include "IOStream.h" #include "ObjectEdit.h" +#include "ResourceManager.h" #define kSaveChangesAlert 1002 @@ -866,11 +867,12 @@ Boolean CloseHouse (void) void OpenHouseResFork (void) { + PortabilityLayer::ResourceManager *rm = PortabilityLayer::ResourceManager::GetInstance(); if (houseResFork == -1) { - houseResFork = FSpOpenResFile(theHousesSpecs[thisHouseIndex], fsCurPerm); - if (houseResFork == -1) - YellowAlert(kYellowFailedResOpen, ResError()); + houseResFork = rm->OpenResFork(theHousesSpecs[thisHouseIndex].m_dir, theHousesSpecs[thisHouseIndex].m_name); + if (houseResFork < 0) + YellowAlert(kYellowFailedResOpen, PLErrors::kResourceError); else UseResFile(houseResFork); } @@ -883,7 +885,9 @@ void CloseHouseResFork (void) { if (houseResFork != -1) { - CloseResFile(houseResFork); + PortabilityLayer::ResourceManager *rm = PortabilityLayer::ResourceManager::GetInstance(); + + rm->CloseResFile(houseResFork); houseResFork = -1; } } diff --git a/GpApp/Map.cpp b/GpApp/Map.cpp index a0e304e..b228769 100644 --- a/GpApp/Map.cpp +++ b/GpApp/Map.cpp @@ -172,7 +172,7 @@ void LoadGraphicPlus (short resID, Rect *theRect) } } DrawPicture(thePicture, theRect); - ReleaseResource((Handle)thePicture); + DisposeHandle((Handle)thePicture); } //-------------------------------------------------------------- RedrawMapContents diff --git a/GpApp/Music.cpp b/GpApp/Music.cpp index e1d49c0..782b6dc 100644 --- a/GpApp/Music.cpp +++ b/GpApp/Music.cpp @@ -261,7 +261,7 @@ PLError_t LoadMusicSounds (void) return PLErrors::kOutOfMemory; BlockMove((Ptr)(static_cast(*theSound) + 20L), theMusicData[i], soundDataSize); - ReleaseResource(theSound); + DisposeHandle(theSound); } return (theErr); } @@ -418,7 +418,7 @@ long MusicBytesNeeded (void) if (theSound == nil) { SetResLoad(true); - return ((long)ResError()); + return -1; } totalBytes += GetMaxResourceSize(theSound); // ReleaseResource(theSound); diff --git a/GpApp/ObjectDraw2.cpp b/GpApp/ObjectDraw2.cpp index b49195b..4386888 100644 --- a/GpApp/ObjectDraw2.cpp +++ b/GpApp/ObjectDraw2.cpp @@ -1158,7 +1158,7 @@ void DrawCalendar (Rect *theRect) QOffsetRect(&bounds, -bounds.left, -bounds.top); QOffsetRect(&bounds, theRect->left, theRect->top); DrawPicture(thePicture, &bounds); - ReleaseResource((Handle)thePicture); + DisposeHandle((Handle)thePicture); SetPort((GrafPtr)backSrcMap); TextFace(bold); @@ -1191,7 +1191,7 @@ void DrawBulletin (Rect *theRect) QOffsetRect(&bounds, -bounds.left, -bounds.top); QOffsetRect(&bounds, theRect->left, theRect->top); DrawPicture(thePicture, &bounds); - ReleaseResource((Handle)thePicture); + DisposeHandle((Handle)thePicture); SetGraphicsPort(wasCPort); } @@ -1246,7 +1246,7 @@ void DrawPictObject (short what, Rect *theRect) bounds = srcRects[what]; QOffsetRect(&bounds, theRect->left, theRect->top); DrawPicture(thePicture, &bounds); - ReleaseResource((Handle)thePicture); + DisposeHandle((Handle)thePicture); SetGraphicsPort(wasCPort); } diff --git a/GpApp/Play.cpp b/GpApp/Play.cpp index 8944d8e..b20c90e 100644 --- a/GpApp/Play.cpp +++ b/GpApp/Play.cpp @@ -526,7 +526,7 @@ void PlayGame (void) QOffsetRect(&bounds, -bounds.left, -bounds.top); QOffsetRect(&bounds, hOffset, 0); DrawPicture(thePicture, &bounds); - ReleaseResource((Handle)thePicture); + DisposeHandle((Handle)thePicture); } #else // ShowMenuBarOld(); // TEMP @@ -576,7 +576,7 @@ void PlayGame (void) QOffsetRect(&bounds, -bounds.left, -bounds.top); QOffsetRect(&bounds, hOffset, 0); DrawPicture(thePicture, &bounds); - ReleaseResource((Handle)thePicture); + DisposeHandle((Handle)thePicture); SetGraphicsPort(wasCPort); } diff --git a/GpApp/Room.cpp b/GpApp/Room.cpp index 56ea9ee..fa59fc6 100644 --- a/GpApp/Room.cpp +++ b/GpApp/Room.cpp @@ -271,7 +271,7 @@ void ReadyBackground (short theID, short *theTiles) dest = (*thePicture)->picFrame.ToRect(); QOffsetRect(&dest, -dest.left, -dest.top); DrawPicture(thePicture, &dest); - ReleaseResource((Handle)thePicture); + DisposeHandle((Handle)thePicture); QSetRect(&src, 0, 0, kTileWide, kTileHigh); QSetRect(&dest, 0, 0, kTileWide, kTileHigh); @@ -913,7 +913,7 @@ short GetOriginalBounding (short theID) boundCode += 4; if ((*boundsRes)->bottom) boundCode += 8; - ReleaseResource((Handle)boundsRes); + DisposeHandle((Handle)boundsRes); } return (boundCode); diff --git a/GpApp/RoomGraphics.cpp b/GpApp/RoomGraphics.cpp index c08a47a..34eeaff 100644 --- a/GpApp/RoomGraphics.cpp +++ b/GpApp/RoomGraphics.cpp @@ -147,7 +147,7 @@ void LoadGraphicSpecial (short resID) OffsetRect(&bounds, -bounds.left, -bounds.top); DrawPicture(thePicture, &bounds); - ReleaseResource((Handle)thePicture); + DisposeHandle((Handle)thePicture); } //-------------------------------------------------------------- DrawRoomBackground diff --git a/GpApp/RoomInfo.cpp b/GpApp/RoomInfo.cpp index 27e236a..ed21284 100644 --- a/GpApp/RoomInfo.cpp +++ b/GpApp/RoomInfo.cpp @@ -13,6 +13,8 @@ #include "DialogUtils.h" #include "Externs.h" #include "RectUtils.h" +#include "ResourceCompiledRef.h" +#include "ResourceManager.h" #include "Utilities.h" @@ -843,10 +845,10 @@ Boolean PictIDExists (short theID) foundIt = false; } else - ReleaseResource((Handle)thePicture); + DisposeHandle((Handle)thePicture); } else - ReleaseResource((Handle)thePicture); + DisposeHandle((Handle)thePicture); // foundIt = false; // numPicts = Count1Resources('PICT'); @@ -874,15 +876,13 @@ short GetFirstPICT (void) { Handle resHandle; Str255 resName; - ResType resType; - short resID; resHandle = Get1IndResource('PICT', 1); if (resHandle != nil) { - GetResInfo(resHandle, &resID, &resType, resName); - ReleaseResource(resHandle); - return (resID); + const PortabilityLayer::ResourceCompiledRef *resRef = PortabilityLayer::ResourceManager::GetInstance()->ResourceForHandle(reinterpret_cast(resHandle)); + DisposeHandle(resHandle); + return resRef->m_resID; } else return (-1); diff --git a/GpApp/SelectHouse.cpp b/GpApp/SelectHouse.cpp index ac9ff39..6475634 100644 --- a/GpApp/SelectHouse.cpp +++ b/GpApp/SelectHouse.cpp @@ -16,6 +16,7 @@ #include "FileManager.h" #include "House.h" #include "RectUtils.h" +#include "ResourceManager.h" #include "VirtualDirectory.h" @@ -111,7 +112,7 @@ void UpdateLoadDialog (DialogPtr theDialog) else LoadDialogPICT(theDialog, kLoadIconFirstItem + i - housePage, kDefaultHousePict8); - CloseResFile(isResFile); + PortabilityLayer::ResourceManager::GetInstance()->CloseResFile(isResFile); } else LoadDialogPICT(theDialog, kLoadIconFirstItem + i - housePage, diff --git a/GpApp/Sound.cpp b/GpApp/Sound.cpp index e5b0cc3..e6737e9 100644 --- a/GpApp/Sound.cpp +++ b/GpApp/Sound.cpp @@ -228,13 +228,13 @@ PLError_t LoadTriggerSound (short soundID) theSoundData[kMaxSounds - 1] = NewPtr(soundDataSize); if (theSoundData[kMaxSounds - 1] == nil) { - ReleaseResource(theSound); + DisposeHandle(theSound); theErr = PLErrors::kOutOfMemory; } else { BlockMove((Ptr)((Byte*)(*theSound) + 20L), theSoundData[kMaxSounds - 1], soundDataSize); - ReleaseResource(theSound); + DisposeHandle(theSound); } } } @@ -275,7 +275,7 @@ PLError_t LoadBufferSounds (void) return (PLErrors::kOutOfMemory); BlockMove((Ptr)((Byte*)(*theSound) + 20L), theSoundData[i], soundDataSize); - ReleaseResource(theSound); + DisposeHandle(theSound); } theSoundData[kMaxSounds - 1] = nil; @@ -436,7 +436,7 @@ long SoundBytesNeeded (void) if (theSound == nil) { SetResLoad(true); - return ((long)ResError()); + return -1; } totalBytes += GetMaxResourceSize(theSound); // ReleaseResource(theSound); diff --git a/GpApp/StructuresInit.cpp b/GpApp/StructuresInit.cpp index 67e20bb..8613050 100644 --- a/GpApp/StructuresInit.cpp +++ b/GpApp/StructuresInit.cpp @@ -84,7 +84,7 @@ void InitScoreboardMap (void) QOffsetRect(&bounds, -bounds.left, -bounds.top); QOffsetRect(&bounds, hOffset, 0); DrawPicture(thePicture, &bounds); - ReleaseResource((Handle)thePicture); + DisposeHandle((Handle)thePicture); QSetRect(&badgeSrcRect, 0, 0, 32, 66); // 2144 pixels theErr = CreateOffScreenGWorld(&badgeSrcMap, &badgeSrcRect, kPreferredPixelFormat); diff --git a/GpApp/StructuresInit2.cpp b/GpApp/StructuresInit2.cpp index ccfc5d3..1bc7c7d 100644 --- a/GpApp/StructuresInit2.cpp +++ b/GpApp/StructuresInit2.cpp @@ -289,7 +289,7 @@ void CreatePointers (void) else { BlockMove(*tempHandle, demoData, kDemoLength); - ReleaseResource(tempHandle); + DisposeHandle(tempHandle); } #endif } diff --git a/GpApp/Utilities.cpp b/GpApp/Utilities.cpp index 06a12d0..d72b0f5 100644 --- a/GpApp/Utilities.cpp +++ b/GpApp/Utilities.cpp @@ -288,7 +288,7 @@ void LoadGraphic (short resID) OffsetRect(&bounds, -bounds.left, -bounds.top); DrawPicture(thePicture, &bounds); - ReleaseResource((Handle)thePicture); + DisposeHandle((Handle)thePicture); } //-------------------------------------------------------------- LoadScaledGraphic @@ -304,7 +304,7 @@ void LoadScaledGraphic (short resID, Rect *theRect) if (thePicture == nil) RedAlert(kErrFailedGraphicLoad); DrawPicture(thePicture, theRect); - ReleaseResource((Handle)thePicture); + DisposeHandle((Handle)thePicture); } //-------------------------------------------------------------- LargeIconPlot @@ -317,7 +317,7 @@ void LargeIconPlot (Rect *theRect, short theID) theErr = GetIconSuite(&theSuite, theID, svAllLargeData); if (theErr == PLErrors::kNone) - theErr = PlotIconSuite(theRect, atNone, ttNone, theSuite); + theErr = PlotIconSuite(theRect, theSuite); } //-------------------------------------------------------------- DrawCIcon diff --git a/PortabilityLayer/DisplayDeviceManager.cpp b/PortabilityLayer/DisplayDeviceManager.cpp index ed9dd94..b31a938 100644 --- a/PortabilityLayer/DisplayDeviceManager.cpp +++ b/PortabilityLayer/DisplayDeviceManager.cpp @@ -71,7 +71,7 @@ namespace PortabilityLayer GpPixelFormat_t DisplayDeviceManagerImpl::GetPixelFormat() const { - return m_pixelFormat; + return m_pixelFormat; } void DisplayDeviceManagerImpl::SyncPalette(IGpDisplayDriver *displayDriver) diff --git a/PortabilityLayer/MemoryManager.cpp b/PortabilityLayer/MemoryManager.cpp index 7b7c075..fcd0206 100644 --- a/PortabilityLayer/MemoryManager.cpp +++ b/PortabilityLayer/MemoryManager.cpp @@ -1,7 +1,8 @@ #include "MemoryManager.h" #include "MMBlock.h" #include "MMHandleBlock.h" -#include "ResourceCompiledRef.h" +#include "ResourceCompiledRef.h" +#include "ResourceManager.h" #include #include @@ -141,8 +142,9 @@ namespace PortabilityLayer { if (!hdl) return; - - assert(hdl->m_rmSelfRef == nullptr); + + if (hdl->m_rmSelfRef) + PortabilityLayer::ResourceManager::GetInstance()->DissociateHandle(hdl); if (hdl->m_contents) Release(hdl->m_contents); diff --git a/PortabilityLayer/MenuManager.cpp b/PortabilityLayer/MenuManager.cpp index 2aaab48..4a1106c 100644 --- a/PortabilityLayer/MenuManager.cpp +++ b/PortabilityLayer/MenuManager.cpp @@ -533,10 +533,10 @@ namespace PortabilityLayer } if (icsHandle) - ReleaseResource(icsHandle); + DisposeHandle(icsHandle); if (ics8Handle) - ReleaseResource(ics8Handle); + DisposeHandle(ics8Handle); m_haveIcon = true; } diff --git a/PortabilityLayer/PLErrorCodes.h b/PortabilityLayer/PLErrorCodes.h index 3acc88c..0a6cf27 100644 --- a/PortabilityLayer/PLErrorCodes.h +++ b/PortabilityLayer/PLErrorCodes.h @@ -22,6 +22,8 @@ namespace PLErrors kIOError, + kResourceError, + kUserCancelled_TEMP, }; } diff --git a/PortabilityLayer/PLMenus.cpp b/PortabilityLayer/PLMenus.cpp index b60eba3..0eb18f0 100644 --- a/PortabilityLayer/PLMenus.cpp +++ b/PortabilityLayer/PLMenus.cpp @@ -27,7 +27,7 @@ MenuHandle GetMenu(int resID) return nullptr; const MenuHandle menu = PortabilityLayer::MenuManager::GetInstance()->DeserializeMenu(*menuRes); - ReleaseResource(menuRes); + DisposeHandle(menuRes); return menu; } diff --git a/PortabilityLayer/PLQDraw.cpp b/PortabilityLayer/PLQDraw.cpp index 0640330..82547f7 100644 --- a/PortabilityLayer/PLQDraw.cpp +++ b/PortabilityLayer/PLQDraw.cpp @@ -63,7 +63,7 @@ PLError_t GetIconSuite(Handle *suite, short resID, IconSuiteFlags flags) return PLErrors::kNone; } -PLError_t PlotIconSuite(Rect *rect, IconAlignmentType alignType, IconTransformType transformType, Handle iconSuite) +PLError_t PlotIconSuite(Rect *rect, Handle iconSuite) { PL_NotYetImplemented(); return PLErrors::kNone; diff --git a/PortabilityLayer/PLQDraw.h b/PortabilityLayer/PLQDraw.h index a47bfaf..9cbc614 100644 --- a/PortabilityLayer/PLQDraw.h +++ b/PortabilityLayer/PLQDraw.h @@ -13,15 +13,6 @@ namespace PortabilityLayer struct Dialog; -enum IconAlignmentType -{ - atNone -}; - -enum IconTransformType -{ - ttNone -}; enum IconSuiteFlags { @@ -103,7 +94,7 @@ void BeginUpdate(WindowPtr graf); void EndUpdate(WindowPtr graf); PLError_t GetIconSuite(Handle *suite, short resID, IconSuiteFlags flags); -PLError_t PlotIconSuite(Rect *rect, IconAlignmentType alignType, IconTransformType transformType, Handle iconSuite); +PLError_t PlotIconSuite(Rect *rect, Handle iconSuite); CIconHandle GetCIcon(short resID); PLError_t PlotCIcon(Rect *rect, CIconHandle icon); diff --git a/PortabilityLayer/PLResourceManager.cpp b/PortabilityLayer/PLResourceManager.cpp index 805612c..5a2f516 100644 --- a/PortabilityLayer/PLResourceManager.cpp +++ b/PortabilityLayer/PLResourceManager.cpp @@ -7,6 +7,9 @@ #include "MacBinary2.h" #include "MacFileMem.h" #include "MemReaderStream.h" +#include "MemoryManager.h" +#include "MMHandleBlock.h" +#include "ResourceCompiledTypeList.h" #include "ResourceFile.h" #include "PLPasStr.h" #include "PLErrorCodes.h" @@ -15,6 +18,8 @@ namespace PortabilityLayer { + struct MMHandleBlock; + class ResourceManagerImpl final : public ResourceManager { public: @@ -26,11 +31,16 @@ namespace PortabilityLayer void SetResLoad(bool load) override; short OpenResFork(VirtualDirectory_t virtualDir, const PLPasStr &filename) override; + void CloseResFile(short ref) override; + MMHandleBlock *GetResource(const ResTypeID &resType, int id) override; short GetCurrentResFile() const override; void SetCurrentResFile(short ref) override; + void DissociateHandle(MMHandleBlock *hdl) const override; + const ResourceCompiledRef *ResourceForHandle(MMHandleBlock *hdl) const override; + static ResourceManagerImpl *GetInstance(); private: @@ -38,9 +48,11 @@ namespace PortabilityLayer { short m_prevFile; short m_nextFile; - ResourceFile* m_resourceFile; + ResourceFile *m_resourceFile; }; + void UnloadAndDestroyResourceFile(ResourceFile *rf); + std::vector m_resFiles; short m_firstResFile; short m_lastResFile; @@ -66,7 +78,10 @@ namespace PortabilityLayer void ResourceManagerImpl::Shutdown() { for (std::vector::iterator it = m_resFiles.begin(), itEnd = m_resFiles.end(); it != itEnd; ++it) - delete it->m_resourceFile; + { + if (it->m_resourceFile) + UnloadAndDestroyResourceFile(it->m_resourceFile); + } m_resFiles.clear(); } @@ -81,6 +96,43 @@ namespace PortabilityLayer m_currentResFile = ref; } + void ResourceManagerImpl::DissociateHandle(MMHandleBlock *hdl) const + { + assert(hdl->m_rmSelfRef); + assert(hdl->m_rmSelfRef->m_handle == hdl); + hdl->m_rmSelfRef->m_handle = nullptr; + hdl->m_rmSelfRef = nullptr; + } + + const ResourceCompiledRef *ResourceManagerImpl::ResourceForHandle(MMHandleBlock *hdl) const + { + return hdl->m_rmSelfRef; + } + + void ResourceManagerImpl::UnloadAndDestroyResourceFile(ResourceFile *rf) + { + PortabilityLayer::MemoryManager *mm = PortabilityLayer::MemoryManager::GetInstance(); + + ResourceCompiledTypeList *rtls = nullptr; + size_t numRTLs = 0; + rf->GetAllResourceTypeLists(rtls, numRTLs); + + for (size_t i = 0; i < numRTLs; i++) + { + const ResourceCompiledTypeList &rtl = rtls[i]; + + const size_t numRefs = rtl.m_numRefs; + for (size_t r = 0; r < numRefs; r++) + { + const ResourceCompiledRef &ref = rtl.m_firstRef[r]; + if (MMHandleBlock *hdl = ref.m_handle) + mm->ReleaseHandle(hdl); + } + } + + delete rf; + } + ResourceManagerImpl *ResourceManagerImpl::GetInstance() { return &ms_instance; @@ -146,6 +198,32 @@ namespace PortabilityLayer return rfid; } + void ResourceManagerImpl::CloseResFile(short ref) + { + ResFileSlot &slot = m_resFiles[ref]; + delete slot.m_resourceFile; + slot.m_resourceFile = nullptr; + + if (m_lastResFile == ref) + m_lastResFile = slot.m_prevFile; + + if (slot.m_prevFile >= 0) + { + ResFileSlot &prevFileSlot = m_resFiles[slot.m_prevFile]; + prevFileSlot.m_nextFile = slot.m_nextFile; + } + + if (slot.m_nextFile >= 0) + { + ResFileSlot &nextFileSlot = m_resFiles[slot.m_nextFile]; + nextFileSlot.m_prevFile = slot.m_prevFile; + } + + slot.m_nextFile = slot.m_prevFile = -1; + + m_currentResFile = m_lastResFile; + } + MMHandleBlock *ResourceManagerImpl::GetResource(const ResTypeID &resType, int id) { short searchIndex = m_currentResFile; diff --git a/PortabilityLayer/PLResources.cpp b/PortabilityLayer/PLResources.cpp index b1fe0d8..4dde44b 100644 --- a/PortabilityLayer/PLResources.cpp +++ b/PortabilityLayer/PLResources.cpp @@ -63,29 +63,12 @@ int Count1Resources(UInt32 resType) return 0; } -void HCreateResFile(PortabilityLayer::VirtualDirectory_t dirID, const PLPasStr &name) -{ - PL_NotYetImplemented(); -} - -PLError_t ResError() +PLError_t HCreateResFile(PortabilityLayer::VirtualDirectory_t dirID, const PLPasStr &name) { PL_NotYetImplemented(); return PLErrors::kNone; } -short FSpOpenResFile(const VFileSpec &spec, int permission) -{ - PortabilityLayer::ResourceManager *rm = PortabilityLayer::ResourceManager::GetInstance(); - - return rm->OpenResFork(spec.m_dir, spec.m_name); -} - -void CloseResFile(short refNum) -{ - PL_NotYetImplemented(); -} - void SetResLoad(Boolean load) { PortabilityLayer::ResourceManager::GetInstance()->SetResLoad(load != 0); @@ -98,11 +81,6 @@ long GetMaxResourceSize(Handle res) return resRef->GetSize(); } -void GetResInfo(Handle res, short *resID, ResType *resType, Str255 resName) -{ - PL_NotYetImplemented(); -} - short HOpenResFile(PortabilityLayer::VirtualDirectory_t dirID, const PLPasStr &name, int permissions) { PL_NotYetImplemented(); diff --git a/PortabilityLayer/PLResources.h b/PortabilityLayer/PLResources.h index 1e5adb3..c8e508b 100644 --- a/PortabilityLayer/PLResources.h +++ b/PortabilityLayer/PLResources.h @@ -4,31 +4,20 @@ #include "PLCore.h" -struct ResType -{ -}; class PLPasStr; -void DetachResource(Handle hdl); -void ReleaseResource(Handle hdl); - short CurResFile(); void UseResFile(short fid); Handle Get1Resource(UInt32 resID, int index); Handle Get1IndResource(UInt32 resID, int index); int Count1Resources(UInt32 resType); -void HCreateResFile(PortabilityLayer::VirtualDirectory_t dirID, const PLPasStr &name); -PLError_t ResError(); - -short FSpOpenResFile(const VFileSpec &spec, int permission); -void CloseResFile(short refNum); +PLError_t HCreateResFile(PortabilityLayer::VirtualDirectory_t dirID, const PLPasStr &name); void SetResLoad(Boolean load); // Sets whether resources should be loaded when requested long GetMaxResourceSize(Handle res); -void GetResInfo(Handle res, short *resID, ResType *resType, Str255 resName); // This should return -1 on error? short HOpenResFile(PortabilityLayer::VirtualDirectory_t dirID, const PLPasStr &name, int permissions); diff --git a/PortabilityLayer/ResourceFile.cpp b/PortabilityLayer/ResourceFile.cpp index 9e7bfda..e7fd70e 100644 --- a/PortabilityLayer/ResourceFile.cpp +++ b/PortabilityLayer/ResourceFile.cpp @@ -336,6 +336,12 @@ namespace PortabilityLayer int ResourceFile::CompiledTypeListSearchPredicate(const ResTypeID &resTypeID, const ResourceCompiledTypeList &typeList) { return memcmp(&resTypeID, &typeList.m_resType, 4); + } + + void ResourceFile::GetAllResourceTypeLists(ResourceCompiledTypeList *&outTypeLists, size_t &outCount) const + { + outTypeLists = m_compiledTypeListBlob; + outCount = m_numResourceTypes; } const ResourceCompiledTypeList *ResourceFile::GetResourceTypeList(const ResTypeID &resType) diff --git a/PortabilityLayer/ResourceFile.h b/PortabilityLayer/ResourceFile.h index ddfec25..a02d25d 100644 --- a/PortabilityLayer/ResourceFile.h +++ b/PortabilityLayer/ResourceFile.h @@ -1,51 +1,52 @@ -#pragma once -#ifndef __PL_RESOURCE_FILE_H__ -#define __PL_RESOURCE_FILE_H__ - -#include "ResTypeID.h" -#include - -namespace PortabilityLayer -{ - class IOStream; - class MacFileMem; - struct MMHandleBlock; - struct ResourceCompiledRef; - struct ResourceCompiledTypeList; - class ResTypeID; - - class ResourceFile - { - public: - - ResourceFile(); - ~ResourceFile(); - - bool Load(IOStream *stream); - - const ResourceCompiledTypeList *GetResourceTypeList(const ResTypeID &resType); - MMHandleBlock *GetResource(const ResTypeID &resType, int id, bool load); - - private: - - uint8_t *m_resDataBlob; - size_t m_resDataBlobSize; - - uint8_t *m_resNameBlob; - size_t m_resNameBlobSize; - - ResourceCompiledRef *m_compiledRefBlob; - size_t m_numResources; - - ResourceCompiledTypeList *m_compiledTypeListBlob; - size_t m_numResourceTypes; - - static bool CompiledRefSortPredicate(const ResourceCompiledRef &a, const ResourceCompiledRef &b); - static bool CompiledTypeListSortPredicate(const ResourceCompiledTypeList &a, const ResourceCompiledTypeList &b); - - static int CompiledRefSearchPredicate(int resID, const ResourceCompiledRef &ref); - static int CompiledTypeListSearchPredicate(const ResTypeID &resTypeID, const ResourceCompiledTypeList &typeList); - }; -} - -#endif +#pragma once +#ifndef __PL_RESOURCE_FILE_H__ +#define __PL_RESOURCE_FILE_H__ + +#include "ResTypeID.h" +#include + +namespace PortabilityLayer +{ + class IOStream; + class MacFileMem; + struct MMHandleBlock; + struct ResourceCompiledRef; + struct ResourceCompiledTypeList; + class ResTypeID; + + class ResourceFile + { + public: + ResourceFile(); + ~ResourceFile(); + + bool Load(IOStream *stream); + + void GetAllResourceTypeLists(ResourceCompiledTypeList *&outTypeLists, size_t &outCount) const; + + const ResourceCompiledTypeList *GetResourceTypeList(const ResTypeID &resType); + MMHandleBlock *GetResource(const ResTypeID &resType, int id, bool load); + + private: + + uint8_t *m_resDataBlob; + size_t m_resDataBlobSize; + + uint8_t *m_resNameBlob; + size_t m_resNameBlobSize; + + ResourceCompiledRef *m_compiledRefBlob; + size_t m_numResources; + + ResourceCompiledTypeList *m_compiledTypeListBlob; + size_t m_numResourceTypes; + + static bool CompiledRefSortPredicate(const ResourceCompiledRef &a, const ResourceCompiledRef &b); + static bool CompiledTypeListSortPredicate(const ResourceCompiledTypeList &a, const ResourceCompiledTypeList &b); + + static int CompiledRefSearchPredicate(int resID, const ResourceCompiledRef &ref); + static int CompiledTypeListSearchPredicate(const ResTypeID &resTypeID, const ResourceCompiledTypeList &typeList); + }; +} + +#endif diff --git a/PortabilityLayer/ResourceManager.h b/PortabilityLayer/ResourceManager.h index 7878dd4..9533ebf 100644 --- a/PortabilityLayer/ResourceManager.h +++ b/PortabilityLayer/ResourceManager.h @@ -7,6 +7,7 @@ class PLPasStr; namespace PortabilityLayer { struct MMHandleBlock; + struct ResourceCompiledRef; class ResTypeID; class ResourceManager @@ -18,11 +19,16 @@ namespace PortabilityLayer virtual void SetResLoad(bool load) = 0; virtual short OpenResFork(VirtualDirectory_t virtualDir, const PLPasStr &filename) = 0; + virtual void CloseResFile(short ref) = 0; + virtual MMHandleBlock *GetResource(const ResTypeID &resType, int id) = 0; virtual short GetCurrentResFile() const = 0; virtual void SetCurrentResFile(short ref) = 0; + virtual void DissociateHandle(MMHandleBlock *hdl) const = 0; + virtual const ResourceCompiledRef *ResourceForHandle(MMHandleBlock *hdl) const = 0; + static ResourceManager *GetInstance(); }; }