protected override void PackToCore(Packer packer, Delegate objectTree)
        {
            if (objectTree is CallbackDelegate)
            {
                var funcRef = objectTree.Method.DeclaringType?.GetFields(BindingFlags.NonPublic |
                                                                         BindingFlags.Instance |
                                                                         BindingFlags.Public |
                                                                         BindingFlags.Static).FirstOrDefault(a => a.FieldType == typeof(RemoteFunctionReference));

                if (funcRef == null)
                {
                    throw new ArgumentException("The CallbackDelegate does not contain a RemoteFunctionReference capture.");
                }

                var fr = (RemoteFunctionReference)funcRef.GetValue(objectTree.Target);

                packer.PackExtendedTypeValue(10, fr.Duplicate());
            }
            else
            {
                var funcRefDetails = FunctionReference.Create(objectTree);
                var refType        = InternalManager.CanonicalizeRef(funcRefDetails.Identifier);

                packer.PackExtendedTypeValue(10, Encoding.UTF8.GetBytes(refType));
            }
        }
        public void Create(IScriptHost host)
        {
            try
            {
                m_scriptHost = host;

                ((IScriptHostWithResourceData)host).GetResourceName(out var nameString);
                string resourceName = Marshal.PtrToStringAnsi(nameString);

                bool useTaskScheduler = true;

#if IS_FXSERVER
                string basePath = "";

                {
                    // we can't invoke natives if not doing this
                    InternalManager.ScriptHost = host;

                    basePath         = Native.API.GetResourcePath(resourceName);
                    useTaskScheduler = Native.API.GetNumResourceMetadata(resourceName, "clr_disable_task_scheduler") == 0;
                }
#endif

                m_appDomain = AppDomain.CreateDomain($"ScriptDomain_{m_instanceId}", AppDomain.CurrentDomain.Evidence, new AppDomainSetup()
                {
#if IS_FXSERVER
                    ApplicationBase = basePath
#endif
                });

                m_appDomain.SetupInformation.ConfigurationFile = "dummy.config";

                m_intManager = (InternalManager)m_appDomain.CreateInstanceAndUnwrap(typeof(InternalManager).Assembly.FullName, typeof(InternalManager).FullName);

                if (useTaskScheduler)
                {
                    m_intManager.CreateTaskScheduler();
                }

                m_intManager.SetResourceName(resourceName);

                // TODO: figure out a cleaner solution to Mono JIT corruption so server doesn't have to be slower
#if IS_FXSERVER
                m_intManager.SetScriptHost(new WrapScriptHost(host), m_instanceId);
#else
                m_intManager.SetScriptHost(Marshal.GetIUnknownForObject(host), m_instanceId);
#endif
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.ToString());

                if (e.InnerException != null)
                {
                    Debug.WriteLine(e.InnerException.ToString());
                }

                throw;
            }
        }
        public void Create(IScriptHost host)
        {
            try
            {
                m_scriptHost = host;

                m_appDomain = AppDomain.CreateDomain($"ScriptDomain_{m_instanceId}");
                m_appDomain.SetupInformation.ConfigurationFile = "dummy.config";

                m_intManager = (InternalManager)m_appDomain.CreateInstanceAndUnwrap(typeof(InternalManager).Assembly.FullName, typeof(InternalManager).FullName);

                // TODO: figure out a cleaner solution to Mono JIT corruption so server doesn't have to be slower
#if IS_FXSERVER
                m_intManager.SetScriptHost(new WrapScriptHost(host), m_instanceId);
#else
                m_intManager.SetScriptHost(Marshal.GetIUnknownForObject(host), m_instanceId);
#endif
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.ToString());

                if (e.InnerException != null)
                {
                    Debug.WriteLine(e.InnerException.ToString());
                }

                throw;
            }
        }
        public void Destroy()
        {
            AppDomain.Unload(m_appDomain);

            m_appDomain  = null;
            m_intManager = null;
        }
Beispiel #5
0
 private static void EnsureRuntimeHandler()
 {
     if (ms_runtimeHandler == null)
     {
         ms_runtimeHandler = InternalManager.CreateInstance <IScriptRuntimeHandler>(new Guid(0xc41e7194, 0x7556, 0x4c02, 0xba, 0x45, 0xa9, 0xc8, 0x4d, 0x18, 0xad, 0x43));
     }
 }
Beispiel #6
0
        public static void Tick()
        {
            var flowBlock = CitizenTaskScheduler.SuppressFlow();

            try
            {
                Action[] tasks;

                lock (m_scheduledTasks)
                {
                    tasks = m_scheduledTasks.ToArray();
                    m_scheduledTasks.Clear();
                }

                foreach (var task in tasks)
                {
                    try
                    {
                        task();
                    }
                    catch (Exception e)
                    {
                        InternalManager.PrintErrorInternal($"task continuation", e);
                    }
                }
            }
            finally
            {
            }

            flowBlock?.Undo();
        }
Beispiel #7
0
        public void Dispose()
        {
            if (!InternalManager.IsProfiling)
            {
                return;
            }

            InternalManager.ProfilerExitScope();
        }
