Skip to content
This repository was archived by the owner on Oct 21, 2022. It is now read-only.

Commit bb7d057

Browse files
committed
Add new console ingame
1 parent 79703b9 commit bb7d057

6 files changed

Lines changed: 358 additions & 125 deletions

File tree

ModLoader/Console.cs

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
using System;
2+
using System.Diagnostics;
3+
using System.IO;
4+
using System.Runtime.InteropServices;
5+
using UnityEngine;
6+
using SFS.IO;
7+
using UnityEngine.SceneManagement;
8+
9+
namespace ModLoader
10+
{
11+
12+
public class Console : MonoBehaviour
13+
{
14+
public static GameObject root;
15+
private ConsoleGUI _consoleGui;
16+
public ConsoleGUI ConsoleGui
17+
{
18+
get
19+
{
20+
return this._consoleGui;
21+
}
22+
}
23+
24+
private FilePath logFile;
25+
26+
27+
private void Awake()
28+
{
29+
Console.root.AddComponent<ConsoleGUI>();
30+
this._consoleGui = Console.root.GetComponent<ConsoleGUI>();
31+
string date = string.Format("{0:yyyy-MM-dd}", DateTime.UtcNow);
32+
this.logFile = FileLocations.BaseFolder.Extend("logs").CreateFolder().ExtendToFile(date+".txt");
33+
}
34+
35+
36+
private void OnEnable()
37+
{
38+
Application.logMessageReceivedThreaded += this.handleLog;
39+
}
40+
41+
private void OnDisable()
42+
{
43+
Application.logMessageReceivedThreaded -= this.handleLog;
44+
}
45+
46+
47+
private void log(string time, string message)
48+
{
49+
this._consoleGui.Logs = $"[{time}] LOG: {message}\n";
50+
this.logFile.AppendText($"[{time}] LOG: {message} \n");
51+
}
52+
53+
private void error(string time, string message)
54+
{
55+
this._consoleGui.Logs = $"[{time}] ERROR: {message}\n";
56+
this.logFile.AppendText($"[{time}] ERROR: {message}\n");
57+
}
58+
59+
private void exception(string time, string message, string stackTrace)
60+
{
61+
this._consoleGui.Logs = $"[{time}] EXCEPTION: {message}\n";
62+
this._consoleGui.Logs = stackTrace;
63+
this.logFile.AppendText($"[{time}] EXCEPTION: {message}\n");
64+
this.logFile.AppendText(stackTrace);
65+
}
66+
67+
private void warning(string time, string message, string stackTrace)
68+
{
69+
this._consoleGui.Logs = $"[{time}] WARNING: {message}\n";
70+
this._consoleGui.Logs = stackTrace;
71+
this.logFile.AppendText($"[{time}] WARNING: {message}\n");
72+
this.logFile.AppendText(stackTrace);
73+
}
74+
75+
private void handleLog(string message, string stackTrace, LogType type)
76+
{
77+
string time = DateTime.UtcNow.ToString("HH:mm");
78+
switch (type)
79+
{
80+
case LogType.Error:
81+
this.error(time, message);
82+
break;
83+
case LogType.Exception:
84+
this.exception(time, message, stackTrace);
85+
break;
86+
case LogType.Warning:
87+
this.warning(time, message, stackTrace);
88+
break;
89+
default:
90+
this.log(time, message);
91+
break;
92+
}
93+
94+
}
95+
96+
}
97+
}

