TGC Codebase Backup



Entity Data Extraction Plug by Lord Harry

26th Jun 2004 16:25
Summary

This DLL Library plug-in will extract Entity data from a .csm file. The entity data is extracted to a text based file with an extension of .elf (Entity List File). I have added the



Description

The entity data is extracted into the .elf file in the following format :

----------------------------------------
ELF Entity List File
Ver1.00
Entity#=0
Flags=0
GroupId=0
"classname"="Light"
"range"="400"
"color"="255 255 0"
"style"="1"
"flags"="1"
XPos=-128.000000
YPos=216.000000
ZPos=280.000000
Entity#=1
Flags=0
GroupId=0
"classname"="Light"
"range"="340"
etc ......
----------------------------------------------------



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    The C/C++ source is as follows and was compiled as a DLL Project using the LCC compiler (with underscores turned off). Comments are sparce I'm afraid, but you should be able to see what's going on easily enough.


#include <windows.h>
#include <stdio.h>
#include <systypes.h>
#include <sysstat.h>
#include <errno.h>
#include <io.h>
#include <string.h>
/*------------------------------------------------------------------------
 Procedure:     LibMain ID:1
 Purpose:       Dll entry point.Called when a dll is loaded or
                unloaded by a process, and when new threads are
                created or destroyed.
 Input:         hDllInst: Instance handle of the dll
                fdwReason: event: attach/detach
                lpvReserved: not used
 Output:        The return value is used only when the fdwReason is
                DLL_PROCESS_ATTACH. True means that the dll has
                sucesfully loaded, False means that the dll is unable
                to initialize and should be unloaded immediately.
 Errors:
------------------------------------------------------------------------*/
BOOL WINAPI LibMain(HINSTANCE hDLLInst, DWORD fdwReason, LPVOID lpvReserved)
{
    switch (fdwReason)
    {
        case DLL_PROCESS_ATTACH:
            // The DLL is being loaded for the first time by a given process.
            // Perform per-process initialization here.  If the initialization
            // is successful, return TRUE; if unsuccessful, return FALSE.


            break;
        case DLL_PROCESS_DETACH:
            // The DLL is being unloaded by a given process.  Do any
            // per-process clean up here, such as undoing what was done in
            // DLL_PROCESS_ATTACH.  The return value is ignored.


            break;
        case DLL_THREAD_ATTACH:
            // A thread is being created in a process that has already loaded
            // this DLL.  Perform any per-thread initialization here.  The
            // return value is ignored.

            break;
        case DLL_THREAD_DETACH:
            // A thread is exiting cleanly in a process that has already
            // loaded this DLL.  Perform any per-thread clean up here.  The
            // return value is ignored.

            break;
    }
    return TRUE;
}

// Function Prototypes
long  CSM_GetEntityBlockStart (void* CSPtr, char* szFilename);
long* CSM_GetVisBlockPtr      (void *CSPtr, int lNoOfGroups);
long* CSM_GetLightMapBlockPtr (void* CSPtr, int lNoOfGroups);
long* CSM_GetMeshBlockPtr     (void* CSPtr, int lNoOfGroups);
long* CSM_GetEntityBlockPtr   (void* CSPtr, int lNoOfGroups);
long* CSM_MovePastString      (void* CSPtr);
long  CSM_ProcessEntities     (void* CSPtr, int lNoOfEntities, char* szFilename);

void __declspec(dllexport) PLUGINCLASS (void* inbuffer, long insize, void* outbuffer, long outsize)
{
	long *iBufPtr;

	iBufPtr = ((long *) outbuffer);
	*iBufPtr = 2;
	return;
}

void __declspec(dllexport) PLUGINNAME (void* inbuffer, long insize, void* outbuffer, long outsize)
{
	char *cBufPtr;

	cBufPtr = ((char *) outbuffer);
	sprintf( cBufPtr, "%s", "Entity List File (*.elf)|*.elf");

	return;
}

void __declspec(dllexport) RUNPLUGIN (void* inbuffer, long insize, void* outbuffer, long outsize)
{
	long lRetVal;
	char *cBufPtr;
	char cMsgText[200];
	char szOutFilename[300];
	char *szFilename = &szOutFilename[0];

	cBufPtr = ((char *) inbuffer);

	strcpy(szFilename, cBufPtr);

	cBufPtr = cBufPtr + strlen(szOutFilename) + 1;

	lRetVal = CSM_GetEntityBlockStart(cBufPtr, szFilename);

	*(long *)outbuffer = lRetVal;

	if (lRetVal == 1)
	{
		sprintf( &cMsgText[0], "Entity List File created atn%s", szFilename);
		MessageBox( NULL, &cMsgText[0], "Entity List File Created", MB_OK);
	}
	else
	{
		MessageBox( NULL, "Failed to create Entity List File", "Entity List File Failed", MB_OK);
	}

	return;
}

