// When the Profiles Manager loads it sets it's values and it loads the profiles public async void Start() { SaveManagerEvents.current.OnProfileSaved += OnProfileSaved; SaveManagerEvents.current.OnProfileDeleted += OnProfileDeleted; SaveManagerEvents.current.OnProfileEdited += OnProfileEdited; SaveManagerEvents.current.OnCreateProfile += OnCreateProfile; SaveManagerEvents.current.OnAllProfilesSaved += OnAllProfilesSaved; SaveManagerEvents.current.OnAllProfilesLoaded += OnAllProfilesLoaded; SaveManagerEvents.current.OnProfileLoaded += OnProfileLoaded; //Sets the main path value for Profiles mainDirectoryPath = Path.Combine(Application.persistentDataPath, "Profiles"); //Sets the profiles.dat path value profilesPath = mainDirectoryPath + "/profiles.dat"; // Checks if the Profiles Folder Exists or not. If it doesn't exist it creates it. if (!Directory.Exists(mainDirectoryPath)) { FileManagerExtension.CreateDirectory(mainDirectoryPath); } // Loads the event FileSystemEventsManager.FileSystemEvents(mainDirectoryPath); //Gets all Directories in the Profiles folder string[] profiles = FileManagerExtension.GetDirectories(mainDirectoryPath); // Shows the Create profile UI if player doesn't have any profiles if (!profiles.Any()) { FirstTimePlaying(); await SaveManager.SaveAllProfilesAsync(profilesPath); return; } await SaveManager.LoadAllProfilesAsync(profilesPath); for (int i = 0; i < profiles.Length; i++) { await LoadProfile(profiles.GetValue(i).ToString()); } }
// This function is used to create a new profile public async void CreateProfile() { // Creates the unique profile ID string profileId = $"{Guid.NewGuid()}"; if (string.IsNullOrEmpty(profileName.text)) { Debug.Log("Profile name can't be empty or null"); return; } // Checks if the Profile already exists or not. while (Directory.Exists($"{mainDirectoryPath}{profileName.text} {profileId}") || Directory.Exists($"{mainDirectoryPath}{profileName.text}")) { profileId = $"{profileId}{Guid.NewGuid()}"; } string fullProfileName = $"{profileName.text} {profileId}"; // Sets the paths needed for the profile string profilePath = Path.Combine(mainDirectoryPath, fullProfileName); string profileSavePath = Path.Combine(profilePath, "Save"); string profileSaveFilePath = Path.Combine(profileSavePath, "data.dat"); string profileConfigPath = Path.Combine(profilePath, "config.dat"); string profileSettingsPath = Path.Combine(profilePath, "Settings.dat"); if (!Directory.Exists(mainDirectoryPath)) { FileManagerExtension.CreateDirectory(mainDirectoryPath); } FileManagerExtension.CreateDirectory(profileSavePath); FileManagerExtension.CreateFile(profileSaveFilePath); FileManagerExtension.CreateFile(profileSettingsPath); SaveManagerEvents.current.CreateProfile(profileName.text, fullProfileName, profileId); // Creates and saves the profile. await SaveManager.SaveProfileAsync(profileConfigPath, "DefaultProfilePicture.png", profileName.text, profileId, fullProfileName); }
// Save Game public static async Task SaveGameAsync(bool hasPlayed, int balance, Vector3 playerPosition, Quaternion playerRotation, Vector3 cameraPosition, Quaternion cameraRotation) { if (SelectedProfileManager.selectedProfile == null) { return; } string fullProfileName = SelectedProfileManager.selectedProfile.fullProfileName; GameData gameData = CreateGameData(hasPlayed, balance, playerPosition, playerRotation, cameraPosition, cameraRotation); if (gameData == null) { return; } string path = Path.Combine(MainDirectoryPath, fullProfileName, "Save", "data.dat"); string dirPath = Path.Combine(MainDirectoryPath, fullProfileName, "Save"); if (!Directory.Exists(dirPath)) { FileManagerExtension.CreateDirectory(dirPath); } string json = JsonUtility.ToJson(gameData); // Creates the unique Encryption key per profile string jsonEncryptedKey = "|9{Ajia:p,g<ae&)9KsLy7;<t9G5sJ>G"; // If the value is longer then 32 characters it will make it 32 characters if (jsonEncryptedKey.Length > 32) { jsonEncryptedKey = jsonEncryptedKey.Truncate(32); } // Encryption process Rijndael crypto = new Rijndael(); byte[] soup = crypto.Encrypt(json, jsonEncryptedKey); await AsyncHelperExtensions.WriteBytesAsync(path, soup); SaveManagerEvents.current.SaveGame(gameData, SelectedProfileManager.selectedProfile); }
// This function is used to Edit an already existing profile public async void EditProfile() { if (SelectedProfileManager.selectedProfile == null) { Debug.Log("You don't have any profile selected"); return; } if (string.IsNullOrEmpty(this.profileName.text)) { Debug.Log("Profile name can't be empty or null"); return; } string profileId = SelectedProfileManager.selectedProfile.profileId; string profileName = this.profileName.text; string fullProfileName = $"{this.profileName.text} {profileId}"; // Checks if the Profile Name is the same if (fullProfileName.Equals(SelectedProfileManager.selectedProfile.fullProfileName, StringComparison.OrdinalIgnoreCase)) { Debug.Log("You didn't change the profile name"); return; } FileManagerExtension.MoveDirectory(Path.Combine(mainDirectoryPath, SelectedProfileManager.selectedProfile.fullProfileName), Path.Combine(mainDirectoryPath, fullProfileName)); // Sets the paths needed for the profile string profilePath = Path.Combine(mainDirectoryPath, fullProfileName); string profileSavePath = Path.Combine(profilePath, "Save"); string profileSaveFilePath = Path.Combine(profileSavePath, "data.dat"); string profileConfigPath = Path.Combine(profilePath, "config.dat"); string profileSettingsPath = Path.Combine(profilePath, "Settings.dat"); if (!Directory.Exists(mainDirectoryPath)) { FileManagerExtension.CreateDirectory(mainDirectoryPath); } if (!Directory.Exists(profileSavePath)) { FileManagerExtension.CreateDirectory(profileSavePath); } if (!File.Exists(profileSaveFilePath)) { FileManagerExtension.CreateFile(profileSaveFilePath); } if (!File.Exists(profileSettingsPath)) { FileManagerExtension.CreateFile(profileSettingsPath); } // Deletes the old Profiles value from profiles.dat SaveManager.profiles.Remove(SaveManager.profiles.FirstOrDefault(p => p.fullProfileName.Equals(SelectedProfileManager.selectedProfile.fullProfileName))); await SaveManager.SaveAllProfilesAsync(profilesPath); if (SelectedProfileManager.selectedProfile == null) { Debug.Log("Failed to create profile reason unknown"); return; } string profileImage = SelectedProfileManager.selectedProfile.profileImage; // Creates and saves the profile. await SaveManager.SaveProfileAsync(profileConfigPath, profileImage, profileName, profileId, fullProfileName); SaveManagerEvents.current.ProfileEdited(SaveManager.CreateProfile(profileId, profileImage, profileName, fullProfileName, false)); SelectedProfileManager.selectedProfile = null; }