void IMyOctreeLeafNode.ReplaceValues(Dictionary <byte, byte> oldToNewValueMap)
 {
     m_octree.ReplaceValues(oldToNewValueMap);
 }
예제 #2
0
        protected override void LoadInternal(int fileVersion, Stream stream, ref bool isOldFormat)
        {
            Debug.Assert(fileVersion == CURRENT_FILE_VERSION);

            ChunkHeader header = new ChunkHeader();
            Dictionary <byte, MyVoxelMaterialDefinition> materialTable = null;
            HashSet <UInt64> materialLeaves = new HashSet <UInt64>();
            HashSet <UInt64> contentLeaves  = new HashSet <UInt64>();

            while (header.ChunkType != ChunkTypeEnum.EndOfFile)
            {
                MyMicroOctreeLeaf contentLeaf;
                MyMicroOctreeLeaf materialLeaf;
                UInt64            key;

                header.ReadFrom(stream);
                Debug.Assert(Enum.IsDefined(typeof(ChunkTypeEnum), header.ChunkType));

                switch (header.ChunkType)
                {
                case ChunkTypeEnum.StorageMetaData:
                    ReadStorageMetaData(stream, header, ref isOldFormat);
                    break;

                case ChunkTypeEnum.MaterialIndexTable:
                    materialTable = ReadMaterialTable(stream, header, ref isOldFormat);
                    break;

                case ChunkTypeEnum.MacroContentNodes:
                    ReadOctreeNodes(stream, header, ref isOldFormat, m_contentNodes);
                    break;

                case ChunkTypeEnum.MacroMaterialNodes:
                    ReadOctreeNodes(stream, header, ref isOldFormat, m_materialNodes);
                    break;

                case ChunkTypeEnum.ContentLeafProvider:
                    ReadProviderLeaf(stream, header, ref isOldFormat, contentLeaves);
                    break;

                case ChunkTypeEnum.ContentLeafOctree:
                    ReadOctreeLeaf(stream, header, ref isOldFormat, MyStorageDataTypeEnum.Content, out key, out contentLeaf);
                    m_contentLeaves.Add(key, contentLeaf);
                    break;

                case ChunkTypeEnum.MaterialLeafProvider:
                    ReadProviderLeaf(stream, header, ref isOldFormat, materialLeaves);
                    break;

                case ChunkTypeEnum.MaterialLeafOctree:
                    ReadOctreeLeaf(stream, header, ref isOldFormat, MyStorageDataTypeEnum.Material, out key, out materialLeaf);
                    m_materialLeaves.Add(key, materialLeaf);
                    break;

                case ChunkTypeEnum.DataProvider:
                    ReadDataProvider(stream, header, ref isOldFormat, out m_dataProvider);
                    break;

                case ChunkTypeEnum.EndOfFile:
                    break;

                default:
                    throw new InvalidBranchException();
                }
            }

            { // At this point data provider should be loaded too, so have him create leaves
                MyCellCoord cell = new MyCellCoord();
                foreach (var key in contentLeaves)
                {
                    cell.SetUnpack(key);
                    cell.Lod += LeafLodCount;
                    m_contentLeaves.Add(key, new MyProviderLeaf(m_dataProvider, MyStorageDataTypeEnum.Content, ref cell));
                }

                foreach (var key in materialLeaves)
                {
                    cell.SetUnpack(key);
                    cell.Lod += LeafLodCount;
                    m_materialLeaves.Add(key, new MyProviderLeaf(m_dataProvider, MyStorageDataTypeEnum.Material, ref cell));
                }
            }

            { // material reindexing when definitions change
                Debug.Assert(materialTable != null);
                bool needsReindexing = false;
                foreach (var entry in materialTable)
                {
                    if (entry.Key != entry.Value.Index)
                    {
                        needsReindexing = true;
                    }
                    m_oldToNewIndexMap.Add(entry.Key, entry.Value.Index);
                }

                if (needsReindexing)
                {
                    if (m_dataProvider != null)
                    {
                        m_dataProvider.ReindexMaterials(m_oldToNewIndexMap);
                    }

                    foreach (var entry in m_materialLeaves)
                    {
                        entry.Value.ReplaceValues(m_oldToNewIndexMap);
                    }

                    MySparseOctree.ReplaceValues(m_materialNodes, m_oldToNewIndexMap);
                }
                m_oldToNewIndexMap.Clear();
            }
        }