Esempio n. 1
0
        public void Register(Type registrar, Type type, InstanceBehaviour instanceBehaviour)
        {
            TypeInformation registerInfo = null;

            if (!typeInformationRegister.ContainsKey(type))
            {
                registerInfo = new TypeInformation(this, type, instanceBehaviour);
                typeInformationRegister.Add(type, registerInfo);
            }

            typeRegister.Add(registrar, type);

            var removelist = new List <TypeInformation>();

            foreach (var typeInformation in uncompletedList)
            {
                typeInformation.RecreateUncompleteCtors();
                if (typeInformation.Completed)
                {
                    removelist.Add(typeInformation);
                }
            }

            uncompletedList.RemoveWhere(t => removelist.Contains(t));
            if (registerInfo != null && !registerInfo.Completed)
            {
                uncompletedList.Add(registerInfo);
            }
        }
Esempio n. 2
0
 public TypeInformation(StandaloneTypeContainer container,
                        Type type, InstanceBehaviour instanceBehaviour, object instance = null)
 {
     this.type         = type;
     Behaviour         = instanceBehaviour;
     typeContainer     = container;
     singeltonInstance = instance;
 }
Esempio n. 3
0
 /// <summary>
 /// Checks whether a given behaviour represents an instance which
 /// appears to have been created by Narrative Threads.
 /// </summary>
 /// <param name="behaviour">The behaviour to check.</param>
 /// <returns>True if the behaviour represents something which appears to have
 /// been created by Narrative Threads; false otherwise.</returns>
 public bool CreatedByNarrativeThreads(InstanceBehaviour behaviour)
 {
     if (behaviour == null)
     {
         throw new ArgumentNullException("behaviour");
     }
     return(CreatedByNarrativeThreads(behaviour.Nwn2Type, behaviour.ResRef));
 }
Esempio n. 4
0
        public void Register(Type registrar, Type type, InstanceBehaviour instanceBehaviour)
        {
            if (!typeInformationRegister.ContainsKey(type))
            {
                typeInformationRegister.Add(type, new TypeInformation(this, type, instanceBehaviour));
            }

            typeRegister.Add(registrar, type);
        }
        public override string GetAddress()
        {
            if (RaiserBlock == null)
            {
                throw new InvalidOperationException("RaiserBlock must be filled in to get a valid address.");
            }

            InstanceBehaviour behaviour = (InstanceBehaviour)RaiserBlock.Behaviour;

            return(addressFactory.GetInstanceAddress("OnUnlock", behaviour.AreaTag, behaviour.Nwn2Type, RaiserBlock.Identifier).Value);
        }
Esempio n. 6
0
        public override string GetCode(params string[] args)
        {
            if (args.Length != parameterCount)
            {
                throw new ArgumentException("Must pass exactly " + parameterCount + " parameters.", "args");
            }

            string sTag         = InstanceBehaviour.GetTagFromCode(args[1]);
            string oDestination = String.Format("GetObjectInArea({0},{1})", sTag, args[0]);

            return(String.Format("JumpPartyToArea({0},{1});", PlayerBehaviour.NWScript_GetPlayer, oDestination));
        }
Esempio n. 7
0
        public ObjectBlock CreateInstanceBlock(INWN2Instance instance, NWN2GameArea area)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }

            InstanceBehaviour behaviour = CreateInstanceBehaviour(instance, area);

            ObjectBlock block = CreateInstanceBlock(behaviour);

            return(block);
        }
        public override string GetCode(params string[] args)
        {
            if (args.Length != parameterCount)
            {
                throw new ArgumentException("Must pass exactly " + parameterCount + " parameters.", "args");
            }

            string sTag      = InstanceBehaviour.GetTagFromCode(args[0]);
            string sTemplate = String.Format("GetSafeResRef({0})", sTag);
            string lLocation = String.Format("GetLocation({0})", args[1]);

            return(String.Format("CreateObject(OBJECT_TYPE_CREATURE,{0},{1},TRUE,{2});", sTemplate, lLocation, sTag));
        }
Esempio n. 9
0
        public ObjectBlock CreateInstanceBlockFromBlueprint(INWN2Blueprint blueprint)
        {
            if (blueprint == null)
            {
                throw new ArgumentNullException("blueprint");
            }

            InstanceBehaviour behaviour = CreateInstanceBehaviourFromBlueprint(blueprint);

            ObjectBlock block = CreateInstanceBlock(behaviour);

            return(block);
        }
