Esempio n. 1
0
 public static MemoryStream ReadAsMemoryStream(string filePath)
 {
     try {
         byte[] buffer = ReadAsArray(filePath);
         if (buffer == null)
         {
             LogSystem.Error("Err ReadFileAsMemoryStream failed:{0}\n", filePath);
             return(null);
         }
         return(new MemoryStream(buffer));
     } catch (Exception e) {
         LogSystem.Error("Exception:{0}\n", e.Message);
         Helper.LogCallStack();
         return(null);
     }
 }
Esempio n. 2
0
        public void Save(string file)
        {
            try {
                if (m_Records.Count > 0)
                {
                    int recordSize = m_Records[0].Length;
                    int recordNum  = m_Records.Count;
                    for (int i = 1; i < recordNum; ++i)
                    {
                        if (m_Records[i].Length != recordSize)
                        {
                            LogSystem.Error("Record Size not equal, {0}!={1}({2})", recordSize, m_Records[i].Length, i);
                            return;
                        }
                    }
                    using (FileStream fs = new FileStream(file, FileMode.Create, FileAccess.Write, FileShare.None)) {
                        FileHeader header = new FileHeader(recordNum, recordSize);
                        WriteFileHeader(fs, header);

                        for (int i = 0; i < recordNum; ++i)
                        {
                            fs.Write(m_Records[i], 0, recordSize);
                        }

                        header.m_StringOffset = (int)fs.Position;
                        WriteStrArray(fs, m_StringList.ToArray());

                        header.m_IntListOffset = (int)fs.Position;
                        WriteIntArray(fs, m_IntLists.ToArray());

                        header.m_FloatListOffset = (int)fs.Position;
                        WriteFloatArray(fs, m_FloatLists.ToArray());

                        header.m_StrListOffset = (int)fs.Position;
                        WriteIntArray(fs, m_StrLists.ToArray());

                        //重写文件头
                        fs.Position = 0;
                        WriteFileHeader(fs, header);

                        fs.Close();
                    }
                }
            } catch (Exception ex) {
                LogSystem.Error("Exception:{0}\n{1}", ex.Message, ex.StackTrace);
            }
        }
Esempio n. 3
0
        /*****************************************************************************************************
        *  //----------------------------------------------------------------------------------------------------
        *****************************************************************************************************/
        public static bool IsValid(string file)
        {
            bool ret = false;

            try {
                using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read)) {
                    FileHeader header = ReadFileHeader(fs);
                    if (header.m_Identity == c_Identity && header.m_Version == c_Version)
                    {
                        ret = true;
                    }
                    fs.Close();
                }
            } catch (Exception ex) {
                LogSystem.Error("Exception:{0}\n{1}", ex.Message, ex.StackTrace);
            }
            return(ret);
        }
Esempio n. 4
0
 public static byte[] ReadAsArray(string filePath)
 {
     byte[] buffer = null;
     try {
         if (OnReadAsArray != null)
         {
             buffer = OnReadAsArray(filePath);
         }
         else
         {
             LogSystem.Error("ReadFileByEngine handler have not register: {0}", filePath);
         }
     } catch (Exception e) {
         LogSystem.Error("Exception:{0}\n", e.Message);
         Helper.LogCallStack();
         return(null);
     }
     return(buffer);
 }
Esempio n. 5
0
        public bool LoadFromBinary(string file)
        {
            long        t1     = TimeUtility.GetElapsedTimeUs();
            bool        result = true;
            BinaryTable table  = new BinaryTable();

            table.Load(HomePath.GetAbsolutePath(file));
            long t2 = TimeUtility.GetElapsedTimeUs();

            long t3 = TimeUtility.GetElapsedTimeUs();

            for (int index = 0; index < table.Records.Count; ++index)
            {
                try {
                    TData data = new TData();
                    bool  ret  = data.ReadFromBinary(table, index);
                    if (ret && !m_DataContainer.ContainsKey(data.GetId()))
                    {
                        m_DataContainer.Add(data.GetId(), data);
                    }
                    else
                    {
                        string info = string.Format("DataTempalteMgr.CollectDataFromBinary collectData Row:{0} failed in {1}!", index, file);
                        LogSystem.Error(info);
                        Helper.LogCallStack(true);
                        result = false;
                    }
                } catch (System.Exception ex) {
                    LogSystem.Error("CollectData failed. file:{0} rowIndex:{1}\nException:{2}\n{3}", file, index, ex.Message, ex.StackTrace);
                }
            }
            long t4 = TimeUtility.GetElapsedTimeUs();

            LogSystem.Info("binary load {0} parse {1}, file {2}", t2 - t1, t4 - t3, file);
            return(result);
        }
