100 lines
4.1 KiB
C#
100 lines
4.1 KiB
C#
using Cryville.Common.Font;
|
|
using Cryville.Common.Logging;
|
|
using Cryville.Common.Unity.UI;
|
|
using Cryville.Culture;
|
|
using System;
|
|
using System.Globalization;
|
|
using System.IO;
|
|
using System.Text;
|
|
using System.Xml;
|
|
using System.Xml.Linq;
|
|
using UnityEngine;
|
|
using Logger = Cryville.Common.Logging.Logger;
|
|
|
|
namespace Cryville.EEW.Unity {
|
|
class App {
|
|
public static string AppDataPath { get; private set; }
|
|
|
|
public static Logger MainLogger { get; private set; }
|
|
static FileStream _logFileStream;
|
|
static StreamLoggerListener _logWriter;
|
|
|
|
static bool _init;
|
|
public static void Init() {
|
|
if (_init) return;
|
|
_init = true;
|
|
|
|
AppDataPath = Application.persistentDataPath;
|
|
|
|
var logPath = Directory.CreateDirectory(Path.Combine(AppDataPath, "logs"));
|
|
_logFileStream = new FileStream(
|
|
Path.Combine(
|
|
logPath.FullName,
|
|
string.Format(
|
|
CultureInfo.InvariantCulture,
|
|
"{0}.log",
|
|
DateTimeOffset.UtcNow.ToString("yyyyMMddHHmmssfff", CultureInfo.InvariantCulture)
|
|
)
|
|
),
|
|
FileMode.Create, FileAccess.Write, FileShare.Read
|
|
);
|
|
_logWriter = new StreamLoggerListener(_logFileStream) { AutoFlush = true };
|
|
MainLogger = new Logger();
|
|
var listener = new InstantLoggerListener();
|
|
listener.Log += MainLogger.Log;
|
|
MainLogger.AddListener(_logWriter);
|
|
Application.logMessageReceivedThreaded += OnInternalLog;
|
|
|
|
MainLogger.Log(1, "App", null, "App Version: {0}", Application.version);
|
|
MainLogger.Log(1, "App", null, "Unity Version: {0}", Application.unityVersion);
|
|
MainLogger.Log(1, "App", null, "Operating System: {0}, Unity = {1}, Family = {2}", Environment.OSVersion, SystemInfo.operatingSystem, SystemInfo.operatingSystemFamily);
|
|
MainLogger.Log(1, "App", null, "Platform: Build = {0}, Unity = {1}", PlatformConfig.Name, Application.platform);
|
|
MainLogger.Log(1, "App", null, "Culture: {0}, UI = {1}, Unity = {2}", SharedCultures.CurrentCulture, SharedCultures.CurrentUICulture, Application.systemLanguage);
|
|
MainLogger.Log(1, "App", null, "Device: Model = {0}, Type = {1}", SystemInfo.deviceModel, SystemInfo.deviceType);
|
|
MainLogger.Log(1, "App", null, "Graphics: Name = {0}, Type = {1}, Vendor = {2}, Version = {3}", SystemInfo.graphicsDeviceName, SystemInfo.graphicsDeviceType, SystemInfo.graphicsDeviceVendor, SystemInfo.graphicsDeviceVersion);
|
|
MainLogger.Log(1, "App", null, "Processor: Count = {0}, Frequency = {1}MHz, Type = {2}", SystemInfo.processorCount, SystemInfo.processorFrequency, SystemInfo.processorType);
|
|
|
|
MainLogger.Log(1, "App", null, "Initializing font manager");
|
|
foreach (var res in Resources.LoadAll<TextAsset>("cldr/common/validity")) {
|
|
IdValidity.Load(LoadXmlDocument(res));
|
|
}
|
|
var metadata = new SupplementalMetadata(LoadXmlDocument("cldr/common/supplemental/supplementalMetadata"));
|
|
var subtags = new LikelySubtags(LoadXmlDocument("cldr/common/supplemental/likelySubtags"), metadata);
|
|
var matcher = new LanguageMatching(LoadXmlDocument("cldr/common/supplemental/languageInfo"), subtags);
|
|
TMPLocalizedText.FontMatcher = new FallbackListFontMatcher(matcher, PlatformConfig.FontManager) {
|
|
MapScriptToTypefaces = PlatformConfig.ScriptFontMap
|
|
};
|
|
TMPLocalizedText.DefaultShader = Resources.Load<Shader>(PlatformConfig.TextShader);
|
|
|
|
MainLogger.Log(1, "App", null, "Loading config");
|
|
SharedSettings.Instance.Init();
|
|
|
|
MainLogger.Log(1, "App", null, "Initialized");
|
|
}
|
|
|
|
static readonly Encoding _encoding = new UTF8Encoding(false, true);
|
|
static readonly XmlReaderSettings _xmlSettings = new() {
|
|
DtdProcessing = DtdProcessing.Ignore,
|
|
};
|
|
static XDocument LoadXmlDocument(string path) {
|
|
return LoadXmlDocument(Resources.Load<TextAsset>(path));
|
|
}
|
|
static XDocument LoadXmlDocument(TextAsset asset) {
|
|
using var stream = new MemoryStream(_encoding.GetBytes(asset.text));
|
|
using var reader = XmlReader.Create(stream, _xmlSettings);
|
|
return XDocument.Load(reader);
|
|
}
|
|
|
|
static void OnInternalLog(string condition, string stackTrace, LogType type) {
|
|
var l = type switch {
|
|
LogType.Log => 1,
|
|
LogType.Assert => 2,
|
|
LogType.Warning => 3,
|
|
LogType.Error or LogType.Exception => 4,
|
|
_ => 1,
|
|
};
|
|
MainLogger.Log(l, "Internal", null, "{0}\n{1}", condition, stackTrace);
|
|
}
|
|
}
|
|
}
|