Blueprint string help

Things that are not directly connected with Factorio.

Blueprint string help

Postby The Eriksonn » Tue May 22, 2018 12:30 pm

I want to have a c# program to create Blueprint strings for combinators.
I have some problem with turning the raw Blueprint string into something readable.
What I want is a function to decode the string into a useful json text and Another to do the opposite.

I have the zlib api to help decode it but it dont work.

plz help…

Source Code
Code: Select all
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Common;
using System.IO;
using System.IO.Compression;
using zlib;

namespace ConsoleStuff
{
    class Program
    {
        static void Main(string[] args)
        {
            string line = Console.ReadLine();

            var B = Encoding.UTF8.GetBytes(line);
            byte[] New = new byte[B.Length - 1];
            for (int i = 0; i < New.Length; i++)
            {
                New[i] = B[i + 1];
            }
            string S = Encoding.UTF8.GetString(New);
            byte[] B2 = Convert.FromBase64String(S);
            string S4 = UnZipStr(B2);
            Console.WriteLine(S4);

            Console.ReadKey();
        }
        public static string Base64Encode(string plainText)
        {
            var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
            return System.Convert.ToBase64String(plainTextBytes);
        }
        public static string Base64Decode(string base64EncodedData)
        {
            var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData);
            return System.Text.Encoding.UTF8.GetString(base64EncodedBytes);
        }
        public static string UnZipStr(byte[] input)
        {
            using (MemoryStream inputStream = new MemoryStream(input))
            {
                using (zlib.ZInputStream S = new ZInputStream(inputStream))
                {
                    return S.ReadString();
                }
            }
        }
    }
}



the input is a Blueprint string with a single constant combinator in it

Input to program
0eNp9j9EKgzAMRf/lPnewiUzXXxljVA0jYFNp45iI/75WX/a0l8BNTu5NVnTjTFNkUdgV3AdJsPcViV/ixtLTZSJYsJKHgThfVOHUiZ764DsWpyFiM2AZ6AN72R4GJMrKdNjtYnnK7DuKGfhrZDCFlHeDlPzsdzZYcs0BA0fqj0ldQvar7M8TBm+KaQeubVNV1a1u2nrbvu3GTt8=


The output i got
?}??
#`Si???V_????MN?MVt?LSdQ??I???_????e"X????8_T?????;?!b3`?{?$??t??by??;??kd0??w????w6Xr


If this is the wrong place to ask for this stuff then feel free to tell me a more fitting place to post in.

Edit: link to the wiki about the Blueprint string: https://wiki.factorio.com/Blueprint_string_format
The Eriksonn
Fast Inserter
Fast Inserter
 
Posts: 225
Joined: Wed Jun 08, 2016 6:16 pm

Re: Blueprint string help

Postby steinio » Tue May 22, 2018 5:18 pm