Esempio n. 10
0
            public TypeInformation(StandaloneTypeContainer container,
                                   Type type, InstanceBehaviour instanceBehaviour, object instance = null)
            {
                this.type         = type;
                Behaviour         = instanceBehaviour;
                typeContainer     = container;
                singeltonInstance = instance;
                ctors             = container
                                    .GetCtorInformations(type)
                                    .OrderByDescending(ctor => ctor.Length)
                                    .ToList();

                Completed = !ctors.Any(c => !c.IsComplete);
            }
Esempio n. 11
0
        public Image GetImage(InstanceBehaviour behaviour)
        {
            Image  image;
            string objectType = behaviour.Nwn2Type.ToString();

            // First, try to get a Narrative Threads user-created image:
            if (nt != null && nt.CreatedByNarrativeThreads(behaviour) && nt.HasImage(behaviour.ResRef))
            {
                image = nt.GetImageForResRef(behaviour.ResRef);
            }

            // Or if this has an icon, use that:
            else if (!String.IsNullOrEmpty(behaviour.IconName))
            {
                image = GetIconImage(behaviour.IconName);
            }

            // Otherwise, try to get a generic image representing this instance's template:
            else
            {
                image = GetImage(objectType, behaviour.ResRef);
            }

            // If neither is available, use a placeholder image:
            if (image == null)
            {
                if (behaviour.Nwn2Type == Nwn2Type.Item)
                {
                    image = GetImage("Placeholder", "Item");
                }
                else if (behaviour.Nwn2Type == Nwn2Type.Waypoint)
                {
                    image = GetImage("Placeholder", "Waypoint");
                }
                else
                {
                    image = GetImage("Placeholder", "Instance");
                }
            }

            return(image);
        }
Esempio n. 12
0
        public ObjectBlock CreateInstanceBlock(InstanceBehaviour behaviour)
        {
            if (behaviour == null)
            {
                throw new ArgumentNullException("behaviour");
            }

            ObjectBlock block = new ObjectBlock(null, behaviour);

            block.AssignImage(images);

            if (helper != null)
            {
                Brush brush = helper.GetBrushForType(behaviour.Nwn2Type);
                if (brush != null)
                {
                    block.Colour = brush;
                }
            }

            return(block);
        }
Esempio n. 13
0
 public static void Register <TRegistrar, T>(InstanceBehaviour instanceBehaviour = InstanceBehaviour.Instance) where T : class
 => instance.Register <TRegistrar, T>(instanceBehaviour);
Esempio n. 14
0
 public static void Register(Type registrar, Type type, InstanceBehaviour instanceBehaviour)
 => instance.Register(registrar, type, instanceBehaviour);
