/// <summary> /// Class constructor. /// </summary> /// <param name="erfType">The type of the ERF file</param> public ErfHeader(ErfType erfType) { string s = erfType.ToString(); type = new byte[] { (byte) s[0], (byte) s[1], (byte) s[2], (byte) ' ' }; version = new byte[] { (byte) 'V', (byte) '1', (byte) '.', (byte) '0' }; languageCount = 0; localizedStringSize = 0; entryCount = 0; offsetToLocalizedString = 0; offsetToKeyList = 0; offsetToResourceList = 0; buildYear = DateTime.Today.Year; buildDay = DateTime.Today.DayOfYear; descriptionStrRef = 0; pad = new byte[116]; }
/// <summary> /// Deserializes a number of ErfString objects from the passed stream, /// placing them into an array. /// </summary> /// <param name="s">The stream</param> /// <param name="count">The number of strings to deserialize</param> /// <param name="type">The type of ERF the string is coming from, some ERFs /// have null terminated strings and some do not</param> /// <returns>An ErfString array with the strings</returns> public static ErfString[] Deserialize(Stream s, int count, ErfType type) { ErfString[] estrings = new ErfString[count]; for (int i = 0; i < count; i++) estrings[i] = new ErfString(s, type); return estrings; }
/// <summary> /// Class constructor /// </summary> /// <param name="s">The string value</param> /// <param name="type">The type of ERF the string is coming from, some ERFs /// have null terminated strings and some do not</param> public ErfString(string s, ErfType type) { // 0 is English. languageID = (Int32) LanguageID.English; val = s; this.type = type; }
/// <summary> /// Class constructor to deserialize an ErfString. /// </summary> /// <param name="s">The stream containing the raw data.</param> /// <param name="type">The type of ERF the string is coming from, some ERFs /// have null terminated strings and some do not</param> public ErfString(Stream s, ErfType type) { // Read the language ID from the stream. byte[] buffer = new Byte[4]; if (buffer.Length != s.Read(buffer, 0, buffer.Length)) throw new NWNException("Invalid erf string in stream"); languageID = BitConverter.ToInt32(buffer, 0); // Read the number of bytes in the string from the stream. if (buffer.Length != s.Read(buffer, 0, buffer.Length)) throw new NWNException("Invalid erf string in stream"); Int32 size = BitConverter.ToInt32(buffer, 0); // Read the string bytes from the stream. buffer = new byte[size]; if (buffer.Length != s.Read(buffer, 0, buffer.Length)) throw new NWNException("Invalid erf string in stream"); val = RawSerializer.DeserializeString(buffer); this.type = type; }
/// <summary> /// Creates a new, empty ERF file of the specified type. /// </summary> /// <param name="type">The type of the ERF</param> /// <param name="description">The ERF's description</param> /// <returns></returns> public static Erf New(ErfType type, string description) { // Create the ERF file and it's header. Erf erf = new Erf(); erf.header = new ErfHeader(type); // Create empty key/resource files since an empty ERF contains 0 files. erf.keys = new ErfKey[0]; erf.resources = new ErfResource[0]; // Create an ErfString for the description. erf.descriptions = new ErfString[1]; erf.descriptions[0] = new ErfString(description, type); // Setup the header to account for our description. erf.header.LanguageCount = 1; erf.header.LocalizedStringSize = erf.descriptions[0].SizeInStream; erf.header.OffsetToLocalizedString = Marshal.SizeOf(typeof(ErfHeader)); erf.header.OffsetToKeyList = erf.header.OffsetToLocalizedString + erf.header.LocalizedStringSize; return erf; }