private async Task LoadBlueprints(string blueprintDirectory) { if (string.IsNullOrWhiteSpace(blueprintDirectory)) { throw new ArgumentException($"'{nameof(blueprintDirectory)}' cannot be null or whitespace", nameof(blueprintDirectory)); } var knownWriteDateTime = DateTime.MinValue; int exceptions = 0; while (true) { try { var lastWriteDateTime = Directory.GetLastWriteTime(blueprintDirectory); if (knownWriteDateTime < lastWriteDateTime) { knownWriteDateTime = lastWriteDateTime; foreach (string bpDir in Directory.GetDirectories(blueprintDirectory)) { if (!LoadedBlueprints.ContainsKey(bpDir) && Directory.Exists(bpDir) && File.Exists($"{bpDir}/blueprint.json")) { BlueprintContext bp = BlueprintContext.FromFolderPath(bpDir); LoadedBlueprints.Add(bpDir, bp); GameObject newButton = buttonObjectPool.GetObject(); newButton.transform.SetParent(contentPanel); BlueprintButton blueprintButton = newButton.GetComponent <BlueprintButton>(); blueprintButton.Setup(bp); } } } } catch (Exception e) { if (exceptions++ < 10) { Debug.LogException(e); } } finally { await Task.Delay(2000); } } }
private void Start() { string[] paths = new string[] { Path.Combine(PathResolver.ScrapMechanicAppdataUserPath, "Blueprints"), PathResolver.WorkShopPath }; foreach (string path in paths) { _ = LoadBlueprints(path); } // UI button events: titleField.onValueChanged.AddListener(_ => applyButton.gameObject.SetActive(true)); descriptionField.onValueChanged.AddListener(_ => applyButton.gameObject.SetActive(true)); applyButton.onClick.AddListener(() => { BlueprintContext blueprintContextReference = SelectedBlueprintButton.BlueprintContextReference; DescriptionData descriptionData = blueprintContextReference.Description; try { descriptionData.Name = titleField.text; descriptionData.Description = descriptionField.text; blueprintContextReference.SaveDescription(); BlueprintButton[] blueprintbuttons = Resources.FindObjectsOfTypeAll <Model.Unity.BlueprintButton>(); blueprintbuttons.First(button => button.BlueprintContextReference == blueprintContextReference).Initialize(); applyButton.gameObject.SetActive(false); GameController.Instance.messageController.WarningMessage( "Title and description changes have been saved."); } catch (Exception e) { GameController.Instance.messageController.OkMessage( $"Could not save {blueprintContextReference.BlueprintFolderPath}/description.json\n{e.Message}"); } }); // file watcher: foreach (string path in paths) { using var watcher = new FileSystemWatcher(path); watcher.NotifyFilter = NotifyFilters.Size; watcher.IncludeSubdirectories = true; watcher.EnableRaisingEvents = true; watcher.Changed += OnChanged; } }
private void OnChanged(object sender, FileSystemEventArgs e)// different thread { try { string path = e.FullPath; bool exists = Directory.Exists(path) || File.Exists(path); if (exists) { FileAttributes fileAttributes = File.GetAttributes(path); if ((fileAttributes & FileAttributes.Directory) != FileAttributes.Directory) { path = Directory.GetParent(path)?.FullName; } if (LoadedBlueprints.TryGetValue(path, out var blueprintCtx)) { _ = RunMain(() => { blueprintCtx.LoadDescription(); blueprintCtx.LoadIcon(); blueprintCtx.btn.Initialize(); if (SelectedBlueprintButton == blueprintCtx.btn) { SelectBlueprintButton(SelectedBlueprintButton); } }).LogErrors(); } else if (File.Exists($"{path}/blueprint.json")) { _ = RunMain(() => { BlueprintContext bp = BlueprintContext.FromFolderPath(path); LoadedBlueprints.Add(path, bp); GameObject newButton = buttonObjectPool.GetObject(); newButton.transform.SetParent(contentPanel); BlueprintButton blueprintButton = newButton.GetComponent <BlueprintButton>(); blueprintButton.Setup(bp); }).LogErrors(); } } else { if (path.Contains('.')) // assume it's a file { path = path.Substring(0, path.LastIndexOf(Path.DirectorySeparatorChar)); } if (LoadedBlueprints.TryGetValue(path, out var blueprintCtx)) { _ = RunMain(() => { if (SelectedBlueprintButton == blueprintCtx.btn) { titleField.text = string.Empty; descriptionField.text = string.Empty; applyButton.gameObject.SetActive(false); } Destroy(blueprintCtx.btn.gameObject); LoadedBlueprints.Remove(path); }).LogErrors(); } } } catch (Exception ex) { Debug.LogException(ex); } }
public void Setup(BlueprintContext blueprintContext) { BlueprintContextReference = blueprintContext; blueprintContext.btn = this; }