Пример #1
0
        /// <summary>
        /// Loads an NBT level
        /// </summary>
        public static Level Load(string path, bool fullPath = false)
        {
            string lvlPath = fullPath ? path : "levels/nbt/" + path + ".cw";

            if (!File.Exists(lvlPath))
            {
                Logger.LogF("[NBTLoader] Level not found '{0}'", LogType.Error, lvlPath);
                return(null);
            }

            NbtFile levelFile = new NbtFile(lvlPath);

            levelFile.Load();
            Level level;

            try {
                level = new Level(levelFile["Name"]?.StringValue ?? Path.GetFileNameWithoutExtension(path),
                                  (ushort)levelFile["X"].ShortValue,
                                  (ushort)levelFile["Y"].ShortValue,
                                  (ushort)levelFile["Z"].ShortValue);
            }
            catch
            {
                Logger.LogF("Error when loading file '{0}'", LogType.Error, fullPath);
                return(null);
            }

            level.Blocks = levelFile["BlockArray"].ByteArrayValue;
            level.UUID   = levelFile["UUID"]?.ByteArrayValue ?? level.UUID;

            try
            {
                NbtCompound Spawn = (NbtCompound)levelFile["Spawn"];
                level.Spawn.X    = Spawn.GetField("X")?.ShortValue ?? level.Spawn.X;
                level.Spawn.Y    = Spawn.GetField("Y")?.ShortValue ?? level.Spawn.Y;
                level.Spawn.Z    = Spawn.GetField("Z")?.ShortValue ?? level.Spawn.Z;
                level.SpawnRot.X = Spawn.GetField("H")?.ByteValue ?? level.SpawnRot.X;
                level.SpawnRot.Y = Spawn.GetField("P")?.ByteValue ?? level.SpawnRot.Y;
            }
            catch
            {
                Logger.LogF("Error when loading spawn for '{0}'", LogType.Error, level.Name);
            }

            level.TimeCreated  = levelFile["TimeCreated"]?.LongValue ?? Extensions.GetUnixTimestamp();
            level.LastModified = levelFile["LastModified"]?.LongValue ?? Extensions.GetUnixTimestamp();
            level.LastAccessed = Extensions.GetUnixTimestamp();

            try
            {
                NbtCompound MapGen = (NbtCompound)levelFile["MapGeneration"];
                level.SeedName = MapGen.GetField("MapGenerationName")?.StringValue ?? "Unknown";
            }
            catch { }

            try
            {
                NbtCompound CreatedBy = (NbtCompound)levelFile["CreatedBy"];
                level.Authors = CreatedBy.GetField("Author")?.StringValue ?? "Unknown";
            } catch { }

            return(level);
        }