static public int constructor(IntPtr l)
 {
     try {
                     #if DEBUG
         var    method     = System.Reflection.MethodBase.GetCurrentMethod();
         string methodName = GetMethodName(method);
                     #if UNITY_5_5_OR_NEWER
         UnityEngine.Profiling.Profiler.BeginSample(methodName);
                     #else
         Profiler.BeginSample(methodName);
                     #endif
                     #endif
         UnityEngine.LowLevel.PlayerLoopSystem o;
         o = new UnityEngine.LowLevel.PlayerLoopSystem();
         pushValue(l, true);
         pushValue(l, o);
         return(2);
     }
     catch (Exception e) {
         return(error(l, e));
     }
             #if DEBUG
     finally {
                     #if UNITY_5_5_OR_NEWER
         UnityEngine.Profiling.Profiler.EndSample();
                     #else
         Profiler.EndSample();
                     #endif
     }
             #endif
 }
Exemple #2
0
    private static bool RemoveEngineLoopSystem <TSystem>(ref UnityEngine.LowLevel.PlayerLoopSystem system)
    {
        if (system.subSystemList == null)
        {
            return(false);
        }

        for (int idx = 0; idx < system.subSystemList.Length; idx++)
        {
            if (system.subSystemList[idx].type == typeof(TSystem))
            {
                var reducedSystemList = new List <UnityEngine.LowLevel.PlayerLoopSystem>(system.subSystemList);
                reducedSystemList.RemoveAt(idx);
                system.subSystemList = reducedSystemList.ToArray();
                return(true);
            }

            if (RemoveEngineLoopSystem <TSystem>(ref system.subSystemList[idx]))
            {
                return(true);
            }
        }

        return(false);
    }
    /// <summary>
    /// Inserts a new player loop system in the player loop, just before another system.
    /// </summary>
    /// <param name="toInsert">System to insert. Needs to have updateDelegate and Type set.</param>
    /// <param name="insertBefore">The subsystem to insert the system before</param>
    public static void InsertSystemBefore(UnityEngine.LowLevel.PlayerLoopSystem toInsert, Type insertBefore)
    {
        if (toInsert.type == null)
        {
            throw new ArgumentException("The inserted player loop system must have a marker type!", nameof(toInsert.type));
        }
        if (toInsert.updateDelegate == null)
        {
            throw new ArgumentException("The inserted player loop system must have an update delegate!", nameof(toInsert.updateDelegate));
        }
        if (insertBefore == null)
        {
            throw new ArgumentNullException(nameof(insertBefore));
        }

        EnsureSystemFetched();

        InsertSystem(ref system, toInsert, insertBefore, InsertType.Before, out var couldInsert);
        if (!couldInsert)
        {
            throw new ArgumentException($"When trying to insert the type {toInsert.type.Name} into the player loop before {insertBefore.Name}, " +
                                        $"{insertBefore.Name} could not be found in the current player loop!");
        }

        UnityEngine.LowLevel.PlayerLoop.SetPlayerLoop(system);
    }
    /// <summary>
    /// Inserts a new player loop system in the player loop, just before another system.
    /// </summary>
    /// <param name="newSystemMarker">Type marker for the new system.</param>
    /// <param name="newSystemUpdate">Callback that will be called each frame before insertBefore.</param>
    /// <param name="insertBefore">The subsystem to insert the system before.</param>
    /// <param name="runInFixedUpdate">Set the loop condition function to the same as the built-in FixedUpdate. NOTE: This doesn't work!</param>
    public static void InsertSystemBefore(Type newSystemMarker, UnityEngine.LowLevel.PlayerLoopSystem.UpdateFunction newSystemUpdate, Type insertBefore, bool runInFixedUpdate = false)
    {
        var playerLoopSystem = new UnityEngine.LowLevel.PlayerLoopSystem {
            type = newSystemMarker, updateDelegate = newSystemUpdate
        };

        if (runInFixedUpdate)
        {
            playerLoopSystem.loopConditionFunction = fixedUpdateLoopCondition;
        }
        InsertSystemBefore(playerLoopSystem, insertBefore);
    }
    public static UnityEngine.LowLevel.PlayerLoopSystem CopySystem(UnityEngine.LowLevel.PlayerLoopSystem system)
    {
        // PlayerLoopSystem is a struct.
        var copy = system;

        // but the sub system list is an array.
        if (system.subSystemList != null)
        {
            copy.subSystemList = new UnityEngine.LowLevel.PlayerLoopSystem[system.subSystemList.Length];
            for (int i = 0; i < copy.subSystemList.Length; i++)
            {
                copy.subSystemList[i] = CopySystem(system.subSystemList[i]);
            }
        }

        return(copy);
    }
    private static void FindNativeStuff(UnityEngine.LowLevel.PlayerLoopSystem playerLoopSystem, Dictionary <IntPtr, List <Type> > ptrToSystems, Func <UnityEngine.LowLevel.PlayerLoopSystem, IntPtr> grabPtr)
    {
        var ptr = grabPtr(playerLoopSystem);

        if (!ptrToSystems.ContainsKey(ptr))
        {
            ptrToSystems[ptr] = new List <Type>();
        }

        ptrToSystems[ptr].Add(playerLoopSystem.type);

        if (playerLoopSystem.subSystemList != null)
        {
            foreach (var subSystem in playerLoopSystem.subSystemList)
            {
                FindNativeStuff(subSystem, ptrToSystems, grabPtr);
            }
        }
    }
