Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Dynamic;
- using System.Diagnostics;
- using System.Reflection;
- using System.Threading;
- using System.Collections.Generic;
- using IronPython.Hosting;
- using Microsoft.Scripting.Hosting;
- using DarkMultiPlayerServer;
- using MessageStream2;
- using System.Net;
- using DarkMultiPlayerCommon;
- namespace DMPScriptedCommands
- {
- public class ShellCommands : DMPPlugin
- {
- public ShellCommands()
- {
- SetCommands("foobar");
- CommandHandler.RegisterCommand ("reloadShell", SetCommands, "Reloads the shell commands from scom");
- }
- void SetCommands(string compliance)
- {
- string[] commands;
- string cwd = Path.GetDirectoryName (Assembly.GetEntryAssembly ().Location);
- string plugdir = Path.Combine (cwd, "Plugins");
- string dir = Path.Combine (plugdir, "shell");
- if (Directory.Exists (plugdir)) {
- if (!Directory.Exists (dir)) {
- Directory.CreateDirectory (dir);
- DarkLog.Debug ("[ScriptedPlugins] Created Script Directory: " + dir);
- }
- commands = Directory.GetFiles (dir);
- foreach (string file in commands) {
- // Register the command
- CommandHandler.RegisterCommand (Path.GetFileNameWithoutExtension (file), GetExecutor(file), "Shell Command");
- DarkLog.Normal("[ScriptedPlugins|Shell] Registered Command: " + Path.GetFileNameWithoutExtension(file));
- }
- } else {
- DarkLog.Error ("[ScriptedPlugins|Shell] Error registering shell commands: Plugins dir does not exist!");
- }
- }
- Action<string> GetExecutor(string file)
- {
- Action<string> Executor = (cmdArgs) =>
- ExecuteCommand (file);
- return Executor;
- }
- void ExecuteCommand(string command)
- {
- ExecutorThread command_executor = new ExecutorThread (command);
- Thread command_thread = new Thread (command_executor.ExecuteCMD);
- command_thread.Start ();
- }
- }
- public class PyCommands : DMPPlugin
- {
- // Delegates
- delegate void DarkLogDelegate(string message);
- // Engine and Script Storage
- readonly ScriptEngine pyengine = Python.CreateEngine ();
- readonly Dictionary<string,dynamic> py_scopes = new Dictionary<string,dynamic> ();
- public PyCommands()
- {
- SetCommands("foobar");
- CommandHandler.RegisterCommand ("reloadPython", SetCommands, "Reloads the python commands from pycom");
- }
- void SetCommands(string compliance)
- {
- string[] py_scripts;
- string cwd = Path.GetDirectoryName (Assembly.GetEntryAssembly ().Location);
- string plugdir = Path.Combine (cwd, "Plugins");
- string dir = Path.Combine (plugdir, "py");
- if (Directory.Exists (plugdir)) {
- if (!Directory.Exists (dir)) {
- Directory.CreateDirectory (dir);
- DarkLog.Debug ("[ScriptedPlugins] Created Script Directory: " + dir);
- }
- py_scripts = Directory.GetFiles (dir);
- foreach (string file in py_scripts) {
- // Register the command
- //CommandHandler.RegisterCommand (Path.GetFileNameWithoutExtension (file), GetExecutor(file), "Shell Command");
- //DarkLog.Normal("[ScriptedCommands|ShellCommands] Registered Command: " + Path.GetFileNameWithoutExtension(file));
- string current_scope = Path.GetFileNameWithoutExtension (file);
- py_scopes [current_scope] = pyengine.CreateScope ();
- // --DMP Script Environment
- py_scopes [current_scope].description = "Python Command";
- py_scopes [current_scope].register_command = true;
- py_scopes [current_scope].call_onregister = false;
- // --Common
- py_scopes [current_scope].Common = typeof(Common);
- py_scopes [current_scope].common_data = new ExpandoObject ();
- py_scopes [current_scope].common_data.CraftType = typeof(CraftType);
- py_scopes [current_scope].common_data.ClientMessageType = typeof(ClientMessageType);
- py_scopes [current_scope].common_data.ServerMessageType = typeof(ServerMessageType);
- py_scopes [current_scope].common_data.ConnectionStatus = typeof(ConnectionStatus);
- py_scopes [current_scope].common_data.ClientState = typeof(ClientState);
- py_scopes [current_scope].common_data.WarpMode = typeof(WarpMode);
- py_scopes [current_scope].common_data.GameMode = typeof(GameMode);
- py_scopes [current_scope].common_data.ModControlMode = typeof(ModControlMode);
- py_scopes [current_scope].common_data.WarpMessageType = typeof(WarpMessageType);
- py_scopes [current_scope].common_data.CraftMessageType = typeof(CraftMessageType);
- py_scopes [current_scope].common_data.ScreenshotMessageType = typeof(ScreenshotMessageType);
- py_scopes [current_scope].common_data.ChatMessageType = typeof(ChatMessageType);
- py_scopes [current_scope].common_data.AdminMessageType = typeof(AdminMessageType);
- py_scopes [current_scope].common_data.LockMessageType = typeof(LockMessageType);
- py_scopes [current_scope].common_data.FlagMessageType = typeof(FlagMessageType);
- py_scopes [current_scope].common_data.PlayerColorMessageType = typeof(PlayerColorMessageType);
- py_scopes [current_scope].common_data.ClientMessage = typeof(ClientMessage);
- py_scopes [current_scope].common_data.ServerMessage = typeof(ServerMessage);
- py_scopes [current_scope].common_data.PlayerStatus = typeof(PlayerStatus);
- py_scopes [current_scope].common_data.Subspace = typeof(Subspace);
- py_scopes [current_scope].common_data.PlayerWarpRate = typeof(PlayerWarpRate);
- py_scopes [current_scope].common_data.HandshakeReply = typeof(HandshakeReply);
- py_scopes [current_scope].common_data.GameDifficulty = typeof(GameDifficulty);
- // --DarkLog
- py_scopes [current_scope].DarkLog = new ExpandoObject ();
- py_scopes [current_scope].DarkLog.Normal = new DarkLogDelegate(DarkLog.Normal);
- py_scopes [current_scope].DarkLog.Debug = new DarkLogDelegate(DarkLog.Debug);
- py_scopes [current_scope].DarkLog.Error = new DarkLogDelegate(DarkLog.Error);
- py_scopes [current_scope].DarkLog.Fatal = new DarkLogDelegate(DarkLog.Fatal);
- // --Messaging
- py_scopes [current_scope].MessageReader = typeof(MessageReader);
- py_scopes [current_scope].MessageWriter = typeof(MessageWriter);
- // --Clients
- py_scopes [current_scope].Clients = typeof(ClientHandler);
- pyengine.ExecuteFile (file, py_scopes [current_scope]);
- if (py_scopes [current_scope].register_command) {
- if (py_scopes [current_scope].call_onregister) {
- ExecuteOnRegister (current_scope, "");
- }
- CommandHandler.RegisterCommand (current_scope, GetExecutor (current_scope), py_scopes [current_scope].description);
- DarkLog.Normal ("[ScriptedPlugins|Python] Registered Command: " + current_scope);
- } else {
- DarkLog.Normal ("[ScriptedPlugins|Python] Executing Script: " + current_scope);
- ExecuteCommand (current_scope, "");
- }
- }
- } else {
- DarkLog.Error ("[ScriptedPlugins|Python] Error registering py commands: Plugins dir does not exist!");
- }
- }
- Action<string> GetExecutor(string scope_name)
- {
- Action<string> Executor = (cmdArgs) =>
- ExecuteCommand (scope_name, cmdArgs);
- return Executor;
- }
- void ExecuteCommand(string scope_name, string cmdArgs)
- {
- ExecutorThread command_executor = new ExecutorThread (py_scopes[scope_name], cmdArgs);
- Thread command_thread = new Thread (command_executor.ExecutePY);
- command_thread.Start ();
- }
- void ExecuteOnRegister(string scope_name, string cmdArgs)
- {
- ExecutorThread command_executor = new ExecutorThread (py_scopes[scope_name], cmdArgs);
- Thread command_thread = new Thread (command_executor.ExecutePYRegister);
- command_thread.Start ();
- }
- }
- public class ExecutorThread
- {
- readonly string shell_command;
- readonly dynamic current_scope;
- readonly string py_args;
- public ExecutorThread(string command)
- {
- shell_command = command;
- }
- public ExecutorThread(dynamic python_scope, string cmdArgs)
- {
- current_scope = python_scope;
- py_args = cmdArgs;
- }
- public static bool IsLinux
- {
- get
- {
- int p = (int) Environment.OSVersion.Platform;
- return (p == 4) || (p == 6) || (p == 128);
- }
- }
- public void ExecuteCMD()
- {
- Process process;
- // Linux and Windows implementations - hopefully. Not tested on the nix.
- ProcessStartInfo processInfo = IsLinux ? new ProcessStartInfo (shell_command) : new ProcessStartInfo ("cmd.exe", "/c " + shell_command);
- processInfo.CreateNoWindow = true;
- processInfo.UseShellExecute = false;
- // Redirect the output
- processInfo.RedirectStandardError = true;
- processInfo.RedirectStandardOutput = true;
- process = Process.Start (processInfo);
- process.WaitForExit ();
- // Read the streams
- string output = process.StandardOutput.ReadToEnd ();
- string error = process.StandardError.ReadToEnd ();
- DarkLog.Normal ("[Shell Output]========================[Shell Output]" + "\n" + (String.IsNullOrEmpty (output) ? "(script had no output)" : output));
- if (!String.IsNullOrEmpty (error)) {
- DarkLog.Error ("[SHELL COMMAND ERROR] " + error);
- }
- process.Close ();
- }
- public void ExecutePY()
- {
- try {
- current_scope.Execute (py_args);
- } catch (Exception e) {
- DarkLog.Error ("[PYTHON ERROR] " + e.Message);
- }
- }
- public void ExecutePYRegister()
- {
- try {
- current_scope.OnRegister (py_args);
- } catch (Exception e) {
- DarkLog.Error ("[PYTHON ERROR] " + e.Message);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement