示例#1
0
        public void LoadTableFromAssetBundle <T>(AssetBundle bundle) where T : class, global::ProtoBuf.IExtensible, global::ProtoBuf.IParseable, new()
        {
            if (null != bundle)
            {
                var         beginTime = System.DateTime.Now.Ticks;
                AssetBinary res       = null;

                var type = typeof(T);

                res = bundle.LoadAsset <AssetBinary>(type.Name);
                if (null == res)
                {
                    return;
                }

                var table = convertTableObject(res, type) as Dictionary <int, object>;
                if (null == table)
                {
                    Debug.LogErrorFormat("load failed table [<color=#ff0000>{0}</color>] failed !!!", type.Name);
                    return;
                }

                if (mTables.ContainsKey(type))
                {
                    mTables.Remove(type);
                }
                mTables.Add(type, table);

                var deltaTime = System.DateTime.Now.Ticks - beginTime;
                //Debug.LogErrorFormat("load {0} cost <color=#00ff00>{1}</color> ms!", type.Name, deltaTime / 10000);
            }
        }
示例#2
0
        public Dictionary <int, object> ReadTableFromResourcesFile <T>(string filePath) where T : class, global::ProtoBuf.IExtensible, global::ProtoBuf.IParseable, new()
        {
            var type      = typeof(T);
            var beginTime = System.DateTime.Now.Ticks;

            var path = System.IO.Path.Combine(filePath, type.Name);

            AssetBinary res = Resources.Load <AssetBinary>(path);

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

            var table = convertTableObject(res, type) as Dictionary <int, object>;

            if (null == table)
            {
                Debug.LogErrorFormat("load failed table [<color=#ff0000>{0}</color>] failed !!!", type.Name);
                return(null);
            }

            var deltaTime = System.DateTime.Now.Ticks - beginTime;

            //Debug.LogErrorFormat("load {0} cost <color=#00ff00>{1}</color> ms!", type.Name, deltaTime / 10000);

            return(table);
        }
示例#3
0
        public void LoadTableFromMemory <T>(byte[] datas) where T : class, global::ProtoBuf.IExtensible, global::ProtoBuf.IParseable, new()
        {
            AssetBinary res = new AssetBinary();

            res.m_DataBytes = datas;

            var type = typeof(T);

            if (null == res)
            {
                return;
            }

            var table = convertTableObject(res, type) as Dictionary <int, object>;

            if (null == table)
            {
                Debug.LogErrorFormat("load failed table [<color=#ff0000>{0}</color>] failed !!!", type.Name);
                return;
            }

            if (mTables.ContainsKey(type))
            {
                mTables.Remove(type);
            }

            mTables.Add(type, table);
        }
示例#4
0
        public void LoadTable(System.Type type)
        {
            var beginTime = System.DateTime.Now.Ticks;

            var path = mPath + type.Name;

            if (mTables.ContainsKey(type))
            {
                return;
            }

            AssetBinary res = null;

            res = AssetLoaderManager.Instance().LoadResources <AssetBinary>(path, AssetType.AT_ASSETS);

            if (null == res)
            {
                return;
            }

            var table = convertTableObject(res, type) as Dictionary <int, object>;

            if (null == table)
            {
                Debug.LogErrorFormat("load failed table [<color=#ff0000>{0}</color>] failed !!!", type.Name);
                return;
            }
            mTables.Add(type, table);

            var deltaTime = System.DateTime.Now.Ticks - beginTime;
            //Debug.LogErrorFormat("load {0} cost <color=#00ff00>{1}</color> ms!", type.Name, deltaTime / 10000);
        }
