Ejemplo n.º 1
0
        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);
            }
        }