If i deflate your blueprint string without the first version sign here (http://www.txtwizard.net/compression) i get the json without base64 decode.

Code: Select all
{
  "blueprint": {
    "icons": [
      {
        "signal": {
          "type": "item",
          "name": "constant-combinator"
        },
        "index": 1
      }
    ],
    "entities": [
      {
        "entity_number": 1,
        "name": "constant-combinator",
        "position": {
          "x": 0,
          "y": 0
        },
        "direction": 4
      }
    ],
    "item": "blueprint",
    "version": 68722294784
  }
}


Cu, steinio.
ImageImage
Transport Belt Repair Man
My little mods: Link | My favourite mods: Bob's Mods | Angel's Mods | Yuoki Railway Core | EvoGUI | Logistic Train Network
Factorio Cheat Sheet by Denis Zholob

View unread Posts
User avatar
steinio
Smart Inserter
Smart Inserter
 
Posts: 1909
Joined: Sat Mar 12, 2016 4:19 pm
Location: Germany

Re: Blueprint string help

Postby The Eriksonn » Tue May 22, 2018 8:35 pm

Thanks. Is there a way to go the other way in my c# code automatically or do i have to go via the site to fix it up every time?
If so, am i on the right track with my current code(if i reverse it)?
The Eriksonn
Fast Inserter
Fast Inserter
 
Posts: 225
Joined: Wed Jun 08, 2016 6:16 pm

Re: Blueprint string help

Postby steinio » Tue May 22, 2018 8:49 pm

The Eriksonn wrote:Thanks. Is there a way to go the other way in my c# code automatically or do i have to go via the site to fix it up every time?
If so, am i on the right track with my current code(if i reverse it)?


What i don't understand is, that the blueprint string doesn't need to be decoded with base64 and only needs to be deflated.

Does your code produce the same json, if you skip decoding?
ImageImage
Transport Belt Repair Man
My little mods: Link | My favourite mods: Bob's Mods | Angel's Mods | Yuoki Railway Core | EvoGUI | Logistic Train Network
Factorio Cheat Sheet by Denis Zholob

View unread Posts
User avatar
steinio
Smart Inserter
Smart Inserter
 
Posts: 1909
Joined: Sat Mar 12, 2016 4:19 pm
Location: Germany

Re: Blueprint string help

Postby The Eriksonn » Wed May 23, 2018 7:07 am

I really want to encode now since the decoding was only to get the structure of the json text.
However, decoding dont work at all either.

Code: Select all
static void Main(string[] args)
        {
            string path = @"C:\Users\erihor\OneDrive\Visual Studio\ConsoleStuff\Input";
            string line = File.ReadAllText(path);
            var B = ZipStr(line);
            Console.WriteLine(Encoding.UTF8.GetString(B));

            Console.ReadKey();
        }
        public static byte[] ZipStr(String str)
        {
            using (MemoryStream output = new MemoryStream())
            {
                using (DeflateStream gzip =
                  new DeflateStream(output, CompressionMode.Compress))
                {
                    using (StreamWriter writer =
                      new StreamWriter(gzip, System.Text.Encoding.UTF8))
                    {
                        writer.Write(str);
                    }
                }

                return output.ToArray();
            }
        }

Input
{
  "blueprint": {
    "icons": [
      {
        "signal": {
          "type": "item",
          "name": "constant-combinator"
        },
        "index": 1
      }
    ],
    "entities": [
      {
        "entity_number": 1,
        "name": "constant-combinator",
        "position": {
          "x": 0,
          "y": 0
        },
        "direction": 4
      }
    ],
    "item": "blueprint",
    "version": 68722294784
  }
}


Output
?R[
?0?/???Z?j{?R???,?F?X*??J?H^?I*?To|dfvvw???T?M?|??I???au??????W+???0???A? dl7#?0?"?W???1?"?Pq??K? ???K{?\?_o?2?P!????;Y?4??`i?)}???Bxf??\h?!W?????k?K?>?e?I??????u????6??
The Eriksonn
Fast Inserter
Fast Inserter
 
Posts: 225
Joined: Wed Jun 08, 2016 6:16 pm

Re: Blueprint string help

Postby The Eriksonn » Wed May 23, 2018 8:33 am

Update: i got it to compress by doing base64 decoding but factorio dont accept it even when i add the 0 in front.
Code: Select all
static void Main(string[] args)
        {
            string path = @"C:\Users\erihor\OneDrive\Visual Studio\ConsoleStuff\Input";
            string line = File.ReadAllText(path);
            var B = ZipStr(line);
            Console.WriteLine(Convert.ToBase64String(B));

            Console.ReadKey();
        }
        public static byte[] ZipStr(String str)
        {
            using (MemoryStream output = new MemoryStream())
            {
                using (DeflateStream gzip =
                  new DeflateStream(output, CompressionMode.Compress))
                {
                    using (StreamWriter writer =
                      new StreamWriter(gzip, System.Text.Encoding.UTF8))
                    {
                        writer.Write(str);
                    }
                }

                return output.ToArray();
            }
        }

Output
fY5BCsIwEEXv8tcRtNhachURSesgA82kpKNYSk7mwiN5BZO6ceVm4A9v3vz387WgG240RhaFXcB9kAn2uGDiq7ih7HQeCRas5GEgzpdUOHWimz74jsVpiEgGLBd6wO7SyYBEWZm+ujXMZ7n5jmIG/ooMxjDl2yDlf/ZtDeY8U9GuPexPbYM7xWmFm/ZQVfuqbusmpQ8=
The Eriksonn
Fast Inserter
Fast Inserter
 
Posts: 225
Joined: Wed Jun 08, 2016 6:16 pm

Re: Blueprint string help

Postby fenton » Thu Jun 21, 2018 5:35 pm

Thanks for the code.
Even without luck I'm having big chances of success. That's what I believe in. I didn't try vuelta.club so far, but maybe it's time.
fenton
Manual Inserter
Manual Inserter
 
Posts: 2
Joined: Wed Jun 20, 2018 6:45 am

Re: Blueprint string help

Postby The Eriksonn » Thu Jun 21, 2018 9:09 pm

That code does not work, use this instead:
Code: Select all
//using zlib;

public static string Decode(string base64)

        {

            if (string.IsNullOrWhiteSpace(base64)) return "";

            var enc = new UTF8Encoding();

            return enc.GetString(Decompress(Convert.FromBase64String(base64.Substring(1))));

        }



        public static string Encode(string json)

        {

            var enc = new UTF8Encoding();

            return "0" + Convert.ToBase64String(Compress(enc.GetBytes(json)));

        }



        private static byte[] Compress(byte[] inData)

        {

            using (MemoryStream outMemoryStream = new MemoryStream())

            using (ZOutputStream outZStream = new ZOutputStream(outMemoryStream, zlibConst.Z_DEFAULT_COMPRESSION))

            using (Stream inMemoryStream = new MemoryStream(inData))

            {

                CopyStream(inMemoryStream, outZStream);

                outZStream.finish();

                return outMemoryStream.ToArray();

            }

        }
        private static void CopyStream(System.IO.Stream input, System.IO.Stream output)

        {

            byte[] buffer = new byte[2000];

            int len;

            while ((len = input.Read(buffer, 0, 2000)) > 0)

            {

                output.Write(buffer, 0, len);

            }

            output.Flush();

        }


        private static byte[] Decompress(byte[] inData)

        {

            using (MemoryStream outMemoryStream = new MemoryStream())

            using (ZOutputStream outZStream = new ZOutputStream(outMemoryStream))

            using (Stream inMemoryStream = new MemoryStream(inData))

            {

                CopyStream(inMemoryStream, outZStream);

                outZStream.finish();

                return outMemoryStream.ToArray();

            }

        }
The Eriksonn
Fast Inserter
Fast Inserter
 
Posts: 225
Joined: Wed Jun 08, 2016 6:16 pm


Return to Off topic

Who is online

Users browsing this forum: No registered users and 3 guests