protected virtual void DoProcessObject(FlashObject obj) { if (ProcessObject != null) { ProcessObject(obj); } }
/// <summary> /// Helper method which sets all the properties in the class to their respected FlashObject field. /// Use InternalNameAttribute to specify a property which has a FlashObject counter-part. /// SetFields does not travel the hierarchy. So Derived types must make their own separate call to SetFields. /// </summary> /// <param name="obj">Object to change properties</param> /// <param name="flash">Flash object to get fields from</param> public static void SetFields <T>(T obj, FlashObject flash) { if (flash == null) { return; } foreach (var prop in typeof(T).GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly)) { var intern = prop.GetCustomAttributes(typeof(InternalNameAttribute), false).FirstOrDefault() as InternalNameAttribute; if (intern == null) { continue; } object value; var type = prop.PropertyType; try { if (type == typeof(string)) { value = flash[intern.Name].Value; } else if (type == typeof(int)) { value = Parse.Int(flash[intern.Name].Value); } else if (type == typeof(long)) { value = Parse.Long(flash[intern.Name].Value); } else if (type == typeof(bool)) { value = Parse.Bool(flash[intern.Name].Value); } else { try { value = Activator.CreateInstance(type, flash[intern.Name]); } catch (Exception e) { throw new NotSupportedException(string.Format("Type {0} not supported by flash serializer", type.FullName), e); } } prop.SetValue(obj, value, null); } catch (Exception e) { StaticLogger.Error(string.Format("Error parsing {0}#{1}", typeof(T).FullName, prop.Name)); StaticLogger.Error(e); } } }
public static FlashObject Deserialize(StreamReader reader) { var ret = new FlashObject("Base"); var current = ret; var levels = new Stack <int>(); levels.Push(0); while (levels.Count > 0) { if (reader.Peek() != ' ') //No Space? Well then it must be the end of the object { return(ret); } var line = reader.ReadLine(); var kv = MatchLine(line); if (kv == null) { throw new NotSupportedException("Unable to parse (" + line + ")"); } while (levels.Count > 0 && GetLevel(line) <= levels.Peek()) { current = current.Parent; levels.Pop(); } var objname = GetObjectName(line); if (objname != null) { var tmp = new FlashObject(objname, kv.Key, kv.Value) { Parent = current }; current[kv.Key] = tmp; current = tmp; levels.Push(GetLevel(line)); } else { if (kv.Value.Length > 0 && kv.Value[0] == '"') { var str = ParseString(kv.Value.Substring(1)); if (str != null) { //Singleline quote kv.Value = str; } else { //Multiline quote kv.Value = kv.Value.Substring(1) + ParseString(reader); reader.ReadLine(); //Read the newline after the quote } } current[kv.Key] = new FlashObject(kv.Key, kv.Value); } } return(ret); }