Exemple #1
0
        public virtual void Run(params object[] param)
        {
            if (_isRunning)
            {
                CLog.LogError("GameRunner,id=" + _id + " is running,can not run again!");
                return;
            }
            _isRunning = true;
            GameCfg gameCfg = GameConfig.GetGameCfg(this._id);

            if (GO == null)
            {
                GO = new GameObject(gameCfg.name);
                GameObject.DontDestroyOnLoad(GO);
            }
            Transform uiTrans = AppManager.Instance.GameStarter.transform.FindChild(gameCfg.ui + "_Template");

            if (uiTrans != null)
            {
                GameObject ui = GameObject.Instantiate(uiTrans.gameObject);
                CTLTools.AddChildToParent(ui, GO, false);
                ui.transform.Find("UICamera").GetComponent <Camera>().depth = gameCfg.uiDepth;
                ui.SetActive(true);
                ui.name = "UI";
            }
            else
            {
                CLog.LogError("can not find UI template in GameStarter!UI:" + gameCfg.ui);
            }

            STContainer = GO.AddComponent <SingletonContainer>();
            STContainer.ResourceMgr.Init(GameConfig.IsResourceLoadMode, string.Format("Assets/{0}/Res", gameCfg.rootDir));
            STContainer.LuaClient.BindGame(gameCfg.id);
            STContainer.LuaClient.StartGame();
        }
 public void LoadList(List <string> names, Action <MultiResourceLoader> OnComplete = null, Action <Resource> OnProgress = null, ResourceType resType = ResourceType.UnKnow)
 {
     if (names == null || names.Count == 0)
     {
         return;
     }
     for (int i = 0; i < names.Count; i++)
     {
         if (_loadList.Contains(names[i]))
         {
             CLog.LogError("Can not has same name in one MultiResourceLoader");
             return;
         }
         else
         {
             _loadList.Add(names[i]);
         }
     }
     this._OnComplete = OnComplete;
     this._OnProgress = OnProgress;
     for (int i = 0; i < names.Count; i++)
     {
         _resourceMgr.GetResource(names[i], OnFinish, OnFinish, resType);
     }
 }
Exemple #3
0
 /// <summary>
 /// 复制整个目录,包含子目录和文件
 /// </summary>
 public bool CopyDirectory(string srcDir, string destDir)
 {
     try
     {
         if (!Directory.Exists(srcDir))
         {
             CLog.LogError("CopyDirectory Error, srcDir not exist!srcDir=" + srcDir);
             return(false);
         }
         if (!Directory.Exists(destDir))
         {
             Directory.CreateDirectory(destDir);
         }
         DirectoryInfo srcDirInfo = new DirectoryInfo(srcDir);
         FileInfo[]    files      = srcDirInfo.GetFiles("*", SearchOption.TopDirectoryOnly);
         foreach (FileInfo file in files)
         {
             file.CopyTo(Path.Combine(destDir, file.Name));
         }
         DirectoryInfo[] directories = srcDirInfo.GetDirectories();
         foreach (DirectoryInfo dir in directories)
         {
             CopyDirectory(Path.Combine(srcDir, dir.Name), Path.Combine(destDir, dir.Name));
         }
         return(true);
     }
     catch (Exception e)
     {
         CLog.LogError("CopyDirectory Error!srcDir=" + srcDir + ",destDir=" + destDir + ",msg=" + e.Message + ",StackTrace=" + e.StackTrace);
         return(false);
     }
 }
        /// <summary>
        /// 获取到某个资源
        /// </summary>
        /// <param name="path">资源路径</param>
        /// <param name="onSucc">成功时的回调</param>
        /// <param name="onFail">失败时的回调</param>
        /// <param name="resType">资源类型,默认是UnKnow,如果是在bundle模式下,并且传入Unknow类型,会改成AssetBundle类型</param>
        /// <returns>返回当前加载的Resource</returns>
        public Resource GetResource(string path, ResourceHandler onSucc = null, ResourceHandler onFail = null,
                                    ResourceType resType = ResourceType.UnKnow)
        {
            if (string.IsNullOrEmpty(path))
            {
                CLog.LogError("[GetResource]ResName can not is null!");
                return(null);
            }

            Resource res;

            _mapRes.TryGetValue(GetCacheResourceKey(path), out res);
            if (res != null)
            {
                if (res.isDone)
                {
                    if (res.isSucc && onSucc != null)
                    {
                        ResourceHandler tempOnSucc = onSucc;
                        onSucc = null;
                        tempOnSucc.Invoke(res);
                        tempOnSucc = null;
                    }
                }
                else
                {
                    AddListener(res, onSucc, onFail);
                }
                return(res);
            }
            res         = new Resource();
            res.path    = path;
            res.resType = (!ResourcesLoadMode && resType == ResourceType.UnKnow) ? ResourceType.AssetBundle : resType;

            //获取到当前资源的依赖资源(可能没法保证顺序,所以拿的时候需要保证所有依赖资源都已经加载好)
            if (!ResourcesLoadMode && res.resType == ResourceType.AssetBundle)
            {
                string[] listDependResPath = GetDependResPath(res);
                if (listDependResPath != null && listDependResPath.Length > 0)
                {
                    List <Resource> listDependRes = new List <Resource>();
                    for (int i = 0; i < listDependResPath.Length; i++)
                    {
                        //加载依赖资源
                        Resource dependRes = GetResource(listDependResPath[i]);
                        listDependRes.Add(dependRes);
                    }
                    res.SetDependsRes(listDependRes);
                }
            }
            //真正加载当前资源
            _mapRes.Add(GetCacheResourceKey(res.path), res);
            res.Retain();
            AddListener(res, onSucc, onFail);
            _resLoader.Load(res);
            return(res);
        }
