using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; using System.IO; namespace Vestris.ResourceLib { /// /// Resource utilities. /// public abstract class ResourceUtil { /// /// Align an address to a 4-byte boundary. /// /// Address in memory. /// 4-byte aligned pointer. internal static IntPtr Align(Int32 p) { return new IntPtr((p + 3) & ~3); } /// /// Align a pointer to a 4-byte boundary. /// /// Pointer to an address in memory. /// 4-byte aligned pointer. internal static IntPtr Align(IntPtr p) { return Align(p.ToInt32()); } /// /// Pad data to a WORD. /// /// Binary stream. /// New position within the binary stream. internal static long PadToWORD(BinaryWriter w) { long pos = w.BaseStream.Position; if (pos % 2 != 0) { long count = 2 - pos % 2; Pad(w, (UInt16)count); pos += count; } return pos; } /// /// Pad data to a DWORD. /// /// Binary stream. /// New position within the binary stream. internal static long PadToDWORD(BinaryWriter w) { long pos = w.BaseStream.Position; if (pos % 4 != 0) { long count = 4 - pos % 4; Pad(w, (UInt16) count); pos += count; } return pos; } /// /// Returns the high WORD from a DWORD value. /// /// WORD value. /// High WORD. internal static UInt16 HiWord(UInt32 value) { return (UInt16) ((value & 0xFFFF0000) >> 16); } /// /// Returns the high WORD from a DWORD value. /// /// WORD value. /// High WORD. internal static UInt16 LoWord(UInt32 value) { return (UInt16) (value & 0x0000FFFF); } /// /// Write a value at a given position. /// Used to write a size of data in an earlier located header. /// /// Binary stream. /// Value to write. /// Address to write the value at. internal static void WriteAt(BinaryWriter w, long value, long address) { long cur = w.BaseStream.Position; w.Seek((int) address, SeekOrigin.Begin); w.Write((UInt16) value); w.Seek((int) cur, SeekOrigin.Begin); } /// /// Pad bytes. /// /// Binary stream. /// Number of bytes to write. /// New position within the stream. internal static long Pad(BinaryWriter w, UInt16 len) { while (len-- > 0) w.Write((byte) 0); return w.BaseStream.Position; } /// /// Neutral language ID. /// public static UInt16 NEUTRALLANGID { get { return MAKELANGID(Kernel32.LANG_NEUTRAL, Kernel32.SUBLANG_NEUTRAL); } } /// /// US-English language ID. /// public static UInt16 USENGLISHLANGID { get { return ResourceUtil.MAKELANGID(Kernel32.LANG_ENGLISH, Kernel32.SUBLANG_ENGLISH_US); } } /// /// Make a language ID from a primary language ID (low-order 10 bits) and a sublanguage (high-order 6 bits). /// /// Primary language ID. /// Sublanguage ID. /// Microsoft language ID. public static UInt16 MAKELANGID(int primary, int sub) { return (UInt16) ((((UInt16)sub) << 10) | ((UInt16)primary)); } /// /// Return the primary language ID from a Microsoft language ID. /// /// Microsoft language ID /// primary language ID (low-order 10 bits) public static UInt16 PRIMARYLANGID(UInt16 lcid) { return (UInt16) (((UInt16)lcid) & 0x3ff); } /// /// Return the sublanguage ID from a Microsoft language ID. /// /// Microsoft language ID. /// Sublanguage ID (high-order 6 bits). public static UInt16 SUBLANGID(UInt16 lcid) { return (UInt16) (((UInt16)lcid) >> 10); } /// /// Returns the memory representation of an object. /// /// Object type. /// Data. /// Object's representation in memory. internal static byte[] GetBytes(T anything) { int rawsize = Marshal.SizeOf(anything); IntPtr buffer = Marshal.AllocHGlobal(rawsize); Marshal.StructureToPtr(anything, buffer, false); byte[] rawdatas = new byte[rawsize]; Marshal.Copy(buffer, rawdatas, 0, rawsize); Marshal.FreeHGlobal(buffer); return rawdatas; } /// /// Get a collection of flags from a flag value. /// /// Flag collection type. /// Flag value. /// Collection of flags. internal static List FlagsToList(UInt32 flagValue) { List flags = new List(); foreach (T f in Enum.GetValues(typeof(T))) { UInt32 f_ui = Convert.ToUInt32(f); if ((flagValue & f_ui) > 0 || flagValue == f_ui) { flags.Add(f.ToString()); } } return flags; } /// /// Get a string representation of flags. /// /// Flag collection type. /// Flag vlaue /// String representation of flags in the f1 | ... | fn format. internal static string FlagsToString(UInt32 flagValue) { List flags = new List(); flags.AddRange(FlagsToList(flagValue)); return String.Join(" | ", flags.ToArray()); } } }