Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Addon Groups with ACE Permissions #293

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 125 additions & 0 deletions SharedClasses/ConfigManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,131 @@ public LocationBlip(string name, Vector3 coordinates, int spriteID, int color)
}
}
#endregion

#region Addons

/// <summary>
/// Gets the addons.json data.
/// </summary>
/// <returns></returns>
public static Addons GetAddons()
{
Addons data = new Addons();

string jsonFile = LoadResourceFile(GetCurrentResourceName(), "config/addons.json");
try
{
if (string.IsNullOrEmpty(jsonFile))
{
#if CLIENT
vMenuClient.Notify.Error("The addons.json file is empty or does not exist, please tell the server owner to fix this.");
#endif
#if SERVER
vMenuServer.DebugLog.Log("The addons.json file is empty or does not exist, please fix this.", vMenuServer.DebugLog.LogLevel.error);
#endif
}
else
{
data = JsonConvert.DeserializeObject<Addons>(jsonFile);
}
}
catch (Exception e)
{
#if CLIENT
vMenuClient.Notify.Error("An error occurred while processing the addons.json file. Please correct any errors in the addons.json file.");
#endif
Debug.WriteLine($"[vMenu] json exception details: {e.Message}\nStackTrace:\n{e.StackTrace}");
}

return data;
}

#if SERVER
/// <summary>
/// Gets the vehicle group data from the addons.json file.
/// </summary>
/// <param name="source">The source of the player.</param>
/// <returns></returns>
public static List<Group> GetAddonsVehicleGroupData(Player player)
{
return CreateAddonsListFromGroupData(player, GetAddons().groups.vehicles);
}

/// <summary>
/// Gets the ped group data from the addons.json file.
/// </summary>
/// <param name="source">The source of the player.</param>
/// <returns></returns>
public static List<Group> GetAddonsPedGroupData(Player player)
{
return CreateAddonsListFromGroupData(player, GetAddons().groups.peds);
}

/// <summary>
/// Gets the weapon group data from the addons.json file.
/// </summary>
/// <param name="source">The source of the player.</param>
/// <returns></returns>
public static List<Group> GetAddonsWeaponGroupData(Player player)
{
return CreateAddonsListFromGroupData(player, GetAddons().groups.weapons);
}

/// <summary>
/// Generates a list of groups based on the group permission in the addons.json file.
/// </summary>
/// <param name="player"></param>
/// <param name="groups"></param>
/// <returns></returns>
private static List<Group> CreateAddonsListFromGroupData(Player player, List<Group> groups)
{
List<Group> finalLst = new List<Group>();
foreach (Group group in groups)
{
string permission = group.permission;
if (string.IsNullOrEmpty(permission))
{
finalLst.Add(group);
}
else if (PermissionsManager.IsAllowed(group.permission, player))
{
finalLst.Add(group);
}
}
return finalLst;
}
#endif

public struct Addons
{
public string[] vehicles { get; set; }
public string[] peds { get; set; }
public string[] weapons { get; set; }
public string[] weapon_components { get; set; }
public Groups groups { get; set; }
}

public struct Groups
{
public List<Group> vehicles { get; set; }
public List<Group> peds { get; set; }
public List<Group> weapons { get; set; }
}

public struct Group
{
public string permission { get; set; }
public string label { get; set; }
public string description { get; set; }
public List<GroupItem> items { get; set; }
}

public struct GroupItem
{
public string label { get; set; }
public string model { get; set; }
}
#endregion
}


Expand Down
38 changes: 34 additions & 4 deletions SharedClasses/PermissionsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ public enum Permission
/// <param name="checkAnyway">if true, then the permissions will be checked even if they aren't setup yet.</param>
/// <returns></returns>
public static bool IsAllowed(Permission permission, Player source, bool checkAnyway = false) => IsAllowedServer(permission, source);
public static bool IsAllowed(string permission, Player source, bool checkAnyway = false) => IsAllowedServer(permission, source);
#endif

#if CLIENT
Expand Down Expand Up @@ -408,6 +409,26 @@ private static bool IsAllowedClient(Permission permission, bool checkAnyway)
}
#endif
#if SERVER
/// <summary>
/// Checks if the player is allowed that specific permission.
/// </summary>
/// <param name="permission"></param>
/// <param name="source"></param>
/// <returns></returns>
private static bool IsAllowedServer(string permission, Player source)
{
if (source == null)
{
return false;
}

if (IsPlayerAceAllowed(source.Handle, GetAceName(permission)))
{
return true;
}
return false;
}

