private void LoadDictionarySuccessCallback(string dictionaryAssetName, object dictionaryAsset, float duration, object userData)
        {
            LoadDictionaryInfo loadDictionaryInfo = (LoadDictionaryInfo)userData;

            if (loadDictionaryInfo == null)
            {
                throw new GameFrameworkException("Load dictionary info is invalid.");
            }

            try
            {
                if (!m_LocalizationHelper.LoadDictionary(dictionaryAsset, loadDictionaryInfo.LoadType, loadDictionaryInfo.UserData))
                {
                    throw new GameFrameworkException(Utility.Text.Format("Load dictionary failure in helper, asset name '{0}'.", dictionaryAssetName));
                }
            }
            catch (Exception exception)
            {
                if (m_LoadDictionaryFailureEventHandler != null)
                {
                    m_LoadDictionaryFailureEventHandler(this, new LoadDictionaryFailureEventArgs(dictionaryAssetName, exception.ToString(), loadDictionaryInfo.UserData));
                    return;
                }

                throw;
            }
            finally
            {
                m_LocalizationHelper.ReleaseDictionaryAsset(dictionaryAsset);
            }

            if (m_LoadDictionarySuccessEventHandler != null)
            {
                m_LoadDictionarySuccessEventHandler(this, new LoadDictionarySuccessEventArgs(dictionaryAssetName, duration, loadDictionaryInfo.UserData));
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 加载字典。
        /// </summary>
        /// <param name="dictionaryAssetName">字典资源名称。</param>
        /// <param name="priority">加载字典资源的优先级。</param>
        /// <param name="userData">用户自定义数据。</param>
        public void LoadDictionary(string dictionaryAssetName, int priority, object userData)
        {
            if (m_ResourceManager == null)
            {
                throw new GameFrameworkException("You must set resource manager first.");
            }

            if (m_LocalizationHelper == null)
            {
                throw new GameFrameworkException("You must set localization helper first.");
            }

            HasAssetResult result = m_ResourceManager.HasAsset(dictionaryAssetName);

            switch (result)
            {
            case HasAssetResult.AssetOnDisk:
            case HasAssetResult.AssetOnFileSystem:
                m_ResourceManager.LoadAsset(dictionaryAssetName, priority, m_LoadAssetCallbacks, userData);
                break;

            case HasAssetResult.BinaryOnDisk:
                m_ResourceManager.LoadBinary(dictionaryAssetName, m_LoadBinaryCallbacks, userData);
                break;

            case HasAssetResult.BinaryOnFileSystem:
                int    dictionaryLength = m_ResourceManager.GetBinaryLength(dictionaryAssetName);
                byte[] dictionaryBytes  = GlobalBytes.Get(dictionaryLength);
                if (dictionaryLength != m_ResourceManager.LoadBinaryFromFileSystem(dictionaryAssetName, dictionaryBytes))
                {
                    throw new GameFrameworkException(Utility.Text.Format("Load binary '{0}' from file system internal error.", dictionaryAssetName));
                }

                try
                {
                    if (!m_LocalizationHelper.LoadDictionary(dictionaryAssetName, dictionaryBytes, 0, dictionaryLength, userData))
                    {
                        throw new GameFrameworkException(Utility.Text.Format("Load dictionary failure in helper, asset name '{0}'.", dictionaryAssetName));
                    }

                    if (m_LoadDictionarySuccessEventHandler != null)
                    {
                        LoadDictionarySuccessEventArgs loadDictionarySuccessEventArgs = LoadDictionarySuccessEventArgs.Create(dictionaryAssetName, 0f, userData);
                        m_LoadDictionarySuccessEventHandler(this, loadDictionarySuccessEventArgs);
                        ReferencePool.Release(loadDictionarySuccessEventArgs);
                    }
                }
                catch (Exception exception)
                {
                    if (m_LoadDictionaryFailureEventHandler != null)
                    {
                        LoadDictionaryFailureEventArgs loadDictionaryFailureEventArgs = LoadDictionaryFailureEventArgs.Create(dictionaryAssetName, exception.ToString(), userData);
                        m_LoadDictionaryFailureEventHandler(this, loadDictionaryFailureEventArgs);
                        ReferencePool.Release(loadDictionaryFailureEventArgs);
                        return;
                    }

                    throw;
                }
                break;

            default:
                throw new GameFrameworkException(Utility.Text.Format("Dictionary asset '{0}' is '{1}'.", dictionaryAssetName, result.ToString()));
            }
        }