Download

Airsim settings.json directory issue

Hey guys! Hope you’re doing great!

i’m currently working with AirSim plugin for autonomous vehicles and trying to find a way where i can reference the settings.json file within a project instead of referencing it from documents/airsim because we have 3 diiferent projects on airsim but only one settings.json file in documents.

i would really appreciate it if you could guide me on how do i inculcate the settings.json file in the project referred in the project itself.

i tried changing C++ code of airsim directory where it was referred in visual studio and hard coded the reference of new copy of settings.json file but it still refers to the settings.json file in documents/airsim.

I also tried creating a symbolic link between settings.json in Documents/AirSim to settings.json in project directory but it still needs to have a setting file outside the project directory.

Settings.json file is included in the project directory itself and theoretically it should refer the file from project folder itself instead of creating a new file at documents/Airsim.

I have tweaked some code while i working on it, below are the details :

1 :In Settings.hpp i tried changing the filepath and hoped that it would be referred in project directory but it still checks for settings.json in Documents/AirSim

Settings.hpp line 23 full_filepath

{

class Settings {

private:

std::string full_filepath_ = “”;

nlohmann::json doc_;

bool load_success_ = false;

}

TO

{

class Settings {

private:

std::string full_filepath_ = “C:\Users\earth\Documents\Unreal Projects\AirSimSettings\Plugins\AirSim\settings.json”;

nlohmann::json doc_;

bool load_success_ = false;

}

  1. In SimHUD.cpp i tried setting the variable value to filepath instead of pointer reference but it lead to no change in the reference and changing the pointer gives error that it can’t convert Fstring to TChar

SimHUD.cpp line 386 readSettingsTextFromFile

{

bool ASimHUD::readSettingsTextFromFile(FString settingsFilepath, std::string& settingsText)

{

bool found = FPaths::FileExists(settingsFilepath);

if (found) {

   FString settingsTextFStr;

bool readSuccessful = FFileHelper::LoadFileToString(settingsTextFStr, *settingsFilepath);

   if (readSuccessful) {

       UAirBlueprintLib::LogMessageString("Loaded settings from ", TCHAR_TO_UTF8(*settingsFilepath), LogDebugLevel::Informational);

       settingsText = TCHAR_TO_UTF8(*settingsTextFStr);

   }

   else {

       UAirBlueprintLib::LogMessageString("Cannot read file ", TCHAR_TO_UTF8(*settingsFilepath), LogDebugLevel::Failure);

       throw std::runtime_error("Cannot read settings file.");

}

TO

{

bool ASimHUD::readSettingsTextFromFile(FString settingsFilepath, std::string& settingsText)

{

*settingsFilepath = “C:\Users\earth\Documents\AirSim\Unreal\Environments\Blocks\Plugins\AirSim\settings.json”;

bool found = FPaths::FileExists(settingsFilepath);

if (found) {

   FString settingsTextFStr;

bool readSuccessful = FFileHelper::LoadFileToString(settingsTextFStr, *settingsFilepath);

   if (readSuccessful) {

       UAirBlueprintLib::LogMessageString("Loaded settings from ", TCHAR_TO_UTF8(*settingsFilepath), LogDebugLevel::Informational);

       settingsText = TCHAR_TO_UTF8(*settingsTextFStr);

   }

   else {

       UAirBlueprintLib::LogMessageString("Cannot read file ", TCHAR_TO_UTF8(*settingsFilepath), LogDebugLevel::Failure);

       throw std::runtime_error("Cannot read settings file.");

}

  1. in AirSimSettings.hpp i changed the code to have a hard reference to the directory of new settings.json, the issue doesn’t get fixed but it does one thing that changed and that is now it generates the new settings.json when it is not there to new path by default

AirSimSettings.hpp line 396 settings_filename

{

static void createDefaultSettingsFile()

{

std::string settings_filename = Settings::getUserDirectoryFullPath(“settings.json”);

   Settings& settings_json = Settings::loadJSonString("{}");

   //write some settings_json in new file otherwise the string "null" is written if all settings_json are empty

   settings_json.setString("SeeDocsAt", "https://github.com/Microsoft/AirSim/blob/master/docs/settings.md");

   settings_json.setDouble("SettingsVersion", 1.2);

}

TO

{

static void createDefaultSettingsFile()

{

std::string settings_filename = “C:\Users\earth\Documents\Unreal Projects\AirSimSettings\Plugins\AirSim\settings.json”;

//settings_filename = Settings::getUserDirectoryFullPath(“settings.json”)

   Settings& settings_json = Settings::loadJSonString("{}");

   //write some settings_json in new file otherwise the string "null" is written if all settings_json are empty

   settings_json.setString("SeeDocsAt", "https://github.com/Microsoft/AirSim/blob/master/docs/settings.md");

   settings_json.setDouble("SettingsVersion", 1.2);

}

  1. In Settings.hpp i changed the string path for getUserDirectoryFullPath() and getExecutableFullPath() but it still refers to old settings.json in Documents/AirSim

Settings.hpp line 44 path

{

static std::string getUserDirectoryFullPath(std::string fileName)

{

   std::string path = common_utils::FileSystem::getAppDataFolder();

   return common_utils::FileSystem::combine(path, fileName);

}

static std::string getExecutableFullPath(std::string fileName)

{

   std::string path = common_utils::FileSystem::getExecutableFolder();

   return common_utils::FileSystem::combine(path, fileName);

}

TO

{

static std::string getUserDirectoryFullPath(std::string fileName)

{

   std::string path = "C:\\Users\\earth\\Documents\\Unreal Projects\\AirSimSettings\\Plugins\\AirSim\";

        //common_utils::FileSystem::getAppDataFolder();

   return common_utils::FileSystem::combine(path, fileName);

}

static std::string getExecutableFullPath(std::string fileName)

{

   std::string path = "C:\\Users\\earth\\Documents\\Unreal Projects\\AirSimSettings\\Plugins\\AirSim\";

        //common_utils::FileSystem::getExecutableFolder();

   return common_utils::FileSystem::combine(path, fileName);

}

and these are the changes i’ve made, please let me know how i can help in any way.

This solved the issue by changing the pointer reference to an environment variable as it was returning an error for conversion of FString to TChar but using environment variable solved the issue.

One more thing to notice here is the environment variable returns Tchar which is type needed for referencing the address of the settings.json file.

bool ASimHUD::readSettingsTextFromFile(FString settingsFilepath, std::string& settingsText)
{
FString customSettingsFilepath = getenv(“DUALITY_SETTINGS”);
// FString(TEXT(“C:\Users\earth\Documents\Unreal Projects\AirSimSettings\Plugins\AirSim\settings.json”));

bool found = FPaths::FileExists(customSettingsFilepath);
if (found) {
    FString settingsTextFStr;
    bool readSuccessful = FFileHelper::LoadFileToString(settingsTextFStr, *customSettingsFilepath); //TCHAR_TO_UTF8(*customSettingsFilepath)); //*
    if (readSuccessful) {
        UAirBlueprintLib::LogMessageString("Loaded settings from ", TCHAR_TO_UTF8(*customSettingsFilepath), LogDebugLevel::Informational); //* 
        settingsText = TCHAR_TO_UTF8(*settingsTextFStr);
    }
    else {
        UAirBlueprintLib::LogMessageString("Cannot read file ", TCHAR_TO_UTF8(*customSettingsFilepath), LogDebugLevel::Failure); //* 
        throw std::runtime_error("Cannot read settings file.");
    }
}

return found;

}

Thanks for the support!