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
{
......@@ -7,7 +12,7 @@ public static GlobalBehaviour Instance
get => _Instance;
set
{
if (_Instance == null)
if (_Instance == null)
_Instance = value;
else
Destroy(value);
......@@ -73,5 +78,72 @@ void ISerializationCallbackReceiver.OnBeforeSerialize()
private void Awake()
{
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;
using System.Collections.Generic;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using TMPro;
using UnityEngine;
using UnityEngine.Networking;
public class DisplayScrolls : MonoBehaviour
{
public string preferredStartScrollName;
public int tryScrollListTimes = 2;
static public List<Scroll> AvailableScrolls;
static public List<Scroll> AllowedScrolls;
public GameObject[] ScrollButtons;
public GameObject ScrollPrefab;
public GameObject DetailScreen;
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()
{
var rect = GetComponent<RectTransform>();
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()
{
//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;
}
}
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);
}
BuildScrollGUI();
}
void BuildScrolls(string jsonString)
void BuildScrollGUI()
{
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
AllowedScrolls = GlobalBehaviour.AvailableScrolls
.Where(s => StageStatic.stage.AllowedScrolls?.Contains(s.@ref) ?? true)
.ToList();
//Build Selection-GUI of Scrolls
ScrollButtons = new GameObject[AllowedScrolls.Count()];
for (int i = 0; i < AllowedScrolls.Count; i++)
{
var obj = Instantiate(ScrollPrefab, Vector3.zero, Quaternion.identity, scrollscreenContent);
obj.GetComponent<RectTransform>().localPosition = GetPosition(i);
{
var obj = Instantiate(ScrollPrefab, scrollscreenContent);
obj.GetComponent<ScrollClickedScript>().scroll = AllowedScrolls[i];
obj.GetComponent<ScrollClickedScript>().DetailScreen = this.DetailScreen;
obj.transform.GetChild(0).gameObject.GetComponent<TextMeshProUGUI>().text = AllowedScrolls[i].label;
......
......@@ -118,8 +118,6 @@ IEnumerator newAssignment()
}
else
{
// Todo delte maybe
Debug.Log("Current mmt answer: " + currentMmtAnswer);
Scroll.ScrollDynamicInfo scrollDynamicInfo = JsonConvert.DeserializeObject<Scroll.ScrollDynamicInfo>(currentMmtAnswer);
processScrollDynamicInfo(scrollDynamicInfo);
}
......
......@@ -300,11 +300,13 @@ public static bool ContainsNumber(string category, int i, bool local)
/// <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"/>.
/// </summary>
public static void ShallowLoadStages()
public static void ShallowLoadStages(bool force = false)
{
StageOfficial = StageLocal = null;
StageOfficial = Stage.Grup(null, true);
StageLocal = Stage.Grup(null, false);
if(force)
StageOfficial = StageLocal = null;
StageOfficial ??= Stage.Grup(null, true);
StageLocal ??= Stage.Grup(null, false);
}
/// <summary>
......
......@@ -33,7 +33,7 @@ void Start()
StartCoroutine(ServerRoutine1());
}
}
// Update is called once per frame
void Update()
{
if (autoend == 1)
......@@ -47,9 +47,6 @@ void Update()
}
}
void PrepareGame()
{
if (autoprepareGame !=0)
......@@ -58,16 +55,8 @@ void PrepareGame()
CommunicationEvents.ServerRunning = true;
UnityEngine.Debug.Log("server fin");
}
}
IEnumerator ServerRoutine1()
{
......@@ -89,13 +78,8 @@ IEnumerator ServerRoutine1()
process = Process.Start(processInfo);
}
else
/*
*/
Process.Start("powershell.exe", command);
// *** Read the streams ***
// Warning: This approach can lead to deadlocks, see Edit #2
//string output = process.StandardOutput.ReadToEnd();
......@@ -109,15 +93,9 @@ IEnumerator ServerRoutine1()
// Console.WriteLine("ExitCode: " + exitCode.ToString(), "ExecuteCommand");
// process.Close();
yield return null;
}
IEnumerator ServerRoutine()
{
CommunicationEvents.ServerAddressLocal = ServerAddressLocalhost + ":" + ServerPortDefault;
......@@ -153,7 +131,7 @@ IEnumerator ServerRoutine()
#endif
while (true && autocheckIfServerIsRunning != 0)
while (autocheckIfServerIsRunning != 0)
{
//Wait for 2 seconds
yield return new WaitForSecondsRealtime(2f);
......@@ -171,8 +149,6 @@ IEnumerator ServerRoutine()
break;
}
yield return null;
}
......@@ -192,6 +168,4 @@ IEnumerator ServerRoutine()
PrepareGame();
yield return null;
}
}
}
\ No newline at end of file
using System.Collections;
using System.Diagnostics;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.SceneManagement;
using static UIconfig;
using static CommunicationEvents;
public class StartServer_mctrl : MonoBehaviour
{
public GameObject ServerScriptGObj;
// Start is called before the first frame update
void Start()
{
StartCoroutine(ServerRoutine());
......@@ -19,9 +13,11 @@ void Start()
IEnumerator ServerRoutine()
{
//Wait for 1 seconds
yield return new WaitForSecondsRealtime(1f);
if (ServerAutoStart == true)
////Wait for 1 seconds
//yield return new WaitForSecondsRealtime(1f);
if (CommunicationEvents.ServerAutoStart)
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.
Finish editing this message first!
Please register or to comment