GPGSProjectSettings.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // <copyright file="GPGSProjectSettings.cs" company="Google Inc.">
  2. // Copyright (C) 2014 Google Inc.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. // </copyright>
  16. // Keep this file even on unsupported configurations.
  17. namespace GooglePlayGames.Editor
  18. {
  19. using System.Collections.Generic;
  20. using System.IO;
  21. #if UNITY_2017_3_OR_NEWER
  22. using UnityEngine.Networking;
  23. #else
  24. using UnityEngine;
  25. #endif
  26. public class GPGSProjectSettings
  27. {
  28. private static GPGSProjectSettings sInstance = null;
  29. public static GPGSProjectSettings Instance
  30. {
  31. get
  32. {
  33. if (sInstance == null)
  34. {
  35. sInstance = new GPGSProjectSettings();
  36. }
  37. return sInstance;
  38. }
  39. }
  40. private bool mDirty = false;
  41. private readonly string mFile;
  42. private Dictionary<string, string> mDict = new Dictionary<string, string>();
  43. private GPGSProjectSettings()
  44. {
  45. mFile = GPGSUtil.SlashesToPlatformSeparator("ProjectSettings/GooglePlayGameSettings.txt");
  46. StreamReader rd = null;
  47. // read the settings file, this list is all the locations it can be in order of precedence.
  48. string[] fileLocations =
  49. {
  50. mFile,
  51. GPGSUtil.SlashesToPlatformSeparator(Path.Combine(GPGSUtil.RootPath, "Editor/projsettings.txt")),
  52. GPGSUtil.SlashesToPlatformSeparator("Assets/Editor/projsettings.txt")
  53. };
  54. foreach (string f in fileLocations)
  55. {
  56. if (File.Exists(f))
  57. {
  58. // assign the reader and break out of the loop
  59. rd = new StreamReader(f);
  60. break;
  61. }
  62. }
  63. if (rd != null)
  64. {
  65. while (!rd.EndOfStream)
  66. {
  67. string line = rd.ReadLine();
  68. if (line == null || line.Trim().Length == 0)
  69. {
  70. break;
  71. }
  72. line = line.Trim();
  73. string[] p = line.Split(new char[] {'='}, 2);
  74. if (p.Length >= 2)
  75. {
  76. mDict[p[0].Trim()] = p[1].Trim();
  77. }
  78. }
  79. rd.Close();
  80. }
  81. }
  82. public string Get(string key, Dictionary<string, string> overrides)
  83. {
  84. if (overrides.ContainsKey(key))
  85. {
  86. return overrides[key];
  87. }
  88. else if (mDict.ContainsKey(key))
  89. {
  90. #if UNITY_2017_3_OR_NEWER
  91. return UnityWebRequest.UnEscapeURL(mDict[key]);
  92. #else
  93. return WWW.UnEscapeURL(mDict[key]);
  94. #endif
  95. }
  96. else
  97. {
  98. return string.Empty;
  99. }
  100. }
  101. public string Get(string key, string defaultValue)
  102. {
  103. if (mDict.ContainsKey(key))
  104. {
  105. #if UNITY_2017_3_OR_NEWER
  106. return UnityWebRequest.UnEscapeURL(mDict[key]);
  107. #else
  108. return WWW.UnEscapeURL(mDict[key]);
  109. #endif
  110. }
  111. else
  112. {
  113. return defaultValue;
  114. }
  115. }
  116. public string Get(string key)
  117. {
  118. return Get(key, string.Empty);
  119. }
  120. public bool GetBool(string key, bool defaultValue)
  121. {
  122. return Get(key, defaultValue ? "true" : "false").Equals("true");
  123. }
  124. public bool GetBool(string key)
  125. {
  126. return Get(key, "false").Equals("true");
  127. }
  128. public void Set(string key, string val)
  129. {
  130. #if UNITY_2017_3_OR_NEWER
  131. string escaped = UnityWebRequest.EscapeURL(val);
  132. #else
  133. string escaped = WWW.EscapeURL(val);
  134. #endif
  135. mDict[key] = escaped;
  136. mDirty = true;
  137. }
  138. public void Set(string key, bool val)
  139. {
  140. Set(key, val ? "true" : "false");
  141. }
  142. public void Save()
  143. {
  144. // See if we are building the plugin, and don't write the settings file
  145. string[] args = System.Environment.GetCommandLineArgs();
  146. foreach (string a in args)
  147. {
  148. if (a == "-g.building")
  149. {
  150. mDirty = false;
  151. break;
  152. }
  153. }
  154. if (!mDirty)
  155. {
  156. return;
  157. }
  158. StreamWriter wr = new StreamWriter(mFile, false);
  159. foreach (string key in mDict.Keys)
  160. {
  161. wr.WriteLine(key + "=" + mDict[key]);
  162. }
  163. wr.Close();
  164. mDirty = false;
  165. }
  166. public static void Reload()
  167. {
  168. sInstance = new GPGSProjectSettings();
  169. }
  170. }
  171. }