//----------------------------------------------------------------------------- // FileHelper.cs // // Microsoft XNA Community Game Platform // Copyright (C) Microsoft Corporation. All rights reserved. //----------------------------------------------------------------------------- using Microsoft.Xna.Framework; using System; using System.Collections.Generic; using System.IO; using System.Text; using System.Threading; #if GAMERSERVICES using Microsoft.Xna.Framework.GamerServices; #endif using RacingGame.Graphics; using Microsoft.Xna.Framework.Graphics; namespace RacingGame.Helpers { /// /// File helper class to get text lines, number of text lines, etc. /// Update: Now also supports the XNA Storage classes :) /// public static class FileHelper { /// /// Load game content file, returns null if file was not found. /// /// Relative filename /// File stream public static Stream LoadGameContentFile(string relativeFilename) { return TitleContainer.OpenStream(relativeFilename); } public static ManualResetEvent StorageContainerMRE = new ManualResetEvent(true); #if GAMERSERVICES /// /// XNA user device, asks for the saving location on the Xbox360, /// theirfore remember this device for the time we run the game. /// static StorageDevice xnaUserDevice = null; /// /// Xna user device /// /// Storage device public static StorageDevice XnaUserDevice { get { if ((xnaUserDevice != null) && !xnaUserDevice.IsConnected) { xnaUserDevice = null; } // Create if not created yet. if (xnaUserDevice == null) { if (Guide.IsVisible) { return null; } IAsyncResult async = StorageDevice.BeginShowSelector(PlayerIndex.One, null, null); async.AsyncWaitHandle.WaitOne(); xnaUserDevice = StorageDevice.EndShowSelector(async); #if XBOX360 if (!Guide.IsVisible) { BaseGame.GamerServicesComponent.Update(new GameTime()); } if (Guide.IsVisible) { Task.Delay(10).Wait(); BaseGame.GamerServicesComponent.Update(new GameTime()); BaseGame.graphicsManager.GraphicsDevice.Clear(Color.Black); BaseGame.graphicsManager.GraphicsDevice.Present(); } #endif } return xnaUserDevice; } } #endif /// /// Returns the number of text lines we got in a file. /// static public string[] GetLines(string filename) { try { using (StreamReader reader = new StreamReader( new FileStream(filename, FileMode.Open, FileAccess.Read), System.Text.Encoding.UTF8)) { // Generic version List lines = new List(); do { lines.Add(reader.ReadLine()); } while (reader.Peek() > -1); return lines.ToArray(); } } catch (FileNotFoundException) { // Failed to read, just return null! return null; } catch (DirectoryNotFoundException) { return null; } catch (IOException) { return null; } } /// /// Write vector3 to stream /// /// Writer /// Vector3 public static void WriteVector3(BinaryWriter writer, Vector3 vec) { if (writer == null) throw new ArgumentNullException("writer"); writer.Write(vec.X); writer.Write(vec.Y); writer.Write(vec.Z); } /// /// Write vector4 to stream /// /// Writer /// Vector4 public static void WriteVector4(BinaryWriter writer, Vector4 vec) { if (writer == null) throw new ArgumentNullException("writer"); writer.Write(vec.X); writer.Write(vec.Y); writer.Write(vec.Z); writer.Write(vec.W); } /// /// Write matrix to stream /// /// Writer /// Matrix public static void WriteMatrix(BinaryWriter writer, Matrix matrix) { if (writer == null) throw new ArgumentNullException("writer"); writer.Write(matrix.M11); writer.Write(matrix.M12); writer.Write(matrix.M13); writer.Write(matrix.M14); writer.Write(matrix.M21); writer.Write(matrix.M22); writer.Write(matrix.M23); writer.Write(matrix.M24); writer.Write(matrix.M31); writer.Write(matrix.M32); writer.Write(matrix.M33); writer.Write(matrix.M34); writer.Write(matrix.M41); writer.Write(matrix.M42); writer.Write(matrix.M43); writer.Write(matrix.M44); } /// /// Read vector3 from stream /// /// Reader /// Vector3 public static Vector3 ReadVector3(BinaryReader reader) { if (reader == null) throw new ArgumentNullException("reader"); return new Vector3( reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()); } /// /// Read vector4 from stream /// /// Reader /// Vector4 public static Vector4 ReadVector4(BinaryReader reader) { if (reader == null) throw new ArgumentNullException("reader"); return new Vector4( reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()); } /// /// Read matrix from stream /// /// Reader /// Matrix public static Matrix ReadMatrix(BinaryReader reader) { if (reader == null) throw new ArgumentNullException("reader"); return new Matrix( reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()); } } }