Skip to content

Commit

Permalink
a few more changes
Browse files Browse the repository at this point in the history
  • Loading branch information
gro-ove committed Mar 1, 2019
1 parent 30739fc commit b1022eb
Show file tree
Hide file tree
Showing 36 changed files with 507 additions and 113 deletions.
1 change: 1 addition & 0 deletions AcManager.Controls/AcManager.Controls.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@
<Compile Include="Presentation\ExtraProgressRings.cs" />
<Compile Include="Presentation\TitleLinkEnabledEntry.cs" />
<Compile Include="RaceGridEditorTable.cs" />
<Compile Include="Services\TimeSliderService.cs" />
<Compile Include="TemperatureValueLabel.cs" />
<Compile Include="AcObjectBase.cs" />
<Compile Include="AcObjectErrorsSection.cs" />
Expand Down
36 changes: 36 additions & 0 deletions AcManager.Controls/Services/TimeSliderService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Windows;
using System.Windows.Controls;
using AcManager.Tools.Data;
using AcTools;
using AcTools.Utils;

namespace AcManager.Controls.Services {
public static class TimeSliderService {
public static bool GetIsTimeSlider(DependencyObject obj) {
return (bool)obj.GetValue(IsTimeSliderProperty);
}

public static void SetIsTimeSlider(DependencyObject obj, bool value) {
obj.SetValue(IsTimeSliderProperty, value);
}

public static readonly DependencyProperty IsTimeSliderProperty = DependencyProperty.RegisterAttached("IsTimeSlider", typeof(bool),
typeof(TimeSliderService), new UIPropertyMetadata(OnIsTimeSliderChanged));

private static void OnIsTimeSliderChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
var element = d as Slider;
if (element == null || !(e.NewValue is bool)) return;

var newValue = (bool)e.NewValue;
if (!newValue) return;

if (PatchHelper.IsFeatureSupported(PatchHelper.FeatureFullDay)) {
element.Minimum = 0;
element.Maximum = CommonAcConsts.TimeAbsoluteMaximum;
} else {
element.Minimum = CommonAcConsts.TimeMinimum;
element.Maximum = CommonAcConsts.TimeMaximum;
}
}
}
}
1 change: 1 addition & 0 deletions AcManager.Tools/AcManager.Tools.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@
<Compile Include="Helpers\AcSettings\CameraChaseSettings.cs" />
<Compile Include="Helpers\AcSettings\DamageDisplayerSettings.cs" />
<Compile Include="Helpers\AcSettings\MessagesSettings.cs" />
<Compile Include="Helpers\AcSettings\PitMenuSettings.cs" />
<Compile Include="Helpers\Api\ApiCacheThing.cs" />
<Compile Include="Helpers\Api\HttpClientHolder.cs" />
<Compile Include="Helpers\Api\Kunos\InvertConverter.cs" />
Expand Down
103 changes: 96 additions & 7 deletions AcManager.Tools/Data/PatchHelper.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using AcManager.Tools.Managers;
using AcTools;
using AcTools.DataFile;
using AcTools.Utils;
using AcTools.Utils.Helpers;
using FirstFloor.ModernUI;
using FirstFloor.ModernUI.Helpers;
using FirstFloor.ModernUI.Serialization;
using JetBrains.Annotations;
using StringBasedFilter;
using StringBasedFilter.Parsing;
using StringBasedFilter.TestEntries;