long CSM_GetEntityBlockStart(void* CSPtr, char* szFilename)
// Finds start of Entity Block
{
	int lError;
	long value;
	long *lCSPtr;
	long lNoOfGroups;
	long lNoOfVisGroups;
	long lNoOfLightMaps;
	long lNoOfMeshBlocks;
	long lNoOfEntityBlocks;

	lError = 0;

	// Check version of CSM file
	lCSPtr = ((long *)CSPtr);
	value = *lCSPtr;

	if ((value != 5) && (value != 4))
	{
		// CSM Cartography Shop versions 4 and 5 only catered for
		return 0;
	}

	// Get No. of groups in Group Block
	lCSPtr++;
	lNoOfGroups = *lCSPtr;
	lCSPtr++;

	// Find end of Group Block
	if (lNoOfGroups > 0)
	{
		lCSPtr = CSM_GetVisBlockPtr(lCSPtr, lNoOfGroups);
	}

	lNoOfVisGroups = *lCSPtr;
	lCSPtr++;

	// Find end of Vis Group Block
	if (lNoOfVisGroups > 0)
	{
		lCSPtr = CSM_GetLightMapBlockPtr(lCSPtr, lNoOfGroups);
	}

	lNoOfLightMaps = *lCSPtr;
	lCSPtr++;

	// Find end of LightMaps Block
	if (lNoOfLightMaps > 0)
	{
		lCSPtr = CSM_GetMeshBlockPtr(lCSPtr, lNoOfGroups);
	}

	lNoOfMeshBlocks = *lCSPtr;
	lCSPtr++;

	// Find end of Mesh Block
	if (lNoOfMeshBlocks > 0)
	{
		lCSPtr = CSM_GetEntityBlockPtr(lCSPtr, lNoOfMeshBlocks);
	}

	lNoOfEntityBlocks = *lCSPtr;
	lCSPtr++;

	// Interpret Entity data and write to file
	if (lNoOfEntityBlocks > 0)
	{
		if (CSM_ProcessEntities(lCSPtr, lNoOfEntityBlocks, szFilename) == 0)
		{
			return lError;
		}
	}

	return 1;
}

long* CSM_GetVisBlockPtr (void* CSPtr, int lNoOfGroups)
{
	// Move Pointer to end of Group Block

    int i;
	long *lCSPtr;

	lCSPtr = ((long *)CSPtr);

	for (i = 0;i < lNoOfGroups; i++)
	{
		lCSPtr++;
		lCSPtr++;
		lCSPtr = CSM_MovePastString(lCSPtr);
		lCSPtr = lCSPtr + 3;
	}

	return lCSPtr;
}

long* CSM_GetLightMapBlockPtr (void* CSPtr, int lNoOfGroups)
{
	// Move Pointer to end of Vis Group Block

    int i;
	long *lCSPtr;

	lCSPtr = ((long *)CSPtr);

	for (i = 0;i < lNoOfGroups; i++)
	{
		lCSPtr = lCSPtr + 4;
	}

	return lCSPtr;
}

long* CSM_GetMeshBlockPtr (void* CSPtr, int lNoOfGroups)
{
	// Move Pointer to end of Light Map Block

    int i, j;
	long *lCSPtr;
	long lWidth;
	long lHeight;
	long lNoOfPixels;

	lCSPtr = ((long *)CSPtr);

	for (i = 0;i < lNoOfGroups; i++)
	{
		lWidth = *lCSPtr;
		lCSPtr++;
		lHeight = *lCSPtr;
		lCSPtr++;
		lNoOfPixels = lWidth * lHeight;
		for (j = 0;j < lNoOfPixels; j++)
		{
			lCSPtr++;
		}
	}

	return lCSPtr;
}

long* CSM_GetEntityBlockPtr (void* CSPtr, int lNoOfGroups)
{
	// Move Pointer to end of Light Map Block

    int i, j, k;
	long *lCSPtr;
	long lNoOfSurfaces;
	long lNoOfVertices;
	long lNoOfTriangles;
	long lNoOfLines;

	lCSPtr = ((long *)CSPtr);

	for (i = 0;i < lNoOfGroups; i++)
	{
		lCSPtr = lCSPtr + 2;
		lCSPtr = CSM_MovePastString(lCSPtr);
		lCSPtr = lCSPtr + 7;
		lNoOfSurfaces = *lCSPtr;
		lCSPtr++;
		for (j=0;j < lNoOfSurfaces; j++)
		{
			lCSPtr++;
			lCSPtr = CSM_MovePastString(lCSPtr);
			lCSPtr = lCSPtr + 6;
			lNoOfVertices = *lCSPtr;
			lCSPtr++;
			lNoOfTriangles = *lCSPtr;
			lCSPtr++;
			lNoOfLines = *lCSPtr;
			lCSPtr++;
			for (k = 0; k < lNoOfVertices; k++)
			{
				lCSPtr = lCSPtr + 15;
			}
			for (k = 0; k < lNoOfTriangles; k++)
			{
				lCSPtr = lCSPtr + 3;
			}
			for (k = 0; k < lNoOfLines; k++)
			{
				lCSPtr = lCSPtr + 2;
			}
		}
	}

	return lCSPtr;
}

