下面的代码是用来读取某种文件的格式的类:
using System;
using System.IO;
using System.Text;
using System.Collections;namespace DYO_Reader
{
    public class Dyo
    {
        public enum ObjectType
        {
            End = -1,
            Object = 0,
            CommonControl = 2,
            Item = 4,
            Mover = 5,
            Ship = 6
        }        public enum AIType
        {
            Monster = 2,
            ClockWorks = 4,
            Pet = 5,
            BigMuscle = 6,
            Krrr = 7,
            Bear = 8,
            Monster2 = 100,
            Meteonyker = 10
        }

        public struct  Vector3
{
         public Vector3(float x,float y,float z)
         {
         this.x=x;
         this.y=y;
         this.z=z;
         }        
public float x;
public float y;
public float z;
}
        
public struct FileFormat
{
public ObjectType type;
public float y_Rotation;
public Vector3 axis_Rotation;
public Vector3 position;
public Vector3 scale;
public bool set_AI;
public int object_ID;
public int unknown0;
public AIType aI_Type;
public int unknown1;
public String name;
public int unknown2;
public  int unknown3;
} public  static FileFormat rd=new FileFormat();
        public  static ArrayList array=new ArrayList();        public void readFile(string FILE_NAME)
        { 
            BinaryReader fh = new BinaryReader(File.OpenRead(FILE_NAME));
            while (true)
            {
                ObjectType objectType = (ObjectType)fh.ReadInt32();                if(objectType == ObjectType.End)
                    break;                rd.type=objectType;

                switch (objectType)
                {
                    case ObjectType.Object:
                    case ObjectType.Item:
                    case ObjectType.Ship:
                        ReadObject(fh);
                        break;
                    case ObjectType.CommonControl:
                        {
                            ReadObject(fh);                            uint unknownCondition = fh.ReadUInt32();                            switch (unknownCondition)
                            {
                                case 0x80000000:
                                    fh.ReadBytes(432);
                                    break;
                                case 0x90000000:
                                    {
                                        fh.ReadBytes(88);
                                        fh.ReadBytes(280);
                                    }
                                    break;
                                default:
                                    fh.ReadBytes(392);
                                    break;
                            }
                        }
                        break;
                    case ObjectType.Mover:
                        {
                            ReadObject(fh);                            fh.ReadBytes(64);
                            fh.ReadBytes(32);                            string moverName = Encoding.Default.GetString(fh.ReadBytes(32));
                            moverName = moverName.Substring(0, moverName.IndexOf('\0'));

rd.name=moverName;
                            rd.unknown0=fh.ReadInt32();
                            rd.unknown1=fh.ReadInt32();
                        }
                        break;
                    default:
                        {
                            Console.WriteLine("Invalid Type");                            goto exit;
                        }
                }
            }
        exit:
            fh.Close();         }        public void ReadObject(BinaryReader fh)
        {
            rd.y_Rotation=fh.ReadSingle();            
rd.axis_Rotation=new Vector3(fh.ReadSingle(), fh.ReadSingle(), fh.ReadSingle());
rd.position=new Vector3(fh.ReadSingle(), fh.ReadSingle(), fh.ReadSingle());
rd.scale=new Vector3(fh.ReadSingle(), fh.ReadSingle(), fh.ReadSingle());
rd.set_AI=(fh.ReadInt32() == 5);
rd.object_ID=fh.ReadInt32();
rd.unknown2=fh.ReadInt32();
rd.aI_Type=(AIType)fh.ReadInt32();
rd.unknown3=fh.ReadInt32();
array.Add(rd);
        } 
    }
}
这是从网上找来的打开该文件的方法但是并没有提供保存的方法
现在我想倒推出保存这种文件格式的方法
不知道怎么搞
搞了一天了
倒推出来的格式还是错误~~
请教一下各位~~
先谢了

解决方案 »

  1.   

    public void WriteObject(BinaryWriter fh)
    {
    fh.WriteSingle(rd.rd.y_Rotation);
    fh.WriteSingle(rd.axis_Rotation.x);
    fh.WriteSingle(rd.axis_Rotation.y);
    fh.WriteSingle(rd.axis_Rotation.z);
    fh.WriteSingle(rd.position.x);
    fh.WriteSingle(rd.position.y);
    fh.WriteSingle(rd.position.z);
    fh.WriteSingle(rd.scale.x);
    fh.WriteSingle(rd.scale.y);
    fh.WriteSingle(rd.scale.z);
    fs.WriteInt32(rd.set_AI?0:5);
    fs.WriteInt32(rd.object_ID);
    fs.WriteInt32(rd.unknown2);
    fs.WriteInt32((int)rd.aI_Type);
    fs.WriteInt32(rd.unknown3);  
    }
      

  2.   

    BinaryWriter 不包含WriteSingle方法的哦
      

  3.   

    public void WriteObject(BinaryWriter fh)
    {
        fh.Write(rd.rd.y_Rotation);
        fh.Write(rd.axis_Rotation.x);
        fh.Write(rd.axis_Rotation.y);
        fh.Write(rd.axis_Rotation.z);
        fh.Write(rd.position.x);
        fh.Write(rd.position.y);
        fh.Write(rd.position.z);
        fh.Write(rd.scale.x);
        fh.Write(rd.scale.y);
        fh.Write(rd.scale.z);
        fs.Write(rd.set_AI?0:5);
        fs.Write(rd.object_ID);
        fs.Write(rd.unknown2);
        fs.Write((int)rd.aI_Type);
        fs.Write(rd.unknown3);     
    }
      

  4.   


            private byte[] WriteStruct<T>(T _struct) {
                int size = Marshal.SizeOf(_struct);
                byte[] buffer = new byte[size];
                IntPtr ptr = Marshal.AllocHGlobal(size);
                Marshal.StructureToPtr(_struct, ptr, true);
                Marshal.Copy(ptr, buffer, 0, size);
                Marshal.FreeHGlobal(ptr);
                return buffer;
            }把结构转换成字节数组, 用流写数组到文件.
      

  5.   

    测试文件在:
    http://e.ys168.com/?zerozone
    WdArena.dyo