Esempio n. 1
0
        public void ReceiveMessage()
        {
            // The entire test harness is already in scope, so we are breaking that scope
            // so we can test if the playerconnection can allow message handlers to make temp allocs
            TempMemoryScope.ExitScope();

            InitSpoofConnection();

            PlayerConnection.instance.Register(spoofMessage.ToGuid(), (MessageEventArgs test) =>
            {
                // We want to ensure we can still use the temp allocator in our registered handler
                var tempAllocatedContainer = new NativeList <int>(Allocator.Temp);
                tempAllocatedContainer.Add(1); // Perform an operation that will validate the safetyhandle
                tempAllocatedContainer.Dispose();
            });

            DotsRuntime.UpdatePreFrame();
            SendSpoofMessage();
            Baselib_Timer_WaitForAtLeast(100);
            DotsRuntime.UpdatePostFrame(true); // Internally calls TransmistAndReceive();

            // Note we don't validate we received the spoofed message.
            // We do this to avoid ci issues that might occur if our message doesn't
            // get picked up over the socket in time. I feel comfortable enough that this test
            // will still validate it's purpose and not frequently fail silently on the farm
            // We don't loop until we receive the message as we explicitly want UpdatePostFrame()
            // to be the code that receives and handles the messages and we currently don't separate
            // the logic for Transmitting and Receiving messages.

            ShutdownSpoofConnection();

            TempMemoryScope.EnterScope(); // restore temp memscope
        }
Esempio n. 2
0
        public unsafe void TempAllocatorRewindFreesMemUsage()
        {
            ExitTempScopesLocally();

            void *[] mem = new void *[4096];
            for (int i = 0; i < mem.Length; i++)
            {
                TempMemoryScope.EnterScope();
                mem[i] = UnsafeUtility.Malloc(4096, 0, Collections.Allocator.Temp);
            }

            int memUsage = UnsafeUtility.GetTempUsed();

            for (int i = mem.Length - 1; i >= 0; i--)
            {
                UnsafeUtility.Free(mem[i], Collections.Allocator.Temp);
                Assert.AreEqual(memUsage, UnsafeUtility.GetTempUsed());

                TempMemoryScope.ExitScope();
                Assert.Less(UnsafeUtility.GetTempUsed(), memUsage);

                memUsage = UnsafeUtility.GetTempUsed();
            }

            Assert.Zero(memUsage);
            EnterTempScopesLocally();
        }
        private static void Main()
        {
            // One-time initialization per application
            DotsRuntime.Initialize();

            // Setup the global static string interning storage
            TempMemoryScope.EnterScope();
            WordStorage.Initialize();
            TempMemoryScope.ExitScope();

            // A UnityInstance can exist per game state (there may potentially be more than one)
            var unity = UnityInstance.Initialize();

            unity.OnTick = (double timestampInSeconds) =>
            {
                var shouldContinue = unity.Update(timestampInSeconds);
                if (shouldContinue == false)
                {
                    unity.Deinitialize();
                }
                return(shouldContinue);
            };

            // Anything which can come after EnterMainLoop must occur in an event because
            // on some platforms EnterMainLoop exits immediately and the application enters
            // an event-driven lifecycle.
            PlatformEvents.OnQuit          += (object sender, QuitEvent evt) => { Shutdown(); };
            PlatformEvents.OnSuspendResume += (object sender, SuspendResumeEvent evt) => { unity.Suspended = evt.Suspend; };

            // Run
            RunLoop.EnterMainLoop(unity.OnTick);

            // DON'T CALL ANY CLEANUP HERE!
        }
        private static void Shutdown()
        {
            // Cleanup of word storage
            TempMemoryScope.EnterScope();
            WordStorage.Shutdown();
            TempMemoryScope.ExitScope();

            DotsRuntime.Shutdown();
        }
Esempio n. 5
0
        public void Deinitialize()
        {
            if (m_StartupScenes.IsCreated)
            {
                m_StartupScenes.Dispose();
            }

            TempMemoryScope.EnterScope();
            m_World.Dispose();
            TypeManager.Shutdown();
            UnsafeUtility.Free(m_TimeData, Collections.Allocator.Persistent);
            TempMemoryScope.ExitScope();
        }
Esempio n. 6
0
        public void PerTestTearDown()
        {
            unsafe
            {
                MessageStreamManager.DestroyStreamBuilder(messageBuilder);
            }
#if ENABLE_PROFILER
            Profiler.Shutdown();
#endif
            Connection.Shutdown();
            SpoofExitInternal();
            TempMemoryScope.ExitScope();
        }
Esempio n. 7
0
        public static UnityInstance Initialize()
        {
#if DEBUG
            if (!DotsRuntime.Initialized)
            {
                throw new InvalidOperationException("Unity.Core.DotsRuntime.Initialize() must be called before a UnityInistance can be initialized");
            }
#endif
            BurstInit();

#if ENABLE_UNITY_COLLECTIONS_CHECKS
            NativeLeakDetection.Mode = NativeLeakDetectionMode.Enabled;
#endif
            TempMemoryScope.EnterScope();
            TypeManager.Initialize();
            var inst = new UnityInstance();
            TempMemoryScope.ExitScope();
            return(inst);
        }
