Ejemplo n.º 1
0
        public static T LoadFromBinary <T>(this byte[] content, Func <T> defaultIfLoadFailed = null)
            where T : ISerializable, new()
        {
            var data = new T();

            try
            {
                using (var reader = new MemoryStream(content))
                {
                    data.Deserialize(new BinaryReader(reader));
                }
            }
            catch (Exception e)
            {
                if (defaultIfLoadFailed != null)
                {
                    data = defaultIfLoadFailed();
                }
                else
                {
                    data = new T();
                }
                Debug.LogError($"Failed to load binary with error :{e.Message} call stack=\n{e.StackTrace}");
            }

            return(data);
        }
Ejemplo n.º 2
0
        // Wont work well if collection has same elements multiple times
        public static IDisposable AffectEach <T>(this IReactiveCollection <T> collection, Action <IConnectionSink, T> affect) where T : class
        {
            var itemConnectionsDict = new Dictionary <T, Connections>();

            collection.BindEach(item => {
                var itemConnections = new Connections();
                if (itemConnectionsDict.ContainsKey(item))
                {
                    Debug.LogError("it seems item is already loaded, this function wont work if elements repeated in the collection");
                    return;
                }
                affect(itemConnections, item);
                itemConnectionsDict[item] = itemConnections;
            }, item => {
                itemConnectionsDict.TakeKey(item).DisconnectAll();
            });

            return(new AnonymousDisposable(() =>
            {
                foreach (var connections in itemConnectionsDict.Values)
                {
                    connections.DisconnectAll();
                }
            }));
        }
Ejemplo n.º 3
0
    public static bool CompareRefs <T>(Stack <string> path, string name, T val, T val2)
    {
        if (object.ReferenceEquals(val, val2))
        {
            Debug.LogError($"{path.Reverse().PrintCollection("/")}/{name} class refs do not match");
            return(false);
        }

        return(true);
    }
Ejemplo n.º 4
0
        public static ICellRW <T> ReflectionFieldToRW <T>(this object obj, string fieldName)
        {
            var f = obj.GetType().GetField(fieldName);

            if (f == null)
            {
                Debug.LogError($"field {fieldName} is not found in obj {obj}"); return(null);
            }
            return(obj.ReflectionFieldToRW <T>(f));
        }
Ejemplo n.º 5
0
    public static bool CompareClassId <T>(Stack <string> path, string name, T val, T val2) where T : IPolymorphable
    {
        if (val.GetClassId() != val2.GetClassId())
        {
            Func <T, string> pr = t => t.GetClassId().ToString();
            Debug.LogError(
                $"{path.Reverse().PrintCollection("/")}/{name} class id do not mach, self: {pr(val)} other: {pr(val2)}");
            return(false);
        }

        return(true);
    }
Ejemplo n.º 6
0
    public static ushort ReadJsonClassId(this JsonTextReader reader)
    {
        reader.Read();
        if (reader.TokenType == JsonToken.PropertyName && (string)reader.Value == "classId")
        {
            return((ushort)reader.ReadAsInt32());
        }
        else
        {
            Debug.LogError("error while reading class id in json");
        }

        return(0);
    }
Ejemplo n.º 7
0
    // Return if needs to check further
    public static bool CompareNull <T>(Stack <string> path, string name, T val, T val2) where T : class
    {
        if (val == null && val2 == null)
        {
            return(false);
        }

        if (val != null && val2 != null)
        {
            return(true);
        }

        Func <T, string> pr = t => t == null ? "null" : "not null";

        Debug.LogError($"{path.Reverse().PrintCollection("/")}/{name} is different, self: {pr(val)} other: {pr(val2)}");

        return(false);
    }
Ejemplo n.º 8
0
        public static T LoadFromJsonString <T>(this string content) where T : IJsonSerializable, new()
        {
            var data = new T();

            try
            {
                using (var reader = new StringReader(content))
                {
                    data.ReadFromJson(new JsonTextReader(reader));
                }
            }
            catch (Exception e)
            {
                data = new T();
                Debug.LogError($"Failed to load json from string {content} with error :{e.Message} call stack=\n{e.StackTrace}");
            }

            return(data);
        }
Ejemplo n.º 9
0
 public static string SaveToJsonString <T>(this T data, bool formatting = true) where T : IJsonSerializable
 {
     try
     {
         using (var stream = new StringWriter())
         {
             var writer = new JsonTextWriter(stream);
             writer.Formatting = formatting ? Formatting.Indented : Formatting.None;
             data.WriteJson(writer);
             return(stream.ToString());
         }
     }
     catch (Exception e)
     {
         Debug.LogError($"Failed to save data to path {data} with error :");
         Debug.LogError(e.Message + e.StackTrace);
         throw;
     }
 }
Ejemplo n.º 10
0
 public static void LogCompError <T>(Stack <string> path, string name, T self, T other)
 {
     Debug.LogError($"{path.Reverse().PrintCollection("/")}/{name} is different, self: {self} other: {other}");
 }