public void TestCustomActivator()
        {
            var serializer = new YamlSerializer();
            var yaml       =
                @"%YAML 1.2
---
!System.Drawing.SolidBrush
Color: Red
...
";

            SolidBrush b = null;

            try {
                b = (SolidBrush)serializer.Deserialize(yaml)[0];
            } catch (MissingMethodException) {
                // SolidBrush has no default constructor!
            }

            var config = new YamlConfig();

            config.AddActivator <SolidBrush>(() => new SolidBrush(Color.Black));
            serializer = new YamlSerializer(config);

            // Now the serializer knows how to activate an instance of SolidBrush.
            b = (SolidBrush)serializer.Deserialize(yaml)[0];

            Assert.AreEqual(b.Color, Color.Red);
        }
 public void CustomActivator()
 {
     var brush = new YamlMapping("Color", "Blue");
     brush.Tag = "!System.Drawing.SolidBrush";
     Assert.Throws<MissingMethodException>(()=>constructor.NodeToObject(brush, YamlNode.DefaultConfig));
     var config = new YamlConfig();
     config.AddActivator<System.Drawing.SolidBrush>(() => new System.Drawing.SolidBrush(System.Drawing.Color.Black));
     Assert.AreEqual(System.Drawing.Color.Blue, ((System.Drawing.SolidBrush)constructor.NodeToObject(brush, config)).Color);
 }
Example #3
0
        public void CustomActivator()
        {
            var brush = new YamlMapping("Color", "Blue");

            brush.Tag = "!System.Drawing.SolidBrush";
            Assert.Throws <MissingMethodException>(() => constructor.NodeToObject(brush, YamlNode.DefaultConfig));
            var config = new YamlConfig();

            config.AddActivator <System.Drawing.SolidBrush>(() => new System.Drawing.SolidBrush(System.Drawing.Color.Black));
            Assert.AreEqual(System.Drawing.Color.Blue, ((System.Drawing.SolidBrush)constructor.NodeToObject(brush, config)).Color);
        }
        public void CustomActivator()
        {
            var brush = new YamlMapping("Color", "Blue");

            brush.Tag = "!System.Drawing.SolidBrush";
            var config = new YamlConfig();

            config.Register(new LegacyTypeConverterFactory());

            config.LookupAssemblies.Add(typeof(System.Drawing.SolidBrush).Assembly);
            Assert.Throws <MissingMethodException>(() => constructor.NodeToObject(brush, new SerializerContext(config)));
            config.AddActivator <System.Drawing.SolidBrush>(() => new System.Drawing.SolidBrush(System.Drawing.Color.Black));
            Assert.AreEqual(System.Drawing.Color.Blue, ((System.Drawing.SolidBrush)constructor.NodeToObject(brush, new SerializerContext(config))).Color);
        }
Example #5
0
        /// <summary>
        /// Load a gameobject from a file
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="roomToInstantiateIn"></param>
        /// <param name="position"> </param>
        /// <param name="rotation"> </param>
        /// <param name="allowNetworkInstantiateIfHasNetworkView"></param>
        /// <param name="visibleToAll">makes all players in the room subscribed to the object</param>
        /// <param name="owner">owner of the loaded object if network instantiated.  By default, it is the server</param>
        /// <returns></returns>
        public static GameObject Load(string filePath, Room roomToInstantiateIn, bool allowNetworkInstantiateIfHasNetworkView = false, Vector3?position = null, Quaternion?rotation = null, bool visibleToAll = true, Player owner = null)
        {
            var dser = new GameObject();

            dser.Room = roomToInstantiateIn;
            var awakes         = new List <Action>();
            var config         = new YamlConfig();
            var actualFilePath = Path.Combine(ResourceFolder, filePath + ".prefab");

            config.AddActivator <GameObject>(() =>
            {
                //be returning an object we've already created, the AddComponent will work properly
                return(dser);
            });

            //config.AddActivator < List<ComponentTracker>>(() =>
            //{
            //    return dser.components;
            //});

            var trackers = new Stack <GameObject.ComponentTracker>();

            config.AddActivator <GameObject.ComponentTracker>(() =>
            {
                var ntrack = new GameObject.ComponentTracker();
                trackers.Push(ntrack);
                return(ntrack);
            });

            foreach (Type t in GetComponentTypes())
            {
                Type tLocal = t;
                if (tLocal == typeof(NetworkView) && !allowNetworkInstantiateIfHasNetworkView)
                {
                    Debug.LogWarning("[Resources.Load] file {0} has a NetworkView component on it, but was run as to not network instantiate. It will not be networked.", actualFilePath);
                }
                GameObject dser1 = dser;
                config.AddActivator(tLocal, () =>
                {
                    Action awake;
                    var ntrack = trackers.Pop();
                    var ret    = dser1.DeserializeAddComponent(tLocal, out awake, ntrack);
                    awakes.Add(awake);
                    return(ret);
                });
            }

            var serializer = new YamlSerializer(config);

            serializer.DeserializeFromFile(actualFilePath, typeof(GameObject));

            if (dser.Resource == null)
            {
                dser.Resource = filePath;
            }

            if (position.HasValue)
            {
                dser.Position = position.Value;
            }
            if (rotation.HasValue)
            {
                dser.Rotation = rotation.Value;
            }

            roomToInstantiateIn.OnGameObjectAdded(dser);

            foreach (var awake in awakes)
            {
                if (awake != null)
                {
                    awake();
                }
            }

            dser.OnComponentAfterDeserialization();

            if (allowNetworkInstantiateIfHasNetworkView && dser.GetComponent <NetworkView>() != null)
            {
                roomToInstantiateIn.ResourceNetworkInstantiate(dser, visibleToAll, owner);
            }

            return(dser);
        }
