|
| 1 | +using Microsoft.Win32; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.IO; |
| 5 | +using System.Linq; |
| 6 | +using System.Reflection; |
| 7 | +using System.Runtime.CompilerServices; |
| 8 | +using System.Text; |
| 9 | +using System.Threading.Tasks; |
| 10 | +using System.Xml; |
| 11 | + |
| 12 | +namespace Java_Bytecode_Toolkit.ExtensionsNS |
| 13 | +{ |
| 14 | + public static class Extensions |
| 15 | + { |
| 16 | + public static void SetFilter(this FileDialog fileDialog, params FileDialogFilter[] fileDialogFilters) |
| 17 | + { |
| 18 | + string fileDialogFilterString = ""; |
| 19 | + |
| 20 | + for (int currentFilterIndex = 0; currentFilterIndex < fileDialogFilters.Length; currentFilterIndex++) |
| 21 | + { |
| 22 | + if (currentFilterIndex != 0) |
| 23 | + { |
| 24 | + fileDialogFilterString += "|"; |
| 25 | + } |
| 26 | + |
| 27 | + fileDialogFilterString += fileDialogFilters[currentFilterIndex].filterName + "|"; |
| 28 | + |
| 29 | + foreach (string fileExtension in fileDialogFilters[currentFilterIndex].fileExtensions) |
| 30 | + { |
| 31 | + fileDialogFilterString += "*." + fileExtension; |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + fileDialog.Filter = fileDialogFilterString; |
| 36 | + } |
| 37 | + |
| 38 | + public static T To<T>(this Enum enumInstance) |
| 39 | + { |
| 40 | + return (T)(object)enumInstance; |
| 41 | + } |
| 42 | + |
| 43 | + public static byte[] ReadBytes(this Stream stream, int offset, int numBytesToRead, bool isLittleEndian) |
| 44 | + { |
| 45 | + byte[] bytes = new byte[numBytesToRead]; |
| 46 | + |
| 47 | + stream.Read(bytes, 0, numBytesToRead); |
| 48 | + |
| 49 | + if (isLittleEndian != BitConverter.IsLittleEndian) |
| 50 | + { |
| 51 | + bytes = bytes.Reverse().ToArray(); |
| 52 | + } |
| 53 | + |
| 54 | + return bytes; |
| 55 | + } |
| 56 | + |
| 57 | + public static T To<T>(this byte[] byteArray) |
| 58 | + { |
| 59 | + if (typeof(T) == typeof(byte)) |
| 60 | + { |
| 61 | + return (T)(object)byteArray[0]; |
| 62 | + } |
| 63 | + else if (typeof(T) == typeof(bool)) |
| 64 | + { |
| 65 | + return (T)(object)BitConverter.ToBoolean(byteArray, 0); |
| 66 | + } |
| 67 | + else if (typeof(T) == typeof(char)) |
| 68 | + { |
| 69 | + return (T)(object)BitConverter.ToChar(byteArray, 0); |
| 70 | + } |
| 71 | + else if (typeof(T) == typeof(double)) |
| 72 | + { |
| 73 | + return (T)(object)BitConverter.ToDouble(byteArray, 0); |
| 74 | + } |
| 75 | + else if (typeof(T) == typeof(Int16)) |
| 76 | + { |
| 77 | + return (T)(object)BitConverter.ToInt16(byteArray, 0); |
| 78 | + } |
| 79 | + else if (typeof(T) == typeof(Int32)) |
| 80 | + { |
| 81 | + return (T)(object)BitConverter.ToInt32(byteArray, 0); |
| 82 | + } |
| 83 | + else if (typeof(T) == typeof(Int64)) |
| 84 | + { |
| 85 | + return (T)(object)BitConverter.ToInt64(byteArray, 0); |
| 86 | + } |
| 87 | + else if (typeof(T) == typeof(Single)) |
| 88 | + { |
| 89 | + return (T)(object)BitConverter.ToSingle(byteArray, 0); |
| 90 | + } |
| 91 | + else if (typeof(T) == typeof(UInt16)) |
| 92 | + { |
| 93 | + return (T)(object)BitConverter.ToUInt16(byteArray, 0); |
| 94 | + } |
| 95 | + else if (typeof(T) == typeof(UInt32)) |
| 96 | + { |
| 97 | + return (T)(object)BitConverter.ToUInt32(byteArray, 0); |
| 98 | + } |
| 99 | + else if (typeof(T) == typeof(UInt64)) |
| 100 | + { |
| 101 | + return (T)(object)BitConverter.ToUInt64(byteArray, 0); |
| 102 | + } |
| 103 | + |
| 104 | + throw new UnsupportedConversionException(); |
| 105 | + } |
| 106 | + |
| 107 | + public unsafe static T ReadBytesAs<T>(this Stream stream, int offset, bool isLittleEndian) |
| 108 | + { |
| 109 | + return stream.ReadBytes(offset, sizeof(T), isLittleEndian).To<T>(); |
| 110 | + } |
| 111 | + |
| 112 | + public unsafe static T ReadBytesFromStreamAs<T>(this Stream stream, bool isLittleEndian) |
| 113 | + { |
| 114 | + int valueSizeInBytes = sizeof(T); |
| 115 | + |
| 116 | + T obtainedValue = stream.ReadBytes( |
| 117 | + (int)stream.Position, |
| 118 | + valueSizeInBytes, |
| 119 | + isLittleEndian |
| 120 | + ).To<T>(); |
| 121 | + |
| 122 | + return obtainedValue; |
| 123 | + } |
| 124 | + |
| 125 | + public static void WriteToXMLWriter(this object obj, XmlWriter xmlWriter, JavaClassFile javaClassFile) |
| 126 | + { |
| 127 | + Type objType = obj.GetType(); |
| 128 | + |
| 129 | + xmlWriter.WriteStartElement(objType.Name); |
| 130 | + |
| 131 | + foreach (FieldInfo fieldInfo in objType.GetFields()) |
| 132 | + { |
| 133 | + string capitalizedFieldName = char.ToUpper( |
| 134 | + fieldInfo.Name[0] |
| 135 | + ) + fieldInfo.Name.Substring(1); |
| 136 | + |
| 137 | + Type fieldType = fieldInfo.FieldType; |
| 138 | + |
| 139 | + if (fieldType.IsArray == true) |
| 140 | + { |
| 141 | + xmlWriter.WriteStartElement( |
| 142 | + capitalizedFieldName |
| 143 | + ); |
| 144 | + |
| 145 | + Array fieldValueAsObjectArray = fieldInfo.GetValue(obj) as Array; |
| 146 | + |
| 147 | + foreach (object element in fieldValueAsObjectArray) |
| 148 | + { |
| 149 | + Type elementType = element.GetType(); |
| 150 | + |
| 151 | + if (elementType.IsPrimitive == true) |
| 152 | + { |
| 153 | + xmlWriter.WriteElementString( |
| 154 | + elementType.Name, |
| 155 | + element.ToString() |
| 156 | + ); |
| 157 | + |
| 158 | + continue; |
| 159 | + } |
| 160 | + |
| 161 | + element.WriteToXMLWriter(xmlWriter, javaClassFile); |
| 162 | + } |
| 163 | + |
| 164 | + xmlWriter.WriteEndElement(); |
| 165 | + |
| 166 | + continue; |
| 167 | + } |
| 168 | + |
| 169 | + MethodInfo writeToXMLWriterMethodInfo = fieldType.GetMethod( |
| 170 | + nameof(WriteToXMLWriter) |
| 171 | + ); |
| 172 | + |
| 173 | + if (writeToXMLWriterMethodInfo == null) |
| 174 | + { |
| 175 | + xmlWriter.WriteElementString( |
| 176 | + capitalizedFieldName, |
| 177 | + fieldInfo.GetValue(obj).ToString() |
| 178 | + ); |
| 179 | + |
| 180 | + continue; |
| 181 | + } |
| 182 | + |
| 183 | + writeToXMLWriterMethodInfo.Invoke( |
| 184 | + fieldInfo.GetValue(obj), |
| 185 | + new object[] |
| 186 | + { |
| 187 | + xmlWriter, |
| 188 | + javaClassFile |
| 189 | + } |
| 190 | + ); |
| 191 | + } |
| 192 | + |
| 193 | + xmlWriter.WriteEndElement(); |
| 194 | + } |
| 195 | + |
| 196 | + public class UnsupportedConversionException : Exception |
| 197 | + { |
| 198 | + public UnsupportedConversionException() : base("Conversion from byte array to current data type is not supported.") |
| 199 | + { |
| 200 | + |
| 201 | + } |
| 202 | + } |
| 203 | + } |
| 204 | +} |
0 commit comments