Skip to content

Instantly share code, notes, and snippets.

@reZach
Created August 16, 2019 16:12
Show Gist options
  • Save reZach/47e58389bd3cc5a20192ecdd0609c2db to your computer and use it in GitHub Desktop.
Save reZach/47e58389bd3cc5a20192ecdd0609c2db to your computer and use it in GitHub Desktop.
decoder
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
namespace GBworldgen
{
public class Program
{
public static void Main(string[] args)
{
// Deserialize map; from GB code
string base64enc = "7c4xCsAgAAPAtNLdoQ/q03yF71WkL6hDlwuEg0xJknu2vx5ZuUiSJLlnWbTU55xTK3//IUmSJEmSJEmSJEmSJEmSJEmSJEmSJEmSJEmSJEmSJPndAQ==";
byte[] base64dec = System.Convert.FromBase64String(base64enc);
Int3 int3 = Int3.zero;
List<List<int>> spanLengths = new List<List<int>>();
List<List<int>> spans = new List<List<int>>();
using (MemoryStream memStream = new MemoryStream(base64dec))
{
using (MemoryStream memStream2 = new MemoryStream())
{
using (DeflateStream deflateStream = new DeflateStream(memStream, CompressionMode.Decompress, true))
{
deflateStream.CopyTo(memStream2);
}
memStream2.Seek((long)0, SeekOrigin.Begin);
using (BinaryReader binaryReader = new BinaryReader(memStream2))
{
binaryReader.ReadInt32();
int3[0] = binaryReader.ReadInt32();
int3[1] = binaryReader.ReadInt32();
int3[2] = binaryReader.ReadInt32();
for (int i = 0; i < int3[1]; i++)
{
for (int j = 0; j < int3[2]; j++)
{
int num = binaryReader.ReadInt32();
//this.span_lengths[i][j] = num;
if (spanLengths.Count <= i)
spanLengths.Add(new List<int>());
if (spanLengths[i].Count <= j)
spanLengths[i].Add(num);
for (int k = 0; k < num; k++)
{
int num1 = binaryReader.ReadInt32();
//this.spans[i][j * int3[0] + k] = num1;
if (spans.Count <= i)
spans.Add(new List<int>());
if (spans[i].Count <= (j * int3[0] + k))
spans[i].AddRange(new List<int>(
Enumerable.Repeat(0, (j * int3[0] + k + 1 - spans[i].Count))));
spans[i][j * int3[0] + k] = num1;
}
}
}
}
}
}
int item = int3[1];
List<List<int>> nativeArrayArrays = new List<List<int>>();
List<List<int>> nums1 = new List<List<int>>();
List<List<int>> nativeArrayArrays2 = new List<List<int>>();
int item1 = int3[0] * int3[2];
for (int l = 0; l < int3[1]; l++)
{
if (nativeArrayArrays.Count <= l)
nativeArrayArrays.Add(new List<int>(
Enumerable.Repeat(0, item1)));
if (nums1.Count <= 1)
nums1.Add(new List<int>(
Enumerable.Repeat(0, item1)));
if (nativeArrayArrays2.Count <= 1)
nativeArrayArrays2.Add(new List<int>(
Enumerable.Repeat(0, item1)));
}
for (int m = 0; m < item; m++)
{
int numSpans = int3[2];
List<int> tileDirections = nativeArrayArrays2[m].GetRange(0, item1);
List<int> tileShapes = nativeArrayArrays[m].GetRange(0, item1);
List<int> tileStyles = nums1[m].GetRange(0, item1);
List<int> span = spans[m].GetRange(0, item1);
List<int> spanlengths = spanLengths[m].GetRange(0, int3[2]);
}
//int[] map = new int[6]
//{
// Encode(1, 2, 2),
// Encode(2, 2, 2),
// Encode(1, 2, 2),
// Encode(2, 2, 2),
// Encode(1, 2, 2),
// Encode(2, 2, 2)
//};
//byte[] serialized = SerializeMap(map);
//Console.WriteLine(Convert.ToBase64String(serialized));
//if (BitConverter.IsLittleEndian)
// Array.Reverse(base64dec);
//for (var i = 0; i < base64dec.Length; i += 4)
//{
// int newInt = BitConverter.ToInt32(base64dec.AsSpan(i, 4));
// Decode(newInt, out int len, out int shape, out int style, out int dir);
// Console.WriteLine($"Len: {len}. Shape: {shape}. Style: {style}. Dir: {dir}.");
//}
//Console.WriteLine(base64dec);
}
public static int Encode(int shape, int style, int dir, int len = 1)
{
return (len << 22) + (style << 13) + (shape + 1 << 4) + dir;
}
public static void Decode(int val, out int len, out int shape, out int style, out int dir)
{
len = val >> 22;
style = val >> 13 & 511;
shape = (val >> 4 & 15) - 1;
dir = val & 3;
}
public static byte[] SerializeMap(int[] map)
{
byte[] result;
using (MemoryStream memoryStream = new MemoryStream())
{
using (DeflateStream deflateStream = new DeflateStream(memoryStream, CompressionLevel.Fastest))
{
using (BinaryWriter binaryWriter = new BinaryWriter(deflateStream))
{
// Write binary; TerrainSystem public byte[] Serialize(Int3 start, Int3 end)
binaryWriter.Write(0);
binaryWriter.Write(6); // end x - start x
binaryWriter.Write(0); // end y - start y
binaryWriter.Write(0); // end z - start z
// Write map
for (int i = 0; i < map.Length; i++)
{
binaryWriter.Write(map[i]);
}
}
}
result = memoryStream.ToArray();
}
return result;
}
}
public struct Int3
{
public int x;
public int y;
public int z;
public static Int3 right;
public static Int3 up;
public static Int3 forward;
public static Int3 zero;
public static Int3 one;
public int this[int index]
{
get
{
if (index == 0)
{
return this.x;
}
if (index == 1)
{
return this.y;
}
return this.z;
}
set
{
if (index == 0)
{
this.x = value;
return;
}
if (index == 1)
{
this.y = value;
return;
}
this.z = value;
}
}
static Int3()
{
Int3.right = new Int3(1, 0, 0);
Int3.up = new Int3(0, 1, 0);
Int3.forward = new Int3(0, 0, 1);
Int3.zero = new Int3(0, 0, 0);
Int3.one = new Int3(1, 1, 1);
}
public Int3(int val)
{
this.x = val;
this.y = val;
this.z = val;
}
public Int3(int _x, int _y, int _z)
{
this.x = _x;
this.y = _y;
this.z = _z;
}
public override bool Equals(object o)
{
if (!(o is Int3))
{
return false;
}
return (Int3)o == this;
}
public override int GetHashCode()
{
return this.x.GetHashCode() ^ this.y.GetHashCode() << 2 ^ this.z.GetHashCode() >> 2;
}
//public static int ManhattanDistance(Int3 pos)
//{
// return Mathf.Abs(pos[0]) + Mathf.Abs(pos[1]) + Mathf.Abs(pos[2]);
//}
public static Int3 operator +(Int3 a, Int3 b)
{
return new Int3(a[0] + b[0], a[1] + b[1], a[2] + b[2]);
}
public static Int3 operator /(Int3 a, int d)
{
return new Int3(a[0] / d, a[1] / d, a[2] / d);
}
public static bool operator ==(Int3 lhs, Int3 rhs)
{
if (lhs[0] != rhs[0] || lhs[1] != rhs[1])
{
return false;
}
return lhs[2] == rhs[2];
}
//public static implicit operator Vector3(Int3 pos)
//{
// return new Vector3((float)pos[0], (float)pos[1], (float)pos[2]);
//}
//public static implicit operator Int3(Vector3 pos)
//{
// return new Int3(Mathf.RoundToInt(pos[0]), Mathf.RoundToInt(pos[1]), Mathf.RoundToInt(pos[2]));
//}
public static bool operator !=(Int3 lhs, Int3 rhs)
{
return !(lhs == rhs);
}
public static Int3 operator *(Int3 a, int d)
{
return new Int3(a[0] * d, a[1] * d, a[2] * d);
}
public static Int3 operator *(int d, Int3 a)
{
return new Int3(a[0] * d, a[1] * d, a[2] * d);
}
public static Int3 operator -(Int3 a, Int3 b)
{
return new Int3(a[0] - b[0], a[1] - b[1], a[2] - b[2]);
}
public static Int3 operator -(Int3 a)
{
return new Int3(-a[0], -a[1], -a[2]);
}
public override string ToString()
{
return string.Format("({0}, {1}, {2})", this.x, this.y, this.z);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment