CARLA
CustomFileDownloader.cpp
Go to the documentation of this file.
1 // Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma de Barcelona (UAB). This work is licensed under the terms of the MIT license. For a copy, see <https://opensource.org/licenses/MIT>.
2 
3 #undef CreateDirectory
4 
6 #include "HttpModule.h"
7 #include "Http.h"
8 #include "Misc/FileHelper.h"
9 
10 #include <OSM2ODR.h>
11 
12 void UCustomFileDownloader::ConvertOSMInOpenDrive(FString FilePath, float Lat_0, float Lon_0)
13 {
14  IPlatformFile &FileManager = FPlatformFileManager::Get().GetPlatformFile();
15 
16  FString FileContent;
17  // Always first check if the file that you want to manipulate exist.
18  if (FileManager.FileExists(*FilePath))
19  {
20  // We use the LoadFileToString to load the file into
21  if (FFileHelper::LoadFileToString(FileContent, *FilePath, FFileHelper::EHashOptions::None))
22  {
23  UE_LOG(LogCarla, Warning, TEXT("FileManipulation: Text From File: %s"), *FilePath);
24  }
25  else
26  {
27  UE_LOG(LogCarla, Warning, TEXT("FileManipulation: Did not load text from file"));
28  }
29  }
30  else
31  {
32  UE_LOG(LogCarla, Warning, TEXT("File: %s does not exist"), *FilePath);
33  return;
34  }
35  std::string OsmFile = std::string(TCHAR_TO_UTF8(*FileContent));
36 
37  osm2odr::OSM2ODRSettings Settings;
38  Settings.proj_string += " +lat_0=" + std::to_string(Lat_0) + " +lon_0=" + std::to_string(Lon_0);
39  Settings.center_map = false;
40  std::string OpenDriveFile = osm2odr::ConvertOSMToOpenDRIVE(OsmFile, Settings);
41 
42  FilePath.RemoveFromEnd(".osm", ESearchCase::Type::IgnoreCase);
43  FilePath += ".xodr";
44 
45  // We use the LoadFileToString to load the file into
46  if (FFileHelper::SaveStringToFile(FString(OpenDriveFile.c_str()), *FilePath))
47  {
48  UE_LOG(LogCarla, Warning, TEXT("FileManipulation: Sucsesfuly Written: \"%s\" to the text file"), *FilePath);
49  }
50  else
51  {
52  UE_LOG(LogCarla, Warning, TEXT("FileManipulation: Failed to write FString to file."));
53  }
54 }
55 
57 {
58  UE_LOG(LogCarla, Log, TEXT("FHttpDownloader CREATED"));
60  Download->Run();
61 }
62 
63 FHttpDownloader::FHttpDownloader(const FString &InVerb, const FString &InUrl, const FString &InFilename, FDownloadComplete &Delegate)
64  : Verb(InVerb), Url(InUrl), Filename(InFilename), DelegateToCall(Delegate)
65 {
66 }
67 
69 {
70  UE_LOG(LogCarla, Log, TEXT("Starting download [%s] Url=[%s]"), *Verb, *Url);
71  TSharedPtr<IHttpRequest, ESPMode::ThreadSafe> Request = FHttpModule::Get().CreateRequest();
72  Request->OnProcessRequestComplete().BindRaw(this, &FHttpDownloader::RequestComplete);
73  Request->SetURL(Url);
74  Request->SetVerb(Verb);
75  Request->ProcessRequest();
76 }
77 
78 void FHttpDownloader::RequestComplete(FHttpRequestPtr HttpRequest, FHttpResponsePtr HttpResponse, bool bSucceeded)
79 {
80  if (!HttpResponse.IsValid())
81  {
82  UE_LOG(LogCarla, Log, TEXT("Download failed. NULL response"));
83  }
84  else
85  {
86  // If we do not get success responses codes we do not do anything
87  if (HttpResponse->GetResponseCode() < 200 || 300 <= HttpResponse->GetResponseCode())
88  {
89  UE_LOG(LogCarla, Error, TEXT("Error during download [%s] Url=[%s] Response=[%d]"),
90  *HttpRequest->GetVerb(),
91  *HttpRequest->GetURL(),
92  HttpResponse->GetResponseCode());
93  delete this;
94  return;
95  }
96 
97  UE_LOG(LogCarla, Log, TEXT("Completed download [%s] Url=[%s] Response=[%d]"),
98  *HttpRequest->GetVerb(),
99  *HttpRequest->GetURL(),
100  HttpResponse->GetResponseCode());
101 
102  HttpRequest->OnProcessRequestComplete().Unbind();
103 
104  FString CurrentFile = FPaths::ProjectContentDir() + "CustomMaps/" + Filename + "/OpenDrive/";
105 
106  // We will use this FileManager to deal with the file.
107  IPlatformFile &FileManager = FPlatformFileManager::Get().GetPlatformFile();
108  if (!FileManager.DirectoryExists(*CurrentFile))
109  {
110  FileManager.CreateDirectory(*CurrentFile);
111  }
112  CurrentFile += Filename + ".osm";
113 
114  FString StringToWrite = HttpResponse->GetContentAsString();
115 
116  // We use the LoadFileToString to load the file into
117  if (FFileHelper::SaveStringToFile(StringToWrite, *CurrentFile, FFileHelper::EEncodingOptions::ForceUTF8WithoutBOM))
118  {
119  UE_LOG(LogCarla, Warning, TEXT("FileManipulation: Sucsesfuly Written "));
120  }
121  else
122  {
123  UE_LOG(LogCarla, Warning, TEXT("FileManipulation: Failed to write FString to file."));
124  }
125  }
126  DelegateToCall.ExecuteIfBound();
127 
128  delete this;
129 }
FHttpDownloader(const FString &InVerb, const FString &InUrl, const FString &InFilename, FDownloadComplete &Delegate)
void ConvertOSMInOpenDrive(FString FilePath, float Lat_0=0, float Lon_0=0)
static T Get(carla::rpc::Response< T > &response)
FDownloadComplete DelegateToCall
FDownloadComplete DownloadDelegate
void RequestComplete(FHttpRequestPtr HttpRequest, FHttpResponsePtr HttpResponse, bool bSucceeded)
Delegate called when the request completes.