JsonDataEditor.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Text;
  7. using UnityEngine;
  8. using XRTool.Util;
  9. namespace PublicTools.XMLDataBase
  10. {
  11. /// <summary>
  12. /// Json数据编辑
  13. /// </summary>
  14. public class JsonDataEditor : TableInterface
  15. {
  16. private string filePath;
  17. private FileStream fileStream;
  18. private StreamReader reader;
  19. private StreamWriter writer;
  20. public JsonDataEditor()
  21. {
  22. }
  23. public JsonDataEditor(string path)
  24. {
  25. this.filePath = path;
  26. }
  27. public void Close()
  28. {
  29. if (writer != null)
  30. {
  31. writer.Flush();
  32. writer.Close();
  33. }
  34. if (reader != null)
  35. {
  36. reader.Close();
  37. }
  38. if (fileStream != null)
  39. {
  40. fileStream.Close();
  41. }
  42. fileStream = null;
  43. reader = null;
  44. writer = null;
  45. filePath = null;
  46. }
  47. /// <summary>
  48. /// 创建一个Json类型的数据
  49. /// </summary>
  50. /// <param name="tableName"></param>
  51. /// <returns></returns>
  52. public bool Create(string tableName)
  53. {
  54. return false;
  55. }
  56. public bool DeleteAllData<T>()
  57. {
  58. throw new System.NotImplementedException();
  59. }
  60. public bool DeleteData<T>(string key)
  61. {
  62. throw new System.NotImplementedException();
  63. }
  64. public List<T> FindAllData<T>()
  65. {
  66. throw new System.NotImplementedException();
  67. }
  68. /// <summary>
  69. /// 查找所有对象
  70. /// </summary>
  71. /// <typeparam name="T"></typeparam>
  72. /// <returns></returns>
  73. public List<T> FindAllData<T>(string data)
  74. {
  75. return null;
  76. }
  77. public List<T> FindAllData<T>(byte[] data)
  78. {
  79. throw new NotImplementedException();
  80. }
  81. public T FindData<T>(string key)
  82. {
  83. throw new System.NotImplementedException();
  84. }
  85. public bool InsertData<T>(T t)
  86. {
  87. string data = JsonConvert.SerializeObject(t);
  88. string prikey = data.Split(',')[0];
  89. string jData = null;
  90. while (!string.IsNullOrEmpty(jData = reader.ReadLine()))
  91. {
  92. string tmpKey = jData.Split(',')[0];
  93. if (tmpKey == prikey)
  94. {
  95. return UpdateData(t);
  96. }
  97. }
  98. writer.WriteLine(data);
  99. return false;
  100. }
  101. public bool InsertData<T>(T data, string priKey)
  102. {
  103. throw new NotImplementedException();
  104. }
  105. public bool Open()
  106. {
  107. return Open(filePath);
  108. }
  109. public bool Open(string path)
  110. {
  111. Close();
  112. if (File.Exists(path))
  113. {
  114. try
  115. {
  116. fileStream = new FileStream(path, FileMode.Open);
  117. reader = new StreamReader(fileStream, UnicodeEncoding.Default);
  118. writer = new StreamWriter(fileStream, UnicodeEncoding.Default);
  119. return fileStream.CanRead;
  120. }
  121. catch (Exception ex)
  122. {
  123. UnityLog.Instance.LogException(ex);
  124. UnityLog.Instance.LogError(path + ex.ToString());
  125. }
  126. }
  127. return false;
  128. }
  129. public void Save()
  130. {
  131. throw new NotImplementedException();
  132. }
  133. public bool UpdateData<T>(T data)
  134. {
  135. return false;
  136. }
  137. public bool UpdateData<T>(T data, string key)
  138. {
  139. throw new NotImplementedException();
  140. }
  141. }
  142. }