コード例 #1
0
 /// <summary>
 /// Sets the <paramref name="fieldName"/> list's values list as
 /// <paramref name="fieldValues"/>, meaning they're referenced
 /// </summary>
 /// <param name="fieldName"></param>
 /// <param name="fieldValues"></param>
 /// <param name="addIfNotFound"></param>
 public void SetList(string fieldName, List <string> fieldValues, bool addIfNotFound = true)
 {
     if (ListValues.TryGetValue(fieldName, out List <string> unused))
     {
         ListValues[fieldName] = fieldValues;
     }
     else if (addIfNotFound)
     {
         ListValues.Add(fieldName, fieldValues);
     }
 }
コード例 #2
0
        public bool Load(string fullPath)
        {
            if (!File.Exists(fullPath))
            {
                return(false);
            }

            StringValues.Clear();
            ListValues.Clear();
            IsConfigLoaded = false;

            // YML Parser
            try
            {
                bool          isNextLineList = false;
                string        listName       = "";
                List <string> currentList    = new List <string>();
                string[]      configLines    = File.ReadAllLines(fullPath);
                for (int i = 0; i < configLines.Length; i++)
                {
                    string line = configLines[i];
                    // even if in the middle of a list, still allow empty spaces
                    if (line == string.Empty || line.TrimStart()[0] == '#')
                    {
                        continue;
                    }

                    // parse lists. cannot parse lists within a list... not yet atleast ;)
                    if (isNextLineList)
                    {
                        string trimmedListItemStart = line.TrimStart();
                        if (trimmedListItemStart[0] == '-')
                        {
                            string listItem = trimmedListItemStart.After("-").TrimStart().TrimEnd();
                            currentList.Add(listItem);

                            // last line
                            if ((i + 1) == configLines.Length)
                            {
                                if (isNextLineList)
                                {
                                    isNextLineList = false;
                                    ListValues.Add(listName, new List <string>(currentList));
                                    listName = "";
                                    currentList.Clear();
                                    break;
                                }
                            }

                            continue;
                        }
                        else
                        {
                            ListValues.Add(listName, new List <string>(currentList));
                            listName = "";
                            currentList.Clear();
                            isNextLineList = false;
                        }
                    }

                    // parse fields. "string" and "bool" pairs are treaded the same when loading and saving
                    // tho they can be parsed to "true" or "false" as a bool later on... see TryGetBoolean
                    string trimmedStart = line.TrimStart();
                    string trimmedEnd   = line.TrimEnd();
                    string fieldName    = trimmedStart.Before(":");
                    string fieldValue   = trimmedEnd.After(":").TrimStart();
                    if (fieldName.IsEmpty() || fieldName[0] == ':')
                    {
                        continue;
                    }

                    // this is the only thing that can tell if looking for a field/value
                    // or a field/list. fieldValue will always be empty unless you put a
                    // value after the colon. if you put something after the colon then
                    // add the - listValues... idek what could happen but it could break
                    if (fieldValue.IsEmpty())
                    {
                        isNextLineList = true;
                        listName       = fieldName;
                    }
                    else
                    {
                        StringValues.Add(fieldName, fieldValue);
                    }
                }

                ConfigPath     = fullPath;
                IsConfigLoaded = true;
                return(true);
            }
            catch
            {
                StringValues.Clear();
                ListValues.Clear();
                IsConfigLoaded = false;
                return(false);
            }
        }