CachedPicture *CachePicture( char *aFilespec ) { FileDebug( "Started caching image" ); int ExistingCacheIndex = GetCacheIndex( aFilespec ); if( ExistingCacheIndex != -1 ) { FileDebug( "Skipped caching image as it's already cached" ); return &PictureCache[ExistingCacheIndex]; } if( NrPicturesCached >= MAX_PICTURE_CACHE_COUNT ) { FileDebug( "Skipped caching image as no more cache slots available" ); return NULL; } HDC hdc = GetDC(NULL); if (!hdc) { FileDebug( "Skipped caching image as failed to lock DC" ); return NULL; } strcpy_s( PictureCache[NrPicturesCached].FileName, DEFAULT_STR_BUFFER_SIZE, aFilespec ); PictureCache[NrPicturesCached].NameHash = GetStrHash( aFilespec ); int ImgType; PictureCache[NrPicturesCached].LoadedPicture = LoadPicture( aFilespec, 0, 0, ImgType, 0, false ); if( PictureCache[NrPicturesCached].LoadedPicture == NULL ) { FileDebug( "Skipped caching image as it could not be loaded" ); ReleaseDC(NULL, hdc); return NULL; } bool image_is_16bit; LONG image_width, image_height; PictureCache[NrPicturesCached].Pixels = getbits( PictureCache[NrPicturesCached].LoadedPicture, hdc, image_width, image_height, image_is_16bit ); if( PictureCache[NrPicturesCached].Pixels == NULL ) { FileDebug( "Skipped caching image as it could not be converted to pixel map" ); ReleaseDC(NULL, hdc); return NULL; } PictureCache[NrPicturesCached].Width = image_width; PictureCache[NrPicturesCached].Height = image_height; PictureCache[NrPicturesCached].NeedsPSCache = true; PictureCache[NrPicturesCached].NeedsSSCache = true; NrPicturesCached++; ReleaseDC(NULL, hdc); FileDebug( "\tFinished caching image" ); return &PictureCache[NrPicturesCached-1]; }
int GetCacheIndex( char *aFilespec ) { // for( int i=0;i<NrPicturesCached;i++) // if( strstr( PictureCache[i].FileName, aFilespec ) ) // return i; int ThisHash = GetStrHash( aFilespec ); for( int i=0;i<NrPicturesCached;i++) if( ThisHash == PictureCache[i].NameHash ) return i; return -1; }
struct chashlist * HashGetAllEntries( struct chash * hash, const char * key ) { int retreser = 3; int buckets = GeneralUsePrimes[hash->bucketCountPlace]; int thisHash = GetStrHash( key ) % buckets; int multi = hash->allowMultiple; struct chashentry * thisEntry = &hash->buckets[thisHash]; struct chashentry * overEntry = &hash->buckets[buckets]; struct chashlist * ret = malloc( sizeof( struct chashlist ) + sizeof( struct chashentry ) * retreser ); ret->length = 0; while( thisEntry->key ) { if( strcmp( thisEntry->key, key ) == 0 ) { ret->items[ret->length].key = thisEntry->key; ret->items[ret->length].value = thisEntry->value; ret->items[ret->length].hash = thisEntry->hash; ret->length++; if( !multi ) { return ret; } if( ret->length == retreser ) { retreser *= 2; ret = realloc( ret, sizeof( struct chashlist ) + sizeof( struct chashentry ) * retreser ); } } thisEntry++; if( thisEntry == overEntry ) thisEntry = &hash->buckets[0]; } if( ret->length == 0 ) { free( ret ); return 0; } return ret; }
int HashTableRemoveSpecific( struct chash * hash, const char * key, void * value ) { int buckets = GeneralUsePrimes[hash->bucketCountPlace]; int thisHash = GetStrHash( key ) % buckets; int multi = hash->allowMultiple; struct chashentry * thisEntry = &hash->buckets[thisHash]; struct chashentry * overEntry = &hash->buckets[buckets]; int startentry = thisHash; int entriesSearched = 0; int removedEntries = 0; //Search the list for matches, search until we have an empty spot. while( thisEntry->key ) { if( strcmp( thisEntry->key, key ) == 0 && thisEntry->value == value ) { free( (char*)thisEntry->key ); thisEntry->key = 0; removedEntries++; if( !multi ) { break; } } thisEntry++; if( thisEntry == overEntry ) thisEntry = &hash->buckets[0]; entriesSearched++; } if( removedEntries == 0 ) return 0; hash->entryCount -= removedEntries; RedoHashRange( hash, startentry, entriesSearched, entriesSearched-removedEntries ); return removedEntries; }
void WINAPI MoveScreenshotToCache( char *Name ) { if( CurScreenshot->Pixels == NULL ) { FileDebug( "Can not move screenshot to cache because there are no screenshots" ); return; } if( NrPicturesCached >= MAX_PICTURE_CACHE_COUNT ) { FileDebug( "Skipped caching image as no more cache slots available" ); return; } FileDebug( "Started caching screenshot" ); strcpy_s( PictureCache[NrPicturesCached].FileName, DEFAULT_STR_BUFFER_SIZE, Name ); PictureCache[NrPicturesCached].NameHash = GetStrHash( Name ); PictureCache[ NrPicturesCached ].LoadedPicture = NULL; int PixelsByteSize = CurScreenshot->GetWidth() * CurScreenshot->GetHeight() * sizeof( COLORREF ); PictureCache[ NrPicturesCached ].Pixels = (LPCOLORREF) _aligned_malloc( PixelsByteSize, SSE_ALIGNMENT ); memcpy( PictureCache[ NrPicturesCached ].Pixels, CurScreenshot->Pixels, PixelsByteSize ); PictureCache[ NrPicturesCached ].Width = CurScreenshot->GetWidth(); PictureCache[ NrPicturesCached ].Height = CurScreenshot->GetHeight(); PictureCache[NrPicturesCached].NeedsPSCache = true; PictureCache[NrPicturesCached].NeedsSSCache = true; NrPicturesCached++; FileDebug( "\tFinished caching screenshot" ); }