IlDivinoGlade

GTA V PS3 - Vehicle Memory Pool Scanner

Oct 11th, 2025 (edited)
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 16.41 KB | None | 0 0
  1. // ==================================================
  2. // VEHICLE MODEL MANAGEMENT SYSTEM
  3. // ==================================================
  4. // This system dynamically scans GTA V's memory to detect all available vehicle models.
  5. // Unlike hardcoded lists, this approach automatically finds vehicles by reading the game's
  6. // internal model pool, making it compatible with different game versions and DLCs.
  7. //
  8. // Key advantages:
  9. // - No manual hash lists needed
  10. // - Automatically detects all vehicles in memory
  11. // - Handles variants intelligently (Police, Armored, Racecar versions, etc.)
  12. // - Memory-efficient: only stores what's actually available
  13. // - Works with any PS3 GTA V build
  14. // ==================================================
  15.  
  16. struct VehicleModel {
  17.     Hash hash;                  // Model hash identifier
  18.     char displayName[64];       // Localized display name (e.g., "Adder")
  19.     char makeName[64];          // Vehicle manufacturer (e.g., "Truffade")
  20.     int vehicleClass;           // Vehicle class (0-21: Compacts, Sedans, Super, etc.)
  21.     int memoryAddress;          // Memory address where model was found (for debugging)
  22. };
  23.  
  24. VehicleModel* g_vehicles = nullptr;
  25. int g_vehicleCount = 0;
  26. bool g_vehiclesScanned = false;
  27.  
  28. // ==================================================
  29. // MEMORY ALLOCATION
  30. // ==================================================
  31. void initModels()
  32. {
  33.     if (!g_vehicles)
  34.     {
  35.         // Allocate memory for up to 500 vehicle models
  36.         g_vehicles = (VehicleModel*)_sys_malloc(sizeof(VehicleModel) * 500);
  37.         if (g_vehicles)
  38.             printf("Allocated %d bytes for vehicles\n", sizeof(VehicleModel) * 500);
  39.         else
  40.             printf("ERROR: Failed to allocate memory for vehicles!\n");
  41.     }
  42. }
  43.  
  44. // ==================================================
  45. // VEHICLE CLASS NAMES
  46. // ==================================================
  47. const char* vehicleClasses_pool[] = {
  48.     "Compacts",           // 0
  49.     "Sedans",             // 1
  50.     "SUVs",               // 2
  51.     "Coupes",             // 3
  52.     "Muscle",             // 4
  53.     "Sports Classics",    // 5
  54.     "Sports",             // 6
  55.     "Super",              // 7
  56.     "Motorcycles",        // 8
  57.     "Off-road",           // 9
  58.     "Industrial",         // 10
  59.     "Utility",            // 11
  60.     "Vans",               // 12
  61.     "Cycles",             // 13
  62.     "Boats",              // 14
  63.     "Helicopters",        // 15
  64.     "Planes",             // 16
  65.     "Service",            // 17
  66.     "Emergency",          // 18
  67.     "Military",           // 19
  68.     "Commercial",         // 20
  69.     "Trains"              // 21
  70. };
  71.  
  72. // ==================================================
  73. // VARIANT TYPE DETECTION
  74. // ==================================================
  75. // Identifies special vehicle variants by analyzing their label names
  76. // Returns a descriptive string for the variant type
  77. const char* getVariantType(const char* labelName, Hash modelHash)
  78. {
  79.     // Check for numeric suffixes (e.g., BATI2, TORNADO3)
  80.     int len = strlen(labelName);
  81.     if (len > 0)
  82.     {
  83.         char lastChar = labelName[len - 1];
  84.         if (lastChar >= '2' && lastChar <= '9')
  85.             return "Variant";
  86.     }
  87.  
  88.     // Special variant keywords
  89.     if (strstr(labelName, "RACE") != NULL) return "Racecar";
  90.     if (strstr(labelName, "ARMOR") != NULL) return "Armored";
  91.     if (strstr(labelName, "LIVERY") != NULL) return "Livery";
  92.     if (strstr(labelName, "POLICE") != NULL) return "Police";
  93.     if (strstr(labelName, "GANG") != NULL) return "Gang";
  94.     if (strstr(labelName, "TAXI") != NULL) return "Taxi";
  95.     if (strstr(labelName, "SHERIFF") != NULL) return "Sheriff";
  96.     if (strstr(labelName, "LOST") != NULL) return "Lost MC";
  97.     if (strstr(labelName, "SNOW") != NULL) return "Snow";
  98.     if (strstr(labelName, "RUSTED") != NULL) return "Rusted";
  99.     if (strstr(labelName, "RUSTY") != NULL) return "Rusty";
  100.     if (strstr(labelName, "LIFEGUARD") != NULL) return "Lifeguard";
  101.     if (strstr(labelName, "CLOWN") != NULL) return "Clown";
  102.     if (strstr(labelName, "PARK") != NULL) return "Park Ranger";
  103.     if (strstr(labelName, "RIOT") != NULL) return "Riot";
  104.  
  105.     // Model-specific suffixes
  106.     if (strstr(labelName, "XL") != NULL) return "XL";
  107.     if (strstr(labelName, "GT") != NULL) return "GT";
  108.     if (strstr(labelName, "XS") != NULL) return "XS";
  109.     if (strstr(labelName, "RR") != NULL) return "RR";
  110.     if (strstr(labelName, "SWB") != NULL) return "Short Wheelbase";
  111.  
  112.     // Fallback: use passenger capacity to differentiate
  113.     int seats = VEHICLE::_GET_VEHICLE_MODEL_MAX_NUMBER_OF_PASSENGERS(modelHash);
  114.     static char seatsBuf[32];
  115.     if (seats > 0 && seats < 20)
  116.     {
  117.         sprintf(seatsBuf, "%d-Seater", seats);
  118.         return seatsBuf;
  119.     }
  120.  
  121.     return "Alt";
  122. }
  123.  
  124. // ==================================================
  125. // MAIN VEHICLE SCANNING FUNCTION
  126. // ==================================================
  127. // Scans specific memory regions where vehicle models are stored
  128. // Automatically detects and catalogs all available vehicles
  129. void scanAllVehicles()
  130. {
  131.     initModels();
  132.  
  133.     if (g_vehiclesScanned)
  134.     {
  135.         return;
  136.     }
  137.  
  138.     g_vehicleCount = 0;
  139.  
  140.     // Memory regions containing vehicle model data on PS3
  141.     // Region 1: 0x40200000 - 0x40400000 (Primary vehicle models)
  142.     // Region 2: 0x40800000 - 0x40A00000 (Additional vehicle models)
  143.     int regions[][2] = {
  144.         {0x40200000, 0x40400000},
  145.         {0x40800000, 0x40A00000}
  146.     };
  147.  
  148.     for (int r = 0; r < 2; r++)
  149.     {
  150.         // Scan memory in 4-byte increments (size of a Hash)
  151.         for (int addr = regions[r][0]; addr < regions[r][1]; addr += 0x4)
  152.         {
  153.             Hash modelHash = *(Hash*)addr;
  154.  
  155.             // Skip null or invalid hashes
  156.             if (modelHash == 0 || modelHash == 0xFFFFFFFF)
  157.                 continue;
  158.  
  159.             // Verify this is a valid vehicle model using game natives
  160.             if (STREAMING::IS_MODEL_IN_CDIMAGE(modelHash) &&
  161.                 STREAMING::IS_MODEL_A_VEHICLE(modelHash))
  162.             {
  163.                 // Get the model's label name (e.g., "ADDER", "ZENTORNO")
  164.                 const char* labelName = VEHICLE::GET_DISPLAY_NAME_FROM_VEHICLE_MODEL(modelHash);
  165.  
  166.                 if (labelName && strlen(labelName) > 0 && strcmp(labelName, "CARNOTFOUND") != 0)
  167.                 {
  168.                     // Check for duplicate hashes
  169.                     bool duplicate = false;
  170.                     for (int i = 0; i < g_vehicleCount; i++)
  171.                     {
  172.                         if (g_vehicles[i].hash == modelHash)
  173.                         {
  174.                             duplicate = true;
  175.                             break;
  176.                         }
  177.                     }
  178.  
  179.                     if (!duplicate && g_vehicleCount < 500)
  180.                     {
  181.                         g_vehicles[g_vehicleCount].hash = modelHash;
  182.  
  183.                         // Get localized display name from game text database
  184.                         const char* fullName = UI::_GET_LABEL_TEXT((char*)labelName);
  185.                         char baseName[64];
  186.  
  187.                         if (fullName && strlen(fullName) > 0 && strcmp(fullName, labelName) != 0)
  188.                         {
  189.                             strncpy(baseName, fullName, 63);
  190.                         }
  191.                         else
  192.                         {
  193.                             // Fallback to label name if localization fails
  194.                             strncpy(baseName, labelName, 63);
  195.                         }
  196.                         baseName[63] = '\0';
  197.  
  198.                         // ==================================================
  199.                         // DUPLICATE NAME HANDLING WITH VARIANT DETECTION
  200.                         // ==================================================
  201.                        
  202.                         // Count how many vehicles with the same base name already exist
  203.                         int nameCount = 0;
  204.                         bool isVariant = false;
  205.  
  206.                         for (int i = 0; i < g_vehicleCount; i++)
  207.                         {
  208.                             // Extract base name (without variant suffix)
  209.                             char existingBase[64];
  210.                             strcpy(existingBase, g_vehicles[i].displayName);
  211.  
  212.                             // Remove variant suffix in parentheses
  213.                             char* paren = strstr(existingBase, " (");
  214.                             if (paren) *paren = '\0';
  215.  
  216.                             // Compare base names
  217.                             if (strcmp(existingBase, baseName) == 0)
  218.                             {
  219.                                 nameCount++;
  220.                             }
  221.                         }
  222.  
  223.                         // Determine if this is a known variant type
  224.                         int len = strlen(labelName);
  225.                         char lastChar = (len > 0) ? labelName[len - 1] : '\0';
  226.  
  227.                         if ((lastChar >= '2' && lastChar <= '9') ||
  228.                             strstr(labelName, "RACE") != NULL ||
  229.                             strstr(labelName, "ARMOR") != NULL ||
  230.                             strstr(labelName, "LIVERY") != NULL ||
  231.                             strstr(labelName, "POLICE") != NULL ||
  232.                             strstr(labelName, "GANG") != NULL)
  233.                         {
  234.                             isVariant = true;
  235.                         }
  236.  
  237.                         // Build final display name
  238.                         char finalName[64];
  239.  
  240.                         if (nameCount > 0)
  241.                         {
  242.                             if (isVariant)
  243.                             {
  244.                                 // This is a recognized variant - add descriptive suffix
  245.                                 const char* variantType = getVariantType(labelName, modelHash);
  246.                                 snprintf(finalName, 63, "%s (%s)", baseName, variantType);
  247.                             }
  248.                             else
  249.                             {
  250.                                 // Generic duplicate - add numeric suffix
  251.                                 snprintf(finalName, 63, "%s (%d)", baseName, nameCount + 1);
  252.                             }
  253.                         }
  254.                         else
  255.                         {
  256.                             // Unique name - no suffix needed
  257.                             strcpy(finalName, baseName);
  258.                         }
  259.  
  260.                         finalName[63] = '\0';
  261.                         strncpy(g_vehicles[g_vehicleCount].displayName, finalName, 63);
  262.                         g_vehicles[g_vehicleCount].displayName[63] = '\0';
  263.  
  264.                         // Get vehicle class (Compacts, Sedans, Super, etc.)
  265.                         int vehClass = VEHICLE::GET_VEHICLE_CLASS_FROM_NAME(modelHash);
  266.                         if (vehClass < 0 || vehClass > 21)
  267.                             vehClass = 0; // Default to Compacts if invalid
  268.  
  269.                         g_vehicles[g_vehicleCount].vehicleClass = vehClass;
  270.                         g_vehicles[g_vehicleCount].memoryAddress = addr;
  271.  
  272.                         g_vehicleCount++;
  273.                     }
  274.                 }
  275.             }
  276.         }
  277.     }
  278.  
  279.     g_vehiclesScanned = true;
  280. }
  281.  
  282. // ==================================================
  283. // DEBUG: PRINT VEHICLES BY CLASS
  284. // ==================================================
  285. // Outputs a formatted list of all detected vehicles organized by class
  286. void printVehiclesByClass()
  287. {
  288.     if (!g_vehiclesScanned || g_vehicleCount == 0)
  289.     {
  290.         return;
  291.     }
  292.  
  293.     // Count vehicles per class
  294.     int classCounts[22] = { 0 };
  295.  
  296.     for (int i = 0; i < g_vehicleCount; i++)
  297.     {
  298.         int classId = g_vehicles[i].vehicleClass;
  299.         if (classId >= 0 && classId < 22)
  300.             classCounts[classId]++;
  301.     }
  302.  
  303.     // Print details for each class
  304.     for (int classId = 0; classId < 22; classId++)
  305.     {
  306.         if (classCounts[classId] == 0)
  307.             continue;
  308.  
  309.         for (int i = 0; i < g_vehicleCount; i++)
  310.         {
  311.             if (g_vehicles[i].vehicleClass == classId)
  312.             {
  313.                 // Output: Display Name (Hash)
  314.             }
  315.         }
  316.     }
  317. }
  318.  
  319. // ==================================================
  320. // MEMORY CLEANUP
  321. // ==================================================
  322. // Frees all allocated memory for vehicle data
  323. void cleanupModels()
  324. {
  325.     if (g_vehicles)
  326.     {
  327.         _sys_free(g_vehicles);
  328.         g_vehicles = nullptr;
  329.         g_vehicleCount = 0;
  330.         g_vehiclesScanned = false;
  331.     }
  332. }
  333.  
  334. // ==================================================
  335. // MENU INTEGRATION STRUCTURES
  336. // ==================================================
  337. // Organizes vehicle data for use in the spawn menu system
  338. struct VehicleClassMenuInfo {
  339.     const char** names;      // Array of display names
  340.     Hash* models;            // Array of model hashes
  341.     int count;               // Number of vehicles in this class
  342. };
  343.  
  344. VehicleClassMenuInfo vehicleClassesMenuInfo[22];
  345. bool g_menuDataReady = false;
  346.  
  347. // ==================================================
  348. // PREPARE MENU DATA
  349. // ==================================================
  350. // Organizes scanned vehicles into class-specific arrays for menu display
  351. // This function should be called once before accessing the vehicle spawn menu
  352. void prepareVehicleMenuData()
  353. {
  354.     if (g_menuDataReady) return;
  355.  
  356.     if (!g_vehiclesScanned) {
  357.         scanAllVehicles();
  358.     }
  359.  
  360.     // Count vehicles per class
  361.     int classCounts[22] = { 0 };
  362.     for (int i = 0; i < g_vehicleCount; i++) {
  363.         int classId = g_vehicles[i].vehicleClass;
  364.         if (classId >= 0 && classId < 22) {
  365.             classCounts[classId]++;
  366.         }
  367.     }
  368.  
  369.     // Allocate and populate arrays for each class
  370.     for (int classId = 0; classId < 22; classId++) {
  371.         if (classCounts[classId] > 0) {
  372.             // Allocate array of name pointers
  373.             vehicleClassesMenuInfo[classId].names = (const char**)_sys_malloc(sizeof(char*) * classCounts[classId]);
  374.  
  375.             // Allocate array of model hashes
  376.             vehicleClassesMenuInfo[classId].models = (Hash*)_sys_malloc(sizeof(Hash) * classCounts[classId]);
  377.  
  378.             vehicleClassesMenuInfo[classId].count = classCounts[classId];
  379.  
  380.             // Fill arrays with vehicle data
  381.             int currentIndex = 0;
  382.             for (int i = 0; i < g_vehicleCount; i++) {
  383.                 if (g_vehicles[i].vehicleClass == classId) {
  384.                     vehicleClassesMenuInfo[classId].names[currentIndex] = g_vehicles[i].displayName;
  385.                     vehicleClassesMenuInfo[classId].models[currentIndex] = g_vehicles[i].hash;
  386.                     currentIndex++;
  387.                 }
  388.             }
  389.         }
  390.         else {
  391.             // No vehicles in this class
  392.             vehicleClassesMenuInfo[classId].names = nullptr;
  393.             vehicleClassesMenuInfo[classId].models = nullptr;
  394.             vehicleClassesMenuInfo[classId].count = 0;
  395.         }
  396.     }
  397.  
  398.     g_menuDataReady = true;
  399. }
  400.  
  401.  
  402.  
  403.  
  404.  
  405.  
  406.  
  407.  
  408.  
  409.  
  410.  
  411.  
  412.  
  413.  
  414.  
  415.  
  416.  
  417.  
  418.  
  419.  
  420.  
  421.  
  422.  
  423.  
  424.  
  425.  
  426.  
  427.  
  428. // ============================================================
  429. // VEHICLE POOL USAGE EXPLANATION (old version)
  430. // ============================================================
  431. //
  432. // The vehicle pool (created by scanAllVehicles()) contains all detected vehicle models found in the game's memory.
  433. //
  434. // Each entry includes:
  435. //   • VehName[i] → the display name of the vehicle
  436. //   • VehHash[i] → the model hash used to spawn it
  437. //
  438. // The following loop dynamically builds a vehicle menu
  439. // by adding every vehicle name as an option.
  440. //
  441. // Example:
  442. //   for (int i = 0; i < vehPoolsz; i++)
  443. //       addOption(VehName[i]);
  444. //
  445. // This means you don’t need to hardcode any vehicle names —
  446. // the menu automatically fills with every model found.
  447. //
  448. // When the player selects one of these options, the code below
  449. // retrieves the correct hash from the pool and spawns the vehicle:
  450. //
  451. //   spawnVehicle(VehHash[index - 1]);
  452. //
  453. // In simple terms:
  454. //   - The "vehicle pool" acts like a live database of all vehicles.
  455. //   - The "for loop" builds the menu interface.
  456. //   - The "spawnVehicle" call creates the selected car in the game.
  457. //
  458. // ============================================================
  459. // NOTE:
  460. // This pool can contain 300+ vehicles (especially on GTA V PS3 - 1.27/1.28),
  461. // so it’s recommended to load or scan it only once during game startup.
  462. // Doing so prevents short freezes when building the list.
  463. // ============================================================
  464. //-discord: gladeildivino
  465. struct VehicleModel {
  466.     Hash hash;
  467.     char name[64];
  468. };
  469.  
  470. VehicleModel g_vehicles[500];
  471.  
  472. int g_vehicleCount = 0;
  473.  
  474. bool g_vehiclesScanned = false;
  475.  
  476. // ==================================================
  477. // Function: scanAllVehicles()
  478. // Description: Scans memory regions to detect vehicle models
  479. // ==================================================
  480. void scanAllVehicles()
  481. {
  482.     if (g_vehiclesScanned)
  483.     {
  484.         printf("Vehicles already scanned! Count: %d\n", g_vehicleCount);
  485.         return;
  486.     }
  487.  
  488.     printf("=== Scanning Vehicles ===\n");
  489.     g_vehicleCount = 0;
  490.  
  491.     // Only scan the regions that contain vehicle model data
  492.     int regions[][2] = {
  493.         {0x40200000, 0x40400000},  // Main region (contains most vehicle data)
  494.         {0x40800000, 0x40A00000}   // Secondary region (contains most vehicle data)
  495.     };
  496.  
  497.     for (int r = 0; r < 2; r++)
  498.     {
  499.         printf("Scanning region %d...\n", r + 1);
  500.  
  501.         // Step through memory, reading 4 bytes at a time
  502.         for (int addr = regions[r][0]; addr < regions[r][1]; addr += 0x4)
  503.         {
  504.             Hash modelHash = *(Hash*)addr;
  505.  
  506.             // Skip invalid or empty model hashes
  507.             if (modelHash == 0 || modelHash == 0xFFFFFFFF)
  508.                 continue;
  509.  
  510.             // Verify that this hash corresponds to a valid vehicle model
  511.             if (STREAMING::IS_MODEL_IN_CDIMAGE(modelHash) &&
  512.                 STREAMING::IS_MODEL_A_VEHICLE(modelHash))
  513.             {
  514.                 const char* name = VEHICLE::GET_DISPLAY_NAME_FROM_VEHICLE_MODEL(modelHash);
  515.  
  516.                 // Skip if model name is invalid or placeholder
  517.                 if (name && strlen(name) > 0 && strcmp(name, "CARNOTFOUND") != 0)
  518.                 {
  519.                     bool duplicate = false;
  520.  
  521.                     // Avoid adding duplicate entries
  522.                     for (int i = 0; i < g_vehicleCount; i++)
  523.                     {
  524.                         if (g_vehicles[i].hash == modelHash)
  525.                         {
  526.                             duplicate = true;
  527.                             break;
  528.                         }
  529.                     }
  530.  
  531.                     // Store new vehicle entry
  532.                     if (!duplicate && g_vehicleCount < 500)
  533.                     {
  534.                         g_vehicles[g_vehicleCount].hash = modelHash;
  535.                         strncpy(g_vehicles[g_vehicleCount].name, name, 63);
  536.                         g_vehicles[g_vehicleCount].name[63] = '\0';
  537.                         g_vehicleCount++;
  538.                     }
  539.                 }
  540.             }
  541.         }
  542.     }
  543.  
  544.     g_vehiclesScanned = true;
  545.     printf("=== Scan complete! Found %d vehicles ===\n", g_vehicleCount);
  546. }
  547.  
  548.  
  549. This code scans game memory to collect all vehicle models currently available.
  550. It’s mainly for GTA V (PS3 1.27 / 1.28), but the logic applies to other platforms too.
  551. Just make sure to run it safely — ideally during game load or in a separate thread —
  552. to prevent minor lag spikes or freezes.
  553.  
  554.  
Add Comment
Please, Sign In to add comment