For direct access use https://forums.oldunreal.com
It's been quite a while since oldunreal had an overhaul, but we are moving to another server which require some updates and changes. The biggest change is the migration of our old reliable YaBB forum to phpBB. This system expects you to login with your username and old password known from YaBB.
If you experience any problems there is also the usual "password forgotten" function. Don't forget to clear your browser cache!
If you have any further concerns feel free to contact me: Smirftsch@oldunreal.com

[Snippet] UExporter/UFactory for binary resources

Post Reply
User avatar
han
Global Moderator
Posts: 686
Joined: Wed Dec 10, 2014 12:38 am

[Snippet] UExporter/UFactory for binary resources

Post by han »

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 [url=http://www.oldunreal.com/cgi-bin/yabb2/YaBB.pl?num=1445541914]HP1 PubSrc[/url] 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.
Last edited by han on Sat Aug 06, 2016 12:49 am, edited 1 time in total.
HX on Mod DB. Revision on Steam. Löffels on Patreon.
Post Reply
  • Similar Topics
    Replies
    Views
    Last post

Return to “C++ Native Mods for UE1”