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

179 lines
5.5 KiB
C#
Raw Normal View History

2024-06-28 18:08:25 +08:00
using AxibugEmuOnline.Client.Manager;
using AxibugEmuOnline.Client.Network;
2024-08-13 18:35:20 +08:00
using System.Collections;
2024-09-20 17:10:18 +08:00
using System.IO;
2024-09-13 10:07:24 +08:00
using System.Threading.Tasks;
2024-06-28 18:08:25 +08:00
using UnityEngine;
2024-09-13 10:07:24 +08:00
using UnityEngine.Networking;
2024-11-27 11:28:51 +08:00
using UnityEngine.Rendering.PostProcessing;
2024-09-13 10:07:24 +08:00
using static AxibugEmuOnline.Client.HttpAPI;
2024-11-11 16:12:54 +08:00
using static AxibugEmuOnline.Client.Manager.LogManager;
2024-06-28 18:08:25 +08:00
namespace AxibugEmuOnline.Client.ClientCore
{
2024-09-11 18:31:25 +08:00
public static class App
2024-06-28 18:08:25 +08:00
{
public static string TokenStr;
public static string IP;
public static int Port;
public static LogManager log;
public static NetworkHelper network;
2024-06-28 18:08:25 +08:00
public static AppLogin login;
public static AppChat chat;
public static UserDataManager user;
//public static AppNetGame netgame;
2024-07-03 14:43:11 +08:00
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;
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-11-28 10:08:49 +08:00
public static void Init(Initer initer)
2024-06-28 18:08:25 +08:00
{
2024-11-14 17:14:52 +08:00
settings = new AppSettings();
2024-06-28 18:08:25 +08:00
log = new LogManager();
LogManager.OnLog += OnNoSugarNetLog;
network = new NetworkHelper();
2024-06-28 18:08:25 +08:00
login = new AppLogin();
chat = new AppChat();
user = new UserDataManager();
2024-07-03 14:43:11 +08:00
emu = new AppEmu();
//netgame = new AppNetGame();
2024-08-14 13:09:22 +08:00
httpAPI = new HttpAPI();
2024-08-22 15:16:58 +08:00
nesRomLib = new RomLib(EnumPlatform.NES);
2024-08-22 17:25:00 +08:00
CacheMgr = new CacheManager();
2024-09-12 17:47:05 +08:00
roomMgr = new AppRoom();
2024-11-28 10:08:49 +08:00
filter = new FilterManager(initer.m_filterVolume, initer.m_filterPreview,initer.m_xmbBg);
2024-08-13 18:35:20 +08:00
var go = new GameObject("[AppAxibugEmuOnline]");
GameObject.DontDestroyOnLoad(go);
2024-09-13 10:07:24 +08:00
tickLoop = go.AddComponent<TickLoop>();
2024-08-13 18:35:20 +08:00
coRunner = go.AddComponent<CoroutineRunner>();
2024-08-22 15:16:58 +08:00
2024-09-20 17:10:18 +08:00
if (UnityEngine.Application.platform == RuntimePlatform.PSP2)
{
//PSV 等平台需要手动创建目录
PersistentDataPathDir();
}
var importNode = GameObject.Find("IMPORTENT");
if (importNode != null) GameObject.DontDestroyOnLoad(importNode);
2024-08-22 15:16:58 +08:00
StartCoroutine(AppTickFlow());
2024-09-13 10:07:24 +08:00
RePullNetInfo();
2024-08-13 18:35:20 +08:00
}
2024-09-20 17:10:18 +08:00
private static void PersistentDataPathDir()
{
if (!Directory.Exists(PersistentDataPath))
{
Directory.CreateDirectory(PersistentDataPath);
}
}
2024-08-13 18:35:20 +08:00
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()
{
StartCoroutine(StartNetInit());
}
static IEnumerator StartNetInit()
{
if (App.network.isConnected)
2024-09-13 10:07:24 +08:00
yield break;
int platform = 0;
2024-11-08 11:49:33 +08:00
bool bTest = true;
2024-11-08 09:41:09 +08:00
if (bTest)
{
yield return null;
Connect("192.168.0.47", 10492);
yield break;
}
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);
}
public static void StopCoroutine(Coroutine cor)
{
coRunner.StopCoroutine(cor);
2024-06-28 18:08:25 +08:00
}
2024-09-13 10:07:24 +08:00
public static void Connect(string IP, int port)
2024-06-28 18:08:25 +08:00
{
2024-09-13 10:07:24 +08:00
Task task = new Task(() =>
{
network.Init(IP, port);
});
task.Start();
2024-06-28 18:08:25 +08:00
}
public static void Close()
{
2024-09-11 18:31:25 +08:00
App.log.Info("停止");
2024-06-28 18:08:25 +08:00
}
static void OnNoSugarNetLog(int LogLevel, string msg)
2024-11-11 16:12:54 +08:00
{
2024-11-26 16:54:51 +08:00
E_LogType logType = (E_LogType)LogLevel;
2024-11-11 16:12:54 +08:00
switch (logType)
{
case E_LogType.Debug:
case E_LogType.Info:
Debug.Log("[AxiEmu]:" + msg);
break;
case E_LogType.Warning:
Debug.LogWarning("[AxiEmu]:" + msg);
break;
case E_LogType.Error:
Debug.LogError("[AxiEmu]:" + msg);
break;
}
2024-06-28 18:08:25 +08:00
}
2024-09-11 16:33:48 +08:00
2024-06-28 18:08:25 +08:00
}
}