SEToolbox.Interop.Asteroids.MyVoxelMap.SaveV2 C# (CSharp) Method

SaveV2() private method

private SaveV2 ( BinaryWriter writer, bool saveMaterialContent ) : void
writer System.IO.BinaryWriter
saveMaterialContent bool
return void
        private void SaveV2(BinaryWriter writer, bool saveMaterialContent)
        {
            //  Size of this voxel map (in voxels)
            writer.Write(Size.X);
            writer.Write(Size.Y);
            writer.Write(Size.Z);

            //  Size of data cell in voxels, doesn't have to be same as current size specified by our constants.
            writer.Write(MyVoxelConstants.VOXEL_DATA_CELL_SIZE_IN_VOXELS);
            writer.Write(MyVoxelConstants.VOXEL_DATA_CELL_SIZE_IN_VOXELS);
            writer.Write(MyVoxelConstants.VOXEL_DATA_CELL_SIZE_IN_VOXELS);

            var materials = SpaceEngineersCore.Resources.VoxelMaterialDefinitions;
            writer.Write((byte)materials.Count);
            for (byte idx = 0; idx < materials.Count; idx++)
            {
                writer.Write(idx);
                writer.Write(materials[idx].Id.SubtypeName);
            }

            Vector3I cellCoord;
            for (cellCoord.X = 0; cellCoord.X < _dataCellsCount.X; cellCoord.X++)
            {
                for (cellCoord.Y = 0; cellCoord.Y < _dataCellsCount.Y; cellCoord.Y++)
                {
                    for (cellCoord.Z = 0; cellCoord.Z < _dataCellsCount.Z; cellCoord.Z++)
                    {
                        var voxelCell = _voxelContentCells[cellCoord.X][cellCoord.Y][cellCoord.Z];

                        if (voxelCell == null)
                        {
                            //  Voxel wasn't found in cell dictionary, so cell must be full
                            writer.Write((byte)MyVoxelCellType.FULL);
                        }
                        else
                        {
                            //  Cell type
                            writer.Write((byte)voxelCell.CellType);

                            //  If we are here, cell is empty or mixed. If empty, we don't need to save each individual voxel.
                            //  But if it is mixed, we will do it here.
                            if (voxelCell.CellType == MyVoxelCellType.MIXED)
                            {
                                Vector3I voxelCoordInCell;
                                for (voxelCoordInCell.X = 0; voxelCoordInCell.X < MyVoxelConstants.VOXEL_DATA_CELL_SIZE_IN_VOXELS; voxelCoordInCell.X++)
                                {
                                    for (voxelCoordInCell.Y = 0; voxelCoordInCell.Y < MyVoxelConstants.VOXEL_DATA_CELL_SIZE_IN_VOXELS; voxelCoordInCell.Y++)
                                    {
                                        for (voxelCoordInCell.Z = 0; voxelCoordInCell.Z < MyVoxelConstants.VOXEL_DATA_CELL_SIZE_IN_VOXELS; voxelCoordInCell.Z++)
                                        {
                                            writer.Write(voxelCell.GetVoxelContent(ref voxelCoordInCell));
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            if (saveMaterialContent)
            {
                // Save material cells
                for (cellCoord.X = 0; cellCoord.X < _dataCellsCount.X; cellCoord.X++)
                {
                    for (cellCoord.Y = 0; cellCoord.Y < _dataCellsCount.Y; cellCoord.Y++)
                    {
                        for (cellCoord.Z = 0; cellCoord.Z < _dataCellsCount.Z; cellCoord.Z++)
                        {
                            var matCell = _voxelMaterialCells[cellCoord.X][cellCoord.Y][cellCoord.Z];
                            var voxelCoordInCell = new Vector3I(0, 0, 0);
                            var isWholeMaterial = matCell.IsSingleMaterialForWholeCell;
                            writer.Write((byte)(isWholeMaterial ? 0x01 : 0x00));
                            if (isWholeMaterial)
                            {
                                //writer.Write(matCell.GetIndestructibleContent(ref voxelCoordInCell));
                                //writer.Write(SpaceEngineersCore.Resources.GetMaterialName(matCell.GetMaterial(ref voxelCoordInCell), VoxelMaterial));
                                writer.Write(matCell.GetMaterial(ref voxelCoordInCell));
                            }
                            else
                            {
                                for (voxelCoordInCell.X = 0; voxelCoordInCell.X < MyVoxelConstants.VOXEL_DATA_CELL_SIZE_IN_VOXELS; voxelCoordInCell.X++)
                                {
                                    for (voxelCoordInCell.Y = 0; voxelCoordInCell.Y < MyVoxelConstants.VOXEL_DATA_CELL_SIZE_IN_VOXELS; voxelCoordInCell.Y++)
                                    {
                                        for (voxelCoordInCell.Z = 0; voxelCoordInCell.Z < MyVoxelConstants.VOXEL_DATA_CELL_SIZE_IN_VOXELS; voxelCoordInCell.Z++)
                                        {
                                            //writer.Write(matCell.GetIndestructibleContent(ref voxelCoordInCell));
                                            //writer.Write(SpaceEngineersCore.Resources.GetMaterialName(matCell.GetMaterial(ref voxelCoordInCell), VoxelMaterial));
                                            writer.Write(matCell.GetMaterial(ref voxelCoordInCell));
                                            writer.Write((byte)0x0);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }