Clean up bootstrap classes

This commit is contained in:
alexiscatnip
2022-01-26 22:01:56 +08:00
parent 7fda01db11
commit a066e98af7
8 changed files with 172 additions and 223 deletions
@@ -0,0 +1,171 @@
using System.IO;
using System.Reflection;
using System.Threading;
using Disk;
using log4net;
using log4net.Appender;
using log4net.Config;
using log4net.Layout;
using OpenMetaverse;
using Raindrop.Netcom;
using UnityEngine;
using Logger = OpenMetaverse.Logger;
namespace Raindrop.Services.Bootstrap
{
//This sets up and tears down the raindrop instance service.
// inspired by https://medium.com/medialesson/simple-service-locator-for-your-unity-project-40e317aad307
//Just attach this script as a component of the game manager. You don't need anything else, unless you want the UI.
public class RaindropBootstrapper : MonoBehaviour
{
//private RaindropInstance instance => ServiceLocator.ServiceLocator.Instance.Get<RaindropInstance>();
//private RaindropNetcom netcom => instance.Netcom;
private void Awake()
{
Start_Raindrop_CoreDependencies();
}
//create the gameobject that helps to create the filesystem
private static void Init_StaticFileSystem()
{
// start the startupCopier
var startupCopierObj = GameObject.CreatePrimitive(PrimitiveType.Cube);
var startupCopier = startupCopierObj.AddComponent<CopyStreamingAssetsToPersistentDataPath>();
}
private static void SendStartupMessageToLogger()
{
Logger.Log("RaindropBootstrapper.cs : RaindropInstance Started and succesfully linked to servicelocator.", Helpers.LogLevel.Info);
}
public static void Start_Raindrop_CoreDependencies()
{
//0. start rolling log file
// ConfigureRollingLogFile();
Init_StaticFileSystem();
//0. start servicelocator pattern.
StartServiceLocator();
//1. construct raindrop instance and register it
CreateAndRegister_RaindropInstance();
}
private static void ConfigureRollingLogFile()
{
// log4net.Config.XmlConfigurator.Configure();
//1. log to unity console.
var unityDebugAppender = new UnityDebugAppender();
unityDebugAppender.Layout = new PatternLayout("%date{HH:mm:ss} [%level] - %message");
//appender.Layout = new PatternLayout("%timestamp [%thread] %-5level - %message%newline");
BasicConfigurator.Configure(unityDebugAppender);
var logFileAppender = new RollingFileAppender();
logFileAppender.Layout = new PatternLayout("%date{HH:mm:ss} [%level] - %message");
logFileAppender.File = Path.Combine(
DirectoryHelpers.GetInternalCacheDir(),
"log.txt");
logFileAppender.AppendToFile = true;
logFileAppender.RollingStyle = RollingFileAppender.RollingMode.Size;
logFileAppender.MaxSizeRollBackups = 10;
logFileAppender.MaxFileSize = 250 * 1000; // 250KB
logFileAppender.StaticLogFileName = true;
XmlConfigurator.Configure();
Logger.Log("Logger is ready", Helpers.LogLevel.Debug);
Logger.Log("Logger is logging to : "+ Path.Combine(
DirectoryHelpers.GetInternalCacheDir(),
"log.txt")
, Helpers.LogLevel.Debug);
}
public static void CreateAndRegister_RaindropInstance()
{
if (ServiceLocator.ServiceLocator.Instance.IsRegistered<RaindropInstance>())
return;
var rdi = new RaindropInstance(new GridClient());
ServiceLocator.ServiceLocator.Instance.Register<RaindropInstance>(rdi);
SendStartupMessageToLogger();
}
public static void StartServiceLocator()
{
if (ServiceLocator.ServiceLocator.Instance == null)
{
ServiceLocator.ServiceLocator.Initiailze();
}
}
void OnApplicationQuit()
{
RaindropInstance instance;
try
{
instance = ServiceLocator.ServiceLocator.Instance.Get<RaindropInstance>();
}
catch
{
return;
}
RaindropNetcom netcom = instance.Netcom;
OpenMetaverse.Logger.Log("Application ending after " + Time.time + " seconds"
, Helpers.LogLevel.Debug);
//if (statusTimer != null)
//{
// statusTimer.Stop();
// statusTimer.Dispose();
// statusTimer = null;
//}
if (!netcom.IsLoggedIn) return;
Thread saveInvToDisk = new Thread(delegate ()
{
instance.Client.Inventory.Store.SaveToDisk(instance.InventoryCacheFileName);
})
{
Name = "Save inventory to disk"
};
saveInvToDisk.Start();
netcom.Logout();
Debug.Log("Logged out! :)");
frmMain_Disposed(instance);
Debug.Log("disposed mainform! :)");
}
//wraps up the netcom and client.
void frmMain_Disposed(RaindropInstance instance)
{
RaindropNetcom netcom = instance.Netcom;
if (netcom != null)
{
netcom.Dispose();
}
//
// if (instance.Client != null)
// {
// instance.UnregisterClientEvents(client);
// }
//if (instance?.Names != null)
//{
// instance.Names.NameUpdated -= new EventHandler<UUIDNameReplyEventArgs>(Names_NameUpdated);
//}
instance.CleanUp();
}
}
}
@@ -22,7 +22,7 @@ namespace Raindrop.Services.Bootstrap
// creates/has dependency on the UIrootGO!!!
private void Awake()
{
RaindropBootstrapper.Start_RaindropInstance();
RaindropBootstrapper.Start_Raindrop_CoreDependencies(); // hacky - to ensure that the UI's dependencies are ready.
OpenMetaverse.Logger.Log("UI variant of application Started. Logging Started.", OpenMetaverse.Helpers.LogLevel.Info);
}
@@ -32,9 +32,7 @@ namespace Raindrop.Services.Bootstrap
//1. mapfetcher - logic, not ui. please refactor
if (!ServiceLocator.ServiceLocator.Instance.IsRegistered<MapService>())
{
Debug.Log("UIBootstrapper creating and registering MapBackend.MapFetcher!");
ServiceLocator.ServiceLocator.Instance.Register<MapService>(new MapService());
//return;
}
//2. ui services
@@ -1,132 +0,0 @@
using System.Threading;
using OpenMetaverse;
using Raindrop.Netcom;
using UnityEngine;
using Logger = OpenMetaverse.Logger;
namespace Raindrop.Services.Bootstrap
{
//This sets up and tears down the raindrop instance service.
//This is attached to a scene as the SOLE gameobject.
// inspired by https://medium.com/medialesson/simple-service-locator-for-your-unity-project-40e317aad307
//please attach this script as a component of the game manager
public class RaindropBootstrapper : MonoBehaviour
{
public GameObject UIBootstrapper;
private RaindropInstance instance => ServiceLocator.ServiceLocator.Instance.Get<RaindropInstance>();
private RaindropNetcom netcom => instance.Netcom;
private void Start()
{
Start_RaindropInstance();
}
private static void StartLogger()
{
Logger.Log("OpenMetaverse.Helpers.LogLevel.Info : Application Started.", Helpers.LogLevel.Info);
}
// //find the unity objects that this script depends on.
// turns out we don't want the base layer to depend on the ui layer, or any unity objects for that matter.
// private void LinkUnityObjects()
// {
// var ui = this.gameObject.GetComponent<UIBootstrapper>();
// if (ui == null)
// {
// Debug.LogError("the UI script is not found.");
// Application.Quit();
// return;
// }
// }
public static void Start_RaindropInstance()
{
// LinkUnityObjects();
//0. start servicelocator pattern.
StartServiceLocator();
//1. main instance.
CreateAndRegister_RaindropInstance();
StartLogger();
//2. construct raindrop instance and register it
var client = ServiceLocator.ServiceLocator.Instance.Get<RaindropInstance>().Client;
Logger.Log("Start_RaindropInstance Bootstrap Success", Helpers.LogLevel.Info);
}
public static void CreateAndRegister_RaindropInstance()
{
if (ServiceLocator.ServiceLocator.Instance.IsRegistered<RaindropInstance>())
return;
Debug.Log("creating and registering raindropinstance!");
var rdi = new RaindropInstance(new GridClient());
ServiceLocator.ServiceLocator.Instance.Register<RaindropInstance>(rdi);
}
public static void StartServiceLocator()
{
if (ServiceLocator.ServiceLocator.Instance == null)
{
ServiceLocator.ServiceLocator.Initiailze();
}
}
void OnApplicationQuit()
{
Debug.Log("Application ending after " + Time.time + " seconds");
//if (statusTimer != null)
//{
// statusTimer.Stop();
// statusTimer.Dispose();
// statusTimer = null;
//}
if (!netcom.IsLoggedIn) return;
Thread saveInvToDisk = new Thread(delegate ()
{
instance.Client.Inventory.Store.SaveToDisk(instance.InventoryCacheFileName);
})
{
Name = "Save inventory to disk"
};
saveInvToDisk.Start();
netcom.Logout();
Debug.Log("Logged out! :)");
frmMain_Disposed();
Debug.Log("disposed mainform! :)");
}
//wraps up the netcom and client.
void frmMain_Disposed( )
{
if (netcom != null)
{
netcom.Dispose();
}
//
// if (instance.Client != null)
// {
// instance.UnregisterClientEvents(client);
// }
//if (instance?.Names != null)
//{
// instance.Names.NameUpdated -= new EventHandler<UUIDNameReplyEventArgs>(Names_NameUpdated);
//}
instance.CleanUp();
}
}
}
-77
View File
@@ -1,77 +0,0 @@
//using log4net.Config;
//using Raindrop;
//using System;
//using System.Collections.Generic;
//using System.IO;
//using System.Linq;
//using System.Text;
//using System.Threading.Tasks;
//using UnityEngine;
//using UnityEngine.SceneManagement;
//namespace ServiceLocator
//{
// //This is like the main() function. it runs before everything else.
// // inspired by https://medium.com/medialesson/simple-service-locator-for-your-unity-project-40e317aad307
// //<Deprecated>
// public static class Bootstrapper
// {
// //[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)]
// public static void Initiailze()
// {
// //if (!log4net.LogManager.GetRepository().Configured)
// //{
// // // log4net not configured
// // foreach (log4net.Util.LogLog message in
// // log4net.LogManager.GetRepository()
// // .ConfigurationMessages
// // .Cast < log4net.Util.LogLog())
// // {
// // // evaluate configuration message
// // }
// //}
// //setup log4net
// //XmlConfigurator.Configure(new FileInfo($"{Application.dataPath}/log4net.xml")); //this cause freeze
// //Debug.Log("xmlconfigurator called.");
// OpenMetaverse.Logger.Log("Logger.Log is working.", OpenMetaverse.Helpers.LogLevel.Info);
// // Initialize default service locator.
// ServiceLocator.Initiailze();
// // Register all your services next.
// ServiceLocator.Instance.Register<RaindropInstance>(new RaindropInstance(new OpenMetaverse.GridClient()));
// ServiceLocator.Instance.Register<UIService>(new UIService());
// // Application is ready to start, load your main scene.
// Scene[] currentScenes = SceneManager.GetAllScenes();
// bool loadUI = true;
// bool load3D = true;
// foreach(var scene in currentScenes)
// {
// if (scene.name == "UIscene")
// {
// loadUI = false;
// }
// if (scene.name == "3Dscene")
// {
// load3D = false;
// }
// }
// if (loadUI)
// SceneManager.LoadScene("UIscene", LoadSceneMode.Additive);
// if (load3D)
// SceneManager.LoadScene("3Dscene", LoadSceneMode.Additive);
// Debug.Log("Bootstrap finished, all scenes loaded.!");
// ServiceLocator.Instance.Get<UIService>().startUIInitialView();
// Debug.Log("UI should be appeared in front of you");
// }
// }
//}
@@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: ade599362d59a714ea3eb5d46c5414e4
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: