/** * Write out the steam app id to the steam_appid.txt file before initializing the API * @param SteamAppId id assigned to the application by Steam */ static void WriteSteamAppIdToDisk(int32 SteamAppId) { if (SteamAppId > 0) { // Turn off sandbox temporarily to make sure file is where it's always expected FScopeSandboxContext ScopedSandbox(false); // Access the physical file writer directly so that we still write next to the executable in CotF builds. FString SteamAppIdFilename = GetSteamAppIdFilename(); IFileHandle* Handle = IPlatformFile::GetPlatformPhysical().OpenWrite(*SteamAppIdFilename, false, false); if (!Handle) { UE_LOG_ONLINE(Fatal, TEXT("Failed to create file: %s"), *SteamAppIdFilename); } else { FString AppId = FString::Printf(TEXT("%d"), SteamAppId); FBufferArchive Archive; Archive.Serialize((void*)TCHAR_TO_ANSI(*AppId), AppId.Len()); Handle->Write(Archive.GetData(), Archive.Num()); delete Handle; Handle = nullptr; } } }
bool FSocketMessageHeader::WrapAndSendPayload(const TArray<uint8>& Payload, FSocket* Socket) { FSocketMessageHeader Header(Payload); FBufferArchive Ar; Ar << Header.Magic; Ar << Header.PayloadSize; Ar.Append(Payload); int32 TotalAmountSent = 0; // How many bytes have been sent int32 AmountToSend = Ar.Num(); int NumTrial = 100; // Only try a limited amount of times // int ChunkSize = 4096; while (AmountToSend > 0) { int AmountSent = 0; // GetData returns a uint8 pointer Socket->Send(Ar.GetData() + TotalAmountSent, Ar.Num() - TotalAmountSent, AmountSent); NumTrial--; if (AmountSent == -1) { continue; } if (NumTrial < 0) { UE_LOG(LogUnrealCV, Error, TEXT("Unable to send. Expect to send %d, sent %d"), Ar.Num(), TotalAmountSent); return false; } UE_LOG(LogUnrealCV, Verbose, TEXT("Sending bytes %d/%d, sent %d"), TotalAmountSent, Ar.Num(), AmountSent); AmountToSend -= AmountSent; TotalAmountSent += AmountSent; } check(AmountToSend == 0); return true; }
bool FNFSMessageHeader::WrapAndSendPayload(const TArray<uint8>& Payload, const FSimpleAbstractSocket& Socket) { // make a header for the payload FNFSMessageHeader Header(Socket, Payload); // serialize out the header FBufferArchive Ar; Ar << Header; // append the payload bytes to send it in one network packet Ar.Append(Payload); // Send it, and make sure it sent it all if (!Socket.Send(Ar.GetData(), Ar.Num())) { UE_LOG(LogSockets, Error, TEXT("Unable to send.")); return false; } return true; }
bool UWizardsSaveGame::SaveGameDataToFile() { //Variable Data -> Binary FBufferArchive ToBinary; SaveLoadData(ToBinary, spellBook); //No Data if (ToBinary.Num() <= 0) return false; //~ //Binary to Hard Disk if (FFileHelper::SaveArrayToFile(ToBinary, *ThePath)) { // Free Binary Array ToBinary.FlushCache(); ToBinary.Empty(); return true; } // Free Binary Array ToBinary.FlushCache(); ToBinary.Empty(); return false; }
int32 UExporter::ExportToFileEx( FExportToFileParams& ExportParams ) { #if WITH_EDITOR check(ExportParams.Object); CurrentFilename = ExportParams.Filename; UExporter* Exporter = ExportParams.Exporter; FString Extension = FPaths::GetExtension(ExportParams.Filename); int32 Result = 0; 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( ExportParams.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, *(ExportParams.Object->GetFullName()) ); CurrentFilename = TEXT(""); return 0; } Exporter->bSelectedOnly = ExportParams.InSelectedOnly; FOutputDevice* TextBuffer = NULL; if( Exporter->bText ) { bool bIsFileDevice = false; FString TempFile = FPaths::GetPath(ExportParams.Filename); if (Exporter->bForceFileOperations || ExportParams.bUseFileArchive) { IFileManager::Get().MakeDirectory(*TempFile); TempFile += TEXT("/UnrealExportFile.tmp"); TextBuffer = new FOutputDeviceFile(*TempFile); if (TextBuffer) { TextBuffer->SetSuppressEventTag(true); TextBuffer->SetAutoEmitLineTerminator(false); bIsFileDevice = true; } } if (TextBuffer == NULL) { if (ExportParams.bUseFileArchive) { UE_LOG(LogExporter, Warning, TEXT("Failed to create file output device... defaulting to string buffer")); } TextBuffer = new FStringOutputDevice(); } const FExportObjectInnerContext Context(ExportParams.IgnoreObjectList); ExportToOutputDevice( &Context, ExportParams.Object, Exporter, *TextBuffer, *Extension, 0, PPF_ExportsNotFullyQualified, ExportParams.InSelectedOnly ); if (bIsFileDevice) { TextBuffer->TearDown(); IFileManager::Get().Move(ExportParams.Filename, *TempFile, 1, 1); } else { FStringOutputDevice& StringBuffer = *((FStringOutputDevice*)TextBuffer); if ( StringBuffer.Len() == 0 ) { Result = -1; } else { if( ExportParams.NoReplaceIdentical ) { FString FileBytes; if ( FFileHelper::LoadFileToString(FileBytes,ExportParams.Filename) && FCString::Strcmp(*StringBuffer,*FileBytes)==0 ) { UE_LOG(LogExporter, Log, TEXT("Not replacing %s because identical"), ExportParams.Filename ); Result = 1; goto Done; } if( ExportParams.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( ExportParams.Filename ) ) ) ) { Result = 1; goto Done; } } } if( !FFileHelper::SaveStringToFile( StringBuffer, ExportParams.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( ExportParams.Filename ) ) ) ) { IFileManager::Get().Delete( ExportParams.Filename, 0, 1 ); if( FFileHelper::SaveStringToFile( StringBuffer, ExportParams.Filename ) ) { Result = 1; goto Done; } } #endif UE_LOG(LogExporter, Error, TEXT("%s"), *FString::Printf( TEXT("Error exporting %s: couldn't open file '%s'"), *(ExportParams.Object->GetFullName()), ExportParams.Filename)); goto Done; } Result = 1; } } } else { for( int32 i = 0; i < Exporter->GetFileCount(); i++ ) { FBufferArchive Buffer; if( ExportToArchive( ExportParams.Object, Exporter, Buffer, *Extension, i ) ) { FString UniqueFilename = Exporter->GetUniqueFilename( ExportParams.Filename, i ); if( ExportParams.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( ExportParams.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 ( !ExportParams.WriteEmptyFiles && !Buffer.Num() ) { Result = 1; goto Done; } if( !FFileHelper::SaveArrayToFile( Buffer, *UniqueFilename ) ) { UE_LOG(LogExporter, Error, TEXT("Error exporting %s: couldn't open file '%s'"), *(ExportParams.Object->GetFullName()), *UniqueFilename); goto Done; } } } Result = 1; } Done: if ( TextBuffer != NULL ) { delete TextBuffer; TextBuffer = NULL; } CurrentFilename = TEXT(""); return Result; #else return 0; #endif }
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 }
bool FNetworkFileServerClientConnection::ProcessPayload(FArchive& Ar) { FBufferArchive Out; bool Result = true; // first part of the payload is always the command uint32 Cmd; Ar << Cmd; UE_LOG(LogFileServer, Verbose, TEXT("Processing payload with Cmd %d"), Cmd); // what type of message is this? NFS_Messages::Type Msg = NFS_Messages::Type(Cmd); // make sure the first thing is GetFileList which initializes the game/platform checkf(Msg == NFS_Messages::GetFileList || Msg == NFS_Messages::Heartbeat || Sandbox != NULL, TEXT("The first client message MUST be GetFileList, not %d"), (int32)Msg); // process the message! bool bSendUnsolicitedFiles = false; { FScopeLock SocketLock(&SocketCriticalSection); switch (Msg) { case NFS_Messages::OpenRead: ProcessOpenFile(Ar, Out, false); break; case NFS_Messages::OpenWrite: ProcessOpenFile(Ar, Out, true); break; case NFS_Messages::Read: ProcessReadFile(Ar, Out); break; case NFS_Messages::Write: ProcessWriteFile(Ar, Out); break; case NFS_Messages::Seek: ProcessSeekFile(Ar, Out); break; case NFS_Messages::Close: ProcessCloseFile(Ar, Out); break; case NFS_Messages::MoveFile: ProcessMoveFile(Ar, Out); break; case NFS_Messages::DeleteFile: ProcessDeleteFile(Ar, Out); break; case NFS_Messages::GetFileInfo: ProcessGetFileInfo(Ar, Out); break; case NFS_Messages::CopyFile: ProcessCopyFile(Ar, Out); break; case NFS_Messages::SetTimeStamp: ProcessSetTimeStamp(Ar, Out); break; case NFS_Messages::SetReadOnly: ProcessSetReadOnly(Ar, Out); break; case NFS_Messages::CreateDirectory: ProcessCreateDirectory(Ar, Out); break; case NFS_Messages::DeleteDirectory: ProcessDeleteDirectory(Ar, Out); break; case NFS_Messages::DeleteDirectoryRecursively: ProcessDeleteDirectoryRecursively(Ar, Out); break; case NFS_Messages::ToAbsolutePathForRead: ProcessToAbsolutePathForRead(Ar, Out); break; case NFS_Messages::ToAbsolutePathForWrite: ProcessToAbsolutePathForWrite(Ar, Out); break; case NFS_Messages::ReportLocalFiles: ProcessReportLocalFiles(Ar, Out); break; case NFS_Messages::GetFileList: Result = ProcessGetFileList(Ar, Out); break; case NFS_Messages::Heartbeat: ProcessHeartbeat(Ar, Out); break; case NFS_Messages::SyncFile: ProcessSyncFile(Ar, Out); bSendUnsolicitedFiles = true; break; case NFS_Messages::RecompileShaders: ProcessRecompileShaders(Ar, Out); break; default: UE_LOG(LogFileServer, Error, TEXT("Bad incomming message tag (%d)."), (int32)Msg); } } // send back a reply if the command wrote anything back out if (Out.Num() && Result ) { int32 NumUnsolictedFiles = UnsolictedFiles.Num(); if (bSendUnsolicitedFiles) { Out << NumUnsolictedFiles; } UE_LOG(LogFileServer, Verbose, TEXT("Returning payload with %d bytes"), Out.Num()); // send back a reply Result &= SendPayload( Out ); if (bSendUnsolicitedFiles && Result ) { for (int32 Index = 0; Index < NumUnsolictedFiles; Index++) { FBufferArchive OutUnsolicitedFile; PackageFile(UnsolictedFiles[Index], OutUnsolicitedFile); UE_LOG(LogFileServer, Display, TEXT("Returning unsolicited file %s with %d bytes"), *UnsolictedFiles[Index], OutUnsolicitedFile.Num()); Result &= SendPayload(OutUnsolicitedFile); } UnsolictedFiles.Empty(); } } UE_LOG(LogFileServer, Verbose, TEXT("Done Processing payload with Cmd %d Total Size sending %d "), Cmd,Out.TotalSize()); return Result; }
bool FHTTPTransport::SendPayloadAndReceiveResponse(TArray<uint8>& In, TArray<uint8>& Out) { RecieveBuffer.Empty(); ReadPtr = 0; #if !PLATFORM_HTML5 if (GIsRequestingExit) // We have already lost HTTP Module. return false; class HTTPRequestHandler { public: HTTPRequestHandler(TArray<uint8>& InOut) :Out(InOut) {} void HttpRequestComplete( FHttpRequestPtr HttpRequest, FHttpResponsePtr HttpResponse, bool bSucceeded) { if (HttpResponse.IsValid()) Out.Append(HttpResponse->GetContent()); } private: TArray<uint8>& Out; }; HTTPRequestHandler Handler(RecieveBuffer); HttpRequest->OnProcessRequestComplete().BindRaw(&Handler,&HTTPRequestHandler::HttpRequestComplete ); if ( In.Num() ) { HttpRequest->SetVerb("POST"); FBufferArchive Ar; Ar << Guid; Ar.Append(In); HttpRequest->SetContent(Ar); } else { HttpRequest->SetVerb("GET"); } HttpRequest->ProcessRequest(); FDateTime StartTime; FTimespan Span = FDateTime::UtcNow() - StartTime; while( HttpRequest->GetStatus() != EHttpRequestStatus::Failed && HttpRequest->GetStatus() != EHttpRequestStatus::Succeeded && Span.GetSeconds() < 10 ) { HttpRequest->Tick(0); Span = FDateTime::UtcNow() - StartTime; } if (HttpRequest->GetStatus() == EHttpRequestStatus::Succeeded) return true; HttpRequest->CancelRequest(); return false; #else // PLATFORM_HTML5 FBufferArchive Ar; if ( In.Num() ) { Ar << Guid; } Ar.Append(In); unsigned char *OutData = NULL; unsigned int OutSize= 0; bool RetVal = true; #if PLATFORM_HTML5_WIN32 RetVal = HTML5Win32::NFSHttp::SendPayLoadAndRecieve(Ar.GetData(), Ar.Num(), &OutData, OutSize); #endif #if PLATFORM_HTML5_BROWSER UE_SendAndRecievePayLoad(TCHAR_TO_ANSI(Url),(char*)Ar.GetData(),Ar.Num(),(char**)&OutData,(int*)&OutSize); #endif if (!Ar.Num()) { uint32 Size = OutSize; uint32 Marker = 0xDeadBeef; RecieveBuffer.Append((uint8*)&Marker,sizeof(uint32)); RecieveBuffer.Append((uint8*)&Size,sizeof(uint32)); } if (OutSize) { RecieveBuffer.Append(OutData,OutSize); #if PLATFORM_HTML5_WIN32 free (OutData); #endif #if PLATFORM_HTML5_BROWSER // don't go through the Unreal Memory system. ::free(OutData); #endif } return RetVal & ReceiveResponse(Out); #endif }