Game API tuts configecf

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

Purpose[edit | edit source]

When you work with items, having some level of knowledge of the config.ecf may be desirable. This could be the need to know a certain stat configuration, to know all it contains, or similar.

Getting all Names, and correlate them to an ID[edit | edit source]

The most basic usuage is the want to know which ID a KEY is related to. A (Sorta ugly, but it doesn't matter much, as you should just use it on server-start, and keep it in storage after), would be

Code[edit | edit source]

Dictionary<String, int> ecfimport = new Dictionary<String, int>(); //Allow for String => int lookup
Dictionary<int, String> ecfimportOrig = new Dictionary<int, String>(); //Allow for int => string lookup
public void LoadEcf()
{
   var source = "../../Configuration/Config_Example.ecf"; //Path to file
   if (debug == 1)
   {
       GameAPI.Console_Write(thismodsub.ToUpper() + " DEBUG: Attempting to load ecf file from : " + source + " :: " + getSource(source));
   }
   using (var input = File.OpenText(getSource(source)))
   {
       string s = "";
       s = input.ReadToEnd();
       string[] st = s.Split('{');
       foreach (string ss in st)
       {
           int te = -1;
           string ts = "";
           string[] sb = ss.Split(',', '\n');
           foreach (string se in sb)
           { 
               if (te == -1 && se.Contains("Id:"))
               {
                   try
                   {
                       te = int.Parse(se.Split(':')[1].Replace("\n", "").Replace(" ", ""));
                   }
                   catch
                   {
                   } 
               } else if (ts == "" && se.Contains("Name:"))
               {
                   try
                   {
                       ts = se.Split(':')[1].Replace("\n", "").Replace(" ", "");
                   }
                   catch
                   {
                   }
               }
               if (te != -1 && ts != "")
               {
                   break;
               }
           }
           if (te != -1 && ts != "")
           {
               if (ecfimport.ContainsKey(ts.ToLower()))
               {
                   if (debug == 1)
                   {
                       GameAPI.Console_Write(thismodsub.ToUpper() + " Notice: collision on ecf " + ts + " : " + te);
                   }
               }
               else
               {
                   ecfimport.Add(ts.ToLower().Replace("\r", ""), te);
                   ecfimportOrig.Add(te, ts.ToLower().Replace("\r", "")); 
               }
           }
       }
   }
   if (debug == 1)
   {
       GameAPI.Console_Write(thismodsub.ToUpper() + " DEBUG: loaded file " + config.LastUsedFile);
   }
}

Getting a specific attribute[edit | edit source]

For other use-cases, you might not be interested in the overhead. In this case, you may just want to look up a specific attribute, such as 'how large is the stacksize for moneycards'
Here, a regex search is pretty convenient.

\{\s*(?:(?!Name).)*Name:[ ]*MoneyCard(?:(?!StackSize:|\})(?:.|\n))*StackSize:[ ]*([0-9]*)

You can replace 'MoneyCard' with the KEY you want to know data on, and 'StackSize' with what you want data on. Do note, that if the expected data is not a number, to change the final part of the RegEx.
If you alternatively want to search for an ID,

\{\s*(?:(?!Id).)*Id:[ ]*2296(?:(?!StackSize:|\})(?:.|\n))*StackSize:[ ]*([0-9]*)

Please keep in mind, that for a specific attribute, such as 'StackSize' the user may have changed it, in the 'config.ecf'.

You should always search the config.ecf for a result. If none was found, you should look at config_example.ecf instead, and take the default.