Esempio n. 15
0
        protected void TrackToolsetChanges(ToolsetEventReporter reporter)
        {
            if (!reporter.IsRunning)
            {
                reporter.Start();
            }

            reporter.ModuleChanged += delegate(object oSender, ModuleChangedEventArgs eArgs)
            {
                /* Fires when a module closes, but that doesn't mean that the new module has
                 * been fully opened yet... usually takes a while! */

                Action action = new Action
                                (
                    delegate()
                {
                    if (manager != null)
                    {
                        manager.EmptyBag(AreasBagName);

                        foreach (NWN2ObjectType type in Enum.GetValues(typeof(NWN2ObjectType)))
                        {
                            string bag = String.Format(InstanceBagNamingFormat, type);
                            if (manager.HasBag(bag))
                            {
                                manager.EmptyBag(bag);
                            }
                        }

                        manager.AddMoveable("Creatures", blocks.CreatePlayerBlock());
                    }
                }
                                );

                uiThreadAccess.Dispatcher.Invoke(DispatcherPriority.Normal, action);
            };


            reporter.InstanceAdded += delegate(object sender, InstanceEventArgs e)
            {
                if (manager == null)
                {
                    return;
                }

                string bag = String.Format(InstanceBagNamingFormat, e.Instance.ObjectType.ToString());
                if (!manager.HasBag(bag))
                {
                    return;
                }

                ObjectBlock block = blocks.CreateInstanceBlock(e.Instance, e.Area);
                manager.AddMoveable(bag, block);
            };


            reporter.InstanceRemoved += delegate(object sender, InstanceEventArgs e)
            {
                if (manager == null || e.Instance == null)
                {
                    return;
                }

                string bag = String.Format(InstanceBagNamingFormat, Nwn2ScriptSlot.GetNwn2Type(e.Instance.ObjectType));
                if (!manager.HasBag(bag))
                {
                    return;
                }

                try {
                    UIElementCollection collection = manager.GetMoveables(bag);

                    ObjectBlock lost = blocks.CreateInstanceBlock(e.Instance, e.Area);

                    foreach (ObjectBlock block in collection)
                    {
                        if (block.Equals(lost))
                        {
                            manager.RemoveMoveable(bag, block);
                            return;
                        }
                    }
                }
                catch (Exception) {}
            };


            reporter.BlueprintAdded += delegate(object sender, BlueprintEventArgs e)
            {
                if (manager == null || e.Blueprint == null)
                {
                    return;
                }

                if (nt.CreatedByNarrativeThreads(e.Blueprint))
                {
                    Thread thread = new Thread(new ParameterizedThreadStart(CreateNarrativeThreadsBlock));
                    thread.IsBackground = true;
                    thread.Start(e.Blueprint);
                }

                if (loadBlueprints)
                {
                    Action action = new Action
                                    (
                        delegate()
                    {
                        string bag = String.Format(BlueprintBagNamingFormat, e.Blueprint.ObjectType.ToString());

                        if (manager.HasBag(bag))
                        {
                            ObjectBlock block = blocks.CreateBlueprintBlock(e.Blueprint);
                            manager.AddMoveable(bag, block);
                        }
                    }
                                    );

                    uiThreadAccess.Dispatcher.Invoke(DispatcherPriority.Normal, action);
                }
            };


            reporter.BlueprintRemoved += delegate(object sender, BlueprintEventArgs e)
            {
                if (manager == null || e.Blueprint == null)
                {
                    return;
                }

                if (nt.CreatedByNarrativeThreads(e.Blueprint))
                {
                    Action action = new Action
                                    (
                        delegate()
                    {
                        string bag = String.Format(InstanceBagNamingFormat, e.Blueprint.ObjectType.ToString());

                        if (manager.HasBag(bag))
                        {
                            ObjectBlock lost     = blocks.CreateInstanceBlockFromBlueprint(e.Blueprint);
                            ObjectBlock removing = null;

                            foreach (ObjectBlock block in manager.GetMoveables(bag))
                            {
                                if (block.Equals(lost))
                                {
                                    removing = block;
                                    break;
                                }
                            }

                            if (removing != null)
                            {
                                manager.RemoveMoveable(bag, removing);
                            }
                        }
                    }
                                    );

                    uiThreadAccess.Dispatcher.Invoke(DispatcherPriority.Normal, action);
                }

                if (loadBlueprints)
                {
                    Action action = new Action
                                    (
                        delegate()
                    {
                        string bag = String.Format(BlueprintBagNamingFormat, e.Blueprint.ObjectType.ToString());

                        if (manager.HasBag(bag))
                        {
                            ObjectBlock lost     = blocks.CreateBlueprintBlock(e.Blueprint);
                            ObjectBlock removing = null;

                            foreach (ObjectBlock block in manager.GetMoveables(bag))
                            {
                                if (block.Equals(lost))
                                {
                                    removing = block;
                                    break;
                                }
                            }

                            if (removing != null)
                            {
                                manager.RemoveMoveable(bag, removing);
                            }
                        }
                    }
                                    );

                    uiThreadAccess.Dispatcher.Invoke(DispatcherPriority.Normal, action);
                }
            };


            reporter.AreaOpened += delegate(object sender, AreaEventArgs e)
            {
                if (manager == null)
                {
                    return;
                }

                Thread thread = new Thread(new ParameterizedThreadStart(CreateBlocksWhenAreaIsReady));
                thread.IsBackground = true;
                thread.Start(e.Area);
            };


            reporter.ResourceViewerClosed += delegate(object sender, ResourceViewerClosedEventArgs e)
            {
                if (manager == null)
                {
                    return;
                }

                foreach (Moveable moveable in manager.GetMoveables(AreasBagName))
                {
                    ObjectBlock block = moveable as ObjectBlock;
                    if (block == null)
                    {
                        continue;
                    }
                    AreaBehaviour area = block.Behaviour as AreaBehaviour;
                    if (area == null)
                    {
                        continue;
                    }

                    // Assumes that there are no conversations or scripts with the same name as an
                    // area, but there's no immediately apparent way around this:
                    // (TODO: Could check that module doesn't have a script or conversation of the same name.)
                    if (area.Identifier == e.ResourceName)
                    {
                        manager.RemoveMoveable(AreasBagName, moveable);
                        break;
                    }
                }

                if (NWN2ToolsetMainForm.App.Module != null && NWN2ToolsetMainForm.App.Module.Areas.ContainsCaseInsensitive(e.ResourceName))
                {
                    // At this point we think it's an area that's been closed, so remove
                    // any instances associated with that area:

                    foreach (NWN2ObjectType type in Enum.GetValues(typeof(NWN2ObjectType)))
                    {
                        string bag = String.Format(InstanceBagNamingFormat, type);
                        if (manager.HasBag(bag))
                        {
                            List <Moveable> removing = new List <Moveable>();

                            foreach (Moveable moveable in manager.GetMoveables(bag))
                            {
                                ObjectBlock block = moveable as ObjectBlock;
                                if (block == null)
                                {
                                    continue;
                                }
                                InstanceBehaviour instance = block.Behaviour as InstanceBehaviour;
                                if (instance == null)
                                {
                                    continue;
                                }

                                if (instance.AreaTag.ToLower() == e.ResourceName.ToLower())
                                {
                                    removing.Add(moveable);
                                }
                            }

                            foreach (Moveable moveable in removing)
                            {
                                manager.RemoveMoveable(bag, moveable);
                            }
                        }
                    }
                }
            };


            // Ensure that an area always has the same resource name and tag:
            reporter.AreaNameChanged += delegate(object oObject, NameChangedEventArgs eArgs)
            {
                NWN2GameArea area = eArgs.Item as NWN2GameArea;
                if (area == null)
                {
                    return;
                }

                string blockHasTag = area.Tag;

                if (area.Tag != area.Name)
                {
                    area.Tag = area.Name;
                }
                OEIShared.Utils.OEIExoLocString oeiStr = Nwn2Strings.GetOEIStringFromString(area.Name);
                if (area.DisplayName != oeiStr)
                {
                    area.DisplayName = oeiStr;
                }

                // Note that this will only work for areas that are currently open...
                // we'll deal with changing the name of closed areas when they open.


                // Update the area block, if the area is open:
                bool open = false;
                foreach (NWN2AreaViewer av in NWN2ToolsetMainForm.App.GetAllAreaViewers())
                {
                    if (av.Area == area)
                    {
                        open = true;
                        break;
                    }
                }
                if (!open)
                {
                    return;
                }

                AreaBehaviour behaviour = blocks.CreateAreaBehaviour(area);

                if (manager.HasBag(AreasBagName))
                {
                    UIElementCollection existingBlocks = manager.GetMoveables(AreasBagName);

                    bool updated = false;

                    foreach (UIElement u in existingBlocks)
                    {
                        ObjectBlock existing = u as ObjectBlock;
                        if (existing == null)
                        {
                            continue;
                        }
                        AreaBehaviour existingBehaviour = existing.Behaviour as AreaBehaviour;
                        if (existingBehaviour == null)
                        {
                            continue;
                        }

                        // If you find an area with the same tag, replace its behaviour to update it:
                        if (existingBehaviour.Tag == blockHasTag)
                        {
                            existing.Behaviour = behaviour;
                            updated            = true;
                            break;
                        }
                    }

                    if (!updated)
                    {
                        ObjectBlock block = blocks.CreateAreaBlock(behaviour);
                        manager.AddMoveable(AreasBagName, block, false);
                    }
                }
            };


            // If a script has its name changed, change it back:
            reporter.ScriptNameChanged += delegate(object oObject, NameChangedEventArgs eArgs)
            {
                Thread thread = new Thread(new ParameterizedThreadStart(ReverseScriptNameChange));
                thread.IsBackground = false;
                thread.Start(eArgs);
            };
        }
