Esempio n. 1
0
        /// <summary>
        /// 第一次使用LuaContext类时触发
        /// </summary>
        static LuaContext()
        {
            UNIEnv.setup();
            _contexts = new Dictionary <int, WeakReference> ();
            _regTypes = new List <Type> ();

            //反射所有类型
            getRegTypes();
        }
Esempio n. 2
0
        /// <summary>
        /// 从Asset中拷贝lua文件
        /// </summary>
        /// <param name="path">目录路径.</param>
        private void copyLuaFileFromAsset(string path)
        {
            AndroidJavaObject currentActivity = UNIEnv.getCurrentActivity();
            AndroidJavaObject assetManager    = currentActivity.Call <AndroidJavaObject> ("getAssets", new object[0]);

            string[] subpaths = assetManager.Call <string[]> ("list", path);
            if (subpaths.Length > 0)
            {
                //当前path为目录
                foreach (string subpath in subpaths)
                {
                    string targetPath = path == "" ? subpath : string.Format("{0}/{1}", path, subpath);
                    copyLuaFileFromAsset(targetPath);
                }
            }
            else
            {
                //当前path为文件
                if (path.ToLower().EndsWith(".lua"))
                {
                    //为lua文件,则进行拷贝
                    string destFilePath = getLuaCacheFilePath(path);

                    AndroidJavaObject inputStream = assetManager.Call <AndroidJavaObject>("open", path);
                    using (MemoryStream ms = new MemoryStream())
                    {
                        try
                        {
                            IntPtr   buffer = AndroidJNI.NewByteArray(1024);
                            jvalue[] args   = new jvalue[1];
                            args[0].l = buffer;

                            IntPtr readMethodId = AndroidJNIHelper.GetMethodID(inputStream.GetRawClass(), "read", "([B)I");
                            int    hasRead      = 0;
                            while ((hasRead = AndroidJNI.CallIntMethod(inputStream.GetRawObject(), readMethodId, args)) != -1)
                            {
                                byte[] byteArray = AndroidJNIHelper.ConvertFromJNIArray <byte[]>(buffer);
                                ms.Write(byteArray, 0, hasRead);
                            }

                            ms.Seek(0, SeekOrigin.Begin);
                            byte[] bytes = new byte[ms.Length];
                            ms.Read(bytes, 0, bytes.Length);

                            File.WriteAllBytes(destFilePath, bytes);
                        }
                        finally
                        {
                            ms.Close();
                        }
                    }
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// 获取Lua文件缓存目录
        /// </summary>
        /// <returns>换粗目录对象.</returns>
        private AndroidJavaObject getLuaCacheDir()
        {
            AndroidJavaObject currentActivity  = UNIEnv.getCurrentActivity();
            AndroidJavaClass  EnvironmentClass = new AndroidJavaClass("android.os.Environment");

            int perm = currentActivity.Call <int> ("checkCallingOrSelfPermission", new object[] { "android.permission.WRITE_EXTERNAL_STORAGE" });

            AndroidJavaObject cacheDir = null;

            if ("mounted" == EnvironmentClass.CallStatic <string> ("getExternalStorageState", new object[0]) && perm == 0)
            {
                cacheDir = currentActivity.Call <AndroidJavaObject> ("getExternalCacheDir", new object[0]);
            }

            if (cacheDir == null)
            {
                cacheDir = currentActivity.Call <AndroidJavaObject> ("getCacheDir", new object[0]);
            }

            string cachePath = cacheDir.Call <string>("toString", new object[0]);

            return(new AndroidJavaObject("java.io.File", string.Format("{0}/lua", cachePath)));
        }
Esempio n. 4
0
 /// <summary>
 /// 第一次使用LuaContext类时触发
 /// </summary>
 static LuaContext()
 {
     UNIEnv.setup();
     _contexts = new Dictionary <int, WeakReference> ();
 }