CheckFileCRC.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package com.xugame.gameconsole.util;
  2. import android.content.Context;
  3. import android.os.Bundle;
  4. import android.os.Handler;
  5. import android.os.Looper;
  6. import android.os.Message;
  7. import android.text.TextUtils;
  8. import androidx.annotation.NonNull;
  9. import com.xugame.gameconsole.R;
  10. import java.io.File;
  11. import java.io.FileInputStream;
  12. import java.io.FileNotFoundException;
  13. import java.io.IOException;
  14. import java.util.zip.CRC32;
  15. import java.util.zip.CheckedInputStream;
  16. public class CheckFileCRC implements Runnable {
  17. private static final String TAG = "CheckFileCRCTAG";
  18. private Thread mThread;
  19. private Handler mHandler;
  20. private CheckFileCRCListener mListener;
  21. private String mPath;
  22. private String mCoreName;
  23. private Context mContext;
  24. public CheckFileCRC(Context context, CheckFileCRCListener listener) {
  25. this.mContext = context;
  26. this.mListener = listener;
  27. }
  28. public void check(String path) {
  29. DebugUtil.i(TAG,"check"+path);
  30. if (TextUtils.isEmpty(path)) {
  31. this.mListener.onCheckCoresCRCError(-1);
  32. return;
  33. }
  34. mPath = path;
  35. File file = new File(path);
  36. if (!file.exists()) {
  37. this.mListener.onCheckCoresCRCError(-2);
  38. return;
  39. }
  40. mCoreName = file.getName();
  41. this.init();
  42. this.mThread = new Thread(this);
  43. this.mThread.setPriority(Thread.MAX_PRIORITY);
  44. this.mThread.start();
  45. }
  46. private void init() {
  47. mHandler = new Handler(Looper.myLooper()) {
  48. @Override
  49. public void handleMessage(@NonNull Message msg) {
  50. super.handleMessage(msg);
  51. if(msg.what==0){
  52. Bundle bundle=msg.getData();
  53. if(bundle!=null){
  54. long filecrc=bundle.getLong("filecrc");
  55. String dfCrc=bundle.getString("default_crc");
  56. if(mListener!=null){
  57. mListener.onCheckCoresCRCDone(""+filecrc,dfCrc);
  58. }
  59. }
  60. }
  61. }
  62. };
  63. }
  64. @Override
  65. public void run() {
  66. long filecrc = ChecksumCRC32(mPath);
  67. String defaultCrc = returnDEF(mCoreName);
  68. DebugUtil.i(TAG,"checkCrc="+filecrc+"defCrc="+defaultCrc);
  69. if(mHandler!=null){
  70. Message message=new Message();
  71. message.what=0;
  72. Bundle bundle=new Bundle();
  73. bundle.putLong("filecrc",filecrc);
  74. bundle.putString("default_crc",defaultCrc);
  75. message.setData(bundle);
  76. mHandler.sendMessage(message);
  77. }
  78. }
  79. public long ChecksumCRC32(String path) {
  80. long checksum = 0;
  81. CheckedInputStream cis = null;
  82. File file = new File(path);
  83. try {
  84. cis = new CheckedInputStream(new FileInputStream(file), new CRC32());
  85. byte[] buf = new byte[1024];
  86. while (cis.read(buf) >= 0) {
  87. checksum = cis.getChecksum().getValue();
  88. }
  89. } catch (FileNotFoundException e) {
  90. // TODO Auto-generated catch block
  91. e.printStackTrace();
  92. } catch (IOException e) {
  93. // TODO Auto-generated catch block
  94. e.printStackTrace();
  95. }
  96. return checksum;
  97. }
  98. private String returnDEF(String name) {
  99. if (name.equals("fbneo_libretro_android.so")) {
  100. return mContext.getResources().getString(R.string.fbneo_libretro_android);
  101. } else if (name.equals("fceumm_libretro_android.so")) {
  102. return mContext.getResources().getString(R.string.fceumm_libretro_android);
  103. } else if (name.equals("mame2010_libretro_android.so")) {
  104. return mContext.getResources().getString(R.string.mame2010_libretro_android);
  105. } else if (name.equals("snes9x2010_libretro_android.so")) {
  106. return mContext.getResources().getString(R.string.snes9x2010_libretro_android);
  107. } else if (name.equals("mgba_libretro_android.so")) {
  108. return mContext.getResources().getString(R.string.mgba_libretro_android);
  109. } else if (name.equals("tgbdual_libretro_android.so")) {
  110. return mContext.getResources().getString(R.string.tgbdual_libretro_android);
  111. } else if (name.equals("genesis_plus_gx_wide_libretro_android.so")) {
  112. return mContext.getResources().getString(R.string.genesis_plus_gx_wide_libretro_android);
  113. } else if (name.equals("mednafen_ngp_libretro_android.so")) {
  114. return mContext.getResources().getString(R.string.mednafen_ngp_libretro_android);
  115. } else if (name.equals("mednafen_pce_libretro_android.so")) {
  116. return mContext.getResources().getString(R.string.mednafen_pce_libretro_android);
  117. } else if (name.equals("pcsx_rearmed_libretro_android.so")) {
  118. return mContext.getResources().getString(R.string.pcsx_rearmed_libretro_android);
  119. } else if (name.equals("smsplus_libretro_android.so")) {
  120. return mContext.getResources().getString(R.string.smsplus_libretro_android);
  121. } else if (name.equals("mednafen_wswan_libretro_android.so")) {
  122. return mContext.getResources().getString(R.string.mednafen_wswan_libretro_android);
  123. } else if (name.equals("ppsspp_libretro_android.so")) {
  124. return mContext.getResources().getString(R.string.ppsspp_libretro_android);
  125. } else if (name.equals("mupen64plus_next_gles2_libretro_android.so")) {
  126. return mContext.getResources().getString(R.string.mupen64plus_next_gles2_libretro_android);
  127. } else if (name.equals("flycast_libretro_android.so")) {
  128. return mContext.getResources().getString(R.string.flycast_libretro_android);
  129. } else if (name.equals("stella_libretro_android.so")) {
  130. return mContext.getResources().getString(R.string.stella_libretro_android);
  131. } else if (name.equals("vice_x64_libretro_android.so")) {
  132. return mContext.getResources().getString(R.string.vice_x64_libretro_android);
  133. } else if (name.equals("picodrive_libretro_android.so")) {
  134. return mContext.getResources().getString(R.string.picodrive_libretro_android);
  135. } else {
  136. return "";
  137. }
  138. }
  139. }