Exemplo n.º 1
0
 private static void GetZipReport(Action <ReadZipReport> onResult)
 {
     if (Application.platform == RuntimePlatform.Android)
     {
         CodesignUtils.GetAndroidCodeMD5Asyn(onResult);
     }
     else
     {
         var md5    = CodesignUtils.GetSuperCodeMD5();
         var report = new ReadZipReport
         {
             codeMD5     = md5,
             fileList    = "",
             fileListMD5 = "",
         };
         onResult?.Invoke(report);
     }
 }
Exemplo n.º 2
0
    public static void GetAndroidCodeMD5Asyn(Action <ReadZipReport> result)
    {
        var t = new Thread(() =>
        {
            try
            {
                var resport = ReadZip();
                result?.Invoke(resport);
            }
            catch (Exception e)
            {
                UnityEngine.Debug.LogException(e);
                var resport = new ReadZipReport()
                {
                    codeMD5     = "crash",
                    fileList    = "crash",
                    fileListMD5 = "crash",
                };
                result?.Invoke(resport);
            }
        });

        t.Start();
    }
Exemplo n.º 3
0
    /*
     * public static ReadZipReport ReadZip()
     * {
     *  var sw = new Stopwatch();
     *  sw.Start();
     *  var ret = new ReadZipReport();
     *  ZipConstants.DefaultCodePage = Encoding.UTF8.CodePage;
     *  var apkPath = Application.dataPath;
     *  var file = File.Open(apkPath, FileMode.Open, FileAccess.Read);
     *  var stream = new ZipInputStream(file);
     *  var LENGTH = 2048;
     *  var tempBytes = new byte[LENGTH];
     *  MD5 codeMD5Calculator = null;
     *  if(CCodesign.calculateCodeMD5)
     *  {
     *      codeMD5Calculator = MD5.Create();
     *  }
     *  var sb = new StringBuilder();
     *  var first = true;
     *  var entry = stream.GetNextEntry();
     *  while (entry != null)
     *  {
     *      if (entry.IsFile)
     *      {
     *          var name = entry.Name;
     *          var isManaged = name.Contains("Managed");
     *          var isSo = name.EndsWith(".so", StringComparison.Ordinal);
     *          var isDex = name.EndsWith(".dex", StringComparison.Ordinal);
     *
     *          if(isManaged || isSo || isDex)
     *          {
     *              var size = stream.Length;
     *              if(CCodesign.calculateCodeMD5)
     *              {
     *                  var len = stream.Read(tempBytes, 0, LENGTH);
     *                  while (len > 0)
     *                  {
     *                      codeMD5Calculator.TransformBlock(tempBytes, 0, len, tempBytes, 0);
     *                      len = stream.Read(tempBytes, 0, LENGTH);
     *                  }
     *              }
     *              // file list
     *              if(first)
     *              {
     *                  first = false;
     *              }
     *              else
     *              {
     *                  sb.Append(",");
     *              }
     *              var shortName = Path.GetFileName(name);
     *              sb.Append(shortName);
     *              sb.Append(",");
     *              sb.Append(size);
     *
     *              //Debug.Log(shortName + " - " + size + " bytes");
     *          }
     *
     *      }
     *
     *      try
     *      {
     *          entry = stream.GetNextEntry();
     *      }
     *      catch
     *      {
     *          break;
     *      }
     *  }
     *  stream.Close();
     *  if(CCodesign.calculateCodeMD5)
     *  {
     *      codeMD5Calculator.TransformFinalBlock(tempBytes, 0, 0);
     *      var hash = codeMD5Calculator.Hash;
     *      var hex = ByteToHexStr(hash);
     *      ret.codeMD5 = hex;
     *      codeMD5Calculator.Dispose();
     *  }
     *  {
     *      var fileList = sb.ToString();
     *      ret.fileList = fileList;
     *      var fileListBytes = Encoding.UTF8.GetBytes(fileList);
     *      var md5Culculator = MD5.Create();
     *      var hash = md5Culculator.ComputeHash(fileListBytes);
     *      md5Culculator.Dispose();
     *      var hex = ByteToHexStr(hash);
     *      ret.fileListMD5 = hex;
     *  }
     *  sw.Stop();
     #if !RELEASE
     *  UnityEngine.Debug.Log("ReadZip usetime " + sw.ElapsedMilliseconds);
     #endif
     *  return ret;
     * }*/


    public static ReadZipReport ReadZip()
    {
#if !RELEASE
        var sw = new Stopwatch();
        sw.Start();
#endif
        var ret = new ReadZipReport();
        ZipConstants.DefaultCodePage = Encoding.UTF8.CodePage;
        var apkPath           = Application.dataPath;
        var LENGTH            = 2048;
        var tempBytes         = new byte[LENGTH];
        MD5 codeMD5Calculator = null;
        if (CCodesign.calculateCodeMD5)
        {
            codeMD5Calculator = MD5.Create();
        }
        var sb    = new StringBuilder();
        var first = true;

        var zipFileStream = File.OpenRead(apkPath);
        var zipFile       = new ZipFile(zipFileStream);
        var e             = zipFile.GetEnumerator();

        while (e.MoveNext())
        {
            var entry = e.Current as ZipEntry;

            if (entry.IsFile)
            {
                var name      = entry.Name;
                var isManaged = name.Contains("Managed");
                var isSo      = name.EndsWith(".so", StringComparison.Ordinal);
                var isDex     = name.EndsWith(".dex", StringComparison.Ordinal);

                if (isManaged || isSo || isDex)
                {
                    var size = entry.Size;
                    if (CCodesign.calculateCodeMD5)
                    {
                        using (var stream = zipFile.GetInputStream(entry))
                        {
                            var len = stream.Read(tempBytes, 0, LENGTH);
                            while (len > 0)
                            {
                                codeMD5Calculator.TransformBlock(tempBytes, 0, len, tempBytes, 0);
                                len = stream.Read(tempBytes, 0, LENGTH);
                            }
                        }
                    }
                    // file list
                    if (first)
                    {
                        first = false;
                    }
                    else
                    {
                        sb.Append(",");
                    }
                    var shortName = Path.GetFileName(name);
                    sb.Append(shortName);
                    sb.Append(",");
                    sb.Append(size);

                    //Debug.Log(shortName + " - " + size + " bytes");
                }
            }
        }
        zipFile.Close();
        if (CCodesign.calculateCodeMD5)
        {
            codeMD5Calculator.TransformFinalBlock(tempBytes, 0, 0);
            var hash = codeMD5Calculator.Hash;
            var hex  = ByteToHexStr(hash);
            ret.codeMD5 = hex;
            codeMD5Calculator.Dispose();
        }
        {
            var fileList = sb.ToString();
            ret.fileList = fileList;
            var fileListBytes = Encoding.UTF8.GetBytes(fileList);
            var md5Culculator = MD5.Create();
            var hash          = md5Culculator.ComputeHash(fileListBytes);
            md5Culculator.Dispose();
            var hex = ByteToHexStr(hash);
            ret.fileListMD5 = hex;
        }
#if !RELEASE
        sw.Stop();
        UnityEngine.Debug.Log("ReadZip usetime " + sw.ElapsedMilliseconds);
#endif
        return(ret);
    }