Exemple #1
0
        /**
         * 取得一个实现接口的实例
         * @param interfaceName
         * @return
         *
         */
        public static object __getOneInstance(string aliasName)
        {
            if (string.IsNullOrEmpty(aliasName))
            {
                DebugX.LogWarning("aliasName is empty:" + aliasName);
                return(null);
            }
            object target = null;

            if (uniqueInstanceMap.TryGetValue(aliasName, out target))
            {
                return(target);
            }

            Type c;

            if (uniqueMap.TryGetValue(aliasName, out c) == true)
            {
                object ins = Activator.CreateInstance(c);
                uniqueInstanceMap[aliasName]  = ins;
                uniqueInstanceMap[c.FullName] = ins;
                return(ins);
            }

            c = getClass(aliasName);

            if (c == null)
            {
                return(null);
            }

            return(Activator.CreateInstance(c));
        }
Exemple #2
0
        protected override void _loadImp(int priority = 0, bool progress = false, uint retryCount = 0)
        {
            string eventType = EventX.COMPLETE;


#if UNITY_EDITOR
            _data = UnityEditor.AssetDatabase.LoadAssetAtPath <UnityEngine.Object>(url);
            if (_data == null)
            {
                _data = Resources.Load(url);
            }
#else
            _data = Resources.Load(url);
#endif


            if (_data == null)
            {
                _data     = "_data is null";
                eventType = EventX.FAILED;

                string message = string.Format("加载文件失败,Resources文件夹下不存在:{0} ", url);
                DebugX.LogWarning(message);
                resourceComplete(eventType, message);
            }
            else
            {
                resourceComplete(eventType);
            }
        }
Exemple #3
0
        /**
         * 取得实现接口的单例,必须含有 getInstance的静态方法;
         * @param interfaceName 接口名称;
         * @return 返回实现的单例
         *
         */
        public static object getInstance(string aliasName)
        {
            if (string.IsNullOrEmpty(aliasName))
            {
                DebugX.LogWarning("aliasName is empty:" + aliasName);
                return(null);
            }

            object ins;

            if (uniqueInstanceMap.TryGetValue(aliasName, out ins) == false)
            {
                Type c;
                if (uniqueMap.TryGetValue(aliasName, out c) == false)
                {
                    c = getClass(aliasName);
                }

                if (c == null)
                {
                    return(null);
                }
                uniqueInstanceMap[aliasName] = ins = Activator.CreateInstance(c);
            }
            return(ins);
        }
Exemple #4
0
        /// <summary>
        /// 注册一个实例与名称对应的关系;
        /// </summary>
        /// <param name="aliasName"></param>
        /// <param name="instance"></param>
        public static void registerSingleton(string aliasName, object instance)
        {
            if (string.IsNullOrEmpty(aliasName))
            {
                DebugX.LogWarning("aliasName is empty:" + aliasName);
                return;
            }

            if (instance == null)
            {
                uniqueInstanceMap.Remove(aliasName);
                return;
            }
            Type   clazz         = instance.GetType();
            string fullClassName = clazz.FullName;

            __classMap[aliasName] = clazz;

            uniqueMap[aliasName]     = clazz;
            uniqueMap[fullClassName] = clazz;

            __aliasMap[fullClassName]    = aliasName;
            __aliasMap[aliasName]        = aliasName;
            uniqueInstanceMap[aliasName] = instance;
        }
        public override void load()
        {
            if (string.IsNullOrEmpty(_url))
            {
                this.dispatchEvent(new EventX(EventX.FAILED, "文件路径为空:" + _url));
                return;
            }

            if (_data != null)
            {
                this.simpleDispatch(EventX.COMPLETE, _data);
                return;
            }

            if (_status == LoadState.LOADING)
            {
                //正在加载中;
                DebugX.LogWarning("正在load资源:" + _url);
                return;
            }

            if (!isLocalFile)
            {
                DebugX.LogWarning("从远程加载资源:" + _url);
            }
            _retryedCount = 0;

            _status = LoadState.LOADING;
            if (coroutine != null)
            {
                StopCoroutine(coroutine);
            }
            coroutine = StartCoroutine(doLoad(_url));
        }
        protected void retryLoad()
        {
            DebugX.LogWarning("{0}重试:{1}", _retryedCount, _url);
            _status = LoadState.LOADING;

            if (coroutine != null)
            {
                StopCoroutine(coroutine);
            }
            coroutine = StartCoroutine(doLoad(_url));
        }
