void ReadBytes(FileInfo[] files) { foreach (FileInfo f in files) { if (f.Extension == ".bytes") { FileStream fs = new FileStream(f.FullName, FileMode.Open); BinaryReader br = new BinaryReader(fs); byte[] result = br.ReadBytes((int)fs.Length); br.Close(); fs.Close(); PtOutBinary outBinary = PtOutBinary.Read(result); ReadBinaryAndCreateXmlData(outBinary); } } UnityEngine.Debug.Log(string.Format("[{0}]", "Finish")); }
public static PtOutBinary Read(byte[] value) { //创建结构体 PtOutBinary info = new PtOutBinary(); //创建字节缓存 ByteBuffer bfs = new ByteBuffer(); bfs.source = value; //读取class_name if (bfs.ReadBool()) { info.class_name = bfs.ReadString(); } //读取field_count if (bfs.ReadBool()) { info.field_count = bfs.ReadInt32(); } //读取rows数组 if (bfs.ReadBool()) { int len = bfs.ReadInt32(); info.rows = new List <PtOutNode>(); for (int i = 0; i < len; ++i) { info.rows.Add(PtOutNode.Read(bfs.ReadBytes())); } } //返回 PtOutBinary 实例 return(info); }
public static byte[] Write(PtOutBinary value) { //创建字节缓存 ByteBuffer bfs = new ByteBuffer(); //写入class_name bfs.WriteBool(value.has_class_name); if (value.has_class_name) { bfs.WriteString(value.class_name); } //写入field_count bfs.WriteBool(value.has_field_count); if (value.has_field_count) { bfs.WriteInt32(value.field_count); } //写入rows数组 bfs.WriteBool(value.has_rows); if (value.has_rows) { int len = value.rows.Count; bfs.WriteInt32(len); for (int i = 0; i < len; ++i) { bfs.WriteBytes(PtOutNode.Write(value.rows[i])); } } //返回 字节数组 return(bfs.source); }
void ReadBinaryAndCreateXmlData(PtOutBinary outBinary) { LoadConfigDataUtil.TypePair tp = LoadConfigDataUtil.GetTypeByName(outBinary.class_name + "BinaryConfig"); object main = Activator.CreateInstance(tp.m_TypeMain); FieldInfo[] fies = tp.m_TypeMain.GetFields(); IList list = Activator.CreateInstance(fies[0].FieldType) as IList; fies[0].SetValue(main, list); for (int i = 2; i < outBinary.rows.Count; ++i) { object child = Activator.CreateInstance(tp.m_TypeChild); list.Add(child); for (int j = 0; j < outBinary.field_count; ++j) { string type = outBinary.rows[0].cols[j]; string name = outBinary.rows[1].cols[j]; string value = outBinary.rows[i].cols[j]; FieldInfo field = tp.m_TypeChild.GetField(name); if (field != null) { if (type == "int") { field.SetValue(child, value == "" ? 0 : int.Parse(value)); } else if (type == "string") { field.SetValue(child, value); } else if (type == "float") { field.SetValue(child, value == "" ? 0 : float.Parse(value)); } else { throw new Exception("Field Type Error"); } } } } MethodInfo mt_Write = tp.m_TypeMain.GetMethod("Write"); FileStream outFs = new FileStream(Environment.CurrentDirectory + string.Format("\\Assets\\AutoConfig\\Resources\\StaticConfigs\\{0}_Final.bytes", outBinary.class_name), FileMode.Create); BinaryWriter bw = new BinaryWriter(outFs); bw.Write(mt_Write.Invoke(null, new object[] { main }) as byte[]); bw.Flush(); bw.Close(); outFs.Close(); UnityEngine.Debug.Log(string.Format("[{0}]", outBinary.class_name + " OK")); AssetDatabase.Refresh(); }