Загрузка данных


using System;
using System.Windows.Forms;
using GTA;
using GTA.Math;
using GTA.Native;

public class SedanCabinCamera : Script
{
    private Camera _carCamera;
    private float _heightOffset = 1.2f; 
    private bool _wasInCar = false;
    private float _currentHeadingOffset = 0.0f;
    private float _currentPitch = 0.0f;
    
    // Добавили переменную для работы с файлом настроек
    private ScriptSettings _config; 

    public SedanCabinCamera()
    {
        // Загружаем файл настроек (если его нет, он создастся при сохранении)
        _config = ScriptSettings.Load("scripts\\CameraOffsets.ini");
        
        KeyDown += OnKeyDown;
        Tick += OnTick;
        UI.Notify("~g~Скрипт камеры (v2) успешно загружен!");
    }

    private void OnTick(object sender, EventArgs e)
    {
        var vehicle = Game.Player.Character.CurrentVehicle;

        if (vehicle == null) 
        { 
            RemoveCamera(); 
            _wasInCar = false; 
            return; 
        }

        bool isTarget = (vehicle.Model.Hash == Game.GenerateHash("jzx100") || 
                         vehicle.Model.Hash == Game.GenerateHash("mk2100"));
        
        if (!isTarget) 
        { 
            RemoveCamera(); 
            _wasInCar = false; 
            return; 
        }

        if (!_wasInCar)
        {
            UI.Notify("~b~Обнаружена машина: " + vehicle.Model.ToString());
            
            // ЧИТАЕМ ВЫСОТУ ИЗ ФАЙЛА ПРИ ПОСАДКЕ В МАШИНУ
            string modelName = GetModelName(vehicle);
            _heightOffset = _config.GetValue<float>("Offsets", modelName, 1.2f);
            
            _wasInCar = true;
        }

        if (Function.Call<int>(Hash.GET_FOLLOW_VEHICLE_CAM_VIEW_MODE) == 4) 
        { 
            RemoveCamera(); 
            return; 
        }

        if (_carCamera == null || !_carCamera.Exists())
        {
            _carCamera = World.CreateCamera(vehicle.Position, vehicle.Rotation, 60f);
            _currentHeadingOffset = 0.0f;
            _currentPitch = 0.0f;
        }

        float lookLR = Function.Call<float>(Hash.GET_CONTROL_NORMAL, 0, 2);
        float lookUD = Function.Call<float>(Hash.GET_CONTROL_NORMAL, 0, 3);

        _currentHeadingOffset -= lookLR * 4.0f;
        _currentPitch -= lookUD * 2.0f;
        _currentPitch = Math.Max(-80.0f, Math.Min(80.0f, _currentPitch));

        if (Math.Abs(lookLR) < 0.1f)
        {
            _currentHeadingOffset += (0.0f - _currentHeadingOffset) * 0.05f;
        }

        float finalHeading = vehicle.Heading + _currentHeadingOffset;
        float rad = (float)(finalHeading * Math.PI / 180.0f);
        
        Vector3 camPos = new Vector3(
            vehicle.Position.X - (float)Math.Sin(rad) * 3.5f,
            vehicle.Position.Y - (float)Math.Cos(rad) * 3.5f,
            vehicle.Position.Z + _heightOffset
        );

        _carCamera.Position = camPos;
        _carCamera.Rotation = new Vector3(_currentPitch, 0f, finalHeading);
        
        World.RenderingCamera = _carCamera;
        Function.Call(Hash.RENDER_SCRIPT_CAMS, true, false, 0, true, false);
    }

    private void OnKeyDown(object sender, KeyEventArgs e)
    {
        var vehicle = Game.Player.Character.CurrentVehicle;
        if (vehicle == null) return;
        
        bool isTarget = (vehicle.Model.Hash == Game.GenerateHash("jzx100") || 
                         vehicle.Model.Hash == Game.GenerateHash("mk2100"));
        if (!isTarget) return;

        if (e.KeyCode == Keys.NumPad9)
        {
            _heightOffset += 0.05f;
            SaveHeight(vehicle); // СОХРАНЯЕМ В ФАЙЛ
            UI.Notify("~g~Высота: " + _heightOffset.ToString("F2")); 
        }
        else if (e.KeyCode == Keys.NumPad3)
        {
            _heightOffset -= 0.05f;
            SaveHeight(vehicle); // СОХРАНЯЕМ В ФАЙЛ
            UI.Notify("~y~Высота: " + _heightOffset.ToString("F2")); 
        }
    }

    // Вспомогательный метод: определяем имя для записи в INI
    private string GetModelName(Vehicle vehicle)
    {
        if (vehicle.Model.Hash == Game.GenerateHash("jzx100")) return "jzx100";
        return "mk2100"; // Если это не jzx100, значит это mk2100 (мы проверили это выше)
    }

    // Вспомогательный метод: сохраняем в файл
    private void SaveHeight(Vehicle vehicle)
    {
        _config.SetValue<float>("Offsets", GetModelName(vehicle), _heightOffset);
        _config.Save();
    }

    private void RemoveCamera()
    {
        if (_carCamera != null && _carCamera.Exists())
        {
            World.RenderingCamera = null;
            Function.Call(Hash.RENDER_SCRIPT_CAMS, false, false, 0, true, false);
            Function.Call(Hash.DESTROY_CAM, _carCamera.Handle, true); 
            _carCamera = null;
        }
    }
}