Exemple #7
0
 public static void unregisterClass(string aliasName)
 {
     if (string.IsNullOrEmpty(aliasName))
     {
         DebugX.LogWarning("aliasName is empty:" + aliasName);
         return;
     }
     if (__classMap.ContainsKey(aliasName))
     {
         __classMap.Remove(aliasName);
     }
 }
        protected override IEnumerator doLoad()
        {
            www = new WWW(formatedURL(url));

            if (checkProgress)
            {
                TickManager.add(progress);
            }
            yield return(www);

            if (checkProgress)
            {
                TickManager.remove(progress);
            }

            if (www == null)
            {
                _state = AssetState.FAILD;
                _data  = null;
                string message = string.Format("加载文件失败:{0} 已被dispose", url);
                DebugX.Log(message);
                this.dispatchEvent(new EventX(EventX.FAILED, message));
            }
            else
            {
                if (string.IsNullOrEmpty(www.error))
                {
//                    if (www.isDone)
//                    {
                    _state = AssetState.READY;
                    _data  = www.bytes;
                    this.simpleDispatch(EventX.COMPLETE, _data);
                    //                   }
//                    else
//                    {
//                        _state = AssetState.FAILD;
//                        _data = null;
//                        string message = string.Format("加载文件失败:{0} 没有完成", url);
//                        DebugX.Log(message);
//                        this.dispatchEvent(new EventX(EventX.FAILED, message));
//                    }
                }
                else
                {
                    _state = AssetState.FAILD;
                    _data  = null;
                    string message = string.Format("加载文件失败:{0} error:{1}", url, www.error);
                    DebugX.LogWarning(message);
                    this.simpleDispatch(EventX.FAILED, message);
                }
            }
        }
        public override void load()
        {
            if (_data != null)
            {
                this.simpleDispatch(EventX.COMPLETE, _data);
                return;
            }

            if (_status == LoadState.LOADING)
            {
                //正在加载中;
                DebugX.LogWarning("localLoading:" + fullLocalURL);
                return;
            }

            if (File.Exists(fullLocalURL) == false)
            {
                _status = LoadState.ERROR;
                string message = string.Format("加载文件失败:{0} error:文件不存在", fullLocalURL);
                DebugX.LogWarning(message);
                this.simpleDispatch(EventX.FAILED, message);
                return;
            }

            switch (_parserType)
            {
            case LoaderXDataType.ASSETBUNDLE:
            case LoaderXDataType.MANIFEST:
                onAssetBundleHandle(AssetBundle.LoadFromFile(fullLocalURL));
                break;

            case LoaderXDataType.TEXTURE:
                _status = LoadState.COMPLETE;
                byte[]    bytes = File.ReadAllBytes(fullLocalURL);
                Texture2D tex   = new Texture2D(2, 2, TextureFormat.ARGB32, false, false);
                tex.LoadImage(bytes);
                _data = tex;
                this.simpleDispatch(EventX.COMPLETE, _data);
                break;

            case LoaderXDataType.BYTES:
            case LoaderXDataType.AMF:
                _status = LoadState.COMPLETE;

                _data = File.ReadAllBytes(fullLocalURL);
                this.simpleDispatch(EventX.COMPLETE, _data);
                break;
            }

            selfComplete();
        }
Exemple #10
0
        public static bool isInUnique(string aliasName)
        {
            if (string.IsNullOrEmpty(aliasName))
            {
                DebugX.LogWarning("aliasName is empty:" + aliasName);
                return(false);
            }
            if (uniqueInstanceMap.ContainsKey(aliasName))
            {
                return(true);
            }

            return(uniqueMap.ContainsKey(aliasName));
        }
Exemple #11
0
        /**
         * 取得一个实现接口的类
         * @param interfaceName
         * @return
         *
         */
        public static Type getClass(string aliasName)
        {
            if (string.IsNullOrEmpty(aliasName))
            {
                DebugX.LogWarning("aliasName is empty:" + aliasName);
                return(null);
            }
            Type c;

            if (__classMap.TryGetValue(aliasName, out c) == false)
            {
                c = Type.GetType(aliasName);
            }
            return(c);
        }
Exemple #12
0
        /// <summary>
        /// 取得别名;
        /// </summary>
        /// <returns>
        /// 别名,null为没有注册过别名
        /// </returns>
        /// <param name='fullName'>
        /// Full name.
        /// </param>
        public static string getAliasName(string fullName)
        {
            if (string.IsNullOrEmpty(fullName))
            {
                DebugX.LogWarning("aliasName is empty:" + fullName);
                return(null);
            }
            string aliasName;

            if (__aliasMap.TryGetValue(fullName, out aliasName) == true)
            {
                return(aliasName);
            }

            return(null);
        }
Exemple #13
0
        public static void ADDTOMAPPING(string _url, AssetBundle assetBundle)
        {
            if (assetBundle == null)
            {
                DebugX.LogWarning("assetBundle is null :" + _url);
                return;
            }
            AssetBundle old = null;
            string      key = _url.ToLower();

            assetBundleMapping.TryGetValue(key, out old);
            if (old != null)
            {
                throw new Exception("assetBundle exit:" + key);
            }

            assetBundleMapping[key] = assetBundle;
        }
Exemple #14
0
        protected virtual void onAssetBundleHandle(AssetBundle assetBundle)
        {
            if (assetBundle != null)
            {
                ADDTOMAPPING(_url, assetBundle);

                _status = LoadState.COMPLETE;
                _data   = assetBundle;
                this.simpleDispatch(EventX.COMPLETE, _data);
            }
            else
            {
                _status = LoadState.ERROR;
                _data   = null;

                string message = string.Format("load:加载:{0},数据没有assetBundle", _url);
                DebugX.LogWarning(message);
                this.simpleDispatch(EventX.FAILED, message);
            }
        }
