Cosmos Engine - Unity3D Game Develop Framework
Inheritance: UnityEngine.MonoBehaviour
Example #1
0
        /// <summary>
        /// Engine entry.... all begins from here
        /// </summary>
        public static AppEngine New(GameObject gameObjectToAttach, IAppEntry entry, IList <IModuleInitable> modules)
        {
            Debuger.Assert(gameObjectToAttach != null && modules != null);
            AppEngine appEngine = gameObjectToAttach.AddComponent <AppEngine>();

            appEngine._isNewByStatic = true;
            appEngine.GameModules    = modules;
            appEngine.AppEntry       = entry;
            return(appEngine);
        }
Example #2
0
        /// <summary>
        /// Engine entry.... all begins from here
        /// </summary>
        public static AppEngine New(GameObject gameObjectToAttach, IModule[] modules, CoroutineDelegate before,
                                    CoroutineDelegate after)
        {
            Logger.Assert(gameObjectToAttach != null && modules != null);
            AppEngine appEngine = gameObjectToAttach.AddComponent <AppEngine>();

            appEngine.GameModules       = modules;
            appEngine.BeforeInitModules = before;
            appEngine.AfterInitModules  = after;
            return(appEngine);
        }
Example #3
0
        public static AssetFileLoader Load(string path, AssetFileBridgeDelegate assetFileLoadedCallback = null, LoaderMode loaderMode = LoaderMode.Async)
        {
            // 添加扩展名
            path = path + AppEngine.GetConfig(KEngineDefaultConfigs.AssetBundleExt);

            LoaderDelgate realcallback = null;

            if (assetFileLoadedCallback != null)
            {
                realcallback = (isOk, obj) => assetFileLoadedCallback(isOk, obj as Object);
            }

            return(AutoNew <AssetFileLoader>(path, realcallback, false, loaderMode));
        }
Example #4
0
        /// DES解密字符串
        /// 待解密的字符串
        /// 解密密钥,要求为8位,和加密密钥相同
        /// 解密成功返回解密后的字符串,失败返源串
        public string DecryptDES(string decryptString)
        {
            string decryptKey = AppEngine.GetConfig("KEngine", "CryptKey") ?? "testkey";

            try
            {
                byte[] rgbKey                    = Encoding.UTF8.GetBytes(decryptKey);
                byte[] rgbIV                     = CustomKeys ?? DefaultKeys;
                byte[] inputByteArray            = Convert.FromBase64String(decryptString);
                DESCryptoServiceProvider DCSP    = new DESCryptoServiceProvider();
                MemoryStream             mStream = new MemoryStream();
                CryptoStream             cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV),
                                                                    CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                return(Encoding.UTF8.GetString(mStream.ToArray()));
            }
            catch
            {
                return(decryptString);
            }
        }
Example #5
0
        /// DES加密字符串
        /// 待加密的字符串
        /// 加密密钥,要求为8位
        /// 加密成功返回加密后的字符串,失败返回源串
        public string EncryptDES(string encryptString)
        {
            string encryptKey = AppEngine.GetConfig("CryptKey"); // 钥匙

            try
            {
                byte[] rgbKey                    = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
                byte[] rgbIV                     = CustomKeys ?? DefaultKeys;
                byte[] inputByteArray            = Encoding.UTF8.GetBytes(encryptString);
                DESCryptoServiceProvider dCSP    = new DESCryptoServiceProvider();
                MemoryStream             mStream = new MemoryStream();
                CryptoStream             cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV),
                                                                    CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                return(Convert.ToBase64String(mStream.ToArray()));
            }
            catch
            {
                return(encryptString);
            }
        }