Exemple #5
0
        public static GameCfg GetGameCfg(int id)
        {
            GameCfg cfg;

            _mapGame.TryGetValue(id, out cfg);
            if (cfg == null)
            {
                CLog.LogError("can not find gameId=" + id + " GameCfg");
            }
            return(cfg);
        }
 public void RegisterRunner(IRunner runner)
 {
     if (!_mapRunners.ContainsKey(runner.GetID()))
     {
         _mapRunners.Add(runner.GetID(), runner);
     }
     else
     {
         CLog.LogError("runnerID:" + runner.GetID() + " has exist!");
     }
 }
        private void OnFinish(Resource res)
        {
            if (res.isSucc)
            {
                res.Retain();
                _mapRes.Add(res.path, res);
            }
            else
            {
                CLog.LogError("[MultiResourceLoader] load " + res.path + " fail!");
            }
            _finishCount++;
            List <Action <Resource> > list;

            _mapTryGetRes.TryGetValue(res.path, out list);
            if (list != null)
            {
                _mapTryGetRes.Remove(res.path);
                for (int i = 0; i < list.Count; i++)
                {
                    list[i].Invoke(res);
                }
            }
            if (_OnProgress != null)
            {
                Action <Resource> tempAction = _OnProgress;
                tempAction.Invoke(res);
            }
            if (_finishCount == _loadList.Count)
            {
                foreach (var item in _mapTryGetRes)
                {
                    Resource tempRes;
                    _mapRes.TryGetValue(item.Key, out tempRes);
                    if (tempRes == null)
                    {
                        continue;
                    }
                    for (int i = 0; i < item.Value.Count; i++)
                    {
                        item.Value[i].Invoke(tempRes);
                    }
                }
                _mapTryGetRes.Clear();
                if (_OnComplete != null)
                {
                    Action <MultiResourceLoader> tempAction = _OnComplete;
                    _OnComplete = null;
                    tempAction.Invoke(this);
                }
            }
        }
        public void Run(int id, params object[] param)
        {
            IRunner runner;

            _mapRunners.TryGetValue(id, out runner);
            if (runner != null)
            {
                runner.Run(param);
            }
            else
            {
                CLog.LogError("[run]can not find runnerID:" + id);
            }
        }
        public void Stop(int id)
        {
            IRunner runner;

            _mapRunners.TryGetValue(id, out runner);
            if (runner != null)
            {
                runner.Stop();
            }
            else
            {
                CLog.LogError("[stop]can not find runnerID:" + id);
            }
        }
