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

165 lines
4.9 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;
using static AxibugEmuOnline.Client.HttpAPI;
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-08-29 18:31:36 +08:00
public static AppSceneLoader SceneLoader;
2024-09-11 18:31:25 +08:00
public static AppRoom roomMgr;
2024-08-13 18:35:20 +08:00
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-08-22 17:25:00 +08:00
2024-08-13 18:35:20 +08:00
[RuntimeInitializeOnLoadMethod]
static void Init()
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-08-29 18:31:36 +08:00
SceneLoader = new AppSceneLoader();
2024-09-12 17:47:05 +08:00
roomMgr = new AppRoom();
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 09:41:09 +08:00
bool bTest = true;
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-08-13 18:35:20 +08:00
Debug.Log("[AxibugEmuOnline]:" + msg);
2024-06-28 18:08:25 +08:00
}
2024-09-11 16:33:48 +08:00
2024-06-28 18:08:25 +08:00
}
}