public static string CopyObjectReference([CanBeNull] Object target, Type type) { lastOperationFailed = true; ClearCutData(); ObjectReference = target; hasObjectReference = true; objectReferences.Clear(); objectReferences.Add(target); if (target == null) { copiedType = type; Content = ""; return(Content); } copiedType = target.GetType(); Content = PrettySerializer.SerializeReference(target); lastOperationFailed = false; return(Content); }
public static string Copy([NotNull] object target) { lastOperationFailed = true; ClearCutData(); if (target == null) { copiedType = Types.Void; ObjectReference = target as Object; hasObjectReference = false; objectReferences.Clear(); Content = "null"; } else { copiedType = target.GetType(); ObjectReference = target as Object; hasObjectReference = objectReference != null; objectReferences.Clear(); Content = PrettySerializer.Serialize(target, ref objectReferences); } lastOperationFailed = false; return(Content); }
public static string CopyObjectReferences(IEnumerable <Object> targets, Type type) { lastOperationFailed = true; ClearCutData(); copiedType = type; hasObjectReference = false; objectReferences.Clear(); var ids = ReusedStringList; #if DEV_MODE && PI_ASSERTATIONS Debug.Assert(ids.Count == 0, "ReusedStringList.Count != 0!"); #endif foreach (var target in targets) { if (!hasObjectReference) { hasObjectReference = true; ObjectReference = target; } objectReferences.Add(target); ids.Add(PrettySerializer.SerializeReference(target)); } var serialize = ids.ToArray(); ids.Clear(); Content = PrettySerializer.Serialize(serialize); lastOperationFailed = false; return(Content); }
public static bool HasObjectReference() { if (hasObjectReference) { #if DEV_MODE Debug.Log("HasObjectReference: " + StringUtils.True); #endif return(true); } if (CopiedType == Types.String) { if (PrettySerializer.IsSerializedReference(Content)) { #if DEV_MODE Debug.Log("HasObjectReference: " + StringUtils.True); #endif return(true); } } #if DEV_MODE Debug.Log("HasObjectReference: " + StringUtils.False); #endif return(false); }
public static string Cut([CanBeNull] object target, [CanBeNull] LinkedMemberInfo memberInfo) { lastOperationFailed = true; if (memberInfo != null) { cutMemberInfo = SerializableMemberInfo.Serialize(memberInfo); } ObjectReference = target as Object; hasObjectReference = objectReference != null; if (target != null) { copiedType = target.GetType(); } else if (memberInfo != null) { copiedType = memberInfo.Type; } else { throw new NullReferenceException("Clipboard.Cut both target and memberInfo were null"); } Content = PrettySerializer.Serialize(target); isCut = true; lastOperationFailed = false; return(Content); }
public static void Duplicate <TComponent>(TComponent[] targets) where TComponent : Component { for (int n = targets.Length - 1; n >= 0; n--) { #if DEV_MODE && PI_ASSERTATIONS Debug.Assert(targets[n] != null, "Duplicate - " + typeof(TComponent) + " " + (n + 1) + " / " + n + " was null!"); #endif #if UNITY_EDITOR UnityEditorInternal.ComponentUtility.CopyComponent(targets[n]); UnityEditorInternal.ComponentUtility.PasteComponentAsNew(targets[n].gameObject); #else var source = targets[n]; var clone = source.gameObject.AddComponent(source.GetType()); byte[] bytes = null; System.Collections.Generic.List <UnityEngine.Object> references = null; #if !DONT_USE_ODIN_SERIALIZER Sisus.OdinSerializer.UnitySerializationUtility.SerializeUnityObject(source, ref bytes, ref references, OdinSerializer.DataFormat.Binary, true); Sisus.OdinSerializer.UnitySerializationUtility.DeserializeUnityObject(clone, ref bytes, ref references, OdinSerializer.DataFormat.Binary); #else PrettySerializer.SerializeUnityObject(source, ref bytes, ref references); PrettySerializer.DeserializeUnityObject(bytes, clone, ref references); #endif #endif } }
public static SplittableInspectorDrawerSerializedState Deserialize(byte[] bytes) { var result = PrettySerializer.FromBytes <SplittableInspectorDrawerSerializedState>(bytes); #if DONT_USE_ODIN_SERIALIZER if (!result.viewIsSplit) { result.splitViewState = null; } #endif return(result); }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { string stringData = reader.Value as string; if (string.IsNullOrEmpty(stringData) || string.Equals(stringData, "null")) { return(null); } var unityObjectToOverwrite = existingValue as Object; PrettySerializer.DeserializeUnityObject(stringData, unityObjectToOverwrite); return(unityObjectToOverwrite); }
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { string serializedString; if (value == null) { serializedString = "null"; } else { List <Object> objectReferences = null; serializedString = PrettySerializer.SerializeUnityObject(value as Object, ref objectReferences); } writer.WriteRawValue(serializedString); }
public static string Copy([CanBeNull] object target, [NotNull] Type type) { lastOperationFailed = true; ClearCutData(); if (target == null) { copiedType = type; ObjectReference = null; hasObjectReference = type.IsUnityObject(); objectReferences.Clear(); if (hasObjectReference) { objectReferences.Add(null); } Content = "null"; } else { #if DEV_MODE Debug.Assert(target.GetType() == type, "target.GetType() " + StringUtils.TypeToString(target) + " != type " + StringUtils.ToString(type)); #endif copiedType = type; var unityObject = target as Object; ObjectReference = unityObject; hasObjectReference = objectReference != null; objectReferences.Clear(); if (hasObjectReference) { objectReferences.Add(unityObject); } Content = PrettySerializer.Serialize(target, ref objectReferences); } lastOperationFailed = false; return(Content); }
public static bool TryPasteUnityObject(Object objectToOverwrite) { try { PrettySerializer.DeserializeUnityObject(Content, objectToOverwrite, ref objectReferences); lastOperationFailed = false; return(true); } #if DEV_MODE catch (Exception e) { Debug.LogError("TryPasteUnityObject failed for " + objectToOverwrite.name + " of type " + StringUtils.TypeToString(objectToOverwrite) + ": " + e); lastOperationFailed = true; return(false); } #else catch { lastOperationFailed = true; return(false); } #endif }
private static object Paste(Type type, bool isOnlyATest) { lastOperationFailed = true; string stringData = Content; object result = null; bool success; try { success = PrettySerializer.DeserializeOverrideNonUnityObject(stringData, type, ref result, objectReferences); } #if DEV_MODE catch (Exception e) #else catch #endif { #if DEV_MODE Debug.Log(e); #endif success = false; } if (!success) { #if DEV_MODE Debug.Log("Could not deserialize Clipboard contents as type " + type.Name + ": " + stringData); #endif if (!CopiedTypeIsDefaultValue && copiedType != type) { try { success = PrettySerializer.DeserializeOverrideNonUnityObject(stringData, copiedType, ref result, objectReferences); } #if DEV_MODE catch (Exception e) #else catch #endif { #if DEV_MODE Debug.Log(e); #endif success = false; } #if DEV_MODE Debug.Log("Could not deserialize Clipboard contents as copied type " + copiedType.Name + ": " + stringData); #endif } if (!success) { throw new ArgumentException("Could not deserialize Clipboard contents as type " + type.Name + " or as copied type " + copiedType.Name + ": " + stringData); } if (!Converter.TryChangeType(ref result, type)) { throw new InvalidCastException("Could not deserialize Clipboard contents as type " + type.Name + " or cast value deserialized as copied type " + copiedType.Name + " to that type: " + stringData); } } if (!isOnlyATest && isCut) { OnCutPasted(); } lastOperationFailed = false; return(result); }
private byte[] ToBytes() { return(PrettySerializer.ToBytes(this)); }