void LoadValueScopes(SubstringOptimizedString subcode, ICompilerValues currentCode) { int currentSubValue = 0; var scopeBeginIndex = 0; while (scopeBeginIndex != -1) { if (currentSubValue != 0) { scopeBeginIndex = subcode.Length > 0 && subcode[0] == CodeValueBegin ? 0 : -1; } if (scopeBeginIndex != -1) { var scopeEndIndex = -1; var scopeAmount = 1; for (int i = scopeBeginIndex + 1; i < subcode.Length; i++) { if (subcode[i] == CodeValueBegin) { scopeAmount++; } else if (subcode[i] == CodeValueEnd) { scopeAmount--; if (scopeAmount == 0) { scopeEndIndex = i; break; } } } var scopeCode = subcode.Substring(scopeBeginIndex + 1, scopeEndIndex - scopeBeginIndex - 1); //print(scopeCode); if (scopeCode.Length > 0) { LoadValues(scopeCode, currentCode, currentSubValue); } subcode = subcode.Substring(scopeEndIndex + 1); currentSubValue++; } else { break; } } }
void LoadValues(SubstringOptimizedString subcode, ICompilerValues currentCode, int value = 0) { var valueIndex = -1; var scopeIndex = subcode.Length; // Type:&"Text"(sub values) // This reads until ( for (int i = 0; i < subcode.Length; i++) { if (subcode[i] == CodeValueQuote) { valueIndex = i; } else if (subcode[i] == CodeValueBegin) { scopeIndex = i; break; } } var valueName = valueIndex == -1 ? subcode.Substring(0, scopeIndex) : subcode.Substring(0, valueIndex); var valueParent = currentCode.Transform.Find("Block") .Find($"Value{(value == 0 ? "" : (value + 1).ToString())}"); var valueObject = Instantiate(CodeIdPrefabs[valueName.ToString()], valueParent); var currentValue = valueObject.GetComponent <ICompilerValue>(); currentCode.Values[value] = currentValue; var loadedValue = valueIndex == -1 ? "" : subcode.Substring(valueIndex + 1, scopeIndex - valueIndex - 1).ToString(); currentValue.LoadValue(loadedValue); // Event Offset var offset = valueObject.gameObject.AddComponent <ValueOffset>(); (currentValue as IUseOffset).Offset = offset; offset.Connection = valueParent; if (scopeIndex < subcode.Length) { var newCode = subcode.Substring(scopeIndex); LoadValueScopes(newCode, currentValue); } }
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; } }
// Subcode and string index end int LoadFunctionScopes(SubstringOptimizedString subcode, ICompilerCode currentFunction, SelectableObject obj, int valueEndIndex) { //print(subcode); // CodeConnections: scopeIndex + 1 int scopeIndex = 0; var totalEndIndex = -1; var scopeBeginIndex = subcode.Length > valueEndIndex + 1 ? (subcode[valueEndIndex + 1] == CodeScopeBegin ? valueEndIndex + 1 : -1) : -1; while (scopeBeginIndex != -1) { if (scopeIndex != 0) { scopeBeginIndex = subcode.Length > 0 ? (subcode[0] == CodeScopeBegin ? 0 : -1) : -1; } if (scopeBeginIndex != -1) { var scopeEndIndex = -1; var scopeAmount = 1; for (int i = scopeBeginIndex + 1; i < subcode.Length; i++) { if (subcode[i] == CodeScopeBegin) { scopeAmount++; } else if (subcode[i] == CodeScopeEnd) { scopeAmount--; if (scopeAmount == 0) { scopeEndIndex = i; break; } } } var scopeCode = subcode.Substring(scopeBeginIndex + 1, scopeEndIndex - scopeBeginIndex - 1); //print(scopeCode); LoadFunctions(scopeCode, null, obj, currentFunction, scopeIndex + 1); totalEndIndex += scopeEndIndex; subcode = subcode.Substring(scopeEndIndex + 1); scopeIndex++; } else { break; } } if (totalEndIndex != -1) { return(totalEndIndex + scopeIndex - 1); } return(-1); }
// 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; } }