ConfigurationEntry.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using System;
  2. using Newtonsoft.Json;
  3. using UnityEngine;
  4. namespace Unity.Services.Core.Configuration
  5. {
  6. /// <summary>
  7. /// Wrapper for project configuration values.
  8. /// </summary>
  9. [Serializable]
  10. class ConfigurationEntry
  11. {
  12. [JsonRequired]
  13. [SerializeField]
  14. string m_Value;
  15. /// <summary>
  16. /// Get the stored configuration value.
  17. /// </summary>
  18. [JsonIgnore]
  19. public string Value => m_Value;
  20. [JsonRequired]
  21. [SerializeField]
  22. bool m_IsReadOnly;
  23. /// <summary>
  24. /// If true, <see cref="Value"/> can't be changed.
  25. /// </summary>
  26. [JsonIgnore]
  27. public bool IsReadOnly
  28. {
  29. get => m_IsReadOnly;
  30. internal set => m_IsReadOnly = value;
  31. }
  32. /// <summary>
  33. /// Create a new instance of the <see cref="ConfigurationEntry"/> class.
  34. /// </summary>
  35. /// <remarks>
  36. /// Required for serialization.
  37. /// </remarks>
  38. public ConfigurationEntry() {}
  39. /// <summary>
  40. /// Create a new instance of the <see cref="ConfigurationEntry"/> class.
  41. /// </summary>
  42. /// <param name="value">
  43. /// The value to store.
  44. /// </param>
  45. /// <param name="isReadOnly">
  46. /// If true, the value can't be changed after construction.
  47. /// </param>
  48. public ConfigurationEntry(string value, bool isReadOnly = false)
  49. {
  50. m_Value = value;
  51. m_IsReadOnly = isReadOnly;
  52. }
  53. /// <summary>
  54. /// Set <see cref="Value"/> only if <see cref="IsReadOnly"/> is false.
  55. /// Does nothing otherwise.
  56. /// </summary>
  57. /// <param name="value">
  58. /// The new value to store.
  59. /// </param>
  60. /// <returns>
  61. /// Return true if <see cref="IsReadOnly"/> is false;
  62. /// return false otherwise.
  63. /// </returns>
  64. public bool TrySetValue(string value)
  65. {
  66. if (IsReadOnly)
  67. {
  68. return false;
  69. }
  70. m_Value = value;
  71. return true;
  72. }
  73. public static implicit operator string(ConfigurationEntry entry) => entry.Value;
  74. public static implicit operator ConfigurationEntry(string value) => new ConfigurationEntry(value);
  75. }
  76. }