Blueprint string help

Things that are not directly connected with Factorio.
Post Reply
The Eriksonn
Fast Inserter
Fast Inserter
Posts: 230
Joined: Wed Jun 08, 2016 6:16 pm
Contact:

Blueprint string help

Post 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

User avatar
steinio
Smart Inserter
Smart Inserter
Posts: 2631
Joined: Sat Mar 12, 2016 4:19 pm
Contact:

Re: Blueprint string help

Post 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.
Image

Transport Belt Repair Man

View unread Posts

The Eriksonn
Fast Inserter
Fast Inserter
Posts: 230
Joined: Wed Jun 08, 2016 6:16 pm
Contact:

Re: Blueprint string help

Post 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)?

User avatar
steinio
Smart Inserter
Smart Inserter
Posts: 2631
Joined: Sat Mar 12, 2016 4:19 pm
Contact:

Re: Blueprint string help

Post 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?
Image

Transport Belt Repair Man

View unread Posts

The Eriksonn
Fast Inserter
Fast Inserter
Posts: 230
Joined: Wed Jun 08, 2016 6:16 pm
Contact:

Re: Blueprint string help

Post 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

The Eriksonn
Fast Inserter
Fast Inserter
Posts: 230
Joined: Wed Jun 08, 2016 6:16 pm
Contact:

Re: Blueprint string help

Post 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

fenton
Manual Inserter
Manual Inserter
Posts: 2
Joined: Wed Jun 20, 2018 6:45 am
Contact:

Re: Blueprint string help

Post by fenton »

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.

The Eriksonn
Fast Inserter
Fast Inserter
Posts: 230
Joined: Wed Jun 08, 2016 6:16 pm
Contact:

Re: Blueprint string help

Post 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();

            }

        }

Post Reply

Return to “Off topic”