Exemple #7
0
    public static void DumpPlayerLoop(string which, UnityEngine.LowLevel.PlayerLoopSystem playerLoop)
    {
        var sb = new StringBuilder();

        sb.AppendLine($"{which} PlayerLoop List");
        foreach (var header in playerLoop.subSystemList)
        {
            sb.AppendFormat("------{0}------", header.type.Name);
            sb.AppendLine();
            foreach (var subSystem in header.subSystemList)
            {
                sb.AppendFormat("{0}.{1}", header.type.Name, subSystem.type.Name);
                sb.AppendLine();

                if (subSystem.subSystemList != null)
                {
                    UnityEngine.Debug.LogWarning("More Subsystem:" + subSystem.subSystemList.Length);
                }
            }
        }

        UnityEngine.Debug.Log(sb.ToString());
    }
        static int PlayerLoopSystemToInternal(PlayerLoopSystem sys, ref List <PlayerLoopSystemInternal> internalSys)
        {
            var idx    = internalSys.Count;
            var newSys = new PlayerLoopSystemInternal
            {
                type                  = sys.type,
                updateDelegate        = sys.updateDelegate,
                updateFunction        = sys.updateFunction,
                loopConditionFunction = sys.loopConditionFunction,
                numSubSystems         = 0
            };

            internalSys.Add(newSys);
            if (sys.subSystemList != null)
            {
                for (int i = 0; i < sys.subSystemList.Length; ++i)
                {
                    newSys.numSubSystems += PlayerLoopSystemToInternal(sys.subSystemList[i], ref internalSys);
                }
            }
            internalSys[idx] = newSys;
            return(newSys.numSubSystems + 1);
        }
    private static void EnsureSystemFetched()
    {
        if (hasFetchedSystem)
        {
            return;
        }

        defaultSystem    = UnityEngine.LowLevel.PlayerLoop.GetDefaultPlayerLoop();
        system           = CopySystem(defaultSystem);
        hasFetchedSystem = true;

        if (!TryFetchFixedUpdatePointer(system))
        {
            Debug.LogError("Couldn't find FixedUpdate in the built-in player loop systems! This means that setting runInFixedUpdate to true in " +
                           "InsertSystemBefore or After won't work!");
        }

        // if the Entities package is not installed, any systems registered keeps running after we leave play mode.
        // This is "intended behaviour". Not joking. https://fogbugz.unity3d.com/default.asp?1089518_lub560iemcggi1c9
        PlayerLoopQuitChecker.GameQuitCallback += () => {
            UnityEngine.LowLevel.PlayerLoop.SetPlayerLoop(defaultSystem);
        };
    }
    private static bool TryFetchFixedUpdatePointer(UnityEngine.LowLevel.PlayerLoopSystem loopSystem)
    {
        if (loopSystem.type == typeof(UnityEngine.PlayerLoop.FixedUpdate))
        {
            fixedUpdateLoopCondition = loopSystem.loopConditionFunction;
            return(true);
        }

        var subSystems = loopSystem.subSystemList;

        if (subSystems != null)
        {
            foreach (var subSystem in subSystems)
            {
                if (TryFetchFixedUpdatePointer(subSystem))
                {
                    return(true);
                }
            }
        }

        return(false);
    }
