SettingManager.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. //------------------------------------------------------------
  2. // Game Framework
  3. // Copyright © 2013-2021 loyalsoft. All rights reserved.
  4. // Homepage: http://www.game7000.com/
  5. // Feedback: http://www.game7000.com/
  6. //------------------------------------------------------------
  7. using System;
  8. using System.Collections.Generic;
  9. namespace GameFramework.Setting
  10. {
  11. /// <summary>
  12. /// 游戏配置管理器。
  13. /// </summary>
  14. internal sealed class SettingManager : GameFrameworkModule, ISettingManager
  15. {
  16. private ISettingHelper m_SettingHelper;
  17. /// <summary>
  18. /// 初始化游戏配置管理器的新实例。
  19. /// </summary>
  20. public SettingManager()
  21. {
  22. m_SettingHelper = null;
  23. }
  24. /// <summary>
  25. /// 获取游戏配置项数量。
  26. /// </summary>
  27. public int Count
  28. {
  29. get
  30. {
  31. if (m_SettingHelper == null)
  32. {
  33. throw new GameFrameworkException("Setting helper is invalid.");
  34. }
  35. return m_SettingHelper.Count;
  36. }
  37. }
  38. /// <summary>
  39. /// 游戏配置管理器轮询。
  40. /// </summary>
  41. /// <param name="elapseSeconds">逻辑流逝时间,以秒为单位。</param>
  42. /// <param name="realElapseSeconds">真实流逝时间,以秒为单位。</param>
  43. internal override void Update(float elapseSeconds, float realElapseSeconds)
  44. {
  45. }
  46. /// <summary>
  47. /// 关闭并清理游戏配置管理器。
  48. /// </summary>
  49. internal override void Shutdown()
  50. {
  51. Save();
  52. }
  53. /// <summary>
  54. /// 设置游戏配置辅助器。
  55. /// </summary>
  56. /// <param name="settingHelper">游戏配置辅助器。</param>
  57. public void SetSettingHelper(ISettingHelper settingHelper)
  58. {
  59. if (settingHelper == null)
  60. {
  61. throw new GameFrameworkException("Setting helper is invalid.");
  62. }
  63. m_SettingHelper = settingHelper;
  64. }
  65. /// <summary>
  66. /// 加载游戏配置。
  67. /// </summary>
  68. /// <returns>是否加载游戏配置成功。</returns>
  69. public bool Load()
  70. {
  71. if (m_SettingHelper == null)
  72. {
  73. throw new GameFrameworkException("Setting helper is invalid.");
  74. }
  75. return m_SettingHelper.Load();
  76. }
  77. /// <summary>
  78. /// 保存游戏配置。
  79. /// </summary>
  80. /// <returns>是否保存游戏配置成功。</returns>
  81. public bool Save()
  82. {
  83. if (m_SettingHelper == null)
  84. {
  85. throw new GameFrameworkException("Setting helper is invalid.");
  86. }
  87. return m_SettingHelper.Save();
  88. }
  89. /// <summary>
  90. /// 获取所有游戏配置项的名称。
  91. /// </summary>
  92. /// <returns>所有游戏配置项的名称。</returns>
  93. public string[] GetAllSettingNames()
  94. {
  95. if (m_SettingHelper == null)
  96. {
  97. throw new GameFrameworkException("Setting helper is invalid.");
  98. }
  99. return m_SettingHelper.GetAllSettingNames();
  100. }
  101. /// <summary>
  102. /// 获取所有游戏配置项的名称。
  103. /// </summary>
  104. /// <param name="results">所有游戏配置项的名称。</param>
  105. public void GetAllSettingNames(List<string> results)
  106. {
  107. if (m_SettingHelper == null)
  108. {
  109. throw new GameFrameworkException("Setting helper is invalid.");
  110. }
  111. m_SettingHelper.GetAllSettingNames(results);
  112. }
  113. /// <summary>
  114. /// 检查是否存在指定游戏配置项。
  115. /// </summary>
  116. /// <param name="settingName">要检查游戏配置项的名称。</param>
  117. /// <returns>指定的游戏配置项是否存在。</returns>
  118. public bool HasSetting(string settingName)
  119. {
  120. if (m_SettingHelper == null)
  121. {
  122. throw new GameFrameworkException("Setting helper is invalid.");
  123. }
  124. if (string.IsNullOrEmpty(settingName))
  125. {
  126. throw new GameFrameworkException("Setting name is invalid.");
  127. }
  128. return m_SettingHelper.HasSetting(settingName);
  129. }
  130. /// <summary>
  131. /// 移除指定游戏配置项。
  132. /// </summary>
  133. /// <param name="settingName">要移除游戏配置项的名称。</param>
  134. /// <returns>是否移除指定游戏配置项成功。</returns>
  135. public bool RemoveSetting(string settingName)
  136. {
  137. if (m_SettingHelper == null)
  138. {
  139. throw new GameFrameworkException("Setting helper is invalid.");
  140. }
  141. if (string.IsNullOrEmpty(settingName))
  142. {
  143. throw new GameFrameworkException("Setting name is invalid.");
  144. }
  145. return m_SettingHelper.RemoveSetting(settingName);
  146. }
  147. /// <summary>
  148. /// 清空所有游戏配置项。
  149. /// </summary>
  150. public void RemoveAllSettings()
  151. {
  152. if (m_SettingHelper == null)
  153. {
  154. throw new GameFrameworkException("Setting helper is invalid.");
  155. }
  156. m_SettingHelper.RemoveAllSettings();
  157. }
  158. /// <summary>
  159. /// 从指定游戏配置项中读取布尔值。
  160. /// </summary>
  161. /// <param name="settingName">要获取游戏配置项的名称。</param>
  162. /// <returns>读取的布尔值。</returns>
  163. public bool GetBool(string settingName)
  164. {
  165. if (m_SettingHelper == null)
  166. {
  167. throw new GameFrameworkException("Setting helper is invalid.");
  168. }
  169. if (string.IsNullOrEmpty(settingName))
  170. {
  171. throw new GameFrameworkException("Setting name is invalid.");
  172. }
  173. return m_SettingHelper.GetBool(settingName);
  174. }
  175. /// <summary>
  176. /// 从指定游戏配置项中读取布尔值。
  177. /// </summary>
  178. /// <param name="settingName">要获取游戏配置项的名称。</param>
  179. /// <param name="defaultValue">当指定的游戏配置项不存在时,返回此默认值。</param>
  180. /// <returns>读取的布尔值。</returns>
  181. public bool GetBool(string settingName, bool defaultValue)
  182. {
  183. if (m_SettingHelper == null)
  184. {
  185. throw new GameFrameworkException("Setting helper is invalid.");
  186. }
  187. if (string.IsNullOrEmpty(settingName))
  188. {
  189. throw new GameFrameworkException("Setting name is invalid.");
  190. }
  191. return m_SettingHelper.GetBool(settingName, defaultValue);
  192. }
  193. /// <summary>
  194. /// 向指定游戏配置项写入布尔值。
  195. /// </summary>
  196. /// <param name="settingName">要写入游戏配置项的名称。</param>
  197. /// <param name="value">要写入的布尔值。</param>
  198. public void SetBool(string settingName, bool value)
  199. {
  200. if (m_SettingHelper == null)
  201. {
  202. throw new GameFrameworkException("Setting helper is invalid.");
  203. }
  204. if (string.IsNullOrEmpty(settingName))
  205. {
  206. throw new GameFrameworkException("Setting name is invalid.");
  207. }
  208. m_SettingHelper.SetBool(settingName, value);
  209. }
  210. /// <summary>
  211. /// 从指定游戏配置项中读取整数值。
  212. /// </summary>
  213. /// <param name="settingName">要获取游戏配置项的名称。</param>
  214. /// <returns>读取的整数值。</returns>
  215. public int GetInt(string settingName)
  216. {
  217. if (m_SettingHelper == null)
  218. {
  219. throw new GameFrameworkException("Setting helper is invalid.");
  220. }
  221. if (string.IsNullOrEmpty(settingName))
  222. {
  223. throw new GameFrameworkException("Setting name is invalid.");
  224. }
  225. return m_SettingHelper.GetInt(settingName);
  226. }
  227. /// <summary>
  228. /// 从指定游戏配置项中读取整数值。
  229. /// </summary>
  230. /// <param name="settingName">要获取游戏配置项的名称。</param>
  231. /// <param name="defaultValue">当指定的游戏配置项不存在时,返回此默认值。</param>
  232. /// <returns>读取的整数值。</returns>
  233. public int GetInt(string settingName, int defaultValue)
  234. {
  235. if (m_SettingHelper == null)
  236. {
  237. throw new GameFrameworkException("Setting helper is invalid.");
  238. }
  239. if (string.IsNullOrEmpty(settingName))
  240. {
  241. throw new GameFrameworkException("Setting name is invalid.");
  242. }
  243. return m_SettingHelper.GetInt(settingName, defaultValue);
  244. }
  245. /// <summary>
  246. /// 向指定游戏配置项写入整数值。
  247. /// </summary>
  248. /// <param name="settingName">要写入游戏配置项的名称。</param>
  249. /// <param name="value">要写入的整数值。</param>
  250. public void SetInt(string settingName, int value)
  251. {
  252. if (m_SettingHelper == null)
  253. {
  254. throw new GameFrameworkException("Setting helper is invalid.");
  255. }
  256. if (string.IsNullOrEmpty(settingName))
  257. {
  258. throw new GameFrameworkException("Setting name is invalid.");
  259. }
  260. m_SettingHelper.SetInt(settingName, value);
  261. }
  262. /// <summary>
  263. /// 从指定游戏配置项中读取浮点数值。
  264. /// </summary>
  265. /// <param name="settingName">要获取游戏配置项的名称。</param>
  266. /// <returns>读取的浮点数值。</returns>
  267. public float GetFloat(string settingName)
  268. {
  269. if (m_SettingHelper == null)
  270. {
  271. throw new GameFrameworkException("Setting helper is invalid.");
  272. }
  273. if (string.IsNullOrEmpty(settingName))
  274. {
  275. throw new GameFrameworkException("Setting name is invalid.");
  276. }
  277. return m_SettingHelper.GetFloat(settingName);
  278. }
  279. /// <summary>
  280. /// 从指定游戏配置项中读取浮点数值。
  281. /// </summary>
  282. /// <param name="settingName">要获取游戏配置项的名称。</param>
  283. /// <param name="defaultValue">当指定的游戏配置项不存在时,返回此默认值。</param>
  284. /// <returns>读取的浮点数值。</returns>
  285. public float GetFloat(string settingName, float defaultValue)
  286. {
  287. if (m_SettingHelper == null)
  288. {
  289. throw new GameFrameworkException("Setting helper is invalid.");
  290. }
  291. if (string.IsNullOrEmpty(settingName))
  292. {
  293. throw new GameFrameworkException("Setting name is invalid.");
  294. }
  295. return m_SettingHelper.GetFloat(settingName, defaultValue);
  296. }
  297. /// <summary>
  298. /// 向指定游戏配置项写入浮点数值。
  299. /// </summary>
  300. /// <param name="settingName">要写入游戏配置项的名称。</param>
  301. /// <param name="value">要写入的浮点数值。</param>
  302. public void SetFloat(string settingName, float value)
  303. {
  304. if (m_SettingHelper == null)
  305. {
  306. throw new GameFrameworkException("Setting helper is invalid.");
  307. }
  308. if (string.IsNullOrEmpty(settingName))
  309. {
  310. throw new GameFrameworkException("Setting name is invalid.");
  311. }
  312. m_SettingHelper.SetFloat(settingName, value);
  313. }
  314. /// <summary>
  315. /// 从指定游戏配置项中读取字符串值。
  316. /// </summary>
  317. /// <param name="settingName">要获取游戏配置项的名称。</param>
  318. /// <returns>读取的字符串值。</returns>
  319. public string GetString(string settingName)
  320. {
  321. if (m_SettingHelper == null)
  322. {
  323. throw new GameFrameworkException("Setting helper is invalid.");
  324. }
  325. if (string.IsNullOrEmpty(settingName))
  326. {
  327. throw new GameFrameworkException("Setting name is invalid.");
  328. }
  329. return m_SettingHelper.GetString(settingName);
  330. }
  331. /// <summary>
  332. /// 从指定游戏配置项中读取字符串值。
  333. /// </summary>
  334. /// <param name="settingName">要获取游戏配置项的名称。</param>
  335. /// <param name="defaultValue">当指定的游戏配置项不存在时,返回此默认值。</param>
  336. /// <returns>读取的字符串值。</returns>
  337. public string GetString(string settingName, string defaultValue)
  338. {
  339. if (m_SettingHelper == null)
  340. {
  341. throw new GameFrameworkException("Setting helper is invalid.");
  342. }
  343. if (string.IsNullOrEmpty(settingName))
  344. {
  345. throw new GameFrameworkException("Setting name is invalid.");
  346. }
  347. return m_SettingHelper.GetString(settingName, defaultValue);
  348. }
  349. /// <summary>
  350. /// 向指定游戏配置项写入字符串值。
  351. /// </summary>
  352. /// <param name="settingName">要写入游戏配置项的名称。</param>
  353. /// <param name="value">要写入的字符串值。</param>
  354. public void SetString(string settingName, string value)
  355. {
  356. if (m_SettingHelper == null)
  357. {
  358. throw new GameFrameworkException("Setting helper is invalid.");
  359. }
  360. if (string.IsNullOrEmpty(settingName))
  361. {
  362. throw new GameFrameworkException("Setting name is invalid.");
  363. }
  364. m_SettingHelper.SetString(settingName, value);
  365. }
  366. /// <summary>
  367. /// 从指定游戏配置项中读取对象。
  368. /// </summary>
  369. /// <typeparam name="T">要读取对象的类型。</typeparam>
  370. /// <param name="settingName">要获取游戏配置项的名称。</param>
  371. /// <returns>读取的对象。</returns>
  372. public T GetObject<T>(string settingName)
  373. {
  374. if (m_SettingHelper == null)
  375. {
  376. throw new GameFrameworkException("Setting helper is invalid.");
  377. }
  378. if (string.IsNullOrEmpty(settingName))
  379. {
  380. throw new GameFrameworkException("Setting name is invalid.");
  381. }
  382. return m_SettingHelper.GetObject<T>(settingName);
  383. }
  384. /// <summary>
  385. /// 从指定游戏配置项中读取对象。
  386. /// </summary>
  387. /// <param name="objectType">要读取对象的类型。</param>
  388. /// <param name="settingName">要获取游戏配置项的名称。</param>
  389. /// <returns>读取的对象。</returns>
  390. public object GetObject(Type objectType, string settingName)
  391. {
  392. if (m_SettingHelper == null)
  393. {
  394. throw new GameFrameworkException("Setting helper is invalid.");
  395. }
  396. if (objectType == null)
  397. {
  398. throw new GameFrameworkException("Object type is invalid.");
  399. }
  400. if (string.IsNullOrEmpty(settingName))
  401. {
  402. throw new GameFrameworkException("Setting name is invalid.");
  403. }
  404. return m_SettingHelper.GetObject(objectType, settingName);
  405. }
  406. /// <summary>
  407. /// 从指定游戏配置项中读取对象。
  408. /// </summary>
  409. /// <typeparam name="T">要读取对象的类型。</typeparam>
  410. /// <param name="settingName">要获取游戏配置项的名称。</param>
  411. /// <param name="defaultObj">当指定的游戏配置项不存在时,返回此默认对象。</param>
  412. /// <returns>读取的对象。</returns>
  413. public T GetObject<T>(string settingName, T defaultObj)
  414. {
  415. if (m_SettingHelper == null)
  416. {
  417. throw new GameFrameworkException("Setting helper is invalid.");
  418. }
  419. if (string.IsNullOrEmpty(settingName))
  420. {
  421. throw new GameFrameworkException("Setting name is invalid.");
  422. }
  423. return m_SettingHelper.GetObject(settingName, defaultObj);
  424. }
  425. /// <summary>
  426. /// 从指定游戏配置项中读取对象。
  427. /// </summary>
  428. /// <param name="objectType">要读取对象的类型。</param>
  429. /// <param name="settingName">要获取游戏配置项的名称。</param>
  430. /// <param name="defaultObj">当指定的游戏配置项不存在时,返回此默认对象。</param>
  431. /// <returns>读取的对象。</returns>
  432. public object GetObject(Type objectType, string settingName, object defaultObj)
  433. {
  434. if (m_SettingHelper == null)
  435. {
  436. throw new GameFrameworkException("Setting helper is invalid.");
  437. }
  438. if (objectType == null)
  439. {
  440. throw new GameFrameworkException("Object type is invalid.");
  441. }
  442. if (string.IsNullOrEmpty(settingName))
  443. {
  444. throw new GameFrameworkException("Setting name is invalid.");
  445. }
  446. return m_SettingHelper.GetObject(objectType, settingName, defaultObj);
  447. }
  448. /// <summary>
  449. /// 向指定游戏配置项写入对象。
  450. /// </summary>
  451. /// <typeparam name="T">要写入对象的类型。</typeparam>
  452. /// <param name="settingName">要写入游戏配置项的名称。</param>
  453. /// <param name="obj">要写入的对象。</param>
  454. public void SetObject<T>(string settingName, T obj)
  455. {
  456. if (m_SettingHelper == null)
  457. {
  458. throw new GameFrameworkException("Setting helper is invalid.");
  459. }
  460. if (string.IsNullOrEmpty(settingName))
  461. {
  462. throw new GameFrameworkException("Setting name is invalid.");
  463. }
  464. m_SettingHelper.SetObject(settingName, obj);
  465. }
  466. /// <summary>
  467. /// 向指定游戏配置项写入对象。
  468. /// </summary>
  469. /// <param name="settingName">要写入游戏配置项的名称。</param>
  470. /// <param name="obj">要写入的对象。</param>
  471. public void SetObject(string settingName, object obj)
  472. {
  473. if (m_SettingHelper == null)
  474. {
  475. throw new GameFrameworkException("Setting helper is invalid.");
  476. }
  477. if (string.IsNullOrEmpty(settingName))
  478. {
  479. throw new GameFrameworkException("Setting name is invalid.");
  480. }
  481. m_SettingHelper.SetObject(settingName, obj);
  482. }
  483. }
  484. }