Exemple #10
0
 public void RemoveFile(string path)
 {
     try
     {
         if (!File.Exists(path))
         {
             return;
         }
         File.Delete(path);
     }catch (Exception e)
     {
         CLog.LogError("Remove File Error!path=" + path);
     }
 }
        protected void OnTick(float dt)
        {
            _isUpdating = true;
//			CLog.Log ("_listSchedulerEntity.count:"+_listSchedulerEntity.Count);
            for (int i = 0; i < _listSchedulerEntity.Count; i++)
            {
                SchedulerEntity entity = _listSchedulerEntity [i];
                if (entity.state == SchedulerEntityState.Error)
                {
                    continue;
                }
                if (entity.state == SchedulerEntityState.ToDoAction)
                {
                    try
                    {
                        if (!entity.OnTick(dt))
                        {
                            entity.state = SchedulerEntityState.ToRemove;

                            SchedulerEntity RemoveEntity = _pool.GetObject();
                            RemoveEntity.Init(entity.handler);
                            RemoveEntity.state = SchedulerEntityState.ToRemove;
                            _listOperateEntity.Add(RemoveEntity);
                        }
                    }
                    //捕获异常,防止有异常后整个scheduler全部卡死
                    catch (System.Exception ex)
                    {
                        entity.state = SchedulerEntityState.Error;
                        CLog.LogError(ex.Message + "\n" + ex.StackTrace);
                    }
                }
            }
            _isUpdating = false;
            for (int i = 0; i < _listOperateEntity.Count; i++)
            {
                SchedulerEntity entity = _listOperateEntity [i];
                if (entity.state == SchedulerEntityState.ToAdd)
                {
                    AddScheduler(entity.handler, entity.delay, entity.times);
                }
                else if (entity.state == SchedulerEntityState.ToRemove)
                {
                    RemoveScheduler(entity.handler);
                }
                _pool.SaveObject(entity);
            }
            _listOperateEntity.Clear();
        }
 public static void AddChildToParent(GameObject child, GameObject parent, bool stayWorldPos = false)
 {
     if (child == null || parent == null)
     {
         CLog.LogError("AddChildToParent child or parent can not null!");
         return;
     }
     child.transform.parent = parent.transform;
     if (!stayWorldPos)
     {
         child.transform.localPosition = Vector3.zero;
         child.transform.localRotation = Quaternion.identity;
         child.transform.localScale    = Vector3.one;
     }
 }
        public static bool TryGameRunner(int gameId, out GameRunner gameRunner)
        {
            gameRunner = null;
            IRunner runner = AppManager.Instance.GetRunner(gameId);

            if (runner is GameRunner)
            {
                gameRunner = (GameRunner)runner;
                return(true);
            }
            else
            {
                CLog.LogError("gameId " + gameId + " is not GameRunner!");
                return(false);
            }
        }
 private void OnResourceDone(Resource res)
 {
     if (res.isSucc)
     {
         List <ResourceHandler> succList;
         _succCallbacks.TryGetValue(res, out succList);
         _succCallbacks.Remove(res);
         if (succList != null)
         {
             for (int i = 0; i < succList.Count; i++)
             {
                 succList[i].Invoke(res);
             }
             succList.Clear();
         }
         res.Release();
     }
     else
     {
         //失败的话,将资源先移除掉
         if (res.refCount > 1)
         {
             CLog.LogError("DestroyResource[resPath=" + res.path + "],RefCount>1.");
         }
         else
         {
             _mapRes.Remove(GetCacheResourceKey(res.path));
         }
         List <ResourceHandler> failList;
         _failCallbacks.TryGetValue(res, out failList);
         _failCallbacks.Remove(res);
         if (failList != null)
         {
             for (int i = 0; i < failList.Count; i++)
             {
                 failList[i].Invoke(res);
             }
             failList.Clear();
         }
         res.Release();
         if (res.refCount <= 0)
         {
             res.DestroyResource();
         }
     }
 }
