Unity3D AssetStore 外部工具下载

Unity3D AssetStore 外部工具下载

参考https://www.jianshu.com/p/3ceb18e69d11

Unity 商店更新后还是没有很好得解决下载资源失败从头再来得问题。只好查找如何离线下载得文章,在简书上有一篇16年得文章利用反射机制调用编辑器解密下载文件得方法,但是因为unity得升级,该方法已经从AssetStoreUtils类里移除了。

解决方案

既然已经删除了DecryptFile函数,那反射肯定不行,那是不是可以从反编译得角度出发,找到该函数得实现方式,来直接解压。

资料查找

// 参考文档
https://github.com/jamesjlinden/unity-decompiled

实现

// 参考文档
//https://github.com/jamesjlinden/unity-decompiled
using UnityEditor;
using System;
using System.Globalization;
using System.IO;
using System.Security.Cryptography;

class DecryptUtility
{
    [MenuItem("Utility/DecryptFile")]
    static void DecryptFile()
    {
        var inputFile = @"D:\2223333";
        var key = "11122222";

        //var unityEditor = typeof(Editor).Assembly;

        //var assetStoreUtils = unityEditor.GetType("UnityEditor.AssetStoreUtils");

        //assetStoreUtils.Invoke("DecryptFile", inputFile, inputFile + ".unitypackage", key);

        DecryptFile(inputFile, inputFile + ".unitypackage", key);
    }

    private static void HexStringToByteArray(string hex, byte[] array, int offset)
    {
        if (offset + array.Length * 2 > hex.Length)
            throw new ArgumentException("Hex string too short");
        for (int index = 0; index < array.Length; ++index)
        {
            string s = hex.Substring(index * 2 + offset, 2);
            array[index] = byte.Parse(s, NumberStyles.HexNumber);
        }
    }

    public static void DecryptFile(string inputFile, string outputFile, string keyIV)
    {
        byte[] array1 = new byte[32];
        byte[] array2 = new byte[16];
        HexStringToByteArray(keyIV, array1, 0);
        HexStringToByteArray(keyIV, array2, 64);
        EditorUtility.DisplayProgressBar("Decrypting", "Decrypting package", 0.0f);
        FileStream fileStream1 = File.Open(inputFile, System.IO.FileMode.Open);
        FileStream fileStream2 = File.Open(outputFile, System.IO.FileMode.CreateNew);
        long length = fileStream1.Length;
        long num = 0;
        AesManaged aesManaged = new AesManaged();
        aesManaged.Key = array1;
        aesManaged.IV = array2;
        CryptoStream cryptoStream = new CryptoStream((Stream)fileStream1, aesManaged.CreateDecryptor(aesManaged.Key, aesManaged.IV), CryptoStreamMode.Read);
        try
        {
            byte[] numArray = new byte[40960];
            int count;
            while ((count = cryptoStream.Read(numArray, 0, numArray.Length)) > 0)
            {
                fileStream2.Write(numArray, 0, count);
                num += (long)count;
                if (EditorUtility.DisplayCancelableProgressBar("Decrypting", "Decrypting package", (float)num / (float)length))
                    throw new Exception("User cancelled decryption");
            }
        }
        finally
        {
            cryptoStream.Close();
            fileStream1.Close();
            fileStream2.Close();
            EditorUtility.ClearProgressBar();
        }
    }
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值