// Reload all profiles found in the Config folder. public void ReloadDefaultProfiles() { _profiles = new List <ILugusConfigProfile>(); _systemProfile = null; _currentUser = null; // TODO: in the case of playerprefs, we have to save a separate playerprefs key indicating which profiles are available // for now, we just take System _systemProfile = new LugusConfigProfileDefault("System", new LugusConfigProviderPlayerPrefs("System")); _systemProfile.Load(); _profiles.Add(_systemProfile); string lastestUser = _systemProfile.GetString("User.Latest", string.Empty); if (!string.IsNullOrEmpty(lastestUser)) { _currentUser = new LugusConfigProfileDefault(lastestUser, new LugusConfigProviderPlayerPrefs(lastestUser)); } else { _currentUser = new LugusConfigProfileDefault("Player", new LugusConfigProviderPlayerPrefs("Player")); } _currentUser.Load(); _profiles.Add(_currentUser); }
public static bool GUISlider(this ILugusConfigProfile profile, string configName, float from, float to, string defaultValue = "0") { bool changed = false; GUILayout.BeginHorizontal(); GUILayout.Label(configName, GUILayout.Width(50)); string content = LugusConfigUtilHelper.use.dataStore.GetString(configName, profile.GetString(configName, defaultValue)); /*string contentNew =*/ GUILayout.Label(content, GUILayout.Width(50)); float contentFloatNew = GUILayout.HorizontalSlider(Mathf.Clamp(float.Parse(content), from, to), from, to, GUILayout.Width(100)); LugusConfigUtilHelper.use.dataStore.SetString(configName, "" + contentFloatNew, true); if (GUILayout.Button("set", GUILayout.Width(40))) { profile.SetString(configName, "" + contentFloatNew, true); changed = true; } GUILayout.EndHorizontal(); return(changed); }
void GUIEditProfileBox(int xPos, int yPos, ILugusConfigProfile profile) { // Draw the actions for the profile yPos += 20; GUI.Box(new Rect(xPos, yPos, BoxWidth, 120), "Edit profile"); yPos += 20; string[] editModeStrings = { "String", "Numerical" }; _profileEditMode = GUI.Toolbar(new Rect(xPos, yPos, BoxWidth, 20), _profileEditMode, editModeStrings); yPos += 20; _profileKeyString = GUI.TextField(new Rect(xPos, yPos, BoxWidth, 20), _profileKeyString); // Either allow a string value-field, or a numerical one switch (_profileEditMode) { case 0: yPos += 20; _profileStringValue = GUI.TextField(new Rect(xPos, yPos, BoxWidth, 20), _profileStringValue); break; case 1: yPos += 20; _profilenumericalValue = GUI.HorizontalSlider(new Rect(xPos, yPos, BoxWidth / 2, 20), _profilenumericalValue, 0.0f, 100.0f); GUI.Label(new Rect(xPos + BoxWidth / 2 + 20, yPos, BoxWidth / 2, 20), _profilenumericalValue.ToString()); break; } yPos += 20; _profileOverwrite = GUI.Toggle(new Rect(xPos, yPos, BoxWidth, 20), _profileOverwrite, "Overwrite?"); yPos += 20; if (GUI.Button(new Rect(xPos, yPos, BoxWidth / 2, 20), "Add")) { switch (_profileEditMode) { case 0: profile.SetString(_profileKeyString, _profileStringValue, _profileOverwrite); break; case 1: profile.SetFloat(_profileKeyString, _profilenumericalValue, _profileOverwrite); break; } } if (GUI.Button(new Rect(xPos + BoxWidth / 2, yPos, BoxWidth / 2, 20), "Remove")) profile.Remove(_profileKeyString); }
public static bool GUIStringInput(this ILugusConfigProfile profile, string configName, string defaultValue = "") { //Debug.Log ("GUIStringInput for profile " + profile.Name); bool changed = false; GUILayout.BeginHorizontal(); GUILayout.Label(configName, GUILayout.Width(50)); string content = LugusConfigUtilHelper.use.dataStore.GetString(configName, profile.GetString(configName, defaultValue)); string contentNew = GUILayout.TextField(content, GUILayout.Width(150)); LugusConfigUtilHelper.use.dataStore.SetString(configName, contentNew, true); if (GUILayout.Button("set", GUILayout.Width(40))) { profile.SetString(configName, contentNew, true); changed = true; } GUILayout.EndHorizontal(); return(changed); }
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(); } }
// Draws the layout of the system profile void GUIProfileBox(int xPos, int yPos, ILugusConfigProfile profile) { // Draw the properties of the system profile int totalItems = 1; string userName = string.Empty; LugusConfigProfileDefault profileDefault = null; if (profile != null) { profileDefault = profile as LugusConfigProfileDefault; if (profileDefault != null) { totalItems += profileDefault.Data.Count; userName = profile.Name; } } GUI.Box(new Rect(xPos, yPos, BoxWidth, totalItems * 20), "Current profile: " + userName); // Draw the system properties if (profile != null) { yPos += 20; foreach (KeyValuePair<string, string> pair in profileDefault.Data) { if (GUI.Button(new Rect(xPos, yPos, BoxWidth, 20), pair.Key + ": " + pair.Value)) _profileKeyString = pair.Key; yPos += 20; } GUIEditProfileBox(xPos, yPos, profile); } }