public static void SaveServer(Data_server pData) { BinaryFormatter br = new BinaryFormatter(); string path = MonoData_Tools.pathHDDServer + "server"; if (!File.Exists(path)) { (new FileInfo(path)).Directory.Create(); } //string path = Application.DataPath + strDatasFolder + strPathServerData + "/ListClient.data"; //Debug.Log("Client List path " + path); FileStream stream = new FileStream(path, FileMode.Create); br.Serialize(stream, pData); stream.Close(); }
public static Data_server LoadServer() { Debug.LogWarning("In load server"); //on saisie la localisation du fichier a charger string path = MonoData_Tools.pathHDDServer + "server"; if (!File.Exists(path)) { (new FileInfo(path)).Directory.Create(); Debug.LogWarning("Folder has been created at " + path); } else { Debug.LogWarning("Folder is already exist at" + path); } ///Si le fichier existe bien alors if (File.Exists(path)) { //On déclare un BinaryFormatter qui va servir a deserialiser nos donnée BinaryFormatter formatter = new BinaryFormatter(); //Le FileStream va nous permettre dans ce cas d'ouvrir un fichier ou sont //stocker les donner a retourner FileStream stream = new FileStream(path, FileMode.Open); //Nous déclarons un PlayerData a qui l'ont assigne le FileStream deserialiser par le BinaryFormatter Data_server data = formatter.Deserialize(stream) as Data_server; //Toujours refermer le fichier! stream.Close(); //on retourne le PlayerData ou l'ont va extraire les donner pour les affecter a un personnage return(data); } ///si le fichier n'existe pas on retourne NULL else { Debug.Log("Save file not found in" + path); return(new Data_server()); } }
/* this function will test if we can register the new client trying to register, we return false if the id and the username are already used, * or if the username is empty */ public static bool RegisterPlayer(ClientData _playerToRegister, Data_server data_Server) { ClientData currentClientToAdd = new ClientData(_playerToRegister); List <ClientData> copyOfTheList = data_Server.ClientRegistered; currentClientToAdd.ID = data_Server.CountIDUnique + 1; for (int i = 0; i < copyOfTheList.Count; i++) { /* check if the username has not already been chosen */ if (copyOfTheList[i].Username == currentClientToAdd.Username) { Debug.LogWarning("i : " + i + "username : "******" already exist, you must enter another one" + " you entered : " + currentClientToAdd.Username); return(false); } /* check if the id isnt already used */ if (copyOfTheList[i].ID == currentClientToAdd.ID) { Debug.LogWarning("id : " + currentClientToAdd.ID + " already exists"); return(false); } #region Username Verifiaction 1 for now if (string.IsNullOrEmpty(currentClientToAdd.Username)) { Debug.LogWarning("Invalid username : "******" try another one"); return(false); } #endregion } /* we can now add PEACEFULLY the player to the list */ data_Server.CountIDUnique++; data_Server.ClientRegistered.Add(currentClientToAdd); Debug.Log("player successfully registered"); //WriteAllClientData(currentClientToAdd); //SaveListClient(); SaveServer(data_Server); currentClientToAdd = null; return(true); }