예제 #1
0
        private static void OnFileDeleted(String filename)
        {
            if (filename != ConfigurationFilePath)
            {
                return;
            }

            Debug.Log($"[{nameof(NuclearEdition)}] The configuration file has been deleted. Reset configuration.");
            _instance = new RootConfiguration();
            WriteConfiguration();
        }
예제 #2
0
        public static void Read(RootConfiguration root, StreamReader sr)
        {
            Type   rootType     = root.GetType();
            String errorMessage = String.Empty;

            while (!sr.EndOfStream)
            {
                String line = sr.ReadLine();
                try
                {
                    if (String.IsNullOrEmpty(line))
                    {
                        continue;
                    }

                    if (line.StartsWith("//"))
                    {
                        continue;
                    }

                    String[] parts = line.Split('=');
                    if (parts.Length != 2)
                    {
                        errorMessage = $"line.Split('=').Length != 2";
                        goto onError;
                    }

                    String key   = parts[0].Trim();
                    String value = parts[1].Trim();

                    parts = key.Split('.');

                    Type         type     = rootType;
                    Object       instance = root;
                    PropertyInfo property = null;
                    for (Int32 i = 0; i < parts.Length; i++)
                    {
                        property = type.GetProperty(parts[i]);
                        if (property?.CanRead != true)
                        {
                            errorMessage = $"type.GetProperty(parts[i])?.CanRead != true [type: {type.FullName}, parts[i]: {parts[i]}, property: {property}";
                            goto onError;
                        }

                        if (i < parts.Length - 1)
                        {
                            instance = property.GetValue(instance);
                            if (instance is null)
                            {
                                errorMessage = $"property.GetValue(instance) is null";
                                goto onError;
                            }

                            type = property.PropertyType;
                        }
                    }

                    if (property?.CanWrite != true)
                    {
                        errorMessage = $"property?.CanWrite != true";
                        goto onError;
                    }

                    Object convertedValue;
                    if (property.PropertyType.IsEnum)
                    {
                        Int64 number = Int64.Parse(value, CultureInfo.InvariantCulture);
                        convertedValue = Enum.ToObject(property.PropertyType, number);
                    }
                    else if (property.PropertyType == typeof(HashSet <String>))
                    {
                        HashSet <String> set = new HashSet <String>();

                        String[] items = value.Split(';');
                        foreach (String item in items)
                        {
                            set.Add(item.Trim());
                        }

                        convertedValue = set;
                    }
                    else
                    {
                        convertedValue = Convert.ChangeType(value, property.PropertyType, CultureInfo.InvariantCulture);
                    }

                    property.SetValue(instance, convertedValue);
                    continue;
                }
                catch (Exception ex)
                {
                    errorMessage = ex.ToString();
                }

onError:
                Debug.LogWarning($"{nameof(NuclearEdition)} Invalid configuration line: {line}. Error: {errorMessage}");
            }
        }
예제 #3
0
 public static void Write(RootConfiguration root, StreamWriter sw)
 {
     WriteSection(String.Empty, root, sw);
 }