Esempio n. 6
0
        public static bool Convert(string file, string outFile, Encoding encoding)
        {
            try {
                string[] lines = File.ReadAllLines(file, encoding);
                if (lines.Length >= 2)
                {
                    string[] types  = lines[0].Split('\t');
                    string[] fields = lines[1].Split('\t');

                    string dirName  = Path.GetDirectoryName(file);
                    string fileName = Path.GetFileName(file);
                    string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(file);

                    int fieldCount       = 0;
                    int excelColumnCount = types.Length;
                    if (fields.Length != excelColumnCount)
                    {
                        LogSystem.Error("[line:2] “{0}” field count != {1}", lines[1], excelColumnCount);
                        return(false);
                    }
                    for (int ix = 0; ix < excelColumnCount; ++ix)
                    {
                        if (string.IsNullOrEmpty(types[ix]) || string.IsNullOrEmpty(fields[ix]))
                        {
                            continue;
                        }
                        ++fieldCount;
                    }
                    BinaryTable table = new BinaryTable();
                    for (int rowIndex = 2; rowIndex < lines.Length; ++rowIndex)
                    {
                        if (lines[rowIndex].StartsWith("#") || lines[rowIndex].StartsWith("//"))
                        {
                            continue;
                        }
                        int      colIndex    = 0;
                        string[] fieldValues = lines[rowIndex].Split('\t');
                        if (fieldValues.Length != excelColumnCount)
                        {
                            LogSystem.Error("[line:{0}] “{1}” field count != {2}", rowIndex + 1, lines[rowIndex], excelColumnCount);
                            continue;
                        }
                        byte[] record = new byte[fieldCount * sizeof(int)];
                        table.Records.Add(record);
                        for (int ix = 0; ix < excelColumnCount; ++ix)
                        {
                            if (string.IsNullOrEmpty(fields[ix]) || string.IsNullOrEmpty(types[ix]))
                            {
                                continue;
                            }
                            string type = types[ix].Trim();
                            string val  = fieldValues[ix].Trim();
                            try {
                                if (0 == type.CompareTo("int") || 0 == type.CompareTo("int32") || 0 == type.CompareTo("long") || 0 == type.CompareTo("int64"))
                                {
                                    int v = 0;
                                    if (!string.IsNullOrEmpty(val))
                                    {
                                        v = int.Parse(val);
                                    }
                                    WriteIndex(record, colIndex, v);
                                }
                                else if (0 == type.CompareTo("float"))
                                {
                                    float v = 0;
                                    if (!string.IsNullOrEmpty(val))
                                    {
                                        v = float.Parse(val);
                                    }
                                    WriteFloat(record, colIndex, v);
                                }
                                else if (0 == type.CompareTo("bool"))
                                {
                                    bool v = false;
                                    if (!string.IsNullOrEmpty(val))
                                    {
                                        v = (val == "true" || val == "1");
                                    }
                                    WriteIndex(record, colIndex, v ? 1 : 0);
                                }
                                else if (0 == type.CompareTo("string"))
                                {
                                    int index = table.AddString(val);
                                    WriteIndex(record, colIndex, index);
                                }
                                else if (0 == type.CompareTo("int[]") || 0 == type.CompareTo("int32[]") || 0 == type.CompareTo("long[]") || 0 == type.CompareTo("int64[]"))
                                {
                                    int index = -1;
                                    if (!string.IsNullOrEmpty(val))
                                    {
                                        string[] v    = val.Split(',', ';', '|', ' ');
                                        int[]    vals = new int[v.Length];
                                        for (int i = 0; i < v.Length; ++i)
                                        {
                                            vals[i] = int.Parse(v[i]);
                                        }
                                        index = table.AddIntList(vals);
                                    }
                                    WriteIndex(record, colIndex, index);
                                }
                                else if (0 == type.CompareTo("float[]"))
                                {
                                    int index = -1;
                                    if (!string.IsNullOrEmpty(val))
                                    {
                                        string[] v    = val.Split(',', ';', '|', ' ');
                                        float[]  vals = new float[v.Length];
                                        for (int i = 0; i < v.Length; ++i)
                                        {
                                            vals[i] = float.Parse(v[i]);
                                        }
                                        index = table.AddFloatList(vals);
                                    }
                                    WriteIndex(record, colIndex, index);
                                }
                                else if (0 == type.CompareTo("bool[]"))
                                {
                                    int index = -1;
                                    if (!string.IsNullOrEmpty(val))
                                    {
                                        string[] v    = val.Split(',', ';', '|', ' ');
                                        int[]    vals = new int[v.Length];
                                        for (int i = 0; i < v.Length; ++i)
                                        {
                                            vals[i] = (v[i] == "true" || v[i] == "1") ? 1 : 0;
                                        }
                                        index = table.AddIntList(vals);
                                    }
                                    WriteIndex(record, colIndex, index);
                                }
                                else if (0 == type.CompareTo("string[]"))
                                {
                                    int index = -1;
                                    if (!string.IsNullOrEmpty(val))
                                    {
                                        string[] vals = val.Split(',', ';', '|', ' ');
                                        index = table.AddStrList(vals);
                                    }
                                    WriteIndex(record, colIndex, index);
                                }
                            } catch (Exception ex) {
                                LogSystem.Error("[line:{0} col:{1}] “{2}”, exception:{3}\n{4}", rowIndex + 1, colIndex + 1, lines[rowIndex], ex.Message, ex.StackTrace);
                            }
                            ++colIndex;
                        }
                    }
                    table.Save(outFile);
                }
                return(true);
            } catch (Exception ex) {
                LogSystem.Error("exception:{0}\n{1}", ex.Message, ex.StackTrace);
                return(false);
            }
        }