Esempio n. 8
0
    public static int Main(string[] args)
    {
        // Not using UnityInstance.Initialize here because it also creates a world, and some tests exist
        // that expect to handle their own world life cycle which currently conflicts with our world design
        UnityInstance.BurstInit();

        DotsRuntime.Initialize();

        TempMemoryScope.EnterScope();
        // Static storage used for the whole lifetime of the process. Create it once here
        // so heap tracking tests won't fight over the storage causing alloc/free mismatches
        WordStorage.Initialize();

        // TypeManager initialization uses temp memory, so let's free it before creating a global scope
        Unity.Entities.TypeManager.Initialize();
        TempMemoryScope.ExitScope();

        // Should have stack trace with tests
        NativeLeakDetection.Mode = NativeLeakDetectionMode.EnabledWithStackTrace;

        int result = 0;

#if UNITY_PORTABLE_TEST_RUNNER
        double start = Time.timeAsDouble;
        UnitTestRunner.Run();
        double end = Time.timeAsDouble;
        PrintResults(end - start);
#else
        result = new AutoRun().Execute(args);
#endif

        TempMemoryScope.EnterScope();
        Unity.Entities.TypeManager.Shutdown();
        WordStorage.Shutdown();
        TempMemoryScope.ExitScope();

        DotsRuntime.Shutdown();

        return(result);
    }
Esempio n. 9
0
        [PreserveBody] // Workaround for case 1276535
        public bool Update(double timestampInSeconds)
        {
            var shouldContinue = true;

            if (m_BootPhase == BootPhase.Running)
            {
                switch (m_RunState)
                {
                case RunState.Resuming:
                    m_RunState = RunState.Running;

                    m_StartTimeInSeconds           = timestampInSeconds - m_ElapsedTimeInSeconds;
                    m_PreviousElapsedTimeInSeconds = m_ElapsedTimeInSeconds;
                    goto case RunState.Running;

                case RunState.Running:
                    ComputeTime(timestampInSeconds);
                    *m_TimeData = new TimeData(
                        elapsedTime: m_ElapsedTimeInSeconds,
                        deltaTime: (float)m_DeltaTimeInSeconds);

                    DotsRuntime.UpdatePreFrame();

                    m_World.Update();
                    shouldContinue = !m_World.QuitUpdate;

                    DotsRuntime.UpdatePostFrame(shouldContinue);
                    break;
                }
            }
            else
            {
                TempMemoryScope.EnterScope();
                if (m_BootPhase == BootPhase.Booting)
                {
                    UpdateBooting();
                }
                else if (m_BootPhase == BootPhase.LoadingConfig)
                {
                    shouldContinue = UpdateLoading();

                    if (m_BootPhase == BootPhase.Running)
                    {
                        // Loaded - set up dots runtime with config info
                        var config = m_EntityManager.GetComponentData <CoreConfig>(m_ConfigEntity);

#if ENABLE_PLAYERCONNECTION
                        Connection.InitializeMulticast(config.editorGuid32, "DOTS_Runtime_Game");
#endif

#if ENABLE_PROFILER
                        ProfilerStats.Stats.debugStats.m_UnityVersionMajor              = config.editorVersionMajor;
                        ProfilerStats.Stats.debugStats.m_UnityVersionMinor              = config.editorVersionMinor;
                        ProfilerStats.Stats.debugStats.m_UnityVersionRevision           = config.editorVersionRevision;
                        ProfilerStats.Stats.debugStats.m_UnityVersionReleaseType        = config.editorVersionReleaseType;
                        ProfilerStats.Stats.debugStats.m_UnityVersionIncrementalVersion = config.editorVersionInc;
#endif
                        // Hook instance time into each world if the required system exists
                        foreach (var world in World.All)
                        {
                            var timeSystem = world.GetExistingSystem <UpdateWorldTimeSystem>();
                            if (timeSystem == null)
                            {
                                continue;
                            }
                            timeSystem.SetInstanceTime(m_TimeData);
                        }
                    }
                }
                else
                {
                    throw new Exception($"Invalid BootPhase specified: {(int)m_BootPhase}");
                }
                TempMemoryScope.ExitScope();
            }

            return(shouldContinue);
        }
Esempio n. 10
0
        static void ExitTempScopesLocally()
        {
            TempMemoryScope.ExitScope();  // Per-test scope

            Assert.Zero(UnsafeUtility.GetTempUsed());
        }
 public void PerTestTearDown()
 {
     PlayerConnectionProfiler.mode.Data = ProfilerModes.ProfileDisabled;
     Unity.Development.Profiling.Profiler.Shutdown();
     TempMemoryScope.ExitScope();
 }