chip-gameV3/Assets/Scripts/Input/ComInputConfiguration.cs

140 lines
4.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 = 2,
Down = 3,
Left = 1,
Action = 8
};
[JsonProperty("rightJoystick")]
public ComJoystickConfiguration RightJoystick { get; set; } = new()
{
Up = 4,
Right = 7,
Down = 6,
Left = 5,
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();
}
}