Beispiel #8
0
        public ProfilerScope(Func <string> name)
        {
            if (!InternalManager.IsProfiling)
            {
                return;
            }

            InternalManager.ProfilerEnterScope(name() ?? "c# scope");
        }
Beispiel #9
0
        public void Create(IScriptHost host)
        {
            try
            {
                m_scriptHost = host;

#if IS_FXSERVER
                string resourceName = "";
                string basePath     = "";

                {
                    ((IScriptHostWithResourceData)host).GetResourceName(out var nameString);

                    resourceName = Marshal.PtrToStringAnsi(nameString);

                    // we can't invoke natives if not doing this
                    InternalManager.ScriptHost = host;

                    basePath = Native.API.GetResourcePath(resourceName);
                }

                // Load required assemblies by resource
                ms_appDomain.AssemblyResolve += (sender, args) =>
                {
                    var assemblyPath = Path.Combine(basePath, new AssemblyName(args.Name).Name + ".dll");
                    if (!File.Exists(assemblyPath))
                    {
                        return(null);
                    }

                    return(Assembly.LoadFile(assemblyPath));
                };
#endif

                m_intManager = (InternalManager)ms_appDomain.CreateInstanceAndUnwrap(
                    typeof(InternalManager).Assembly.FullName, typeof(InternalManager).FullName);

                // TODO: figure out a cleaner solution to Mono JIT corruption so server doesn't have to be slower
#if IS_FXSERVER
                m_intManager.SetScriptHost(new WrapScriptHost(host), m_instanceId);
#else
                m_intManager.SetScriptHost(Marshal.GetIUnknownForObject(host), m_instanceId);
#endif
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.ToString());

                if (e.InnerException != null)
                {
                    Debug.WriteLine(e.InnerException.ToString());
                }

                throw;
            }
        }
Beispiel #10
0
        public void Destroy()
        {
            m_intManager?.Destroy();

            AppDomain.Unload(m_appDomain);

            m_appDomain  = null;
            m_intManager = null;
            m_scriptHost = null;
        }
Beispiel #11
0
        static PushRuntime()
        {
            ms_runtimeHandler = InternalManager.CreateInstance <IScriptRuntimeHandler>(new Guid(0xc41e7194, 0x7556, 0x4c02, 0xba, 0x45, 0xa9, 0xc8, 0x4d, 0x18, 0xad, 0x43));

#if !IS_FXSERVER || OS_WIN
            ms_pushMethod = new FastMethod <Action <IntPtr, IntPtr> >("PushRuntime", ms_runtimeHandler, typeof(IScriptRuntimeHandler), 0);
            ms_popMethod  = new FastMethod <Action <IntPtr, IntPtr> >("PopRuntime", ms_runtimeHandler, typeof(IScriptRuntimeHandler), 2);

            ms_runtimeHandlerIface = Marshal.GetComInterfaceForObject(ms_runtimeHandler, typeof(IScriptRuntimeHandler));
#endif
        }
Beispiel #12
0
        public InternalManager()
        {
            //CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture;
            //CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.InvariantCulture;

            GlobalManager = this;

            Thread.CurrentThread.CurrentCulture   = CultureInfo.InvariantCulture;
            Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture;

            InitializeAssemblyResolver();

            CitizenTaskScheduler.Create();
        }
Beispiel #13
0
        public InternalManager()
        {
            //CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture;
            //CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.InvariantCulture;

            GlobalManager = this;

            Thread.CurrentThread.CurrentCulture   = CultureInfo.InvariantCulture;
            Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture;

            InitializeAssemblyResolver();
            CitizenTaskScheduler.Create();

#if !IS_FXSERVER
            SynchronizationContext.SetSynchronizationContext(new CitizenSynchronizationContext());
#endif
        }
Beispiel #14
0
        public void Create(IScriptHost host)
        {
            try
            {
                m_scriptHost = host;

                m_appDomain = AppDomain.CreateDomain($"ScriptDomain_{m_instanceId}");

                m_intManager = (InternalManager)m_appDomain.CreateInstanceAndUnwrap(typeof(InternalManager).Assembly.FullName, typeof(InternalManager).FullName);
                m_intManager.SetScriptHost(Marshal.GetIUnknownForObject(host), m_instanceId);
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.ToString());

                if (e.InnerException != null)
                {
                    Debug.WriteLine(e.InnerException.ToString());
                }

                throw;
            }
        }
Beispiel #15
0
 public static void UnregisterScript(BaseScript script)
 {
     InternalManager.RemoveScript(script);
 }
Beispiel #16
0
 public static void RegisterScript(BaseScript script)
 {
     InternalManager.AddScript(script);
 }
Beispiel #17
0
        private static IAsyncResult BeginDelay(int delay, AsyncCallback callback, object state)
        {
            InternalManager.AddDelay(delay, callback);

            return(new DummyAsyncResult());
        }
Beispiel #18
0
 public void Destroy()
 {
     m_intManager = null;
 }