/// <summary>Executes a define.</summary> /// <param name="queue">The command queue involved.</param> /// <param name="entry">Entry to be executed.</param> public static void Define(CommandEntry entry, CommandQueue queue) { string name = entry.GetArgument(queue, 1).ToLowerFast(); if (queue.Engine.Functions.ContainsKey(name)) { if (BooleanTag.TryFor(entry.GetNamedArgumentObject(queue, "quiet_fail"))?.Internal ?? false) { if (entry.ShouldShowGood(queue)) { entry.GoodOutput(queue, "Function '" + TextStyle.Separate + name + TextStyle.Base + "' already exists!"); } } else { queue.HandleError(entry, "Function '" + TextStyle.Separate + name + TextStyle.Base + "' already exists!"); } } else { queue.Engine.Functions.Add(name, new CommandScript(name, CommandScript.TYPE_NAME_FUNCTION, entry.InnerCommandBlock, entry.System, entry.BlockStart, entry.DBMode)); if (entry.ShouldShowGood(queue)) { entry.GoodOutput(queue, "Function '" + TextStyle.Separate + name + TextStyle.Base + "' defined."); } } }
/// <summary>Executes an undefine.</summary> /// <param name="queue">The command queue involved.</param> /// <param name="entry">Entry to be executed.</param> public static void Undefine(CommandEntry entry, CommandQueue queue) { string name = entry.GetArgument(queue, 1).ToLowerFast(); if (!queue.Engine.Functions.Remove(name)) { if (BooleanTag.TryFor(entry.GetNamedArgumentObject(queue, "quiet_fail"))?.Internal ?? false) { if (entry.ShouldShowGood(queue)) { entry.GoodOutput(queue, "Function '" + TextStyle.Separate + name + TextStyle.Base + "' doesn't exist!"); } } else { queue.HandleError(entry, "Function '" + TextStyle.Separate + name + TextStyle.Base + "' doesn't exist!"); } } else { if (entry.ShouldShowGood(queue)) { entry.GoodOutput(queue, "Function '" + TextStyle.Separate + name + TextStyle.Base + "' undefined."); } } }
/// <summary>Executes the command.</summary> /// <param name="queue">The command queue involved.</param> /// <param name="entry">Entry to be executed.</param> public static void Execute(CommandQueue queue, CommandEntry entry) { if (entry.IsCallback) { return; } string type = entry.GetArgument(queue, 0).ToLowerFast(); string eventname = entry.GetArgument(queue, 1).ToLowerFast(); if (type == "clear" && eventname == "all") { foreach (KeyValuePair <string, ScriptEvent> evt in queue.Engine.Events) { evt.Value.Clear(); } if (entry.ShouldShowGood(queue)) { entry.GoodOutput(queue, "Cleared all events."); } return; } if (!queue.Engine.Events.TryGetValue(eventname, out ScriptEvent theEvent)) { queue.HandleError(entry, "Unknown event '" + TextStyle.Separate + eventname + TextStyle.Base + "'."); return; } if (type == "clear") { theEvent.Clear(); if (entry.ShouldShowGood(queue)) { entry.GoodOutput(queue, "Cleared event '" + TextStyle.Separate + eventname + TextStyle.Base + "' of all handlers."); } } else if (type == "remove") { if (entry.Arguments.Length < 3) { ShowUsage(queue, entry); return; } string name = entry.GetArgument(queue, 2).ToLowerFast(); bool success = theEvent.RemoveEventHandler(name); if (success) { if (entry.ShouldShowGood(queue)) { entry.GoodOutput(queue, "Removed event handler '" + TextStyle.Separate + name + TextStyle.Base + "'."); } } else { if (BooleanTag.TryFor(entry.GetNamedArgumentObject(queue, "quiet_fail"))?.Internal ?? false) { if (entry.ShouldShowGood(queue)) { entry.GoodOutput(queue, "Unknown event handler '" + TextStyle.Separate + name + TextStyle.Base + "'."); } } else { queue.HandleError(entry, "Unknown event handler '" + TextStyle.Separate + name + TextStyle.Base + "'."); } } } else if (type == "add") { if (entry.Arguments.Length < 3) { ShowUsage(queue, entry); return; } string name = entry.GetArgument(queue, 2).ToLowerFast(); if (entry.InnerCommandBlock == null) { queue.HandleError(entry, "Event command invalid: No block follows!"); return; } if (theEvent.HasHandler(name)) { if (BooleanTag.TryFor(entry.GetNamedArgumentObject(queue, "quiet_fail"))?.Internal ?? false) { if (entry.ShouldShowGood(queue)) { entry.GoodOutput(queue, "Handler '" + TextStyle.Separate + name + TextStyle.Base + "' already exists!"); } } else { queue.HandleError(entry, "Handler '" + TextStyle.Separate + name + TextStyle.Base + "' already exists!"); } } double priority = 0; if (entry.Arguments.Length > 3) { priority = NumberTag.For(entry.GetArgumentObject(queue, 3), queue.Error).Internal; } List <CommandEntry> entries = new(entry.InnerCommandBlock.Length + 2); MapTag expectedContext = new(); expectedContext.Internal.Add("context", entry.System.TagTypes.Type_Map.TagForm); entries.Add(entry.System.TheRequireCommand.GenerateEntry(expectedContext, entry.ScriptName, entry.ScriptLine)); entries.AddRange(entry.InnerCommandBlock); CommandScript script = new(theEvent.Name + "__handler__" + name, CommandScript.TYPE_NAME_EVENT, entries.ToArray(), entry.System, entry.BlockStart, DebugMode.MINIMAL); theEvent.RegisterEventHandler(priority, script, name); entry.GoodOutput(queue, "Handler '" + TextStyle.Separate + name + "" + TextStyle.Base + "' defined for event '" + TextStyle.Separate + theEvent.Name + TextStyle.Base + "'."); } else { ShowUsage(queue, entry); } }
/// <summary>Executes the command.</summary> /// <param name="queue">The command queue involved.</param> /// <param name="entry">Entry to be executed.</param> public static void Execute(CommandQueue queue, CommandEntry entry) { TemplateObject obj = entry.GetArgumentObject(queue, 0); FunctionTag function = FunctionTag.CreateFor(obj, queue.GetTagData()); if (function == null) { queue.HandleError(entry, "Cannot call function '" + TextStyle.Separate + obj.ToString() + TextStyle.Base + "': it does not exist!"); return; } CommandScript script = function.Internal; if (entry.ShouldShowGood(queue)) { entry.GoodOutput(queue, "Calling '" + function.GetDebugString() + TextStyle.Base + "'..."); } CompiledCommandRunnable runnable = script.Compiled.ReferenceCompiledRunnable.Duplicate(); if (runnable.Entry.Entries.Length > 0) { Dictionary <string, SingleCILVariable> varlookup = runnable.Entry.Entries[0].VarLookup; foreach (string var in entry.NamedArguments.Keys) { if (!var.StartsWithNull()) { if (varlookup.TryGetValue(var, out SingleCILVariable varx)) { // TODO: Type verification! runnable.Entry.GetSetter(varx.Index).Invoke(runnable, entry.GetNamedArgumentObject(queue, var)); } } } } if (entry.NamedArguments.ContainsKey(CommandEntry.SAVE_NAME_ARG_ID)) { bool sgood = entry.ShouldShowGood(queue); string vname = entry.NamedArguments[CommandEntry.SAVE_NAME_ARG_ID].ToString(); if (sgood) { entry.GoodOutput(queue, "Noticing variable track for " + vname + "."); } CompiledCommandRunnable curRunnable = queue.CurrentRunnable; if (!entry.VarLookup.TryGetValue(vname, out SingleCILVariable locVar)) { queue.HandleError(entry, "Invalid save-to variable: " + vname + "!"); return; } runnable.Callback = () => { // TODO: Fix! /*if (runnable.Entry.Entries.Length > 0) * { * MapTag mt = new MapTag(); * Dictionary<string, SingleCILVariable> varlookup = runnable.Entry.Entries[0].VarLookup; * foreach (SingleCILVariable vara in varlookup.Values) * { * if (runnable.LocalVariables[vara.Index].Internal != null) * { * mt.Internal.Add(vara.Name, runnable.LocalVariables[vara.Index].Internal); * } * } * curRunnable.LocalVariables[locVar.Index].Internal = mt; * }*/ if (sgood) { entry.GoodOutput(queue, "Call complete."); } }; } queue.RunningStack.Push(runnable); }