AxibugEmuOnline/AxibugEmuOnline.Client/Assets/Script/App.cs

180 lines
5.6 KiB
C#
Raw Normal View History

2024-12-04 23:15:05 +08:00
using AxibugEmuOnline.Client.Manager;
using AxibugEmuOnline.Client.Network;
using System.Collections;
using System.IO;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.Networking;
using static AxibugEmuOnline.Client.HttpAPI;
using static AxibugEmuOnline.Client.Manager.LogManager;
namespace AxibugEmuOnline.Client.ClientCore
{
public static class App
{
public static string TokenStr;
public static string IP;
public static int Port;
public static LogManager log;
public static NetworkHelper network;
public static AppLogin login;
public static AppChat chat;
public static UserDataManager user;
//public static AppNetGame netgame;
public static AppEmu emu;
2024-08-22 15:16:58 +08:00
public static RomLib nesRomLib;
2024-08-13 18:35:20 +08:00
public static HttpAPI httpAPI;
2024-08-22 17:25:00 +08:00
public static CacheManager CacheMgr;
2024-09-11 18:31:25 +08:00
public static AppRoom roomMgr;
2024-11-14 17:14:52 +08:00
public static AppSettings settings;
2024-11-27 11:28:51 +08:00
public static FilterManager filter;
public static AppShare share;
2024-09-13 10:07:24 +08:00
#region Mono
public static TickLoop tickLoop;
2024-08-13 18:35:20 +08:00
private static CoroutineRunner coRunner;
2024-09-13 10:07:24 +08:00
#endregion
2024-09-20 17:10:18 +08:00
#if UNITY_PSP2
public static string PersistentDataPath => "ux0:data/AxibugEmu";
#else
2024-08-22 17:25:00 +08:00
public static string PersistentDataPath => Application.persistentDataPath;
2024-09-20 17:10:18 +08:00
#endif
2024-12-04 23:15:05 +08:00
public static void Init(Initer initer)
{
settings = new AppSettings();
log = new LogManager();
LogManager.OnLog += OnNoSugarNetLog;
network = new NetworkHelper();
login = new AppLogin();
chat = new AppChat();
user = new UserDataManager();
emu = new AppEmu();
//netgame = new AppNetGame();
httpAPI = new HttpAPI();
nesRomLib = new RomLib(EnumPlatform.NES);
CacheMgr = new CacheManager();
2024-09-12 17:47:05 +08:00
roomMgr = new AppRoom();
share = new AppShare();
filter = new FilterManager(initer.m_filterVolume, initer.m_filterPreview, initer.m_xmbBg);
2024-12-04 23:15:05 +08:00
var go = new GameObject("[AppAxibugEmuOnline]");
GameObject.DontDestroyOnLoad(go);
tickLoop = go.AddComponent<TickLoop>();
coRunner = go.AddComponent<CoroutineRunner>();
2024-09-20 17:10:18 +08:00
if (UnityEngine.Application.platform == RuntimePlatform.PSP2)
2024-12-04 23:15:05 +08:00
{
//PSV 等平台需要手动创建目录
PersistentDataPathDir();
}
2024-12-04 23:15:05 +08:00
var importNode = GameObject.Find("IMPORTENT");
if (importNode != null) GameObject.DontDestroyOnLoad(importNode);
StartCoroutine(AppTickFlow());
RePullNetInfo();
}
2024-09-20 17:10:18 +08:00
private static void PersistentDataPathDir()
2024-12-04 23:15:05 +08:00
{
2024-09-20 17:10:18 +08:00
if (!Directory.Exists(PersistentDataPath))
2024-12-04 23:15:05 +08:00
{
Directory.CreateDirectory(PersistentDataPath);
}
}
2024-08-22 15:16:58 +08:00
private static IEnumerator AppTickFlow()
{
while (true)
{
Tick();
yield return null;
}
}
2024-09-13 10:07:24 +08:00
public static void RePullNetInfo()
2024-12-04 23:15:05 +08:00
{
2024-09-13 10:07:24 +08:00
StartCoroutine(StartNetInit());
}
static IEnumerator StartNetInit()
{
if (App.network.isConnected)
2024-09-13 10:07:24 +08:00
yield break;
int platform = 0;
2024-12-04 23:15:05 +08:00
bool bTest = false;
if (bTest)
{
yield return null;
Connect("192.168.0.47", 10492);
yield break;
2024-11-08 09:41:09 +08:00
}
2024-09-13 10:07:24 +08:00
UnityWebRequest request = UnityWebRequest.Get($"{App.httpAPI.WebSiteApi}/CheckStandInfo?platform={platform}&version={Application.version}");
yield return request.SendWebRequest();
if (request.result != UnityWebRequest.Result.Success)
yield break;
App.log.Debug($"ApiResp => {request.downloadHandler.text}");
Resp_CheckStandInfo resp = JsonUtility.FromJson<Resp_CheckStandInfo>(request.downloadHandler.text);
//需要更新
if (resp.needUpdateClient == 1)
{
//TODO
}
yield return null;
2024-09-13 13:46:04 +08:00
//Connect("127.0.0.1", 10492);
Connect(resp.serverIp, resp.serverPort);
2024-09-13 10:07:24 +08:00
}
2024-08-22 15:16:58 +08:00
private static void Tick()
{
nesRomLib.ExecuteFetchRomInfo();
}
2024-08-13 18:35:20 +08:00
public static Coroutine StartCoroutine(IEnumerator itor)
{
return coRunner.StartCoroutine(itor);
2024-12-04 23:15:05 +08:00
}
2024-08-13 18:35:20 +08:00
public static void StopCoroutine(Coroutine cor)
{
coRunner.StopCoroutine(cor);
2024-12-04 23:15:05 +08:00
}
public static void Connect(string IP, int port)
{
2024-09-13 10:07:24 +08:00
Task task = new Task(() =>
{
network.Init(IP, port);
2024-12-04 23:15:05 +08:00
});
task.Start();
}
public static void Close()
2024-11-11 16:12:54 +08:00
{
2024-12-04 23:15:05 +08:00
App.log.Info("停止");
}
static void OnNoSugarNetLog(int LogLevel, string msg)
{
E_LogType logType = (E_LogType)LogLevel;
2024-11-11 16:12:54 +08:00
switch (logType)
2024-12-04 23:15:05 +08:00
{
2024-11-11 16:12:54 +08:00
case E_LogType.Debug:
case E_LogType.Info:
2024-12-04 23:15:05 +08:00
Debug.Log("[AxiEmu]:" + msg);
2024-11-11 16:12:54 +08:00
break;
case E_LogType.Warning:
2024-12-04 23:15:05 +08:00
Debug.LogWarning("[AxiEmu]:" + msg);
2024-11-11 16:12:54 +08:00
break;
case E_LogType.Error:
2024-12-04 23:15:05 +08:00
Debug.LogError("[AxiEmu]:" + msg);
break;
}
}
}
2024-06-28 18:08:25 +08:00
}