Exemplo n.º 1
0
    public void LoadDataFile(string FileName)
    {
        try
        {
            //首先检查用户数据目录里是否有该文件//
            //string file_name = CUtility.GetFullFilePath(FileName);
            string file_name = TempLoad.GetResFullPath(FileName);
            int    item_size = 0;
            buffer = File.ReadAllBytes(file_name);
            int data_start = 0;
            if (buffer.Length > 38 && buffer[0] == 'M' && buffer[1] == 'p' && buffer[2] == 'd' && buffer[3] == '\n')
            {
                //取md5码
                int index = 4;
                while (buffer[index] != '\n' && index < 38) //MD5一般32位
                {
                    index++;
                }
                string md5 = System.Text.Encoding.Default.GetString(buffer, 4, index - 4);
                if (md5 != GetProtoMd5())
                {
                    Debug.LogError(string.Format("Load file({0}) failed, data md5 {1} != {2} (code md5), protobuf struct dismatch.", FileName, md5, GetProtoMd5()));
                }

                //取svn版本号
                data_start = index + 1;
                while (buffer.Length > data_start && buffer[data_start] != '\n' && data_start < 50)
                {
                    data_start++;
                }
                //Log.Debug("svn ver:{0}", System.Text.Encoding.Default.GetString(buffer, index+1, data_start - index - 1));

                data_start++;//跳过 \n 后面才是数据正文
                byte[] tbuf = buffer;
                buffer = new byte[tbuf.Length - data_start];
                Buffer.BlockCopy(tbuf, data_start, buffer, 0, tbuf.Length - data_start);
            }

            NetMsg       message  = new NetMsg(0, buffer);
            TDataArray   datas    = message.GetBody <TDataArray>();
            List <TData> dataList = GetDataList(datas);
            int          count    = dataList.Count;

            data = new Dictionary <uint, TData>(count);
            for (int i = 0; i < count; ++i)
            {
                try
                {
                    data.Add(GetKey(dataList[i]), dataList[i]);
                }
                catch (System.Exception e)
                {
                    Debug.LogError(string.Format("Load file({0}) data add key{1} failed, error({2})", FileName, GetKey(dataList[i]), e.ToString()));
                }
            }

            buffer = null;
        }
        catch (System.Exception e)
        {
            Debug.LogError(string.Format("Load file({0}) failed, error({1})", FileName, e.ToString()));
        }
    }