ProductParse.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class ProductParse : MonoBehaviour
  5. {
  6. public string ID
  7. {
  8. get
  9. {
  10. return this.m_strID;
  11. }
  12. }
  13. public string GetName()
  14. {
  15. return this.GetData(ProductParse.KEY_NAME);
  16. }
  17. public int GetPrice()
  18. {
  19. int result;
  20. try
  21. {
  22. int num = -1;
  23. string data = this.GetData(ProductParse.KEY_PRICE);
  24. if (!int.TryParse(data, out num))
  25. {
  26. throw new Exception("値段のデータに不正な値が入っている\n値:" + data);
  27. }
  28. result = num;
  29. }
  30. catch (Exception ex)
  31. {
  32. NDebug.Assert(ex.Message, false);
  33. Debug.LogError(ex.Message);
  34. result = 0;
  35. }
  36. return result;
  37. }
  38. public string GetImageName()
  39. {
  40. return this.GetData(ProductParse.KEY_IMAGE);
  41. }
  42. public string GetDescription()
  43. {
  44. return this.GetData(ProductParse.KEY_DESC);
  45. }
  46. private static void InitProductList()
  47. {
  48. if (ProductParse.m_ProductList != null)
  49. {
  50. return;
  51. }
  52. ProductParse.m_ProductList = new Dictionary<string, Dictionary<string, string>>();
  53. Dictionary<string, string> dictionary = new Dictionary<string, string>();
  54. dictionary.Add(string.Empty, string.Empty);
  55. }
  56. private Dictionary<string, string> GetDataDic()
  57. {
  58. ProductParse.InitProductList();
  59. Dictionary<string, string> result;
  60. try
  61. {
  62. if (!ProductParse.m_ProductList.ContainsKey(this.m_strID))
  63. {
  64. throw new Exception("商品ID[" + this.m_strID + "]の情報が存在しない");
  65. }
  66. result = ProductParse.m_ProductList[this.m_strID];
  67. }
  68. catch (Exception ex)
  69. {
  70. NDebug.Assert(ex.Message, false);
  71. Debug.LogError(ex.Message);
  72. result = null;
  73. }
  74. return result;
  75. }
  76. private string GetData(string key)
  77. {
  78. string result;
  79. try
  80. {
  81. Dictionary<string, string> dataDic = this.GetDataDic();
  82. if (!dataDic.ContainsKey(key))
  83. {
  84. throw new Exception("商品データのキー[" + key + "]は存在しない");
  85. }
  86. result = dataDic[key];
  87. }
  88. catch (Exception ex)
  89. {
  90. NDebug.Assert(ex.Message, false);
  91. Debug.LogError(ex.Message);
  92. result = null;
  93. }
  94. return result;
  95. }
  96. private static string KEY_NAME = "name";
  97. private static string KEY_PRICE = "price";
  98. private static string KEY_IMAGE = "image";
  99. private static string KEY_DESC = "desc";
  100. private static Dictionary<string, Dictionary<string, string>> m_ProductList;
  101. private string m_strID;
  102. }