public void LaunchesToolset()
        {
            Process[] processes = Process.GetProcessesByName(Nwn2ToolsetFunctions.ToolsetLauncherDefaultName);
            if (processes.Length > 0)
            {
                Assert.Fail("Toolset was already running when test began.");
            }

            Nwn2ToolsetFunctions.RunNeverwinterNightsTwoToolset();

            for (int i = 0; i < 20; i++)               // make 20 checks over 5 seconds before failing.
            {
                processes = Process.GetProcessesByName(Nwn2ToolsetFunctions.ToolsetLauncherDefaultName);

                if (processes.Length == 1)
                {
                    processes[0].Kill();                     // kill the toolset once the process has been found
                    return;
                }

                Assert.Less(processes.Length, 2, "Only 1 toolset process should be running, " +
                            "but " + processes.Length + " were found.");

                Thread.Sleep(250);
            }

            Assert.Fail("Toolset did not run (within 5 seconds of method call).");
        }
        public void KillsToolset()
        {
            Process[] processes = Process.GetProcessesByName(Nwn2ToolsetFunctions.ToolsetLauncherDefaultName);
            if (processes.Length > 0)
            {
                Assert.Fail("Toolset was already running when test began.");
            }

            Nwn2ToolsetFunctions.RunNeverwinterNightsTwoToolset();

            Process toolset = null;

            for (int i = 0; i < 20; i++)               // check for 5 seconds before failing.
            {
                processes = Process.GetProcessesByName(Nwn2ToolsetFunctions.ToolsetLauncherDefaultName);

                if (processes.Length == 1)
                {
                    toolset = processes[0];
                    break;
                }

                Assert.Less(processes.Length, 2, "Only 1 toolset process should be running, " +
                            "but " + processes.Length + " were found.");

                Thread.Sleep(250);
            }

            Assert.IsNotNull(toolset, "Toolset did not run, so the toolset process could not be ended.");

            Nwn2ToolsetFunctions.KillNeverwinterNightsTwoToolset();

            for (int i = 0; i < 20; i++)               // check for 5 seconds before failing.
            {
                if (toolset.HasExited)
                {
                    return;
                }
                Thread.Sleep(250);
            }

            Assert.Fail("Toolset process was not ended.");
        }
