Ejemplo n.º 1
0
        public static async Task DoAsyncWithMaxSimultaneous(IEnumerable <Func <Task> > tasks, int maxSimultaneous)
        {
            Debug.Assert(maxSimultaneous >= 1, "should allow at least one task at a time");
            List <Task> currTasks = new List <Task>();

            foreach (var task in tasks)
            {
                // Wait until simultaneous tasks count drops below limit.
                await WaitingUntillTasksCountLessThan(maxSimultaneous);

                // Start next task.
                currTasks.Add(task());
            }

            // Wait until all tasks complete.
            await WaitingUntillTasksCountLessThan(1);

            async Task WaitingUntillTasksCountLessThan(int count)
            {
                while (currTasks.Count >= count)
                {
                    await Frame.one;
                    currTasks.RemoveAll(currTask => currTask.IsCompleted);
                }
            }
        }
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 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.º 4
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.º 5
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.º 6
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.º 7
0
    public static void WriteToStream <T>(this List <T> data, BinaryWriter stream) where T : ISerializable, new()
    {
        Debug.Assert(data.Count > UInt16.MaxValue, "writing stream failed");
        ushort size = (ushort)data.Count;

        stream.Write(size);
        data.Capacity = size;
        for (int q = 0; q < size; q++)
        {
            stream.Write(data[q]);
        }
    }
Ejemplo n.º 8
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.º 9
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.º 10
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.º 11
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.º 12
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}");
 }