private ScriptData ParseTokenToScript(string str) { if (string.IsNullOrEmpty(str)) { return(null); } JToken token = null; try { token = JToken.Parse(str); } catch (Exception e) { waywardManager.Log($@"<red>ERROR: Failed Parsing string:'{str}' into JSON token:</red> {e}"); } if (token == null) { waywardManager.Log($@"<red>ERROR: Failed Parsing string:'{str}' into JSON token</red>"); } ScriptData data = null; try { data = (ScriptData)token.ToObject(typeof(ScriptData)); UpdateScriptMemory(data); } catch (Exception e) { waywardManager.Log($@"<red>ERROR: Failed parsing JSON to script:</red> {e}"); } return(data); }
public ScriptData GetScript(string str) { ScriptData data = null; if (IsId(str)) { data = RetrieveScriptFromMemory(str); if (data != null) { return(data); } data = GetScriptFromFile(str); } else { try { data = ParseTokenToScript(str); } catch (Exception e) { waywardManager.Log($@"<red>ERROR: Failed parsing '{str}' into script: {e}</red>"); return(null); } } if (data == null) { waywardManager.Log($@"<red>ERROR: Failed retrieving script data from '{str}'</red>"); return(null); } return(data); }
private void UpdateScriptMemory(ScriptData data) { if (data.id != string.Empty) { scriptDataMemory.Insert(0, (ScriptData)Activator.CreateInstance(typeof(ScriptData), data)); if (scriptDataMemory.Count > memoryLength) { scriptDataMemory.RemoveAt(scriptDataMemory.Count - 1); } } }
private ScriptData RetrieveScriptFromMemory(string id) { foreach (ScriptData data in scriptDataMemory) { if (data.id == id) { ScriptData copiedData = (ScriptData)Activator.CreateInstance(typeof(ScriptData), data); return(copiedData); } } return(null); }
private ScriptData GetScriptFromFile(string id) { if (scriptFiles == null || !scriptFiles.ContainsKey(id)) { waywardManager.Log($@"<red>ERROR: Script with id '{id}' not found</red>"); } DataPointer pointer = scriptFiles[id]; StreamReader file = new StreamReader(pointer.filePath); ScriptData data = new ScriptData(); try { data.code = file.ReadToEnd(); data.id = id; } catch (Exception e) { waywardManager.Log($@"<red>ERROR: Failed retrieving script from '{pointer.filePath}': {e}</red>"); return(null); } UpdateScriptMemory(data); return(data); }
public ScriptData(ScriptData data) { id = data.id; code = data.code; }
public string GetCode(string methodName = "") { ScriptData data = DataManager.instance.GetScript(text); return(data.code); }