Advertisement
dsonbill

DMPScriptedCommands

Apr 11th, 2015
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.68 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Dynamic;
  4. using System.Diagnostics;
  5. using System.Reflection;
  6. using System.Threading;
  7. using System.Collections.Generic;
  8.  
  9. using IronPython.Hosting;
  10. using Microsoft.Scripting.Hosting;
  11.  
  12. using DarkMultiPlayerServer;
  13. using MessageStream2;
  14. using System.Net;
  15. using DarkMultiPlayerCommon;
  16.  
  17.  
  18. namespace DMPScriptedCommands
  19. {
  20.     public class ShellCommands : DMPPlugin
  21.     {
  22.    
  23.         public ShellCommands()
  24.         {
  25.             SetCommands("foobar");
  26.             CommandHandler.RegisterCommand ("reloadShell", SetCommands, "Reloads the shell commands from scom");
  27.         }
  28.    
  29.         void SetCommands(string compliance)
  30.         {
  31.             string[] commands;
  32.    
  33.             string cwd = Path.GetDirectoryName (Assembly.GetEntryAssembly ().Location);
  34.             string plugdir = Path.Combine (cwd, "Plugins");
  35.             string dir = Path.Combine (plugdir, "shell");
  36.    
  37.             if (Directory.Exists (plugdir)) {
  38.    
  39.                 if (!Directory.Exists (dir)) {
  40.                     Directory.CreateDirectory (dir);
  41.                     DarkLog.Debug ("[ScriptedPlugins] Created Script Directory: " + dir);
  42.                 }
  43.    
  44.                 commands = Directory.GetFiles (dir);
  45.    
  46.                 foreach (string file in commands) {
  47.                     // Register the command
  48.                     CommandHandler.RegisterCommand (Path.GetFileNameWithoutExtension (file), GetExecutor(file), "Shell Command");
  49.                     DarkLog.Normal("[ScriptedPlugins|Shell] Registered Command: " + Path.GetFileNameWithoutExtension(file));
  50.                 }
  51.    
  52.             } else {
  53.                 DarkLog.Error ("[ScriptedPlugins|Shell] Error registering shell commands: Plugins dir does not exist!");
  54.             }
  55.         }
  56.    
  57.         Action<string> GetExecutor(string file)
  58.         {
  59.             Action<string> Executor = (cmdArgs) =>
  60.                 ExecuteCommand (file);
  61.             return Executor;
  62.         }
  63.    
  64.         void ExecuteCommand(string command)
  65.         {
  66.             ExecutorThread command_executor = new ExecutorThread (command);
  67.    
  68.             Thread command_thread = new Thread (command_executor.ExecuteCMD);
  69.             command_thread.Start ();
  70.         }
  71.    
  72.     }
  73.    
  74.     public class PyCommands : DMPPlugin
  75.     {
  76.         // Delegates
  77.         delegate void DarkLogDelegate(string message);
  78.    
  79.         // Engine and Script Storage
  80.         readonly ScriptEngine pyengine = Python.CreateEngine ();
  81.         readonly Dictionary<string,dynamic> py_scopes = new Dictionary<string,dynamic> ();
  82.    
  83.         public PyCommands()
  84.         {
  85.             SetCommands("foobar");
  86.             CommandHandler.RegisterCommand ("reloadPython", SetCommands, "Reloads the python commands from pycom");
  87.         }
  88.    
  89.    
  90.         void SetCommands(string compliance)
  91.         {
  92.             string[] py_scripts;
  93.    
  94.             string cwd = Path.GetDirectoryName (Assembly.GetEntryAssembly ().Location);
  95.             string plugdir = Path.Combine (cwd, "Plugins");
  96.             string dir = Path.Combine (plugdir, "py");
  97.    
  98.             if (Directory.Exists (plugdir)) {
  99.    
  100.                 if (!Directory.Exists (dir)) {
  101.                     Directory.CreateDirectory (dir);
  102.                     DarkLog.Debug ("[ScriptedPlugins] Created Script Directory: " + dir);
  103.                 }
  104.    
  105.                 py_scripts = Directory.GetFiles (dir);
  106.    
  107.    
  108.                 foreach (string file in py_scripts) {
  109.                     // Register the command
  110.                     //CommandHandler.RegisterCommand (Path.GetFileNameWithoutExtension (file), GetExecutor(file), "Shell Command");
  111.                     //DarkLog.Normal("[ScriptedCommands|ShellCommands] Registered Command: " + Path.GetFileNameWithoutExtension(file));
  112.                     string current_scope = Path.GetFileNameWithoutExtension (file);
  113.    
  114.                     py_scopes [current_scope] = pyengine.CreateScope ();
  115.    
  116.                     // --DMP Script Environment
  117.                     py_scopes [current_scope].description = "Python Command";
  118.                     py_scopes [current_scope].register_command = true;
  119.                     py_scopes [current_scope].call_onregister = false;
  120.    
  121.                     // --Common
  122.                     py_scopes [current_scope].Common = typeof(Common);
  123.                     py_scopes [current_scope].common_data = new ExpandoObject ();
  124.                     py_scopes [current_scope].common_data.CraftType = typeof(CraftType);
  125.                     py_scopes [current_scope].common_data.ClientMessageType = typeof(ClientMessageType);
  126.                     py_scopes [current_scope].common_data.ServerMessageType = typeof(ServerMessageType);
  127.                     py_scopes [current_scope].common_data.ConnectionStatus = typeof(ConnectionStatus);
  128.                     py_scopes [current_scope].common_data.ClientState = typeof(ClientState);
  129.                     py_scopes [current_scope].common_data.WarpMode = typeof(WarpMode);
  130.                     py_scopes [current_scope].common_data.GameMode = typeof(GameMode);
  131.                     py_scopes [current_scope].common_data.ModControlMode = typeof(ModControlMode);
  132.                     py_scopes [current_scope].common_data.WarpMessageType = typeof(WarpMessageType);
  133.                     py_scopes [current_scope].common_data.CraftMessageType = typeof(CraftMessageType);
  134.                     py_scopes [current_scope].common_data.ScreenshotMessageType = typeof(ScreenshotMessageType);
  135.                     py_scopes [current_scope].common_data.ChatMessageType = typeof(ChatMessageType);
  136.                     py_scopes [current_scope].common_data.AdminMessageType = typeof(AdminMessageType);
  137.                     py_scopes [current_scope].common_data.LockMessageType = typeof(LockMessageType);
  138.                     py_scopes [current_scope].common_data.FlagMessageType = typeof(FlagMessageType);
  139.                     py_scopes [current_scope].common_data.PlayerColorMessageType = typeof(PlayerColorMessageType);
  140.                     py_scopes [current_scope].common_data.ClientMessage = typeof(ClientMessage);
  141.                     py_scopes [current_scope].common_data.ServerMessage = typeof(ServerMessage);
  142.                     py_scopes [current_scope].common_data.PlayerStatus = typeof(PlayerStatus);
  143.                     py_scopes [current_scope].common_data.Subspace = typeof(Subspace);
  144.                     py_scopes [current_scope].common_data.PlayerWarpRate = typeof(PlayerWarpRate);
  145.                     py_scopes [current_scope].common_data.HandshakeReply = typeof(HandshakeReply);
  146.                     py_scopes [current_scope].common_data.GameDifficulty = typeof(GameDifficulty);
  147.                    
  148.                     // --DarkLog
  149.                     py_scopes [current_scope].DarkLog = new ExpandoObject ();
  150.                     py_scopes [current_scope].DarkLog.Normal = new DarkLogDelegate(DarkLog.Normal);
  151.                     py_scopes [current_scope].DarkLog.Debug  = new DarkLogDelegate(DarkLog.Debug);
  152.                     py_scopes [current_scope].DarkLog.Error  = new DarkLogDelegate(DarkLog.Error);
  153.                     py_scopes [current_scope].DarkLog.Fatal  = new DarkLogDelegate(DarkLog.Fatal);
  154.    
  155.                     // --Messaging
  156.                     py_scopes [current_scope].MessageReader = typeof(MessageReader);
  157.                     py_scopes [current_scope].MessageWriter = typeof(MessageWriter);
  158.    
  159.                     // --Clients
  160.                     py_scopes [current_scope].Clients = typeof(ClientHandler);
  161.    
  162.                     pyengine.ExecuteFile (file, py_scopes [current_scope]);
  163.    
  164.                     if (py_scopes [current_scope].register_command) {
  165.                         if (py_scopes [current_scope].call_onregister) {
  166.                             ExecuteOnRegister (current_scope, "");
  167.                         }
  168.    
  169.                         CommandHandler.RegisterCommand (current_scope, GetExecutor (current_scope), py_scopes [current_scope].description);
  170.                         DarkLog.Normal ("[ScriptedPlugins|Python] Registered Command: " + current_scope);
  171.                     } else {
  172.                         DarkLog.Normal ("[ScriptedPlugins|Python] Executing Script: " + current_scope);
  173.                         ExecuteCommand (current_scope, "");
  174.                     }
  175.    
  176.                 }
  177.    
  178.             } else {
  179.                 DarkLog.Error ("[ScriptedPlugins|Python] Error registering py commands: Plugins dir does not exist!");
  180.             }
  181.         }
  182.    
  183.         Action<string> GetExecutor(string scope_name)
  184.         {
  185.             Action<string> Executor = (cmdArgs) =>
  186.                 ExecuteCommand (scope_name, cmdArgs);
  187.             return Executor;
  188.         }
  189.    
  190.         void ExecuteCommand(string scope_name, string cmdArgs)
  191.         {
  192.             ExecutorThread command_executor = new ExecutorThread (py_scopes[scope_name], cmdArgs);
  193.    
  194.             Thread command_thread = new Thread (command_executor.ExecutePY);
  195.             command_thread.Start ();
  196.         }
  197.         void ExecuteOnRegister(string scope_name, string cmdArgs)
  198.         {
  199.             ExecutorThread command_executor = new ExecutorThread (py_scopes[scope_name], cmdArgs);
  200.    
  201.             Thread command_thread = new Thread (command_executor.ExecutePYRegister);
  202.             command_thread.Start ();
  203.         }
  204.     }
  205.    
  206.     public class ExecutorThread
  207.     {
  208.         readonly string shell_command;
  209.         readonly dynamic current_scope;
  210.         readonly string py_args;
  211.    
  212.         public ExecutorThread(string command)
  213.         {
  214.             shell_command = command;
  215.         }
  216.    
  217.         public ExecutorThread(dynamic python_scope, string cmdArgs)
  218.         {
  219.             current_scope = python_scope;
  220.             py_args = cmdArgs;
  221.         }
  222.    
  223.         public static bool IsLinux
  224.         {
  225.             get
  226.             {
  227.                 int p = (int) Environment.OSVersion.Platform;
  228.                 return (p == 4) || (p == 6) || (p == 128);
  229.             }
  230.         }
  231.    
  232.         public void ExecuteCMD()
  233.         {
  234.             Process process;
  235.    
  236.             // Linux and Windows implementations - hopefully. Not tested on the nix.
  237.             ProcessStartInfo processInfo = IsLinux ? new ProcessStartInfo (shell_command) : new ProcessStartInfo ("cmd.exe", "/c " + shell_command);
  238.    
  239.             processInfo.CreateNoWindow = true;
  240.             processInfo.UseShellExecute = false;
  241.    
  242.             // Redirect the output
  243.             processInfo.RedirectStandardError = true;
  244.             processInfo.RedirectStandardOutput = true;
  245.    
  246.             process = Process.Start (processInfo);
  247.             process.WaitForExit ();
  248.    
  249.             // Read the streams
  250.             string output = process.StandardOutput.ReadToEnd ();
  251.             string error = process.StandardError.ReadToEnd ();
  252.    
  253.             DarkLog.Normal ("[Shell Output]========================[Shell Output]" + "\n" + (String.IsNullOrEmpty (output) ? "(script had no output)" : output));
  254.    
  255.    
  256.             if (!String.IsNullOrEmpty (error)) {
  257.                 DarkLog.Error ("[SHELL COMMAND ERROR]  " + error);
  258.             }
  259.    
  260.             process.Close ();
  261.         }
  262.    
  263.         public void ExecutePY()
  264.         {
  265.             try {
  266.                 current_scope.Execute (py_args);
  267.             } catch (Exception e) {
  268.                 DarkLog.Error ("[PYTHON ERROR]  " + e.Message);
  269.             }
  270.         }
  271.    
  272.         public void ExecutePYRegister()
  273.         {
  274.             try {
  275.                 current_scope.OnRegister (py_args);
  276.             } catch (Exception e) {
  277.                 DarkLog.Error ("[PYTHON ERROR]  " + e.Message);
  278.             }
  279.         }
  280.     }
  281. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement