Files
RAGECOOP-V/Client/Scripts/Util/Util.cs

273 lines
8.9 KiB
C#
Raw Normal View History

2022-10-23 19:02:39 +08:00
using System;
using System.Drawing;
using System.IO;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using GTA;
using GTA.Math;
using GTA.Native;
2022-10-23 19:02:39 +08:00
using GTA.UI;
2022-10-19 19:07:46 +08:00
using LemonUI.Elements;
2022-10-15 17:06:19 +08:00
using Newtonsoft.Json;
2022-07-20 17:50:01 +08:00
using RageCoop.Core;
2022-11-16 17:37:31 +08:00
using static RageCoop.Client.Shared;
2022-11-16 17:40:07 +08:00
using Font = GTA.UI.Font;
[assembly: InternalsVisibleTo("RageCoop.Client.Installer")]
2022-10-23 19:02:39 +08:00
namespace RageCoop.Client
{
internal static class Util
{
/// <summary>
/// The location of the cursor on screen between 0 and 1.
/// </summary>
public static PointF CursorPositionRelative
{
get
{
var cursorX = Game.IsControlEnabled(Control.CursorX)
? Game.GetControlValueNormalized(Control.CursorX)
: Game.GetDisabledControlValueNormalized(Control.CursorX);
var cursorY = Game.IsControlEnabled(Control.CursorY)
? Game.GetControlValueNormalized(Control.CursorY)
: Game.GetDisabledControlValueNormalized(Control.CursorY);
return new PointF(cursorX, cursorY);
}
}
public static Point CursorPosition
{
get
{
var p = CursorPositionRelative;
var res = Screen.Resolution;
return new Point((int)(p.X * res.Width), (int)(p.Y * res.Height));
}
}
2022-10-23 19:02:39 +08:00
public static SizeF ResolutionMaintainRatio
{
get
{
// Get the game width and height
var screenw = Screen.Resolution.Width;
var screenh = Screen.Resolution.Height;
// Calculate the ratio
var ratio = (float)screenw / screenh;
// And the width with that ratio
var width = 1080f * ratio;
// Finally, return a SizeF
return new SizeF(width, 1080f);
}
}
2022-10-19 19:07:46 +08:00
public static Vector3 GetRotation(this EntityBone b)
{
return b.ForwardVector.ToEulerRotation(b.UpVector);
}
2022-10-23 19:02:39 +08:00
2022-10-19 19:07:46 +08:00
public static void DrawTextFromCoord(Vector3 coord, string text, float scale = 0.5f, Point offset = default)
{
Point toDraw = default;
if (WorldToScreen(coord, ref toDraw))
{
toDraw.X += offset.X;
toDraw.Y += offset.Y;
2022-10-23 19:02:39 +08:00
new ScaledText(toDraw, text, scale, Font.ChaletLondon)
2022-10-19 19:07:46 +08:00
{
Outline = true,
2022-10-23 19:02:39 +08:00
Alignment = Alignment.Center,
Color = Color.White
2022-10-19 19:07:46 +08:00
}.Draw();
}
}
2022-10-23 19:02:39 +08:00
2022-07-14 10:33:24 +08:00
public static bool WorldToScreen(Vector3 pos, ref Point screenPos)
{
float x, y;
unsafe
{
var res = ResolutionMaintainRatio;
if (Call<bool>(GET_SCREEN_COORD_FROM_WORLD_COORD, pos.X, pos.Y, pos.Z, &x, &y))
2022-07-14 10:33:24 +08:00
{
2022-09-06 21:46:35 +08:00
screenPos = new Point((int)(res.Width * x), (int)(y * 1080));
2022-07-14 10:33:24 +08:00
return true;
}
}
2022-10-23 19:02:39 +08:00
return false;
}
2022-09-06 21:46:35 +08:00
public static Settings ReadSettings(string path = null)
{
path = path ?? SettingsPath;
Directory.CreateDirectory(Directory.GetParent(path).FullName);
2022-10-09 22:07:52 +08:00
Settings settings;
try
{
2022-10-09 22:07:52 +08:00
settings = JsonConvert.DeserializeObject<Settings>(File.ReadAllText(path));
}
2022-10-09 22:07:52 +08:00
catch (Exception ex)
{
2022-10-09 22:07:52 +08:00
Main.Logger?.Error(ex);
File.WriteAllText(path, JsonConvert.SerializeObject(settings = new Settings(), Formatting.Indented));
}
return settings;
}
2022-10-23 19:02:39 +08:00
2022-09-06 21:46:35 +08:00
public static bool SaveSettings(string path = null, Settings settings = null)
{
try
{
path = path ?? SettingsPath;
settings = settings ?? Main.Settings;
Directory.CreateDirectory(Directory.GetParent(path).FullName);
2022-10-10 16:45:58 +08:00
File.WriteAllText(path, JsonConvert.SerializeObject(settings, Formatting.Indented));
return true;
}
catch (Exception ex)
{
2022-10-08 23:49:48 +08:00
Main.Logger?.Error(ex);
return false;
}
}
public static Vehicle CreateVehicle(Model model, Vector3 position, float heading = 0f)
{
2022-10-23 19:02:39 +08:00
if (!model.IsLoaded) return null;
return (Vehicle)Entity.FromHandle(Call<int>(CREATE_VEHICLE, model.Hash, position.X,
2022-10-23 19:02:39 +08:00
position.Y, position.Z, heading, false, false));
}
2022-10-23 19:02:39 +08:00
public static Ped CreatePed(Model model, Vector3 position, float heading = 0f)
{
2022-10-23 19:02:39 +08:00
if (!model.IsLoaded) return null;
return (Ped)Entity.FromHandle(Call<int>(CREATE_PED, 26, model.Hash, position.X, position.Y,
2022-10-23 19:02:39 +08:00
position.Z, heading, false, false));
}
2022-10-23 19:02:39 +08:00
public static void SetOnFire(this Entity e, bool toggle)
{
if (toggle)
Call(START_ENTITY_FIRE, e.Handle);
else
Call(STOP_ENTITY_FIRE, e.Handle);
}
2022-10-23 19:02:39 +08:00
2022-07-20 17:50:01 +08:00
public static void SetFrozen(this Entity e, bool toggle)
2022-07-02 17:14:56 +08:00
{
Call(FREEZE_ENTITY_POSITION, e, toggle);
2022-07-02 17:14:56 +08:00
}
public static SyncedPed GetSyncEntity(this Ped p)
{
2022-10-23 19:02:39 +08:00
if (p == null) return null;
var c = EntityPool.GetPedByHandle(p.Handle);
2022-10-23 19:02:39 +08:00
if (c == null) EntityPool.Add(c = new SyncedPed(p));
return c;
}
public static SyncedVehicle GetSyncEntity(this Vehicle veh)
{
2022-10-23 19:02:39 +08:00
if (veh == null) return null;
var v = EntityPool.GetVehicleByHandle(veh.Handle);
2022-10-23 19:02:39 +08:00
if (v == null) EntityPool.Add(v = new SyncedVehicle(veh));
return v;
}
2022-06-03 13:11:17 +08:00
2022-10-23 19:02:39 +08:00
public static void ApplyForce(this Entity e, int boneIndex, Vector3 direction, Vector3 rotation = default,
ForceType forceType = ForceType.MaxForceRot2)
2022-07-17 18:44:16 +08:00
{
Call(APPLY_FORCE_TO_ENTITY, e.Handle, forceType, direction.X, direction.Y, direction.Z,
2022-10-23 19:02:39 +08:00
rotation.X, rotation.Y, rotation.Z, boneIndex, false, true, true, false, true);
2022-07-17 18:44:16 +08:00
}
2022-10-23 19:02:39 +08:00
2022-06-03 13:11:17 +08:00
public static byte GetPlayerRadioIndex()
{
return (byte)Call<int>(GET_PLAYER_RADIO_STATION_INDEX);
2022-06-03 13:11:17 +08:00
}
2022-10-23 19:02:39 +08:00
2022-06-03 13:11:17 +08:00
public static void SetPlayerRadioIndex(int index)
{
Call(SET_RADIO_TO_STATION_INDEX, index);
2022-06-03 13:11:17 +08:00
}
public static EntityPopulationType GetPopulationType(int handle)
=> (EntityPopulationType)Call<int>(GET_ENTITY_POPULATION_TYPE, handle);
public static unsafe void DeleteEntity(int handle)
{
Call(SET_ENTITY_AS_MISSION_ENTITY, handle, false, true);
Call(DELETE_ENTITY, &handle);
}
[DllImport("kernel32.dll")]
public static extern ulong GetTickCount64();
2022-10-23 19:02:39 +08:00
#region -- POINTER --
private static int _steeringAngleOffset { get; set; }
public static unsafe void NativeMemory()
{
IntPtr address;
address = Game.FindPattern("\x74\x0A\xF3\x0F\x11\xB3\x1C\x09\x00\x00\xEB\x25", "xxxxxx????xx");
if (address != IntPtr.Zero) _steeringAngleOffset = *(int*)(address + 6) + 8;
}
public static unsafe void CustomSteeringAngle(this Vehicle veh, float value)
{
var address = new IntPtr((long)veh.MemoryAddress);
if (address == IntPtr.Zero || _steeringAngleOffset == 0) return;
*(float*)(address + _steeringAngleOffset).ToPointer() = value;
}
#endregion
#region MATH
public static Vector3 LinearVectorLerp(Vector3 start, Vector3 end, ulong currentTime, int duration)
{
return new Vector3
{
X = LinearFloatLerp(start.X, end.X, currentTime, duration),
Y = LinearFloatLerp(start.Y, end.Y, currentTime, duration),
Z = LinearFloatLerp(start.Z, end.Z, currentTime, duration)
};
}
public static float LinearFloatLerp(float start, float end, ulong currentTime, int duration)
{
return (end - start) * currentTime / duration + start;
}
public static float Lerp(float from, float to, float fAlpha)
{
return from * (1.0f - fAlpha) + to * fAlpha; //from + (to - from) * fAlpha
}
public static Vector3 RotationToDirection(Vector3 rotation)
{
var z = MathExtensions.DegToRad(rotation.Z);
var x = MathExtensions.DegToRad(rotation.X);
var num = Math.Abs(Math.Cos(x));
return new Vector3
{
X = (float)(-Math.Sin(z) * num),
Y = (float)(Math.Cos(z) * num),
Z = (float)Math.Sin(x)
};
}
#endregion
}
2022-10-23 19:02:39 +08:00
}