382 lines
9.9 KiB
C#
382 lines
9.9 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
|
||
namespace BF
|
||
{
|
||
/// <summary>
|
||
/// 口头约束 各个manager的Create函数只提供给BFMain使用
|
||
/// </summary>
|
||
public class BFMain : MonoSingleton<BFMain>
|
||
{
|
||
List<ManagerBase> managerList;
|
||
|
||
// 客户端c#代码版本号,每次发布新包后加1,lua层存在一套代码兼容多版本c#代码
|
||
public const int CLIENT_VERSION = 2;
|
||
|
||
// 是否是单机版
|
||
public static bool IsStandAlone = false;
|
||
public static bool IsShenhe = false;
|
||
public static bool IsGotServerTime = false;
|
||
public const string FILE_HEAD = "for_file_head";
|
||
public const string FILE_HEAD_BASE64 = "Zm9yX2ZpbGVfaGVhZ";
|
||
public static long ServerTime = 0;
|
||
public static long DifferenceTime = 0;
|
||
|
||
protected override void Init()
|
||
{
|
||
base.Init();
|
||
Screen.sleepTimeout = SleepTimeout.NeverSleep;
|
||
#if !UNITY_EDITOR
|
||
BestHTTP.HTTPManager.Logger.Level = BestHTTP.Logger.Loglevels.None; // 避免真机上BestHTTP的异常导致的崩溃
|
||
#endif
|
||
InitMgrs();
|
||
StartGame();
|
||
}
|
||
|
||
void InitMgrs()
|
||
{
|
||
managerList = new List<ManagerBase>();
|
||
}
|
||
|
||
public void StartGame()
|
||
{
|
||
#if UNITY_EDITOR
|
||
var sceneName = UnityEngine.SceneManagement.SceneManager.GetActiveScene().name;
|
||
if (Application.isPlaying && sceneName.CompareTo("enter_scene") == 0)
|
||
{
|
||
GameLaunchMgr.LaunchGame();
|
||
}
|
||
#else
|
||
GameLaunchMgr.LaunchGame();
|
||
#endif
|
||
}
|
||
|
||
void Update()
|
||
{
|
||
var count = managerList.Count;
|
||
for (int i = 0; i < count; i++)
|
||
{
|
||
managerList[i].Update();
|
||
}
|
||
}
|
||
|
||
void LateUpdate()
|
||
{
|
||
var count = managerList.Count;
|
||
for (int i = 0; i < count; i++)
|
||
{
|
||
managerList[i].LateUpdate();
|
||
}
|
||
}
|
||
|
||
void OnDestroy()
|
||
{
|
||
var count = managerList.Count;
|
||
for (int i = 0; i < count; i++)
|
||
{
|
||
managerList[i].Destroy();
|
||
}
|
||
managerList.Clear();
|
||
}
|
||
|
||
void OnApplicationPause(bool pause)
|
||
{
|
||
if (luaMgr != null)
|
||
{
|
||
luaMgr.OnApplicationPause(pause);
|
||
}
|
||
}
|
||
|
||
void OnApplicationFocus(bool focus)
|
||
{
|
||
if (luaMgr != null)
|
||
{
|
||
luaMgr.OnApplicationFocus(focus);
|
||
}
|
||
}
|
||
|
||
void OnApplicationQuit()
|
||
{
|
||
if (luaMgr != null)
|
||
{
|
||
luaMgr.OnApplicationQuit();
|
||
}
|
||
Destroy();
|
||
}
|
||
|
||
public void Destroy()
|
||
{
|
||
Destroy(gameObject);
|
||
}
|
||
|
||
GameLaunchManager gameLaunchMgr;
|
||
public GameLaunchManager GameLaunchMgr
|
||
{
|
||
get
|
||
{
|
||
if (gameLaunchMgr == null)
|
||
{
|
||
gameLaunchMgr = GameLaunchManager.Create();
|
||
gameLaunchMgr.Init();
|
||
managerList.Add(gameLaunchMgr);
|
||
}
|
||
return gameLaunchMgr;
|
||
}
|
||
}
|
||
|
||
LuaManager luaMgr;
|
||
public LuaManager LuaMgr
|
||
{
|
||
get
|
||
{
|
||
if (luaMgr == null)
|
||
{
|
||
luaMgr = LuaManager.Create();
|
||
luaMgr.SetMono(this);
|
||
luaMgr.Init();
|
||
managerList.Add(luaMgr);
|
||
}
|
||
return luaMgr;
|
||
}
|
||
}
|
||
|
||
LoomManager loomMgr;
|
||
public LoomManager LoomMgr
|
||
{
|
||
get
|
||
{
|
||
if (loomMgr == null)
|
||
{
|
||
loomMgr = LoomManager.Create();
|
||
loomMgr.SetMono(this);
|
||
loomMgr.Init();
|
||
managerList.Add(loomMgr);
|
||
}
|
||
return loomMgr;
|
||
}
|
||
}
|
||
|
||
PoolManager poolMgr;
|
||
public PoolManager PoolMgr
|
||
{
|
||
get
|
||
{
|
||
if (poolMgr == null)
|
||
{
|
||
poolMgr = PoolManager.Create();
|
||
managerList.Add(poolMgr);
|
||
}
|
||
return poolMgr;
|
||
}
|
||
}
|
||
|
||
BFTouchManager touchMgr;
|
||
public BFTouchManager TouchMgr
|
||
{
|
||
get
|
||
{
|
||
if (touchMgr == null)
|
||
{
|
||
touchMgr = BFTouchManager.Create();
|
||
managerList.Add(touchMgr);
|
||
}
|
||
return touchMgr;
|
||
}
|
||
}
|
||
|
||
RenderManager renderMgr;
|
||
public RenderManager RenderMgr
|
||
{
|
||
get
|
||
{
|
||
if (renderMgr == null)
|
||
{
|
||
renderMgr = RenderManager.Create();
|
||
renderMgr.SetMono(this);
|
||
managerList.Add(renderMgr);
|
||
}
|
||
return renderMgr;
|
||
}
|
||
}
|
||
|
||
ResourceManager resMgr;
|
||
public ResourceManager ResMgr
|
||
{
|
||
get
|
||
{
|
||
if (resMgr == null)
|
||
{
|
||
resMgr = ResourceManager.Create();
|
||
resMgr.Init();
|
||
managerList.Add(resMgr);
|
||
}
|
||
return resMgr;
|
||
}
|
||
}
|
||
|
||
NetManager netMgr;
|
||
public NetManager NetMgr
|
||
{
|
||
get
|
||
{
|
||
if (netMgr == null)
|
||
{
|
||
netMgr = NetManager.Create();
|
||
managerList.Add(netMgr);
|
||
}
|
||
return netMgr;
|
||
}
|
||
}
|
||
|
||
BFWebRequestManager webRequestMgr;
|
||
public BFWebRequestManager WebRequestMgr
|
||
{
|
||
get
|
||
{
|
||
if (webRequestMgr == null)
|
||
{
|
||
webRequestMgr = BFWebRequestManager.Create();
|
||
managerList.Add(webRequestMgr);
|
||
}
|
||
return webRequestMgr;
|
||
}
|
||
}
|
||
|
||
OneShotManager oneShotManager;
|
||
public OneShotManager OneShotManager
|
||
{
|
||
get
|
||
{
|
||
if (oneShotManager == null)
|
||
{
|
||
oneShotManager = OneShotManager.Create();
|
||
oneShotManager.Init();
|
||
managerList.Add(oneShotManager);
|
||
}
|
||
return oneShotManager;
|
||
}
|
||
}
|
||
|
||
SoundManager soundManager;
|
||
public SoundManager SoundManager
|
||
{
|
||
get
|
||
{
|
||
if (soundManager == null)
|
||
{
|
||
soundManager = SoundManager.Create();
|
||
soundManager.Init();
|
||
managerList.Add(soundManager);
|
||
}
|
||
return soundManager;
|
||
}
|
||
}
|
||
|
||
TimeLineManager timeLineManager;
|
||
public TimeLineManager TimeLineManager
|
||
{
|
||
get
|
||
{
|
||
if (timeLineManager == null)
|
||
{
|
||
timeLineManager = TimeLineManager.Create();
|
||
timeLineManager.Init();
|
||
managerList.Add(timeLineManager);
|
||
}
|
||
return timeLineManager;
|
||
}
|
||
}
|
||
|
||
TaskManager taskMgr;
|
||
public TaskManager TaskMgr
|
||
{
|
||
get
|
||
{
|
||
if (taskMgr == null)
|
||
{
|
||
taskMgr = TaskManager.Create();
|
||
taskMgr.Init();
|
||
taskMgr.SetMono(this);
|
||
managerList.Add(taskMgr);
|
||
}
|
||
return taskMgr;
|
||
}
|
||
}
|
||
|
||
BIReport biReport;
|
||
public BIReport BIReport
|
||
{
|
||
get
|
||
{
|
||
if (biReport == null)
|
||
{
|
||
biReport = BIReport.Create();
|
||
biReport.Init();
|
||
managerList.Add(biReport);
|
||
}
|
||
return biReport;
|
||
}
|
||
}
|
||
|
||
SDKManager sdkMgr;
|
||
public SDKManager SDKMgr
|
||
{
|
||
get
|
||
{
|
||
if (sdkMgr == null)
|
||
{
|
||
sdkMgr = SDKManager.Create();
|
||
sdkMgr.Init();
|
||
managerList.Add(sdkMgr);
|
||
}
|
||
return sdkMgr;
|
||
}
|
||
}
|
||
|
||
BattleManager battleMgr;
|
||
public BattleManager BattleMgr
|
||
{
|
||
get
|
||
{
|
||
if (battleMgr == null)
|
||
{
|
||
battleMgr = BattleManager.Create();
|
||
battleMgr.SetMono(this);
|
||
battleMgr.Init();
|
||
managerList.Add(battleMgr);
|
||
}
|
||
return battleMgr;
|
||
}
|
||
}
|
||
|
||
public static void SetServerTime(long serverTime)
|
||
{
|
||
ServerTime = serverTime;
|
||
DifferenceTime = Convert.ToInt64(Time.realtimeSinceStartup);
|
||
}
|
||
|
||
public static string ForFree(string str)
|
||
{
|
||
var bytes = System.Text.Encoding.UTF8.GetBytes(str);
|
||
var bytesCount = bytes.Length;
|
||
var index = 0;
|
||
var key = BF.Utils.GetFreeChar();
|
||
var keyLength = key.Length;
|
||
var headLength = FILE_HEAD.Length;
|
||
var newbytes = new byte[bytes.Length + headLength];
|
||
for (int i = 0; i < headLength; i++)
|
||
{
|
||
var headByte = (byte)FILE_HEAD[i];
|
||
newbytes[i] = headByte;
|
||
}
|
||
for (int i = 0; i < bytesCount; i++)
|
||
{
|
||
newbytes[i + headLength] = (byte)(bytes[i] ^ (key[index % keyLength]));
|
||
index++;
|
||
}
|
||
return System.Convert.ToBase64String(newbytes);
|
||
}
|
||
}
|
||
}
|