Game API Yamldotnet

From Empyrion: Galactic Survival Wiki
Jump to: navigation, search

Index: GameAPI

Using YAML files[edit | edit source]

Installing[edit | edit source]

Simply reference 'Assembly-CSharp-firstpass.dll'

(This is the same as including the C# module YamlDotNet)

Using[edit | edit source]

using YamlDotNet.Serialization;

Example usuage[edit | edit source]

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using YamlDotNet.Serialization;
    namespace SpotGuard
    {
        class ConfigYaml
        {
            public static Config Grab(String filePath)
            {

                //Import the desired file. Here a complete filepath is passed as string        

                var input = File.OpenText(filePath);
                var deserializer = new Deserializer();
                var order = deserializer.Deserialize<Config>(input);
                return order;
            }
            public class Config
            {
                //List all the members of the type.    
                [YamlMember(Alias = "SpecialsFile", SerializeAs = typeof(String))]
                public String SpecialsFile { get; set; }
                [YamlMember(Alias = "TotalMaxSlots")]
                public Int32 TotalMaxSlots { get; set; }  
                [YamlMember(Alias = "NeverKickVip", SerializeAs = typeof(Boolean))]
                public Boolean NeverKickVip { get; set; }                 
                //Technically the 'YamlMember' line is not neccessary, but it can be used to specify how you want to serialize the given data, and to specify an alias (how it is specified in the .yaml file)
            }
        }
    }

Assumingly, you will at some point have Yaml, that is multi-tiered

 Players:
 - PlayerID: 1
 - PlayerID: 2

In this case, you will want to have a secondary class of that, that is referenced as a list.

(In the above example, this would be inside the Config class)

 public List<player> Players { get; set; }

And you would then have a class called 'player', where you do the same, as if you were getting it directly (see config class above)

Outputting with 'default values'[edit | edit source]

If you want your yaml file containing the defaults (such as '0', 'null', (etc)), you might want to use SerializationOptions.EmitDefaults, when initializing your serializer.

var serializer = new Serializer( SerializationOptions.EmitDefaults);

The default behaviour is omit defaults (SerializationOptions.None)

For more options on this, see:

https://github.com/aaubry/YamlDotNet/blob/master/YamlDotNet/Serialization/SerializationOptions.cs

Important[edit | edit source]

When using members of a class, you should expect them to be blank /uninitialized, if the entry is MISSING from the yaml.

Please always initiate with a value, or type.

Especially important for Lists.

Accomplish[edit | edit source]

Use non-defined entries[edit | edit source]

In order to use a non-defined list of entries (for instance, having)

ExternalCall:

- url: derp
  key: val

   key2: val2

(Where ExternalCall is defined, but none of the entries are), you can cast it to an object, by

[YamlMember(Alias = "ExternalCall", SerializeAs = typeof(Object))]
public List<Object> ExternalCall { get; set; } = new List<Object>();

You can then use the object, as

foreach(var d in a.ExternalCall)
{

  Dictionary<Object, Object> m = (Dictionary<Object, Object>)d;   foreach(var n in m)   {    GameAPI.Console_Write(n.Key + " : " + n.Value);   }

}

(You are likely going to want to explicitly cast key and value, when used (Convert it to string, then cast it to type))

Related standard(s)[edit | edit source]

GameAPI standards filelinking - For when specifying the input filepath

Documentation[edit | edit source]

For more documentation on the YamlDotNet usuage: http://aaubry.net/pages/yamldotnet.html You may also refer to the 'deathmessenger' mod.