Space Engineers, figuring out the API

This is a short note on how we can develop scripts for use in the game Space Engineers. Background A friend suggested Space Engineers to me, so I bought it and after some scavenging, mining and dying—I tried out the Programmable Block. To my surprise, it used C#! There was one caveat though: the documentation was outdated and often unusable. I managed to piece together scripts using various posts on the official wiki, its Action List, and API List, plus some Reddit threads. ...

August 5, 2017 · 2 min · Michael Bisbjerg

Deserializing different types based on properties, with Newtonsoft.Json

Sometimes we’re presented objects in JSON that do not directly map to a strongly typed object. Here’s a simple example: [ { "type": "Car", "wheels": 4, "trunk": true }, { "type": "Bicycle", "wheels": 2, "persons": 1 } ] The goal is to deserialize these into their respective types, Car and Bicycle, both inheriting from a shared base class Vehicle. JsonConverter JsonConverter allows you to customize how JSON is deserialized. Here’s an implementation that checks the type property and instantiates the appropriate object: ...

August 10, 2016 · 2 min · Michael Bisbjerg

Detecting mismatched objects with Newtonsoft.Json

A common issue in API client libraries is mismatches between the JSON response and the expected strongly typed objects. I encountered this with TMDbLib and created a validator to catch these mismatches during unit testing. The Settings We can set up a JsonSerializerSettings object to: Treat missing properties as errors. Use a custom ContractResolver to enforce that all expected properties must exist in the JSON. Handle and log deserialization errors without throwing immediately. private static readonly List<ErrorEventArgs> Errors = new List<ErrorEventArgs>(); public static void Main() { JsonSerializerSettings settings = new JsonSerializerSettings { MissingMemberHandling = MissingMemberHandling.Error, ContractResolver = new FailingContractResolver(), Error = Error }; JsonSerializer serializer = JsonSerializer.Create(settings); } private static void Error(object sender, ErrorEventArgs errorEventArgs) { Errors.Add(errorEventArgs); errorEventArgs.ErrorContext.Handled = true; } This configuration makes sure that: ...

August 10, 2016 · 2 min · Michael Bisbjerg

Lets Encrypt organized for reverse nginx proxy

This is a short post on using Let’s Encrypt to get TLS certificates for NGINX virtual hosts. The setup assumes: Ubuntu NGINX hosting multiple domains A scheduler (e.g., cron or Rundeck) Let’s Encrypt Let’s Encrypt offers free domain-validated (DV) certificates for HTTPS and other secure services. letsencrypt.org GitHub Initial Setup I run multiple web services on VMs behind a single public IP. My NGINX proxy manages SSL termination and routing using virtual host configs. ...

July 7, 2016 · 2 min · Michael Bisbjerg

Bencoding Library–Part two

Previously, I described a Bencoding Library I made to simplify working with the Bencoding structure in .NET. I also published it on Codeplex. However, I discovered a flaw—if a string contains a null-byte (as most .torrent files do), it will fail. So I made a fix, which has been uploaded to Codeplex as changeset 1102. As can be seen in the changeset, a few files have been changed. Ignoring all the test files, the changes were made to: ...

August 13, 2010 · 1 min · Michael Bisbjerg