SHARE
TWEET

Ruby Class

ForeverZer0 Jul 28th, 2014 236 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.IO;
  2. using System.Text;
  3. using IronRuby.Builtins;
  4. using Microsoft.Scripting.Hosting;
  5. using zlib;
  6.  
  7. namespace Zer0Division
  8. {
  9.         public static class Ruby
  10.         {
  11.  
  12.                 #region Properties
  13.  
  14.                 public static bool IsInitialized { get; private set; }
  15.  
  16.                 /// <summary>
  17.                 /// Gets the Ruby script runtime
  18.                 /// </summary>
  19.                 public static ScriptRuntime Runtime { get; private set; }
  20.  
  21.                 /// <summary>
  22.                 /// Gets the Ruby script engine
  23.                 /// </summary>
  24.                 public static ScriptEngine Engine { get; private set; }
  25.  
  26.                 /// <summary>
  27.                 /// Gets the Ruby script scope
  28.                 /// </summary>
  29.                 public static ScriptScope Scope { get; private set; }
  30.  
  31.                 #endregion
  32.  
  33.                 internal static void Initialize()
  34.                 {
  35.                         if (IsInitialized)
  36.                                 return;
  37.                         Runtime = IronRuby.Ruby.CreateRuntime();
  38.                         Engine = IronRuby.Ruby.GetEngine(Runtime);
  39.                         Scope = Engine.CreateScope();
  40.                         Engine.Execute(@"load_assembly 'IronRuby.Libraries', 'IronRuby.StandardLibrary.Zlib'", Scope);
  41.                         Engine.Execute(@"require 'Ruby/hidden.rb';require 'Ruby/rpg.rb'", Scope);
  42.                         IsInitialized = true;
  43.                 }
  44.  
  45.                 #region Marshal
  46.  
  47.                 /// <summary>
  48.                 /// Uses Marshal to serialize an object and return as an array of bytes
  49.                 /// </summary>
  50.                 /// <param name="data">Object to serialize</param>
  51.                 /// <returns>Array of bytes representing the serialized object</returns>
  52.                 public static byte[] MarshalDump(object data)
  53.                 {
  54.                         Scope.SetVariable("data", data);
  55.                         MutableString result = Engine.Execute(@"Marshal.dump(data)", Scope);
  56.                         Scope.RemoveVariable("data");
  57.                         return result.ToByteArray();
  58.                 }
  59.  
  60.                 /// <summary>
  61.                 /// Uses Marshal to serialize an object and save it to disk
  62.                 /// </summary>
  63.                 /// <param name="filename">Filename where serialized data will be saved</param>
  64.                 /// <param name="data">Object to serialize and save</param>
  65.                 public static void MarshalDump(string filename, object data)
  66.                 {
  67.                         File.WriteAllBytes(filename, MarshalDump(data));
  68.                 }
  69.  
  70.                 /// <summary>
  71.                 /// Deserializes a Marshaled object and returns it
  72.                 /// </summary>
  73.                 /// <param name="filename">Filename of Marshaled object</param>
  74.                 /// <returns>Deserialized object</returns>
  75.                 public static dynamic MarshalLoad(string filename)
  76.                 {
  77.                         return MarshalLoad(File.ReadAllBytes(filename));
  78.                 }
  79.  
  80.                 /// <summary>
  81.                 /// Deserializes a Marshaled object and returns it
  82.                 /// </summary>
  83.                 /// <param name="data">Byte array of data to deserialize</param>
  84.                 /// <returns>Deserialized object</returns>
  85.                 public static dynamic MarshalLoad(byte[] data)
  86.                 {
  87.                         Scope.SetVariable("data", MutableString.CreateBinary(data));
  88.                         object result = Engine.Execute(@"Marshal.load(data)", Scope);
  89.                         Scope.RemoveVariable("data");
  90.                         return result;
  91.                 }
  92.  
  93.                 #endregion
  94.  
  95.                 #region Zlib
  96.  
  97.                 /// <summary>
  98.                 /// Inflates the compressed string using Ruby's Zlib module
  99.                 /// </summary>
  100.                 /// <param name="data">The Ruby string to decompress</param>
  101.                 /// <returns>The decompressed Ruby string as a System.String</returns>
  102.                 public static string ZlibInflate(MutableString data)
  103.                 {
  104.                         Scope.SetVariable("data", data);
  105.                         MutableString result = Engine.Execute(@"Zlib::Inflate.inflate(data)", Scope);
  106.                         Scope.RemoveVariable("data");
  107.                         return result.ToString(Encoding.UTF8);
  108.                 }
  109.  
  110.                 /// <summary>
  111.                 /// Deflate a string using zlib.net library since IronRuby's Deflate module is buggy
  112.                 /// </summary>
  113.                 /// <param name="data">The string to compress</param>
  114.                 /// <returns>The decompressed string as a Ruby string</returns>
  115.                 public static MutableString ZlibDeflate(string data)
  116.                 {
  117.                         using (var outMemoryStream = new MemoryStream())
  118.                         using (var outZStream = new ZOutputStream(outMemoryStream, zlibConst.Z_BEST_COMPRESSION))
  119.                         {
  120.                                 var bytes = Encoding.UTF8.GetBytes(data);
  121.                                 outZStream.Write(bytes, 0, bytes.Length);
  122.                                 outZStream.finish();
  123.                                 return MutableString.CreateBinary(outMemoryStream.ToArray());
  124.                         }
  125.                 }
  126.  
  127.                 #endregion
  128.         }
  129. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top