/// <summary> /// Translates Flip source into the target language and /// returns the results as a string. /// </summary> /// <param name="script">The Flip source to be translated.</param> /// <returns>The source code of the target language.</returns> public override string Translate(FlipScript script) { SampleScriptProvider sample = new SampleScriptProvider(); string output = sample.GiveGold; return(output); }
public void AddConditionToConversationLine(NWN2ConversationConnector line, NWN2GameConversation conversation) { if (line == null) { throw new ArgumentNullException("line"); } if (conversation == null) { throw new ArgumentNullException("conversation"); } if (!conversation.AllConnectors.Contains(line)) { throw new ArgumentException("Line is not a part of the given conversation.", "line"); } LaunchFlip(); bool openingExistingScript = ScriptHelper.HasFlipScriptAttachedAsCondition(line); if (openingExistingScript && window.AskWhetherToSaveCurrentScript() == MessageBoxResult.Cancel) { return; } window.ConditionalFrame.Address = addressFactory.GetConversationAddress(conversation.Name, line.Line.LineGuid, ScriptType.Conditional).Value; if (openingExistingScript) { NWN2GameScript script = new NWN2GameScript(line.Conditions[0].Script); script.Demand(); FlipScript flipScript = scriptHelper.GetFlipScript(script, Attachment.Ignore); window.OpenFlipScript(new ScriptTriggerTuple(flipScript, null)); window.ConditionalFrame.Dialogue = Nwn2Strings.GetStringFromOEIString(line.Line.Text); //ActivityLog.Write(new Activity("OpenedScript","ScriptName",script.Name,"Event",String.Empty)); Log.WriteAction(LogAction.opened, "script", script.Name + "(attached as condition to line '" + line.Line.Text.GetSafeString(OEIShared.Utils.BWLanguages.CurrentLanguage) + "')"); } else { window.IsDirty = true; window.EnterConditionMode(); window.ConditionalFrame.Dialogue = Nwn2Strings.GetStringFromOEIString(line.Line.Text); //ActivityLog.Write(new Activity("NewScript","CreatedVia","AddingConditionToConversationLine","Event",String.Empty)); string lineText; try { lineText = line.Line.Text.GetSafeString(OEIShared.Utils.BWLanguages.CurrentLanguage).Value; } catch (Exception) { lineText = String.Empty; } Log.WriteAction(LogAction.added, "script", "as condition to a line of conversation ('" + lineText + "')"); } }
public void UseConversationLineAsTrigger(NWN2ConversationConnector line, NWN2GameConversation conversation) { if (line == null) { throw new ArgumentNullException("line"); } if (conversation == null) { throw new ArgumentNullException("conversation"); } if (!conversation.AllConnectors.Contains(line)) { throw new ArgumentException("Line is not a part of the given conversation.", "line"); } LaunchFlip(); window.LeaveConditionMode(); bool openingExistingScript = ScriptHelper.HasFlipScriptAttachedAsAction(line); if (openingExistingScript && window.AskWhetherToSaveCurrentScript() == MessageBoxResult.Cancel) { return; } TriggerControl trigger = triggers.GetTrigger(line, conversation); if (openingExistingScript) { NWN2GameScript script = new NWN2GameScript(line.Actions[0].Script); script.Demand(); FlipScript flipScript = scriptHelper.GetFlipScript(script, Attachment.Ignore); window.OpenFlipScript(new ScriptTriggerTuple(flipScript, trigger)); //ActivityLog.Write(new Activity("OpenedScript","ScriptName",script.Name,"Event",trigger.GetLogText())); Log.WriteAction(LogAction.opened, "script", script.Name + " (attached to line '" + line.Line.Text.GetSafeString(OEIShared.Utils.BWLanguages.CurrentLanguage) + "')"); } else { window.SetTrigger(trigger); window.IsDirty = true; //ActivityLog.Write(new Activity("NewScript","CreatedVia","UsingConversationLineAsEvent","Event",trigger.GetLogText())); string lineText; try { lineText = line.Line.Text.GetSafeString(OEIShared.Utils.BWLanguages.CurrentLanguage).Value; } catch (Exception) { lineText = String.Empty; } Log.WriteAction(LogAction.added, "script", "to a line of conversation ('" + lineText + "')"); } }
public void OpenScript(NWN2GameScript script) { if (script == null) { throw new ArgumentNullException("script"); } LaunchFlip(); if (window.AskWhetherToSaveCurrentScript() == MessageBoxResult.Cancel) { return; } script.Demand(); try { FlipScript flipScript = scriptHelper.GetFlipScript(script, Attachment.Ignore); if (flipScript == null) { throw new InvalidOperationException("Script could not be understood as a Flip script."); } TriggerControl trigger; try { trigger = scriptHelper.GetTrigger(script); } catch (ArgumentException) { trigger = null; } ScriptTriggerTuple tuple = new ScriptTriggerTuple(flipScript, trigger); window.OpenFlipScript(tuple); //ActivityLog.Write(new Activity("OpenedScript","ScriptName",script.Name,"Event",triggerTextForLog)); if (trigger != null) { Log.WriteAction(LogAction.opened, "script", script.Name + " (attached to '" + trigger.GetLogText() + "')"); } else { Log.WriteAction(LogAction.opened, "script", script.Name); } } catch (Exception x) { throw new ApplicationException("Failed to open script '" + script.Name + "'.", x); } }
//Functions/MEthods // access modifier, return datatypoe, methor name, parameters private void Awake() { playerCamera = Camera.main; // Camera.main uses fine object of tag internally. leftFlipper = GetFlipper(leftFlipperName); Assert.IsNotNull(leftFlipper, "Child: " + leftFlipperName + " was not found."); playerCamera = Camera.main; // Camera.main uses fine object of tag internally. rightFlipper = GetFlipper(rightFlipperName); Assert.IsNotNull(rightFlipper, "Child: " + rightFlipperName + " was not found."); Cursor.lockState = CursorLockMode.Confined; Cursor.visible = false; }
public List <ScriptTriggerTuple> GetAllScripts(Attachment attachment) { NWN2GameModule mod = NWN2ToolsetMainForm.App.Module; if (mod == null) { throw new InvalidOperationException("No module is open."); } List <ScriptTriggerTuple> tuples = new List <ScriptTriggerTuple>(); foreach (NWN2GameScript script in mod.Scripts.Values) { if (!WasCreatedByFlip(script)) { continue; } try { script.Demand(); FlipScript flipScript = GetFlipScript(script, attachment); if (flipScript != null) { TriggerControl trigger; if (attachment == Attachment.Ignore) { trigger = new BlankTrigger(flipScript.Name); } else { trigger = GetTrigger(script); } tuples.Add(new ScriptTriggerTuple(flipScript, trigger)); } } catch (Exception x) { MessageBox.Show("Something went wrong when trying to add " + script.Name + " to the set of openable scripts.\n\n" + x); } } return(tuples); }
public List <ScriptTriggerTuple> GetTuples(NWN2GameScriptDictionary dict) { if (dict == null) { throw new ArgumentNullException("dict"); } List <ScriptTriggerTuple> flipScripts = new List <ScriptTriggerTuple>(dict.Count); foreach (NWN2GameScript script in dict.Values) { try { FlipScript fs = GetFlipScript(script); flipScripts.Add(new ScriptTriggerTuple(fs)); } catch (Exception) {} } return(flipScripts); }
public ScriptTriggerTuple(FlipScript script) : this(script, null) { }
public ScriptTriggerTuple(FlipScript script, TriggerControl trigger) { this.script = script; this.trigger = trigger; }
/// <summary> /// Translates Flip source into the target language and /// generates a file containing the results. /// </summary> /// <param name="script">The Flip source to be translated.</param> /// <param name="path">The path at which to generate the output file.</param> public override void Translate(FlipScript script, string path) { string output = Translate(script); WriteToFile(output, path); }
public bool SaveScript(FlipWindow window) { if (window == null) { throw new ArgumentNullException("window"); } if (attacher == null) { throw new InvalidOperationException("No attacher to save scripts with."); } if (!window.IsComplete) { //ActivityLog.Write(new Activity("TriedToSaveIncompleteScript")); Log.WriteMessage("tried to save incomplete script"); MessageBox.Show("Your script isn't finished! Fill in all the blanks before saving."); return(false); } IScriptFrame scriptFrame = window.GetCurrentScriptFrame(); AbstractScriptWriter scriptWriter; if (window.Mode == ScriptType.Conditional) { scriptWriter = new ConditionalScriptWriter(window.ConditionalFrame); } else { scriptWriter = new ScriptWriter(window.TriggerBar); } string code = scriptWriter.GetCombinedCode(); FlipScript script = new FlipScript(code, window.Mode, String.Empty); string address = scriptFrame.GetAddress(); try { string savedAs = attacher.Attach(script, address); window.TriggerBar.CurrentScriptIsBasedOn = savedAs; window.ConditionalFrame.CurrentScriptIsBasedOn = savedAs; window.IsDirty = false; //ActivityLog.Write(new Activity("SavedScript","SavedAs",savedAs)); Log.WriteAction(LogAction.saved, "script", "as " + savedAs); //MessageBox.Show("Script was saved successfully."); try { string nl = window.NaturalLanguage; if (String.IsNullOrEmpty(nl)) { window.NaturalLanguage = "Saved."; } else { window.NaturalLanguage = nl + Environment.NewLine + "Saved."; } } catch (Exception) {} return(true); } catch (MatchingInstanceNotFoundException x) { //ActivityLog.Write(new Activity("TriedToSaveScriptButTargetCouldNotBeFound","TargetType",x.Address.TargetType.ToString(),"TargetTagOrResRef",x.Address.InstanceTag)); Log.WriteMessage("tried to save script but couldn't find target (was looking for " + x.Address.TargetType + " with tag/resref " + x.Address.InstanceTag + ")"); MessageBox.Show(String.Format("There's no {0} like this (with tag '{1}') in any area that's open.\nMake sure that the area containing " + "the {0} is open when you try to save, or it won't work.", x.Address.TargetType, x.Address.InstanceTag)); return(false); } catch (Exception x) { string errorMessage = x.ToString(); if (errorMessage.Contains("flip_functions")) { try { string path = Path.Combine(NWN2Toolset.NWN2.IO.NWN2ResourceManager.Instance.OverrideDirectory.DirectoryName, "flip_functions.nss"); if (!File.Exists(path)) { MessageBox.Show("File " + path + " is missing. Scripts will not compile successfully. Please re-install Flip."); return(false); } } catch (Exception) {} } MessageBox.Show(String.Format("Something went wrong when saving the script.{0}{0}{1}", Environment.NewLine, errorMessage)); return(false); } }
/// <summary> /// Translates Flip source into the target language and /// generates a file containing the results. /// </summary> /// <param name="script">The Flip source to be translated.</param> /// <param name="path">The path at which to generate the output file.</param> public override void Translate(FlipScript script, string path) { WriteToFile(script.Code, path); }
/// <summary> /// Translates Flip source into the target language and /// returns the results as a string. /// </summary> /// <param name="script">The Flip source to be translated.</param> /// <returns>The source code of the target language.</returns> public override string Translate(FlipScript script) { return(script.Code); }
/// <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); }
/// <summary> /// Translates Flip source into the language of the target game, compiles /// the generated code if necessary, and attaches the results to a /// module or level of that game. /// </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) { return(null); }
/// <summary> /// /// </summary> /// <param name="dict"></param> /// <param name="attachment"></param> /// <returns></returns> /// <remarks>Note that this automatically opens and closes areas/conversations etc. - it is only /// for use with analysis methods, not for users.</remarks> public List <ScriptTriggerTuple> GetAllScriptsFromModule(Attachment attachment) { NWN2GameModule mod = session.GetModule(); if (mod == null) { throw new InvalidOperationException("No module is open."); } NWN2GameScriptDictionary dict = mod.Scripts; if (attachment == Attachment.Ignore) { return(GetTuples(dict)); } Dictionary <Nwn2Address, FlipScript> moduleScripts = new Dictionary <Nwn2Address, FlipScript>(); Dictionary <Nwn2Address, FlipScript> areaScripts = new Dictionary <Nwn2Address, FlipScript>(); Dictionary <Nwn2ConversationAddress, FlipScript> conversationScripts = new Dictionary <Nwn2ConversationAddress, FlipScript>(); foreach (NWN2GameScript nwn2Script in dict.Values) { try { nwn2Script.Demand(); string code, address, naturalLanguage; ScriptWriter.ParseNWScript(nwn2Script.Data, out code, out address, out naturalLanguage); ScriptType scriptType = GetScriptType(nwn2Script); FlipScript script = new FlipScript(code, scriptType, nwn2Script.Name); Nwn2ConversationAddress ca = Nwn2ConversationAddress.TryCreate(address); if (ca != null) { conversationScripts.Add(ca, script); } else { Nwn2Address a = Nwn2Address.TryCreate(address); if (a != null) { if (a.TargetType == Nwn2Type.Module) { moduleScripts.Add(a, script); } else { areaScripts.Add(a, script); } } } nwn2Script.Release(); } catch (Exception) {} } List <ScriptTriggerTuple> scripts = new List <ScriptTriggerTuple>(dict.Count); // this is what we'll return if (attachment == Attachment.AttachedToConversation || attachment == Attachment.Attached) { // Index by conversation name, so we can check all the scripts for a conversation in one go. Dictionary <string, List <Nwn2ConversationAddress> > convNameIndex = new Dictionary <string, List <Nwn2ConversationAddress> >(); foreach (Nwn2ConversationAddress address in conversationScripts.Keys) { if (!convNameIndex.ContainsKey(address.Conversation)) { convNameIndex.Add(address.Conversation, new List <Nwn2ConversationAddress>()); } convNameIndex[address.Conversation].Add(address); } foreach (string convName in convNameIndex.Keys) { NWN2GameConversation conv = mod.Conversations[convName]; if (conv == null) { continue; } conv.Demand(); foreach (Nwn2ConversationAddress address in convNameIndex[convName]) { FlipScript script = conversationScripts[address]; NWN2ConversationLine line = conv.GetLineFromGUID(address.LineID); if (line == null) { continue; } if (address.AttachedAs == ScriptType.Standard && line.Actions.Count > 0) { IResourceEntry resource = line.Actions[0].Script; if (resource != null && resource.ResRef.Value == script.Name) { scripts.Add(new ScriptTriggerTuple(script, triggerFactory.GetTriggerFromAddress(address))); } } else if (address.AttachedAs == ScriptType.Conditional && line.OwningConnector.Conditions.Count > 0) { IResourceEntry resource = line.OwningConnector.Conditions[0].Script; if (resource != null && resource.ResRef.Value == script.Name) { scripts.Add(new ScriptTriggerTuple(script, triggerFactory.GetTriggerFromAddress(address))); } } } conv.Release(); } } if (attachment == Attachment.AttachedToScriptSlot || attachment == Attachment.Attached) { // First, just check for scripts attached to the module - easy: foreach (Nwn2Address address in moduleScripts.Keys) { FlipScript script = moduleScripts[address]; IResourceEntry resource = typeof(NWN2ModuleInformation).GetProperty(address.TargetSlot).GetValue(mod.ModuleInfo, null) as IResourceEntry; if (resource != null && resource.ResRef.Value == script.Name) { scripts.Add(new ScriptTriggerTuple(script, triggerFactory.GetTriggerFromAddress(address))); } } // Next, check whether any script is attached to a Narrative Threads blueprint, and // if you find one, add it to the collection and remove it from consideration: List <Nwn2Address> processed = new List <Nwn2Address>(); foreach (Nwn2Address address in areaScripts.Keys) { if (Nwn2ScriptSlot.GetObjectType(address.TargetType) == null) { continue; // ignore area, module etc. } FlipScript script = areaScripts[address]; // First check that if a blueprint which uses this tag as resref exists, it was created by Narrative Threads: if (nt.CreatedByNarrativeThreads(address.TargetType, address.InstanceTag)) { NWN2ObjectType objectType = Nwn2ScriptSlot.GetObjectType(address.TargetType).Value; INWN2Blueprint blueprint = session.GetBlueprint(address.InstanceTag.ToLower(), objectType); if (blueprint != null) { IResourceEntry resource = blueprint.GetType().GetProperty(address.TargetSlot).GetValue(blueprint, null) as IResourceEntry; if (resource != null && resource.ResRef.Value == script.Name) { scripts.Add(new ScriptTriggerTuple(script, triggerFactory.GetTriggerFromAddress(address))); processed.Add(address); } } } } foreach (Nwn2Address p in processed) { areaScripts.Remove(p); // has been added, so don't check for it more than once } // Then, open each area in turn, and see whether a script is attached to it. If it is, add // it to the collection and remove it from consideration: foreach (NWN2GameArea area in mod.Areas.Values) { if (areaScripts.Count == 0) { break; } session.OpenArea(area); processed = new List <Nwn2Address>(); foreach (Nwn2Address address in areaScripts.Keys) { FlipScript script = areaScripts[address]; if (address.TargetType == Nwn2Type.Area) { IResourceEntry resource = typeof(NWN2GameArea).GetProperty(address.TargetSlot).GetValue(area, null) as IResourceEntry; if (resource != null && resource.ResRef.Value == script.Name) { scripts.Add(new ScriptTriggerTuple(script, triggerFactory.GetTriggerFromAddress(address))); processed.Add(address); // don't check whether to add it more than once } } else { NWN2InstanceCollection instances = session.GetObjectsByAddressInArea(address, area.Tag); if (instances != null) { // Check the script is still attached to the target object in the slot it claims to be, // in at least one instance, and if so add it to the set of scripts to be opened: foreach (INWN2Instance instance in instances) { IResourceEntry resource = instance.GetType().GetProperty(address.TargetSlot).GetValue(instance, null) as IResourceEntry; if (resource != null && resource.ResRef.Value == script.Name) { scripts.Add(new ScriptTriggerTuple(script, triggerFactory.GetTriggerFromAddress(address))); processed.Add(address); break; } } } } } foreach (Nwn2Address p in processed) { areaScripts.Remove(p); // has been added, so don't check for it more than once } session.CloseArea(area); } } return(scripts); }