コード例 #1
0
ファイル: WLD.cs プロジェクト: Unchated/pogee-3d-editor
        public static WLD Load(Stream stream,string name="default.wld")
        {
            WLD wld = new WLD();
            wld.Name = name;
            int size = Marshal.SizeOf(typeof(WLDHeader));
            var barray = new byte[size];
            stream.Read(barray, 0, size);

            var header = Functions.ByteArrayToStructure<WLDHeader>(barray);

            if (header.Magic != 0x54503d02)
            {
                throw new Exception("Invalid file format");
            }

            if (header.Version == 0x00015500)
            {
                wld._format = Format.Old;
            }
            else if (header.Version == 0x1000C800)
            {
                wld._format = Format.New;
            }
            else
            {
                throw new Exception("Unknown file version");
            }

            //var shash = stream.Position;
            barray = new byte[header.StringHashSize];
            stream.Read(barray, 0, (int)header.StringHashSize);
            wld._sHash = WLD.DecodeFileName(barray);
            wld._strings = wld._sHash.Split('\0');

            var fragCount = header.FragmentCount;

            stream.Seek(size + header.StringHashSize, SeekOrigin.Begin);

            int fragSize = Marshal.SizeOf(typeof(BasicWLDFragment));

            for (int i = 0; i < header.FragmentCount; i++)
            {
                barray = new byte[fragSize];
                stream.Read(barray, 0, fragSize);
                var fragment = Functions.ByteArrayToStructure<BasicWLDFragment>(barray);
                int nameRef = (int)fragment.NameRef;

                var position = stream.Position;

                switch (fragment.Id)
                {
                    case 0x03:
                        var bmpname = new BitmapName(i, nameRef);
                        bmpname.Handler(stream);
                        wld._fragments.Add(bmpname);
                        break;
                    case 0x04:
                        var binfo = new BitmapInfo(i, nameRef);
                        binfo.Handler(stream);
                        wld._fragments.Add(binfo);
                        break;
                    case 0x05:
                        var bitmapInfoRef = new BitmapInfoReference(i, nameRef);
                        bitmapInfoRef.Handler(stream);
                        wld._fragments.Add(bitmapInfoRef);
                        break;
                    case 0x09:
                        break;
                    case 0x10:
                        var skelset = new SkeletonTrackSet(i, nameRef);
                        skelset.Handler(stream);
                        skelset.FragmentName = wld.GetStringAtHashIndex(0-skelset.FragmentNameRef);
                        wld._fragments.Add(skelset);
                        break;
                    case 0x11:
                        var skeltrackRef = new SkeletonTrackReference(i, nameRef);
                        skeltrackRef.Handler(stream);
                        wld._fragments.Add(skeltrackRef);
                        break;
                    case 0x12:
                        var skelpiece = new SkeletonPieceTrack(i, nameRef);
                        skelpiece.Handler(stream);
                        wld._fragments.Add(skelpiece);
                        break;
                    case 0x13:
                        var skelpref = new SkeletonPieceTrackReference(i, nameRef);
                        skelpref.Handler(stream);
                        //skelpref.FragmentName = wld.GetStringAtHashIndex(0 - skelpref.FragmentNameRef);
                        wld._fragments.Add(skelpref);
                        break;
                    case 0x14:
                        var modelref = new ModelReference(i, nameRef);
                        modelref.Handler(stream);
                        modelref.FragmentName = wld.GetStringAtHashIndex(0 - modelref.FragmentNameRef);
                        wld._fragments.Add(modelref);
                        modelref.MagicString = wld.GetStringAtHashIndex(modelref.MagicStringRef);
                        break;
                    case 0x15:
                        var objlocation = new ObjectLocation(i, nameRef);
                        objlocation.Handler(stream);
                        wld._fragments.Add(objlocation);
                        break;
                    case 0x22:
                        //num_0x22++;
                        break;
                    case 0x2d:
                        var meshref = new MeshReference(i, nameRef);
                        meshref.Handler(stream);
                        wld._fragments.Add(meshref);
                        break;
                    case 0x31:
                        var tlist = new TextureList(i, nameRef);
                        tlist.Handler(stream);
                        wld._fragments.Add(tlist);
                        break;
                    case 0x30:
                        var texture = new Texture(i, nameRef);
                        texture.Handler(stream);
                        wld._fragments.Add(texture);
                        break;
                    // Grab the number of vertices and polygons
                    case 0x36:
                        var mesh = new Mesh(i, nameRef);
                        mesh.Handler(stream);
                        wld._fragments.Add(mesh);
                        break;
                }
                stream.Seek(position + fragment.Size - 4, SeekOrigin.Begin);
            }

            return wld;
        }