43 lines
1.8 KiB
C#
43 lines
1.8 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Net;
|
|
using System.Net.Sockets;
|
|
using UnityEngine;
|
|
|
|
// 内网联调配置:开发机探测 IP 的本地持久化(persistentDataPath/dev_server.txt,单行 IPv4)。
|
|
// 跨网段时广播/组播都出不了网段(路由器不转发),只能向已知开发机 IP 单播——该 IP 由本文件持久化,
|
|
// 首次由 DevServerPrompt 弹框录入并验证成功后写入(见 LoadDll.CoLocalDiscoverThenDownload)。
|
|
// 注意:Application.persistentDataPath 只能在主线程访问,故 Load/Save 只能在主线程调;
|
|
// 发现线程(后台)需要的值由调用方在主线程读好传入。
|
|
// 热更层 DevServerDiscovery 内联了同语义的读取(XWorld.Link 未引用 Base 程序集),改格式时两边须同步。
|
|
public static class DevServerConfig
|
|
{
|
|
public static string FilePath => Path.Combine(Application.persistentDataPath, "dev_server.txt");
|
|
|
|
// 读配置:文件不存在/IO 异常/内容不是合法 IPv4 → null(静默,等同"无配置")。
|
|
public static string Load()
|
|
{
|
|
try
|
|
{
|
|
string path = FilePath;
|
|
if (!File.Exists(path)) return null;
|
|
string line = File.ReadAllText(path).Trim();
|
|
if (line.Length == 0) return null;
|
|
if (!IPAddress.TryParse(line, out IPAddress ip)) return null;
|
|
if (ip.AddressFamily != AddressFamily.InterNetwork) return null; // 只认 IPv4
|
|
return ip.ToString();
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
|
|
// 写配置:单行覆盖;IO 异常静默吞(写不进只是下次还弹框,不致命)。
|
|
public static void Save(string ip)
|
|
{
|
|
try { File.WriteAllText(FilePath, ip ?? ""); }
|
|
catch (Exception) { }
|
|
}
|
|
}
|