AllPlayers.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. //================================================================================
  2. //
  3. //================================================================================
  4. using UnityEngine;
  5. using System.Collections;
  6. using System;
  7. //================================================================================
  8. //
  9. //================================================================================
  10. namespace ReaderRabbit
  11. {
  12. //================================================================================
  13. //
  14. //================================================================================
  15. public class AllPlayers
  16. {
  17. //================================================================================
  18. // Singleton
  19. //================================================================================
  20. private static AllPlayers s_Instance = null;
  21. public static AllPlayers Instance()
  22. {
  23. if (s_Instance == null)
  24. s_Instance = new AllPlayers();
  25. return s_Instance;
  26. }
  27. //================================================================================
  28. //
  29. //================================================================================
  30. const int PLAYERS_MAX = 99;
  31. const string ALL_PLAYERS_FILENAME = "_all_.lst";
  32. private string[] m_ListOfPlayers;
  33. private int m_IndexForNewPlayer;
  34. //================================================================================
  35. //
  36. //================================================================================
  37. public AllPlayers()
  38. {
  39. m_ListOfPlayers = new string[PLAYERS_MAX];
  40. for (int i = 0; i < PLAYERS_MAX; ++i)
  41. m_ListOfPlayers[i] = "";
  42. m_IndexForNewPlayer = -1;
  43. }
  44. //================================================================================
  45. //
  46. //================================================================================
  47. public string[] GetPlayerList()
  48. {
  49. return m_ListOfPlayers;
  50. }
  51. //================================================================================
  52. //
  53. //================================================================================
  54. public int GetIndexByPlayerName(string name)
  55. {
  56. return Array.IndexOf(m_ListOfPlayers, name);
  57. }
  58. //================================================================================
  59. //
  60. //================================================================================
  61. public int GetIndexForNewPlayer()
  62. {
  63. return m_IndexForNewPlayer;
  64. }
  65. //================================================================================
  66. //
  67. //================================================================================
  68. public void SetNewPlayerName(string name)
  69. {
  70. if (m_IndexForNewPlayer != -1)
  71. m_ListOfPlayers[m_IndexForNewPlayer] = name;
  72. }
  73. //================================================================================
  74. //
  75. //================================================================================
  76. public void RemovePlayer(string name)
  77. {
  78. KishiTechUnity.KishiTechDebug.KTDebug.Instance().Log("Removing player \'" + name + "\'...");
  79. if (!string.IsNullOrEmpty(name))
  80. {
  81. int playerIndex = GetIndexByPlayerName(name);
  82. m_ListOfPlayers[playerIndex] = "";
  83. if (ES2.Exists(name + ".dat"))
  84. {
  85. DeletePlayerImages(name);
  86. ES2.Delete(name + ".dat");
  87. }
  88. SaveData();
  89. }
  90. }
  91. //================================================================================
  92. //
  93. //================================================================================
  94. public void DeletePlayerImages(string name)
  95. {
  96. string path = Application.persistentDataPath + "/" + name + "/";
  97. if (ES2.Exists(path))
  98. ES2.Delete(path);
  99. }
  100. //================================================================================
  101. //
  102. //================================================================================
  103. public void LoadData()
  104. {
  105. KishiTechUnity.KishiTechDebug.KTDebug.Instance().Log("Calling AllPlayers.LoadData()...");
  106. KishiTechUnity.KishiTechDebug.KTDebug.Instance().Log("Persistent data path: " + Application.persistentDataPath);
  107. if (ES2.Exists(ALL_PLAYERS_FILENAME))
  108. {
  109. // Loading data sequentially (without tags) for faster save/load data - loading must be in the same sequence.
  110. using(ES2Reader reader = ES2Reader.Create(ALL_PLAYERS_FILENAME))
  111. {
  112. m_ListOfPlayers = reader.ReadArray<string>();
  113. }
  114. //Array.Sort(m_ListOfPlayers);
  115. m_IndexForNewPlayer = -1;
  116. for (int i = 0; i < m_ListOfPlayers.Length; ++i)
  117. {
  118. if (String.IsNullOrEmpty(m_ListOfPlayers[i]))
  119. {
  120. m_IndexForNewPlayer = i;
  121. break;
  122. }
  123. }
  124. KishiTechUnity.KishiTechDebug.KTDebug.Instance().Log("Index for new player is " + m_IndexForNewPlayer);
  125. }
  126. else
  127. {
  128. KishiTechUnity.KishiTechDebug.KTDebug.Instance().Log("File " + ALL_PLAYERS_FILENAME + " does not exist!");
  129. m_IndexForNewPlayer = 0;
  130. KishiTechUnity.KishiTechDebug.KTDebug.Instance().Log("Index for new player is " + m_IndexForNewPlayer);
  131. }
  132. }
  133. //================================================================================
  134. //
  135. //================================================================================
  136. public void SaveData()
  137. {
  138. KishiTechUnity.KishiTechDebug.KTDebug.Instance().Log("Calling AllPlayers.SaveData()...");
  139. KishiTechUnity.KishiTechDebug.KTDebug.Instance().Log("Persistent data path: " + Application.persistentDataPath);
  140. //Array.Sort(m_ListOfPlayers);
  141. // Saving data sequentially (without tags) for faster save/load data - loading must be in the same sequence.
  142. using(ES2Writer writer = ES2Writer.Create(ALL_PLAYERS_FILENAME))
  143. {
  144. writer.Write(m_ListOfPlayers);
  145. writer.Save(false); // false means we are not using tags, since we are saving data sequentially.
  146. }
  147. }
  148. } // public class AllPlayers
  149. } // namespace ReaderRabbit