ModLoader/ConsoleGUI.cs

Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
using UnityEngine;
2+
using static UnityEngine.GUI;
3+
4+
namespace ModLoader
5+
{
6+
7+
public class ConsoleGUI : MonoBehaviour
8+
{
9+
private const string WINDOW_NAME = "Mod loader console";
10+
private const float MENU_HEIGHT = 0.6f;
11+
private const float MENU_WIDTH = 0.4f;
12+
13+
// show console initial position and size
14+
private Rect _consoleRect;
15+
16+
//draw windows Gui items
17+
private WindowFunction _windosDraw;
18+
19+
/// show in what position is the scroll of console
20+
private Vector2 _scrollConsole = Vector2.zero;
21+
private Vector2 _scrollScene = Vector2.zero;
22+
private Vector2 _scrollMods = Vector2.zero;
23+
24+
private bool _isVisible;
25+
// show if the console is visible for user
26+
public bool IsVisible
27+
{
28+
get { return this._isVisible; }
29+
set { this._isVisible = value; }
30+
}
31+
32+
// identify what tab render
33+
private int _activeTab;
34+
35+
// tab list
36+
public string[] tabs = new string[] { "Console", "Scene", "Mods" };
37+
38+
// store all logs in game
39+
private GUIContent _logs;
40+
public string Logs
41+
{
42+
set
43+
{
44+
this._logs.text += value;
45+
if (this._activeTab == 0 && this._scrollConsole.y > this._consoleheight * 0.2)
46+
{
47+
this._scrollConsole.y = this._consoleheight;
48+
}
49+
50+
51+
}
52+
}
53+
54+
private GUIContent _sceneGameObjects;
55+
public GameObject[] SceneGameObjects
56+
{
57+
set
58+
{
59+
this._sceneGameObjects = new GUIContent();
60+
Component[] components;
61+
foreach (GameObject sceneObject in value)
62+
{
63+
this._sceneGameObjects.text += sceneObject.name + "\n";
64+
components = sceneObject.GetComponents(typeof(Component));
65+
foreach (Component component in components)
66+
{
67+
this._sceneGameObjects.text += "\t" + component.ToString() + "\n";
68+
}
69+
}
70+
}
71+
}
72+
73+
private GUIContent _mods;
74+
public SFSMod[] Mods
75+
{
76+
set
77+
{
78+
this._mods = new GUIContent();
79+
foreach (SFSMod mod in value)
80+
{
81+
this._mods.text += "Name:" + mod.Name + "\n";
82+
this._mods.text += "Id:" + mod.ModId + "\n";
83+
this._mods.text += "Author:" + mod.Author + "\n";
84+
this._mods.text += "Description:\n";
85+
this._mods.text += mod.Description + "\n\n";
86+
}
87+
}
88+
}
89+
90+
// console styles like background and text color
91+
private GUIStyle _consoleStyle;
92+
93+
// this make resize console
94+
private float _consoleheight;
95+
96+
void Awake()
97+
{
98+
this._isVisible = true;
99+
this._consoleStyle = new GUIStyle();
100+
this._logs = new GUIContent();
101+
this._sceneGameObjects = new GUIContent();
102+
this._mods = new GUIContent();
103+
this._consoleRect = new Rect(Screen.width * 0.1f, Screen.height * 0.1f, Screen.width * MENU_WIDTH, Screen.height * MENU_HEIGHT);
104+
this._windosDraw = new GUI.WindowFunction(this.windowFunction);
105+
}
106+
107+
void Start()
108+
{
109+
this._consoleStyle.normal.textColor = Color.white;
110+
this._consoleStyle.normal.background = this.makeTexture(Color.black);
111+
}
112+
113+
/// <sumary>
114+
/// Create solid color texture
115+
///</sumary>
116+
private Texture2D makeTexture(Color col)
117+
{
118+
Color[] pix = new Color[1];
119+
for (int i = 0; i < pix.Length; ++i)
120+
{
121+
pix[i] = col;
122+
}
123+
Texture2D result = new Texture2D(1, 1);
124+
result.SetPixels(pix);
125+
result.Apply();
126+
return result;
127+
}
128+
129+
private void Update()
130+
{
131+
if (Input.GetKeyDown(KeyCode.F1)){
132+
this._isVisible = !this._isVisible;
133+
}
134+
}
135+
136+
async void OnGUI()
137+
{
138+
139+
if (this._isVisible)
140+
{
141+
this._consoleRect = GUI.Window(GUIUtility.GetControlID(FocusType.Passive), this._consoleRect, this._windosDraw, WINDOW_NAME);
142+
}
143+
}
144+
145+
private void windowFunction(int windowID)
146+
{
147+
this._activeTab = GUI.Toolbar(new Rect(10, 20, this._consoleRect.width - 20f, 30), this._activeTab, this.tabs);
148+
switch (this._activeTab)
149+
{
150+
case 0:
151+
this.consoleTab();
152+
break;
153+
case 1:
154+
this.sceneTab();
155+
break;
156+
case 2:
157+
this.modsTab();
158+
break;
159+
}
160+
GUI.DragWindow();
161+
}
162+
163+
private void consoleTab()
164+
{
165+
this._consoleheight = this._consoleStyle.CalcHeight(this._logs, this._consoleRect.width - 20f);
166+
if (this._consoleheight < this._consoleRect.height - 60f)
167+
{
168+
this._scrollConsole.y = this._consoleheight = this._consoleRect.height - 60f;
169+
}
170+
171+
this._scrollConsole = GUI.BeginScrollView(new Rect(0, 55, this._consoleRect.width, this._consoleRect.height - 60f), this._scrollConsole, new Rect(0, 0, this._consoleRect.width - 20f, this._consoleheight));
172+
173+
GUI.Box(new Rect(10, 0, this._consoleRect.width - 30f, this._consoleheight), this._logs, this._consoleStyle);
174+
175+
GUI.EndScrollView();
176+
}
177+
178+
private void modsTab()
179+
{
180+
this._consoleheight = this._consoleStyle.CalcHeight(this._mods, this._consoleRect.width - 20f);
181+
if (this._consoleheight < this._consoleRect.height - 60f)
182+
{
183+
this._scrollMods.y = this._consoleheight = this._consoleRect.height - 60f;
184+
}
185+
186+
this._scrollMods = GUI.BeginScrollView(new Rect(0, 55, this._consoleRect.width, this._consoleRect.height - 60f), this._scrollMods, new Rect(0, 0, this._consoleRect.width - 20f, this._consoleheight));
187+
188+
GUI.Box(new Rect(10, 0, this._consoleRect.width - 30f, this._consoleheight), this._mods, this._consoleStyle);
189+
190+
GUI.EndScrollView();
191+
}
192+
193+
private void sceneTab()
194+
{
195+
this._consoleheight = this._consoleStyle.CalcHeight(this._sceneGameObjects, this._consoleRect.width - 20f);
196+
if (this._consoleheight < this._consoleRect.height - 60f)
197+
{
198+
this._scrollScene.y = this._consoleheight = this._consoleRect.height - 60f;
199+
}
200+
201+
this._scrollScene = GUI.BeginScrollView(new Rect(0, 55, this._consoleRect.width, this._consoleRect.height - 60f), this._scrollScene, new Rect(0, 0, this._consoleRect.width - 20f, this._consoleheight));
202+
203+
GUI.Box(new Rect(10, 0, this._consoleRect.width - 30f, this._consoleheight), this._sceneGameObjects, this._consoleStyle);
204+
205+
GUI.EndScrollView();
206+
}
207+
}
208+
209+
}

