Archived
Private
Public Access
1
0
This repository has been archived on 2026-02-04. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
ProjectBackup/C#/FiveM/TaxiJob/TaxiJob.Client/TaxiJob.cs
2022-11-12 13:10:03 +01:00

95 lines
3.5 KiB
C#

#pragma warning disable CS1998
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using CitizenFX.Core;
using CitizenFX.Core.Native;
using Nexd.ESX.Client;
using TaxiJob.Client.Extensions;
using TaxiJob.Client.Handler;
using TaxiJob.Shared;
namespace TaxiJob.Client {
public sealed class TaxiJob : BaseScript {
private static TaxiJob _instance;
public static readonly ClientConfig Config = new ClientConfig();
public static bool InDuty = false;
public TaxiJob() {
_instance = this;
EventHandlers["onClientResourceStart"] += new Action<string>(OnStart); //esx:playerLoaded
}
public static Task<T> ServerCallback<T>(string name, [Optional] dynamic args) {
var source = new TaskCompletionSource<T>();
ESX.TriggerServerCallback(name, new Action<dynamic>(o => {
source.TrySetResult((T)o);
}), args);
return source.Task;
}
public static Task<dynamic> ServerCallback2Results(string name, [Optional] dynamic args) {
var source = new TaskCompletionSource<dynamic>();
ESX.Raw.TriggerServerCallback(name, new Action<dynamic, dynamic>((o, o1) => {
dynamic data = new ExpandoObject();
data.first = o as ExpandoObject;
data.second = o1 as ExpandoObject;
source.TrySetResult(data);
}), args);
return source.Task;
}
public static Task<Vehicle> SpawnVehicle(int model, Vector4 coords, bool local = false) {
var source = new TaskCompletionSource<Vehicle>();
if (local) {
ESX.Game.Raw.SpawnLocalVehicle(model, coords.ToVector3(), coords.W, new Action<int>(handle => {
source.TrySetResult(new Vehicle(handle));
}));
}
else {
ESX.Game.Raw.SpawnVehicle(model, coords.ToVector3(), coords.W, new Action<int>(handle => {
source.TrySetResult(new Vehicle(handle));
}));
}
return source.Task;
}
public static async Task<string> GeneratePlate() {
var plate = await _instance.Exports["esx_vehicleshop"].GeneratePlate();
return Convert.ToString(plate);
}
public static void PrintDynamic(dynamic data, string prefix = "") {
foreach (var element in (data as IDictionary<string, object>)) {
Debug.WriteLine($"{prefix}{element.Key}: {element.Value}");
if (element.Value.GetType() == typeof(ExpandoObject)) {
PrintDynamic(element.Value, prefix + "\t");
}
if (element.Value.GetType() == typeof(List<object>)) {
Debug.WriteLine(prefix + "\t", string.Join(", ", element.Value));
}
}
}
[Command("taximeter")]
private void OnCommand() {
Taximeter.Attributes.MeterVisible = !Taximeter.Attributes.MeterVisible;
Taximeter.Update();
}
private void OnStart(string resourceName) {
if (API.GetCurrentResourceName() != resourceName) return;
Tick += async () => {
if (ESX.GetPlayerData().job.name != "taxi") return;
CloakroomHandler.OnTick();
if (!InDuty) return;
JobHandler.OnTick();
};
}
}
}