public void LoadResourceAsync <T>(string resourcePath, Action <T> onLoadCallback) where T : Resource { Resource resource = GetCachedResource(resourcePath); if (resource == null) { if (!_loadingResources.ContainsKey(resourcePath)) { _loadingResources[resourcePath] = new LoadingResourceEntry(typeof(T)); Program.OpenDream.Connection.SendPacket(new PacketRequestResource(resourcePath)); Task.Delay(5000).ContinueWith(r => { if (_loadingResources.ContainsKey(resourcePath)) { Console.WriteLine("Resource '" + resourcePath + "' was requested, but is still not recieved 5 seconds later."); } }); } _loadingResources[resourcePath].LoadCallbacks.Add((Resource resource) => { onLoadCallback.Invoke((T)resource); }); } else { onLoadCallback.Invoke((T)resource); } }
public void HandlePacketResource(PacketResource pResource) { if (_loadingResources.ContainsKey(pResource.ResourcePath)) { LoadingResourceEntry entry = _loadingResources[pResource.ResourcePath]; Resource resource = (Resource)Activator.CreateInstance(entry.ResourceType, new object[] { pResource.ResourcePath, pResource.ResourceData }); _resourceCache[pResource.ResourcePath] = resource; foreach (Action <Resource> callback in entry.LoadCallbacks) { try { callback.Invoke(resource); } catch (Exception e) { Console.WriteLine("Exception while calling resource load callback: " + e.Message); } } _loadingResources.Remove(pResource.ResourcePath); } else { throw new Exception("Received unexpected resource packet for '" + pResource.ResourcePath + "'"); } }