# Path: il2fb/ds/airbridge/compat.py
# IS_WINDOWS = (sys.platform == "win32")
# 
# Path: il2fb/ds/airbridge/typing.py

# 
# Path: il2fb/ds/airbridge/dedicated_server/path.py
# def normalize_exe_path(initial: str) -> Path:
#     return Path(initial).resolve()
# 
# def normalize_config_path(root_dir: Path, initial: str=None) -> Path:
#     return normalize_file_path(
#         root_dir=root_dir,
#         initial=initial,
#         default_name=DEFAULT_CONFIG_NAME,
#     )
# 
# def normalize_start_script_path(root_dir: Path, initial: str=None) -> Path:
#     return normalize_file_path(
#         root_dir=root_dir,
#         initial=initial,
#         default_name=DEFAULT_START_SCRIPT_NAME,
#     )
# 
# Path: il2fb/ds/airbridge/dedicated_server/validators.py
# def validate_dedicated_server_file_access(file_path: StringOrPath) -> None:
#     if not os.access(file_path, os.F_OK):
#         raise FileNotFoundError(
#             f"file does not exist (path='{file_path}')"
#         )
# 
#     if not os.access(file_path, os.R_OK):
#         raise PermissionError(
#             f"file does not have read access (path='{file_path}')"
#         )
# 
# def validate_dedicated_server_config(config: ServerConfig) -> None:
#     if not config.console.connection.port:
#         raise ValueError(
#             "server's console is disabled, please enable it "
#             "(see: https://github.com/IL2HorusTeam/il2fb-ds-config#console-section)"
#         )
# 
#     if not config.device_link.connection.port:
#         raise ValueError(
#             "server's device link is disabled, please enable it "
#             "(see: https://github.com/IL2HorusTeam/il2fb-ds-config#devicelink-section)"
#         )


import asyncio
import configparser
import functools
import io
import logging
import psutil
from pathlib import Path
from typing import List, Awaitable, Optional
from il2fb.config.ds import ServerConfig
from il2fb.ds.airbridge.compat import IS_WINDOWS
from il2fb.ds.airbridge.typing import IntOrNone, StringHandler, StringList
from il2fb.ds.airbridge.dedicated_server.path import (
    normalize_exe_path, normalize_config_path, normalize_start_script_path,
)
from il2fb.ds.airbridge.dedicated_server.validators import (
    validate_dedicated_server_file_access, validate_dedicated_server_config,
)
    async def start(self) -> Awaitable[None]:
        try:
            await self._spawn_process()
            self._maybe_setup_stderr_handler()
            await self._wait_started()
            self._maybe_setup_stdout_handler()
        except Exception:
            if self._process:
                self._process.kill()
                await self._process.wait()

            raise

    async def _spawn_process(self) -> Awaitable[None]:
        args = (
            []
            if IS_WINDOWS
            else [self._wine_bin_path]
        )
        args.extend([
            str(self.exe_path),
            '-conf', str(self.config_path),
            '-cmd', str(self.start_script_path),
        ])

        kwargs = dict(
            stdin=asyncio.subprocess.PIPE,
            stdout=asyncio.subprocess.PIPE,
            stderr=asyncio.subprocess.PIPE,
        )

# Path: il2fb/ds/airbridge/compat.py
# IS_WINDOWS = (sys.platform == "win32")
# 
# Path: il2fb/ds/airbridge/typing.py

# 
# Path: il2fb/ds/airbridge/dedicated_server/path.py
# def normalize_exe_path(initial: str) -> Path:
#     return Path(initial).resolve()
# 
# def normalize_config_path(root_dir: Path, initial: str=None) -> Path:
#     return normalize_file_path(
#         root_dir=root_dir,
#         initial=initial,
#         default_name=DEFAULT_CONFIG_NAME,
#     )
# 
# def normalize_start_script_path(root_dir: Path, initial: str=None) -> Path:
#     return normalize_file_path(
#         root_dir=root_dir,
#         initial=initial,
#         default_name=DEFAULT_START_SCRIPT_NAME,
#     )
# 
# Path: il2fb/ds/airbridge/dedicated_server/validators.py
# def validate_dedicated_server_file_access(file_path: StringOrPath) -> None:
#     if not os.access(file_path, os.F_OK):
#         raise FileNotFoundError(
#             f"file does not exist (path='{file_path}')"
#         )
# 
#     if not os.access(file_path, os.R_OK):
#         raise PermissionError(
#             f"file does not have read access (path='{file_path}')"
#         )
# 
# def validate_dedicated_server_config(config: ServerConfig) -> None:
#     if not config.console.connection.port:
#         raise ValueError(
#             "server's console is disabled, please enable it "
#             "(see: https://github.com/IL2HorusTeam/il2fb-ds-config#console-section)"
#         )
# 
#     if not config.device_link.connection.port:
#         raise ValueError(
#             "server's device link is disabled, please enable it "
#             "(see: https://github.com/IL2HorusTeam/il2fb-ds-config#devicelink-section)"
#         )


# Path: il2fb/ds/airbridge/dedicated_server/instance.py
import asyncio
import configparser
import functools
import io
import logging
import psutil
from pathlib import Path
from typing import List, Awaitable, Optional
from il2fb.config.ds import ServerConfig
from il2fb.ds.airbridge.compat import IS_WINDOWS
from il2fb.ds.airbridge.typing import IntOrNone, StringHandler, StringList
from il2fb.ds.airbridge.dedicated_server.path import (
    normalize_exe_path, normalize_config_path, normalize_start_script_path,
)
from il2fb.ds.airbridge.dedicated_server.validators import (
    validate_dedicated_server_file_access, validate_dedicated_server_config,
)

    async def start(self) -> Awaitable[None]:
        try:
            await self._spawn_process()
            self._maybe_setup_stderr_handler()
            await self._wait_started()
            self._maybe_setup_stdout_handler()
        except Exception:
            if self._process:
                self._process.kill()
                await self._process.wait()

            raise

    async def _spawn_process(self) -> Awaitable[None]:
        args = (
            []
            if IS_WINDOWS
            else [self._wine_bin_path]
        )
        args.extend([
            str(self.exe_path),
            '-conf', str(self.config_path),
            '-cmd', str(self.start_script_path),
        ])

        kwargs = dict(
            stdin=asyncio.subprocess.PIPE,
            stdout=asyncio.subprocess.PIPE,
            stderr=asyncio.subprocess.PIPE,
        )

        if not IS_WINDOWS: