Skip to content

Commit

Permalink
Finish Windows image-as-a-resource code (still need to document the p…
Browse files Browse the repository at this point in the history
…rocess for

embedding). Now allow the resource to be called whatever (ResourceHacker uses
the file name of the iamge minus extension, as uppercase, as a default).
Check all RT_RCDATA type resources until checkImageHeaderFromBytesAndSize is
satisfied. Fix argument parsing to allow for no arguments.

Still have to allow additional arguments to be passed. Manana.
  • Loading branch information
eliotmiranda committed Mar 22, 2024
1 parent 8c6246a commit 5b3cef5
Showing 1 changed file with 52 additions and 30 deletions.
82 changes: 52 additions & 30 deletions platforms/win32/vm/sqWin32Main.c
Expand Up @@ -1476,49 +1476,63 @@ void __cdecl Cleanup(void)
/****************************************************************************/
/* Embedded Images */
/****************************************************************************/
#if 1
// Look for an image as a resource. type: RT_RCDATA, name: "SMALLTALKIMAGE"
// Look for an image as a resource. type: RT_RCDATA. Check all such resources
// until a candidate is found.
// If found, answer the ImageIsAResource marker, otherwise return NULL.

static HRSRC imageResource = NULL;
static BOOL foundResource = false;
static LPSTR imageResourceName = NULL;
#define IMRESTYPE (LPCSTR)RT_RCDATA

#define theVM (HMODULE)0
static BOOL
enumImageResources(HMODULE hModule, LPCSTR lpType, LPSTR lpName, LONG_PTR ign)
{
if (lpType == IMRESTYPE
&& !strcmp(lpName,"SMALLTALKIMAGE")) {
foundResource = true;
HRSRC resource;
HGLOBAL handle;
void *data;
DWORD dataSize;

#if 0 // debug/development
fprintf(stderr,"enumImageResources %s\n", lpName);
#endif
if (lpType != IMRESTYPE)
return true; // keep on enumerating

// N.B. There is no need to close the handes answered here; that's only
// necessary in 16-bit Windows
if ((resource = FindResourceA(theVM,lpName,IMRESTYPE))
&& (handle = LoadResource(theVM,resource))
&& (data = LockResource(handle))
&& (dataSize = SizeofResource(theVM,resource))
&& checkImageHeaderFromBytesAndSize(data,(sqInt)dataSize)) {
imageResourceName = malloc(strlen(lpName) + 1);
strcpy(imageResourceName,lpName);
sqFilePluginNoteImageResourceData(data,dataSize);
return false; // found resource, so stop enumerating
}
#if 0 // debug/development
if (!resource)
fprintf(stderr,"FindResourceA %s => %lu\n", lpName, GetLastError());
else if (!handle)
fprintf(stderr,"LoadResource => %lu\n", GetLastError());
#endif
return true; // keep on enumerating
}

static sqImageFile
findEmbeddedImage(void)
{
HGLOBAL handle;
void *data;
DWORD dataSize;

foundResource = false;
EnumResourceNamesA((HMODULE)0,IMRESTYPE,enumImageResources,0);
if (!foundResource)
return 0;

if (!(imageResource = FindResourceA((HMODULE)0,"SMALLTALKIMAGE",IMRESTYPE))
|| !(handle = LoadResource((HMODULE)0,imageResource))
|| !(data = LockResource(handle)))
return 0;

dataSize = SizeofResource((HMODULE)0,imageResource);
sqFilePluginNoteImageResourceData(data,dataSize);
return ImageIsAResource;
}
#else
static __inline__ sqImageFile findEmbeddedImage(void) { return 0; }
if (!imageResourceName)
EnumResourceNamesA(theVM,IMRESTYPE,enumImageResources,0);
#if 0 // debug/development
if (imageResourceName)
fprintf(stderr,"imageResourceName: %s\n", imageResourceName);
#endif
return imageResourceName ? ImageIsAResource : 0;
}

sqInt sqImageFileIsEmbedded() { return imageResourceName != NULL; }
#undef theVM


/****************************************************************************/
Expand Down Expand Up @@ -1719,7 +1733,14 @@ sqMain(int argc, char *argv[])
if (!imageFile) {
imageFile = sqImageFileOpen(imageName,"rb");
readImageFromFileHeapSizeStartingAt(imageFile, virtualMemory, 0);
} else {
}
else {
if (imageFile == ImageIsAResource) {
int i;
for (i = strlen(imageResourceName); i >= 0; --i)
imageNameW[i] = imageName[i] = imageResourceName[i];
}

readImageFromFileHeapSizeStartingAt(imageFile, virtualMemory, sqImageFilePosition(imageFile));
}
sqImageFileClose(imageFile);
Expand Down Expand Up @@ -2252,9 +2273,10 @@ parseGenericArgs(int argc, char *argv[])
return 1; /* ok not to have an image since user can choose one. */
default:
/* It is OK to run the console VM provided an image has been
* provided by the ini file.
* provided by the ini file, or if there is an embedded image.
*/
return imageName[0] != 0;
return imageName[0] != 0
|| findEmbeddedImage();
}

/* Always allow the command-line to override an implicit image name. */
Expand Down

0 comments on commit 5b3cef5

Please sign in to comment.