public void ChangeType(ref object obj, Type newType, TaggedTypesCfg taggedTypes, bool keepTypeConfig = false) { var previous = obj; var tObj = obj as IGotClassTag; if (keepTypeConfig && tObj != null) { _perTypeConfig[tObj.ClassTag] = tObj.Encode().ToString(); } obj = Activator.CreateInstance(newType); var std = obj as ICfg; if (std != null) { string data; if (_perTypeConfig.TryGetValue(taggedTypes.Tag(newType), out data)) { std.Decode(data); } } StdExtensions.TryCopy_Std_AndOtherData(previous, obj); }
/* public bool SelectType<T>(ref object obj, bool keepTypeConfig = false) { * var changed = false; * * var all = typeof(T).TryGetTaggedClasses(); * * if (all == null) { * "No Types Holder".writeWarning(); * return false; * } * * // var previous = obj; * * var type = obj?.GetType(); * * if (all.Select(ref type).nl(ref changed)) { * ChangeType(ref obj, type, all, keepTypeConfig); * } * * return changed; * }*/ /* private bool PEGI_inList<T>(ref object obj, int ind, ref int edited, TaggedTypesCfg cfg) * { * * var changed = false; * * if (typeof(T).IsUnityObject()) * { * var uo = obj as UnityEngine.Object; * if (PEGI_inList_Obj(ref uo).changes(ref changed)) * obj = uo; * } * else * { * * if (unrecognized) * unrecognizedUnderTag.write("Type Tag {0} was unrecognized during decoding".F(unrecognizedUnderTag), 40); * * if (!name.IsNullOrEmpty()) * name.write(); * * SelectType<T>(ref obj, cfg); * * } * * return changed; * } */ public bool PEGI_inList <T>(ref object obj, int ind, ref int edited) { var changed = false; if (typeof(T).IsUnityObject()) { var uo = obj as UnityEngine.Object; if (PEGI_inList_Obj(ref uo).changes(ref changed)) { obj = uo; } } else { if (unrecognized) { unrecognizedUnderTag.write("Type Tag {0} was unrecognized during decoding".F(unrecognizedUnderTag), 40); } if (!name.IsNullOrEmpty()) { name.write(); } if (typeof(T) is IGotClassTag) { SelectType <T>(ref obj, TaggedTypesCfg.TryGetOrCreate(typeof(T))); } } return(changed); }
public static void Replace_IfDifferent <T>(this TaggedTypesCfg cfg, ref T obj, Type newType) { if (obj.GetType() != newType) { obj = (T)Activator.CreateInstance(newType); } }
public static void TryChangeObjectType(IList list, int index, Type type, TaggedTypesCfg cfg, ListMetaData ld = null) { object previous = null; if (list != null && index >= 0 && index < list.Count) { previous = list[index]; } var el = previous; var iTag = el as IGotClassTag; var std = (el as ICfgCustom); var ed = ld.TryGetElement(index); if (ed != null && ld.keepTypeData && iTag != null) { ed.ChangeType(ref el, type, cfg, ld.keepTypeData); } else { el = std.TryDecodeInto <object>(type); CfgExtensions.TryCopy_Std_AndOtherData(previous, el); } list[index] = el; }
public static bool CanAdd <T>(this List <T> list, ref object obj, out T conv, bool onlyIfNew = true) { conv = default; if (obj == null || list == null) { return(false); } if (!(obj is T)) { GameObject go; if (typeof(T).IsSubclassOf(typeof(MonoBehaviour))) { go = (obj as MonoBehaviour)?.gameObject; } else { go = obj as GameObject; } if (go) { conv = go.GetComponent <T>(); } } else { conv = (T)obj; } if (conv == null || conv.Equals(default(T))) { return(false); } var objType = obj.GetType(); var dl = typeof(T).TryGetDerivedClasses(); if (dl != null) { if (!dl.Contains(objType)) { return(false); } } else { var tc = TaggedTypesCfg.TryGetOrCreate(typeof(T)); if (tc != null && !tc.Types.Contains(objType)) { return(false); } } return(!onlyIfNew || !list.Contains(conv)); }
private static T Decode <T>(string tag, string data, TaggedTypesCfg tps) where T : IGotClassTag { if (tag == CfgEncoder.NullTag) { return(default(T)); } var type = tps.TaggedTypes.TryGet(tag); return((type == null) ? default(T) : data.DecodeInto_Type <T>(type)); }
public static void Decode <T>(this string data, out T val, TaggedTypesCfg typeList) where T : IGotClassTag { val = default(T); var cody = new CfgDecoder(data); var type = typeList.TaggedTypes.TryGet(cody.GetTag()); if (type != null) { val = cody.GetData().DecodeInto_Type <T>(type); } }
public static List <T> Decode_List <T>(this string data, out List <T> l, TaggedTypesCfg tps) where T : IGotClassTag { var cody = new CfgDecoder(data); l = new List <T>(); foreach (var tag in cody) { l.Add(cody.DecodeData <T>(tps)); } return(l); }
private static T Decode <T>(string tag, string data, TaggedTypesCfg tps, ListMetaData ld, int index) where T : IGotClassTag { if (tag == CfgEncoder.NullTag) { return(default(T)); } var type = tps.TaggedTypes.TryGet(tag); if (type != null) { return(data.DecodeInto_Type <T>(type)); } ld.elementDatas[index].Unrecognized(tag, data); return(default(T)); }
public bool SelectType <T>(ref object obj, TaggedTypesCfg all, bool keepTypeConfig = false) { var changed = false; if (all == null) { "No Types Holder".writeWarning(); return(false); } var type = obj?.GetType(); if (all.Select(ref type).nl(ref changed)) { ChangeType(ref obj, type, all, keepTypeConfig); } return(changed); }
public static TaggedTypesCfg TryGetOrCreate(Type type) { if (!typeof(IGotClassTag).IsAssignableFrom(type)) { return(null); } TaggedTypesCfg cfg; if (_configs.TryGetValue(type, out cfg)) { return(cfg); } cfg = new TaggedTypesCfg(type); _configs[type] = cfg; return(cfg); }
public static TaggedTypesCfg TryGetOrCreate(Type type) { if (!typeof(IGotClassTag).IsAssignableFrom(type)) { // QcUtils.ChillLogger.LogErrorOnce("notGotTag{0}".F(type.ToString()), "{0} doesn't implement IGotClassTag".F(type.ToString())); return(null); } TaggedTypesCfg cfg; if (_configs.TryGetValue(type, out cfg)) { return(cfg); } cfg = new TaggedTypesCfg(type); _configs[type] = cfg; return(cfg); }
private static T DecodeData <T>(this CfgDecoder cody, TaggedTypesCfg tps, ListMetaData ld) where T : IGotClassTag => Decode <T>(cody.currentTag, cody.GetData(), tps, ld, cody.currentTagIndex);
public static List <T> Decode_List <T>(this string data, out List <T> l, ref ListMetaData ld, TaggedTypesCfg tps) where T : IGotClassTag { l = new List <T>(); if (ld == null) { ld = new ListMetaData(); } var overCody = new CfgDecoder(data); foreach (var tag in overCody) { switch (tag) { case CfgEncoder.ListMetaTag: ld.Decode(overCody.GetData()); break; case CfgEncoder.ListTag: var cody = new CfgDecoder(overCody.GetData()); foreach (var t in cody) { l.Add(cody.DecodeData <T>(tps, ld)); } break; default: l.Add(overCody.DecodeData <T>(tps, ld)); break; } } return(l); }
public static List <T> Decode_List_Abstract <T>(this string data, out List <T> l, TaggedTypesCfg taggedTypes) where T : IGotClassTag { l = new List <T>(); #if UNITY_EDITOR var ct = taggedTypes.CoreType; var lt = typeof(T); if (ct != lt && !lt.IsSubclassOf(ct)) { Debug.LogError("Type of {0} is not a subclass of {1}".F(lt.ToPegiStringType(), ct.ToPegiStringType())); return(l); } #endif var cody = new CfgDecoder(data); foreach (var tag in cody) { var tmp = cody.DecodeData <T>(taggedTypes); if (tmp != null) { l.Add(tmp); } } return(l); }
private T Decode <T>(string tagAsTypeIndex, TaggedTypesCfg tps) where T : ICfg { if (tagAsTypeIndex == CfgEncoder.NullTag) { return(default);
public CfgEncoder Add(string tag, IGotClassTag typeTag, TaggedTypesCfg cfg) => typeTag == null ? this : Add(tag, new CfgEncoder().Add(typeTag.ClassTag, typeTag.Encode()));
public CfgEncoder Add <T>(string tag, List <T> val, ListMetaData ld, TaggedTypesCfg tts) where T : IGotClassTag => Add_Abstract(tag, val, ld);
private static T DecodeData <T>(this CfgDecoder cody, TaggedTypesCfg tps) where T : IGotClassTag => Decode <T>(cody.currentTag, cody.GetData(), tps);
public CfgEncoder Add_IfNotEmpty <T>(string tag, List <T> val, TaggedTypesCfg tts, ListMetaData ld) where T : IGotClassTag => val.IsNullOrEmpty() ? this : Add_Abstract(tag, val, ld);
private static T Decode <T>(string tag, string data, TaggedTypesCfg tps, ListMetaData ld, int index) where T : IGotClassTag { if (tag == CfgEncoder.NullTag) { return(default);