Skip to content

Commit

Permalink
additional data for books
Browse files Browse the repository at this point in the history
  • Loading branch information
Megageorgio committed Jun 25, 2022
1 parent a073e57 commit 0777988
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 19 deletions.
80 changes: 61 additions & 19 deletions NbtScrapper/NbtScrapper/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,37 @@ private void extractButton_Click(object sender, EventArgs e)
extractButton.Enabled = false;
var regionFiles = new List<string>();
var playerFiles = new List<string>();
var regionPath = Path.Combine(inputPathTextBox.Text, "region");
if (Directory.Exists(regionPath))
{
regionFiles.AddRange(Directory.GetFiles(regionPath));
}
var path = inputPathTextBox.Text;

var entitiesPath = Path.Combine(inputPathTextBox.Text, "entities");
if (Directory.Exists(entitiesPath))
{
regionFiles.AddRange(Directory.GetFiles(entitiesPath));
}
RecursiveFolderSearch(path);

var playerDataPath = Path.Combine(inputPathTextBox.Text, "playerdata");
if (Directory.Exists(playerDataPath))
Task.Run(() => FindBooksAndSigns(regionFiles, playerFiles));

void RecursiveFolderSearch(string path)
{
playerFiles.AddRange(Directory.GetFiles(playerDataPath));
}
var regionPath = Path.Combine(path, "region");
if (Directory.Exists(regionPath))
{
regionFiles.AddRange(Directory.GetFiles(regionPath));
}

Task.Run(() => FindBooksAndSigns(regionFiles, playerFiles));
var entitiesPath = Path.Combine(path, "entities");
if (Directory.Exists(entitiesPath))
{
regionFiles.AddRange(Directory.GetFiles(entitiesPath));
}

var playerDataPath = Path.Combine(path, "playerdata");
if (Directory.Exists(playerDataPath))
{
playerFiles.AddRange(Directory.GetFiles(playerDataPath));
}

foreach (var dir in Directory.GetDirectories(path))
{
RecursiveFolderSearch(dir);
}
}
}

private void FindBooksAndSigns(ICollection<string> regionFiles, ICollection<string> playerFiles)
Expand Down Expand Up @@ -110,6 +122,7 @@ private void FindBooksAndSigns(ICollection<string> regionFiles, ICollection<stri
var region = new RegionFile(file);
var j = 0;
var count = region.AllChunks.Count();

foreach (var chunk in region.AllChunks)
{
j++;
Expand All @@ -125,7 +138,7 @@ private void FindBooksAndSigns(ICollection<string> regionFiles, ICollection<stri
{
continue;
}

chunk.Load();
CyclicSearch(chunk.Data, SearchMethod);
chunk.Remove();
Expand Down Expand Up @@ -165,7 +178,7 @@ void SearchMethod(NbtContainerTag container, NbtTag tag)

if (pages != null && pages.Count != 0)
{
var text = "";
var text = string.Empty;
foreach (var page in pages)
{
var isSnbt = SnbtParser.ClassicTryParse(page.StringValue, false, out var result);
Expand All @@ -180,9 +193,39 @@ void SearchMethod(NbtContainerTag container, NbtTag tag)

hashes.Add(bookHash);

NbtContainerTag inventory = null;
NbtTag id = null;
if (container.Parent is NbtCompound)
{
id = container.Parent?["id"];
if (id != null)
{
inventory = container.Parent;
}
else if (container.Parent?.Parent is NbtCompound)
{
id = container.Parent?.Parent?["id"];
if (id != null)
{
inventory = container.Parent.Parent;
}
}
}


var coords = string.Empty;
if (inventory?["x"] != null)
{
coords =
$"at x:{inventory["x"].IntValue}, y:{inventory["y"].IntValue}, z:{inventory["z"].IntValue}";
}

bookFileWriter.WriteLine("=====================================");
bookFileWriter.WriteLine(
$@"Book ""{(book["title"] == null ? "No name" : book["title"].StringValue)}"" by {(book["author"] == null ? "Unknown" : book["author"].StringValue)}");
bookFileWriter.WriteLine($"(content hash = {Convert.ToHexString(bookHash)})");
bookFileWriter.WriteLine(
$"Found {container["Count"].ByteValue}x {tag.StringValue} in slot {(container["Slot"] == null ? "None" : container["Slot"].ByteValue)} {(id == null ? string.Empty : "of block entity " + id.StringValue)} {coords}");
bookFileWriter.WriteLine();
var i = 0;
foreach (var page in pages)
Expand Down Expand Up @@ -252,7 +295,7 @@ private void RecursiveSearch(NbtContainerTag container, Action<NbtContainerTag,

private void CyclicSearch(NbtContainerTag rootTag, Action<NbtContainerTag, NbtTag> action)
{
var stack = new ConcurrentStack<NbtContainerTag>();
var stack = new Stack<NbtContainerTag>();
stack.Push(rootTag);
while (stack.TryPop(out var container))
{
Expand All @@ -267,7 +310,6 @@ private void CyclicSearch(NbtContainerTag rootTag, Action<NbtContainerTag, NbtTa
}
}
}

}
}

Expand Down
2 changes: 2 additions & 0 deletions NbtScrapper/NbtScrapper/NbtScrapper.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
<TargetFramework>net6.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWindowsForms>true</UseWindowsForms>
<PublishSingleFile>true</PublishSingleFile>
<SelfContained>false</SelfContained>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

Expand Down

0 comments on commit 0777988

Please sign in to comment.