void LoadFunctions(SubstringOptimizedString subcode, ICompilerEvent currentEvent, SelectableObject obj, ICompilerCode currentCode = null, int codeId = 0) { var functionScope = subcode.IndexOf(CodeValueBegin); ICompilerCode lastFunctionObject = null; while (functionScope != -1) { var valueScopeAmount = 1; var valueEndIndex = -1; for (int i = functionScope + 1; i < subcode.Length; i++) { if (subcode[i] == CodeValueBegin) { valueScopeAmount++; } else if (subcode[i] == CodeValueEnd) { valueScopeAmount--; if (valueScopeAmount == 0) { if (subcode.Length > i + 1 && subcode[i + 1] == CodeValueBegin) { // There is still another () // Eg: (Val1)(Val2) } else // if it is the end { valueEndIndex = i; break; } } } } // Check if code contains equal amount of {} if (valueEndIndex == -1) { Debug.LogWarning($"{currentEvent as MonoBehaviour} code was corrupted."); return; } // Create Function var functionName = subcode.Substring(0, functionScope).ToString(); //print(functionName); // Broadcast function is only (currently) function with no values, but InputField var functionValueSplit = functionName.IndexOf(FunctionSplit); string functionValue = ""; if (functionValueSplit != -1) { functionValue = functionName.Substring(functionValueSplit + 1); functionName = functionName.Substring(0, functionValueSplit); } #if UNITY_EDITOR if (!CodeIdPrefabs.ContainsKey(functionName)) { Debug.LogError($"Doesn't Contain {functionName}"); } #endif var functionObject = Instantiate(CodeIdPrefabs[functionName], obj.Script.transform); var currentFunction = functionObject.GetComponent <ICompilerCode>(); if (functionValueSplit != -1) { if (currentFunction is B_Broadcast bb) { bb.broadcastName.text = functionValue; } else if (currentFunction is B_Comment cc) { cc.inputField.text = functionValue; } } // Event Offset var offset = functionObject.gameObject.AddComponent <BlockOffset>(); currentFunction.Offset = offset; if (lastFunctionObject != null) { lastFunctionObject.CodeConnections[0] = currentFunction; offset.Connection = (lastFunctionObject as MonoBehaviour).transform.Find("CodeConnection"); } else { if (currentEvent != null) { currentEvent.CodeConnections[0] = currentFunction; offset.Connection = (currentEvent as MonoBehaviour).transform.Find("Scope") .Find("CodeConnection"); } else { currentCode.CodeConnections[codeId] = currentFunction; if (codeId == 1) { offset.Connection = (currentCode as MonoBehaviour).transform.Find("Scope") .Find("CodeConnection"); } else { offset.Connection = (currentCode as MonoBehaviour).transform.Find($"Scope{codeId}") .Find("CodeConnection"); } } } offset.Connect(); // Todo: Implement () value creation (LoadValues) var valueString = subcode.Substring(functionScope /*+ 1*/, valueEndIndex - functionScope + 1 /*- 1*/); if (valueString.Length > 0) { LoadValueScopes(valueString, currentFunction); } // Todo: Implement {}{} function searches (if, if else) (recursion) // Function Scopes var innerScope = LoadFunctionScopes(subcode, currentFunction, obj, valueEndIndex); // Connect innerScope.Variable1? to mainfunction // Next Loop: if (innerScope != -1) { subcode = subcode.Substring(innerScope + 2); } else { subcode = subcode.Substring(valueEndIndex + 1); } // Next Loop: functionScope = subcode.IndexOf(CodeValueBegin); lastFunctionObject = currentFunction; } }
// Load Events public void LoadCode(SelectableObject obj, SubstringOptimizedString code) { var eventScope = code.IndexOf(CodeScopeBegin); ICompilerEvent lastEventObject = null; // CREATE CODE IF THERE IS CODE if (eventScope != -1) { //print(code); Editor.OpenScript(obj); Editor.CloseEditor(); lastEventObject = obj.Script.beginEvent; } while (eventScope != -1) { // Find where event {} ends var scopeAmount = 1; var eventScopeEnd = -1; for (int i = eventScope + 1; i < code.Length; i++) { if (code[i] == CodeScopeBegin) { scopeAmount++; } else if (code[i] == CodeScopeEnd) { scopeAmount--; if (scopeAmount == 0) { eventScopeEnd = i; break; } } } // Check if code contains equal amount of {} if (eventScopeEnd == -1) { Debug.LogWarning($"{obj} code was corrupted."); return; } // Create Event var eventName = code.Substring(0, eventScope); // Is it a Variable Event var varSplit = eventName.IndexOf(VariableSplit); if (varSplit != -1) { eventName = eventName.Substring(0, varSplit); } var eventObject = Instantiate(CodeIdPrefabs[eventName.ToString()], obj.Script.transform); var currentEvent = eventObject.GetComponent <ICompilerEvent>(); // Event has custom saving if (varSplit != -1) { var str = eventName.ToString(); if (str == "VarEvent") { B_VariableEvent variable = currentEvent as B_VariableEvent; variable.variableName.text = code.Substring(varSplit + 1, eventScope - varSplit - 1).ToString(); } else if (str == "Receive") { B_Received variable = currentEvent as B_Received; variable.broadcastName.text = code.Substring(varSplit + 1, eventScope - varSplit - 1).ToString(); } } // Event Offset var offset = eventObject.gameObject.AddComponent <BlockOffset>(); currentEvent.Offset = offset; offset.Connection = (lastEventObject as MonoBehaviour).transform.Find("EventConnection"); offset.Connect(); //offset.Connect(); // Connect event to previous event lastEventObject.NextConnection = currentEvent; // FUNCTIONS INSIDE OF CODE var subcode = code.Substring(eventScope + 1, eventScopeEnd - eventScope - 1); LoadFunctions(subcode, currentEvent, obj); // Update Size //currentEvent.UpdateSize(0); // Next Loop: code = code.Substring(eventScopeEnd + 1); eventScope = code.IndexOf(CodeScopeBegin); lastEventObject = currentEvent; } }