#if UNITY_EDITOR using System; using System.Diagnostics; using System.IO; using System.Net; using UnityEditor; using Debug = UnityEngine.Debug; namespace Blocks4u.Editor { [InitializeOnLoad] public class ServerHolder { public static BlocklyServer Server { get; private set; } private static string ConnectionURL => $"http://127.0.0.1:38080/"; static ServerHolder() { try { var path = DetectAssetsDirectory(); AppDomain.CurrentDomain.DomainUnload += OnDomainUnload; //TODO token генерить при каждом запуске униту //TODO вынести параметры порта в отдельный EditorPrefs //TODO чекать что порт уже не занят! Debug.LogError("Starting server"); Server = new BlocklyServer(path, ConnectionURL, "UUID1234567"); } catch (Exception e) { Debug.LogException(e); } } private static string DetectAssetsDirectory() { // Получаем stack trace и находим вызывающий метод var stackTrace = new StackTrace(true); var frames = stackTrace.GetFrames(); if (frames == null) { throw new Exception("Failed to get assets directory"); } foreach (var frame in frames) { var fileName = frame.GetFileName(); if (string.IsNullOrEmpty(fileName) || !fileName.EndsWith(nameof(ServerHolder) + ".cs")) { continue; } return Path.Combine(Path.GetDirectoryName(Path.GetDirectoryName(fileName)) ?? string.Empty, "htdocs"); } throw new Exception("Failed to get assets directory"); } private static void OnDomainUnload(object sender, EventArgs e) { AppDomain.CurrentDomain.DomainUnload -= OnDomainUnload; Debug.LogError("Unloading server"); Server?.Dispose(); } } } #endif