Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Drawing;
- using System.Linq;
- using System.Net;
- using System.Runtime.InteropServices;
- using System.Text;
- using LSPD_First_Response.Engine.Scripting.Entities;
- using LSPD_First_Response.Mod.API;
- using Rage;
- using Rage.Native;
- using Unosquare.Labs.EmbedIO;
- using Unosquare.Labs.EmbedIO.Modules;
- using HttpListenerContext = Unosquare.Net.HttpListenerContext;
- namespace sardas_plugin
- {
- public class APIVehicle
- {
- public string name;
- // ReSharper disable once InconsistentNaming
- private string OwnerName;
- public string ownerName
- {
- get { return OwnerName; }
- set
- {
- OwnerName = value;
- if(owner!=null)
- owner = Util.getPersonaForName(value);
- }
- }
- public Persona owner { get; private set; }
- public string regStatus;
- public string insuranceStatus;
- public bool stolen;
- }
- public class ResponseMessage
- {
- public object responseBody { get; set; }
- public bool success;
- public string message;
- }
- public class WebAPI : WebApiController
- {
- /// <summary>
- /// In this method, we load up the .ini file so other methods can use it.
- /// </summary>
- /// <returns></returns>
- public static InitializationFile initialiseFile()
- {
- //InitializationFile is a Rage class.
- var ini = new InitializationFile("Plugins/LSPDFR/sardas.ini");
- ini.Create();
- return ini;
- }
- [WebApiHandler(HttpVerbs.Post, "/api/login/")]
- public bool login(WebServer server, HttpListenerContext context)
- {
- try
- {
- var initializationFile = initialiseFile();
- var username = initializationFile.ReadString("Login Data", "username", "ExampleOfficer");
- var password = initializationFile.ReadString("Login Data", "password", "ExamplePassword");
- var postData = context.RequestFormDataDictionary();
- Rage.Game.Console.Print($"Set Username: {username}");
- Rage.Game.Console.Print($"Set Password: {password}");
- if (postData!=null && postData.ContainsKey("username") && postData.ContainsKey("password"))
- {
- Rage.Game.Console.Print($"Username: {postData["username"]}");
- Rage.Game.Console.Print($"Password: {postData["password"]}");
- if ((string)postData["username"] == username && (string)postData["password"] == password)
- {
- return true;
- }
- }
- context.Response.StatusCode = 403;
- return true;
- }
- catch (Exception ex)
- {
- return HandleError(context, ex);
- }
- }
- [WebApiHandler(HttpVerbs.Post, "/api/settings/login/")]
- public bool changeLogin(WebServer server, HttpListenerContext context)
- {
- try
- {
- var initializationFile = initialiseFile();
- var postData = context.RequestFormDataDictionary();
- if (postData != null && postData.ContainsKey("username"))
- {
- initializationFile.Write("Login Data", "username", postData["username"] as string);
- if (postData.ContainsKey("password"))
- {
- initializationFile.Write("Login Data", "password", postData["password"] as string);
- }
- }
- return true;
- }
- catch (Exception ex)
- {
- return HandleError(context, ex);
- }
- }
- [WebApiHandler(HttpVerbs.Get, "/api/test/")]
- public bool testAPI(WebServer server, HttpListenerContext context)
- {
- try
- {
- foreach (var b in Encoding.ASCII.GetBytes("Hello World"))
- {
- context.Response.OutputStream.WriteByte(b);
- }
- context.Response.OutputStream.Close();
- context.Response.Close();
- return true;
- }
- catch (Exception ex)
- {
- Game.LogVerbose("Error while accessing test API");
- Game.LogVerbose(ex.StackTrace);
- return HandleError(context, ex);
- }
- }
- [WebApiHandler(HttpVerbs.Get, "/api/veh/{reg}")]
- public bool getVehicle(WebServer server, HttpListenerContext context, string reg)
- {
- try
- {
- APIVehicle veh;
- if((veh=Util.getVehicleForPlate(reg))!=null)
- {
- return context.JsonResponse(new ResponseMessage()
- {
- success = true,
- message = "Vehicle Found",
- responseBody = veh
- });
- }
- context.Response.StatusCode = (int)HttpStatusCode.NotFound;
- return context.JsonResponse(new ResponseMessage()
- {
- success = false,
- message = "Vehicle not found",
- responseBody = null
- });
- }
- catch (Exception ex)
- {
- Game.LogVerbose("Error while accessing vehicle API");
- Game.LogVerbose(ex.StackTrace);
- return HandleError(context, ex);
- }
- }
- [WebApiHandler(HttpVerbs.Get, "/api/ped/{name}")]
- public bool getPed(WebServer server, HttpListenerContext context, string name)
- {
- try
- {
- var persona = Util.getPersonaForName(name);
- if (persona != null)
- {
- return context.JsonResponse(new ResponseMessage()
- {
- success = true,
- message = "Ped found",
- responseBody = persona
- });
- }
- context.Response.StatusCode = (int)HttpStatusCode.NotFound;
- return context.JsonResponse(new ResponseMessage()
- {
- success = false,
- message = "Ped not found",
- responseBody = null
- });
- }
- catch (Exception ex)
- {
- Game.LogVerbose("Error while accessing ped API");
- Game.LogVerbose(ex.StackTrace);
- return HandleError(context, ex);
- }
- }
- protected bool HandleError(HttpListenerContext context, Exception ex, int statusCode = 500)
- {
- var errorResponse = new
- {
- Title = "Unexpected Error",
- ErrorCode = ex.GetType().Name,
- Description = ex.ExceptionMessage(),
- };
- context.Response.StatusCode = statusCode;
- return context.JsonResponse(errorResponse);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement