ConfigParser.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Reflection;
  7. using UnityEngine;
  8. using Valve.Newtonsoft.Json;
  9. using Valve.Newtonsoft.Json.Linq;
  10. namespace ConfigReader
  11. {
  12. public static class ConfigParser
  13. {
  14. private static string ConfigPath = "./Assets/CSVInput/Config.json";
  15. public static bool ParseConfig()
  16. {
  17. try
  18. {
  19. string data = File.ReadAllText(ConfigPath);
  20. var source = JsonConvert.DeserializeObject<JToken>(data);
  21. var destinationProperties = typeof(Config).GetProperties(BindingFlags.Public | BindingFlags.Static);
  22. foreach (JProperty prop in source)
  23. {
  24. var destinationProp = destinationProperties.SingleOrDefault(p => p.Name.Equals(prop.Name, StringComparison.OrdinalIgnoreCase));
  25. var value = ((JValue)prop.Value).Value;
  26. destinationProp.SetValue(null, Convert.ChangeType(value, destinationProp.PropertyType));
  27. }
  28. }
  29. catch
  30. {
  31. return false;
  32. }
  33. return true;
  34. }
  35. }
  36. }