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

Implemented entity transmit feature #608

Open
wants to merge 37 commits into
base: main
Choose a base branch
from

Conversation

KillStr3aK
Copy link
Contributor

@KillStr3aK KillStr3aK commented Oct 6, 2024

With this implementation, developers are able to control the entity transmit per player

Examples

These are simple examples, but it can be used for way more complex things.

// hide every door for everyone as an example
RegisterListener<Listeners.CheckTransmit>((CCheckTransmitInfoList infoList) =>
{
    IEnumerable<CPropDoorRotating> doors = Utilities.FindAllEntitiesByDesignerName<CPropDoorRotating>("prop_door_rotating");

    if (!doors.Any())
        return;

    foreach ((CCheckTransmitInfo info, CCSPlayerController? player) in infoList)
    {
        if (player == null)
            continue;

        foreach (CPropDoorRotating door in doors)
        {
            info.TransmitEntities.Remove(door);
        }
    }
});

image

@KillStr3aK KillStr3aK marked this pull request as ready for review October 6, 2024 08:38
Comment on lines +3 to +5
# Visual Studio Version 17
VisualStudioVersion = 17.12.35309.182
MinimumVisualStudioVersion = 10.0.40219.1
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure about these, probably was autogenerated when saving

Comment on lines +163 to +164
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {069C4CD4-BACA-446A-A6B8-0194E4F75355}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure about these, probably was autogenerated when saving

@roflmuffin
Copy link
Owner

roflmuffin commented Oct 10, 2024

Overall the PR looks great!😍 I am wondering though if we can possibly improve the DX of iterating over the transmit list. Maybe something like this (pseudo-ish code)?

RegisterListener<Listeners.CheckTransmit>((CCheckTransmitInfoList infoList, int infoCount) =>
{
    // Get the list of the currently available doors (prop_door_rotating)
    IEnumerable<CPropDoorRotating> doors = Utilities.FindAllEntitiesByDesignerName<CPropDoorRotating>("prop_door_rotating");

    // Do nothing if there is none.
    if (!doors.Any())
        return;

    foreach(var (CFixedBitVecBase transmitEntities, CCSPlayerController? player) in infoList) 
    {
        if (player == null || !ShouldSeeDoors.ContainsKey(player.Slot)) 
            continue;
        
        foreach (CPropDoorRotating door in doors)
        {
            transmitEntities.Remove(door);
        }   
    }
});

The idea being that if we can provide access to an IEnumerable ourselves (or preferably IReadOnlyList<T> so that we can maintan index access), we can handle the bounds checking and prevent issues with that, as well as simplifying the access to the entities list & player.

Example enumerator implementation on CCheckTransmitInfoList. But we have to somehow pass the length of the transmit list through from native to managed, which looks hard with the current implementation simply casting from a pointer:

public sealed class CCheckTransmitInfoList : NativeObject,
        IReadOnlyList<(CFixedBitVecBase TransmitEntities, CCSPlayerController? Player)>
    {
        private int CheckTransmitPlayerSlotOffset = GameData.GetOffset("CheckTransmitPlayerSlot");

        private unsafe nint* Inner => (nint*)base.Handle;

        public unsafe CCheckTransmitInfoList(IntPtr pointer) : base(pointer)
        {
        }

        public int Count { get; }


        private unsafe (CCheckTransmitInfo, int) Get(int index)
        {
            return (Marshal.PtrToStructure<CCheckTransmitInfo>(this.Inner[index]),
                (int)(*(byte*)(this.Inner[index] + CheckTransmitPlayerSlotOffset)));
        }

        public IEnumerator<(CFixedBitVecBase TransmitEntities, CCSPlayerController? Player)> GetEnumerator()
        {
            for (int i = 0; i < this.Count; i++)
            {
                yield return this[i];
            }
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        /// <summary>
        /// Get transmit info for the given index.
        /// </summary>
        /// <param name="index">Index of the info you want to retrieve from the list, should be between 0 and 'infoCount' - 1</param>
        /// <returns></returns>
        public (CFixedBitVecBase TransmitEntities, CCSPlayerController? Player) this[int index]
        {
            get
            {
                // Ideally throw here if out of range
                var (transmit, slot) = this.Get(index);
                CCSPlayerController? player = Utilities.GetPlayerFromSlot(slot);
                return (transmit.TransmitEntities, player);
            }
        }
    }

@KillStr3aK
Copy link
Contributor Author

Overall the PR looks great!😍 I am wondering though if we can possibly improve the DX of iterating over the transmit list. Maybe something like this (pseudo-ish code)?

RegisterListener<Listeners.CheckTransmit>((CCheckTransmitInfoList infoList, int infoCount) =>
{
    // Get the list of the currently available doors (prop_door_rotating)
    IEnumerable<CPropDoorRotating> doors = Utilities.FindAllEntitiesByDesignerName<CPropDoorRotating>("prop_door_rotating");

    // Do nothing if there is none.
    if (!doors.Any())
        return;

    foreach(var (CFixedBitVecBase transmitEntities, CCSPlayerController? player) in infoList) 
    {
        if (player == null || !ShouldSeeDoors.ContainsKey(player.Slot)) 
            continue;
        
        foreach (CPropDoorRotating door in doors)
        {
            transmitEntities.Remove(door);
        }   
    }
});

The idea being that if we can provide access to an IEnumerable ourselves (or preferably IReadOnlyList<T> so that we can maintan index access), we can handle the bounds checking and prevent issues with that, as well as simplifying the access to the entities list & player.

Example enumerator implementation on CCheckTransmitInfoList. But we have to somehow pass the length of the transmit list through from native to managed, which looks hard with the current implementation simply casting from a pointer:

public sealed class CCheckTransmitInfoList : NativeObject,
        IReadOnlyList<(CFixedBitVecBase TransmitEntities, CCSPlayerController? Player)>
    {
        private int CheckTransmitPlayerSlotOffset = GameData.GetOffset("CheckTransmitPlayerSlot");

        private unsafe nint* Inner => (nint*)base.Handle;

        public unsafe CCheckTransmitInfoList(IntPtr pointer) : base(pointer)
        {
        }

        public int Count { get; }


        private unsafe (CCheckTransmitInfo, int) Get(int index)
        {
            return (Marshal.PtrToStructure<CCheckTransmitInfo>(this.Inner[index]),
                (int)(*(byte*)(this.Inner[index] + CheckTransmitPlayerSlotOffset)));
        }

        public IEnumerator<(CFixedBitVecBase TransmitEntities, CCSPlayerController? Player)> GetEnumerator()
        {
            for (int i = 0; i < this.Count; i++)
            {
                yield return this[i];
            }
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        /// <summary>
        /// Get transmit info for the given index.
        /// </summary>
        /// <param name="index">Index of the info you want to retrieve from the list, should be between 0 and 'infoCount' - 1</param>
        /// <returns></returns>
        public (CFixedBitVecBase TransmitEntities, CCSPlayerController? Player) this[int index]
        {
            get
            {
                // Ideally throw here if out of range
                var (transmit, slot) = this.Get(index);
                CCSPlayerController? player = Utilities.GetPlayerFromSlot(slot);
                return (transmit.TransmitEntities, player);
            }
        }
    }

Great idea! Will take a look on it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants