Пример #1
0
        public static TyresSet CreateTyresSet([NotNull] this TyresMachine machine, [NotNull] TyresEntry frontOriginal, [NotNull] TyresEntry rearOriginal,
                                              [CanBeNull] string name, [CanBeNull] string shortName)
        {
            var front = CreateTyresEntry(machine, frontOriginal, name, shortName);
            var rear  = CreateTyresEntry(machine, rearOriginal, name, shortName);

            return(new TyresSet(front, rear));
        }
Пример #2
0
        public static IEnumerable <TyresSet> GetTyresSets([NotNull] this CarObject car)
        {
            var tyres = car.AcdData?.GetIniFile("tyres.ini");

            if (tyres?.IsEmptyOrDamaged() != false)
            {
                return(new TyresSet[0]);
            }

            var defaultSet = tyres["COMPOUND_DEFAULT"].GetInt("INDEX", 0);

            return(TyresEntry.GetTyres(car).Where(x => x.Item1 != null && x.Item2 != null).Select((x, i) => new TyresSet(x.Item1, x.Item2)
            {
                DefaultSet = i == defaultSet
            }));
        }
Пример #3
0
        public static TyresSet GetOriginalTyresSet([NotNull] this CarObject car)
        {
            var tyres = car.AcdData?.GetIniFile("tyres.ini");

            if (tyres?.IsEmptyOrDamaged() != false)
            {
                return(null);
            }

            var front = TyresEntry.Create(car, @"__CM_FRONT_ORIGINAL", true);
            var rear  = TyresEntry.Create(car, @"__CM_REAR_ORIGINAL", true);

            if (front != null && rear != null)
            {
                return(new TyresSet(front, rear));
            }
            else
            {
                return(null);
            }
        }
Пример #4
0
        public static void Save([NotNull] this IEnumerable <TyresSet> sets, int setsVersion, [NotNull] CarObject car,
                                [CanBeNull] TyresEntry originalTyresFront, [CanBeNull] TyresEntry originalTyresRear, bool keepCurves = false)
        {
            try {
                var uniqueSets = sets.Distinct(TyresSet.TyresSetComparer).ToList();

                if (uniqueSets.Count == 0)
                {
                    throw new Exception("At least one set is required");
                }

                if (!uniqueSets.All(x => x.Front.Version == setsVersion && x.Rear.Version == setsVersion))
                {
                    throw new Exception("Versions are different");
                }

                var data = car.AcdData;
                if (data == null)
                {
                    throw new Exception("Data is unreadable");
                }

                var tyresIni = data.GetIniFile("tyres.ini");
                tyresIni["HEADER"].Set("VERSION", setsVersion);

                var defaultIndex = uniqueSets.FindIndex(x => x.DefaultSet);
                tyresIni["COMPOUND_DEFAULT"].Set("INDEX", defaultIndex == -1 ? 0 : defaultIndex);

                if (originalTyresFront != null)
                {
                    tyresIni["__CM_FRONT_ORIGINAL"]         = originalTyresFront.MainSection;
                    tyresIni["__CM_THERMAL_FRONT_ORIGINAL"] = originalTyresFront.ThermalSection;
                }

                if (originalTyresRear != null)
                {
                    tyresIni["__CM_REAR_ORIGINAL"]         = originalTyresRear.MainSection;
                    tyresIni["__CM_THERMAL_REAR_ORIGINAL"] = originalTyresRear.ThermalSection;
                }

                SetTyres(true);
                SetTyres(false);

                void SetTyres(bool isRear)
                {
                    var key        = isRear ? "REAR" : "FRONT";
                    var thermalKey = $"THERMAL_{key}";

                    var currentTyres        = keepCurves ? tyresIni.GetSections(key, -1).ToList() : null;
                    var currentThermalTyres = keepCurves ? tyresIni.GetSections(thermalKey, -1).ToList() : null;

                    tyresIni.SetSections(key, -1, uniqueSets.Select((x, i) => {
                        var entry     = isRear ? x.Rear : x.Front;
                        var curveName = currentTyres?.ElementAtOrDefault(i)?.GetNonEmpty("WEAR_CURVE");
                        if (!keepCurves || curveName == null)
                        {
                            var curve     = data.GetRawFile($@"__cm_tyre_wearcurve_{key.ToLowerInvariant()}_{i}.lut");
                            curve.Content = entry.WearCurveData ?? "";
                            curve.Save();
                            curveName = curve.Name;
                        }

                        return(FixDigits(new IniFileSection(data, entry.MainSection)
                        {
                            ["NAME"] = x.GetName(),
                            ["SHORT_NAME"] = x.GetShortName(),
                            ["WEAR_CURVE"] = curveName,
                            ["__CM_SOURCE_ID"] = entry.SourceCarId
                        }));
                    }));

                    tyresIni.SetSections(thermalKey, -1, uniqueSets.Select((x, i) => {
                        var entry     = isRear ? x.Rear : x.Front;
                        var curveName = currentThermalTyres?.ElementAtOrDefault(i)?.GetNonEmpty("PERFORMANCE_CURVE");
                        if (!keepCurves || curveName == null)
                        {
                            var curve     = data.GetRawFile($@"__cm_tyre_perfcurve_{key.ToLowerInvariant()}_{i}.lut");
                            curve.Content = entry.PerformanceCurveData ?? "";
                            curve.Save();
                            curveName = curve.Name;
                        }

                        return(FixDigits(new IniFileSection(data, entry.ThermalSection)
                        {
                            ["PERFORMANCE_CURVE"] = curveName
                        }));
                    }));
                }

                IniFileSection FixDigits(IniFileSection result)
                {
                    foreach (var key in result.Keys.ToList())
                    {
                        var digits = GetValueDigits(key);
                        if (digits != null)
                        {
                            result.Set(key, result.GetDouble(key, 0d), "F" + digits);
                        }
                    }
                    return(result);
                }

                tyresIni.Save(true);
            } catch (Exception e) {
                NonfatalError.Notify("Can’t save changes", e);
            }
        }