Exemple #15
0
        IEnumerator LoadDirectResource(Resource res)
        {
            string          loadPath = GetInResPath(res);
            ResourceRequest request  = Resources.LoadAsync(loadPath);

            yield return(request);

            res.isDone = true;
            if (request.asset == null)
            {
                res.errorTxt = "Load resource [" + loadPath + "] fail!";
                CLog.LogError(res.errorTxt);
            }
            else
            {
                res.SetDirectObject(request.asset);
            }
            OnDone(res);
        }
        public LuaMultiState(LuaFileUtils luaFileUtils)
        {
#if !MULTI_STATE
            CLog.LogError("not define symbols MULTI_STATE!");
#endif
            this.luaFileUtils = luaFileUtils;
            if (mainState == null)
            {
                mainState = this;
            }

            LuaException.Init();
            L = LuaNewState();
            stateMap.Add(L, this);
            OpenToLuaLibs();
            ToLua.OpenLibs(L);
            OpenBaseLibs();
            LuaSetTop(0);
            InitLuaPath();
        }
Exemple #17
0
 public void RemoveDir(string dirPath)
 {
     try
     {
         if (!Directory.Exists(dirPath))
         {
             return;
         }
         string[] files = Directory.GetFiles(dirPath, "*.*", SearchOption.AllDirectories);
         foreach (var path in files)
         {
             string filePath = path.Replace("\\", "/");
             File.Delete(filePath);
         }
         RemoveEmptyDir(dirPath);
     }catch (Exception e)
     {
         CLog.LogError("remove dir error!path=" + dirPath);
     }
 }
Exemple #18
0
        void LoadDirectResource(Resource res)
        {
            string loadPath = GetInResPath(res);

            UnityEngine.Object go = null;
#if UNITY_EDITOR
            go = UnityEditor.AssetDatabase.LoadAssetAtPath <UnityEngine.Object>(loadPath);
#endif

            res.isDone = true;
            if (go == null)
            {
                res.errorTxt = "Load resource [" + loadPath + "] fail!";
                CLog.LogError(res.errorTxt);
            }
            else
            {
                res.SetDirectObject(go);
            }
            OnDone(res);
        }
        IEnumerator LoadWWWResource(Resource res)
        {
            string url = GetInResPath(res);

            using (WWW www = new WWW(url))
            {
                yield return(www);

                res.isDone = true;
                if (string.IsNullOrEmpty(www.error))
                {
                    res.SetWWWObject(www);
                }
                else
                {
                    res.errorTxt = www.error;
                    CLog.LogError("Load resource [" + url + "] fail!");
                }
            }
            OnDone(res);
        }
 public static T Deserialize <T>(string str)
 {
     if (str.IsEmpty())
     {
         CLog.LogError("Deserialize str can not null!");
         return(default(T));
     }
     try
     {
         IFormatter   formatter = new BinaryFormatter();
         byte[]       buffer    = Convert.FromBase64String(str);
         MemoryStream ms        = new MemoryStream(buffer);
         T            obj       = (T)formatter.Deserialize(ms);
         ms.Flush();
         ms.Close();
         return(obj);
     }
     catch (Exception ex)
     {
         throw new Exception("Serialize fail,reason:" + ex.Message);
     }
 }
 public static string Serialize <T>(T obj)
 {
     if (obj == null)
     {
         CLog.LogError("Serialize obj can not null!");
         return(null);
     }
     try
     {
         IFormatter   formatter = new BinaryFormatter();
         MemoryStream ms        = new MemoryStream();
         formatter.Serialize(ms, obj);
         ms.Position = 0;
         byte[] buffer = new byte[ms.Length];
         ms.Read(buffer, 0, buffer.Length);
         ms.Flush();
         ms.Close();
         return(Convert.ToBase64String(buffer));
     }
     catch (Exception ex)
     {
         throw new Exception("Serialize fail,reason:" + ex.Message);
     }
 }
        public void Init(string assetBundleEntryXmlContent)
        {
            try
            {
                XmlDocument document = new XmlDocument();
                document.LoadXml(assetBundleEntryXmlContent);

                foreach (XmlElement element in document.FirstChild.NextSibling.ChildNodes)
                {
                    if (element.Name == "AssetMappings")
                    {
                        mainAssetBundlePath = element.GetAttribute("manifest");
                        foreach (XmlElement mapping in element.ChildNodes)
                        {
                            assetPathToAssetBundleNames.Add(mapping.GetAttribute("assetName"), mapping.GetAttribute("bundleName"));
                        }
                    }
                }
            }
            catch (Exception e)
            {
                CLog.LogError(e.Message + "," + e.StackTrace);
            }
        }
 public static void LogError(string msg)
 {
     CLog.LogError(msg);
 }