/src/N2012/KOEI.WP7_2012/MemoryTable/HorseNameTable.cs

http://libwp7n2012.googlecode.com/ · C# · 178 lines · 122 code · 16 blank · 40 comment · 38 complexity · 199ac0994b86699eab799abd2169024e MD5 · raw file

  1. /* libwp7n2012 -- Winning Post 7 2012 Cheat Library
  2. Copyright (C) 2012 AIPON of author
  3. This library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. This library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with this library; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  14. Written by AIPON */
  15. using System;
  16. using System.Collections.Generic;
  17. namespace KOEI.WP7_2012.MemoryTable
  18. {
  19. /// <summary>
  20. /// ???????????????????????
  21. /// </summary>
  22. public class HorseNameTable
  23. {
  24. private KOEI.WP7_2012.Helper.ProcessMemory pm_;
  25. private Dictionary< int, KOEI.WP7_2012.Horse.Name.NameData > name_data_dic_;
  26. private ConfigurationInterface config_;
  27. private HorseNameTableConfiguration system_table_config_;
  28. private HorseNameTableConfiguration user_table_config_;
  29. /// <summary>
  30. /// ???????
  31. /// </summary>
  32. /// <param name="pm">??????????</param>
  33. /// <param name="config">????????????????</param>
  34. public HorseNameTable( KOEI.WP7_2012.Helper.ProcessMemory pm, ConfigurationInterface config )
  35. {
  36. this.pm_ = pm;
  37. this.name_data_dic_ = new Dictionary< int, KOEI.WP7_2012.Horse.Name.NameData >();
  38. this.config_ = config;
  39. this.system_table_config_ = config.HorseNameSystemTable;
  40. this.user_table_config_ = config.HorseNameUserTable;
  41. // ??????????????????
  42. this.CreateSystemNameTable();
  43. // ??????????????????
  44. this.CreateUserNameTable();
  45. }
  46. /// <summary>
  47. /// ????????????(foreach???)
  48. /// </summary>
  49. /// <returns>????</returns>
  50. public IEnumerator< KOEI.WP7_2012.Horse.Name.NameData > GetEnumerator()
  51. {
  52. foreach( var name_data in this.name_data_dic_ ) {
  53. yield return name_data.Value;
  54. }
  55. }
  56. /// <summary>
  57. /// ???????
  58. /// </summary>
  59. /// <param name="n">????</param>
  60. /// <returns>??????????????null???</returns>
  61. public KOEI.WP7_2012.Horse.Name.NameData this[int n]
  62. {
  63. get {
  64. if( !this.name_data_dic_.ContainsKey(n) ) {
  65. return null;
  66. }
  67. return this.name_data_dic_[n];
  68. }
  69. }
  70. private static bool IsKana( byte x )
  71. {
  72. return ( x >= 0xB6 && x <= 0xC4 ) || ( x >= 0x01 && x <= 0x56 ) || ( x >= 0xA2 && x <= 0xAB ) || ( x >= 0xB6 && x <= 0xC4 ) || ( x==0xFE );
  73. }
  74. private static bool IsEnglish( byte x )
  75. {
  76. return ( x >= 0x57 && x <= 0x9E ) || ( x >= 0x9F && x <= 0xA1 ) || ( x >= 0xAC && x <= 0xB4 ) || ( x == 0xFD );
  77. }
  78. private void CreateSystemNameTable()
  79. {
  80. var table_size = BitConverter.ToUInt32( this.pm_.ReadMemory( this.system_table_config_.TableSizeAddress, 4 ), 0 );
  81. var pointer_address = BitConverter.ToUInt32( this.pm_.ReadMemory( this.system_table_config_.PointerAddress, 4 ), 0 );
  82. var table = this.pm_.ReadMemory( pointer_address, table_size );
  83. using( var ms = new System.IO.MemoryStream( table ) ) {
  84. for( var i=0; i<this.system_table_config_.RecordMaxLength; ++i ) {
  85. var b = 0;
  86. var name_data = new KOEI.WP7_2012.Horse.Name.NameData() {
  87. Id = (uint)i,
  88. Type = KOEI.WP7_2012.Horse.Name.NameDataType.SYSTEM,
  89. };
  90. using( var buff = new System.IO.MemoryStream() ) {
  91. while( true ) {
  92. if( ( b = ms.ReadByte() ) == -1 ) {
  93. throw new Exception("?????????????????");
  94. }
  95. if( !IsKana( (byte)b ) && !IsEnglish( (byte)b ) ) {
  96. throw new Exception("?????????????????(??)");
  97. }
  98. if( !IsKana( (byte)b ) ) {
  99. ms.Seek( -1, System.IO.SeekOrigin.Current );
  100. name_data.Kana = KOEI.WP7_2012.Helper.KoeiCode.ToString( buff.ToArray() );
  101. break;
  102. }
  103. buff.WriteByte( (byte)b );
  104. }
  105. }
  106. using( var buff = new System.IO.MemoryStream() ) {
  107. while( true ) {
  108. if( ( b = ms.ReadByte() ) == -1 ) {
  109. break;
  110. // throw new Exception("?????????????????");
  111. }
  112. if( !IsKana( (byte)b ) && !IsEnglish( (byte)b ) && i != this.system_table_config_.RecordMaxLength ) {
  113. throw new Exception("?????????????????(??)");
  114. }
  115. if( !IsEnglish( (byte)b ) ) {
  116. ms.Seek( -1, System.IO.SeekOrigin.Current );
  117. name_data.English = KOEI.WP7_2012.Helper.KoeiCode.ToString( buff.ToArray() );
  118. break;
  119. }
  120. // ?????????????
  121. if( ( b >= 0x57 && b <= 0x9E ) || b == 0xFD ) {
  122. buff.WriteByte( (byte)b );
  123. }
  124. }
  125. }
  126. this.name_data_dic_[i] = name_data;
  127. }
  128. }
  129. }
  130. private void CreateUserNameTable()
  131. {
  132. var table_size = BitConverter.ToUInt32( this.pm_.ReadMemory( this.user_table_config_.TableSizeAddress, 4 ), 0 );
  133. var pointer_address = BitConverter.ToUInt32( this.pm_.ReadMemory( this.user_table_config_.PointerAddress, 4 ), 0 );
  134. var table = this.pm_.ReadMemory( pointer_address, table_size );
  135. var i = this.user_table_config_.StartNumber;
  136. using( var ms = new System.IO.MemoryStream( table ) ) {
  137. var buff = new byte[9];
  138. while(true) {
  139. if( ms.Read( buff, 0, buff.Length ) != buff.Length ) {
  140. break;
  141. }
  142. if( buff[0] == 0 && i > config_.UserNameTableStartNumber + 1 ) {
  143. break;
  144. }
  145. var name = KOEI.WP7_2012.Helper.KoeiCode.ToString( buff );
  146. this.name_data_dic_[ (int)i ] = new KOEI.WP7_2012.Horse.Name.NameData() {
  147. Id = i,
  148. Kana = name,
  149. English = name,
  150. Type = KOEI.WP7_2012.Horse.Name.NameDataType.USER,
  151. };
  152. i++;
  153. }
  154. }
  155. }
  156. }
  157. }