/// <summary> /// Load a level. <remarks>Calls OnLevelLoadEvent.</remarks> /// </summary> /// <returns>The loaded level</returns> //TODO: Load all the types of levels (old mcforge, new mcforge, fcraft, minecpp, etc...) public static Level LoadLevel(string levelName) { if (FindLevel(levelName) != null) { return(null); } string Name = "levels/" + levelName + ".cw"; Level finalLevel = new Level(new Vector3S(32, 32, 32)); finalLevel.Name = levelName; try { if (File.Exists(Name)) { finalLevel.CWMap = new ClassicWorld(Name); finalLevel.Settings = new Metadata(); // -- Create our HC Specific finalLevel.Settings finalLevel.CWMap.MetadataParsers.Add("MCForge", finalLevel.Settings); // -- Register it with the finalLevel.CWMap loader finalLevel.CWMap.Load(); // -- Load the finalLevel.CWMap finalLevel.Settings = (Metadata)finalLevel.CWMap.MetadataParsers["MCForge"]; // -- Creates HC Metadata if it does not exist. if (finalLevel.Settings.perbuild == null) { finalLevel.Settings.perbuild = 0; } if (finalLevel.Settings.pervisit == null) { finalLevel.Settings.pervisit = 0; } finalLevel.HandleMetaData(); Level.OnAllLevelsLoad.Call(finalLevel, new LevelLoadEventArgs(true)); Logger.Log("[Level] " + levelName + " was loaded"); return(finalLevel); } } catch (Exception e) { Logger.Log(e.Message); Logger.Log(e.StackTrace); } return(null); }
/// <summary> /// Load a level. <remarks>Calls OnLevelLoadEvent.</remarks> /// </summary> /// <returns>The loaded level</returns> //TODO: Load all the types of levels (old mcforge, new mcforge, fcraft, minecpp, etc...) public static Level LoadLevel(string levelName) { if (FindLevel(levelName) != null) return null; string Name = "levels/" + levelName + ".cw"; Level finalLevel = new Level(new Vector3S(32, 32, 32)); finalLevel.Name = levelName; try { if (File.Exists(Name)) { finalLevel.CWMap = new ClassicWorld(Name); finalLevel.Settings = new Metadata(); // -- Create our HC Specific finalLevel.Settings finalLevel.CWMap.MetadataParsers.Add("MCForge", finalLevel.Settings); // -- Register it with the finalLevel.CWMap loader finalLevel.CWMap.Load(); // -- Load the finalLevel.CWMap finalLevel.Settings = (Metadata)finalLevel.CWMap.MetadataParsers["MCForge"]; // -- Creates HC Metadata if it does not exist. if (finalLevel.Settings.perbuild == null) { finalLevel.Settings.perbuild = 0; } if (finalLevel.Settings.pervisit == null) { finalLevel.Settings.pervisit = 0; } finalLevel.HandleMetaData(); Level.OnAllLevelsLoad.Call(finalLevel, new LevelLoadEventArgs(true)); Logger.Log("[Level] " + levelName + " was loaded"); return finalLevel; } } catch (Exception e) { Logger.Log(e.Message); Logger.Log(e.StackTrace); } return null; }
/// <summary> /// Load a level. /// </summary> /// <returns>The loaded level</returns> public static Level LoadLevel(string levelName) { string Name = "levels\\" + levelName + ".lvl"; Level finalLevel = new Level(new Vector3S(32, 32, 32)); finalLevel.Name = levelName; try { BinaryReader Binary = null; try { Binary = new BinaryReader(File.Open(Name, FileMode.Open)); } catch { return null; } using (Binary) { long v = Binary.ReadInt64(); if (v != 28542713840690029) //The magic number { Logger.Log("Not a new MCForge Level! Attemping to load old MCForge level format!", Color.Red, Color.Black); Binary.Dispose(); FileStream fs = File.OpenRead(Name); try { GZipStream gs = new GZipStream(fs, CompressionMode.Decompress); byte[] ver = new byte[2]; gs.Read(ver, 0, ver.Length); ushort version = BitConverter.ToUInt16(ver, 0); if (version == 1874) //Is a old MCForge level! { #region Old MCForge Level ushort[] vars = new ushort[6]; byte[] rot = new byte[2]; byte[] header = new byte[16]; gs.Read(header, 0, header.Length); vars[0] = BitConverter.ToUInt16(header, 0); //X vars[1] = BitConverter.ToUInt16(header, 2); //Z vars[2] = BitConverter.ToUInt16(header, 4); //Y vars[3] = BitConverter.ToUInt16(header, 6); //SpawnX vars[4] = BitConverter.ToUInt16(header, 8); //SpawnZ vars[5] = BitConverter.ToUInt16(header, 10); //SpawnY rot[0] = header[12]; //SpawnHeading rot[1] = header[13]; //SpawnYaw finalLevel.Size = new Vector3S((short)vars[0], (short)vars[1], (short)vars[2]); finalLevel.SpawnPos = new Vector3S((short)vars[3], (short)vars[5], (short)vars[4]); finalLevel.SpawnRot = new byte[2] { rot[0], rot[1] }; finalLevel._TotalBlocks = finalLevel.Size.x * finalLevel.Size.z * finalLevel.Size.y; byte[] blocks = new byte[finalLevel.Size.x * finalLevel.Size.z * finalLevel.Size.y]; gs.Read(blocks, 0, blocks.Length); finalLevel.Data = new byte[finalLevel._TotalBlocks]; for (int x = 0; x < finalLevel.Size.x; x++) for (int y = 0; y < finalLevel.Size.y; y++) for (int z = 0; z < finalLevel.Size.z; z++) try { finalLevel.SetBlock(x, y, z, (byte)OldMCForgeToNewMCForge.Convert(blocks[finalLevel.PosToInt((ushort)x, (ushort)y, (ushort)z)])); } //Converts all custom blocks to normal blocks. catch { continue; } gs.Close(); #endregion } } catch { return null; } } else //Is a new MCForge level! { #region New MCForge Level string s = Binary.ReadString(); int x = Convert.ToInt32(s.Split('@')[0]); int y = Convert.ToInt32(s.Split('@')[1]); int z = Convert.ToInt32(s.Split('@')[2]); finalLevel.Size = new Vector3S((short)x, (short)z, (short)y); s = Binary.ReadString(); x = Convert.ToInt32(s.Split('!')[0]); y = Convert.ToInt32(s.Split('!')[1]); z = Convert.ToInt32(s.Split('!')[2]); finalLevel.SpawnPos = new Vector3S((short)x, (short)z, (short)y); s = Binary.ReadString(); int heading = Convert.ToInt32(s.Split('~')[0]); int yaw = Convert.ToInt32(s.Split('~')[1]); finalLevel.SpawnRot = new byte[2] { (byte)heading, (byte)yaw }; int count = Binary.ReadInt32(); for (int i = 0; i < count; i++) //Metadata for blocks { string key = Binary.ReadString(); string value = Binary.ReadString(); finalLevel.ExtraData[key] = value; } finalLevel._TotalBlocks = Binary.ReadInt32(); int ByteLength = Binary.ReadInt32(); byte[] b = Decompress(Binary.ReadBytes(ByteLength)); finalLevel.Data = new byte[finalLevel._TotalBlocks]; finalLevel.Data = b; try { string EOF = Binary.ReadString(); if (EOF != "EOF") { Binary.Dispose(); return null; } } catch { Binary.Dispose(); return null; } #endregion } } Binary.Dispose(); finalLevel.HandleMetaData(); return finalLevel; } catch (Exception e) { Logger.Log(e.Message); Logger.Log(e.StackTrace); } return null; }
/// <summary> /// Load a level. <remarks>Calls OnLevelLoadEvent.</remarks> /// </summary> /// <returns>The loaded level</returns> //TODO: Load all the types of levels (old mcforge, new mcforge, fcraft, minecpp, etc...) public static Level LoadLevel(string levelName) { if (FindLevel(levelName) != null) { return(null); } string Name = "levels/" + levelName + ".lvl"; Level finalLevel = new Level(new Vector3S(32, 32, 32)); finalLevel.Name = levelName; try { BinaryReader Binary = null; try { Binary = new BinaryReader(File.Open(Name, FileMode.Open)); } catch { return(null); } using (Binary) { long v = Binary.ReadInt64(); if (v != MAGIC_NUMBER) //The magic number { Binary.Dispose(); return(new MCForgeOldMap().Load(levelName, Name)); } else //Is a new MCForge level! { #region New MCForge Level byte version = Binary.ReadByte(); if (version == 1) { string s = Binary.ReadString(); int x = Convert.ToInt32(s.Split('@')[0]); int y = Convert.ToInt32(s.Split('@')[1]); int z = Convert.ToInt32(s.Split('@')[2]); finalLevel.Size = new Vector3S((short)x, (short)z, (short)y); s = Binary.ReadString(); x = Convert.ToInt32(s.Split('!')[0]); y = Convert.ToInt32(s.Split('!')[1]); z = Convert.ToInt32(s.Split('!')[2]); finalLevel.SpawnPos = new Vector3S((short)x, (short)z, (short)y); s = Binary.ReadString(); int heading = Convert.ToInt32(s.Split('~')[0]); int yaw = Convert.ToInt32(s.Split('~')[1]); finalLevel.SpawnRot = new byte[2] { (byte)heading, (byte)yaw }; int count = Binary.ReadInt32(); for (int i = 0; i < count; i++) //Metadata for blocks { string key = Binary.ReadString(); string value = Binary.ReadString(); finalLevel.ExtraData[key] = value; } finalLevel._TotalBlocks = Binary.ReadInt32(); int ByteLength = Binary.ReadInt32(); byte[] b = Decompress(Binary.ReadBytes(ByteLength)); finalLevel.Data = new byte[finalLevel._TotalBlocks]; finalLevel.Data = b; try { string EOF = Binary.ReadString(); if (EOF != "EOF") { Binary.Dispose(); return(null); } } catch { Binary.Dispose(); return(null); } } //TODO: Move to HandleMetaData #endregion } } Binary.Dispose(); finalLevel.HandleMetaData(); Level.OnAllLevelsLoad.Call(finalLevel, new LevelLoadEventArgs(true)); Logger.Log("[Level] " + levelName + " was loaded"); return(finalLevel); } catch (Exception e) { Logger.Log(e.Message); Logger.Log(e.StackTrace); } return(null); }
/// <summary> /// Load a level. <remarks>Calls OnLevelLoadEvent.</remarks> /// </summary> /// <returns>The loaded level</returns> //TODO: Load all the types of levels (old mcforge, new mcforge, fcraft, minecpp, etc...) public static Level LoadLevel(string levelName) { if (FindLevel(levelName) != null) return null; string Name = "levels/" + levelName + ".lvl"; Level finalLevel = new Level(new Vector3S(32, 32, 32)); finalLevel.Name = levelName; try { BinaryReader Binary = null; try { Binary = new BinaryReader(File.Open(Name, FileMode.Open)); } catch { return null; } using (Binary) { long v = Binary.ReadInt64(); if (v != MAGIC_NUMBER) //The magic number { Binary.Dispose(); return new MCForgeOldMap().Load(levelName, Name); } else //Is a new MCForge level! { #region New MCForge Level byte version = Binary.ReadByte(); if (version == 1) { string s = Binary.ReadString(); int x = Convert.ToInt32(s.Split('@')[0]); int y = Convert.ToInt32(s.Split('@')[1]); int z = Convert.ToInt32(s.Split('@')[2]); finalLevel.Size = new Vector3S((short)x, (short)z, (short)y); s = Binary.ReadString(); x = Convert.ToInt32(s.Split('!')[0]); y = Convert.ToInt32(s.Split('!')[1]); z = Convert.ToInt32(s.Split('!')[2]); finalLevel.SpawnPos = new Vector3S((short)x, (short)z, (short)y); s = Binary.ReadString(); int heading = Convert.ToInt32(s.Split('~')[0]); int yaw = Convert.ToInt32(s.Split('~')[1]); finalLevel.SpawnRot = new byte[2] { (byte)heading, (byte)yaw }; int count = Binary.ReadInt32(); for (int i = 0; i < count; i++) //Metadata for blocks { string key = Binary.ReadString(); string value = Binary.ReadString(); finalLevel.ExtraData[key] = value; } finalLevel._TotalBlocks = Binary.ReadInt32(); int ByteLength = Binary.ReadInt32(); byte[] b = Decompress(Binary.ReadBytes(ByteLength)); finalLevel.Data = new byte[finalLevel._TotalBlocks]; finalLevel.Data = b; try { string EOF = Binary.ReadString(); if (EOF != "EOF") { Binary.Dispose(); return null; } } catch { Binary.Dispose(); return null; } } //TODO: Move to HandleMetaData #endregion } } Binary.Dispose(); finalLevel.HandleMetaData(); Level.OnAllLevelsLoad.Call(finalLevel, new LevelLoadEventArgs(true)); Logger.Log("[Level] " + levelName + " was loaded"); return finalLevel; } catch (Exception e) { Logger.Log(e.Message); Logger.Log(e.StackTrace); } return null; }