Page 1 of 1

Blueprint string help

Posted: Tue May 22, 2018 12:30 pm
by The Eriksonn
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
the input is a Blueprint string with a single constant combinator in it
Input to program
The output i got
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

Re: Blueprint string help

Posted: Tue May 22, 2018 5:18 pm
by steinio
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.

Re: Blueprint string help

Posted: Tue May 22, 2018 8:35 pm
by The Eriksonn
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)?

Re: Blueprint string help

Posted: Tue May 22, 2018 8:49 pm
by steinio
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?

Re: Blueprint string help

Posted: Wed May 23, 2018 7:07 am
by The Eriksonn
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
Output

Re: Blueprint string help

Posted: Wed May 23, 2018 8:33 am
by The Eriksonn
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

Re: Blueprint string help

Posted: Thu Jun 21, 2018 5:35 pm
by fenton
Thanks for the code.

Re: Blueprint string help

Posted: Thu Jun 21, 2018 9:09 pm
by The Eriksonn
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();

            }

        }