public LevelTerrain LoadTerrainFile(System.IO.Stream stream)
        {
            System.IO.BinaryReader br = new System.IO.BinaryReader(stream);

            byte[] fileBuffer = new byte[stream.Length];
            br.Read(fileBuffer, 0, (int)stream.Length);

            LevelTerrain terrain = new LevelTerrain();
            Scale scales = new Scale();
            scales.Scales = new Vector3(10.0f, 1.0f, 10.0f);
            terrain.Scaling = scales;

            // assume square image
            int dim = (int)Math.Sqrt(stream.Length);
            terrain.HeightMapDimensions = new Point(dim, dim);
            terrain.HeightMap = fileBuffer;

            String name = "TERRAIN" + GetNextTerrainIdx();
            terrain.Name = name;
            return terrain;
        }
        public void LoadLevelFile(System.IO.Stream stream)
        {
            System.IO.BinaryReader br = new System.IO.BinaryReader(stream);

            /* Get Current App Version For Comparison */
            String currentVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();

            /* Check Loading File's Version To Current */
            String fileVersion = br.ReadString();
            if (fileVersion != currentVersion)
            {
                /* ERROR: File Version Out of Date */
                throw new IncorrectFileVersionException();
            }

            /* Load the Level Attributes */
            levelName = br.ReadString();

            /* Load the Object Type Palette */
            List<FileOps.LevelObjectTypeDefinition> types = FileOps.ReadObjectPalette(br);

            /* Level Terrain */
            int numTerrainChunks = br.ReadInt32();
            for (int i = 0; i < numTerrainChunks; i++)
            {
                LevelTerrain terrain = new LevelTerrain();
                terrain.Name = br.ReadString();
                terrain.Position = FileOps.ReadVec3(br);

                Scale scale = new Scale();
                scale.scales = FileOps.ReadVec3(br);
                terrain.Scaling = scale;

                Point pt = new Point();
                pt.X = br.ReadInt32();
                pt.Y = br.ReadInt32();
                terrain.HeightMapDimensions = pt;

                int numSamples = terrain.HeightMapDimensions.X * terrain.HeightMapDimensions.Y;
                byte[] heightMap = new byte[numSamples];
                br.Read(heightMap, 0, numSamples);
                terrain.HeightMap = heightMap;

                terrainObjects.Add(terrain.Name, terrain);
            }

            /* Load Level Object Instances */
            int levelCnt = br.ReadInt32();
            System.Collections.Generic.List<LevelObject> lvlObjs = new List<LevelObject>();

            for (int i = 0; i < levelCnt; i++)
            {
                LevelObject lo = new LevelObject();

                /* Save Object Instance Attributes */
                lo.Index = br.ReadInt32();
                lo.Name = br.ReadString();
                lo.TypeId = new Guid(br.ReadString());

                /* Object Instance Transforms */
                lo.Rotation = FileOps.ReadVec3(br);
                lo.Scale = FileOps.ReadVec3(br);
                lo.Position = FileOps.ReadVec3(br);

                lvlObjs.Add(lo);

                m_nextUnusedObID = Math.Max(lo.Index, m_nextUnusedObID);
                m_nextUnusedObID++;
            }

            br.Close();

            /* Load the Level Object Types Definitions From
             * Files For Each Type in Level's Object Palette */
            foreach (FileOps.LevelObjectTypeDefinition t in types)
            {
                /* Try to Open the Type's File */
                String typeFilename = levelFileName;
                int idx = typeFilename.LastIndexOf('\\')+1;
                typeFilename = typeFilename.Substring(0, idx);
                typeFilename += t.FileName;

                System.IO.FileStream typeFile = null;

                try
                {
                    typeFile = System.IO.File.Open(typeFilename, System.IO.FileMode.Open);

                    /* Fill Out a New Object Type from The File Definition */
                    LevelObjectType lot = LoadTypeFile(typeFile);

                    /* Verify the Type Matches the Guid From the Level Type Palette */
                    if (lot.TypeID == t.TypeId)
                    {
                        this.objectPalette.Add(lot.Name, lot);
                    }
                    else
                    {
                        System.Diagnostics.Debug.Assert(true);
                    }

                    /* Increment the Type ID counter */
                    m_nextUnusedTypeID = Math.Max(lot.Index, m_nextUnusedTypeID);
                    m_nextUnusedTypeID++;
                }
                finally
                {
                    typeFile.Close();
                }
            }

            /* Add the object to the level linked to their type */
            foreach (LevelObject lo in lvlObjs)
            {
                AddObjectToLvl(lo, false);
            }
        }