GPGSProjectSettings.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. using UnityEngine;
  22. public class GPGSProjectSettings
  23. {
  24. private static GPGSProjectSettings sInstance = null;
  25. public static GPGSProjectSettings Instance
  26. {
  27. get
  28. {
  29. if (sInstance == null)
  30. {
  31. sInstance = new GPGSProjectSettings();
  32. }
  33. return sInstance;
  34. }
  35. }
  36. private bool mDirty = false;
  37. private readonly string mFile;
  38. private Dictionary<string, string> mDict = new Dictionary<string, string>();
  39. private GPGSProjectSettings()
  40. {
  41. string ds = Path.DirectorySeparatorChar.ToString();
  42. mFile = "ProjectSettings/GooglePlayGameSettings.txt".Replace("/", ds);
  43. StreamReader rd = null;
  44. // read the settings file, this list is all the locations it can be in order of precedence.
  45. string[] fileLocations =
  46. {
  47. mFile,
  48. "Assets/GooglePlayGames/Editor/projsettings.txt".Replace("/", ds),
  49. "Assets/Editor/projsettings.txt".Replace("/", ds)
  50. };
  51. foreach (string f in fileLocations)
  52. {
  53. if (File.Exists(f))
  54. {
  55. // assign the reader and break out of the loop
  56. rd = new StreamReader(f);
  57. break;
  58. }
  59. }
  60. if (rd != null)
  61. {
  62. while (!rd.EndOfStream)
  63. {
  64. string line = rd.ReadLine();
  65. if (line == null || line.Trim().Length == 0)
  66. {
  67. break;
  68. }
  69. line = line.Trim();
  70. string[] p = line.Split(new char[] { '=' }, 2);
  71. if (p.Length >= 2)
  72. {
  73. mDict[p[0].Trim()] = p[1].Trim();
  74. }
  75. }
  76. rd.Close();
  77. }
  78. }
  79. public string Get(string key, Dictionary<string, string> overrides)
  80. {
  81. if (overrides.ContainsKey(key))
  82. {
  83. return overrides[key];
  84. }
  85. else if (mDict.ContainsKey(key))
  86. {
  87. return WWW.UnEscapeURL(mDict[key]);
  88. }
  89. else
  90. {
  91. return string.Empty;
  92. }
  93. }
  94. public string Get(string key, string defaultValue)
  95. {
  96. if (mDict.ContainsKey(key))
  97. {
  98. string val = WWW.UnEscapeURL(mDict[key]);
  99. return val;
  100. }
  101. else
  102. {
  103. return defaultValue;
  104. }
  105. }
  106. public string Get(string key)
  107. {
  108. return Get(key, string.Empty);
  109. }
  110. public bool GetBool(string key, bool defaultValue)
  111. {
  112. return Get(key, defaultValue ? "true" : "false").Equals("true");
  113. }
  114. public bool GetBool(string key)
  115. {
  116. return Get(key, "false").Equals("true");
  117. }
  118. public void Set(string key, string val)
  119. {
  120. string escaped = WWW.EscapeURL(val);
  121. mDict[key] = escaped;
  122. mDirty = true;
  123. }
  124. public void Set(string key, bool val)
  125. {
  126. Set(key, val ? "true" : "false");
  127. }
  128. public void Save()
  129. {
  130. // See if we are building the plugin, and don't write the settings file
  131. string[] args = System.Environment.GetCommandLineArgs();
  132. foreach (string a in args)
  133. {
  134. if (a == "-g.building")
  135. {
  136. mDirty = false;
  137. break;
  138. }
  139. }
  140. if (!mDirty)
  141. {
  142. return;
  143. }
  144. StreamWriter wr = new StreamWriter(mFile, false);
  145. foreach (string key in mDict.Keys)
  146. {
  147. wr.WriteLine(key + "=" + mDict[key]);
  148. }
  149. wr.Close();
  150. mDirty = false;
  151. }
  152. public static void Reload ()
  153. {
  154. sInstance = new GPGSProjectSettings();
  155. }
  156. }
  157. }