import configparser
import os

class BulletTrackingConfig:
    def __init__(self, config_file='bullet_tracking.ini'):
        self.config_file = config_file
        self.config = configparser.ConfigParser()
        self.load_config()
    
    def load_config(self):
        """加载配置文件"""
        if not os.path.exists(self.config_file):
            self.create_default_config()
        
        self.config.read(self.config_file)
        
        # 追踪参数
        self.max_distance = self.get_float('TrackingParameters', 'max_tracking_distance', 300.0)
        self.auto_track = self.get_boolean('TrackingParameters', 'auto_tracking', True)
        self.tracking_strength = self.get_float('TrackingParameters', 'tracking_strength', 0.85)
        self.headshot_priority = self.get_boolean('TrackingParameters', 'headshot_priority', True)
        self.fov = self.get_float('TrackingParameters', 'field_of_view', 120.0)
        
        # 武器配置
        self.weapon_damage = self.get_float('WeaponConfig', 'base_damage', 45.0)
        self.weapon_range = self.get_float('WeaponConfig', 'effective_range', 200.0)
        self.bullet_speed = self.get_float('WeaponConfig', 'bullet_speed', 900.0)
        self.fire_rate = self.get_float('WeaponConfig', 'fire_rate', 600.0)
        
        # 性能设置
        self.update_rate = self.get_int('Performance', 'update_rate', 60)
        self.smoothness = self.get_int('Performance', 'smoothness', 8)
        self.prediction_enabled = self.get_boolean('Performance', 'prediction', True)
        
        # 高级功能
        self.homing_enabled = self.get_boolean('AdvancedFeatures', 'homing_enabled', True)
        self.prediction_strength = self.get_float('AdvancedFeatures', 'prediction_strength', 1.2)
        self.adaptive_tracking = self.get_boolean('AdvancedFeatures', 'adaptive_tracking', True)
        self.recoil_compensation = self.get_boolean('AdvancedFeatures', 'recoil_compensation', True)
    
    def get_float(self, section, key, default):
        """获取浮点数值"""
        try:
            return self.config.getfloat(section, key)
        except (configparser.NoSectionError, configparser.NoOptionError, ValueError):
            return default
    
    def get_int(self, section, key, default):
        """获取整数值"""
        try:
            return self.config.getint(section, key)
        except (configparser.NoSectionError, configparser.NoOptionError, ValueError):
            return default
    
    def get_boolean(self, section, key, default):
        """获取布尔值"""
        try:
            value = self.config.get(section, key).lower()
            return value in ['enabled', 'true', 'yes', '1']
        except (configparser.NoSectionError, configparser.NoOptionError):
            return default
    
    def create_default_config(self):
        """创建默认配置文件"""
        self.config['TrackingParameters'] = {
            'max_tracking_distance': '300.0',
            'auto_tracking': 'enabled',
            'tracking_strength': '0.85',
            'headshot_priority': 'enabled',
            'field_of_view': '120.0'
        }
        
        self.config['WeaponConfig'] = {
            'base_damage': '45.0',
            'effective_range': '200.0',
            'bullet_speed': '900.0',
            'fire_rate': '600.0'
        }
        
        self.config['Performance'] = {
            'update_rate': '60',
            'smoothness': '8',
            'prediction': 'enabled'
        }
        
        self.config['AdvancedFeatures'] = {
            'homing_enabled': 'enabled',
            'prediction_strength': '1.2',
            'adaptive_tracking': 'enabled',
            'recoil_compensation': 'enabled'
        }
        
        with open(self.config_file, 'w') as configfile:
            self.config.write(configfile)
        
        print(f"Default configuration created: {self.config_file}")
    
    def save_config(self):
        """保存当前配置到文件"""
        with open(self.config_file, 'w') as configfile:
            self.config.write(configfile)
        print("Configuration saved successfully")
    
    def update_setting(self, section, key, value):
        """更新配置设置"""
        if section not in self.config:
            self.config[section] = {}
        
        if isinstance(value, bool):
            self.config[section][key] = 'enabled' if value else 'disabled'
        else:
            self.config[section][key] = str(value)
        
        # 更新内存中的值
        self.load_config()
    
    def print_config(self):
        """打印当前配置"""
        print("=== Current Bullet Tracking Configuration ===")
        print(f"Tracking Parameters:")
        print(f"  Max Distance: {self.max_distance}")
        print(f"  Auto Track: {self.auto_track}")
        print(f"  Tracking Strength: {self.tracking_strength}")
        print(f"  Headshot Priority: {self.headshot_priority}")
        print(f"  Field of View: {self.fov}")
        
        print(f"\nWeapon Configuration:")
        print(f"  Base Damage: {self.weapon_damage}")
        print(f"  Effective Range: {self.weapon_range}")
        print(f"  Bullet Speed: {self.bullet_speed}")
        print(f"  Fire Rate: {self.fire_rate}")
        
        print(f"\nPerformance Settings:")
        print(f"  Update Rate: {self.update_rate} Hz")
        print(f"  Smoothness: {self.smoothness}")
        print(f"  Prediction: {self.prediction_enabled}")
        
        print(f"\nAdvanced Features:")
        print(f"  Homing Enabled: {self.homing_enabled}")
        print(f"  Prediction Strength: {self.prediction_strength}")
        print(f"  Adaptive Tracking: {self.adaptive_tracking}")
        print(f"  Recoil Compensation: {self.recoil_compensation}")
        print("===========================================")

# 使用示例
if __name__ == "__main__":
    # 创建配置管理器
    config_manager = BulletTrackingConfig()
    
    # 打印当前配置
    config_manager.print_config()
    
    # 使用配置值
    if config_manager.auto_track:
        print(f"\nAuto tracking enabled with {config_manager.tracking_strength * 100}% strength")
    
    if config_manager.headshot_priority:
        print("Headshot priority is active")
    
    # 动态更新配置
    config_manager.update_setting('TrackingParameters', 'tracking_strength', 0.95)
    config_manager.update_setting('Performance', 'update_rate', 120)
    
    print("\nAfter updating settings:")
    config_manager.print_config()
    
    # 保存配置
    config_manager.save_config()

辞酱👀窝主🍃辞酱👀窝主🍃辞酱👀窝主🍃辞酱👀窝主🍃辞酱👀窝主🍃辞酱👀窝主🍃辞酱辞酱👀窝主🍃辞酱👀窝主🍃辞酱👀窝主🍃辞酱👀窝主🍃cpp👀窝主🍃cpp👀窝主🍃cpp