private void SuperintendentSavingAll(object sender, EventArgs e) { foreach (var item in InnerWrappersList.Select(x => x.Value).OfType <T>().Where(x => x.Changed)) { item.SaveAsync(); } }
public IAcObjectNew AddNew(string id = null) { if (id == null) { id = Directories.GetUniqueId("SERVER", "_{0:D2}", true, 0); } var directory = Directories.GetLocation(id, true); if (Directory.Exists(directory)) { throw new InformativeException("Can’t add a new object", $"ID “{id}” is already taken."); } using (IgnoreChanges()) { Directory.CreateDirectory(directory); File.WriteAllText(Path.Combine(directory, "server_cfg.ini"), ""); File.WriteAllText(Path.Combine(directory, "entry_list.ini"), ""); var obj = CreateAndLoadAcObject(id, true); InnerWrappersList.Add(new AcItemWrapper(this, obj)); UpdateList(true); return(obj); } }
public IAcObjectNew AddNew(string id = null) { if (id == null) { id = Path.GetFileName(FileUtils.EnsureUnique(Path.Combine(Directories.EnabledDirectory, "SERVER"), "_{0:D2}", true, 0)) ?? @"CM_SERVER"; } var directory = Path.Combine(Directories.EnabledDirectory, id); if (Directory.Exists(directory)) { throw new InformativeException("Can’t add a new object", "This ID is already taken."); } using (IgnoreChanges()) { Directory.CreateDirectory(directory); File.WriteAllText(Path.Combine(directory, "server_cfg.ini"), ""); File.WriteAllText(Path.Combine(directory, "entry_list.ini"), ""); var obj = CreateAndLoadAcObject(id, true); InnerWrappersList.Add(new AcItemWrapper(this, obj)); UpdateList(); return(obj); } }
private void Superintendent_Closing(object sender, Superintendent.ClosingEventArgs e) { foreach (var item in InnerWrappersList.Select(x => x.Value).OfType <T>().Where(x => x.Changed)) { Logging.Debug(item); e.Add(item.DisplayName); } }
public IAcObjectNew AddNew(string id = null) { var newId = Guid.NewGuid() + UserChampionshipObject.FileExtension; var filename = Directories.GetLocation(newId, true); if (File.Exists(filename)) { throw new InformativeException("Can’t add a new object", "This ID is already taken."); } var defaultFilename = Path.Combine(AcRootDirectory.Instance.RequireValue, @"launcher", @"themes", @"default", @"modules", @"champs", @"default.json"); var data = File.Exists(defaultFilename) ? File.ReadAllText(defaultFilename) : @"{ ""name"":""My championship"", ""rules"":{""practice"":30,""qualifying"":60,""points"":[10,8,6,3,2,1],""penalties"":true,""jumpstart"":1}, ""opponents"":[{""name"":""PLAYER"",""skin"":""red_white"",""car"":""abarth500""}], ""rounds"":[{""track"":""magione"",""laps"":10,""weather"":4,""surface"":3}], ""maxCars"":18}"; var parsed = JObject.Parse(data); var name = parsed.GetStringValueOnly("name"); if (Enabled.Any(x => x.Name == name)) { for (var i = 1; i < 999; i++) { var candidate = $@"{name} ({i})"; if (Enabled.All(x => x.Name != candidate)) { name = candidate; break; } } parsed[@"name"] = name; data = parsed.ToString(Formatting.Indented); } using (IgnoreChanges()) { File.WriteAllText(filename, data); var obj = CreateAndLoadAcObject(newId, true); InnerWrappersList.Add(new AcItemWrapper(this, obj)); UpdateList(true); return(obj); } }
void IDirectoryListener.FileOrDirectoryDeleted(object sender, FileSystemEventArgs e) { if (ShouldIgnoreChanges()) { return; } // special case for whole directory being deleted if (e.Name == null) { var state = Directories.CheckIfEnabled(e.FullPath); ActionExtension.InvokeInMainThreadAsync(() => { while (InnerWrappersList.Remove(InnerWrappersList.FirstOrDefault(x => x.Value.Enabled == state))) { } }); return; } OnDeleted(e.FullPath); }
public IAcObjectNew AddNew(string id = null) { var mainDirectory = Directories.GetMainDirectory(); if (id == null) { var uniqueId = Path.GetFileName(FileUtils.EnsureUnique(Path.Combine(mainDirectory, "skin"))); id = Prompt.Show("Choose a name for a new car skin:", "New car skin", required: true, maxLength: 80, placeholder: "?", defaultValue: uniqueId); if (id == null) { return(null); } } var directory = Directories.GetLocation(id, true); if (Directory.Exists(directory)) { throw new InformativeException("Can’t add a new object", $"ID “{id}” is already taken."); } using (IgnoreChanges()) { Directory.CreateDirectory(directory); File.WriteAllText(Path.Combine(directory, "ui_skin.json"), new JObject { ["skinname"] = AcStringValues.NameFromId(id), ["drivername"] = "", ["country"] = "", ["team"] = "", ["number"] = "", ["priority"] = 0 }.ToString(Formatting.Indented)); var obj = CreateAndLoadAcObject(id, true); InnerWrappersList.Add(new AcItemWrapper(this, obj)); UpdateList(true); return(obj); } }
void IWatchingChangeApplier.ApplyChange(string dir, WatchingChange change) { #if LOGGING Debug.WriteLine($"ACMGR [NEW]: IWatchingChangeApplier.ApplyChange({dir}, {change.Type})\n" + $" ORIGINAL FILENAME: {change.FullFilename}\n" + $" NEW LOCATION: {change.NewLocation}"); #endif string id; try { id = LocationToId(dir); } catch (Exception) { // can’t get location from id return; } bool isFreshlyLoaded; var obj = GetById(id, out isFreshlyLoaded); #if LOGGING Debug.WriteLine($" id: {id}; object: {obj}; location: {obj?.Location}"); if (obj != null && !obj.Location.Equals(dir, StringComparison.OrdinalIgnoreCase)) { if (change.Type == WatcherChangeTypes.Created) { Debug.WriteLine(@" wrong location, removed"); RemoveFromList(obj.Id); } else { Debug.WriteLine(@" wrong location, nulled"); } obj = null; } #else if (obj != null && !obj.Location.Equals(dir, StringComparison.OrdinalIgnoreCase)) { if (change.Type == WatcherChangeTypes.Created) { RemoveFromList(obj.Id); } obj = null; } #endif switch (change.Type) { case WatcherChangeTypes.Changed: if (obj != null && !isFreshlyLoaded && (change.FullFilename == null || !obj.HandleChangedFile(change.FullFilename))) { obj.Reload(); } break; case WatcherChangeTypes.Created: if (obj != null) { if (!isFreshlyLoaded) { obj.Reload(); } } else if (FileUtils.Exists(dir) && Filter(dir)) { id = LocationToId(FileUtils.GetOriginalFilename(dir)); obj = CreateAndLoadAcObject(id, Directories.CheckIfEnabled(dir)); InnerWrappersList.Add(new AcItemWrapper(this, obj)); UpdateList(); } break; case WatcherChangeTypes.Deleted: if (obj != null) { if (FileUtils.Exists(dir) && Filter(dir)) { if (!isFreshlyLoaded) { obj.Reload(); } } else { RemoveFromList(obj.Id); } } break; case WatcherChangeTypes.Renamed: if (obj != null) { if (dir == change.NewLocation) { if (isFreshlyLoaded) { obj.Reload(); } break; } RemoveFromList(obj.Id); } if (FileUtils.Exists(change.NewLocation)) { obj = CreateAndLoadAcObject(LocationToId(change.NewLocation), Directories.CheckIfEnabled(change.NewLocation)); InnerWrappersList.Add(new AcItemWrapper(this, obj)); UpdateList(); } break; case WatcherChangeTypes.All: Logging.Warning("WatcherChangeTypes.All!"); break; default: throw new ArgumentOutOfRangeException(); } #if LOGGING Debug.WriteLine(@" current list: " + InnerWrappersList.Select(x => x.Value.Id).JoinToString(", ")); #endif }