Пример #5
0
 public static void Save([NotNull] this TyresSet sets, int setsVersion, [NotNull] CarObject car,
                         [CanBeNull] TyresEntry originalTyresFront, [CanBeNull] TyresEntry originalTyresRear, bool keepCurves = false)
 {
     Save(new[] { sets }, setsVersion, car, originalTyresFront, originalTyresRear, keepCurves);
 }
Пример #6
0
 public static TyresEntry CreateTyresEntry([NotNull] this TyresMachine machine, [NotNull] TyresEntry original,
                                           [CanBeNull] string name, [CanBeNull] string shortName)
 {
     return(machine.Conjure(original.Width, original.Radius, original.Radius - original.RimRadius)
            .ToTyresEntry(original, name, shortName));
 }
Пример #7
0
 public static NeuralTyresEntry ToNeuralTyresEntry(this TyresEntry entry)
 {
     return(new NeuralTyresEntry(entry.SourceCarId, entry.Version, entry.MainSection, entry.ThermalSection));
 }
Пример #8
0
        public static TyresEntry ToTyresEntry([NotNull] this NeuralTyresEntry values, [CanBeNull] TyresEntry original,
                                              [CanBeNull] string name, [CanBeNull] string shortName)
        {
            var mainSection    = original?.MainSection.Clone() ?? new IniFileSection(null);
            var thermalSection = original?.ThermalSection.Clone() ?? new IniFileSection(null);

            mainSection.Set("NAME", name.Or(values.Name));
            mainSection.Set("SHORT_NAME", shortName.Or(values.ShortName));

            foreach (var key in values.Keys.ApartFrom(NeuralTyresEntry.TemporaryKeys))
            {
                if (key.StartsWith(ThermalPrefix))
                {
                    var actualKey = key.Substring(ThermalPrefix.Length);
                    thermalSection.Set(actualKey, Fix(values[key], key));
                }
                else
                {
                    mainSection.Set(key, Fix(values[key], key));
                }
            }

            return(new TyresEntry(original?.SourceCarId, values.Version, mainSection, thermalSection,
                                  original?.WearCurveData, original?.PerformanceCurveData, original?.RearTyres ?? false, null));

            double Fix(double value, string key)
            {
                var digits = GetValueDigits(key);

                return(digits.HasValue ? FlexibleParser.ParseDouble(value.ToString($@"F{digits.Value}"), value) : value);
            }
        }