public Action(List Data, string BaseDir, SpriteData spriteData) { Properties Props = new Properties(Data); if(!Props.Get("name", ref Name)) throw new Exception("Action without name specified"); Props.Get("fps", ref Speed); Props.Get("x-offset", ref Offset.X); Props.Get("y-offset", ref Offset.Y); List<string> ImageFileNames = new List<string>(); Props.GetStringList("images", ImageFileNames); Props.PrintUnusedWarnings(); foreach(string ImageFile in ImageFileNames) { Surface surface = new Surface(BaseDir + "/" + ImageFile); Width = Math.Max(Width, surface.Width); Height = Math.Max(Height, surface.Height); Frames.Add(surface); } string MirrorActionName = null; Props.Get("mirror-action", ref MirrorActionName); if(MirrorActionName != null) { Action MirrorAction = spriteData.Actions[MirrorActionName]; foreach(Surface surface in MirrorAction.Frames) { Surface flippedSurface = new Surface(surface); flippedSurface.Left = surface.Right; flippedSurface.Right = surface.Left; Width = Math.Max(Width, surface.Width); Height = Math.Max(Height, surface.Height); Frames.Add(flippedSurface); } } }
public object Read(List list) { object result = CreateObject(type); Properties props = new Properties(list); // iterate over all fields and properties foreach(FieldOrProperty field in FieldOrProperty.GetFieldsAndProperties(type)) { LispChildAttribute ChildAttrib = (LispChildAttribute) field.GetCustomAttribute(typeof(LispChildAttribute)); if(ChildAttrib != null) { string Name = ChildAttrib.Name; if(field.Type == typeof(int)) { int val = 0; if(!props.Get(Name, ref val)) { if(!ChildAttrib.Optional) Console.WriteLine("Field '" + Name + "' not in lisp"); } else { field.SetValue(result, val); } } else if(field.Type == typeof(string)) { string val = null; if(!props.Get(Name, ref val)) { if(!ChildAttrib.Optional) Console.WriteLine("Field '" + Name + "' not in lisp"); } else { field.SetValue(result, val); } } else if(field.Type == typeof(float)) { float val = 0; if(!props.Get(Name, ref val)) { if(!ChildAttrib.Optional) Console.WriteLine("Field '" + Name + "' not in lisp"); } else { field.SetValue(result, val); } } else if (field.Type.IsEnum) { Enum val = null; if (!props.Get(Name, ref val, field.Type)) { if (!ChildAttrib.Optional) Console.WriteLine("Field '" + Name + "' not in lisp"); } else { field.SetValue(result, val); } } else if(field.Type == typeof(bool)) { bool val = false; if(!props.Get(Name, ref val)) { if(!ChildAttrib.Optional) Console.WriteLine("Field '" + Name + "' not in lisp"); } else { field.SetValue(result, val); } } else if(field.Type == typeof(List<int>)) { List<int> val = new List<int>(); if(!props.GetIntList(Name, val)) { if(!ChildAttrib.Optional) Console.WriteLine("Field '" + Name + "' not in lisp"); } else { field.SetValue(result, val); } } else { ILispSerializer serializer = LispSerializer.GetSerializer(field.Type); if(serializer == null) throw new LispException("Type " + field.Type + " not supported for serialization"); List val = null; if(!props.Get(Name, ref val)) { if(!ChildAttrib.Optional) Console.WriteLine("Field '" + Name + "' not in lisp"); } else { object oval = serializer.Read(val); field.SetValue(result, oval); } } } foreach(LispChildsAttribute childsAttrib in field.GetCustomAttributes(typeof(LispChildsAttribute))) { object objectList = field.GetValue(result); Type ListType = field.Type; MethodInfo AddMethod = ListType.GetMethod( "Add", new Type[] { childsAttrib.ListType }, null); if(AddMethod == null) throw new LispException("No Add method found for field " + field.Name); ILispSerializer serializer = LispSerializer.GetSerializer(childsAttrib.Type); if(serializer == null) serializer = LispSerializer.CreateRootSerializer(childsAttrib.Type); foreach(List childList in props.GetList(childsAttrib.Name)) { object child = serializer.Read(childList); AddMethod.Invoke(objectList, new object[] { child } ); } } } if(result is ICustomLispSerializer) { ICustomLispSerializer custom = (ICustomLispSerializer) result; custom.CustomLispRead(props); custom.FinishRead(); } return result; }
public Action(List Data, string BaseDir, SpriteData spriteData) { Properties Props = new Properties(Data); if(!Props.Get("name", ref Name)) throw new Exception("Action without name specified"); Props.Get("fps", ref Speed); if(Props.Exists("hitbox")) { List<float> hitbox = new List<float>(); Props.GetFloatList("hitbox", hitbox); if (hitbox.Count != 4) throw new Exception("hitbox must specify exactly 4 coordinates"); Hitbox = new RectangleF(hitbox[0], hitbox[1], hitbox[2], hitbox[3]); Offset.X = Hitbox.Left; Offset.Y = Hitbox.Top; } List<string> ImageFileNames = new List<string>(); Props.GetStringList("images", ImageFileNames); Props.PrintUnusedWarnings(); foreach(string ImageFile in ImageFileNames) { Surface surface = new Surface(BaseDir + "/" + ImageFile); Width = Math.Max(Width, surface.Width); Height = Math.Max(Height, surface.Height); Frames.Add(surface); } string MirrorActionName = null; Props.Get("mirror-action", ref MirrorActionName); if(MirrorActionName != null) { Action MirrorAction = spriteData.Actions[MirrorActionName]; foreach(Surface surface in MirrorAction.Frames) { Surface flippedSurface = new Surface(surface); flippedSurface.Left = surface.Right; flippedSurface.Right = surface.Left; Width = Math.Max(Width, surface.Width); Height = Math.Max(Height, surface.Height); Frames.Add(flippedSurface); } } }
public object Read(TextReader Reader, string Source) { LispRootAttribute RootAttrib = (LispRootAttribute) Attribute.GetCustomAttribute(RootType, typeof(LispRootAttribute)); if(RootAttrib == null) throw new LispException("Type needs to have LispRoot attribute"); Lexer Lexer = new Lexer(Reader); Parser Parser = new Parser(Lexer); List Root = Parser.Parse(); Properties RootP = new Properties(Root); List List = null; if(!RootP.Get(RootAttrib.Name, ref List)) throw new LispException("'" + Source + "' is not a " + RootAttrib.Name + " file"); return ReadType(RootType, List); }