Beispiel #1
0
        protected void DeserializeMembranes(MembraneDef membraneDef, Membrane membrane, List <Membrane> membraneList)
        {
            Dictionary <IReceptor, Point> receptorLocationMap  = new Dictionary <IReceptor, Point>();
            Dictionary <IReceptor, List <UserConfig> > configs = new Dictionary <IReceptor, List <UserConfig> >();
            Point noLocation = new Point(-1, -1);

            membrane.Name = membraneDef.Name;

            membraneDef.Receptors.ForEach(n =>
            {
                IReceptor r            = membrane.RegisterReceptor(n.Name, n.AssemblyName);
                receptorLocationMap[r] = n.Location;
                r.Enabled  = n.Enabled;
                configs[r] = n.UserConfigs;
            });

            // After registration, but before the NewReceptor fire event, set the drop point.
            // Load all the receptors defined in this membrane first.
            membrane.LoadReceptors((rec) =>
            {
                // Restore any user configuration values.
                List <UserConfig> configList;

                if (configs.TryGetValue(rec, out configList))
                {
                    configList.ForEach(uc =>
                    {
                        Type rt         = rec.Instance.GetType();
                        PropertyInfo pi = rt.GetProperty(uc.Name);
                        string strVal   = uc.Value;
                        object val      = Converter.Convert(strVal, pi.PropertyType);
                        pi.SetValue(rec.Instance, val);
                    });
                }

                Point p;

                // Internal receptors, like ourselves, will not be in this deserialized collection.
                if (receptorLocationMap.TryGetValue(rec, out p))
                {
                    if (p == noLocation)
                    {
                        VisualizerController.View.ClientDropPoint = VisualizerController.View.GetRandomLocation();
                    }
                    else
                    {
                        VisualizerController.View.ClientDropPoint = p;
                    }
                }
            });

            // Load the permeability configuration.  We do this after loading the receptors, because
            // this way the permeability has been initialized via the NewReceptor event hook.
            membraneDef.Permeabilities.ForEach(p =>
            {
                membrane.ProtocolPermeability[new PermeabilityKey()
                                              {
                                                  Protocol = p.Protocol, Direction = p.Direction
                                              }] = new PermeabilityConfiguration()
                {
                    Permeable = p.Permeable
                };
            });

            // Next, load the inner membrane and receptors.
            membraneDef.Membranes.ForEach(innerMembraneDef =>
            {
                Membrane innerMembrane = membrane.CreateInnerMembrane();
                membraneList.Add(innerMembrane);
                // Handled now by the NewMembrane event handler.
                // Each membrane needs a system receptor to handle, among other things, the carrier animation.
                // innerMembrane.RegisterReceptor("System", this);
                DeserializeMembranes(innerMembraneDef, innerMembrane, membraneList);
            });
        }