namespace AcManager.Tools.Data {
public class PatchHelper {
public static readonly string FeatureFullDay = "CONDITIONS_24H";
public static readonly string FeatureSpecificDate = "CONDITIONS_SPECIFIC_DATE";
public static readonly string FeatureCustomGForces = "VIEW_CUSTOM_GFORCES";
public static readonly string FeatureExtraScreenshotFormats = "EXTRA_SCREENSHOT_FORMATS";
public static readonly string FeatureNeedsOdometerValue = "NEEDS_ODOMETER_VALUE";
public static readonly string FeatureDynamicShadowResolution = "DYNAMIC_SHADOWS_RESOLUTION";
public static readonly string FeaturePovForButtons = "POV_FOR_BUTTONS";

public static bool OptionPatchSupport = false;

public static readonly string MainFileName = "dwrite.dll";
Expand All @@ -23,15 +39,89 @@ public static string GetRootDirectory() {
return Path.Combine(AcRootDirectory.Instance.RequireValue, "extension");
}

public static string GetConfigFilename(string configName) {
return Path.Combine(GetRootDirectory(), "config", configName);
}

public static string GetUserConfigFilename(string configName) {
return Path.Combine(AcPaths.GetDocumentsCfgDirectory(), "extension", configName);
}

public static string GetManifestFilename() {
return Path.Combine(GetRootDirectory(), "config", "data_manifest.ini");
return GetConfigFilename("data_manifest.ini");
}

public static string GetInstalledLog() {
return Path.Combine(GetRootDirectory(), "installed.log");
}

private static IniFile _manifest;
private static Dictionary<string, IniFile> _configs = new Dictionary<string, IniFile>();
private static Dictionary<string, bool> _featureSupported = new Dictionary<string, bool>();

[CanBeNull]
private static string TryToRead(string filename) {
try {
if (File.Exists(filename)) {
return FileUtils.ReadAllText(filename);
}
} catch (Exception e) {
Logging.Error(e);
}
return null;
}

public static IniFile GetConfig([NotNull] string configName) {
return _configs.GetValueOrSet(configName, () => IniFile.Parse(
TryToRead(GetConfigFilename(configName))
+ Environment.NewLine
+ TryToRead(GetUserConfigFilename(configName))));
}

private class FeatureTester : ITester<object> {
public string ParameterFromKey(string key) {
return null;
}

public bool Test(object obj, string key, ITestEntry value) {
return value.Test(null);
}
}

private static ITestEntry FeatureTestEntryFactory(string item) {
var v = item.As<bool?>();
if (v.HasValue) {
return new ConstTestEntry(v.Value);
}

var split = item.Split(new[] { ':' }, 2);
var config = GetConfig(split[0].Trim());
if (split.Length == 1) {
return new ConstTestEntry(config["BASIC"].GetBool("ENABLED", false));
}

var keyValue = split[1].Split(new[] { '=' }, 2);
var sectionKey = keyValue[0].Split(new[] { '/' }, 2);
return new ConstTestEntry(keyValue.Length == 1
? config[sectionKey[0].Trim()].GetBool(sectionKey.Length == 2 ? sectionKey[1].Trim() : @"ENABLED", false)
: config[sectionKey[0].Trim()].GetPossiblyEmpty(sectionKey.Length == 2 ? sectionKey[1].Trim() : @"ENABLED") == keyValue[1].Trim());
}

public static bool IsFeatureSupported([NotNull] string featureId) {
return _featureSupported.GetValueOrSet(featureId, () => {
if (GetInstalledVersion() == null || !GetConfig("general.ini")["BASIC"].GetBool("ENABLED", true)) return false;
var query = GetManifest()["FEATURES"].GetNonEmpty(featureId);
if (string.IsNullOrWhiteSpace(query)) return false;
var filter = Filter.Create(query, new FilterParams { CustomTestEntryFactory = FeatureTestEntryFactory });
return filter.Test(new FeatureTester(), new object());
});
}

public static int ClampTime(int time) {
return IsFeatureSupported(FeatureFullDay)
? time.Clamp(0, CommonAcConsts.TimeAbsoluteMaximum)
: time.Clamp(CommonAcConsts.TimeMinimum, CommonAcConsts.TimeMaximum);
}

private static Lazier<Tuple<string, string>> _installed;

static PatchHelper() {
Expand All @@ -58,7 +148,7 @@ static PatchHelper() {
}

public static IniFile GetManifest() {
return _manifest ?? (_manifest = new IniFile(GetManifestFilename()));
return GetConfig("data_manifest.ini");
}

[CanBeNull]
Expand All @@ -74,11 +164,10 @@ public static string GetInstalledBuild() {
}

public static void Reload() {
_manifest = null;
_configs.Clear();
_featureSupported.Clear();
_installed.Reset();
ActionExtension.InvokeInMainThreadAsync(() => {
Reloaded?.Invoke(null, EventArgs.Empty);
});
ActionExtension.InvokeInMainThreadAsync(() => { Reloaded?.Invoke(null, EventArgs.Empty); });
}
}
}
2 changes: 1 addition & 1 deletion AcManager.Tools/Data/WeatherTypeExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ void AddType(WeatherType t, double baseDistance) {

private static bool FitsTime(WeatherObject weatherObject, int time) {
return weatherObject.GetTimeDiapason()?.Contains(time)
?? time >= CommonAcConsts.TimeMinimum && time <= CommonAcConsts.TimeMaximum;
?? PatchHelper.ClampTime(time) == time;
}

public static bool Fits(this WeatherObject weatherObject, int? time, double? temperature) {
Expand Down
4 changes: 4 additions & 0 deletions AcManager.Tools/Helpers/AcSettings/AcSettingsHolder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,10 @@ public static ControlsSettings Controls {
public static SystemSettings System => _system ?? (_system = new SystemSettings());


private static PitMenuSettings _pitMenu;
public static PitMenuSettings PitMenu => _pitMenu ?? (_pitMenu = new PitMenuSettings());


private static FfPostProcessSettings _ffPostProcess;
public static FfPostProcessSettings FfPostProcess => _ffPostProcess ?? (_ffPostProcess = new FfPostProcessSettings());

Expand Down
56 changes: 56 additions & 0 deletions AcManager.Tools/Helpers/AcSettings/PitMenuSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
namespace AcManager.Tools.Helpers.AcSettings {
public class PitMenuSettings : IniSettings {
internal PitMenuSettings() : base("pitstop", systemConfig: true) { }

private bool _useMousePitstop;

public bool UseMousePitstop {
get => _useMousePitstop;
set => Apply(value, ref _useMousePitstop);
}

private bool _stayInCar;

public bool StayInCar {
get => _stayInCar;
set => Apply(value, ref _stayInCar);
}

private bool _autoAppOnPitlane;

public bool AutoAppOnPitlane {
get => _autoAppOnPitlane;
set => Apply(value, ref _autoAppOnPitlane);
}

private int _visibilityMaxTime;

public int VisibilityMaxTime {
get => _visibilityMaxTime;
set => Apply(value, ref _visibilityMaxTime);
}

private int _presetsCount;

public int PresetsCount {
get => _presetsCount;
set => Apply(value, ref _presetsCount);
}

protected override void LoadFromIni() {
UseMousePitstop = Ini["SETTINGS"].GetBool("USE_MOUSE_PITSTOP", false);
StayInCar = Ini["SETTINGS"].GetBool("STAY_IN_CAR", true);
AutoAppOnPitlane = Ini["SETTINGS"].GetBool("AUTO_APP_ON_PITLANE", true);
VisibilityMaxTime = Ini["SETTINGS"].GetInt("VISIBILITY_MAX_TIME", 5);
PresetsCount = Ini["SETTINGS"].GetInt("PRESETS_COUNT", 5);
}

protected override void SetToIni() {
Ini["SETTINGS"].Set("USE_MOUSE_PITSTOP", UseMousePitstop);
Ini["SETTINGS"].Set("STAY_IN_CAR", StayInCar);
Ini["SETTINGS"].Set("AUTO_APP_ON_PITLANE", AutoAppOnPitlane);
Ini["SETTINGS"].Set("VISIBILITY_MAX_TIME", VisibilityMaxTime);
Ini["SETTINGS"].Set("PRESETS_COUNT", PresetsCount);
}
}
}
29 changes: 22 additions & 7 deletions AcManager.Tools/Helpers/AcSettings/SystemSettings.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
using System.Linq;
using System.Collections.Generic;
using System.Linq;
using AcTools.DataFile;
using AcTools.Utils;
using AcTools.Utils.Helpers;
using FirstFloor.ModernUI.Helpers;

namespace AcManager.Tools.Helpers.AcSettings {
public class SystemSettings : IniSettings {
internal SystemSettings() : base("assetto_corsa", systemConfig: true) { }

public SettingEntry[] ScreenshotFormats { get; } = {
new SettingEntry("JPG", ToolsStrings.AcSettings_ScreenshotFormat_Jpeg),
new SettingEntry("BMP", ToolsStrings.AcSettings_ScreenshotFormat_Bmp),
new SettingEntry("PNG", "PNG (works only with Custom Shaders Patch)"),
};
public static IEnumerable<SettingEntry> DefaultScreenshotFormats() {
return new[] {
new SettingEntry("JPG", ToolsStrings.AcSettings_ScreenshotFormat_Jpeg),
new SettingEntry("BMP", ToolsStrings.AcSettings_ScreenshotFormat_Bmp)
};
}

private List<SettingEntry> _screenshotFormats = DefaultScreenshotFormats().ToList();

public IReadOnlyList<SettingEntry> ScreenshotFormats {
get => _screenshotFormats;
set {
if (value.SequenceEqual(_screenshotFormats)) return;
_screenshotFormats = value.ToList();
OnPropertyChanged(false);
ScreenshotFormat = _screenshotFormats.GetByIdOrDefault(ScreenshotFormat?.Id) ?? _screenshotFormats.FirstOrDefault();
}
}

#region Miscellaneous
private int _simulationValue;
Expand Down Expand Up @@ -181,7 +196,7 @@ protected override void LoadFromIni() {
AllowFreeCamera = Ini["CAMERA"].GetBool("ALLOW_FREE_CAMERA", false);
Logging = !Ini["LOG"].GetBool("SUPPRESS", false);
HideDriver = Ini["DRIVER"].GetBool("HIDE", false);
ScreenshotFormat = Ini["SCREENSHOT"].GetEntry("FORMAT", ScreenshotFormats);
ScreenshotFormat = Ini["SCREENSHOT"].GetOrCreateEntry("FORMAT", _screenshotFormats, v => $"{v} (not supported)");
MirrorsFieldOfView = Ini["MIRRORS"].GetInt("FOV", MirrorsFieldOfViewDefault);
MirrorsFarPlane = Ini["MIRRORS"].GetInt("FAR_PLANE", MirrorsFarPlaneDefault);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using AcManager.Tools.Data;
using AcManager.Tools.Helpers.DirectInput;
using AcTools.DataFile;

Expand All @@ -12,7 +13,9 @@ public WheelButtonEntry(string id, string name, bool supportsPov = false) : base
}

public override bool IsCompatibleWith(DirectInputButton obj) {
return _supportsPov ? base.IsCompatibleWith(obj) : obj?.GetType() == typeof(DirectInputButton);
return _supportsPov || PatchHelper.IsFeatureSupported(PatchHelper.FeaturePovForButtons)
? base.IsCompatibleWith(obj)
: obj?.GetType() == typeof(DirectInputButton);
}

public override void Load(IniFile ini, IReadOnlyList<IDirectInputDevice> devices) {
Expand Down
2 changes: 1 addition & 1 deletion AcManager.Tools/Helpers/DirectInput/DirectInputDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ public void OnTick() {
var buttons = state.GetButtons();
for (var i = 0; i < Buttons.Length; i++) {
var b = Buttons[i];
b.Value = b.Id < buttons.Length && buttons[b.Id] && (b.Id % 2 == Index % 2);
b.Value = b.Id < buttons.Length && buttons[b.Id];
}

var povs = state.GetPointOfViewControllers();
Expand Down
16 changes: 15 additions & 1 deletion AcManager.Tools/Helpers/IniFileExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,19 @@ public static SettingEntry GetEntry(this IniFileSection section, [LocalizationRe
return entries.FirstOrDefault(x => x.Value == value) ?? entries.First();
}

[NotNull]
public static SettingEntry GetOrCreateEntry(this IniFileSection section, [LocalizationRequired(false)] string key, IList<SettingEntry> entries,
Func<string, string> nameCallback) {
var value = section.GetNonEmpty(key);
var entry = entries.FirstOrDefault(x => x.Value == value);
if (entry != null) {
return entry;
}
if (string.IsNullOrWhiteSpace(value)) return entries.First();
entries.Add(new SettingEntry(value, nameCallback(value)));
return entries.Last();
}

public static void Set(this IniFileSection section, [LocalizationRequired(false)] string key, SettingEntry entry) {
section.Set(key, entry.Value);
}
Expand All @@ -54,7 +67,8 @@ public static Color GetColor(this IniFileSection section, [LocalizationRequired(
return result.Length == 3 ? Color.FromRgb(result[0], result[1], result[2]) : defaultValue;
}

public static Color GetColor(this IniFileSection section, [LocalizationRequired(false)] string key, Color defaultValue, double defaultMultipler, out double multipler) {
public static Color GetColor(this IniFileSection section, [LocalizationRequired(false)] string key, Color defaultValue, double defaultMultipler,
out double multipler) {
var strings = section.GetStrings(key);
var result = strings.Select(x => FlexibleParser.ParseInt(x, 0).ClampToByte()).ToArray();
if (strings.Length != 4) {
Expand Down
6 changes: 5 additions & 1 deletion AcManager.Tools/Managers/AcRootDirectory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,11 @@ public static bool CheckDirectory(string directory, out string reason) {
return false;
}

if (!Directory.Exists(Path.Combine(directory, @"apps"))) {
var appsDirectory = Path.Combine(directory, @"apps");
if (!Directory.Exists(appsDirectory)) {
Logging.Warning("Apps folder not found: " + appsDirectory);
Logging.Warning("All directories found: " + Directory.GetDirectories(directory).JoinToString(@", "));
Logging.Warning("All files found: " + Directory.GetFiles(directory).JoinToString(@", "));
reason = string.Format(ToolsStrings.AcRootDirectory_MissingDirectory, @"apps");
return false;
}
Expand Down
Loading

0 comments on commit b1022eb

Please sign in to comment.