Code cleanup.

This commit is contained in:
2023-02-13 16:09:19 +08:00
parent ee4399109a
commit 67b44db1ae
2 changed files with 34 additions and 40 deletions

View File

@@ -16,7 +16,6 @@ using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.SceneManagement;
using UnityEngine.Scripting;
using UnityEngine.UI;
using diag = System.Diagnostics;
using Logger = Cryville.Common.Logger;
@@ -75,6 +74,8 @@ namespace Cryville.Crtr {
#region MonoBehaviour
void Start() {
d_addLogEntry = new Action<int, string, string>(AddLogEntry);
var logobj = GameObject.Find("Logs");
if (logobj != null)
logs = logobj.GetComponent<TextMeshProUGUI>();
@@ -175,6 +176,12 @@ namespace Cryville.Crtr {
if (texHandler.isDone) {
var tex = texHandler.texture;
tex.wrapMode = TextureWrapMode.Clamp;
if (frames.ContainsKey(name)) {
Logger.Log("main", 3, "Load/Prehandle", "Duplicated texture name: {0}", name);
}
else {
frames.Add(name, new SpriteFrame(tex));
}
texs.Add(name, tex);
texLoader.Dispose();
texHandler.Dispose();
@@ -190,14 +197,14 @@ namespace Cryville.Crtr {
}
#endif
}
if (texLoader == null)
if (texLoader == null) {
if (texLoadQueue.Count > 0) {
#if UNITY_5_4_OR_NEWER
texHandler = new DownloadHandlerTexture();
texLoader = new UnityWebRequest(Game.FileProtocolPrefix + texLoadQueue.Dequeue(), "GET", texHandler, null);
texLoader.SendWebRequest();
#else
texLoader = new WWW(Game.FileProtocolPrefix + texLoadQueue.Dequeue());
texLoader = new WWW(Game.FileProtocolPrefix + texLoadQueue.Dequeue());
#endif
}
else if (!texloaddone) {
@@ -205,6 +212,7 @@ namespace Cryville.Crtr {
texloadtimer.Stop();
Logger.Log("main", 1, "Load/MainThread", "Main thread done ({0}ms)", texloadtimer.Elapsed.TotalMilliseconds);
}
}
if (!loadThread.IsAlive) {
if (threadException != null) {
Logger.Log("main", 4, "Load/MainThread", "Load failed");
@@ -226,26 +234,28 @@ namespace Cryville.Crtr {
readonly StringBuffer logsbuf = new StringBuffer();
readonly List<string> logEntries = new List<string>();
int logsLength = 0;
Action<int,string,string> d_addLogEntry;
void AddLogEntry(int level, string module, string msg) {
string color;
switch (level) {
case 0: color = "#888888"; break;
case 1: color = "#bbbbbb"; break;
case 2: color = "#0088ff"; break;
case 3: color = "#ffff00"; break;
case 4: color = "#ff0000"; break;
case 5: color = "#bb0000"; break;
default: color = "#ff00ff"; break;
}
var l = string.Format(
"\n<color={1}bb><{2}> {3}</color>",
DateTime.UtcNow.ToString("s"), color, module, msg
);
logEntries.Add(l);
logsLength += l.Length;
}
void LogUpdate() {
logsbuf.Clear();
Game.MainLogger.Enumerate((level, module, msg) => {
string color;
switch (level) {
case 0: color = "#888888"; break;
case 1: color = "#bbbbbb"; break;
case 2: color = "#0088ff"; break;
case 3: color = "#ffff00"; break;
case 4: color = "#ff0000"; break;
case 5: color = "#bb0000"; break;
default: color = "#ff00ff"; break;
}
var l = string.Format(
"\n<color={1}bb><{2}> {3}</color>",
DateTime.UtcNow.ToString("s"), color, module, msg
);
logEntries.Add(l);
logsLength += l.Length;
});
Game.MainLogger.Enumerate(d_addLogEntry);
while (logsLength >= 4096) {
logsLength -= logEntries[0].Length;
logEntries.RemoveAt(0);
@@ -442,16 +452,6 @@ namespace Cryville.Crtr {
try {
diag::Stopwatch timer = new diag::Stopwatch();
timer.Reset(); timer.Start();
Logger.Log("main", 0, "Load/Prehandle", "Initializing textures");
foreach (var t in texs) {
if (frames.ContainsKey(t.Key)) {
Logger.Log("main", 3, "Load/Prehandle", "Duplicated texture name: {0}", t.Key);
continue;
}
var f = new SpriteFrame(t.Value);
f.Init();
frames.Add(t.Key, f);
}
Logger.Log("main", 0, "Load/Prehandle", "Prehandling (iteration 2)");
cbus.BroadcastPreInit();
Logger.Log("main", 0, "Load/Prehandle", "Prehandling (iteration 3)");

View File

@@ -61,10 +61,9 @@ namespace Cryville.Crtr {
return new Vector2(Texture.width, Texture.height);
}
}
public void Init() {
if (Texture == null)
throw new InvalidOperationException("Missing texture");
public SpriteFrame(Texture2D tex) {
if (tex == null) throw new ArgumentNullException("tex");
Texture = tex;
m_frame = new Rect(Vector2.zero, Size);
var w = m_frame.width;
var h = m_frame.height;
@@ -75,10 +74,5 @@ namespace Cryville.Crtr {
if (m_rotated) UV = new Rect(x, y, tw, -th);
else UV = new Rect(x, y, tw, -th);
}
public SpriteFrame() { }
public SpriteFrame(Texture2D tex) {
Texture = tex;
}
}
}