Can we use regex in the programmable blocks somehow? Serialization without handcrafting a JSON parser?

DISCUSSION

While string.IndexOf() and string.Substring() can do a lot, my life would be much easier if I could actually use the regex that C# has. I'm setting up my own JSON serialization, since I want to have systems that can keep track of where they are in multiple simultaneous multisecond processes.

Any suggestions?

Posted Aug 3, 2019, 3:37 PM UTC

15
domingo_svk•

If possible, I'd avoid regex and JSON. They are computationally expensive, too expensive to be run 60 times per second.Even too frequent string operations are taxing ...

It is OK to parse command from user or load/save state, but anything more frequent begs for performance problems ...

If possible, use C# structures or simple objects - IIRC they replaced String as parameter for antenna signals (though you can still use string).

5
ouroborus777•

System.Text.RegularExpressions.Regex is available but, since you can't add a using line, you have to spell it out every time.

Regex and JSON are poor choices for code that has limited processing time.

5
toasterbotOP•

Fair enough. I forgot how far sting.Split() goes towards unserializing data. Since basically anything I'd want to store will be numeric, I could just use the following.

my_var1=3.1415;my_var2=2.718 ...etc

1
ouroborus777•

I would suggest creating a class designed to hold a chunk of data, with a serialize and deserialize function built in to it. With that, you would only have to include the absolute minimum data in the serialized string and you could keep the (de)serialization to a minimum.

public class Data {
  char cDelimiter;
  string sDelimiter;
  
  public double SomeDouble = 0;
  public float SomeFloat = 0;
  public string SomeString = "test string";
  
  public Data(char delimiter) {
    cDelimiter = delimiter;
    sDelimiter = delimiter.ToString();
  }
  
  public override string ToString() {
    List<string> work = new List<string>();

    work.Add(SomeDouble.ToString("G17"));
    work.Add(SomeFloat.ToString("G9"));
    work.Add(SomeString);

    return string.Join(sDelimiter, work);
  }
  
  public void FromString(string data) {
    string[] parts = data.Split(cDelimiter);

    double.TryParse(parts[0], ref SomeDouble);
    float.TryParse(parts[1], ref SomeFloat);
    SomeString = parts[2];
  }
}

// Different characters have different problems.
// \x0 probably won't show up in strings, but isn't edit friendly.
Data data = new Data('\x0');

public Program() {
    data.SomeDouble = 3.14F;
    data.SomeFloat = 1.414f;
    data.SomeString = "Test";
    Me.CustomData = data.ToString();
    Echo(Me.CustomData);
}


public void Main() {
    Echo(Me.CustomData);
    data.FromString(Me.CustomData);
    Echo(data.SomeDouble.ToString());
    Echo(data.SomeFloat.ToString());
    Echo(data.SomeString);
}
1
Whiplash141Guided Missile Salesman•

No real serialization is available to the programmable block. You can however use the MyIni class to quickly store and parse data to strings: https://github.com/malware-dev/MDK-SE/wiki/Handling-configuration-and-storage

Keep in mind that performance in the PB is paramount since slow scripts slow down the entire game :)

3