logo
Main

Forums

Downloads

Unreal-Netiquette

Donate for Oldunreal:
Donate

borderline

Links to our wiki:
Wiki

Walkthrough

Links

Tutorials

Unreal Reference

Usermaps

borderline

Contact us:
Submit News
Page Index Toggle Pages: 1 Send TopicPrint
Normal Topic [Snippet] UExporter/UFactory for binary resources (Read 638 times)
han
Global Moderator
Unreal Rendering Guru
Developer Team
*****
Offline


Oldunreal member

Posts: 565
Location: Germany
Joined: Dec 10th, 2014
Gender: Male
[Snippet] UExporter/UFactory for binary resources
Aug 6th, 2016 at 12:47am
Print Post  
Sample code for a simple UExporter/UFactory for UPalettes class and ACT (Adobe Color Table) file format. The basic idea is to showcase how to write UExporters/UFactories which tightly integrate into the engine (batchexport, #exec new Factory*, etc.).

One important consideration is that the UExporter/UFactory class you add needs to get loaded, either because the dll file it is implemented in was already loader, or by using some autoloading out of int files approach. I included the autoloading code in the UCC.cpp of my HP1 PubSrc if you want to take a look at it. Smirftsch added the autoloading of UFactories/UExporters to 227j some time ago too.

Anyway, here you go:
Code
Select All
// Imports Palette in Adobe Color Table file format.
class HTK_API UPaletteFactoryACT : public UFactory
{
	DECLARE_HTK_CLASS(UPaletteFactoryACT,UFactory,0)

	// Constructors.
	UPaletteFactoryACT();
	void StaticConstructor();

	// UFactory interfacce.
	UObject* FactoryCreateBinary( UClass* Class, UObject* InParent, FName Name, DWORD Flags, UObject* Context, const TCHAR* Type, const BYTE*& Buffer, const BYTE* BufferEnd, FFeedbackContext* Warn );
};

// Exports Palette to Adobe Color Table file format.
class HTK_API UPaletteExporterACT : public UExporter
{
	DECLARE_HTK_CLASS(UPaletteExporterACT,UExporter,0)

	// Constructors.
	UPaletteExporterACT();
	void StaticConstructor();

	// UExporter interface.
	UBOOL ExportBinary( UObject* Object, const TCHAR* Type, FArchive& Ar, FFeedbackContext* Warn );
};
 



Code
Select All
/*=============================================================================
	UPaletteFactoryACT.cpp: Imports a Palette in Adobe Color Table format.
	Copyright 2016 Sebastian Kaufel. All Rights Reserved.

	Revision history:
		* Created by Sebastian Kaufel

	References:
		* http://www.adobe.com/devnet-apps/photoshop/fileformatashtml/#50577411_pgfId-1070626

	Notes:
		* They missed the "8 bytes" part in the documentation.
		* Format is RGB only.
=============================================================================*/

#include "HTK.h"

/*-----------------------------------------------------------------------------
	Defines.
-----------------------------------------------------------------------------*/

#define ACT_FILE_SIZE 768

/*-----------------------------------------------------------------------------
	Implementation.
-----------------------------------------------------------------------------*/

IMPLEMENT_CLASS(UPaletteFactoryACT);

/*-----------------------------------------------------------------------------
	Constructors.
-----------------------------------------------------------------------------*/

UPaletteFactoryACT::UPaletteFactoryACT()
{
	guard(UPaletteFactoryACT::UPaletteFactoryACT);
	unguard;
}
void UPaletteFactoryACT::StaticConstructor()
{
	guard(UPaletteFactoryACT::StaticConstructor);
	SupportedClass = UPalette::StaticClass();

	AutoPriority = 0;
	bCreateNew   = 0;
	bText        = 0;

	new(Formats)FString(TEXT("act;Adobe Color Table"));
	unguard;
}

/*-----------------------------------------------------------------------------
	UFactory interface.
-----------------------------------------------------------------------------*/

UObject* UPaletteFactoryACT::FactoryCreateBinary( UClass* Class, UObject* InParent, FName Name, DWORD Flags, UObject* Context, const TCHAR* Type, const BYTE*& Buffer, const BYTE* BufferEnd, FFeedbackContext* Warn )
{
	guard(UPaletteFactoryACT::FactoryCreateBinary);
	size_t Size = BufferEnd-Buffer;
	if ( Size==ACT_FILE_SIZE )
	{
		UPalette* Palette = new(InParent,Name,Flags) UPalette;
		Palette->Colors.Empty();
		Palette->Colors.AddZeroed( NUM_PAL_COLORS );
		for ( INT i=0; i<NUM_PAL_COLORS; i++ )
			Palette->Colors(i) = FColor(Buffer[i*3],Buffer[i*3+1],Buffer[i*3+2],255);
		return Palette;
	}
	// Don't import if size mismatches, but put out a warning.
	else
	{
		Warn->Logf( NAME_Warning, TEXT("Adobe Color Table file is %i bytes, should be %i bytes. Skipping Import."), Size, ACT_FILE_SIZE );
		return NULL;
	}
	unguard;
}

/*-----------------------------------------------------------------------------
	The End.
-----------------------------------------------------------------------------*/
 



Code
Select All
/*=============================================================================
	UPaletteExporterACT.cpp: Exports Palette to Adobe Color Table format.
	Copyright 2016 Sebastian Kaufel. All Rights Reserved.

	Revision history:
		* Created by Sebastian Kaufel

	References:
		* http://www.adobe.com/devnet-apps/photoshop/fileformatashtml/#50577411_pgfId-1070626

	Notes:
		* They missed the "8 bytes" part in the documentation.
		* Format is RGB only.
=============================================================================*/

#include "HTK.h"

/*-----------------------------------------------------------------------------
	Implementation.
-----------------------------------------------------------------------------*/

IMPLEMENT_CLASS(UPaletteExporterACT);

/*-----------------------------------------------------------------------------
	Constructors.
-----------------------------------------------------------------------------*/

UPaletteExporterACT::UPaletteExporterACT()
{
	guard(UPaletteExporterACT::UPaletteExporterACT);
	unguard;
}

void UPaletteExporterACT::StaticConstructor()
{
	guard(UPaletteExporterACT::StaticConstructor);

	// Set Supported Class.
	SupportedClass = UPalette::StaticClass();

	// Set Binary Mode.
	bText = 0;

	new(Formats) FString(TEXT("ACT"));
	unguard;
}

/*-----------------------------------------------------------------------------
	UExporter interface.
-----------------------------------------------------------------------------*/

UBOOL UPaletteExporterACT::ExportBinary( UObject* Object, const TCHAR* Type, FArchive& Ar, FFeedbackContext* Warn )
{
	guard(UPaletteExporterACT::ExportBinary);
	UPalette* Palette = Cast<UPalette>( Object );
	if ( Palette )
	{
		if ( Palette->Colors.Num()!=NUM_PAL_COLORS )
			Warn->Logf( NAME_Warning, TEXT("Found %i Palette Colors, should be %i."), Palette->Colors.Num(), NUM_PAL_COLORS );

		// Write Palette Contents.
		INT Index;
		for ( Index=0; Index<Min(Palette->Colors.Num(),256); Index++ )
			Ar.Serialize( &Palette->Colors(Index), 3 );

		// Write Black until end of ACT.
		DWORD Black=0;
		for ( Index; Index<256; Index++ )
			Ar.Serialize( &Black, 3 );
		return 1;
	}
	return 0;
	unguard;
}

/*-----------------------------------------------------------------------------
	The End.
-----------------------------------------------------------------------------*/
 



And finally the *.int entries for autoloading:
Code
Select All
[Public]
Object=(Name=HTK.PaletteFactoryACT,Class=Class,MetaClass=Core.Factory)
Object=(Name=HTK.PaletteExporterACT,Class=Class,MetaClass=Core.Exporter)
 



Notes:
  • The "Type" parameter in UFactory::FactoryCreateBinary/UExporter::ExportBinary contains the file extension.
  • For text files you would set UFactory::bText/UExporter::bText to 1 inside the StaticContructor and implement the UFactory::FactoryCreateText/UExporter::ExportText interface.

  

HX on Mod DB. Revision on Steam.
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint
Bookmarks: del.icio.us Digg Facebook Google Google+ Linked in reddit StumbleUpon Twitter Yahoo