123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- //================================================================================
- //
- //================================================================================
- using UnityEngine;
- using System.Collections;
- using System;
- //================================================================================
- //
- //================================================================================
- namespace ReaderRabbit
- {
- //================================================================================
- //
- //================================================================================
- public class AllPlayers
- {
- //================================================================================
- // Singleton
- //================================================================================
- private static AllPlayers s_Instance = null;
- public static AllPlayers Instance()
- {
- if (s_Instance == null)
- s_Instance = new AllPlayers();
- return s_Instance;
- }
-
- //================================================================================
- //
- //================================================================================
- const int PLAYERS_MAX = 99;
- const string ALL_PLAYERS_FILENAME = "_all_.lst";
- private string[] m_ListOfPlayers;
- private int m_IndexForNewPlayer;
- //================================================================================
- //
- //================================================================================
- public AllPlayers()
- {
- m_ListOfPlayers = new string[PLAYERS_MAX];
- for (int i = 0; i < PLAYERS_MAX; ++i)
- m_ListOfPlayers[i] = "";
- m_IndexForNewPlayer = -1;
- }
- //================================================================================
- //
- //================================================================================
- public string[] GetPlayerList()
- {
- return m_ListOfPlayers;
- }
-
- //================================================================================
- //
- //================================================================================
- public int GetIndexByPlayerName(string name)
- {
- return Array.IndexOf(m_ListOfPlayers, name);
- }
- //================================================================================
- //
- //================================================================================
- public int GetIndexForNewPlayer()
- {
- return m_IndexForNewPlayer;
- }
- //================================================================================
- //
- //================================================================================
- public void SetNewPlayerName(string name)
- {
- if (m_IndexForNewPlayer != -1)
- m_ListOfPlayers[m_IndexForNewPlayer] = name;
- }
-
- //================================================================================
- //
- //================================================================================
- public void RemovePlayer(string name)
- {
- KishiTechUnity.KishiTechDebug.KTDebug.Instance().Log("Removing player \'" + name + "\'...");
- if (!string.IsNullOrEmpty(name))
- {
- int playerIndex = GetIndexByPlayerName(name);
- m_ListOfPlayers[playerIndex] = "";
- if (ES2.Exists(name + ".dat"))
- {
- DeletePlayerImages(name);
- ES2.Delete(name + ".dat");
- }
- SaveData();
- }
- }
- //================================================================================
- //
- //================================================================================
- public void DeletePlayerImages(string name)
- {
- string path = Application.persistentDataPath + "/" + name + "/";
- if (ES2.Exists(path))
- ES2.Delete(path);
- }
-
- //================================================================================
- //
- //================================================================================
- public void LoadData()
- {
- KishiTechUnity.KishiTechDebug.KTDebug.Instance().Log("Calling AllPlayers.LoadData()...");
- KishiTechUnity.KishiTechDebug.KTDebug.Instance().Log("Persistent data path: " + Application.persistentDataPath);
-
- if (ES2.Exists(ALL_PLAYERS_FILENAME))
- {
- // Loading data sequentially (without tags) for faster save/load data - loading must be in the same sequence.
- using(ES2Reader reader = ES2Reader.Create(ALL_PLAYERS_FILENAME))
- {
- m_ListOfPlayers = reader.ReadArray<string>();
- }
- //Array.Sort(m_ListOfPlayers);
- m_IndexForNewPlayer = -1;
- for (int i = 0; i < m_ListOfPlayers.Length; ++i)
- {
- if (String.IsNullOrEmpty(m_ListOfPlayers[i]))
- {
- m_IndexForNewPlayer = i;
- break;
- }
- }
- KishiTechUnity.KishiTechDebug.KTDebug.Instance().Log("Index for new player is " + m_IndexForNewPlayer);
- }
- else
- {
- KishiTechUnity.KishiTechDebug.KTDebug.Instance().Log("File " + ALL_PLAYERS_FILENAME + " does not exist!");
- m_IndexForNewPlayer = 0;
- KishiTechUnity.KishiTechDebug.KTDebug.Instance().Log("Index for new player is " + m_IndexForNewPlayer);
- }
- }
- //================================================================================
- //
- //================================================================================
- public void SaveData()
- {
- KishiTechUnity.KishiTechDebug.KTDebug.Instance().Log("Calling AllPlayers.SaveData()...");
- KishiTechUnity.KishiTechDebug.KTDebug.Instance().Log("Persistent data path: " + Application.persistentDataPath);
- //Array.Sort(m_ListOfPlayers);
- // Saving data sequentially (without tags) for faster save/load data - loading must be in the same sequence.
- using(ES2Writer writer = ES2Writer.Create(ALL_PLAYERS_FILENAME))
- {
- writer.Write(m_ListOfPlayers);
- writer.Save(false); // false means we are not using tags, since we are saving data sequentially.
- }
- }
- } // public class AllPlayers
- } // namespace ReaderRabbit
|