Exemple #11
0
        private static void InstallCallback <T>(UnityEngine.LowLevel.PlayerLoopSystem playerLoop, Type subsystem,
                                                UnityEngine.LowLevel.PlayerLoopSystem.UpdateFunction callback)
        {
            for (var i = 0; i < playerLoop.subSystemList.Length; ++i)
            {
                int subsystemListLength = playerLoop.subSystemList[i].subSystemList.Length;

                if (playerLoop.subSystemList[i].type != subsystem)
                {
                    continue;
                }

                // Create new subsystem list and add callback
                var newSubsystemList = new UnityEngine.LowLevel.PlayerLoopSystem[subsystemListLength + 1];
                for (var j = 0; j < subsystemListLength; j++)
                {
                    newSubsystemList[j] = playerLoop.subSystemList[i].subSystemList[j];
                }

                newSubsystemList[subsystemListLength].type           = typeof(UnityEngine.PlayerLoop.FixedUpdate);
                newSubsystemList[subsystemListLength].updateDelegate = callback;
                playerLoop.subSystemList[i].subSystemList            = newSubsystemList;
            }
        }
Exemple #12
0
        static UnityEngine.LowLevel.PlayerLoopSystem.UpdateFunction RemoveCallback <T>(UnityEngine.LowLevel.PlayerLoopSystem playerLoop)
        {
            for (var i = 0; i < playerLoop.subSystemList.Length; ++i)
            {
                var subsystemListLength = playerLoop.subSystemList[i].subSystemList.Length;
                for (var j = 0; j < subsystemListLength; j++)
                {
                    var item = playerLoop.subSystemList[i].subSystemList[j];
                    if (item.type != typeof(T))
                    {
                        continue;
                    }
                    playerLoop.subSystemList[i].subSystemList =
                        ExceptIndex(playerLoop.subSystemList[i].subSystemList, j);
                    return(item.updateDelegate);
                }
            }

            return(null);
        }
    private static void InsertSystem(ref UnityEngine.LowLevel.PlayerLoopSystem currentLoopRecursive, UnityEngine.LowLevel.PlayerLoopSystem toInsert, Type insertTarget, InsertType insertType,
                                     out bool couldInsert)
    {
        var currentSubSystems = currentLoopRecursive.subSystemList;

        if (currentSubSystems == null)
        {
            couldInsert = false;
            return;
        }

        int indexOfTarget = -1;

        for (int i = 0; i < currentSubSystems.Length; i++)
        {
            if (currentSubSystems[i].type == insertTarget)
            {
                indexOfTarget = i;
                break;
            }
        }

        if (indexOfTarget != -1)
        {
            var newSubSystems = new UnityEngine.LowLevel.PlayerLoopSystem[currentSubSystems.Length + 1];

            var insertIndex = insertType == InsertType.Before ? indexOfTarget : indexOfTarget + 1;

            for (int i = 0; i < newSubSystems.Length; i++)
            {
                if (i < insertIndex)
                {
                    newSubSystems[i] = currentSubSystems[i];
                }
                else if (i == insertIndex)
                {
                    newSubSystems[i] = toInsert;
                }
                else
                {
                    newSubSystems[i] = currentSubSystems[i - 1];
                }
            }

            couldInsert = true;
            currentLoopRecursive.subSystemList = newSubSystems;
        }
        else
        {
            for (var i = 0; i < currentSubSystems.Length; i++)
            {
                var subSystem = currentSubSystems[i];
                InsertSystem(ref subSystem, toInsert, insertTarget, insertType, out var couldInsertInInner);
                if (couldInsertInInner)
                {
                    currentSubSystems[i] = subSystem;
                    couldInsert          = true;
                    return;
                }
            }

            couldInsert = false;
        }
    }