protected override void ExpandCore() { if (_tree == null) { NBTFile file = new NBTFile(_path); _tree = new NbtTree(); _tree.ReadFrom(file.GetDataInputStream(_compressionType)); if (_tree.Root != null) { _container = new CompoundTagContainer(_tree.Root); } } var list = new SortedList <TagKey, TagNode>(); foreach (var item in _tree.Root) { list.Add(new TagKey(item.Key, item.Value.GetTagType()), item.Value); } foreach (TagNode tag in list.Values) { TagDataNode node = TagDataNode.CreateFromTag(tag); if (node != null) { Nodes.Add(node); } } }
public static TagNodeCompound GetLevelDat() { if (levelDat == null) { string path = Path.Combine(savePath, "level.dat"); levelFile = new NBTFile(path); levelTree = new NbtTree(); levelTree.ReadFrom(levelFile.GetDataInputStream()); levelDat = levelTree.Root["Data"] as TagNodeCompound; } return(levelDat); }
public static TagNode DeserializeNode (byte[] data) { NbtTree tree = new NbtTree(); using (MemoryStream ms = new MemoryStream(data)) { tree.ReadFrom(ms); } TagNodeCompound root = tree.Root; if (root == null || !root.ContainsKey("root")) return null; return root["root"]; }
private static NbtFileDataNode TryCreateFrom (string path, CompressionType compressionType) { try { NBTFile file = new NBTFile(path); NbtTree tree = new NbtTree(); tree.ReadFrom(file.GetDataInputStream(compressionType)); if (tree.Root == null) return null; return new NbtFileDataNode(path, compressionType); } catch { return null; } }
protected override void ExpandCore () { if (_tree == null) { _tree = new NbtTree(); _tree.ReadFrom(_regionFile.GetChunkDataInputStream(_x, _z)); if (_tree.Root != null) _container = new CompoundTagContainer(_tree.Root); } foreach (TagNode tag in _tree.Root.Values) { TagDataNode node = TagDataNode.CreateFromTag(tag); if (node != null) Nodes.Add(node); } }
public static TagNodeCompound GetPlayerData() { if (playerData == null) { string path = Path.Combine(savePath, "playerdata"); string[] files = Directory.GetFiles(path); if (files.Length == 0) { throw new Exception("no player data"); } playerFile = new PlayerFile(files[0]); playerData = new NbtTree(); playerData.ReadFrom(playerFile.GetDataInputStream()); } return(playerData.Root); }
public static TagNode DeserializeNode(byte[] data) { NbtTree tree = new NbtTree(); using (MemoryStream ms = new MemoryStream(data)) { tree.ReadFrom(ms); } TagNodeCompound root = tree.Root; if (root == null || !root.ContainsKey("root")) { return(null); } return(root["root"]); }
private static NbtFileDataNode TryCreateFrom(string path, CompressionType compressionType) { try { var file = new NBTFile(path); var tree = new NbtTree(); tree.ReadFrom(file.GetDataInputStream(compressionType)); if (tree.Root == null) return null; return new NbtFileDataNode(path, compressionType); } catch { return null; } }
/// <summary> /// 通过 Region 文件列表构造命令方块编辑工具类的实例 /// </summary> /// <param name="files">Region 文件列表</param> internal CommandBlockIO(string[] files) { // 遍历文件列表 foreach (var file in files) { // 打开 Region 文件 // TODO 非 Region 文件可能抛异常 var region = new RegionFile(file); // 添加到 Region 列表 this.regions.Add(region); // 遍历 Chunk 列表 for (var chunkX = 0; chunkX < 32; chunkX++) { for (var chunkZ = 0; chunkZ < 32; chunkZ++) { if (region.HasChunk(chunkX, chunkZ)) { var tree = new NbtTree(); tree.ReadFrom(region.GetChunkDataInputStream(chunkX, chunkZ)); // Level var level = tree.Root["Level"] as TagNodeCompound; // TileEntities var tileEntities = level["TileEntities"] as TagNodeList; // 遍历 TileEntity 列表 foreach (TagNodeCompound tileEntity in tileEntities) { // 如果是 CommandBlock if (tileEntity["id"].ToString().Equals("Control")) { // 加入 CommandBlock 列表 this.CommandBlocks.Add(new TileCommandBlock(tileEntity, region, chunkX, chunkZ)); // 输出调试信息 // System.Diagnostics.Debug.WriteLine("Add" + this.commandBlocks[this.commandBlocks.Count - 1]); } } } } } } }
protected override void ExpandCore() { if (_tree == null) { _tree = new NbtTree(); _tree.ReadFrom(_regionFile.GetChunkDataInputStream(_x, _z)); if (_tree.Root != null) { _container = new CompoundTagContainer(_tree.Root); } } foreach (TagNode tag in _tree.Root.Values) { TagDataNode node = TagDataNode.CreateFromTag(tag); if (node != null) { Nodes.Add(node); } } }
public async static Task <TagNodeCompound> GetChunkNodeAsync(int x, int z) { Vector2Int key = new Vector2Int(x, z); if (!chunkDictNBT.ContainsKey(key)) { int regionX = GetRegionCoordinate(x); int regionZ = GetRegionCoordinate(z); RegionFile region = GetRegion(regionX, regionZ); if (region != null) { int _x = x - regionX * 32; int _z = z - regionZ * 32; if (region.HasChunk(_x, _z)) { NbtTree _tree = new NbtTree(); Stream stream = region.GetChunkDataInputStream(_x, _z); await Task.Run(() => { _tree.ReadFrom(stream); }); chunkDictNBT[key] = _tree; } } else { Debug.LogError("Region does not exist! need generation."); } } if (chunkDictNBT.ContainsKey(key)) { return(chunkDictNBT[key].Root); } return(null); }
protected override void ExpandCore() { if (_tree == null) { NBTFile file = new NBTFile(_path); _tree = new NbtTree(); _tree.ReadFrom(file.GetDataInputStream(_compressionType)); if (_tree.Root != null) { _container = new CompoundTagContainer(_tree.Root); } } foreach (TagNode tag in _tree.Root.Values) { TagDataNode node = TagDataNode.CreateFromTag(tag); if (node != null) { Nodes.Add(node); } } }
public static TagNodeCompound GetChunkNode(int x, int z) { key.Set(x, z); if (!chunkDictNBT.ContainsKey(key)) { int regionX = GetRegionCoordinate(x); int regionZ = GetRegionCoordinate(z); RegionFile region = GetRegion(regionX, regionZ); if (region != null) { int _x = x - regionX * 32; int _z = z - regionZ * 32; if (region.HasChunk(_x, _z)) { UnityEngine.Profiling.Profiler.BeginSample("GetChunkDataInputStream"); NbtTree _tree = new NbtTree(); using (Stream stream = region.GetChunkDataInputStream(_x, _z)) { UnityEngine.Profiling.Profiler.BeginSample("NBTTree ReadFrom"); _tree.ReadFrom(stream); UnityEngine.Profiling.Profiler.EndSample(); } UnityEngine.Profiling.Profiler.EndSample(); chunkDictNBT.Add(key, _tree); } } } if (chunkDictNBT.ContainsKey(key)) { return(chunkDictNBT[key].Root); } return(null); }
private void Apply(string path, List <LanguageItem> items, List <LanguageItem> signs, List <LanguageItem> containers, List <LanguageItem> Entities, List <LanguageItem> MobSpawners) { //try //{ string[] files = Directory.GetFiles(path + "\\region"); for (int i = 0; i < files.Length; i++) { Dispatcher.BeginInvoke(new Action(delegate { totalProgress.Value += 1; progress.Value = 0; })); RegionFile region = new RegionFile(files[i]); for (int I = 0; I < 32; I++) { for (int J = 0; J < 32; J++) { Dispatcher.BeginInvoke(new Action(delegate { progress.Value += 1; })); if (region.HasChunk(I, J)) { try { NbtTree nbtTree = new NbtTree(); nbtTree.ReadFrom(region.GetChunkDataInputStream(I, J)); TagNodeList tagNodeList = nbtTree.Root["Level"].ToTagCompound()["TileEntities"].ToTagList(); TagNodeList entityList = nbtTree.Root["Level"].ToTagCompound()["Entities"].ToTagList(); bool flag = false; if (entityList != null) { foreach (TagNodeCompound tagNodeCompound in entityList) { if (LangTranslator.EntityTranslator(tagNodeCompound, Entities)) { flag = true; } } } if (tagNodeList != null) { foreach (TagNodeCompound tagNodeCompound in tagNodeList) { string data = tagNodeCompound["id"].ToTagString().Data; if (data == CMDBLOCK) { if (LangTranslator.CommandBlockTranslator(tagNodeCompound, items)) { flag = true; } } else if (data == SIGN) { if (LangTranslator.SignTranslator(tagNodeCompound, signs)) { flag = true; } } else if (tagNodeCompound.ContainsKey("Items") || tagNodeCompound.ContainsKey("RecordItem")) { if (LangTranslator.ContainerTranslator(tagNodeCompound, containers)) { flag = true; } } else if (tagNodeCompound.ContainsKey("SpawnData")) { if (LangTranslator.EntityTranslator(tagNodeCompound["SpawnData"].ToTagCompound(), MobSpawners)) { flag = true; } } } } if (flag) { using (Stream chunkDataOutputStream = region.GetChunkDataOutputStream(I, J)) { nbtTree.WriteTo(chunkDataOutputStream); } } } catch { } } } } } Dispatcher.BeginInvoke(new Action(delegate { progress.Value = 0; totalProgress.Value = 0; progress.IsIndeterminate = true; totalProgress.IsIndeterminate = true; })); //} //catch //{ // Dispatcher.BeginInvoke(new Action(delegate { (System.Windows.Application.Current.MainWindow as MetroWindow).ShowMessageAsync("无法完成操作", "存档可能已经损坏", MessageDialogStyle.Affirmative, new MetroDialogSettings() { AffirmativeButtonText = "确定" }); })); //} }
private void Start(string[] files, string Fpath, bool[] settings, string version, TagNodeCompound level) { //try //{ XmlDocument doc = new XmlDocument(); doc.AppendChild(doc.CreateXmlDeclaration("1.0", "UTF-8", null)); XmlElement root = doc.CreateElement("LanguageItems"); root.SetAttribute("SaveVersion", version); root.SetAttribute("CommandBlocks", settings[0].ToString()); root.SetAttribute("Signs", settings[1].ToString()); root.SetAttribute("Containers", settings[2].ToString()); root.SetAttribute("Entities", settings[3].ToString()); root.SetAttribute("Spawners", settings[4].ToString()); root.SetAttribute("SaveInfo", settings[5].ToString()); doc.AppendChild(root); if (settings[5]) { XmlElement SaveInfo = doc.CreateElement("SaveInfo"); SaveInfo.SetAttribute("Original", level["LevelName"].ToTagString().Data); root.AppendChild(SaveInfo); } XmlElement CommandBlocks = doc.CreateElement("CommandBlocks"); XmlElement Signs = doc.CreateElement("Signs"); XmlElement Containers = doc.CreateElement("Containers"); XmlElement Entities = doc.CreateElement("Entities"); XmlElement Spawners = doc.CreateElement("Spwaners"); if (settings[0]) { root.AppendChild(CommandBlocks); } if (settings[1]) { root.AppendChild(Signs); } if (settings[2]) { root.AppendChild(Containers); } if (settings[3]) { root.AppendChild(Entities); } if (settings[4]) { root.AppendChild(Spawners); } CommandBlocks.SetAttribute("StartIndex", "1"); Signs.SetAttribute("StartIndex", "1"); Containers.SetAttribute("StartIndex", "1"); Entities.SetAttribute("StartIndex", "1"); Spawners.SetAttribute("StartIndex", "1"); List <LanguageItem> list = new List <LanguageItem>(); foreach (string text in files) { RegionFile regionFile = new RegionFile(text); for (int i = 0; i < 32; i++) { for (int j = 0; j < 32; j++) { Dispatcher.BeginInvoke(new Action(delegate { progress.Value += 1; })); if (regionFile.HasChunk(i, j)) { try { NbtTree tree = new NbtTree(); tree.ReadFrom(regionFile.GetChunkDataInputStream(i, j)); TagNodeCompound Root = tree.Root["Level"].ToTagCompound(); TagNodeList TileEntityList = Root["TileEntities"].ToTagList(); TagNodeList EntityList = Root["Entities"].ToTagList(); if (EntityList != null && settings[3]) { foreach (TagNodeCompound tagNodeCompound in EntityList) { LanguageItem item = new LanguageItem(tagNodeCompound, LangItemType.Entity); if (item.Check(list)) { Entities.AppendChild(item.GetXmlElement(doc)); } } } if (TileEntityList != null) { foreach (TagNodeCompound tagNodeCompound in TileEntityList) { string data = tagNodeCompound["id"].ToTagString().Data; if (data == CMDBLOCK) { if (settings[0]) { LanguageItem item = new LanguageItem(tagNodeCompound, LangItemType.CommandBlock); if (item.Check(list)) { CommandBlocks.AppendChild(item.GetXmlElement(doc)); } } } else if (data == SIGN) { if (settings[1]) { LanguageItem item = new LanguageItem(tagNodeCompound, LangItemType.Sign); if (item.Check(list)) { Signs.AppendChild(item.GetXmlElement(doc)); } } } else if (tagNodeCompound.Keys.Contains("Items") || tagNodeCompound.Keys.Contains("RecordItem") || tagNodeCompound.ContainsKey("CustomName") || tagNodeCompound.ContainsKey("Lock")) { if (settings[2]) { LanguageItem item = new LanguageItem(tagNodeCompound, LangItemType.Container); if (item.Check(list)) { Containers.AppendChild(item.GetXmlElement(doc)); } } } else if (data.Contains("mob_spawner")) { if (settings[4]) { LanguageItem item = new LanguageItem(tagNodeCompound, LangItemType.MobSpawner); if (item.Check(list)) { Spawners.AppendChild(item.GetXmlElement(doc)); } } } } } } catch { continue; } } } } Dispatcher.BeginInvoke(new Action(delegate { progress.Value = 0; totalProgress.Value += 1; TotalProgressInfo.Content = "正在扫描第" + totalProgress.Value + "个文件,共" + totalProgress.Maximum + "个"; string[] split = text.Split('\\'); ProgressInfo.Content = "正在扫描 " + split[split.Length - 1]; })); } doc.Save(Fpath); Dispatcher.BeginInvoke(new Action(delegate { progress.Value = 0; totalProgress.Value = 0; progress.IsIndeterminate = true; totalProgress.IsIndeterminate = true; label.Content = "已完成扫描"; TotalProgressInfo.Content = "已完成扫描"; ProgressInfo.Content = ""; Next1.IsEnabled = true; file.Content = "主文件已保存到 " + Fpath; file.ToolTip = Fpath; })); //} //catch (Exception e) //{ // Dispatcher.BeginInvoke(new Action(delegate { (System.Windows.Application.Current.MainWindow as MetroWindow).ShowMessageAsync("无法打开文件", "文件不存在或已经损坏", MessageDialogStyle.Affirmative, new MetroDialogSettings() { AffirmativeButtonText = "确定" }); })); //} }
//private TagNodeCompound _metaRoot; protected override void ExpandCore() { if (_tree == null) { NBTFile file = new NBTFile(_path); _tree = new NbtTree(); _tree.ReadFrom(file.GetDataInputStream(_compressionType)); //_metaRoot = new TagNodeCompound(); if (_tree.Root != null) { //_metaRoot.Add(_tree.Name, _tree.Root); _container = new CompoundTagContainer(_tree.Root); } } /*foreach (TagNode tag in _metaRoot.Values) { TagDataNode node = TagDataNode.CreateFromTag(tag); if (node != null) Nodes.Add(node); }*/ foreach (TagNode tag in _tree.Root.Values) { TagDataNode node = TagDataNode.CreateFromTag(tag); if (node != null) Nodes.Add(node); } }
protected override void ExpandCore() { if (_tree == null) { NBTFile file = new NBTFile(_path); _tree = new NbtTree(); _tree.ReadFrom(file.GetDataInputStream(_compressionType)); if (_tree.Root != null) { _container = new CompoundTagContainer(_tree.Root); } } var list = new SortedList<TagKey, TagNode>(); foreach (var item in _tree.Root) { list.Add(new TagKey(item.Key, item.Value.GetTagType()), item.Value); } foreach (TagNode tag in list.Values) { TagDataNode node = TagDataNode.CreateFromTag(tag); if (node != null) Nodes.Add(node); } }
/// <summary> /// 保存所有修改到 Region 文件 /// </summary> internal void saveAll() { // 缓存变量 RegionFile lastRegion = null; NbtTree lastTree = null; TagNodeList lastTileEntities = null; int lastChunkX = -1; int lastChunkZ = -1; // 遍历命令方块列表 foreach (var commandBlock in this.CommandBlocks) { // 如果需要保存 if (commandBlock.edit) { // 读取目标区块的 TileEntities if (lastRegion == commandBlock.region && commandBlock.chunkX == lastChunkX && commandBlock.chunkZ == lastChunkZ) { // 如果 region chunk 都没变 则触发缓存 继续用上次的 TileEntities } else { if (lastRegion != null) { // 保存之前的修改到文件 using (var stream = lastRegion.GetChunkDataOutputStream(lastChunkX, lastChunkZ)) { lastTree.WriteTo(stream); } } // 打开 Region 文件 lastRegion = commandBlock.region; // 打开目标 Chunk lastTree = new NbtTree(); lastTree.ReadFrom(lastRegion.GetChunkDataInputStream(commandBlock.chunkX, commandBlock.chunkZ)); // Level var level = lastTree.Root["Level"] as TagNodeCompound; // TileEntities lastTileEntities = level["TileEntities"] as TagNodeList; // 保存区块位置 lastChunkX = commandBlock.chunkX; lastChunkZ = commandBlock.chunkZ; } // 遍历 TileEntity 列表 foreach (TagNodeCompound tileEntity in lastTileEntities) { // 当前 TileEntity 的坐标 int x = int.Parse(tileEntity["x"].ToString()); int y = int.Parse(tileEntity["y"].ToString()); int z = int.Parse(tileEntity["z"].ToString()); // 如果坐标一致 if (x == commandBlock.x && y == commandBlock.y && z == commandBlock.z) { // 修改 Command tileEntity["Command"] = new TagNodeString(commandBlock.command); // 输出调试信息 // System.Diagnostics.Debug.WriteLine("Save" + commandBlock); // 设置 CommandBlock 的保存标记 commandBlock.edit = false; break; } } } } if (lastRegion != null) { // 保存之前的修改到文件 using (var str = lastRegion.GetChunkDataOutputStream(lastChunkX, lastChunkZ)) { lastTree.WriteTo(str); } } }
static void LoadNbtStream(TreeNode node, int descriptionIndex, Stream stream) { NbtTree tree = new NbtTree(); tree.ReadFrom(stream); if (node.Tag != null && node.Tag is NbtDataNode) { (node.Tag as NbtDataNode).Tree = tree; } PopulateNodeFromTag(node, descriptionIndex, tree.Root); }
internal static void TryLoadFile(TreeNodeCollection parent, string path) { string ext = Path.GetExtension(path); if (ext == ".mcr" || ext == ".mca") { TreeNode node = ServerNode.CreateLazyRegion(path); parent.Add(node); LinkDataNodeParent(node, node.Parent); return; } if (ext == ".dat" || ext == ".nbt" || ext == ".schematic") { try { NBTFile file = new NBTFile(path); NbtTree tree = new NbtTree(); tree.ReadFrom(file.GetDataInputStream()); TreeNode node = ServerNode.CreateLazyNbt(path, CompressionType.GZip); parent.Add(node); LinkDataNodeParent(node, node.Parent); return; } catch { } try { NBTFile file = new NBTFile(path); NbtTree tree = new NbtTree(); tree.ReadFrom(file.GetDataInputStream(CompressionType.None)); TreeNode node = ServerNode.CreateLazyNbt(path, CompressionType.None); parent.Add(node); LinkDataNodeParent(node, node.Parent); return; } catch { } } }
private void OnGUI() { save = EditorGUILayout.TextField("save", save); pos = EditorGUILayout.Vector3IntField("pos", pos); GUILayout.Label("type=" + type); GUILayout.Label("data=" + blockdata); GUILayout.Label("biome=" + biomeType); GUILayout.Label("skyLight=" + skyLight); if (GUILayout.Button("Update")) { int chunkX = Mathf.FloorToInt(pos.x / 16f); int chunkY = Mathf.FloorToInt(pos.y / 16f); int chunkZ = Mathf.FloorToInt(pos.z / 16f); int xInChunk = pos.x - chunkX * 16; int yInChunk = pos.y - chunkY * 16; int zInChunk = pos.z - chunkZ * 16; TagNodeCompound Chunk = null; int regionX = NBTHelper.GetRegionCoordinate(chunkX); int regionZ = NBTHelper.GetRegionCoordinate(chunkZ); string path = Environment.ExpandEnvironmentVariables("%APPDATA%"); if (!Directory.Exists(path)) { path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); } path = Path.Combine(path, ".minecraft"); path = Path.Combine(path, "saves"); path = Path.Combine(path, save); path = Path.Combine(path, "region"); path = Path.Combine(path, "r." + regionX + "." + regionZ + ".mca"); RegionFile region = new RegionFile(path); if (region != null) { int _x = chunkX - regionX * 32; int _z = chunkZ - regionZ * 32; if (region.HasChunk(_x, _z)) { NbtTree _tree = new NbtTree(); using (Stream stream = region.GetChunkDataInputStream(_x, _z)) { _tree.ReadFrom(stream); } Chunk = _tree.Root; } } if (Chunk != null) { TagNodeCompound Level = Chunk["Level"] as TagNodeCompound; TagNodeList Sections = Level["Sections"] as TagNodeList; if (chunkY < Sections.Count) { TagNodeCompound section = Sections[chunkY] as TagNodeCompound; TagNodeByteArray Blocks = section["Blocks"] as TagNodeByteArray; byte[] blocks = new byte[4096]; Buffer.BlockCopy(Blocks, 0, blocks, 0, 4096); int blockPos = yInChunk * 16 * 16 + zInChunk * 16 + xInChunk; type = blocks[blockPos]; TagNodeByteArray Data = section["Data"] as TagNodeByteArray; blockdata = NBTHelper.GetNibble(Data.Data, blockPos); TagNodeByteArray SkyLight = section["SkyLight"] as TagNodeByteArray; skyLight = NBTHelper.GetNibble(SkyLight.Data, blockPos); } TagNodeByteArray Biomes = Level["Biomes"] as TagNodeByteArray; biomeType = Biomes[xInChunk * 16 + zInChunk]; } Biome biome; if (biomeType < 6) { biome = gBiomes[biomeType]; } else { biome = gBiomes[0]; Debug.Log("no biome,type=" + biomeType); } float temp = biome.temp - Mathf.Max(pos.y - 64, 0) * 0.0016f; AdjTemp = Mathf.Clamp01(temp); AdjRainfall = Mathf.Clamp01(biome.rainfall) * AdjTemp; Vector2 uv = new Vector2(AdjTemp, AdjRainfall); Texture2D grass = Resources.Load <Texture2D>("GUI/grass"); c = grass.GetPixelBilinear(1 - AdjTemp, AdjRainfall); } GUILayout.Label("temp=" + AdjTemp); GUILayout.Label("rainfall=" + AdjRainfall); EditorGUILayout.ColorField(c, GUILayout.Height(30)); }