Esempio n. 16
0
        public void UpdateBlockWithNewTag(NWN2PropertyValueChangedEventArgs e)
        {
            try {
                if (tracking.Contains(e.PropertyName) && e.NewValue != e.OldValue)
                {
                    foreach (object o in e.ChangedObjects)
                    {
                        if (o is INWN2Instance)
                        {
                            INWN2Instance     instance  = (INWN2Instance)o;
                            InstanceBehaviour behaviour = blocks.CreateInstanceBehaviour(instance);

                            string bag = String.Format(Nwn2MoveableProvider.InstanceBagNamingFormat, instance.ObjectType);

                            if (window.BlockBox.HasBag(bag))
                            {
                                UIElementCollection existingBlocks = window.BlockBox.GetMoveables(bag);

                                // If it's the tag that's changed, use the old tag to search, otherwise use the current one:
                                string tag;
                                if (e.PropertyName == "Tag")
                                {
                                    tag = e.OldValue as string;
                                }
                                else
                                {
                                    tag = ((INWN2Object)instance).Tag;
                                }

                                bool updated = false;

                                foreach (UIElement u in existingBlocks)
                                {
                                    ObjectBlock existing = u as ObjectBlock;
                                    if (existing == null)
                                    {
                                        continue;
                                    }
                                    InstanceBehaviour existingBehaviour = existing.Behaviour as InstanceBehaviour;
                                    if (existingBehaviour == null)
                                    {
                                        continue;
                                    }

                                    // If you find an instance of the same type, resref and tag, replace its behaviour to update it:
                                    if (existingBehaviour.ResRef == behaviour.ResRef && existingBehaviour.Nwn2Type == behaviour.Nwn2Type && existingBehaviour.Tag == tag)
                                    {
                                        existing.Behaviour = behaviour;
                                        updated            = true;
                                        break;
                                    }
                                }

                                if (!updated)
                                {
                                    ObjectBlock block = blocks.CreateInstanceBlock(behaviour);
                                    window.BlockBox.AddMoveable(bag, block, false);
                                }
                            }


                            if (e.PropertyName == "Tag")
                            {
                                UpdateScriptsFollowingTagChange(instance, (string)e.OldValue, (string)e.NewValue, true);
                            }
                        }

                        else if (o is NWN2GameArea)
                        {
                            NWN2GameArea area = (NWN2GameArea)o;

                            // Refuse changes to tags or areas unless they are to match the resource name:
                            if (e.PropertyName == "Tag" && ((string)e.NewValue) != area.Name)
                            {
                                area.Tag = area.Name;
                                return;
                            }
                            else if (e.PropertyName == "Display Name" && (Nwn2Strings.GetStringFromOEIString((OEIShared.Utils.OEIExoLocString)e.NewValue) != area.Name))
                            {
                                area.DisplayName = Nwn2Strings.GetOEIStringFromString(area.Name);
                                return;
                            }

                            AreaBehaviour behaviour = blocks.CreateAreaBehaviour(area);

                            if (window.BlockBox.HasBag(Nwn2MoveableProvider.AreasBagName))
                            {
                                UIElementCollection existingBlocks = window.BlockBox.GetMoveables(Nwn2MoveableProvider.AreasBagName);

                                string tag;
                                if (e.PropertyName == "Tag")
                                {
                                    tag = e.OldValue as string;
                                }
                                else
                                {
                                    tag = area.Tag;
                                }

                                bool updated = false;

                                foreach (UIElement u in existingBlocks)
                                {
                                    ObjectBlock existing = u as ObjectBlock;
                                    if (existing == null)
                                    {
                                        continue;
                                    }
                                    AreaBehaviour existingBehaviour = existing.Behaviour as AreaBehaviour;
                                    if (existingBehaviour == null)
                                    {
                                        continue;
                                    }

                                    // If you find an area with the same tag, replace its behaviour to update it:
                                    if (existingBehaviour.Tag == tag)
                                    {
                                        existing.Behaviour = behaviour;
                                        updated            = true;
                                        break;
                                    }
                                }

                                if (!updated)
                                {
                                    ObjectBlock block = blocks.CreateAreaBlock(behaviour);
                                    window.BlockBox.AddMoveable(Nwn2MoveableProvider.AreasBagName, block, false);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception x) {
                MessageBox.Show("Something went wrong when updating a block.\n\n" + x);
            }
        }
Esempio n. 17
0
 public void Register <TRegistrar, T>(InstanceBehaviour instanceBehaviour = InstanceBehaviour.Instance) where T : class
 => Register(typeof(TRegistrar), typeof(T), instanceBehaviour);