PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/ProjectEngines/RGSSProjectEngineCommon/Marshal/RubyMarshalDummy.cs

http://nekokun.googlecode.com/
C# | 555 lines | 551 code | 2 blank | 2 comment | 12 complexity | e81584a2488841d66bc1126e77f8d186 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Text.RegularExpressions;
  5. using System.IO;
  6. using System.Drawing;
  7. using System.Runtime.InteropServices;
  8. namespace orzTech.NekoKun.ProjectEngines.RGSS
  9. {
  10. public class Marshal
  11. {
  12. // Fields
  13. private static List<object> r_objects = new List<object>();
  14. private static BinaryReader r_stream;
  15. private static List<RubySymbol> r_symbols = new List<RubySymbol>();
  16. private static Dictionary<object, int> w_objects = new Dictionary<object, int>();
  17. private static BinaryWriter w_stream;
  18. private static Dictionary<RubySymbol, int> w_symbols = new Dictionary<RubySymbol, int>();
  19. private static StringBuilder write_str;
  20. // Methods
  21. private static void dump(object ob, [Optional, DefaultParameterValue(false)] bool _pass)
  22. {
  23. if (!_pass)
  24. {
  25. write_str.Append('\x0004');
  26. write_str.Append('\b');
  27. }
  28. if ((((!(ob is int) && !(ob is long)) && (!(ob is RubyNil) && !(ob is bool))) && !(ob is RubySymbol)) && w_objects.ContainsKey(ob))
  29. {
  30. write_str.Append('@');
  31. writeint((long)w_objects[ob]);
  32. }
  33. else if ((ob is long) || (ob is int))
  34. {
  35. write_str.Append('i');
  36. writeint(Convert.ToInt64(ob));
  37. }
  38. else if (ob is string)
  39. {
  40. string s = (string)ob;
  41. write_str.Append('"');
  42. writestr(s);
  43. }
  44. else if (ob is RubySymbol)
  45. {
  46. RubySymbol symbol = (RubySymbol)ob;
  47. writesym(symbol);
  48. }
  49. else if (ob is RubyNil)
  50. {
  51. write_str.Append('0');
  52. }
  53. else if (ob is RubyObject)
  54. {
  55. writeobject((RubyObject)ob);
  56. }
  57. else if (ob is bool)
  58. {
  59. write_str.Append(((bool)ob) ? 'T' : 'F');
  60. }
  61. else if (ob is RubyTable)
  62. {
  63. writeTable((RubyTable)ob);
  64. }
  65. else if (ob is ICollection<object>)
  66. {
  67. writeList((ICollection<object>)ob);
  68. }
  69. else if (ob is Regex)
  70. {
  71. writereg((Regex)ob);
  72. }
  73. else
  74. {
  75. if (!(ob is float) && !(ob is double))
  76. {
  77. throw new Exception("?????????????" + ob.GetType().ToString());
  78. }
  79. write_float((double)ob);
  80. }
  81. }
  82. public static string Dump(object data, [Optional, DefaultParameterValue(null)] Stream s)
  83. {
  84. w_symbols.Clear();
  85. w_objects.Clear();
  86. write_str = new StringBuilder();
  87. dump(data, false);
  88. if ((s != null) && s.CanWrite)
  89. {
  90. w_stream = new BinaryWriter(s);
  91. string str2 = write_str.ToString();
  92. for (int i = 0; i < str2.Length; i++)
  93. {
  94. byte num = (byte)str2[i];
  95. s.WriteByte(num);
  96. }
  97. }
  98. return write_str.ToString();
  99. }
  100. public static object Load(Stream s)
  101. {
  102. if (!s.CanRead)
  103. {
  104. return null;
  105. }
  106. r_symbols.Clear();
  107. r_objects.Clear();
  108. r_stream = new BinaryReader(s);
  109. return read(true);
  110. }
  111. private static object read([Optional, DefaultParameterValue(true)] bool _pass)
  112. {
  113. object obj2 = null;
  114. int num5;
  115. if (_pass)
  116. {
  117. r_stream.Read();
  118. r_stream.Read();
  119. }
  120. byte num = r_stream.ReadByte();
  121. long num2 = 0L;
  122. int num3 = 0;
  123. switch (num)
  124. {
  125. case 0x40:
  126. num3 = (int)readint();
  127. if (num3 >= r_objects.Count)
  128. return new ArgumentOutOfRangeException("r_Object", num3, "r_Object");
  129. return new RubyPointer(r_objects[num3]);
  130. case 70:
  131. return false;
  132. case 0x49:
  133. {
  134. RubyExpendObject obj3 = new RubyExpendObject();
  135. r_objects.Add(obj3);
  136. obj3.real = read(false);
  137. num2 = readint();
  138. for (num5 = 0; num5 < num2; num5++)
  139. {
  140. obj3.variables[(RubySymbol)read(false)] = read(false);
  141. }
  142. obj2 = obj3;
  143. break;
  144. }
  145. case 0x2f:
  146. {
  147. string pattern = readstr();
  148. int num4 = r_stream.ReadByte();
  149. RegexOptions none = RegexOptions.None;
  150. if (num4 >= 4)
  151. {
  152. none |= RegexOptions.Multiline;
  153. num4 -= 4;
  154. }
  155. if (num4 >= 2)
  156. {
  157. none |= RegexOptions.IgnorePatternWhitespace;
  158. num4 -= 2;
  159. }
  160. if (num4 >= 1)
  161. {
  162. none |= RegexOptions.IgnoreCase;
  163. num4--;
  164. }
  165. obj2 = new Regex(pattern, none);
  166. r_objects.Add(obj2);
  167. break;
  168. }
  169. case 0x30:
  170. return new RubyNil();
  171. case 0x22:
  172. obj2 = readstr();
  173. break;
  174. case 0x3a:
  175. return readsym();
  176. case 0x3b:
  177. num3 = (int)readint();
  178. return r_symbols[num3];
  179. case 0x54:
  180. return true;
  181. case 0x5b:
  182. {
  183. num2 = readint();
  184. List<object> item = new List<object>((int)num2);
  185. r_objects.Add(item);
  186. for (num5 = 0; num5 < num2; num5++)
  187. {
  188. item.Add(read(false));
  189. }
  190. return item;
  191. }
  192. case 0x66:
  193. string str = readstr();
  194. if (str.Contains("\0"))
  195. {
  196. str = str.Remove(str.IndexOf("\0"));
  197. }//orzFly
  198. obj2 = Convert.ToSingle(str);
  199. break;
  200. case 0x75:
  201. {
  202. long num6;
  203. int num8;
  204. int num9;
  205. string str2 = ((RubySymbol)read(false)).getStr();
  206. if (!(str2 == "Table"))
  207. {
  208. int num14;
  209. int num15;
  210. int num16;
  211. int num18;
  212. int num19;
  213. switch (str2)
  214. {
  215. case "Color":
  216. num6 = readint();
  217. num14 = Convert.ToInt32(r_stream.ReadDouble());
  218. num15 = Convert.ToInt32(r_stream.ReadDouble());
  219. num16 = Convert.ToInt32(r_stream.ReadDouble());
  220. obj2 = Color.FromArgb(Convert.ToInt32(r_stream.ReadDouble()), num14, num15, num16);
  221. r_objects.Add(obj2);
  222. break;
  223. case "Rect":
  224. num6 = readint();
  225. num8 = readint4(4);
  226. num9 = readint4(4);
  227. num18 = readint4(4);
  228. num19 = readint4(4);
  229. obj2 = new Rectangle(num8, num9, num18, num19);
  230. r_objects.Add(obj2);
  231. break;
  232. case "Tone":
  233. num6 = readint();
  234. obj2 = new RubyTone(Convert.ToInt32(r_stream.ReadDouble()), Convert.ToInt32(r_stream.ReadDouble()), Convert.ToInt32(r_stream.ReadDouble()), Convert.ToInt32(r_stream.ReadDouble()));
  235. r_objects.Add(obj2);
  236. break;
  237. }
  238. if (obj2 == null) throw new Exception("???????????????:" + str2);
  239. return obj2;
  240. }
  241. num6 = readint();
  242. int size = readint4(4);
  243. num8 = readint4(4);
  244. num9 = readint4(4);
  245. int num10 = readint4(4);
  246. int num11 = readint4(4);
  247. RubyTable table = new RubyTable(size, num8, num9, num10);
  248. r_objects.Add(table);
  249. for (num5 = 0; num5 < num10; num5++)
  250. {
  251. for (int i = 0; i < num9; i++)
  252. {
  253. for (int j = 0; j < num8; j++)
  254. {
  255. table.value[j, i, num5] = readint4(2);
  256. }
  257. }
  258. }
  259. obj2 = table;
  260. break;
  261. }
  262. case 0x7b:
  263. {
  264. Dictionary<object, object> dictionary = new Dictionary<object, object>();
  265. r_objects.Add(dictionary);
  266. num2 = readint();
  267. num5 = 0;
  268. while (num5 < num2)
  269. {
  270. dictionary[read(false)] = read(false);
  271. num5++;
  272. }
  273. obj2 = dictionary;
  274. return obj2;
  275. }
  276. case 0x69:
  277. return readint();
  278. case 0x6f:
  279. {
  280. RubyObject obj4 = new RubyObject();
  281. r_objects.Add(obj4);
  282. obj4.class_name = (RubySymbol)read(false);
  283. num2 = readint();
  284. for (num5 = 0; num5 < num2; num5++)
  285. {
  286. obj4.variables[(RubySymbol)read(false)] = read(false);
  287. }
  288. return obj4;
  289. }
  290. default:
  291. throw new Exception("?????????????:" + ((char)num));
  292. }
  293. r_objects.Add(obj2);
  294. return obj2;
  295. }
  296. private static long readint()
  297. {
  298. long num2;
  299. int num3;
  300. int num4;
  301. sbyte num = r_stream.ReadSByte();
  302. if (num <= -5)
  303. {
  304. return (long)(num + 5);
  305. }
  306. if (num < 0)
  307. {
  308. num2 = 0L;
  309. for (num3 = 0; num3 < -num; num3++)
  310. {
  311. num4 = 0xff - r_stream.ReadByte();
  312. num2 += num4 << (8 * num3);
  313. }
  314. return (-num2 - 1L);
  315. }
  316. if (num == 0)
  317. {
  318. return 0L;
  319. }
  320. if (num <= 4)
  321. {
  322. num2 = 0L;
  323. for (num3 = 0; num3 < num; num3++)
  324. {
  325. num4 = r_stream.ReadByte();
  326. num2 += num4 << (8 * num3);
  327. }
  328. return num2;
  329. }
  330. return (num - 5);
  331. }
  332. private static int readint4([Optional, DefaultParameterValue(4)] int d)
  333. {
  334. int num = 0;
  335. for (int i = 0; i < d; i++)
  336. {
  337. int num3 = r_stream.ReadByte();
  338. num += num3 << (8 * i);
  339. }
  340. return num;
  341. }
  342. private static string readstr()
  343. {
  344. long num = readint();
  345. byte[] bytes = new byte[num];
  346. for (int i = 0; i < num; i++)
  347. {
  348. bytes[i] = r_stream.ReadByte();
  349. }
  350. UnicodeEncoding encoding = new UnicodeEncoding();
  351. UTF8Encoding encoding2 = new UTF8Encoding();
  352. byte[] buffer2 = Encoding.Convert(Encoding.UTF8, Encoding.Unicode, bytes);
  353. return encoding.GetString(buffer2);
  354. }
  355. private static RubySymbol readsym()
  356. {
  357. RubySymbol item = RubySymbol.GetSymbol(readstr());
  358. r_symbols.Add(item);
  359. return item;
  360. }
  361. private static string special_num(int num, [Optional, DefaultParameterValue(4)] int length)
  362. {
  363. string str = "";
  364. int num2 = 0;
  365. while (num != 0)
  366. {
  367. str = ((char)(num % 0x100)) + str;
  368. num = num >> 8;
  369. num2++;
  370. if (num2 >= length)
  371. {
  372. break;
  373. }
  374. }
  375. for (int i = 0; i < (length - num2); i++)
  376. {
  377. str = '\0' + str;
  378. }
  379. return str;
  380. }
  381. private static void write_float(double f)
  382. {
  383. write_str.Append('f');
  384. writestr(string.Format("{0:g}", f));
  385. }
  386. private static void writearray(object[] ar)
  387. {
  388. write_str.Append('[');
  389. writeint((long)ar.Length);
  390. foreach (object obj2 in ar)
  391. {
  392. dump(obj2, true);
  393. }
  394. }
  395. private static void writeColor(Color c)
  396. {
  397. write_str.Append('u');
  398. writesym(RubySymbol.GetSymbol("Color"));
  399. writeint(0x20L);
  400. }
  401. private static void writeint(long i)
  402. {
  403. if (i > 0x7aL)
  404. {
  405. write_str.Append((char)((ushort)Math.Ceiling(Math.Log((double)i, 256.0))));
  406. while (i > 0L)
  407. {
  408. write_str.Append((char)((ushort)(i & 0xffL)));
  409. i = i >> 8;
  410. }
  411. }
  412. else if (i > 0L)
  413. {
  414. write_str.Append((char)((ushort)(i + 5L)));
  415. }
  416. else if (i == 0L)
  417. {
  418. write_str.Append('\0');
  419. }
  420. else if (i >= -123L)
  421. {
  422. write_str.Append((char)((ushort)(i - 5L)));
  423. }
  424. else
  425. {
  426. long num = ~i;
  427. write_str.Append((char)((ushort)((sbyte)(256.0 - Math.Ceiling(Math.Log((double)num, 256.0))))));
  428. while (num > 0L)
  429. {
  430. write_str.Append((char)((ushort)((0xffL - num) & 0xffL)));
  431. num = num >> 8;
  432. }
  433. }
  434. }
  435. private static void writeList(ICollection<object> ar)
  436. {
  437. write_str.Append('[');
  438. writeint((long)ar.Count);
  439. foreach (object obj2 in ar)
  440. {
  441. dump(obj2, true);
  442. }
  443. }
  444. private static void writeobject(RubyObject o)
  445. {
  446. write_str.Append('o');
  447. writesym(o.class_name);
  448. writeint((long)o.variables.Count);
  449. foreach (RubySymbol symbol in o.variables.Keys)
  450. {
  451. writesym(symbol);
  452. dump(o.variables[symbol], true);
  453. }
  454. }
  455. private static void writereg(Regex r)
  456. {
  457. write_str.Append('/');
  458. byte num = 0;
  459. if ((r.Options & RegexOptions.Multiline) == RegexOptions.Multiline)
  460. {
  461. num = (byte)(num + 4);
  462. }
  463. if ((r.Options & RegexOptions.IgnorePatternWhitespace) == RegexOptions.IgnorePatternWhitespace)
  464. {
  465. num = (byte)(num + 2);
  466. }
  467. if ((r.Options & RegexOptions.IgnoreCase) == RegexOptions.IgnoreCase)
  468. {
  469. num = (byte)(num + 1);
  470. }
  471. writestr(r.ToString());
  472. write_str.Append((char)num);
  473. }
  474. private static void writestr(string s)
  475. {
  476. byte[] bytes = Encoding.Unicode.GetBytes(s);
  477. byte[] buffer2 = Encoding.Convert(Encoding.Unicode, Encoding.UTF8, bytes);
  478. writeint((long)Helper.TrueStrLength.TrueLength(Encoding.UTF8.GetString(buffer2)));
  479. foreach (byte num in buffer2)
  480. {
  481. write_str.Append((char)num);
  482. }
  483. }
  484. private static void writesym(RubySymbol s)
  485. {
  486. if (w_symbols.ContainsKey(s))
  487. {
  488. int num = w_symbols[s];
  489. write_str.Append(';');
  490. writeint((long)num);
  491. }
  492. else
  493. {
  494. write_str.Append(':');
  495. writestr(s.getStr());
  496. w_symbols[s] = w_symbols.Count;
  497. }
  498. }
  499. private static void writeTable(RubyTable t)
  500. {
  501. write_str.Append('u');
  502. writesym(RubySymbol.GetSymbol("Table"));
  503. int length = t.value.GetLength(0);
  504. int num = t.value.GetLength(1);
  505. int num3 = t.value.GetLength(2);
  506. writeint((long)((((length * num) * num3) * 2) + 20));
  507. write_str.Append(special_num(t.size, 4));
  508. write_str.Append(special_num(length, 4));
  509. write_str.Append(special_num(num, 4));
  510. write_str.Append(special_num(num3, 4));
  511. write_str.Append(special_num((length * num) * num3, 4));
  512. for (int i = 0; i < num3; i++)
  513. {
  514. for (int j = 0; j < num; j++)
  515. {
  516. for (int k = 0; k < length; k++)
  517. {
  518. write_str.Append(special_num(t.value[k, j, i], 2));
  519. }
  520. }
  521. }
  522. }
  523. }
  524. }