GameAPI standards filelinking

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

Index: GameAPI

Standard: Using variable path, when exposing files for user[edit | edit source]

Basic[edit | edit source]

When using a configuration file, and having part of these expose 'other files' (for instance, if you are refering to a list with a lot of deathmessages), allow for variable pathing (../) in the entries. This allows for better mod-cooperation, so that you for instance can have a 'reputation list', 'whitelist', 'vip-list' (..) shared between several mods.

Proposed implementation[edit | edit source]

Read for ../, ..\, and interpretate them as 'go up a level, from the current location' (where the mod is executed from) For instance, if string is ../config.yaml, and the mod is in "D:\APPS\steamapps\common\Empyrion - Dedicated Server\Content\Mods\SpotGuard", you would look for the file in "D:\APPS\steamapps\common\Empyrion - Dedicated Server\Content\Mods\" (etc)

Pre-req[edit | edit source]

  • Exposing the 'link' to another file, in a config file.

Code sample[edit | edit source]

       public string getSource(string source, string helper = null)
       {
           if (helper is null || helper == "")
           {
               //modpath to 'this mod' 
               helper = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
           }
           //If string contains '../', go 'up a level'
           int ss = source.IndexOf("../");
           int sd = source.IndexOf("..\\");
           while (ss >= 0 || sd >= 0)
           {
               ss = source.IndexOf("../");
               sd = source.IndexOf("..\\");
               if (ss >= 0 || sd >= 0)
               {
                   if (ss >= 0)
                   {
                       source = source.Remove(ss, 3);
                   }
                   else
                   {
                       source = source.Remove(sd, 3);
                   }
                   var pattern = @"(\\[\w\s-]*)\\$";
                   helper = Regex.Replace(helper, pattern, "\\");
               }
           }
           //If last entry is a backslash, don't append, else append a backslash, before including name
           if (helper.Length - 1 == helper.LastIndexOf("\\"))
           {
               return helper + "" + source;
           }
           else
           {
               return helper + "\\" + source;
           }
       }

Used with:

var source = "config.yaml";
getSource(source)

('source' string should be loaded from said configuration file)

File-standards[edit | edit source]

Refer to this article