예제 #1
0
    /// <summary>
    /// 反序列化.
    /// </summary>
    public static CsvFile Deserialize(MemoryStream stream)
    {
        int ver = FileMgr.ReadInt(stream);

        if (ver != CsvFileMgr.Version)
        {
            NIDebug.Log("Csv版本 {0} 错误, 最新版本 {1}", ver, CsvFileMgr.Version);
            return(null);
        }

        // 文件名
        string name = FileMgr.ReadString(stream);

        CsvFile csv = new CsvFile(name);

        csv.primaryKey = FileMgr.ReadString(stream);

        // 主key类型
        var pkeyType = (LPC.LPCValue.ValueType)FileMgr.ReadInt(stream);

        // 列名
        int n = FileMgr.ReadInt(stream);

        csv.columns = new Dictionary <string, int>(n);
        for (int i = 0; i < n; i++)
        {
            string k = FileMgr.ReadString(stream);
            int    v = FileMgr.ReadInt(stream);
            csv.columns.Add(k, v);
        }

        // 行数
        n        = FileMgr.ReadInt(stream);
        csv.rows = new CsvRow[n];

        // 主key的列
        int pkeyIdx = csv.columns[csv.primaryKey];

        // 每行主key
        for (var i = 0; i < n; i++)
        {
            var row = new CsvRow(csv);
            if (pkeyType == LPCValue.ValueType.INT)
            {
                int pkey = FileMgr.ReadInt(stream);
                row.Add(pkeyIdx, LPCValue.Create(pkey));
            }
            else
            {
                string pkey = FileMgr.ReadString(stream);
                row.Add(pkeyIdx, LPCValue.Create(pkey));
            }
            csv.AddRow(i, row);
        }

        // 行数据
        for (int i = 0; i < n; i++)
        {
            int len = FileMgr.ReadInt(stream);
            csv.rows[i].rowData = new byte[len];
            stream.Read(csv.rows[i].rowData, 0, len);
        }

        return(csv);
    }