private bool Skip(ISSBH_File File, PropertyInfo prop) { object[] attrs = prop.GetCustomAttributes(true); bool skip = false; foreach (object attr in attrs) { if (attr is ParseTag tag) { if (!tag.IF.Equals("")) { string[] args = tag.IF.Split('>'); PropertyInfo checkprop = null; foreach (PropertyInfo pi in File.GetType().GetProperties()) { if (pi.Name.Equals(args[0])) { checkprop = pi; break; } } skip = true; if (checkprop != null) { if ((ushort)checkprop.GetValue(File) > int.Parse(args[1])) { skip = false; break; } } } } } return(skip); }
private void WriteSSBHFile(ISSBH_File File) { foreach (var prop in File.GetType().GetProperties()) { if (Skip(File, prop)) { continue; } if (prop.PropertyType == typeof(string)) { objectQueue.AddLast(prop.GetValue(File)); objectOffset.Add((uint)Position, prop.GetValue(File)); Write((long)0); } else if (prop.PropertyType.IsArray) { var Array = (prop.GetValue(File) as Array); bool Inline = false; if (prop.GetCustomAttribute(typeof(ParseTag)) != null) { Inline = ((ParseTag)prop.GetCustomAttribute(typeof(ParseTag))).InLine; } if (!Inline) { if (Array.Length > 0) { objectOffset.Add((uint)Position, Array); } objectQueue.AddLast(Array); Write((long)0); Write((long)Array.Length); } else { // inline array foreach (object o in Array) { WriteProperty(o); } } } else { WriteProperty(prop.GetValue(File)); } } // Post Write // TODO: mostly for materials.... }
public static void WriteSSBHFile(string fileName, ISSBH_File file, bool writeHeader = true) { using (SSBHExporter exporter = new SSBHExporter(new FileStream(fileName, FileMode.Create))) { // write ssbh header if (writeHeader) { exporter.Write(new char[] { 'H', 'B', 'S', 'S' }); exporter.Write(0x40); exporter.Pad(0x10); } // write file contents exporter.AddSSBHFile(file); exporter.Pad(8); } }
private bool Skip(ISSBH_File file, PropertyInfo prop) { object[] attrs = prop.GetCustomAttributes(true); bool skip = false; foreach (object attr in attrs) { if (attr is ParseTag tag) { if (tag.Ignore) { return(true); } } } return(skip); }
public bool TryParse(out ISSBH_File File) { File = null; if (FileSize < 4) { return(false); } string Magic = new string(ReadChars(4)); Seek(Position - 4); if (Magic.Equals("HBSS")) { Seek(0x10); Magic = new string(ReadChars(4)); Seek(0x10); } var Types = (from domainAssembly in AppDomain.CurrentDomain.GetAssemblies() from assemblyType in domainAssembly.GetTypes() where typeof(ISSBH_File).IsAssignableFrom(assemblyType) select assemblyType).ToArray(); foreach (var c in Types) { SSBHFileAttribute attr = c.GetCustomAttributes( typeof(SSBHFileAttribute), true ).FirstOrDefault() as SSBHFileAttribute; if (attr != null) { if (attr.Magic.Equals(Magic)) { MethodInfo method = typeof(SSBHParser).GetMethod("Parse"); method = method.MakeGenericMethod(c); File = (ISSBH_File)method.Invoke(this, null); return(true); } } } return(false); }
public bool TryParse(out ISSBH_File file) { file = null; if (FileSize < 4) { return(false); } string fileMagic = new string(ReadChars(4)); Seek(Position - 4); if (fileMagic.Equals("HBSS")) { Seek(0x10); fileMagic = new string(ReadChars(4)); Seek(0x10); } if (parseMethodByMagic.ContainsKey(fileMagic)) { file = parseMethodByMagic[fileMagic](this); return(true); } // The type is not known, so do a very slow check to find it. foreach (var type in issbhTypes) { if (type.GetCustomAttributes(typeof(SSBHFileAttribute), true).FirstOrDefault() is SSBHFileAttribute attr) { if (attr.Magic.Equals(fileMagic)) { MethodInfo parseMethod = typeof(SSBHParser).GetMethod("Parse"); parseMethod = parseMethod.MakeGenericMethod(type); file = (ISSBH_File)parseMethod.Invoke(this, null); return(true); } } } return(false); }
private void AddSSBHFile(ISSBH_File file) { objectQueue.AddFirst(file); while (objectQueue.Count > 0) { var obj = objectQueue.First(); objectQueue.RemoveFirst(); // I guess? if (obj is Array) { Pad(0x8); } // not sure if 4 or 8 if (obj is string) { Pad(0x4); } if (objectOffset.ContainsValue(obj)) { long key = objectOffset.FirstOrDefault(x => x.Value == obj).Key; if (key != 0) { long Temp = Position; BaseStream.Position = key; WriteProperty(Temp - key); BaseStream.Position = Temp; objectOffset.Remove((uint)key); } } if (obj is Array Array) { if (Array.GetType() == typeof(byte[])) { foreach (byte o in Array) { Write(o); } } else { LinkedList <object> ObjectQueueTemp = objectQueue; objectQueue = new LinkedList <object>(); foreach (object o in Array) { WriteProperty(o); } foreach (object o in ObjectQueueTemp) { objectQueue.AddLast(o); } } } else { WriteProperty(obj); } } }
private void WriteSSBHFile(ISSBH_File file) { foreach (var prop in file.GetType().GetProperties()) { if (Skip(file, prop)) { continue; } if (prop.PropertyType == typeof(string)) { if (prop.GetValue(file) == null) { Write((long)0); continue; } objectQueue.AddLast(prop.GetValue(file)); objectOffset.Add((uint)Position, prop.GetValue(file)); Write((long)0); } else if (prop.PropertyType.IsArray) { var array = (prop.GetValue(file) as Array); bool inline = false; if (prop.GetCustomAttribute(typeof(ParseTag)) != null) { inline = ((ParseTag)prop.GetCustomAttribute(typeof(ParseTag))).InLine; } if (!inline) { if (array.Length > 0) { objectOffset.Add((uint)Position, array); } objectQueue.AddLast(array); Write((long)0); Write((long)array.Length); } else { // inline array foreach (object o in array) { WriteProperty(o); } } } else if (prop.PropertyType == typeof(SSBHOffset)) { // HACK: for materials var dataObject = file.GetType().GetProperty("DataObject").GetValue(file); var matentry = new MaterialEntry(dataObject); objectOffset.Add((uint)Position, matentry); objectQueue.AddLast(matentry); Write((long)0); } else { WriteProperty(prop.GetValue(file)); } } // TODO: Post Write is only used for materials. file.PostWrite(this); }