示例#5
0
        public static bool ConvertAsset(string dir, string filter, string save_dir)
        {
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
            string[] strList = Directory.GetFiles(dir, filter, SearchOption.TopDirectoryOnly);
            for (int i = 0; i < strList.Length; ++i)
            {
                var txtPath = strList[i];
                var name    = Path.GetFileNameWithoutExtension(txtPath);
                try
                {
                    var         assetPath = save_dir + name + ".asset";
                    AssetBinary asset     = ScriptableSingleton <AssetBinary> .CreateInstance <AssetBinary>();

                    asset.m_DataBytes = File.ReadAllBytes(txtPath);

                    if (File.Exists(assetPath))
                    {
                        AssetBinary oldAsset = AssetDatabase.LoadAssetAtPath <AssetBinary>(assetPath);
                        oldAsset.m_DataBytes = asset.m_DataBytes;
                        EditorUtility.SetDirty(oldAsset);
                        AssetDatabase.SaveAssets();
                    }
                    else
                    {
                        AssetDatabase.CreateAsset(asset, assetPath);
                    }
                    Debug.LogFormat("<color=#00ff00>convert <color=#ffff00>{0}.asset</color> succeed !</color>", name);
                }
                catch (System.Exception e)
                {
                    Debug.LogErrorFormat(e.ToString());
                    Debug.LogErrorFormat("<color=#ff0000>convert <color=#ffff00>{0}.asset</color> failed !</color>", name);
                    return(false);
                }
            }
            return(true);
        }
示例#6
0
        public Dictionary <int, object> ReadTableFromAssetBundle <T>(AssetBundle bundle) where T : class, global::ProtoBuf.IExtensible, global::ProtoBuf.IParseable, new()
        {
            if (null != bundle)
            {
                var         beginTime = System.DateTime.Now.Ticks;
                AssetBinary res       = null;

                var type = typeof(T);
                res = bundle.LoadAsset <AssetBinary>(type.Name);
                if (null == res)
                {
                    return(null);
                }

                var table = convertTableObject(res, type) as Dictionary <int, object>;

                var deltaTime = System.DateTime.Now.Ticks - beginTime;
                //Debug.LogErrorFormat("load {0} cost <color=#00ff00>{1}</color> ms!", type.Name, deltaTime / 10000);

                return(table);
            }
            return(null);
        }
示例#7
0
        object convertTableObject(AssetBinary asset, System.Type type)
        {
            if (asset == null || null == type)
            {
                return(null);
            }

            var IDMap = type.GetProperty("ID").GetGetMethod();

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

            Dictionary <int, object> table = new Dictionary <int, object>();
            bool bCanParse = ProtoBuf.Serializer.CanParse(type);

            byte[] data = asset.m_DataBytes;

            for (int i = 0; i < data.Length;)
            {
                int len = 0;
                for (int j = i; j < i + 8; ++j)
                {
                    if (data[j] > 0)
                    {
                        len = len * 10 + (data[j] - '0');
                    }
                }

                i += 8;

                MemoryStream mDataStream = new MemoryStream(data, i, len);

                try
                {
                    object tableData = null;

                    if (bCanParse)
                    {
                        tableData = ProtoBuf.Serializer.ParseEx(type, mDataStream);
                    }
                    else
                    {
                        tableData = ProtoBuf.Serializer.DeserializeEx(type, mDataStream);
                    }

                    if (tableData == null)
                    {
                        Debug.LogErrorFormat("table data is nil {0}, {1}", type.Name, i);
                    }
                    else
                    {
                        var id = (int)IDMap.Invoke(tableData, null);
                        if (!table.ContainsKey(id))
                        {
                            table.Add(id, tableData);
                        }
                        else
                        {
                            Debug.LogErrorFormat("table {0} key repeated id = {1}", type.Name, id);
                            return(null);
                        }
                    }
                }
                catch (System.Exception e)
                {
                    Debug.LogErrorFormat("{0} : *.cs don't match the *.xls, delete the *.proto, regenerate the *.cs", type.Name);
                    Debug.LogErrorFormat("error deserialize at line {0}, with error {1}", i + 1, e.ToString());

                    string ErrorMsg = "表格:" + type.Name + " 加载失败,原因:" + e.Message;

                    Debug.LogErrorFormat("【读表错误!】 {0}", ErrorMsg);

                    return(null);
                }

                i += len;
            }

            return(table);
        }