bool UDiffAssetsCommandlet::ExportFile(const FString& Filename, const TArray<UObject *>& LoadedObjects)
{
	FString Extension = TEXT("t3d");
	FStringOutputDevice Buffer;
	const FExportObjectInnerContext Context;
	for (int32 Index = 0; Index < LoadedObjects.Num(); Index++)
	{
		UExporter* Exporter = UExporter::FindExporter( LoadedObjects[Index], *Extension );
		if (!Exporter)
		{
			UE_LOG(LogDiffAssetsCommandlet, Warning, TEXT("Could not find exporter."));
			return false;
		}
		UExporter::ExportToOutputDevice( &Context, LoadedObjects[Index], Exporter, Buffer, *Extension, 0, PPF_ExportsNotFullyQualified, false );
		TMap<FString,FString> NativePropertyValues;
		if ( LoadedObjects[Index]->GetNativePropertyValues(NativePropertyValues) && NativePropertyValues.Num())
		{
			int32 LargestKey = 0;
			for ( TMap<FString,FString>::TIterator It(NativePropertyValues); It; ++It )
			{
				LargestKey = FMath::Max(LargestKey, It.Key().Len());
			}
			for ( TMap<FString,FString>::TIterator It(NativePropertyValues); It; ++It )
			{
				Buffer.Logf(TEXT("  %s=%s"), *It.Key().RightPad(LargestKey), *It.Value());
			}
		}
	}
	if (!Buffer.Len())
	{
		UE_LOG(LogDiffAssetsCommandlet, Warning, TEXT("No text was exported!"));
		return false;
	}
	if( !FFileHelper::SaveStringToFile( Buffer, *Filename ) )
	{
		UE_LOG(LogDiffAssetsCommandlet, Warning, TEXT("Could not write %s"), *Filename);
		return false;
	}
	return true;
}
示例#2
0
int32 UExporter::ExportToFile( UObject* Object, UExporter* InExporter, const TCHAR* Filename, bool InSelectedOnly, bool NoReplaceIdentical, bool Prompt )
{
#if WITH_EDITOR
    check(Object);

    CurrentFilename = Filename;

    UExporter*	Exporter	= InExporter;
    int32		Result		= 0;
    FString		Extension;

    if (!Exporter)
    {
        // look for an exporter with all possible extensions, so an exporter can have something like *.xxx.yyy as an extension
        int32 SearchStart = 0;
        int32 DotLocation;
        while (!Exporter && (DotLocation = CurrentFilename.Find(TEXT("."), ESearchCase::CaseSensitive, ESearchDir::FromStart, SearchStart)) != INDEX_NONE)
        {
            // get everything after the current .
            Extension = CurrentFilename.Mid(DotLocation + 1);

            // try to find an exporter with it
            Exporter = FindExporter( Object, *Extension );

            // skip past the dot in case we look again
            SearchStart = DotLocation + 1;
        }
    }

    if( !Exporter )
    {
        UE_LOG(LogExporter, Warning, TEXT("No %s exporter found for %s"), *Extension, *Object->GetFullName() );
        CurrentFilename = TEXT("");
        return 0;
    }

    Exporter->bSelectedOnly = InSelectedOnly;

    if( Exporter->bText )
    {
        FStringOutputDevice Buffer;
        const FExportObjectInnerContext Context;
        ExportToOutputDevice( &Context, Object, Exporter, Buffer, *Extension, 0, PPF_ExportsNotFullyQualified, InSelectedOnly );
        if ( Buffer.Len() == 0 )
        {
            Result = -1;
        }
        else
        {
            if( NoReplaceIdentical )
            {
                FString FileBytes;
                if ( FFileHelper::LoadFileToString(FileBytes,Filename) && FCString::Strcmp(*Buffer,*FileBytes) == 0 )
                {
                    UE_LOG(LogExporter, Log,  TEXT("Not replacing %s because identical"), Filename );
                    Result = 1;
                    goto Done;
                }

                if( Prompt )
                {
                    if( !GWarn->YesNof( FText::Format( NSLOCTEXT("Core", "Overwrite", "The file '{0}' needs to be updated.  Do you want to overwrite the existing version?"), FText::FromString( Filename ) ) ) )
                    {
                        Result = 1;
                        goto Done;
                    }
                }
            }
            if( !FFileHelper::SaveStringToFile( Buffer, Filename ) )
            {
#if 0
                if( GWarn->YesNof( FText::Format( NSLOCTEXT("Core", "OverwriteReadOnly", "'{0}' is marked read-only.  Would you like to try to force overwriting it?"), FText::FromString( Filename ) ) ) )
                {
                    IFileManager::Get().Delete( Filename, 0, 1 );
                    if( FFileHelper::SaveStringToFile( Buffer, Filename ) )
                    {
                        Result = 1;
                        goto Done;
                    }
                }
#endif
                UE_LOG(LogExporter, Error, TEXT("Error exporting %s: couldn't open file '%s'"), *Object->GetFullName(), Filename);
                goto Done;
            }
            Result = 1;
        }
    }
    else
    {
        for( int32 i = 0; i < Exporter->GetFileCount(); i++ )
        {
            FBufferArchive Buffer;
            if( ExportToArchive( Object, Exporter, Buffer, *Extension, i ) )
            {
                FString UniqueFilename = Exporter->GetUniqueFilename( Filename, i );

                if( NoReplaceIdentical )
                {
                    TArray<uint8> FileBytes;

                    if(	FFileHelper::LoadFileToArray( FileBytes, *UniqueFilename )
                            &&	FileBytes.Num() == Buffer.Num()
                            &&	FMemory::Memcmp( &FileBytes[ 0 ], &Buffer[ 0 ], Buffer.Num() ) == 0 )
                    {
                        UE_LOG(LogExporter, Log,  TEXT( "Not replacing %s because identical" ), *UniqueFilename );
                        Result = 1;
                        goto Done;
                    }
                    if( Prompt )
                    {
                        if( !GWarn->YesNof( FText::Format( NSLOCTEXT("Core", "Overwrite", "The file '{0}' needs to be updated.  Do you want to overwrite the existing version?"), FText::FromString( UniqueFilename ) ) ) )
                        {
                            Result = 1;
                            goto Done;
                        }
                    }
                }

                if( !FFileHelper::SaveArrayToFile( Buffer, *UniqueFilename ) )
                {
                    UE_LOG(LogExporter, Error, TEXT("Error exporting %s: couldn't open file '%s'"), *Object->GetFullName(), *UniqueFilename);
                    goto Done;
                }
            }
        }
        Result = 1;
    }
Done:
    CurrentFilename = TEXT("");

    return Result;
#else
    return 0;
#endif
}