/// <summary> /// Creates a dynamic command with the given parameters /// </summary> internal DynamicCommand(string[] commands, string description, string help, int defaultPermissionLevel, DynamicCommandHandler action) { _commands = commands; _action = action; _description = description; _help = help; _defaultPermissionLevel = defaultPermissionLevel; }
public static void InitScripts() { var scripts = Directory.GetFiles(Constants.ScriptsFolder, "*.*", SearchOption.AllDirectories) .Where(s => s.EndsWith(LuaEngine.FileExtension, StringComparison.OrdinalIgnoreCase) || s.EndsWith(JsEngine.FileExtension, StringComparison.OrdinalIgnoreCase)); foreach (string script in scripts) { var filePath = script; // Needed prior C# 5.0 as closure var fileRelativePath = FileTools.GetRelativePath(filePath, Constants.ScriptsFolder); var fileName = Path.GetFileName(filePath); if (fileName.StartsWith("_")) { Log.Out($"Script file {fileRelativePath} is ignored because it starts with underscore."); continue; } Log.Debug($"Loading script {fileRelativePath} ..."); try { bool scriptUsed = false; var scriptEngine = ScriptEngine.GetInstance(Path.GetExtension(filePath)); var metadata = scriptEngine.LoadMetadata(filePath); // Register commands var metadataCommands = metadata.GetValue("commands") ?? metadata.GetValue("command", ""); // never returns null var commandNames = metadataCommands.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); if (commandNames.Length > 0) { scriptUsed = true; var description = metadata.GetValue("description", ""); var help = metadata.GetValue("help", null); var defaultPermission = metadata.GetValue("defaultPermission").ToInt() ?? 0; var action = new DynamicCommandHandler((p, si) => scriptEngine.ExecuteCommand(filePath, p, si)); var commandObject = new DynamicCommand(commandNames, description, help, defaultPermission, action); AddCommand(commandObject); Log.Out($"Registered command{(commandNames.Length == 1 ? "" : "s")} \"{commandNames.Join(" ")}\" from script {fileRelativePath}."); } // Register events var metadataEvents = metadata.GetValue("events") ?? metadata.GetValue("event", ""); // never returns null var eventNames = metadataEvents.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); if (eventNames.Length > 0) { scriptUsed = true; foreach (var eventName in eventNames) { if (!EnumHelper.TryParse <ScriptEvent>(eventName, out var eventType, true)) { Log.Warning($"Event \"{eventName}\" in script {fileRelativePath} is unknown and will be ignored."); continue; } if (_events[(int)eventType] == null) { _events[(int)eventType] = new List <string>(); } _events[(int)eventType].Add(filePath); } Log.Out($"Registered event{(eventNames.Length == 1 ? "" : "s")} \"{eventNames.Join(" ")}\" from script {fileRelativePath}."); } if (!scriptUsed) { Log.Out($"Script file {fileRelativePath} is ignored because it defines neither command names nor events."); } } catch (Exception ex) { Log.Error($"Could not load command script {fileRelativePath}: {ex}"); } } SaveChanges(); Log.Debug("All script commands added."); }