Example #6
0
        /// <summary>
        /// Load a gameobject from a file
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="roomToInstantiateIn"></param>
        /// <param name="position"> </param>
        /// <param name="rotation"> </param>
        /// <param name="allowNetworkInstantiateIfHasNetworkView"></param>
        /// <param name="visibleToAll">makes all players in the room subscribed to the object</param>
        /// <param name="owner">owner of the loaded object if network instantiated.  By default, it is the server</param>
        /// <returns></returns>
        public static GameObject Load(string filePath, Room roomToInstantiateIn, bool allowNetworkInstantiateIfHasNetworkView = false, Vector3? position = null, Quaternion? rotation = null, bool visibleToAll = true, Player owner = null)
        {
            var dser = new GameObject();
            dser.Room = roomToInstantiateIn;
            var awakes = new List<Action>();
            var config = new YamlConfig();
            var actualFilePath = Path.Combine(ResourceFolder, filePath + ".prefab");

            config.AddActivator<GameObject>(() =>
            {
                //be returning an object we've already created, the AddComponent will work properly
                return dser;
            });

            //config.AddActivator < List<ComponentTracker>>(() =>
            //{
            //    return dser.components;
            //});

            var trackers = new Stack<GameObject.ComponentTracker>();

            config.AddActivator<GameObject.ComponentTracker>(() =>
            {
                var ntrack = new GameObject.ComponentTracker();
                trackers.Push(ntrack);
                return ntrack;
            });

            foreach (Type t in GetComponentTypes())
            {
                Type tLocal = t;
                if (tLocal == typeof(NetworkView) && !allowNetworkInstantiateIfHasNetworkView)
                {
                    Debug.LogWarning("[Resources.Load] file {0} has a NetworkView component on it, but was run as to not network instantiate. It will not be networked.", actualFilePath);
                }
                GameObject dser1 = dser;
                config.AddActivator(tLocal, () =>
                {
                    Action awake;
                    var ntrack = trackers.Pop();
                    var ret = dser1.DeserializeAddComponent(tLocal, out awake, ntrack);
                    awakes.Add(awake);
                    return ret;
                });
            }

            var serializer = new YamlSerializer(config);
            serializer.DeserializeFromFile(actualFilePath, typeof(GameObject));

            if (dser.Resource == null)
                dser.Resource = filePath;

            if (position.HasValue)
                dser.Position = position.Value;
            if (rotation.HasValue)
                dser.Rotation = rotation.Value;

            roomToInstantiateIn.OnGameObjectAdded(dser);

            foreach (var awake in awakes)
                if (awake != null) awake();
            
            dser.OnComponentAfterDeserialization();

            if (allowNetworkInstantiateIfHasNetworkView && dser.GetComponent<NetworkView>() != null)
                roomToInstantiateIn.ResourceNetworkInstantiate(dser, visibleToAll, owner);

            return dser;
        }
        public void TestCustomActivator()
        {
            var serializer = new YamlSerializer();
            var yaml =
              @"%YAML 1.2
---
!System.Drawing.SolidBrush
Color: Red
...
";

            SolidBrush b = null;
            try {
                b = (SolidBrush)serializer.Deserialize(yaml)[0];
            } catch ( MissingMethodException ) {
                // SolidBrush has no default constructor!
            }

            var config = new YamlConfig();
            config.AddActivator<SolidBrush>(() => new SolidBrush(Color.Black));
            serializer = new YamlSerializer(config);

            // Now the serializer knows how to activate an instance of SolidBrush.
            b = (SolidBrush)serializer.Deserialize(yaml)[0];

            Assert.AreEqual(b.Color, Color.Red);
        
        }
        public void CustomActivator()
        {
            var brush = new YamlMapping("Color", "Blue");
            brush.Tag = "!System.Drawing.SolidBrush";
            var config = new YamlConfig();
            config.Register(new LegacyTypeConverterFactory());

            config.LookupAssemblies.Add(typeof(System.Drawing.SolidBrush).Assembly);
            Assert.Throws<MissingMethodException>(() => constructor.NodeToObject(brush, new SerializerContext(config)));
            config.AddActivator<System.Drawing.SolidBrush>(() => new System.Drawing.SolidBrush(System.Drawing.Color.Black));
            Assert.AreEqual(System.Drawing.Color.Blue, ((System.Drawing.SolidBrush)constructor.NodeToObject(brush, new SerializerContext(config))).Color);
        }