140 lines
4.2 KiB
C#
140 lines
4.2 KiB
C#
using Newtonsoft.Json;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.IO;
|
||
using UnityEngine;
|
||
|
||
[Serializable]
|
||
public sealed class ComJoystickConfiguration
|
||
{
|
||
[JsonProperty("up")]
|
||
public int Up { get; set; }
|
||
|
||
[JsonProperty("right")]
|
||
public int Right { get; set; }
|
||
|
||
[JsonProperty("down")]
|
||
public int Down { get; set; }
|
||
|
||
[JsonProperty("left")]
|
||
public int Left { get; set; }
|
||
|
||
[JsonProperty("action")]
|
||
public int Action { get; set; }
|
||
}
|
||
|
||
[Serializable]
|
||
public sealed class ComInputConfiguration
|
||
{
|
||
[JsonProperty("leftJoystick")]
|
||
public ComJoystickConfiguration LeftJoystick { get; set; } = new()
|
||
{
|
||
Up = 0,
|
||
Right = 1,
|
||
Down = 2,
|
||
Left = 3,
|
||
Action = 4
|
||
};
|
||
|
||
[JsonProperty("rightJoystick")]
|
||
public ComJoystickConfiguration RightJoystick { get; set; } = new()
|
||
{
|
||
Up = 5,
|
||
Right = 6,
|
||
Down = 7,
|
||
Left = 8,
|
||
Action = 9
|
||
};
|
||
|
||
[JsonProperty("serviceButton")]
|
||
public int ServiceButton { get; set; } = 10;
|
||
|
||
public bool TryValidate(out string error)
|
||
{
|
||
if (LeftJoystick == null || RightJoystick == null)
|
||
{
|
||
error = "Не заданы настройки одного из джойстиков.";
|
||
return false;
|
||
}
|
||
|
||
int[] indices =
|
||
{
|
||
LeftJoystick.Up,
|
||
LeftJoystick.Right,
|
||
LeftJoystick.Down,
|
||
LeftJoystick.Left,
|
||
LeftJoystick.Action,
|
||
RightJoystick.Up,
|
||
RightJoystick.Right,
|
||
RightJoystick.Down,
|
||
RightJoystick.Left,
|
||
RightJoystick.Action,
|
||
ServiceButton
|
||
};
|
||
|
||
HashSet<int> uniqueIndices = new();
|
||
foreach (int index in indices)
|
||
{
|
||
if (index < 0 || index > ComInputConfigurationLoader.MaxControlIndex)
|
||
{
|
||
error = $"Индекс {index} находится вне допустимого диапазона 0-{ComInputConfigurationLoader.MaxControlIndex}.";
|
||
return false;
|
||
}
|
||
|
||
if (!uniqueIndices.Add(index))
|
||
{
|
||
error = $"Индекс {index} назначен нескольким элементам управления.";
|
||
return false;
|
||
}
|
||
}
|
||
|
||
error = string.Empty;
|
||
return true;
|
||
}
|
||
}
|
||
|
||
public static class ComInputConfigurationLoader
|
||
{
|
||
private const int _maxControlIndex = 13;
|
||
private const string _fileName = "ComInputConfig.json";
|
||
|
||
public static int MaxControlIndex => _maxControlIndex;
|
||
public static string FileName => _fileName;
|
||
|
||
public static string ConfigPath => Path.Combine(Application.streamingAssetsPath, FileName);
|
||
|
||
public static ComInputConfiguration Load()
|
||
{
|
||
if (!File.Exists(ConfigPath))
|
||
{
|
||
Debug.LogWarning($"Конфигурация COM-управления не найдена: {ConfigPath}. Используется схема по умолчанию.");
|
||
return new ComInputConfiguration();
|
||
}
|
||
|
||
try
|
||
{
|
||
string json = File.ReadAllText(ConfigPath);
|
||
ComInputConfiguration configuration = JsonConvert.DeserializeObject<ComInputConfiguration>(json);
|
||
if (configuration == null)
|
||
{
|
||
Debug.LogError("Конфигурация COM-управления пуста. Используется схема по умолчанию.");
|
||
return new ComInputConfiguration();
|
||
}
|
||
|
||
if (configuration.TryValidate(out string error))
|
||
{
|
||
Debug.Log($"Конфигурация COM-управления загружена: {ConfigPath}");
|
||
return configuration;
|
||
}
|
||
|
||
Debug.LogError($"Некорректная конфигурация COM-управления: {error} Используется схема по умолчанию.");
|
||
}
|
||
catch (Exception exception)
|
||
{
|
||
Debug.LogError($"Не удалось загрузить конфигурацию COM-управления: {exception.Message}. Используется схема по умолчанию.");
|
||
}
|
||
|
||
return new ComInputConfiguration();
|
||
}
|
||
}
|