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.Networking;
using UnityEngine.SceneManagement; using UnityEngine.SceneManagement;
using UnityEngine.Scripting; using UnityEngine.Scripting;
using UnityEngine.UI;
using diag = System.Diagnostics; using diag = System.Diagnostics;
using Logger = Cryville.Common.Logger; using Logger = Cryville.Common.Logger;
@@ -75,6 +74,8 @@ namespace Cryville.Crtr {
#region MonoBehaviour #region MonoBehaviour
void Start() { void Start() {
d_addLogEntry = new Action<int, string, string>(AddLogEntry);
var logobj = GameObject.Find("Logs"); var logobj = GameObject.Find("Logs");
if (logobj != null) if (logobj != null)
logs = logobj.GetComponent<TextMeshProUGUI>(); logs = logobj.GetComponent<TextMeshProUGUI>();
@@ -175,6 +176,12 @@ namespace Cryville.Crtr {
if (texHandler.isDone) { if (texHandler.isDone) {
var tex = texHandler.texture; var tex = texHandler.texture;
tex.wrapMode = TextureWrapMode.Clamp; 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); texs.Add(name, tex);
texLoader.Dispose(); texLoader.Dispose();
texHandler.Dispose(); texHandler.Dispose();
@@ -190,7 +197,7 @@ namespace Cryville.Crtr {
} }
#endif #endif
} }
if (texLoader == null) if (texLoader == null) {
if (texLoadQueue.Count > 0) { if (texLoadQueue.Count > 0) {
#if UNITY_5_4_OR_NEWER #if UNITY_5_4_OR_NEWER
texHandler = new DownloadHandlerTexture(); texHandler = new DownloadHandlerTexture();
@@ -205,6 +212,7 @@ namespace Cryville.Crtr {
texloadtimer.Stop(); texloadtimer.Stop();
Logger.Log("main", 1, "Load/MainThread", "Main thread done ({0}ms)", texloadtimer.Elapsed.TotalMilliseconds); Logger.Log("main", 1, "Load/MainThread", "Main thread done ({0}ms)", texloadtimer.Elapsed.TotalMilliseconds);
} }
}
if (!loadThread.IsAlive) { if (!loadThread.IsAlive) {
if (threadException != null) { if (threadException != null) {
Logger.Log("main", 4, "Load/MainThread", "Load failed"); Logger.Log("main", 4, "Load/MainThread", "Load failed");
@@ -226,9 +234,8 @@ namespace Cryville.Crtr {
readonly StringBuffer logsbuf = new StringBuffer(); readonly StringBuffer logsbuf = new StringBuffer();
readonly List<string> logEntries = new List<string>(); readonly List<string> logEntries = new List<string>();
int logsLength = 0; int logsLength = 0;
void LogUpdate() { Action<int,string,string> d_addLogEntry;
logsbuf.Clear(); void AddLogEntry(int level, string module, string msg) {
Game.MainLogger.Enumerate((level, module, msg) => {
string color; string color;
switch (level) { switch (level) {
case 0: color = "#888888"; break; case 0: color = "#888888"; break;
@@ -245,7 +252,10 @@ namespace Cryville.Crtr {
); );
logEntries.Add(l); logEntries.Add(l);
logsLength += l.Length; logsLength += l.Length;
}); }
void LogUpdate() {
logsbuf.Clear();
Game.MainLogger.Enumerate(d_addLogEntry);
while (logsLength >= 4096) { while (logsLength >= 4096) {
logsLength -= logEntries[0].Length; logsLength -= logEntries[0].Length;
logEntries.RemoveAt(0); logEntries.RemoveAt(0);
@@ -442,16 +452,6 @@ namespace Cryville.Crtr {
try { try {
diag::Stopwatch timer = new diag::Stopwatch(); diag::Stopwatch timer = new diag::Stopwatch();
timer.Reset(); timer.Start(); 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)"); Logger.Log("main", 0, "Load/Prehandle", "Prehandling (iteration 2)");
cbus.BroadcastPreInit(); cbus.BroadcastPreInit();
Logger.Log("main", 0, "Load/Prehandle", "Prehandling (iteration 3)"); 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); return new Vector2(Texture.width, Texture.height);
} }
} }
public SpriteFrame(Texture2D tex) {
public void Init() { if (tex == null) throw new ArgumentNullException("tex");
if (Texture == null) Texture = tex;
throw new InvalidOperationException("Missing texture");
m_frame = new Rect(Vector2.zero, Size); m_frame = new Rect(Vector2.zero, Size);
var w = m_frame.width; var w = m_frame.width;
var h = m_frame.height; var h = m_frame.height;
@@ -75,10 +74,5 @@ namespace Cryville.Crtr {
if (m_rotated) UV = new Rect(x, y, tw, -th); if (m_rotated) UV = new Rect(x, y, tw, -th);
else UV = new Rect(x, y, tw, -th); else UV = new Rect(x, y, tw, -th);
} }
public SpriteFrame() { }
public SpriteFrame(Texture2D tex) {
Texture = tex;
}
} }
} }