The Profile class can be mostly anything you want it to be. In the case of Galactic Wars, I use it to store the game data for each player. You will notice that I append the gamer’s gamertag to the profile path due to how storage locations are handled in Windows. The idea is straight forward though: for each gamer, look for a profile file and load it if it exists. If it doesn’t exist, create one and then save it.
You will notice that I am using a peculiar attribute (XmlNodeAttribute) on my class and properties. This is from a new version of Focused Games Framework which has a redesigned XmlProvider.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | using System; using System.IO; using System.Xml; using System.ComponentModel; using FocusedGames.Xml; using Microsoft.Xna.Framework.Storage; using Microsoft.Xna.Framework.GamerServices; namespace FocusedGames.GalacticWars.Data { [XmlNode("Profile", XmlTypes.Complex)] public class Profile { public const string DataFileName = "profile.xml"; public static Profile FromGamer(SignedInGamer gamer) { // TODO: Handle exception in a pretty manner. if (gamer == null) return null; IAsyncResult result = Guide.BeginShowStorageDeviceSelector(gamer.PlayerIndex, null, null); while (!result.IsCompleted) ; StorageDevice device = Guide.EndShowStorageDeviceSelector(result); StorageContainer container = device.OpenContainer("Galactic Wars"); string dataPath = Path.Combine(container.Path, gamer.Gamertag); string dataFile = Path.Combine(dataPath, DataFileName); Profile profile = null; bool doSave = false; if (File.Exists(dataFile)) { // DESERIALIZE THAT SHIZ YO! XmlProvider provider = new XmlProvider(); XmlDocument document = new XmlDocument(); document.Load(dataFile); profile = provider.Deserialize(document.SelectSingleNode("Profile")) as Profile; } else { doSave = true; profile = new Profile(); profile.GamerTag = gamer.Gamertag; profile.Credits = 15000; profile.GameVersion = GameContext.NormalizedVersion; } if (profile != null) { profile.Gamer = gamer; profile.StoragePath = dataPath; if (doSave) SaveProfile(profile); } return profile; } public static void SaveProfile(Profile profile) { XmlDocument document = new XmlDocument(); document.AppendChild(document.CreateXmlDeclaration("1.0", "UTF-8", null)); XmlProvider provider = new XmlProvider(); document.AppendChild(provider.Serialize(profile, document)); if (!Directory.Exists(profile.StoragePath)) Directory.CreateDirectory(profile.StoragePath); document.Save(Path.Combine(profile.StoragePath, DataFileName)); } public Profile() { } public SignedInGamer Gamer { get; set; } public string StoragePath { get; set; } [XmlNode("GamerTag", XmlTypes.Simple)] public string GamerTag { get; set; } [XmlNode("Credits", XmlTypes.Simple)] [TypeConverter(typeof(Int32Converter))] public int Credits { get; set; } [XmlNode("GameVersion", XmlTypes.Simple)] public string GameVersion { get; set; } } } |