Esempio n. 3
0
        /// <summary>
        /// Translates Flip source into NWScript, compiles it,
        /// and attaches the results to a Neverwinter Nights 2 module.
        /// </summary>
        /// <param name="source">The Flip source to be compiled.</param>
        /// <param name="address">An address representing the location
        /// to attach this script to.</param>
        /// <returns>The name the script was saved under.</returns>
        public override string Attach(FlipScript source, string address)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }
            if (address == null)
            {
                throw new ArgumentNullException("address");
            }
            if (address == String.Empty)
            {
                throw new ArgumentException("Must provide a valid address for attaching script.", "address");
            }

            if (!Nwn2ToolsetFunctions.ToolsetIsOpen())
            {
                throw new InvalidOperationException("Toolset must be open to attach scripts.");
            }

            NWN2GameModule module = session.GetModule();

            if (module == null)
            {
                throw new InvalidOperationException("No module is currently open in the toolset.");
            }

            string name = GetUnusedName();

            try {
                if (name.Length > 32)
                {
                    throw new ApplicationException("Cannot attach script under the generated name ('" + name + "') " +
                                                   "because it is of length " + name.Length + ", and the maximum " +
                                                   "valid length of a resource name for NWN2 is 32.");
                }

                NWN2GameScript script = session.AddScript(name, source.Code);

                session.CompileScript(script);

                if (address.StartsWith("Conversation"))
                {
                    Nwn2ConversationAddress convAddress = new Nwn2ConversationAddress(address);

                    NWN2GameConversation conversation = session.GetConversation(convAddress.Conversation);

                    if (conversation == null)
                    {
                        throw new ArgumentException("Conversation '" + convAddress.Conversation + "' was not found in current module.", "address");
                    }

//					foreach (NWN2Toolset.NWN2.Views.INWN2Viewer v in NWN2Toolset.NWN2ToolsetMainForm.App.GetAllViewers()) {
//						NWN2Toolset.NWN2.Views.NWN2ConversationViewer cv = v as NWN2Toolset.NWN2.Views.NWN2ConversationViewer;
//						if (cv != null) {
//							System.Windows.MessageBox.Show("From viewer...\n" + cv.Conversation.Name +
//							                               "\nConnectors: " + cv.Conversation.AllConnectors.Count + "\n" +
//							                               "Entries: " + cv.Conversation.Entries.Count + "\n" +
//							                               "Replies: " + cv.Conversation.Replies.Count + "\n" +
//							                               "Loaded: " + conversation.Loaded);
//						}
//					}
//
//
//							System.Windows.MessageBox.Show("From module, before insisting on load...\n" + conversation.Name +
//							                               "\nConnectors: " + conversation.AllConnectors.Count + "\n" +
//							                               "Entries: " + conversation.Entries.Count + "\n" +
//							                               "Replies: " + conversation.Replies.Count + "\n" +
//							                               "Loaded: " + conversation.Loaded);
//
//
//
//					if (!conversation.Loaded) conversation.Demand();
//
//
//							System.Windows.MessageBox.Show("From module, after insisting on load...\n" + conversation.Name +
//							                               "\nConnectors: " + conversation.AllConnectors.Count + "\n" +
//							                               "Entries: " + conversation.Entries.Count + "\n" +
//							                               "Replies: " + conversation.Replies.Count + "\n" +
//							                               "Loaded: " + conversation.Loaded);



                    NWN2ConversationLine line = session.GetConversationLine(conversation, convAddress.LineID);

                    if (line == null)
                    {
                        throw new ArgumentException("Line with ID " + convAddress.LineID + " was not found in current module.", "address");
                    }

                    if (convAddress.AttachedAs == ScriptType.Conditional)
                    {
                        session.AttachScriptToConversationAsCondition(script, line, conversation);
                    }

                    else
                    {
                        session.AttachScriptToConversation(script, line, conversation);
                    }
                }

                else
                {
                    Nwn2Address nwn2Address = new Nwn2Address(address);

                    if (!Scripts.IsEventRaiser(nwn2Address.TargetType))
                    {
                        throw new ArgumentException("Cannot attach scripts to a " + nwn2Address.TargetType + ".");
                    }

                    if (nwn2Address.TargetType == Nwn2Type.Module)
                    {
                        session.AttachScriptToModule(script, nwn2Address.TargetSlot);
                    }

                    else if (nwn2Address.TargetType == Nwn2Type.Area)
                    {
                        NWN2GameArea area = session.GetArea(nwn2Address.AreaTag);
                        if (area == null)
                        {
                            throw new ArgumentException("Area '" + nwn2Address.AreaTag + "' was not found in current module.", "address");
                        }

                        session.AttachScriptToArea(script, area, nwn2Address.TargetSlot);
                    }

                    else
                    {
                        /*
                         * We want to attach to ALL instances matching the address in ALL OPEN areas, ignoring AreaTag and UseIndex.
                         */

                        NWN2ObjectType nwn2ObjectType = Nwn2ScriptSlot.GetObjectType(nwn2Address.TargetType).Value;

                        bool attached = false;

                        foreach (NWN2GameArea a in module.Areas.Values)
                        {
                            if (!session.AreaIsOpen(a))
                            {
                                continue;
                            }

                            NWN2InstanceCollection instances = session.GetObjectsByTag(a, nwn2ObjectType, nwn2Address.InstanceTag);

                            foreach (INWN2Instance instance in instances)
                            {
                                session.AttachScriptToObject(script, instance, nwn2Address.TargetSlot);
                                attached = true;
                            }
                        }

                        /*
                         * We also want to attach to any blueprints which use this tag as their resref.
                         */

                        // First check that if a blueprint which uses this tag as resref exists, it was created by Narrative Threads:
                        if (nt.CreatedByNarrativeThreads(Nwn2ScriptSlot.GetNwn2Type(nwn2ObjectType), nwn2Address.InstanceTag))
                        {
                            INWN2Blueprint blueprint = session.GetBlueprint(nwn2Address.InstanceTag.ToLower(), nwn2ObjectType);

                            if (blueprint != null)
                            {
                                session.AttachScriptToBlueprint(script, blueprint, nwn2Address.TargetSlot);
                                attached = true;
                            }
                        }

                        if (!attached)
                        {
                            string error;

                            if (nt.IsLoaded)
                            {
                                error = String.Format("There isn't a {0} with tag '{1}' in any of the areas that are open, or " +
                                                      "in the blueprints collection.",
                                                      nwn2Address.TargetType,
                                                      nwn2Address.InstanceTag);
                            }
                            else
                            {
                                error = String.Format("There isn't a {0} with tag '{1}' in any of the areas that are open.",
                                                      nwn2Address.TargetType,
                                                      nwn2Address.InstanceTag);
                            }

                            throw new MatchingInstanceNotFoundException(error, nwn2Address);
                        }
                    }
                }
            }
            catch (MatchingInstanceNotFoundException x) {
                throw x;
            }
            catch (Exception x) {
                throw new ApplicationException("Something went wrong while saving and attaching script.", x);
            }

            if (backups != null)
            {
                if (!Directory.Exists(backups))
                {
                    try {
                        Directory.CreateDirectory(backups);
                    }
                    catch (Exception) {
                        return(name);
                    }
                }

                string saveTo;

                if (createFoldersForUsers)
                {
                    try {
                        saveTo = Path.Combine(backups, Environment.UserName);
                        if (!Directory.Exists(saveTo))
                        {
                            Directory.CreateDirectory(saveTo);
                        }

                        saveTo = Path.Combine(saveTo, module.Name);
                        if (!Directory.Exists(saveTo))
                        {
                            Directory.CreateDirectory(saveTo);
                        }
                    }
                    catch (Exception) {
                        saveTo = backups;
                    }
                }
                else
                {
                    saveTo = backups;
                }

                try {
                    WriteBackup(name, saveTo, source.Code);
                }
                catch (Exception) {     }
            }

            return(name);
        }