Exemple #15
0
        public static void registerSingleton <T>(string aliasName) where T : new()
        {
            if (string.IsNullOrEmpty(aliasName))
            {
                DebugX.LogWarning("aliasName is empty:" + aliasName);
                return;
            }

            Type clazz = typeof(T);

            string fullClassName = clazz.FullName;

            __classMap[aliasName] = clazz;

            __aliasMap[fullClassName] = aliasName;
            __aliasMap[aliasName]     = aliasName;

            uniqueMap[aliasName]     = clazz;
            uniqueMap[fullClassName] = clazz;
        }
        protected override void _loadImp(int priority = 0, bool progress = false, uint retryCount = 0)
        {
            string eventType = EventX.COMPLETE;

            _data = Resources.Load(url) as GameObject;

            if (_data == null)
            {
                _data     = "_data is null";
                eventType = EventX.FAILED;

                string message = string.Format("加载文件失败,Resources文件夹下不存在:{0} ", url);
                DebugX.LogWarning(message);
                resourceComplete(eventType, message);
            }
            else
            {
                resourceComplete(eventType);
            }
        }
Exemple #17
0
 public object InternalCreateInstance(Type type, object[] args)
 {
     if (type != null)
     {
         if (type.IsAbstract && type.IsSealed)
         {
             return(type);
         }
         try
         {
             return(Activator.CreateInstance(type, args));
         }
         catch (Exception exception)
         {
             string msg = String.Format("amf Activator.CreateInstance {0} error type:{1}", type.FullName,
                                        exception.Message);
             DebugX.LogWarning(msg);
         }
     }
     return(null);
 }
        private IEnumerator doLoad(string url)
        {
            UnityWebRequest request;

            switch (_parserType)
            {
            case LoaderXDataType.BYTES:
            case LoaderXDataType.AMF:
            case LoaderXDataType.TEXTURE:
                request = UnityWebRequest.Get(url);
                break;

            case LoaderXDataType.MANIFEST:
            case LoaderXDataType.ASSETBUNDLE:
                request = UnityWebRequest.Get(url);
                break;

            case LoaderXDataType.POST:
                request = UnityWebRequest.Post(url, postData);
                break;

            case LoaderXDataType.GET:
                string fullPath = url;
                if (string.IsNullOrEmpty(postData) == false)
                {
                    fullPath = url + "?" + postData;
                }
                request = UnityWebRequest.Get(fullPath);
                break;

            default:
                request = UnityWebRequest.Get(url);
                break;
            }

            float stratTime = Time.realtimeSinceStartup;
            bool  isTimeout = false;

            if (timeout > 0)
            {
                request.timeout = Mathf.CeilToInt(timeout);
            }
            while (!request.isDone)
            {
                if (timeout > 0 && (Time.realtimeSinceStartup - stratTime) > (timeout * 1.2f))
                {
                    isTimeout = true;
                    break;
                }

                if (checkProgress)
                {
                    update(request.downloadProgress);
                }

                yield return(request.SendWebRequest());
            }

            long responseCode = request.responseCode;

            if (request.isNetworkError || (responseCode != 200 && responseCode != 204) || isTimeout)
            {
                string error = "code=" + responseCode;
                if (isTimeout)
                {
                    error += ",error=isTimeout:" + timeout;
                }
                else if (request.isNetworkError)
                {
                    error += ",error=" + request.error;
                }
                else
                {
                    if (responseCode == 404)
                    {
                        mapping404[_url] = true;
                    }
                }
                _status = LoadState.ERROR;
                string message = string.Format("下载文件失败:{0} reason:{1}", _url, error);
                DebugX.LogWarning(message);

                request.Dispose();
                request = null;

                if (retryCount > _retryedCount)
                {
                    _retryedCount++;
                    _status = LoadState.LOADING;
                    ///本身加载需要时间,所以不必later太长
                    CallLater.Add(retryLoad, 1.0f);
                    yield break;
                }

                this.simpleDispatch(EventX.FAILED, message);
            }
            else
            {
                _status = LoadState.COMPLETE;
                switch (_parserType)
                {
                case LoaderXDataType.BYTES:
                case LoaderXDataType.AMF:
                    _data = request.downloadHandler.data;
                    break;

                case LoaderXDataType.ASSETBUNDLE:
                case LoaderXDataType.MANIFEST:
                    onAssetBundleHandle(AssetBundle.LoadFromMemory(request.downloadHandler.data));
                    break;

                case LoaderXDataType.TEXTURE:
                    //linner;
                    Texture2D tex = new Texture2D(2, 2, TextureFormat.ARGB32, false, false);
                    tex.LoadImage(request.downloadHandler.data);
                    _data = tex;
                    break;

                default:
                    _data = request.downloadHandler.data;
                    break;
                }
                request.Dispose();
                request = null;
                this.simpleDispatch(EventX.COMPLETE, _data);
            }
            selfComplete();
        }