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) { 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); } }
public static string GetMainDirectory(this IAcDirectories directories) { return(Path.GetDirectoryName(directories.GetLocation(@"_", true))); }