/// <summary>
/// Checks if the player is allowed that specific permission.
/// </summary>
Expand Down Expand Up @@ -469,7 +490,7 @@ public static List<Permission> GetPermissionAndParentPermissions(Permission perm
/// Sets the permissions for a specific player (checks server side, sends event to client side).
/// </summary>
/// <param name="player"></param>
public static void SetPermissionsForPlayer([FromSource]Player player)
public static void SetPermissionsForPlayer([FromSource] Player player)
{
if (player == null)
{
Expand Down Expand Up @@ -554,10 +575,19 @@ public static void SetPermissions(string permissions)
private static string GetAceName(Permission permission)
{
string name = permission.ToString();
return GetAceName(name);
}

/// <summary>
/// Gets the full permission ace name for the specific permission name.
/// </summary>
/// <param name="permission"></param>
/// <returns></returns>
private static string GetAceName(string permission)
{
string prefix = "vMenu.";

switch (name.Substring(0, 2))
switch (permission.Substring(0, 2))
{
case "OP":
prefix += "OnlinePlayers";
Expand Down Expand Up @@ -599,10 +629,10 @@ private static string GetAceName(Permission permission)
prefix += "VoiceChat";
break;
default:
return prefix + name;
return prefix + permission;
}

return prefix + "." + name.Substring(2);
return prefix + "." + permission.Substring(2);
}
#endif
}
Expand Down
45 changes: 23 additions & 22 deletions vMenu/EntitySpawner.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

using CitizenFX.Core;
Expand All @@ -22,16 +23,16 @@ public class EntitySpawner : BaseScript
public EntitySpawner()
{
#if DEBUG
RegisterCommand("testEntity", new Action<int, List<object>>((source, args) =>
{
string prop = (string)args[0];
SpawnEntity(prop, Game.PlayerPed.Position);
}), false);
RegisterCommand("endTest", new Action(() =>
{
FinishPlacement();
}), false);
RegisterCommand("testEntity", new Action<int, List<object>>((source, args) =>
{
var prop = (string)args[0];
SpawnEntity(prop, Game.PlayerPed.Position);
}), false);

RegisterCommand("endTest", new Action(() =>
{
FinishPlacement();
}), false);
#endif
}

Expand Down Expand Up @@ -109,8 +110,8 @@ public static async void FinishPlacement(bool duplicate = false)
{
if (duplicate)
{
int hash = CurrentEntity.Model.Hash;
Vector3 position = CurrentEntity.Position;
var hash = CurrentEntity.Model.Hash;
var position = CurrentEntity.Position;
CurrentEntity = null;
await Delay(1); // Mandatory
SpawnEntity((uint)hash, position);
Expand Down Expand Up @@ -154,7 +155,7 @@ private void DrawButtons() //TODO: Right keys
/// <returns>Output direction vector</returns>
private Vector3 RotationToDirection(Vector3 rotation)
{
Vector3 adj = new Vector3(
var adj = new Vector3(
(float)Math.PI / 180f * rotation.X,
(float)Math.PI / 180f * rotation.Y,
(float)Math.PI / 180f * rotation.Z
Expand All @@ -173,20 +174,20 @@ private Vector3 RotationToDirection(Vector3 rotation)
/// <returns>destination if no hit was found and coords of hit if there was one</returns>
private Vector3 GetCoordsPlayerIsLookingAt()
{
Vector3 camRotation = GetGameplayCamRot(0);
Vector3 camCoords = GetGameplayCamCoord();
Vector3 camDirection = RotationToDirection(camRotation);
var camRotation = GetGameplayCamRot(0);
var camCoords = GetGameplayCamCoord();
var camDirection = RotationToDirection(camRotation);

Vector3 dest = new Vector3(
var dest = new Vector3(
camCoords.X + (camDirection.X * RayDistance),
camCoords.Y + (camDirection.Y * RayDistance),
camCoords.Z + (camDirection.Z * RayDistance)
);

RaycastResult res = World.Raycast(camCoords, dest, IntersectOptions.Everything, Game.PlayerPed);
var res = World.Raycast(camCoords, dest, IntersectOptions.Everything, Game.PlayerPed);

#if DEBUG
DrawLine(Game.PlayerPed.Position.X, Game.PlayerPed.Position.Y,Game.PlayerPed.Position.Z, dest.X, dest.Y, dest.Z, 255, 0, 0, 255);
DrawLine(Game.PlayerPed.Position.X, Game.PlayerPed.Position.Y, Game.PlayerPed.Position.Z, dest.X, dest.Y, dest.Z, 255, 0, 0, 255);
#endif

return res.DitHit ? res.HitPosition : dest;
Expand Down Expand Up @@ -219,7 +220,7 @@ private async Task MoveHandler()
}
}

float headingOffset = 0f;
var headingOffset = 0f;
while (Active)
{
if (CurrentEntity == null || !CurrentEntity.Exists())
Expand All @@ -228,7 +229,7 @@ private async Task MoveHandler()
CurrentEntity = null;
break;
}
int handle = CurrentEntity.Handle;
var handle = CurrentEntity.Handle;

DrawButtons();

Expand All @@ -238,7 +239,7 @@ private async Task MoveHandler()
SetEntityAlpha(handle, (int)(255 * 0.4), 0);
CurrentEntity.Heading = (GetGameplayCamRot(0).Z + headingOffset) % 360f;

Vector3 newPosition = GetCoordsPlayerIsLookingAt();
var newPosition = GetCoordsPlayerIsLookingAt();

CurrentEntity.Position = newPosition;
if (CurrentEntity.HeightAboveGround < 3.0f)
Expand Down
Loading