Initial sync
This commit is contained in:
144
Test/MapReader.cs
Executable file
144
Test/MapReader.cs
Executable file
@@ -0,0 +1,144 @@
|
||||
// using System.Drawing;
|
||||
// using Nbt.Serialization;
|
||||
// using Nbt.Tag;
|
||||
|
||||
// namespace Nbt.Test;
|
||||
|
||||
// public static class MapReader
|
||||
// {
|
||||
// private static readonly Color[] colorTable = [
|
||||
// Color.Transparent,
|
||||
// Color.FromArgb(127, 178, 56),
|
||||
// Color.FromArgb(247, 233, 163),
|
||||
// Color.FromArgb(199, 199, 199),
|
||||
// Color.FromArgb(255, 0, 0),
|
||||
// Color.FromArgb(160, 160, 255),
|
||||
// Color.FromArgb(167, 167, 167),
|
||||
// Color.FromArgb(0, 124, 0),
|
||||
// Color.FromArgb(255, 255, 255),
|
||||
// Color.FromArgb(164, 168, 184),
|
||||
// Color.FromArgb(151, 109, 77),
|
||||
// Color.FromArgb(112, 112, 112),
|
||||
// Color.FromArgb(64, 64, 255),
|
||||
// Color.FromArgb(143, 119, 72),
|
||||
// Color.FromArgb(255, 252, 245),
|
||||
// Color.FromArgb(216, 127, 51),
|
||||
// Color.FromArgb(178, 76, 216),
|
||||
// Color.FromArgb(102, 153, 216),
|
||||
// Color.FromArgb(229, 229, 51),
|
||||
// Color.FromArgb(127, 204, 25),
|
||||
// Color.FromArgb(242, 127, 165),
|
||||
// Color.FromArgb(76, 76, 76),
|
||||
// Color.FromArgb(153, 153, 153),
|
||||
// Color.FromArgb(76, 127, 153),
|
||||
// Color.FromArgb(127, 63, 178),
|
||||
// Color.FromArgb(51, 76, 178),
|
||||
// Color.FromArgb(102, 76, 51),
|
||||
// Color.FromArgb(102, 127, 51),
|
||||
// Color.FromArgb(153, 51, 51),
|
||||
// Color.FromArgb(25, 25, 25),
|
||||
// Color.FromArgb(250, 238, 77),
|
||||
// Color.FromArgb(92, 219, 213),
|
||||
// Color.FromArgb(74, 128, 255),
|
||||
// Color.FromArgb(0, 217, 58),
|
||||
// Color.FromArgb(129, 86, 49),
|
||||
// Color.FromArgb(112, 2, 0),
|
||||
// Color.FromArgb(209, 177, 161),
|
||||
// Color.FromArgb(159, 82, 36),
|
||||
// Color.FromArgb(149, 87, 108),
|
||||
// Color.FromArgb(112, 108, 138),
|
||||
// Color.FromArgb(186, 133, 36),
|
||||
// Color.FromArgb(103, 117, 53),
|
||||
// Color.FromArgb(160, 77, 78),
|
||||
// Color.FromArgb(57, 41, 35),
|
||||
// Color.FromArgb(135, 107, 98),
|
||||
// Color.FromArgb(87, 92, 92),
|
||||
// Color.FromArgb(122, 73, 88),
|
||||
// Color.FromArgb(76, 62, 92),
|
||||
// Color.FromArgb(76, 50, 35),
|
||||
// Color.FromArgb(76, 82, 42),
|
||||
// Color.FromArgb(142, 60, 46),
|
||||
// Color.FromArgb(37, 22, 16),
|
||||
// Color.FromArgb(189, 48, 49),
|
||||
// Color.FromArgb(148, 63, 97),
|
||||
// Color.FromArgb(92, 25, 29),
|
||||
// Color.FromArgb(22, 126, 134),
|
||||
// Color.FromArgb(58, 142, 140),
|
||||
// Color.FromArgb(86, 44, 62),
|
||||
// Color.FromArgb(20, 180, 133),
|
||||
// Color.FromArgb(100, 100, 100),
|
||||
// Color.FromArgb(216, 175, 147),
|
||||
// Color.FromArgb(127, 167, 150)
|
||||
// ];
|
||||
|
||||
// private static readonly double[] multiplierTable = [
|
||||
// 180.0 / 255.0,
|
||||
// 220.0 / 255.0,
|
||||
// 1.0,
|
||||
// 135.0 / 255.0
|
||||
// ];
|
||||
|
||||
// public static void Main(string[] args)
|
||||
// {
|
||||
// const string inputDir = @"C:\Users\hb68\Desktop\a";
|
||||
// const string outputDir = @"C:\Users\hb68\Desktop\b";
|
||||
// const int width = 128, height = 128;
|
||||
// const int pixelSize = 8;
|
||||
|
||||
// Parallel.ForEach(Directory.EnumerateFiles(inputDir), static file =>
|
||||
// {
|
||||
// INbtTag rootTag;
|
||||
// using (var inputStream = File.OpenRead(file))
|
||||
// {
|
||||
// var reader = NbtReader.Create(inputStream);
|
||||
|
||||
// rootTag = reader.ReadNamedTag().Tag;
|
||||
// }
|
||||
|
||||
// var colors = rootTag["data"]["colors"].AsArray<sbyte>().Value;
|
||||
|
||||
// using var image = new Bitmap(width * pixelSize, height * pixelSize);
|
||||
|
||||
// var k = 0;
|
||||
// for (var i = 0; i < height; i++)
|
||||
// {
|
||||
// for (var j = 0; j < width; j++)
|
||||
// {
|
||||
// var colorId = (byte)colors[k];
|
||||
// var baseColorId = colorId / 4;
|
||||
// var colorOffset = colorId % 4;
|
||||
|
||||
// var baseColor = colorTable[baseColorId];
|
||||
// var multiplier = multiplierTable[colorOffset];
|
||||
|
||||
// var color = Color.FromArgb(baseColor.A, (int)(baseColor.R * multiplier), (int)(baseColor.G * multiplier), (int)(baseColor.B * multiplier));
|
||||
|
||||
// if (pixelSize > 1)
|
||||
// {
|
||||
// var imgI = i * pixelSize;
|
||||
// var imgJ = j * pixelSize;
|
||||
|
||||
// for (var xOff = 0; xOff < pixelSize; xOff++)
|
||||
// {
|
||||
// for (var yOff = 0; yOff < pixelSize; yOff++)
|
||||
// {
|
||||
// image.SetPixel(imgJ + xOff, imgI + yOff, color);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// image.SetPixel(j, i, color);
|
||||
// }
|
||||
|
||||
// k++;
|
||||
// }
|
||||
// }
|
||||
|
||||
// var fileName = Path.GetFileName(file);
|
||||
// image.Save(Path.Combine(outputDir, $"{fileName}.png"), System.Drawing.Imaging.ImageFormat.Png);
|
||||
|
||||
// Console.WriteLine(fileName);
|
||||
// });
|
||||
// }
|
||||
// }
|
||||
128
Test/Program.cs
Executable file
128
Test/Program.cs
Executable file
@@ -0,0 +1,128 @@
|
||||
namespace Nbt.Test;
|
||||
|
||||
using System.IO.Compression;
|
||||
using K4os.Compression.LZ4.Streams;
|
||||
using Nbt.Serialization;
|
||||
using Nbt.Tag;
|
||||
|
||||
public static class Program
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
WorldFileReading();
|
||||
}
|
||||
|
||||
private static void Test()
|
||||
{
|
||||
const string filePath = @"D:\Minecraft\game\profiles\test\saves\creatif\players\hbdu68.dat";
|
||||
|
||||
INbtTag rootTag;
|
||||
|
||||
using (var inputStream = File.OpenRead(filePath))
|
||||
{
|
||||
using var reader = NbtReader.Create(inputStream);
|
||||
|
||||
rootTag = reader.ReadNamedTag().Tag;
|
||||
}
|
||||
|
||||
// SNbt.Serialize(Console.Out, rootTag, new() { Style = SNbt.SerializationStyle.Indented });
|
||||
var s = JsonNbt.Serialize(rootTag);
|
||||
var t = JsonNbt.Deserialize(s);
|
||||
|
||||
Console.WriteLine(rootTag.Equals(t));
|
||||
}
|
||||
|
||||
private static void WorldFileReading()
|
||||
{
|
||||
const string inputFile = @"/home/hbecher/Téléchargements/sc-murder/region/r.-3.0.mca";
|
||||
|
||||
// chunk coordinates and last modified timestamps
|
||||
// two 4kiB tables (1024 ints)
|
||||
|
||||
var allOfTheShite = new NbtCompound();
|
||||
|
||||
Span<byte> buf4 = stackalloc byte[4];
|
||||
|
||||
using var inputStream = File.OpenRead(inputFile);
|
||||
|
||||
for (var chunkX = 0; chunkX < 32; chunkX++)
|
||||
{
|
||||
for (var chunkZ = 0; chunkZ < 32; chunkZ++)
|
||||
{
|
||||
var chunkOffset = (chunkX + (chunkZ << 5)) << 2;
|
||||
|
||||
inputStream.Position = chunkOffset;
|
||||
|
||||
inputStream.Read(buf4);
|
||||
buf4.Reverse();
|
||||
var rawLoc = BitConverter.ToUInt32(buf4);
|
||||
var offset = (int)((rawLoc & ~0xFFu) << 4);
|
||||
var size = (int)((rawLoc & 0xFFu) << 12);
|
||||
|
||||
if (offset == 0 && size == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
using (inputStream.Mark())
|
||||
{
|
||||
inputStream.Position += 1024 * 4;
|
||||
inputStream.Read(buf4);
|
||||
}
|
||||
|
||||
buf4.Reverse();
|
||||
var timestamp = BitConverter.ToInt32(buf4);
|
||||
|
||||
using (inputStream.Mark())
|
||||
{
|
||||
inputStream.Position = offset;
|
||||
|
||||
inputStream.Read(buf4);
|
||||
buf4.Reverse();
|
||||
|
||||
var length = BitConverter.ToInt32(buf4);
|
||||
var compressionMode = inputStream.ReadByte();
|
||||
|
||||
Stream inflaterStream;
|
||||
bool leaveOpen = false;
|
||||
|
||||
switch (compressionMode)
|
||||
{
|
||||
case 1:
|
||||
inflaterStream = new GZipStream(inputStream, CompressionMode.Decompress, true);
|
||||
break;
|
||||
|
||||
case 2:
|
||||
inflaterStream = new ZLibStream(inputStream, CompressionMode.Decompress, true);
|
||||
break;
|
||||
|
||||
case 3:
|
||||
inflaterStream = inputStream;
|
||||
leaveOpen = true;
|
||||
break;
|
||||
|
||||
case 4:
|
||||
inflaterStream = LZ4Stream.Decode(inputStream, leaveOpen: true);
|
||||
break;
|
||||
|
||||
default:
|
||||
continue;
|
||||
}
|
||||
|
||||
INbtTag tag;
|
||||
using (var nbtReader = new NbtReader(inflaterStream, leaveOpen))
|
||||
{
|
||||
tag = nbtReader.ReadNamedTag().Tag;
|
||||
}
|
||||
|
||||
allOfTheShite[$"{chunkX}-{chunkZ}"] = tag;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
using var outputStream = File.CreateText(@"/home/hbecher/Téléchargements/a.snbt");
|
||||
|
||||
// SNbt.Serialize(outputStream, allOfTheShite, new() { Style = SNbt.SerializationStyle.Indented });
|
||||
JsonNbt.ToJson(outputStream, allOfTheShite);
|
||||
}
|
||||
}
|
||||
6
Test/StreamExtensions.cs
Executable file
6
Test/StreamExtensions.cs
Executable file
@@ -0,0 +1,6 @@
|
||||
namespace Nbt.Test;
|
||||
|
||||
public static class StreamExtensions
|
||||
{
|
||||
public static StreamMark Mark(this Stream stream) => new(stream);
|
||||
}
|
||||
9
Test/StreamMark.cs
Executable file
9
Test/StreamMark.cs
Executable file
@@ -0,0 +1,9 @@
|
||||
namespace Nbt.Test;
|
||||
|
||||
public readonly struct StreamMark(Stream stream) : IDisposable
|
||||
{
|
||||
private readonly Stream _stream = stream;
|
||||
private readonly long _pos = stream.Position;
|
||||
|
||||
public void Dispose() => _stream.Position = _pos;
|
||||
}
|
||||
15
Test/Test.csproj
Executable file
15
Test/Test.csproj
Executable file
@@ -0,0 +1,15 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<StartupObject></StartupObject>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Nbt\Nbt.csproj" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="System.Drawing.Common" Version="8.0.2" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
Reference in New Issue
Block a user