protected List <ILugusConfigProfile> _profiles = new List <ILugusConfigProfile>(); // All profiles registered in this configuration, incl. system profile. #endregion #if !UNITY_WEBPLAYER && !UNITY_IPHONE && !UNITY_ANDROID && !UNITY_WP8 // Reload all profiles found in the Config folder. public void ReloadDefaultProfiles() { _profiles = new List <ILugusConfigProfile>(); _systemProfile = null; _currentUser = null; // Load the profiles found in the config folder of the application datapath // and try to set the latest user as the current user. // If no profiles could be found in the folder, // then create a default system and user profile. string configpath = Application.dataPath + "/Config/"; if (!Directory.Exists(configpath)) { Debug.LogWarning("LugusConfigDefault: Config folder didn't exist yet. Creating it."); Directory.CreateDirectory(configpath); } DirectoryInfo directoryInfo = new DirectoryInfo(configpath); FileInfo[] files = directoryInfo.GetFiles("*.xml"); System.DateTime mostRecentUserSaveTime = new global::System.DateTime(2000, 01, 01); if (files.Length > 0) { // Create and load profiles foreach (FileInfo fileInfo in files) { string profileName = fileInfo.Name.Remove(fileInfo.Name.LastIndexOf(".xml")); LugusConfigProfileDefault profile = new LugusConfigProfileDefault(profileName); profile.Load(); if (profileName == "System") { _systemProfile = profile; Debug.Log("LugusConfigDefault: Found system profile."); } else { Debug.Log("LugusConfigDefault: Found user profile: " + profileName); // In most cases, the System config profile will contain a reference to the latest player profile (see below), which will override this. // However, in some rare cases (System file is missing or does not contain User.Latest, the system can default to the last saved non-system profile. // mostRecentUserSaveTime.CompareTo will return -1 if new value is more recent than itself. if (mostRecentUserSaveTime.CompareTo(fileInfo.LastWriteTime) < 0) { mostRecentUserSaveTime = fileInfo.LastWriteTime; _currentUser = profile; } } _profiles.Add(profile); } } // If the system profile wasn't found, create it. if (_systemProfile == null) { Debug.LogWarning("LugusConfigDefault: A system config profile was not found. Now creating one at runtime."); LugusConfigProfileDefault sysProfile = new LugusConfigProfileDefault("System"); this.System = sysProfile; _profiles.Add(sysProfile); } // Set current user to the one saved in system profile. string lastestUser = _systemProfile.GetString("User.Latest", string.Empty); if (!string.IsNullOrEmpty(lastestUser)) { ILugusConfigProfile playerProfile = _profiles.Find(profile => profile.Name == lastestUser); if (playerProfile != null) { _currentUser = playerProfile; } else if (_currentUser != null) { // If a player file was deleted, no file can be returned. In that case, default to _currentUser's default value of the latest save (see above). Debug.LogWarning("LugusConfigDefault: The system config profile's last user value referred to a file that doesn't exist anymore. Defaulting to latest saved file instead: " + _currentUser.Name + "."); // If we're defaulting to the latest save, might as well change the latest user in the system profile to a file that does exist. _systemProfile.SetString("User.Latest", _currentUser.Name, true); _systemProfile.Store(); } } // If the system profile did not have a value for latest user, but there are other profiles available, default to the latest save (see above). // Display a warning to make developers aware of this. else if (_currentUser != null) { Debug.LogWarning("LugusConfigDefault: The system config profile did not (yet) contain a value indicating the last user. Defaulting to latest saved file instead: " + _currentUser.Name + "."); // If we're defaulting to the latest save, might as well save that name from now on. _systemProfile.SetString("User.Latest", _currentUser.Name, true); _systemProfile.Store(); } // If a current user couldn't be found at all (either listed in the system profile or by defaulting to the latest saved profile), create a new one. if (_currentUser == null) { Debug.LogWarning("LugusConfigDefault: A latest user profile was not found. Now creating one named \"Player\"."); _currentUser = new LugusConfigProfileDefault("Player"); _profiles.Add(_currentUser); // If we're creating a new user profile, might as well save it to the system profile from now on. _systemProfile.SetString("User.Latest", "Player", true); _systemProfile.Store(); } }
// Tests values in the profile by inserting and extracting // random values and comparing them. Will output a debug // error message when two values are not considered equal. bool TestRandomValues() { bool testResult = true; Random.seed = (int)Time.time; // A series of random test to put and pull values from a profile LugusConfigProfileDefault profile = new LugusConfigProfileDefault("Test"); for (int i = 0; i < 10; i++) { string key = string.Empty; float random; // Test booleans random = Random.value; bool insertBool; if (random < 0.5f) insertBool = false; else insertBool = true; key = "boolValue" + i; profile.SetBool(key, insertBool); bool extractBool = profile.GetBool(key); if (insertBool != extractBool) { testResult = false; Debug.LogError("Extracted bool value did not match the inserted value. Inserted: " + insertBool + " Extracted: " + extractBool); } // Test integers int insertInt = Random.Range(0, 100); key = "intValue" + i; profile.SetInt(key, insertInt); int extractInt = profile.GetInt(key, -1); if (insertInt != extractInt) { testResult = false; Debug.LogError("Extracted int value did not match the inserted value. Inserted: " + insertInt + " Extracted: " + extractInt); } // Test floats float insertFloat = Random.Range(0.0f, 100.0f); key = "floatValue" + i; profile.SetFloat(key, insertFloat); float extractFloat = profile.GetFloat(key, -1.0f); if (!Mathf.Approximately(insertFloat, extractFloat)) { testResult = false; Debug.LogError("Extracted float value did not match the inserted value. Inserted: " + insertFloat + " Extracted: " + extractFloat); } } return testResult; }