Exemplo n.º 1
0
        public static void SaveSchematic(Colony colony, Schematic schematic)
        {
            var colonySaves = GameLoader.Schematic_SAVE_LOC + $"\\{colony.ColonyID}\\";

            if (!Directory.Exists(colonySaves))
            {
                Directory.CreateDirectory(colonySaves);
            }

            List <NbtTag> tags = new List <NbtTag>();

            tags.Add(new NbtInt("Width", schematic.XMax));
            tags.Add(new NbtInt("Height", schematic.YMax));
            tags.Add(new NbtInt("Length", schematic.ZMax));

            List <NbtTag> blocks = new List <NbtTag>();

            for (int Y = 0; Y <= schematic.YMax; Y++)
            {
                for (int Z = 0; Z <= schematic.ZMax; Z++)
                {
                    for (int X = 0; X <= schematic.XMax; X++)
                    {
                        NbtCompound compTag = new NbtCompound();
                        compTag.Add(new NbtInt("x", X));
                        compTag.Add(new NbtInt("y", Y));
                        compTag.Add(new NbtInt("z", Z));
                        compTag.Add(new NbtString("id", schematic.Blocks[X, Y, Z].BlockID));
                        blocks.Add(compTag);
                    }
                }
            }

            NbtList nbtList = new NbtList("CSBlocks", blocks);

            tags.Add(nbtList);

            NbtFile nbtFile      = new NbtFile(new NbtCompound("CompoundTag", tags));
            var     fileSave     = Path.Combine(colonySaves, schematic.Name + ".schematic");
            var     metaDataSave = Path.Combine(GameLoader.Schematic_SAVE_LOC, schematic.Name + ".schematic.metadata.json");

            if (File.Exists(fileSave))
            {
                File.Delete(fileSave);
            }

            if (File.Exists(metaDataSave))
            {
                File.Delete(metaDataSave);
            }

            GenerateMetaData(metaDataSave, schematic.Name, schematic);
            nbtFile.SaveToFile(fileSave, NbtCompression.GZip);
        }
Exemplo n.º 2
0
        public static Schematic LoadSchematic(NbtFile nbtFile, Vector3Int startPos)
        {
            RawSchematic raw = LoadRaw(nbtFile);

            SchematicBlock[,,] blocks = default(SchematicBlock[, , ]);

            if (raw.CSBlocks != null && raw.CSBlocks.Length > 0)
            {
                blocks = raw.CSBlocks;
            }
            else
            {
                blocks = GetBlocks(raw);
            }

            string    name      = Path.GetFileNameWithoutExtension(nbtFile.FileName);
            Schematic schematic = new Schematic(name, raw.XMax, raw.YMax, raw.ZMax, blocks, startPos);

            return(schematic);
        }
Exemplo n.º 3
0
        public static SchematicMetadata GenerateMetaData(string name, int colonyId)
        {
            if (TryGetScematicFilePath(name, colonyId, out string path))
            {
                var colonySaves = GameLoader.Schematic_SAVE_LOC + $"\\{colonyId}\\";

                if (!Directory.Exists(colonySaves))
                {
                    Directory.CreateDirectory(colonySaves);
                }

                var       metadataPath = Path.Combine(GameLoader.Schematic_SAVE_LOC, name + METADATA_FILEEXT);
                Schematic schematic    = LoadSchematic(new NbtFile(path), Vector3Int.invalidPos);


                return(GenerateMetaData(metadataPath, name.Substring(0, name.LastIndexOf('.')), schematic));
            }
            else
            {
                return(null);
            }
        }
Exemplo n.º 4
0
        public static bool TryGetSchematic(string name, int colonyId, Vector3Int location, out Schematic schematic)
        {
            if (TryGetScematicFilePath(name, colonyId, out var scematic))
            {
                schematic = LoadSchematic(scematic, location);
            }
            else
            {
                schematic = null;
            }

            return(schematic != null);
        }
Exemplo n.º 5
0
        private static SchematicMetadata GenerateMetaData(string metadataPath, string name, Schematic schematic)
        {
            var metadata = new SchematicMetadata();

            metadata.Name = name;

            for (int Y = 0; Y <= schematic.YMax; Y++)
            {
                for (int Z = 0; Z <= schematic.ZMax; Z++)
                {
                    for (int X = 0; X <= schematic.XMax; X++)
                    {
                        if (schematic.Blocks.GetLength(0) > X &&
                            schematic.Blocks.GetLength(1) > Y &&
                            schematic.Blocks.GetLength(2) > Z &&
                            schematic.Blocks[X, Y, Z] != null)
                        {
                            var block = schematic.Blocks[X, Y, Z].MappedBlock;

                            if (block.CSIndex != ColonyBuiltIn.ItemTypes.AIR.Id)
                            {
                                var buildType = ItemTypes.GetType(block.CSIndex);

                                if (!buildType.Name.Contains("bedend"))
                                {
                                    var index = block.CSIndex;

                                    if (!string.IsNullOrWhiteSpace(buildType.ParentType) && !buildType.Name.Contains("grass") && !buildType.Name.Contains("leaves"))
                                    {
                                        index = ItemTypes.GetType(buildType.ParentType).ItemIndex;
                                    }

                                    if (metadata.Blocks.TryGetValue(index, out var blockMeta))
                                    {
                                        blockMeta.Count++;
                                    }
                                    else
                                    {
                                        blockMeta = new SchematicBlockMetadata();
                                        blockMeta.Count++;
                                        blockMeta.ItemId = index;

                                        metadata.Blocks.Add(blockMeta.ItemId, blockMeta);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            metadata.MaxX = schematic.XMax;
            metadata.MaxY = schematic.YMax;
            metadata.MaxZ = schematic.ZMax;

            JSON.Serialize(metadataPath, metadata.JsonSerialize());

            return(metadata);
        }