/// <summary> /// Converts the room model's data back into parsed LPC. /// </summary> private void PushFunctionDataToCode(FunctionCallsCollection functions) { //first, find main function Parser.Token body = this.Code.GetFunctionBody("create"); if (body == null) { MessageBox.Show("DEBUG ERROR: Item model's code failed to update properly. The 'create' function body was not found."); return; } //assume we have children in the function body if (body.Children.Count < 2) { MessageBox.Show("DEBUG ERROR: Missing function body for 'create'. Parser mismatched token children."); return; } //build a list of all function calls that already exist and just need to be changed Dictionary <string, Parser.Token> list = this.Code.GetAllFunctionCalls(body); List <string> remaining = new List <string>(functions.CallList.Keys); //build list of relevant function calls within body foreach (string codeCall in list.Keys) { foreach (string dataCall in functions.CallList.Keys) { if (dataCall == codeCall.Trim()) { //edit currently existsing data within the parsed LPC object if (list[dataCall].Type == Parser.TokenType.FunctionCall) { //make sure hash symbol is removed before adding string[] temp = dataCall.Split(Globals.Generator.FunctionHashSymbol.ToCharArray()); string functionName = temp[0]; //edit exiting function const int FUNCTION_CALL_PARAMS = 0; list[dataCall].SetTextAsTokens(functions.CallList[functionName], FUNCTION_CALL_PARAMS); } remaining.Remove(dataCall); } } } //now deal with all the remaining function calls from //the model that aren't in the LPC parse map yet. List <string> removedFunctions = new List <string>(); foreach (string dataCall in remaining) { /* * const int FUNCTION_BODY = 1; * //make sure hash symbol is removed before adding * string[] temp = dataCall.Split(Globals.Generator.FunctionHashSymbol.ToCharArray()); * string functionName = temp[0]; * * //add functiuons that don't exist within the parsed LPC * //but do exist within the model * body.InsertTextAsTokensEnd("\t" + functionName + "();\n",FUNCTION_BODY); */ const int FUNCTION_BODY = 1; //make sure hash symbol is removed before adding string parameters = functions.CallList[dataCall]; if (parameters.Length > 0 && parameters != null && parameters != "({})" && parameters != "([])" && parameters != "\"\"") { string[] temp = dataCall.Split(Globals.Generator.FunctionHashSymbol.ToCharArray()); string functionName = temp[0]; //add functiuons that don't exist within the parsed LPC //but do exist within the model body.InsertTextAsTokensEnd("\t" + functionName + "();\n", FUNCTION_BODY); } else { removedFunctions.Add(dataCall); } } //loop through again to fill in the parameters for everything we just created list = this.Code.GetAllFunctionCalls(body); //HACK ALERT!!! //when we entered the functions with the hash symbol they used whatever value was tagged onto the end of them //However, now they are using a number count assigned by the 'GetAllFunctionCalls()' which will inevitably make //the results turn sour. So we need to convert all hashed function values to a numeric system, while //also keeing in mind that the first function with a given name is not given a hash symbol by 'GetAllFunctionCalls()'. //Basically we are going to tack on a numbered count after each instance of a function call except the first. Dictionary <string, int> funcHashCount = new Dictionary <string, int>(); foreach (string dataCall in remaining) { string functionKey = dataCall; if (!removedFunctions.Contains(dataCall)) { //hack alert: see above if (functionKey.Contains(Globals.Generator.FunctionHashSymbol)) { string[] temp = functionKey.Split(Globals.Generator.FunctionHashSymbol.ToCharArray()); functionKey = temp[0]; //don't add hash to first instance of a function call if (!funcHashCount.ContainsKey(functionKey)) { funcHashCount.Add(functionKey, 0); } else { funcHashCount[functionKey]++; functionKey = functionKey + Globals.Generator.FunctionHashSymbol + funcHashCount[functionKey]; } } //end hack alert const int FUNCTION_CALL_PARAMS = 0; if (functionKey != null && functionKey.Length > 0 && list[functionKey].Type == Parser.TokenType.FunctionCall) { //make sure hash symbol is removed before adding string[] temp = functionKey.Split(Globals.Generator.FunctionHashSymbol.ToCharArray()); string functionName = temp[0]; list[functionKey].SetTextAsTokens(functions.CallList[dataCall], FUNCTION_CALL_PARAMS); } } //end if not in removed } foreach (string file in functions.AdditionalIncludes) { this.Code.AddInclude(file); } return; }
/// <summary> /// Converts the room model's data back into parsed LPC. /// </summary> private void PushDataToCode() { //first, find main function Parser.Token body = this.Code.GetFunctionBody("create"); if (body == null) { MessageBox.Show("DEBUG ERROR: Room model's code failed to update properly. The 'create' function body was not found."); return; } //assume we have children in the function body if (body.Children.Count < 2) { MessageBox.Show("DEBUG ERROR: Missing function body for 'create'. Parser mismatched token children."); return; } //build a list of all function calls that already exist and just need to be changed Dictionary <string, Parser.Token> list = this.Code.GetAllFunctionCalls(body); List <string> remaining = new List <string>(this.FunctionCalls.CallList.Keys); //build list of relevant function calls within body foreach (string codeCall in list.Keys) { foreach (string dataCall in this.FunctionCalls.CallList.Keys) { if (dataCall == null || dataCall.Length < 1) { MessageBox.Show("Warning! A function with no name was produced for '" + this.RawFileName + "'. The flawed data has not been written but you you should verify the contents of the file."); } else { if (dataCall == codeCall.Trim()) { //edit currently existsing data within the parsed LPC object const int FUNCTION_CALL_PARAMS = 0; string parameters = this.FunctionCalls.CallList[dataCall]; if (parameters.Length < 1 || parameters == "({})" || parameters == "([])" || parameters == "\"\"") { //don't remove if it's the create function if (dataCall == "create") { list[dataCall].SetTextAsTokens(parameters, FUNCTION_CALL_PARAMS); remaining.Remove(dataCall); } else { RemoveFunctionCallFromCode(dataCall, body); } } else { string encodedText = parameters; list[dataCall].SetTextAsTokens(encodedText, FUNCTION_CALL_PARAMS); remaining.Remove(dataCall); } } } } } //now deal with all the remaining function calls from //the model that aren't in the LPC parse map yet. List <string> removedFunctions = new List <string>(); foreach (string dataCall in remaining) { const int FUNCTION_BODY = 1; string parameters = this.FunctionCalls.CallList[dataCall]; //add functiuons that don't exist within the parsed LPC //but do exist within the model. But only add them it the //the parameters aren't empty if (parameters.Length > 0 && parameters != null && parameters != "({})" && parameters != "([])" && parameters != "\"\"") { body.InsertTextAsTokensEnd("\t" + dataCall + "();\n", FUNCTION_BODY); } else { removedFunctions.Add(dataCall); } } //loop through again to fill in the parameters for everything we just created //if the parameters are empty remove that function call from the code list = this.Code.GetAllFunctionCalls(body); foreach (string dataCall in remaining) { if (dataCall == null || dataCall.Length < 1) { MessageBox.Show("Warning! A function with no name was produced for '" + this.RawFileName + "'. The flawed data has not been written but you you should verify the contents of the file."); } else { if (!removedFunctions.Contains(dataCall)) { const int FUNCTION_CALL_PARAMS = 0; string parameters = this.FunctionCalls.CallList[dataCall]; list[dataCall].SetTextAsTokens(parameters, FUNCTION_CALL_PARAMS); } } } foreach (string file in FunctionCalls.AdditionalIncludes) { this.Code.AddInclude(file); } return; }