ModLoader/Loader.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public class Loader : MonoBehaviour
1919

2020
// This save Loader instance
2121
public static Loader main;
22+
private Console _console;
2223

2324
// This save the gameObject that implement Loader class
2425
public static GameObject root;
@@ -73,10 +74,23 @@ private void Start()
7374
catch (Exception e)
7475
{
7576
Debug.LogException(e);
77+
this._modList.Remove(mod.ModId);
7678
}
7779
}
80+
this.postloading();
81+
Debug.Log("All Ready");
7882
}
7983

84+
private void postloading()
85+
{
86+
this._console = Console.root.GetComponent<Console>();
87+
this._console.ConsoleGui.Mods = this.getMods();
88+
this._console.ConsoleGui.SceneGameObjects = UnityEngine.Object.FindObjectsOfType<GameObject>();
89+
}
90+
91+
//this._consoleGui.Mods = Loader.main.getMods();
92+
//Loader.main.suscribeOnChangeScene(this.OnSceneLoaded);
93+
8094
/// <summary>
8195
/// get mod instance.
8296
/// </summary>
@@ -87,6 +101,16 @@ public SFSMod getMod(string modId)
87101
return this._modList[modId];
88102
}
89103

104+
public SFSMod[] getMods()
105+
{
106+
List<SFSMod>mods = new List<SFSMod>();
107+
foreach(SFSMod mod in this._modList.Values)
108+
{
109+
mods.Add(mod);
110+
}
111+
return mods.ToArray();
112+
}
113+
90114
/// <summary>
91115
/// This method read MODS folder and identify what is the entry point for each folder.
92116
/// </summary>
@@ -256,6 +280,11 @@ private bool verifyVersion(string version1, string version2)
256280
private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
257281
{
258282
Debug.Log("Scene change to "+scene.name);
283+
if (this._console)
284+
{
285+
this._console.ConsoleGui.SceneGameObjects = UnityEngine.Object.FindObjectsOfType<GameObject>();
286+
}
287+
259288
}
260289

261290
/// <summary>

0 commit comments

Comments
 (0)