long* CSM_MovePastString(void* CSPtr)
{
	// Moves Pointer past null terminated string

	char *cCSPtr;
	long *lCSPtr;
	int i;
	int iExitFlag;

	iExitFlag = 0;

	// Convert Pointer to char(byte) pointer
	cCSPtr = ((char *)CSPtr);

	do
	{
		// Find end of string (ASC 0)
		if (*cCSPtr == 0)
		{
			iExitFlag = 1;
		}
		cCSPtr++;
	}
	while (iExitFlag == 0);

	// Convert Pointer back to long
	lCSPtr = ((long *)cCSPtr);

	return lCSPtr;
}

long CSM_ProcessEntities(void* CSPtr, int lNoOfEntities, char* szFilename)
{
	int   i, iExitFlag, fh;
	int   iCharsWritten, iPropCharsWritten;
	long* lCSPtr;
	long  lFlags, lGroup;
	float fXPos, fYPos, fZPos;
	float* fCSPtr;
	char  cEOL = 'n';
	char* cCSPtr;
	char  cStrBuffer[1000];
	char  cStrProperties[1000];
	char* cBuffer = &cStrBuffer[0];
	char* cProperties = &cStrProperties[0];
	char  cELFHeader[] = "ELF Entity List FilenVer1.00n";
	FILE *fEntityFile;

	// Create and Open Entity File for Write access
	fh = _creat( szFilename, _S_IWRITE);
	if ( fh == -1)
	{
		return 0;
	}
	else
	{
		_close(fh);
	}

	// Associate Entity file with a stream
	if (( fEntityFile = fopen(szFilename, "w")) == NULL )
	{
		return 0;
	}

	// Convert Ptr to long
	lCSPtr = ((long *)CSPtr);

	// Loop through Entity Block and extract data
	iCharsWritten = 0;

	for (i = 0; i < lNoOfEntities; i++)
	{
		// Store Entity Number details
		iCharsWritten += sprintf( cBuffer + iCharsWritten, "Entity#=%un", i);
		lFlags = *lCSPtr;
		lCSPtr++;
		iCharsWritten += sprintf( cBuffer + iCharsWritten, "Flags=%lun", lFlags);
		lGroup = *lCSPtr;
		lCSPtr++;
		iCharsWritten += sprintf( cBuffer + iCharsWritten, "GroupId=%lun", lGroup);
		// Convert Ptr to Char
		cCSPtr = ((char *) lCSPtr);

		iExitFlag = 0;
		do
		{
			// Find end of string (ASC 0)
			if (*cCSPtr == 0)
			{
				iExitFlag = 1;
			}
			else
			{
				if (*cCSPtr == 10)
				{
					iCharsWritten += sprintf( cBuffer + iCharsWritten, "%c", cEOL);
				}
				else
				{
					iCharsWritten += _snprintf( cBuffer + iCharsWritten, 1, "%c", *cCSPtr);
				}
			}
			cCSPtr++;
		}
		while (iExitFlag == 0);

		// Convert Ptr to float
		fCSPtr = ((float *)cCSPtr);
		fXPos = *fCSPtr;
		fCSPtr++;
		iCharsWritten += sprintf( cBuffer + iCharsWritten, "XPos=%.6fn", fXPos);
		fYPos = *fCSPtr;
		fCSPtr++;
		iCharsWritten += sprintf( cBuffer + iCharsWritten, "YPos=%.6fn", fYPos);
		fZPos = *fCSPtr;
		fCSPtr++;
		iCharsWritten += sprintf( cBuffer + iCharsWritten, "ZPos=%.6fn", fZPos);
		// Convert Ptr to long
		lCSPtr = ((long *)fCSPtr);
		//iCharsWritten += sprintf( cBuffer + iCharsWritten, "%s", cProperties);
	}

	fprintf(fEntityFile, "%s", &cELFHeader[0]);
	fprintf(fEntityFile, "%s", cBuffer);
	fclose( fEntityFile );

	return 1;
}