Advertisement
Guest User

PersistCar

a guest
Apr 21st, 2019
576
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 15.14 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include "common.hpp"
  4. #include "natives.hpp"
  5. #include "VehicleHelper.h"
  6. #include "ModelAttachment.h"
  7.  
  8. namespace big
  9. {
  10.     class persistcar
  11.     {
  12.     public:
  13.         static void save_vehicle(Vehicle vehicle, std::string file_name)
  14.         {
  15.             if (ENTITY::DOES_ENTITY_EXIST(vehicle) == FALSE || ENTITY::IS_ENTITY_A_VEHICLE(vehicle) == FALSE)
  16.             {
  17.                 LOG_ERROR("Persist Car was passed an incorrect entity");
  18.                 return;
  19.             }
  20.  
  21.             auto file_path = check_vehicle_folder();
  22.             file_path /= file_name;
  23.             std::ofstream file(file_path, std::ios::out | std::ios::trunc);
  24.             file << get_full_vehicle_json(vehicle).dump(4);
  25.             file.close();
  26.         }
  27.  
  28.         static Vehicle load_vehicle(std::string file_name)
  29.         {
  30.             auto file_path = check_vehicle_folder();
  31.             file_path /= file_name;
  32.             std::ifstream file(file_path);
  33.  
  34.             nlohmann::json vehicle_json;
  35.             file >> vehicle_json;
  36.             file.close();
  37.  
  38.             return spawn_vehicle_full(vehicle_json, PLAYER::PLAYER_PED_ID());
  39.         }
  40.  
  41.         static std::vector<std::string> list_files()
  42.         {
  43.             auto file_path = check_vehicle_folder();
  44.             std::vector<std::string> return_value;
  45.             for (const auto& p : std::filesystem::directory_iterator(file_path))
  46.                 if (p.path().extension() == ".json")
  47.                     return_value.push_back(p.path().filename().generic_string());
  48.             return return_value;
  49.         }
  50.  
  51.         static Vehicle clone_ped_car(Ped ped)
  52.         {
  53.             if (PED::IS_PED_IN_ANY_VEHICLE(ped, FALSE))
  54.                 return spawn_vehicle_full(get_full_vehicle_json(PED::GET_VEHICLE_PED_IS_IN(ped, FALSE)), ped);
  55.             return NULL;
  56.         }
  57.  
  58.     private:
  59.         static Vehicle spawn_vehicle_full(nlohmann::json vehicle_json, Ped ped)
  60.         {
  61.             Vehicle vehicle = spawn_vehicle(vehicle_json, ped);
  62.             if (!vehicle_json["Tow"].is_null())
  63.             {
  64.                 Vehicle tow = spawn_vehicle(vehicle_json["Tow"], ped);
  65.                 VEHICLE::ATTACH_VEHICLE_TO_TRAILER(vehicle, tow, 1.0f);
  66.             }
  67.             VEHICLE::SET_VEHICLE_ON_GROUND_PROPERLY(vehicle);
  68.             return vehicle;
  69.         }
  70.  
  71.         static Vehicle spawn_vehicle(nlohmann::json vehicle_json, Ped ped)
  72.         {
  73.             Vehicle vehicle = spawn_vehicle_json(vehicle_json, ped);
  74.             std::vector<nlohmann::json> model_attachments = vehicle_json["Model Attachments"];
  75.             for (nlohmann::json model_attachment : model_attachments)
  76.             {
  77.                 auto attachment = model_attachment.get<model_attachment::model_attachment>();
  78.                 STREAMING::REQUEST_MODEL(attachment.model_hash);
  79.                 Object object = OBJECT::CREATE_OBJECT(attachment.model_hash, 0, 0, 0, TRUE, FALSE, FALSE);
  80.                 ENTITY::ATTACH_ENTITY_TO_ENTITY(object, vehicle, 0, attachment.position.x, attachment.position.y, attachment.position.z, attachment.rotation.x, attachment.rotation.y, attachment.rotation.z, false, false, false, false, 0, true);
  81.                 STREAMING::SET_MODEL_AS_NO_LONGER_NEEDED(attachment.model_hash);
  82.             }
  83.             std::vector<nlohmann::json> vehicle_attachments = vehicle_json["Vehicle Attachments"];
  84.             for (nlohmann::json vehicle_attachment : vehicle_attachments)
  85.             {
  86.                 Vehicle vehicle_to_attach = spawn_vehicle_json(vehicle_attachment["Vehicle"], ped);
  87.                 auto attachment = vehicle_attachment["Model Attachment"].get<model_attachment::model_attachment>();
  88.                 ENTITY::ATTACH_ENTITY_TO_ENTITY(vehicle_to_attach, vehicle, 0, attachment.position.x, attachment.position.y, attachment.position.z, attachment.rotation.x, attachment.rotation.y, attachment.rotation.z, false, false, false, false, 0, true);
  89.                 VEHICLE::SET_VEHICLE_IS_CONSIDERED_BY_PLAYER(vehicle_to_attach, false);
  90.             }
  91.             return vehicle;
  92.         }
  93.  
  94.         static Vehicle spawn_vehicle_json(nlohmann::json vehicle_json, Ped ped)
  95.         {
  96.             Hash vehicle_hash = vehicle_json["Vehicle Model Hash"];
  97.  
  98.             auto pos = ENTITY::GET_ENTITY_COORDS(PLAYER::PLAYER_PED_ID(), true);
  99.             Vehicle vehicle = CreateVehicle(vehicle_hash, pos.x, pos.y, pos.z, ENTITY::GET_ENTITY_HEADING(ped));
  100.             VEHICLE::SET_VEHICLE_DIRT_LEVEL(vehicle, 0.0f);
  101.             VEHICLE::SET_VEHICLE_MOD_KIT(vehicle, 0);
  102.             VEHICLE::SET_VEHICLE_TYRES_CAN_BURST(vehicle, FALSE);
  103.             VEHICLE::SET_VEHICLE_COLOURS(vehicle, vehicle_json["Primary Color"], vehicle_json["Secondary Color"]);
  104.             if (!vehicle_json["Custom Primary Color"].is_null())
  105.             {
  106.                 std::vector<int> primary_custom_color = vehicle_json["Custom Primary Color"];
  107.                 VEHICLE::SET_VEHICLE_CUSTOM_PRIMARY_COLOUR(vehicle, primary_custom_color[0], primary_custom_color[1], primary_custom_color[2]);
  108.             }
  109.             if (!vehicle_json["Custom Secondary Color"].is_null())
  110.             {
  111.                 std::vector<int> secondary_custom_color = vehicle_json["Custom Secondary Color"];
  112.                 VEHICLE::SET_VEHICLE_CUSTOM_SECONDARY_COLOUR(vehicle, secondary_custom_color[0], secondary_custom_color[1], secondary_custom_color[2]);
  113.             }
  114.             VEHICLE::SET_VEHICLE_WINDOW_TINT(vehicle, vehicle_json["Vehicle Window Tint"]);
  115.             if (!vehicle_json["Radio Station"].is_null())
  116.                 AUDIO::SET_VEH_RADIO_STATION(vehicle, vehicle_json["Radio Station"].get<std::string>().c_str());
  117.             VEHICLE::SET_VEHICLE_NUMBER_PLATE_TEXT(vehicle, vehicle_json["Plate Text"].get<std::string>().c_str());
  118.             VEHICLE::SET_VEHICLE_NUMBER_PLATE_TEXT_INDEX(vehicle, vehicle_json["Plate Text Index"]);
  119.             VEHICLE::SET_VEHICLE_EXTRA_COLOURS(vehicle, vehicle_json["Pearlescent Color"], vehicle_json["Wheel Color"]);
  120.             std::map<int, bool> vehicle_extras = vehicle_json["Vehicle Extras"];
  121.             for (int i = 0; i <= 20; i++)
  122.             {
  123.                 if (VEHICLE::DOES_EXTRA_EXIST(vehicle, i))
  124.                     VEHICLE::SET_VEHICLE_EXTRA(vehicle, i, vehicle_extras[i]);
  125.             }
  126.             if (!vehicle_json["Vehicle Livery"].is_null())
  127.             {
  128.                 VEHICLE::SET_VEHICLE_LIVERY(vehicle, vehicle_json["Vehicle Livery"]);
  129.             }
  130.             if (VEHICLE::IS_THIS_MODEL_A_CAR(ENTITY::GET_ENTITY_MODEL(vehicle)) == TRUE || VEHICLE::IS_THIS_MODEL_A_BIKE(ENTITY::GET_ENTITY_MODEL(vehicle)))
  131.             {
  132.                 VEHICLE::SET_VEHICLE_WHEEL_TYPE(vehicle, vehicle_json["Wheel Type"]);
  133.                 for (int i = MOD_SPOILERS; i <= MOD_LIVERY; i++)
  134.                 {
  135.                     BOOL bModOn = !vehicle_json[mod_names[i]].is_null();
  136.                     if (bModOn == TRUE)
  137.                     {
  138.                         if (i == MOD_TIRESMOKE)
  139.                         {
  140.                             std::vector<int> tire_smoke_color = vehicle_json["Tire Smoke Color"];
  141.                             VEHICLE::SET_VEHICLE_TYRE_SMOKE_COLOR(vehicle, tire_smoke_color[0], tire_smoke_color[1], tire_smoke_color[2]);
  142.                             VEHICLE::TOGGLE_VEHICLE_MOD(vehicle, MOD_TIRESMOKE, TRUE);
  143.                         }
  144.                         else if (vehicle_json[mod_names[i]].is_array())
  145.                         {
  146.                             std::vector<int> mod = vehicle_json[mod_names[i]];
  147.                             VEHICLE::SET_VEHICLE_MOD(vehicle, i, mod[0], mod[1]);
  148.                         }
  149.                         else
  150.                         {
  151.                             VEHICLE::TOGGLE_VEHICLE_MOD(vehicle, i, TRUE);
  152.                         }
  153.                     }
  154.                 }
  155.                 std::vector<bool> neon_lights = vehicle_json["Neon Lights"];
  156.                 for (int i = NEON_LEFT; i <= NEON_BACK; i++)
  157.                     VEHICLE::_SET_VEHICLE_NEON_LIGHT_ENABLED(vehicle, i, neon_lights[i]);
  158.                 std::vector<int> neon_color = vehicle_json["Neon Color"];
  159.                 VEHICLE::_SET_VEHICLE_NEON_LIGHTS_COLOUR(vehicle, neon_color[0], neon_color[1], neon_color[2]);
  160.                 if (VEHICLE::IS_VEHICLE_A_CONVERTIBLE(vehicle, 0))
  161.                 {
  162.                     int convertableState = vehicle_json["Convertable State"];
  163.                     if (convertableState == 0 || convertableState == 3 || convertableState == 5)
  164.                         VEHICLE::RAISE_CONVERTIBLE_ROOF(vehicle, TRUE);
  165.                     else
  166.                         VEHICLE::LOWER_CONVERTIBLE_ROOF(vehicle, TRUE);
  167.                 }
  168.                 VEHICLE::SET_VEHICLE_INTERIOR_COLOUR(vehicle, vehicle_json["Interior Color"]);
  169.                 VEHICLE::SET_VEHICLE_DASHBOARD_COLOUR(vehicle, vehicle_json["Dash Color"]);
  170.                 if (vehicle_json["Clan Logo"] == TRUE)
  171.                     AddClanLogoToVehicle(vehicle, ped);
  172.                 VEHICLE::SET_VEHICLE_HEADLIGHT_COLOUR(vehicle, vehicle_json["Headlight Color"]);
  173.             }
  174.             return vehicle;
  175.         }
  176.  
  177.         static nlohmann::json get_full_vehicle_json(Vehicle vehicle)
  178.         {
  179.             nlohmann::json vehicle_json = get_vehicle_json(vehicle);
  180.             vehicle_json["Model Attachments"] = get_model_attachments(vehicle);
  181.             vehicle_json["Vehicle Attachments"] = get_vehicle_attachents(vehicle);
  182.             Vehicle tow;
  183.             VEHICLE::GET_VEHICLE_TRAILER_VEHICLE(vehicle, &tow);
  184.             if (ENTITY::DOES_ENTITY_EXIST(tow) && vehicle != tow)
  185.             {
  186.                 vehicle_json["Tow"] = get_vehicle_json(tow);
  187.                 vehicle_json["Tow"]["Model Attachments"] = get_model_attachments(tow);
  188.                 vehicle_json["Tow"]["Vehicle Attachments"] = get_vehicle_attachents(tow);
  189.             }
  190.             return vehicle_json;
  191.         }
  192.  
  193.         static model_attachment::model_attachment get_model_attachment(Vehicle vehicle, Object object)
  194.         {
  195.             Vector3 object_location = ENTITY::GET_ENTITY_COORDS(object, 0);
  196.             Vector3 location = ENTITY::GET_OFFSET_FROM_ENTITY_GIVEN_WORLD_COORDS(vehicle, object_location.x, object_location.y, object_location.z);
  197.             Vector3 object_rotation = ENTITY::GET_ENTITY_ROTATION(object, 0);
  198.             Vector3 vehicle_rotation = ENTITY::GET_ENTITY_ROTATION(vehicle, 0);
  199.             Vector3 rotation;
  200.             rotation.x = (object_rotation.x - vehicle_rotation.x);
  201.             rotation.y = (object_rotation.y - vehicle_rotation.y);
  202.             rotation.z = (object_rotation.z - vehicle_rotation.z);
  203.             model_attachment::model_attachment attachment = { ENTITY::GET_ENTITY_MODEL(object), location, rotation };
  204.             return attachment;
  205.         }
  206.  
  207.         static nlohmann::json get_model_attachments(Vehicle vehicle)
  208.         {
  209.             std::vector<nlohmann::json> attached_objects;
  210.             rage::CObjectInterface* objIF = g_pointers->m_replay_interface->pCObjectInterface;
  211.             int numObj = objIF->iMaxObjects;
  212.             for (int i = 0; i < numObj; i++)
  213.             {
  214.                 rage::CObject* pCObject = objIF->get_object(i);
  215.                 if (pCObject == nullptr)
  216.                     continue;
  217.  
  218.                 Object object = g_pointers->m_ptr_to_handle(pCObject);
  219.                 if (object == 0)
  220.                     break;
  221.  
  222.                 if (!ENTITY::IS_ENTITY_ATTACHED_TO_ENTITY(vehicle, object))
  223.                     continue;
  224.  
  225.                 attached_objects.push_back(get_model_attachment(vehicle, object));
  226.             };
  227.             return attached_objects;
  228.         }
  229.  
  230.         static nlohmann::json get_vehicle_attachents(Vehicle vehicle)
  231.         {
  232.             std::vector<nlohmann::json> attached_vehicles;
  233.             Vehicle tow;
  234.             VEHICLE::GET_VEHICLE_TRAILER_VEHICLE(vehicle, &tow);
  235.             rage::CVehicleInterface* vehicle_interface = g_pointers->m_replay_interface->pCVehicleInterface;
  236.             for (int i = 0; i < vehicle_interface->iMaxVehicles; i++)
  237.             {
  238.                 auto* vehicle_ptr = vehicle_interface->get_vehicle(i);
  239.                 if (vehicle_ptr == nullptr)
  240.                     continue;
  241.  
  242.                 Vehicle object = g_pointers->m_ptr_to_handle((rage::CObject*)vehicle_ptr);
  243.                 if (object == 0)
  244.                     break;
  245.  
  246.                 Vehicle object_tow;
  247.                 VEHICLE::GET_VEHICLE_TRAILER_VEHICLE(object, &object_tow);
  248.  
  249.                 if (!ENTITY::IS_ENTITY_ATTACHED_TO_ENTITY(vehicle, object) || object == tow || object_tow == vehicle)
  250.                     continue;
  251.  
  252.                 nlohmann::json model_attachment;
  253.                 model_attachment["Vehicle"] = get_vehicle_json(object);
  254.                 model_attachment["Model Attachment"] = get_model_attachment(vehicle, object);
  255.                 attached_vehicles.push_back(model_attachment);
  256.             }
  257.             return attached_vehicles;
  258.         }
  259.  
  260.         static nlohmann::json get_vehicle_json(Vehicle vehicle)
  261.         {
  262.             nlohmann::json vehicle_json;
  263.  
  264.             vehicle_json["Vehicle Model Hash"] = ENTITY::GET_ENTITY_MODEL(vehicle);
  265.             int primary_color, secondary_color;
  266.             VEHICLE::GET_VEHICLE_COLOURS(vehicle, &primary_color, &secondary_color);
  267.             vehicle_json["Primary Color"] = primary_color;
  268.             vehicle_json["Secondary Color"] = secondary_color;
  269.             if (VEHICLE::GET_IS_VEHICLE_PRIMARY_COLOUR_CUSTOM(vehicle))
  270.             {
  271.                 int custom_primary_color[3]{};
  272.                 VEHICLE::GET_VEHICLE_CUSTOM_PRIMARY_COLOUR(vehicle, &custom_primary_color[0], &custom_primary_color[1], &custom_primary_color[2]);
  273.                 vehicle_json["Custom Primary Color"] = custom_primary_color;
  274.             }
  275.             if (VEHICLE::GET_IS_VEHICLE_SECONDARY_COLOUR_CUSTOM(vehicle))
  276.             {
  277.                 int custom_secondary_color[3]{};
  278.                 VEHICLE::GET_VEHICLE_CUSTOM_SECONDARY_COLOUR(vehicle, &custom_secondary_color[0], &custom_secondary_color[1], &custom_secondary_color[2]);
  279.                 vehicle_json["Custom Secondary Color"] = custom_secondary_color;
  280.             }
  281.             vehicle_json["Vehicle Window Tint"] = VEHICLE::GET_VEHICLE_WINDOW_TINT(vehicle);
  282.             auto radio_station = AUDIO::GET_PLAYER_RADIO_STATION_NAME();
  283.             if (radio_station == nullptr)
  284.                 radio_station = "OFF";
  285.             vehicle_json["Radio Station"] = radio_station;
  286.             vehicle_json["Plate Text"] = VEHICLE::GET_VEHICLE_NUMBER_PLATE_TEXT(vehicle);
  287.             vehicle_json["Plate Text Index"] = VEHICLE::GET_VEHICLE_NUMBER_PLATE_TEXT_INDEX(vehicle);
  288.             int pearlescent_color, wheel_color;
  289.             VEHICLE::GET_VEHICLE_EXTRA_COLOURS(vehicle, &pearlescent_color, &wheel_color);
  290.             vehicle_json["Pearlescent Color"] = pearlescent_color;
  291.             vehicle_json["Wheel Color"] = wheel_color;
  292.             std::map<int,bool> vehicle_extras;
  293.             for (int i = 0; i <= 20; i++)
  294.             {
  295.                 if (VEHICLE::DOES_EXTRA_EXIST(vehicle, i))
  296.                     vehicle_extras[i] = !VEHICLE::IS_VEHICLE_EXTRA_TURNED_ON(vehicle, i);
  297.             }
  298.             vehicle_json["Vehicle Extras"] = vehicle_extras;
  299.             if ((VEHICLE::GET_VEHICLE_LIVERY_COUNT(vehicle) > 1) && VEHICLE::GET_VEHICLE_LIVERY(vehicle) >= 0)
  300.             {
  301.                 vehicle_json["Vehicle Livery"] = VEHICLE::GET_VEHICLE_LIVERY(vehicle);
  302.             }
  303.             if (VEHICLE::IS_THIS_MODEL_A_CAR(ENTITY::GET_ENTITY_MODEL(vehicle)) == TRUE || VEHICLE::IS_THIS_MODEL_A_BIKE(ENTITY::GET_ENTITY_MODEL(vehicle)))
  304.             {
  305.                 vehicle_json["Wheel Type"] = VEHICLE::GET_VEHICLE_WHEEL_TYPE(vehicle);
  306.                 for (int i = MOD_SPOILERS; i <= MOD_LIVERY; i++)
  307.                 {
  308.                     BOOL is_mod_on = VEHICLE::IS_TOGGLE_MOD_ON(vehicle, i);
  309.                     if (i == MOD_TIRESMOKE && is_mod_on == TRUE)
  310.                     {
  311.                         int tire_smoke_color[3]{};
  312.                         VEHICLE::GET_VEHICLE_TYRE_SMOKE_COLOR(vehicle, &tire_smoke_color[0], &tire_smoke_color[1], &tire_smoke_color[2]);
  313.                         vehicle_json["Tire Smoke Color"] = tire_smoke_color;
  314.                     }
  315.                     else if (is_mod_on == TRUE)
  316.                     {
  317.                         vehicle_json[mod_names[i]] = "TOGGLE";
  318.                     }
  319.                     if (VEHICLE::GET_VEHICLE_MOD(vehicle, i) != -1)
  320.                     {
  321.                         int vehicle_mod[2] = { VEHICLE::GET_VEHICLE_MOD(vehicle, i), VEHICLE::GET_VEHICLE_MOD_VARIATION(vehicle, i) };
  322.                         vehicle_json[mod_names[i]] = vehicle_mod;
  323.                     }
  324.                 }
  325.                 bool neon_lights[4]{};
  326.                 for (int i = NEON_LEFT; i <= NEON_BACK; i++)
  327.                     neon_lights[i] = VEHICLE::_IS_VEHICLE_NEON_LIGHT_ENABLED(vehicle, i);
  328.                 int neon_color[3]{};
  329.                 VEHICLE::_GET_VEHICLE_NEON_LIGHTS_COLOUR(vehicle, &neon_color[0], &neon_color[1], &neon_color[2]);
  330.                 vehicle_json["Neon Color"] = neon_color;
  331.                 vehicle_json["Neon Lights"] = neon_lights;
  332.                 if (VEHICLE::IS_VEHICLE_A_CONVERTIBLE(vehicle, 0))
  333.                     vehicle_json["Convertable State"] = VEHICLE::GET_CONVERTIBLE_ROOF_STATE(vehicle);
  334.                 int interior_color, dashboard_color;
  335.                 VEHICLE::GET_VEHICLE_INTERIOR_COLOUR(vehicle, &interior_color);
  336.                 VEHICLE::GET_VEHICLE_DASHBOARD_COLOUR(vehicle, &dashboard_color);
  337.                 vehicle_json["Interior Color"] = interior_color;
  338.                 vehicle_json["Dash Color"] = dashboard_color;
  339.                 vehicle_json["Clan Logo"] = GRAPHICS::_DOES_VEHICLE_HAVE_DECAL(vehicle, 0);
  340.                 vehicle_json["Headlight Color"] = VEHICLE::GET_VEHICLE_HEADLIGHT_COLOUR(vehicle);
  341.             }
  342.             return vehicle_json;
  343.         }
  344.  
  345.         static std::filesystem::path check_vehicle_folder()
  346.         {
  347.             auto file_path = std::filesystem::path(std::getenv("appdata"));
  348.             file_path /= "BigBaseV2";
  349.             file_path /= "Vehicles";
  350.  
  351.             if (!std::filesystem::exists(file_path))
  352.             {
  353.                 std::filesystem::create_directory(file_path);
  354.             }
  355.             else if (!std::filesystem::is_directory(file_path))
  356.             {
  357.                 std::filesystem::remove(file_path);
  358.                 std::filesystem::create_directory(file_path);
  359.             }
  360.  
  361.             return file_path;
  362.         }
  363.     };
  364. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement