// Project: XnaGraphicEngine, File: FileHelper.cs // Namespace: XnaGraphicEngine.Helpers, Class: FileHelper // Path: C:\code\XnaGraphicEngine\Helpers, Author: Abi // Code lines: 137, Size of file: 4,13 KB // Creation date: 15.10.2006 09:08 // Last modified: 22.10.2006 18:03 // Generated with Commenter by abi.exDream.com #region Using directives using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Storage; using System; using System.Collections.Generic; using System.IO; using System.Text; using System.Threading; #endregion namespace XnaGraphicEngine.Helpers { /// /// File helper class to get text lines, number of text lines, etc. /// Update: Now also supports the XNA Storage classes :) /// public sealed class FileHelper { #region Constructor /// /// Private constructor to prevent instantiation. /// private FileHelper() { } // FileHelper() #endregion #region CreateGameContentFile /// /// Create game content file, will create file if it does not exist. /// Else the existing file is just loaded. /// /// Relative filename /// File stream public static FileStream CreateGameContentFile(string relativeFilename, bool createNew) { string fullPath = Path.Combine( StorageContainer.TitleLocation, relativeFilename); return File.Open(fullPath, createNew ? FileMode.Create : FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite); } // CreateGameContentFile(relativeFilename) #endregion #region LoadGameContentFile /// /// Load game content file, returns null if file was not found. /// /// Relative filename /// File stream public static FileStream LoadGameContentFile(string relativeFilename) { string fullPath = Path.Combine( StorageContainer.TitleLocation, relativeFilename); if (File.Exists(fullPath) == false) return null; else return File.Open(fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); } // LoadGameContentFile(relativeFilename) #endregion #region SaveGameContentFile public static FileStream SaveGameContentFile(string relativeFilename) { string fullPath = Path.Combine( StorageContainer.TitleLocation, relativeFilename); return File.Open(fullPath, FileMode.Create, FileAccess.Write); } // SaveGameContentFile(relativeFilename) #endregion #region OpenOrCreateFileForCurrentPlayer /// /// 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 { // Create if not created yet. if (xnaUserDevice == null) xnaUserDevice = StorageDevice.ShowStorageDeviceGuide(PlayerIndex.One); return xnaUserDevice; } // get } // XnaUserDevice /// /// Open or create file for current player. Basically just creates a /// FileStream using the specified FileMode flag, but on the Xbox360 /// we have to ask the user first where he wants to. /// Basically used for the GameSettings and the Log class. /// /// Filename /// Mode /// Access /// File stream public static FileStream OpenFileForCurrentPlayer( string filename, FileMode mode, FileAccess access) { // Open a storage container StorageContainer container = XnaUserDevice.OpenContainer("RacingGame"); // Add the container path to our filename string fullFilename = Path.Combine(container.Path, filename); // Opens or creates the requested file return new FileStream( fullFilename, mode, access, FileShare.ReadWrite); } // OpenFileForCurrentPlayer(filename, mode, access) #endregion #region Get text lines /// /// Returns the number of text lines we got in a file. /// static public string[] GetLines(string filename) { try { 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); reader.Close(); return lines.ToArray(); } // try catch (FileNotFoundException) { // Failed to read, just return null! return null; } // catch catch (DirectoryNotFoundException) { return null; } // catch catch (IOException) { return null; } // catch } // GetLines(filename) #endregion #region Create text file /// /// Create text file /// /// Filename /// Text for file /// /// Will be thrown if file already exists. /// public static void CreateTextFile( string filename, string textForFile, Encoding fileEncoding) { StreamWriter textWriter = new StreamWriter( new FileStream(filename, FileMode.Create, FileAccess.Write), fileEncoding);//System.Text.Encoding.UTF8); string[] textLines = StringHelper.SplitMultilineText(textForFile); foreach (string line in textLines) textWriter.WriteLine(line); textWriter.Close(); } // CreateTextFile(filename, textForFile) #endregion #region Write Helpers /// /// 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); } // WriteVector3(writer, vec) /// /// 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); } // WriteVector4(writer, vec) /// /// 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); } // WriteMatrix(writer, matrix) #endregion #region Read Helpers /// /// 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()); } // ReadVector3(reader) /// /// 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()); } // ReadVector4(reader) /// /// 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()); } // ReadMatrix(reader) #endregion } // class FileHelper } // namespace XnaGraphicEngine.Helpers