目前功能尚不完全。仅作备份
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.IO; namespace Marshal { public partial class Marshal { static BinaryWriter w_stream; static Dictionary<rb_Symbol, int> w_symbols = new Dictionary<rb_Symbol, int> { }; static Dictionary<object, int> w_objects = new Dictionary<object, int> { }; static StringBuilder write_str; static public string rb_marshal_dump(object data, Stream s = null) { w_symbols.Clear(); w_objects.Clear(); write_str = new StringBuilder(); dump(data); if (s != null && s.CanWrite) { w_stream = new BinaryWriter(s); foreach (byte i in write_str.ToString()) { s.WriteByte(i); } } return write_str.ToString(); } static void dump(object ob, bool _pass = false) { if (!_pass) { write_str.Append((char)4); write_str.Append((char)8); } // 第一操作 请求Objects(Symbol判定已经排除) if (!(ob is int || ob is long || ob is rb_Nil || ob is bool || ob is rb_Symbol)) { if (w_objects.ContainsKey(ob)) { write_str.Append((char)64); writeint(w_objects[ob]); return; } } // 第二操作 顺序输入存储 if (ob is long || ob is int) { write_str.Append((char)105); long i = Convert.ToInt64(ob); writeint(i); } else if (ob is string) { string s = (string)ob; write_str.Append((char)34); writestr(s); } else if (ob is rb_Symbol) { rb_Symbol sy = (rb_Symbol)ob; writesym(sy); } else if (ob is rb_Nil) { write_str.Append((char)48); } else if (ob is rb_Object) { writeobject((rb_Object)ob); } else if (ob is bool) { write_str.Append((char)((bool)ob ? 84 : 70)); } else if (ob is rb_Table) { writeTable((rb_Table)ob); } else if (ob is object[]) { writearray((object[])ob); } else if (ob is float || ob is double) { write_float((double)ob); } else { throw new Exception(ob.GetType().ToString()); } } static List<rb_Symbol> r_symbols = new List<rb_Symbol> { }; static List<object> r_objects = new List<object> { }; static BinaryReader r_stream; static public object rb_marshal_load(Stream s) { if (!s.CanRead) { return null; } r_symbols.Clear(); r_objects.Clear(); r_stream = new BinaryReader(s); return read(); } static object read(bool _pass = true) { if (_pass) { r_stream.Read(); r_stream.Read(); } object gotten; byte c = r_stream.ReadByte(); long length = 0; int i = 0; object[] a; switch (c) { case 34: // " 字符串 gotten = readstr(); break; case 48: // 0 NIL return new rb_Nil(); case 47: // '/' 正则表达式 string re = readstr(); int options = r_stream.ReadByte(); RegexOptions reg = RegexOptions.None; if (options >= 4) { reg |= RegexOptions.Multiline; options -= 4; } if (options >= 2) { reg |= RegexOptions.IgnorePatternWhitespace; options -= 2; } if (options >= 1) { reg |= RegexOptions.IgnoreCase; options -= 1; } gotten = new Regex(re, reg); break; case 58: // : 符号 return readsym(); case 59: // ; 符号引用 i = (int)readint(); return r_symbols[i]; case 64: // @ 物体引用 i = (int)readint(); return r_objects[i]; case 70: // F 伪值 return false; case 73: // I 扩展自定义类 rb_expand_Object oba = new rb_expand_Object(); oba.real = read(false); length = readint(); for (int j = 0; j < length; j++) { oba.variables[(rb_Symbol)read(false)] = read(false); } gotten = oba; break; case 84: // T 真 return true; case 91: // [ 数组 length = readint(); a = new object[length]; r_objects.Add(a); for (int j = 0; j < length; j++) { a[j] = read(false); } return a; case 102: // f 浮点 gotten = Convert.ToSingle(readstr()); break; case 105: // i 整数 return readint(); case 111: // o 自定义类 rb_Object ob = new rb_Object(); r_objects.Add(ob); ob.class_name = (rb_Symbol)read(false); length = readint(); for (int j = 0; j < length; j++) { ob.variables[(rb_Symbol)read(false)] = read(false); } return ob; case 117: // Table string s = ((rb_Symbol)read(false)).getStr(); if (s == "Table") { long check1 = readint(); int d = readint4(); int x = readint4(); int y = readint4(); int z = readint4(); int check2 = readint4(); rb_Table array = new rb_Table(d, x, y, z); for (int j = 0; j < z; j++) { for (int k = 0; k < y; k++) { for (int l = 0; l < x; l++) { array.value[l, k, j] = readint4(2); } } } gotten = array; break; } else { throw new Exception("不支持的用户定义类型:" + s); } case 123: Dictionary<Object, Object> dictionary = new Dictionary<object, object> { }; length = readint(); for (int j = 0; j < length; j++) { dictionary[read(false)] = read(false); } gotten = dictionary; break; default: throw new Exception("不可预知的标识符:" + (char)c); } r_objects.Add(gotten); return gotten; } } public class rb_Symbol { string real; public rb_Symbol(string s) { real = s; } public override string ToString() { return ":" + real; } public string getStr() { return real; } } public class rb_Table { public readonly int size; public int[, ,] value; public rb_Table(int size, int x_size, int y_size = 1, int z_size = 1) { this.size = size; value = new int[x_size, y_size, z_size]; } int get_Value(int x, int y, int z) { return value[x, y, z]; } public override string ToString() { string s = "Ruby::Table{Size = "; s += size.ToString() + "} ["; for (int i = 0; i < value.GetLength(2); i++) { s += "["; for (int j = 0; j < value.GetLength(1); j++) { s += "["; for (int k = 0; k < value.GetLength(0); k++) { s += value[k,j,i].ToString() + " "; } s += "]"; } s += "]"; } s += "]"; return s; } } public class rb_Object { public Dictionary<rb_Symbol, Object> variables = new Dictionary<rb_Symbol, object> { }; public rb_Symbol class_name { get; set; } override public string ToString() { string s = "#<" + class_name + "> {"; foreach (rb_Symbol i in variables.Keys) { s += (i + " = " + variables[i] + " "); } s += "}"; return s; } } public class rb_expand_Object : rb_Object { public object real { get; set; } override public string ToString() { string s = "#<" + class_name + "> [" + real.ToString() + "]{"; foreach (rb_Symbol i in variables.Keys) { s += (i + " = " + variables[i] + " , "); } s += "}"; return s; } } public class rb_Nil : rb_Object { public override String ToString() { return "Ruby::Nil"; } } }
第二代码
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Marshal { public partial class Help { public class TrueStrLength { public TrueStrLength() { } static public int trueLength(string str) { int lenTotal = 0; int n = str.Length; string strWord = ""; int asc; for (int i = 0; i < n; i++) { strWord = str.Substring(i, 1); asc = Convert.ToChar(strWord); if (asc < 0 || asc > 127) lenTotal = lenTotal + 3; else lenTotal = lenTotal + 1; } return lenTotal; } static public string cutTrueLength(string strOriginal, int maxTrueLength, char chrPad, bool blnCutTail) { string strNew = strOriginal; if (strOriginal == null || maxTrueLength <= 0) { strNew = ""; return strNew; } int trueLen = trueLength(strOriginal); if (trueLen > maxTrueLength) { if (blnCutTail) { for (int i = strOriginal.Length - 1; i > 0; i--) { strNew = strNew.Substring(0, i); if (trueLength(strNew) == maxTrueLength) break; else if (trueLength(strNew) < maxTrueLength) { strNew += chrPad.ToString(); break; } } } } else { for (int i = 0; i < maxTrueLength - trueLen; i++) { strNew += chrPad.ToString(); } } return strNew; } } } partial class Help { public static string Dictionary_String(Dictionary<Object, Object> d) { string s = "{"; foreach (object i in d.Keys) { s += (i + " => " + d[i] + " , "); } s += "}"; return s; } public static string Array_String(object[] ar) { string s = "["; foreach (object i in ar) { s += ((i.GetType() == ar.GetType()) ? Array_String((object[])i) : i.ToString()) + ","; } return s + "]"; } } } namespace Marshal { partial class Marshal { static void writeint(long i) { if (i > 122) { write_str.Append((char)Math.Ceiling(Math.Log(i, 256f))); while (i > 0) { write_str.Append((char)(i & 0xff)); i = i >> 8; } } else if (i > 0) { write_str.Append((char)(i + 5)); } else if (i == 0) { write_str.Append((char)0); } else if (i >= -123) { write_str.Append((char)(i - 5)); } else { long t = ~i; write_str.Append((char)(sbyte)Math.Ceiling((Math.Log(t, 256))) * (-1)); while (t > 0) { write_str.Append((char)(t & 0xff)); t = t >> 8; } } } static void writestr(string s) { byte[] cs = Encoding.Unicode.GetBytes(s); byte[] bt = Encoding.Convert(Encoding.Unicode, Encoding.UTF8, cs); string st = Encoding.UTF8.GetString(bt); writeint(Help.TrueStrLength.trueLength(st)); //System.Windows.Forms.MessageBox.Show(st + (st == s).ToString()); //write_str.Append(st); foreach (byte i in bt) { write_str.Append((char)i); } } static void writearray(object[] ar) { write_str.Append((char)91); writeint(ar.Length); foreach (object i in ar) { dump(i, true); } } static void writeobject(rb_Object o) { write_str.Append((char)111); writesym(o.class_name); writeint(o.variables.Count); foreach (rb_Symbol i in o.variables.Keys) { writesym(i); dump(o.variables[i], true); } } static void writesym(rb_Symbol s) { // 符号序列检查 if (w_symbols.ContainsKey(s)) { int index = w_symbols[s]; write_str.Append((char)59); writeint(index); } else { write_str.Append((char)58); writestr(s.getStr()); w_symbols[s] = w_symbols.Count; } } static void writeTable(rb_Table t) { write_str.Append((char)117); writesym(new rb_Symbol("Table")); int x = t.value.GetLength(0); int y = t.value.GetLength(1); int z = t.value.GetLength(2); writeint(x * y * z * 2 + 20); write_str.Append(special_num(t.size)); write_str.Append(special_num(x)); write_str.Append(special_num(y)); write_str.Append(special_num(z)); write_str.Append(special_num(x * y * z)); for (int i = 0; i < z; i++) { for (int j = 0; j < y; j++) { for (int k = 0; k < x; k++) { write_str.Append(special_num(t.value[k, j, i],2)); } } } } static string special_num(int num,int length = 4) { string s = ""; int count = 0; while (num != 0) { s = (char)(num % 256) + s; num = num >> 8; count += 1; if (count >= length) { break; } } for (int j = 0; j < length - count; j++) { s = (char)0 + s; } return s; } static void write_float(double f) { write_str.Append((char)102); writestr(string.Format("{0:g}", f)); } static long readint() { sbyte c = r_stream.ReadSByte(); if (c <= -5) { return c + 5; } else if (c < 0) { long g = 0; for (int i = 0; i < -c; i++) { int t = 255 - r_stream.ReadByte(); g = g + (t << (8 * i)); } return -g - 1; } else if (c == 0) { return 0; } else if (c <= 4) { long g = 0; for (int i = 0; i < c; i++) { int t = r_stream.ReadByte(); g = g + (t << (8 * i)); } return g; } else { return c - 5; } } static string readstr() { long l = readint(); byte[] cs = new byte[l]; for (int i = 0; i < l; i++) { cs[i] = r_stream.ReadByte(); } UnicodeEncoding uni = new UnicodeEncoding(); UTF8Encoding utf = new UTF8Encoding(); byte[] bt = Encoding.Convert(Encoding.UTF8, Encoding.Unicode, cs); return uni.GetString(bt); } static rb_Symbol readsym() { rb_Symbol s = new rb_Symbol(readstr()); r_symbols.Add(s); return s; } static int readint4(int d = 4) { int g = 0; for (int i = 0; i < d; i++) { int t = r_stream.ReadByte(); g = g + (t << (8 * i)); } return g; } } }