80 lines
2.6 KiB
C#
80 lines
2.6 KiB
C#
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
|
||
public class PasswordCatcher : MonoBehaviour
|
||
{
|
||
bool passwordForSettings;
|
||
private void Start()
|
||
{
|
||
passwordForSettings = SettingsManager.GetBool("PasswordOnSettings", false);
|
||
SettingsManager.OnSettingsChanged.AddListener(ApplySettings);
|
||
}
|
||
|
||
|
||
void ApplySettings()
|
||
{
|
||
bool passwordForSettingsNewValue = SettingsManager.GetBool("PasswordOnSettings", false);
|
||
if (NeedToSetPassword(passwordForSettingsNewValue))
|
||
{
|
||
passwordForSettings = passwordForSettingsNewValue;
|
||
GameManager.instance.stateMachine.ChangeState(new SetupPasswordState());
|
||
}
|
||
if (NeedToRemovePassword(passwordForSettingsNewValue))
|
||
{
|
||
ExternalFilesGetter.SetPassword("");
|
||
print("Пароль сброшен");
|
||
}
|
||
}
|
||
|
||
bool NeedToRemovePassword(bool newValue)
|
||
{
|
||
//значение поменялось на не нужен пароль
|
||
return newValue != passwordForSettings && newValue == false;
|
||
}
|
||
bool NeedToSetPassword(bool passwordForSettingsNewValue)
|
||
{
|
||
//поменялось значение настройки на "Пароль нужен"
|
||
bool changeStateToNeedPassword = passwordForSettingsNewValue != passwordForSettings && passwordForSettingsNewValue;
|
||
if (changeStateToNeedPassword) return true;
|
||
|
||
//Должен быть пароль но его нет или он не валидный.
|
||
if(passwordForSettingsNewValue && PasswordFileNonValid()) return true;
|
||
|
||
return false;
|
||
}
|
||
|
||
bool PasswordFileNonValid()
|
||
{
|
||
string password = ExternalFilesGetter.GetSettingsPassword();
|
||
if (password == null) return true;
|
||
if(password.Length % 2 == 1) return true;//длина должны быть четной
|
||
if (!PwdCombinationsValid(SplitPasswordIntoPairs(password))) return true; // парольные комбинации валидны?
|
||
|
||
return false;
|
||
}
|
||
|
||
List<string> SplitPasswordIntoPairs(string password)
|
||
{
|
||
List<string> result = new List<string>();
|
||
for (int i = 0; i < password.Length; i += 2)
|
||
{
|
||
result.Add(password.Substring(i, 2));
|
||
}
|
||
|
||
return result;
|
||
}
|
||
|
||
bool PwdCombinationsValid(List<string> combinations)
|
||
{
|
||
List<string> validCombinations = new List<string> { "L1", "L2", "R1", "R2", "A1", "A2", "P1", "P2" };
|
||
|
||
foreach (string combination in combinations)
|
||
{
|
||
if (!validCombinations.Contains(combination)) return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
}
|