Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.IO;
- using System.Text;
- using IronRuby.Builtins;
- using Microsoft.Scripting.Hosting;
- using zlib;
- namespace Zer0Division
- {
- public static class Ruby
- {
- #region Properties
- public static bool IsInitialized { get; private set; }
- /// <summary>
- /// Gets the Ruby script runtime
- /// </summary>
- public static ScriptRuntime Runtime { get; private set; }
- /// <summary>
- /// Gets the Ruby script engine
- /// </summary>
- public static ScriptEngine Engine { get; private set; }
- /// <summary>
- /// Gets the Ruby script scope
- /// </summary>
- public static ScriptScope Scope { get; private set; }
- #endregion
- internal static void Initialize()
- {
- if (IsInitialized)
- return;
- Runtime = IronRuby.Ruby.CreateRuntime();
- Engine = IronRuby.Ruby.GetEngine(Runtime);
- Scope = Engine.CreateScope();
- Engine.Execute(@"load_assembly 'IronRuby.Libraries', 'IronRuby.StandardLibrary.Zlib'", Scope);
- Engine.Execute(@"require 'Ruby/hidden.rb';require 'Ruby/rpg.rb'", Scope);
- IsInitialized = true;
- }
- #region Marshal
- /// <summary>
- /// Uses Marshal to serialize an object and return as an array of bytes
- /// </summary>
- /// <param name="data">Object to serialize</param>
- /// <returns>Array of bytes representing the serialized object</returns>
- public static byte[] MarshalDump(object data)
- {
- Scope.SetVariable("data", data);
- MutableString result = Engine.Execute(@"Marshal.dump(data)", Scope);
- Scope.RemoveVariable("data");
- return result.ToByteArray();
- }
- /// <summary>
- /// Uses Marshal to serialize an object and save it to disk
- /// </summary>
- /// <param name="filename">Filename where serialized data will be saved</param>
- /// <param name="data">Object to serialize and save</param>
- public static void MarshalDump(string filename, object data)
- {
- File.WriteAllBytes(filename, MarshalDump(data));
- }
- /// <summary>
- /// Deserializes a Marshaled object and returns it
- /// </summary>
- /// <param name="filename">Filename of Marshaled object</param>
- /// <returns>Deserialized object</returns>
- public static dynamic MarshalLoad(string filename)
- {
- return MarshalLoad(File.ReadAllBytes(filename));
- }
- /// <summary>
- /// Deserializes a Marshaled object and returns it
- /// </summary>
- /// <param name="data">Byte array of data to deserialize</param>
- /// <returns>Deserialized object</returns>
- public static dynamic MarshalLoad(byte[] data)
- {
- Scope.SetVariable("data", MutableString.CreateBinary(data));
- object result = Engine.Execute(@"Marshal.load(data)", Scope);
- Scope.RemoveVariable("data");
- return result;
- }
- #endregion
- #region Zlib
- /// <summary>
- /// Inflates the compressed string using Ruby's Zlib module
- /// </summary>
- /// <param name="data">The Ruby string to decompress</param>
- /// <returns>The decompressed Ruby string as a System.String</returns>
- public static string ZlibInflate(MutableString data)
- {
- Scope.SetVariable("data", data);
- MutableString result = Engine.Execute(@"Zlib::Inflate.inflate(data)", Scope);
- Scope.RemoveVariable("data");
- return result.ToString(Encoding.UTF8);
- }
- /// <summary>
- /// Deflate a string using zlib.net library since IronRuby's Deflate module is buggy
- /// </summary>
- /// <param name="data">The string to compress</param>
- /// <returns>The decompressed string as a Ruby string</returns>
- public static MutableString ZlibDeflate(string data)
- {
- using (var outMemoryStream = new MemoryStream())
- using (var outZStream = new ZOutputStream(outMemoryStream, zlibConst.Z_BEST_COMPRESSION))
- {
- var bytes = Encoding.UTF8.GetBytes(data);
- outZStream.Write(bytes, 0, bytes.Length);
- outZStream.finish();
- return MutableString.CreateBinary(outMemoryStream.ToArray());
- }
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement