public override void Initialize() { // load all models lock (_syncRoot) { if (_initialized) { return; } foreach (var voxelModel in VoxelModelStorage.Enumerate()) { var vmodel = new VisualVoxelModel(voxelModel, VoxelMeshFactory); vmodel.BuildMesh(); //Build the mesh of all local models _models.Add(voxelModel.Name, vmodel); } _initialized = true; } // if we have some requested models, remove those that was loaded List <string> loadedModels; lock (_syncRoot) { loadedModels = _pendingModels.Where(Contains).ToList(); loadedModels.ForEach(m => _pendingModels.Remove(m)); } // fire events foreach (var loadedModel in loadedModels) { var model = GetModel(loadedModel, false); OnVoxelModelAvailable(new VoxelModelReceivedEventArgs { Model = model.VoxelModel }); } HashSet <string> requestSet; lock (_syncRoot) { _pendingModels.ExceptWith(loadedModels); requestSet = _pendingModels; _pendingModels = new HashSet <string>(); } // request the rest models foreach (var absentModel in requestSet) { RequestModelAsync(absentModel); } }
/// <summary> /// Request a missing model from the server /// </summary> /// <param name="name"></param> public void DownloadModel(string name) { logger.Info("Downloading model: {0}", name); try { var req = WebRequest.Create(string.Format("http://utopiarealms.com/models/{0}/download", Uri.EscapeDataString(name))); var response = req.GetResponse(); using (var stream = response.GetResponseStream()) { var voxelModel = VoxelModel.LoadFromStream(stream); lock (_syncRoot) { if (_models.ContainsKey(voxelModel.Name)) { _models.Remove(voxelModel.Name); } var model = new VisualVoxelModel(voxelModel, VoxelMeshFactory); model.BuildMesh(); _models.Add(voxelModel.Name, model); _pendingModels.Remove(voxelModel.Name); } _receivedModels.Enqueue(voxelModel); VoxelModelStorage.Save(voxelModel); } response.Close(); } catch (Exception x) { logger.Error("Unable to download the model: {0}", x.Message); } }