Example #6
0
        private IEnumerator _Init(string path, LoaderMode loaderMode)
        {
            IsLoadAssetBundle = AppEngine.GetConfig("KEngine", "IsLoadAssetBundle").ToInt32() != 0;

            Object getAsset = null;

            if (IsEditorLoadAsset)
            {
#if UNITY_EDITOR
                if (path.EndsWith(".unity"))
                {
                    // scene
                    getAsset = KResourceModule.Instance;
                    Log.LogWarning("Load scene from Build Settings: {0}", path);
                }
                else
                {
                    getAsset = UnityEditor.AssetDatabase.LoadAssetAtPath("Assets/" + KEngineDef.ResourcesBuildDir + "/" + path, typeof(UnityEngine.Object));
                    if (getAsset == null)
                    {
                        Log.Error("Asset is NULL(from {0} Folder): {1}", KEngineDef.ResourcesBuildDir, path);
                    }
                }
#else
                Log.Error("`IsEditorLoadAsset` is Unity Editor only");
#endif
                OnFinish(getAsset);
            }
            else if (!IsLoadAssetBundle)
            {
                string extension = Path.GetExtension(path);
                path = path.Substring(0, path.Length - extension.Length); // remove extensions

                getAsset = Resources.Load <Object>(path);
                if (getAsset == null)
                {
                    Log.Error("Asset is NULL(from Resources Folder): {0}", path);
                }
                OnFinish(getAsset);
            }
            else
            {
                _bundleLoader = AssetBundleLoader.Load(path, null, loaderMode);

                while (!_bundleLoader.IsCompleted)
                {
                    if (IsReadyDisposed) // 中途释放
                    {
                        _bundleLoader.Release();
                        OnFinish(null);
                        yield break;
                    }
                    yield return(null);
                }

                if (!_bundleLoader.IsSuccess)
                {
                    Log.Error("[AssetFileLoader]Load BundleLoader Failed(Error) when Finished: {0}", path);
                    _bundleLoader.Release();
                    OnFinish(null);
                    yield break;
                }

                var assetBundle = _bundleLoader.Bundle;

                DateTime beginTime = DateTime.Now;
#if UNITY_5 || UNITY_2017_1_OR_NEWER
                // Unity 5 下,不能用mainAsset, 要取对象名
                var abAssetName = Path.GetFileNameWithoutExtension(Url).ToLower();
                if (!assetBundle.isStreamedSceneAssetBundle)
                {
                    if (loaderMode == LoaderMode.Sync)
                    {
                        getAsset = assetBundle.LoadAsset(abAssetName);
                        Debuger.Assert(getAsset);
                        _bundleLoader.PushLoadedAsset(getAsset);
                    }
                    else
                    {
                        var request = assetBundle.LoadAssetAsync(abAssetName);
                        while (!request.isDone)
                        {
                            yield return(null);
                        }
                        Debuger.Assert(getAsset = request.asset);
                        _bundleLoader.PushLoadedAsset(getAsset);
                    }
                }
                else
                {
                    // if it's a scene in asset bundle, did nothing
                    // but set a fault Object the result
                    getAsset = KResourceModule.Instance;
                }
#else
                // 经过AddWatch调试,.mainAsset这个getter第一次执行时特别久,要做序列化
                //AssetBundleRequest request = assetBundle.LoadAsync("", typeof(Object));// mainAsset
                //while (!request.isDone)
                //{
                //    yield return null;
                //}
                try
                {
                    Debuger.Assert(getAsset = assetBundle.mainAsset);
                }
                catch
                {
                    Log.Error("[OnAssetBundleLoaded:mainAsset]{0}", path);
                }
#endif

                KResourceModule.LogLoadTime("AssetFileBridge", path, beginTime);

                if (getAsset == null)
                {
                    Log.Error("Asset is NULL: {0}", path);
                }
            }

            if (Application.isEditor)
            {
                if (getAsset != null)
                {
                    KResoourceLoadedAssetDebugger.Create(getAsset.GetType().Name, Url, getAsset as Object);
                }

                // 编辑器环境下,如果遇到GameObject,对Shader进行Fix
                if (getAsset is GameObject)
                {
                    var go = getAsset as GameObject;
                    foreach (var r in go.GetComponentsInChildren <Renderer>(true))
                    {
                        RefreshMaterialsShaders(r);
                    }
                }
            }

            if (getAsset != null)
            {
                // 更名~ 注明来源asset bundle 带有类型
                getAsset.name = String.Format("{0}~{1}", getAsset, Url);
            }
            OnFinish(getAsset);
        }
Example #7
0
        /// <summary>
        /// Check Bundles/[Platform]/xxxx.kk exists?
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static bool IsBundleResourceExist(string url)
        {
            if (KResourceModule.IsEditorLoadAsset)
            {
                var editorPath   = "Assets/" + KEngineDef.ResourcesBuildDir + "/" + url;
                var hasEditorUrl = File.Exists(editorPath);
                if (hasEditorUrl)
                {
                    return(true);
                }
            }

            return(KResourceModule.IsResourceExist(KResourceModule.BundlesPathRelative + url.ToLower() + AppEngine.GetConfig(KEngineDefaultConfigs.AssetBundleExt)));
        }