Skip to content
Snippets Groups Projects
Commit b21f6e65 authored by MaZiFAU's avatar MaZiFAU
Browse files

Performance Improvements;

Performance Improvements:
+cache all Scrolls
+cache Stages in StageStatic.ShallowLoadStages
parent 8fa3d1ad
No related branches found
No related tags found
No related merge requests found
using UnityEngine; using Newtonsoft.Json;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
public class GlobalBehaviour : MonoBehaviour, ISerializationCallbackReceiver public class GlobalBehaviour : MonoBehaviour, ISerializationCallbackReceiver
{ {
...@@ -73,5 +78,72 @@ void ISerializationCallbackReceiver.OnBeforeSerialize() ...@@ -73,5 +78,72 @@ void ISerializationCallbackReceiver.OnBeforeSerialize()
private void Awake() private void Awake()
{ {
Instance = this; Instance = this;
DontDestroyOnLoad(this);
}
private void Start()
{
PostServerConnection();
}
//TODO: Move where appropiate
public int tryScrollListTimes = 2;
static public List<Scroll> AvailableScrolls;
private void PostServerConnection()
{
StartCoroutine(getScrollsfromServer());
IEnumerator getScrollsfromServer()
{
//Try /scroll/listAll endpoint when scroll/list is not working
UnityWebRequest request = UnityWebRequest.Get(CommunicationEvents.ServerAdress + "/scroll/list");
//Postman-Echo-Mock
//UnityWebRequest request = UnityWebRequest.Get("https://019a8ea5-843a-498b-8d0c-778669aef987.mock.pstmn.io/get");
for (int i = 0; i < this.tryScrollListTimes; i++)
{
request = UnityWebRequest.Get(CommunicationEvents.ServerAdress + "/scroll/list");
request.method = UnityWebRequest.kHttpVerbGET;
yield return request.SendWebRequest();
if (request.result == UnityWebRequest.Result.ConnectionError
|| request.result == UnityWebRequest.Result.ProtocolError)
{
Debug.LogWarning(request.error);
Debug.Log("GET Scroll/list failed. Attempt: " + (i + 1).ToString());
}
else
break;
}
string jsonString = null;
if (request.result == UnityWebRequest.Result.ConnectionError
|| request.result == UnityWebRequest.Result.ProtocolError)
{
Debug.LogWarning(request.error);
}
else
{
CommunicationEvents.ServerRunning = true;
jsonString = request.downloadHandler.text;
}
if (string.IsNullOrEmpty(jsonString)
|| jsonString.Equals("[]"))
{
jsonString = File.ReadAllText(Application.streamingAssetsPath + "/scrolls.json");
Debug.Log("Using Fallback Scrolls: \n" + jsonString);
}
System.DateTime startTime = System.DateTime.UtcNow;
AvailableScrolls = JsonConvert.DeserializeObject<List<Scroll>>(jsonString);
Debug.Log("Scroll Parsing in: " + (System.DateTime.UtcNow - startTime).TotalMilliseconds + "ms");
}
} }
} }
using Newtonsoft.Json; using System.Collections.Generic;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq; using System.Linq;
using TMPro; using TMPro;
using UnityEngine; using UnityEngine;
using UnityEngine.Networking;
public class DisplayScrolls : MonoBehaviour public class DisplayScrolls : MonoBehaviour
{ {
public string preferredStartScrollName; public string preferredStartScrollName;
public int tryScrollListTimes = 2;
static public List<Scroll> AvailableScrolls;
static public List<Scroll> AllowedScrolls; static public List<Scroll> AllowedScrolls;
public GameObject[] ScrollButtons; public GameObject[] ScrollButtons;
public GameObject ScrollPrefab; public GameObject ScrollPrefab;
public GameObject DetailScreen; public GameObject DetailScreen;
public Transform scrollscreenContent; public Transform scrollscreenContent;
public int x_Start;
public int y_Start;
public int X_Pacece_Between_Items;
public int y_Pacece_Between_Items;
public int number_of_Column;
public Vector3 GetPosition(int i)
{
//return new Vector3(x_Start + (X_Pacece_Between_Items * (i % number_of_Column)), y_Start + (-y_Pacece_Between_Items * (i / number_of_Column)), 0f);
return Vector3.zero;
}
// Start is called before the first frame update
void Start() void Start()
{ {
var rect = GetComponent<RectTransform>(); BuildScrollGUI();
x_Start = (int)(rect.rect.x + X_Pacece_Between_Items * .5f);
y_Start = (int)(-rect.rect.y - y_Pacece_Between_Items * .5f);//);
number_of_Column = Mathf.Max(1, (int)(rect.rect.width / ScrollPrefab.GetComponent<RectTransform>().rect.width) - 1);
//get Scrolls from Backend;
//string path = "Mock-Scrolls.json";
//string jsonString = File.ReadAllText(path);
//buildScrollSelection(jsonString);
StartCoroutine(getScrollsfromServer());
} }
IEnumerator getScrollsfromServer() void BuildScrollGUI()
{
//Try /scroll/listAll endpoint when scroll/list is not working
UnityWebRequest request = UnityWebRequest.Get(CommunicationEvents.ServerAdress + "/scroll/list");
//Postman-Echo-Mock
//UnityWebRequest request = UnityWebRequest.Get("https://019a8ea5-843a-498b-8d0c-778669aef987.mock.pstmn.io/get");
for (int i = 0; i < this.tryScrollListTimes; i++)
{
request = UnityWebRequest.Get(CommunicationEvents.ServerAdress + "/scroll/list");
request.method = UnityWebRequest.kHttpVerbGET;
yield return request.SendWebRequest();
if (request.result == UnityWebRequest.Result.ConnectionError
|| request.result == UnityWebRequest.Result.ProtocolError)
{ {
Debug.LogWarning(request.error); AllowedScrolls = GlobalBehaviour.AvailableScrolls
Debug.Log("GET Scroll/list failed. Attempt: " + (i + 1).ToString());
}
else
{
break;
}
}
if (request.result == UnityWebRequest.Result.ConnectionError
|| request.result == UnityWebRequest.Result.ProtocolError)
{
Debug.LogWarning(request.error);
string jsonString = File.ReadAllText(Application.streamingAssetsPath + "/scrolls.json");
Debug.Log(jsonString);
BuildScrolls(jsonString);
}
else
{
CommunicationEvents.ServerRunning = true;
string jsonString = request.downloadHandler.text;
Debug.Log("JsonString from Server: \n" + jsonString);
if (jsonString.Equals("[]"))
jsonString = File.ReadAllText(Application.streamingAssetsPath + "/scrolls.json");
Debug.Log("Used JsonString: \n" + jsonString);
//scroll display not yet implemented;
//buildScrollSelection(jsonString);
BuildScrolls(jsonString);
}
}
void BuildScrolls(string jsonString)
{
System.DateTime startTime = System.DateTime.UtcNow;
AvailableScrolls = JsonConvert.DeserializeObject<List<Scroll>>(jsonString);
Debug.Log("Scroll Parsing in: " + (System.DateTime.UtcNow - startTime).TotalMilliseconds + "ms");
AllowedScrolls = AvailableScrolls
.Where(s => StageStatic.stage.AllowedScrolls?.Contains(s.@ref) ?? true) .Where(s => StageStatic.stage.AllowedScrolls?.Contains(s.@ref) ?? true)
.ToList(); .ToList();
...@@ -113,8 +29,7 @@ void BuildScrolls(string jsonString) ...@@ -113,8 +29,7 @@ void BuildScrolls(string jsonString)
ScrollButtons = new GameObject[AllowedScrolls.Count()]; ScrollButtons = new GameObject[AllowedScrolls.Count()];
for (int i = 0; i < AllowedScrolls.Count; i++) for (int i = 0; i < AllowedScrolls.Count; i++)
{ {
var obj = Instantiate(ScrollPrefab, Vector3.zero, Quaternion.identity, scrollscreenContent); var obj = Instantiate(ScrollPrefab, scrollscreenContent);
obj.GetComponent<RectTransform>().localPosition = GetPosition(i);
obj.GetComponent<ScrollClickedScript>().scroll = AllowedScrolls[i]; obj.GetComponent<ScrollClickedScript>().scroll = AllowedScrolls[i];
obj.GetComponent<ScrollClickedScript>().DetailScreen = this.DetailScreen; obj.GetComponent<ScrollClickedScript>().DetailScreen = this.DetailScreen;
obj.transform.GetChild(0).gameObject.GetComponent<TextMeshProUGUI>().text = AllowedScrolls[i].label; obj.transform.GetChild(0).gameObject.GetComponent<TextMeshProUGUI>().text = AllowedScrolls[i].label;
......
...@@ -118,8 +118,6 @@ IEnumerator newAssignment() ...@@ -118,8 +118,6 @@ IEnumerator newAssignment()
} }
else else
{ {
// Todo delte maybe
Debug.Log("Current mmt answer: " + currentMmtAnswer);
Scroll.ScrollDynamicInfo scrollDynamicInfo = JsonConvert.DeserializeObject<Scroll.ScrollDynamicInfo>(currentMmtAnswer); Scroll.ScrollDynamicInfo scrollDynamicInfo = JsonConvert.DeserializeObject<Scroll.ScrollDynamicInfo>(currentMmtAnswer);
processScrollDynamicInfo(scrollDynamicInfo); processScrollDynamicInfo(scrollDynamicInfo);
} }
......
...@@ -300,11 +300,13 @@ public static bool ContainsNumber(string category, int i, bool local) ...@@ -300,11 +300,13 @@ public static bool ContainsNumber(string category, int i, bool local)
/// <summary> /// <summary>
/// Looks for and initial loads (see <see cref="Stage.ShallowLoad(out Stage, string)"/>) <see cref="Stage">Stages</see> in <see cref="local_stage"/> and !<see cref="local_stage"/>. /// Looks for and initial loads (see <see cref="Stage.ShallowLoad(out Stage, string)"/>) <see cref="Stage">Stages</see> in <see cref="local_stage"/> and !<see cref="local_stage"/>.
/// </summary> /// </summary>
public static void ShallowLoadStages() public static void ShallowLoadStages(bool force = false)
{ {
if(force)
StageOfficial = StageLocal = null; StageOfficial = StageLocal = null;
StageOfficial = Stage.Grup(null, true);
StageLocal = Stage.Grup(null, false); StageOfficial ??= Stage.Grup(null, true);
StageLocal ??= Stage.Grup(null, false);
} }
/// <summary> /// <summary>
......
...@@ -33,7 +33,7 @@ void Start() ...@@ -33,7 +33,7 @@ void Start()
StartCoroutine(ServerRoutine1()); StartCoroutine(ServerRoutine1());
} }
} }
// Update is called once per frame
void Update() void Update()
{ {
if (autoend == 1) if (autoend == 1)
...@@ -47,9 +47,6 @@ void Update() ...@@ -47,9 +47,6 @@ void Update()
} }
} }
void PrepareGame() void PrepareGame()
{ {
if (autoprepareGame !=0) if (autoprepareGame !=0)
...@@ -58,16 +55,8 @@ void PrepareGame() ...@@ -58,16 +55,8 @@ void PrepareGame()
CommunicationEvents.ServerRunning = true; CommunicationEvents.ServerRunning = true;
UnityEngine.Debug.Log("server fin"); UnityEngine.Debug.Log("server fin");
} }
} }
IEnumerator ServerRoutine1() IEnumerator ServerRoutine1()
{ {
...@@ -89,13 +78,8 @@ IEnumerator ServerRoutine1() ...@@ -89,13 +78,8 @@ IEnumerator ServerRoutine1()
process = Process.Start(processInfo); process = Process.Start(processInfo);
} }
else else
/*
*/
Process.Start("powershell.exe", command); Process.Start("powershell.exe", command);
// *** Read the streams *** // *** Read the streams ***
// Warning: This approach can lead to deadlocks, see Edit #2 // Warning: This approach can lead to deadlocks, see Edit #2
//string output = process.StandardOutput.ReadToEnd(); //string output = process.StandardOutput.ReadToEnd();
...@@ -109,15 +93,9 @@ IEnumerator ServerRoutine1() ...@@ -109,15 +93,9 @@ IEnumerator ServerRoutine1()
// Console.WriteLine("ExitCode: " + exitCode.ToString(), "ExecuteCommand"); // Console.WriteLine("ExitCode: " + exitCode.ToString(), "ExecuteCommand");
// process.Close(); // process.Close();
yield return null; yield return null;
} }
IEnumerator ServerRoutine() IEnumerator ServerRoutine()
{ {
CommunicationEvents.ServerAddressLocal = ServerAddressLocalhost + ":" + ServerPortDefault; CommunicationEvents.ServerAddressLocal = ServerAddressLocalhost + ":" + ServerPortDefault;
...@@ -153,7 +131,7 @@ IEnumerator ServerRoutine() ...@@ -153,7 +131,7 @@ IEnumerator ServerRoutine()
#endif #endif
while (true && autocheckIfServerIsRunning != 0) while (autocheckIfServerIsRunning != 0)
{ {
//Wait for 2 seconds //Wait for 2 seconds
yield return new WaitForSecondsRealtime(2f); yield return new WaitForSecondsRealtime(2f);
...@@ -171,8 +149,6 @@ IEnumerator ServerRoutine() ...@@ -171,8 +149,6 @@ IEnumerator ServerRoutine()
break; break;
} }
yield return null; yield return null;
} }
...@@ -192,6 +168,4 @@ IEnumerator ServerRoutine() ...@@ -192,6 +168,4 @@ IEnumerator ServerRoutine()
PrepareGame(); PrepareGame();
yield return null; yield return null;
} }
} }
\ No newline at end of file
using System.Collections; using System.Collections;
using System.Diagnostics;
using UnityEngine; using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.SceneManagement;
using static UIconfig;
using static CommunicationEvents;
public class StartServer_mctrl : MonoBehaviour public class StartServer_mctrl : MonoBehaviour
{ {
public GameObject ServerScriptGObj; public GameObject ServerScriptGObj;
// Start is called before the first frame update
void Start() void Start()
{ {
StartCoroutine(ServerRoutine()); StartCoroutine(ServerRoutine());
...@@ -19,9 +13,11 @@ void Start() ...@@ -19,9 +13,11 @@ void Start()
IEnumerator ServerRoutine() IEnumerator ServerRoutine()
{ {
//Wait for 1 seconds ////Wait for 1 seconds
yield return new WaitForSecondsRealtime(1f); //yield return new WaitForSecondsRealtime(1f);
if (ServerAutoStart == true) if (CommunicationEvents.ServerAutoStart)
ServerScriptGObj.SetActive(true); ServerScriptGObj.SetActive(true);
yield break;
} }
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment