private static bool UpdateModels(Document document, MapObject mo) { var updatedChildren = false; foreach (var child in mo.GetChildren()) { updatedChildren |= UpdateModels(document, child); } var e = mo as Entity; if (e == null || !ShouldHaveModel(e)) { var has = e != null && HasModel(e); if (has) { UnsetModel(e); } return(updatedChildren || has); } var model = GetModelName(e); var existingModel = e.MetaData.Get <string>(ModelNameMetaKey); if (String.Equals(model, existingModel, StringComparison.InvariantCultureIgnoreCase)) { return(updatedChildren); // Already set; No need to continue } var file = document.Environment.Root.TraversePath(model); if (file == null || !ModelProvider.CanLoad(file)) { // Model not valid, get rid of it UnsetModel(e); return(true); } try { SetModel(e, ModelProvider.CreateModelReference(file)); return(true); } catch { // Couldn't load return(updatedChildren); } }
protected List <ModelReference> LoadAllModels(IEnumerable <string> modelDirs) { List <ModelReference> models = new List <ModelReference>(); foreach (string dir in modelDirs) { string[] files = Directory.GetFiles(dir); foreach (string modelPath in files) { NativeFile file = null; if (!string.IsNullOrEmpty(modelPath)) { file = new NativeFile(modelPath); } if (file == null || !ModelProvider.CanLoad(file)) { continue; } #if !DEBUG try { #endif var mr = ModelProvider.CreateModelReference(file); models.Add(mr); #if !DEBUG } catch (Exception) { // Couldn't load continue; } #endif } } return(models); }
private static bool UpdateModels(Document document, MapObject mo, Dictionary <string, ModelReference> cache) { var updatedChildren = false; foreach (var child in mo.GetChildren()) { updatedChildren |= UpdateModels(document, child, cache); } var e = mo as Entity; if (e == null || !ShouldHaveModel(e)) { var has = e != null && HasModel(e); if (has) { UnsetModel(e); } return(updatedChildren || has); } var model = GetModelName(e); var existingModel = e.MetaData.Get <string>(ModelNameMetaKey); if (String.Equals(model, existingModel, StringComparison.OrdinalIgnoreCase)) { return(updatedChildren); // Already set; No need to continue } if (cache.ContainsKey(model)) { var mr = cache[model]; if (mr == null) { UnsetModel(e); } else { SetModel(e, mr); } return(true); } else { string modelPath = Directories.GetModelPath(model); NativeFile file = null; if (!string.IsNullOrEmpty(modelPath)) { file = new NativeFile(modelPath); } if (file == null || !ModelProvider.CanLoad(file)) { // Model not valid, get rid of it UnsetModel(e); cache.Add(model, null); return(true); } #if !DEBUG try { #endif var mr = ModelProvider.CreateModelReference(file); SetModel(e, mr); cache.Add(model, mr); return(true); #if !DEBUG } catch (Exception exception) { File.AppendAllText("modelLoadErrors.txt", $"\nFailed to load {file.FullPathName}: " + $"{exception.Message} ({exception.GetType().Name})\n" + $"{exception.StackTrace}"); // Couldn't load cache.Add(model, null); return(updatedChildren); } #endif } }
private static bool UpdateModels(Document document, MapObject mo, Dictionary <string, ModelReference> cache) { var updatedChildren = false; foreach (var child in mo.GetChildren()) { updatedChildren |= UpdateModels(document, child, cache); } var e = mo as Entity; if (e == null || !ShouldHaveModel(e)) { var has = e != null && HasModel(e); if (has) { UnsetModel(e); } return(updatedChildren || has); } var model = GetModelName(e); var existingModel = e.MetaData.Get <string>(ModelNameMetaKey); if (String.Equals(model, existingModel, StringComparison.InvariantCultureIgnoreCase)) { return(updatedChildren); // Already set; No need to continue } if (cache.ContainsKey(model)) { var mr = cache[model]; if (mr == null) { UnsetModel(e); } else { SetModel(e, mr); } return(true); } else { var file = new NativeFile(Directories.ModelDir + "/" + model); //TODO: try to understand how this originally worked? //document.Environment.Root.TraversePath(model); if (file == null || !ModelProvider.CanLoad(file)) { // Model not valid, get rid of it UnsetModel(e); cache.Add(model, null); return(true); } #if !DEBUG try { #endif var mr = ModelProvider.CreateModelReference(file); SetModel(e, mr); cache.Add(model, mr); return(true); #if !DEBUG } catch (Exception) { // Couldn't load cache.Add(model, null); return(updatedChildren); } #endif } }