Jump to content

Recommended Posts

Posted

Hi Guys,

Had someone in a discord ask about generating an updated version of vehicleData.json so i took it on as a challenge for myself to create some code that will automatically grab the vehicles listed on the rage.mp wiki and create some information about them into a JSON File. Feel free to extend this or do whatever.

GetVehicleInfo.cs

    public class GetVehicleInfo : Script
    {
        public GetVehicleInfo()
        {
 
        }
 
        public static Task<string> MakeAsyncRequest(string url, string contentType)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.ContentType = contentType;
            request.Method = WebRequestMethods.Http.Get;
            request.Timeout = 20000;
            request.Proxy = null;
 
            Task<WebResponse> task = Task.Factory.FromAsync(
                request.BeginGetResponse,
                asyncResult => request.EndGetResponse(asyncResult),
                (object)null);
 
            return task.ContinueWith(t => ReadStreamFromResponse(t.Result));
        }
 
        private static string ReadStreamFromResponse(WebResponse response)
        {
            using (Stream responseStream = response.GetResponseStream())
            using (StreamReader sr = new StreamReader(responseStream))
            {
                //Need to return this response
                string strContent = sr.ReadToEnd();
                return strContent;
            }
        }
 
        public async Task<List<string>> ReturnVehicleNamesAsync(Client player)
        {
            List<string> tempVehicleStore = new List<string>();
 
            player.SendChatMessage("Fetching vehicle data... this may take a couple seconds.");
 
            string html = await MakeAsyncRequest("https://wiki.rage.mp/index.php?title=Vehicles", "text/html");
            string pattern = @"<code[^>]*?>(.*?)</code>";
            MatchCollection matches = Regex.Matches(html, pattern, RegexOptions.IgnoreCase | RegexOptions.Singleline);
 
            if (matches.Count != 0)
            {
                foreach(Match match in matches)
                {
                    GroupCollection groups = match.Groups;
                    tempVehicleStore.Add(groups[1].Value);
                }
 
                return tempVehicleStore;
            } else
            {
                player.SendChatMessage("Error matching regex to vehicles..");
            }
 
            return null;
        }
 
        public void WriteToFile(string path, string content)
        {
            try
            {
                Directory.CreateDirectory(Path.GetDirectoryName(path));
                File.WriteAllText(path, content);
            }
            catch (IOException e)
            {
                NAPI.Util.ConsoleOutput("Error: ", e.Message);
                NAPI.Util.ConsoleOutput("Stack: ", e.StackTrace);
            }
        }
    }
 
    public class VehicleData
    {
        public string VehicleName { get; set; }
        public float MaxSpeed { get; set; }
        public float MaxBraking { get; set; }
        public float MaxTraction { get; set; }
        public float MaxAcceleration { get; set; }
        public int MaxNumberOfPassengers { get; set; }
        public int MaxOccupants { get; set; }
        public int VehicleClass { get; set; }
    }

 

Command i created with this class:

        [Command("fetchvehicledata")]
        public async void FetchVehicleDataAsync(Client player)
        {
            List<VehicleData> vehData = new List<VehicleData>();
            GetVehicleInfo vehicle = new GetVehicleInfo();

            List<string> vehiclenames = await vehicle.ReturnVehicleNamesAsync(player);

            player.SendChatMessage("Fetch complete, getting related vehicle info..");
            
            foreach(string v in vehiclenames)
            {
                uint id = NAPI.Util.GetHashKey(v);

                vehData.Add(new VehicleData()
                {
                    VehicleName = NAPI.Vehicle.GetVehicleDisplayName((VehicleHash)id),
                    MaxSpeed = NAPI.Vehicle.GetVehicleMaxSpeed((VehicleHash)id),
                    MaxBraking = NAPI.Vehicle.GetVehicleMaxBraking((VehicleHash)id),
                    MaxTraction = NAPI.Vehicle.GetVehicleMaxTraction((VehicleHash)id),
                    MaxAcceleration = NAPI.Vehicle.GetVehicleMaxAcceleration((VehicleHash)id),
                    MaxNumberOfPassengers = NAPI.Vehicle.GetVehicleMaxPassengers((VehicleHash)id),
                    MaxOccupants = NAPI.Vehicle.GetVehicleMaxOccupants((VehicleHash)id),
                    VehicleClass = NAPI.Vehicle.GetVehicleClass((VehicleHash)id)
                });
            }
            vehicle.WriteToFile(@"output/vehicleData.json", NAPI.Util.ToJson(vehData));
            player.SendChatMessage("vehicleData.json exported, check output folder in your server directory");
        }

 

Note: I did use the GTANetwork's deprecated code as a reference when creating this so partial credits to them :)

 

Kind Regards,

JCurtis.

  • Like 3

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...