Game Development Community

MD5file resource

by Dave Young · 07/03/2006 (11:13 am) · 10 comments

Download Code File

The md5 code was based on the RSA implementation.

1) Add in the accompanying files (md5file.cc, md5file.h)

2) Add this to your md5console.cc file:

(under the other header includes, add:

#include "md5file.h"


and at the bottom of the file, under all other functions:

ConsoleFunction(getFileMD5, const char*, 2, 2, "getFileMD5(file)")
{
	const char * fileMD5;
	fileMD5 = MD5File( argv[1] ); 
	return((const char *) fileMD5);
}

3) To use the function in script, simply do:

%filemd5 = getFileMd5("main.cs");
echo(%filemd5);

#1
06/29/2006 (10:57 am)
Thank you Dave!

A very useful resource!

- Tom.
#2
07/01/2006 (9:50 am)
Quote:
2) Add this to your md5console.cc file:

I don't have that file.
#3
07/01/2006 (10:40 am)
It should be under source/console.

If not, try creating one:
#include "platform/platform.h"
#include "console/console.h"
#include "console/consoleInternal.h"
#include "console/ast.h"
#include "core/resManager.h"
#include "core/fileStream.h"
#include "console/compiler.h"
#include "platform/event.h"
#include "platform/gameInterface.h"
#include "md5file.h"

static const char Base16Values[] = "0123456789ABCDEF";
ConsoleFunction(getFileMD5, const char*, 2, 2, "getFileMD5(file)")
{
        const char * fileMD5;
        fileMD5 = MD5File( argv[1] ); 
        //Con::printf("MD5 Checksum for file %s = <%s>", argv[1], csMD5);
        return((const char *) fileMD5);
}
#4
07/01/2006 (10:47 am)
I think we are on the right track, but now I am missing a header from that file.
C:\Torque\Felswourne\engine\core\md5console.cc(10): fatal error C1083: Cannot open include file: 'md5.h': No such file or directory
#5
07/01/2006 (10:51 am)
Sorry, I pasted the wrong thing. I've updated the comment.

Delete that file, and create a new one under /source/console/, with the new content above.

(If your /source/console/ folder contains a md5.h file, then please add in the include again. :) )
#6
07/01/2006 (10:57 am)
That got it working, now time for the scripts. Thanks for the help Tom! 5/5
#7
01/11/2008 (12:54 pm)
I assume the missing files came from this resource actually: http://www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=4392

works great. Thank you for the contribution
#8
08/24/2008 (6:15 am)
I made a little modification to the consolefunction to save some memory and added a getMD5 for strings:

ConsoleFunction(getFileMD5, const char*, 2, 2, "getFileMD5(file)")
{
        char * fileMD5;
	char *returnBuffer = Con::getReturnBuffer( 256 );
        fileMD5 = MD5File( argv[1] ); 
        dSprintf( returnBuffer, 256, "%s", fileMD5 );
        dFree(fileMD5);
        return returnBuffer;

}

ConsoleFunction(getMD5, const char*, 2, 2, "getMD5(string)")
{
        char * strMD5;
	char *returnBuffer = Con::getReturnBuffer( 256 );
        strMD5 = MD5String( argv[1] ); 
        dSprintf( returnBuffer, 256, "%s", strMD5 );
        dFree(strMD5);
        return returnBuffer;
}
I did the convert to a consolebuffer to free the memory which was duplicated in PrintMD5 and never released again. Maybe there is a better way to do this but it works and save lots of memory ;)
#9
07/20/2009 (8:10 pm)
after I copied the 3 files do I need to recompile before I can use the getMD5 function?
#10
08/28/2010 (2:00 am)
In TGEA Pro 1.01 this code will only calculate the md5 of files in the base directory when files are iterated with
for (%file = findFirstFileMultiExpr("*"); %file !$= ""; %file = findNextFileMultiExpr("*"))
{
}

So I changed the consolefunction to differentiate between directories (and blank files such as console.log) from actual files:

ConsoleFunction(getFileMD5, const char*, 2, 2, "getFileMD5(file)")  
 {  
         char * fileMD5;  
         char *returnBuffer = Con::getReturnBuffer( 256 );  
         fileMD5 = MD5File( argv[1] );

		 if(fileMD5)// added to return "NULL" string instead of a true null which is useless in script
			dSprintf( returnBuffer, 256, "%s", fileMD5 ); 
		 else
			dSprintf( returnBuffer, 256, "NULL"); 

         dFree(fileMD5);  
         return returnBuffer;  
   
 }

So, in script I could make separate lists of directories and files this way:

for (%file = findFirstFileMultiExpr("*"); %file !$= ""; %file = findNextFileMultiExpr("*"))
	{
		%ourMD5 = getfilemd5(%file);
		if(%ourMD5 $= "NULL")	
		{
			// this is to discern directories from empty files like console.log
			%lastchar = getsubstr(%file,strlen(%file)-1,strlen(%file)-1);	// if the last character is a "/" it's a directory
			if(%lastchar $="/")
			{
			   //this would be a directory path
			}
		}
		else
		{
		    // this would be an actual file
		}
	}