281 lines
9.8 KiB
C#
281 lines
9.8 KiB
C#
using System;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using System.Net.NetworkInformation;
|
||
using UnityEngine;
|
||
|
||
public static class ExternalFilesGetter
|
||
{
|
||
const string GAMEDATA_FOLDER = "C4";
|
||
const string IMAGES_DIR = "Images";
|
||
const string SERIAL_PATH = "serialdata.dat";
|
||
const string OVERRIDES_FILE = "overrides/Light.conf";
|
||
const string ELEKTRO_VERSION_FILE = "elektro_version.rr";
|
||
const string SENSOR_VERSION_FILE = "HopperSensor.rr";
|
||
const string SETTINGS_PASSWORD = "SettingsPwd.rr";
|
||
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
/// <param name="spriteName">передавать без расширения</param>
|
||
/// <returns></returns>
|
||
public static Sprite GetChipSprite(string spriteName)
|
||
{
|
||
string docPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
|
||
string path = Path.Combine(docPath, GAMEDATA_FOLDER,IMAGES_DIR, spriteName+".png");
|
||
|
||
//проверить есть ли файл
|
||
if (File.Exists(path))
|
||
{
|
||
var bytes = File.ReadAllBytes(path);
|
||
var tex = new Texture2D(2,2,TextureFormat.ARGB32, false);
|
||
tex.LoadImage(bytes);
|
||
tex.filterMode = FilterMode.Point;
|
||
|
||
|
||
var sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f),pixelsPerUnit: 625);
|
||
return sprite;
|
||
}
|
||
else
|
||
{
|
||
Debug.LogWarning($"Не найден файл {spriteName}");
|
||
return null;
|
||
}
|
||
|
||
}
|
||
|
||
/// <summary>
|
||
/// Читает серийные данные из файла serialdata.dat
|
||
/// </summary>
|
||
/// <returns>Текст из файла или "<не указано>", если файл не существует</returns>
|
||
public static string GetSerial()
|
||
{
|
||
string docPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
|
||
string filePath = Path.Combine(docPath, GAMEDATA_FOLDER, SERIAL_PATH);
|
||
|
||
try
|
||
{
|
||
// Проверяем существование файла
|
||
if (!File.Exists(filePath))
|
||
{
|
||
Debug.LogWarning($"Файл serialdata.dat не найден по пути: {filePath}");
|
||
return "<не указано>";
|
||
}
|
||
|
||
// Читаем весь текст из файла
|
||
string serialData = File.ReadAllText(filePath);
|
||
|
||
// Убираем лишние пробелы и проверяем на пустоту
|
||
if (string.IsNullOrWhiteSpace(serialData))
|
||
{
|
||
Debug.LogWarning("Файл serialdata.dat пуст");
|
||
return "<не указано>";
|
||
}
|
||
|
||
return serialData.Trim();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Debug.LogError($"Ошибка при чтении файла serialdata.dat: {ex.Message}");
|
||
return "<не указано>";
|
||
}
|
||
}
|
||
|
||
public static string GetLightVersion()
|
||
{
|
||
string docPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
|
||
string filePath = Path.Combine(docPath, GAMEDATA_FOLDER, OVERRIDES_FILE);
|
||
|
||
try
|
||
{
|
||
// Проверяем существование файла
|
||
if (!File.Exists(filePath))
|
||
{
|
||
Debug.LogWarning($"Файл не найден по пути: {filePath}");
|
||
return "";
|
||
}
|
||
|
||
// Читаем весь текст из файла
|
||
string serialData = File.ReadAllText(filePath);
|
||
|
||
// Убираем лишние пробелы и проверяем на пустоту
|
||
if (string.IsNullOrWhiteSpace(serialData))
|
||
{
|
||
Debug.LogWarning("Файл пуст");
|
||
return "";
|
||
}
|
||
|
||
return serialData.Trim();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Debug.LogError($"Ошибка при чтении файла: {ex.Message}");
|
||
return "";
|
||
}
|
||
}
|
||
|
||
public static bool NeedToStartServer()
|
||
{
|
||
string docPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
|
||
string filePath = Path.Combine(docPath, GAMEDATA_FOLDER, "serverOn.rr");
|
||
|
||
try
|
||
{
|
||
// Проверяем существование файла
|
||
if (!File.Exists(filePath)) return false;
|
||
|
||
// Читаем содержимое файла
|
||
string content = File.ReadAllText(filePath).Trim();
|
||
if (string.IsNullOrWhiteSpace(content)) return false;
|
||
|
||
if(content == "1")
|
||
return true;
|
||
else
|
||
return false;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Debug.LogError($"Ошибка при чтении файла пароля: {ex.Message}");
|
||
return false;
|
||
}
|
||
}
|
||
|
||
|
||
public static string GetElektroVersion()
|
||
{
|
||
string docPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
|
||
string filePath = Path.Combine(docPath, GAMEDATA_FOLDER, ELEKTRO_VERSION_FILE);
|
||
|
||
try
|
||
{
|
||
// Проверяем существование файла
|
||
if (!File.Exists(filePath))
|
||
{
|
||
Debug.LogWarning($"Файл {ELEKTRO_VERSION_FILE} не найден по пути: {filePath}");
|
||
return string.Empty;
|
||
}
|
||
|
||
// Читаем содержимое файла
|
||
string versionData = File.ReadAllText(filePath).Trim();
|
||
|
||
// Убираем лишние пробелы и проверяем на пустоту
|
||
if (string.IsNullOrWhiteSpace(versionData))
|
||
{
|
||
Debug.LogWarning($"Файл {ELEKTRO_VERSION_FILE} пуст");
|
||
return string.Empty;
|
||
}
|
||
|
||
// Проверяем содержимое файла
|
||
Debug.Log($"Считана версия подключения кнопок: {versionData}");
|
||
return versionData;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Debug.LogError($"Ошибка при чтении файла {ELEKTRO_VERSION_FILE}: {ex.Message}");
|
||
return string.Empty;
|
||
}
|
||
}
|
||
|
||
|
||
public static char GetSensorPositiveValue()
|
||
{
|
||
string docPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
|
||
string filePath = Path.Combine(docPath, GAMEDATA_FOLDER, SENSOR_VERSION_FILE);
|
||
|
||
char china_hopper_positive = '0'; //позитивное значение для китайских хопперов (Нормально разомкнутый (по умолчанию)
|
||
char ru_hopper_positive = '1'; //позитивное значение для русского хоппера (Нормально замкнутый)
|
||
|
||
|
||
try
|
||
{
|
||
// Проверяем существование файла
|
||
if (!File.Exists(filePath))
|
||
{
|
||
Debug.LogWarning($"Файл {SENSOR_VERSION_FILE} не найден по пути: {filePath}");
|
||
return china_hopper_positive;
|
||
}
|
||
|
||
// Читаем содержимое файла
|
||
string versionData = File.ReadAllText(filePath).Trim();
|
||
|
||
// Убираем лишние пробелы и проверяем на пустоту
|
||
if (string.IsNullOrWhiteSpace(versionData))
|
||
{
|
||
Debug.LogWarning($"Файл {SENSOR_VERSION_FILE} пуст");
|
||
return china_hopper_positive;
|
||
}
|
||
|
||
char result = china_hopper_positive;
|
||
switch (versionData)
|
||
{
|
||
case "1":
|
||
result = china_hopper_positive;
|
||
break;
|
||
case "2":
|
||
result = ru_hopper_positive;
|
||
break;
|
||
default:
|
||
result = china_hopper_positive;
|
||
break;
|
||
}
|
||
|
||
return result;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Debug.LogError($"Ошибка при чтении файла {SENSOR_VERSION_FILE}: {ex.Message}");
|
||
return china_hopper_positive;
|
||
}
|
||
}
|
||
|
||
public static string GetSettingsPassword()
|
||
{
|
||
string filePath = Path.Combine(Application.persistentDataPath, SETTINGS_PASSWORD);
|
||
|
||
try
|
||
{
|
||
// Проверяем существование файла
|
||
if (!File.Exists(filePath))
|
||
{
|
||
Debug.LogWarning($"Файл {SETTINGS_PASSWORD} не найден по пути: {filePath}");
|
||
return null;
|
||
}
|
||
|
||
// Читаем содержимое файла
|
||
string password = File.ReadAllText(filePath).Trim();
|
||
|
||
// Убираем лишние пробелы и проверяем на пустоту
|
||
if (string.IsNullOrWhiteSpace(password))
|
||
{
|
||
Debug.LogWarning($"Файл {SETTINGS_PASSWORD} пуст");
|
||
return null;
|
||
}
|
||
|
||
return password;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Debug.LogError($"Ошибка при чтении файла {SETTINGS_PASSWORD}: {ex.Message}");
|
||
return null;
|
||
}
|
||
}
|
||
|
||
public static void SetPassword(string password)
|
||
{
|
||
string filePath = Path.Combine(Application.persistentDataPath, SETTINGS_PASSWORD);
|
||
try
|
||
{
|
||
File.WriteAllText(filePath, password);
|
||
|
||
Console.WriteLine($"Файл успешно записан: {filePath}");
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Console.WriteLine($"Ошибка при записи файла: {ex.Message}");
|
||
throw;
|
||
}
|
||
}
|
||
|
||
}
|
||
|