/// <summary> /// Get poses defined underneath <POSES> inside the .dsl file. /// </summary> /// <param name="_position">Where to start collecting data.</param> static void GetPoses(int _position) { string dsPath = Application.streamingAssetsPath + @"/" + DialogueSystem.GET_DIALOGUE_SCRIPTING_FILE(); string line = null; bool atTargetLine = false; if (File.Exists(dsPath)) { using (StreamReader fileReader = new StreamReader(dsPath)) { int position = 0; while (true) { line = fileReader.ReadLine(); if (atTargetLine) { if (line == STRINGNULL) { return; } } if (position > _position) { atTargetLine = true; if (line != STRINGNULL) { string[] data = line.Split('='); DefinedPoses.Add(data[0].Replace(WHITESPACE, STRINGNULL), Convert.ToInt32(data[1].Replace(WHITESPACE, STRINGNULL))); } } position++; } } } }
/// <summary> /// Get Character names defined underneath <CHARACTERS> inside the .dsl file /// </summary> /// <param name="_position">Where to start collecting data.</param> static void GetCharacterNames(int _position) { string dsPath = Application.streamingAssetsPath + @"/" + DialogueSystem.GET_DIALOGUE_SCRIPTING_FILE(); string line = null; bool atTargetLine = false; if (File.Exists(dsPath)) { using (StreamReader fileReader = new StreamReader(dsPath)) { int position = 0; while (true) { line = fileReader.ReadLine(); if (atTargetLine) { if (line == STRINGNULL) { return; } } if (position > _position) { atTargetLine = true; if (line != STRINGNULL) { DefinedCharacters.Add(line); } } position++; } } } }
/// <summary> /// Define characters listed underneath <CHARACTERS> in the .dsl file /// </summary> public static void Define_Characters() { string dsPath = Application.streamingAssetsPath + @"/" + DialogueSystem.GET_DIALOGUE_SCRIPTING_FILE(); string line = null; int position = 0; bool foundPose = false; if (File.Exists(dsPath)) { using (StreamReader fileReader = new StreamReader(dsPath)) { while (true) { line = fileReader.ReadLine(); if (line == STRINGNULL) { if (foundPose) { return; } } line.Split(Delimiters); if (HAS(line, "<CHARACTERS>")) { foundPose = true; try { GetCharacterNames(position); } catch { } } position++; } } } Debug.LogError("File specified doesn't exist. Try creating one in StreamingAssets folder."); }
/// <summary> /// Get all dialogue in a specified Dialogue_Set inside a .dsl file /// </summary> /// <param name="_position">Position to start collecting data</param> public static void GetDialogue(int _position) { //Access the DSL Path string dsPath = Application.streamingAssetsPath + @"/" + DialogueSystem.GET_DIALOGUE_SCRIPTING_FILE(); //This is used to read a line in the file when iterating string line = null; //Toggle if we are at a desired position in the file. bool atTargetLine = false; //If the defined path exist, interate through the file if (File.Exists(dsPath)) { //Stream reader allows use to read our file without compromising too much performance. using (StreamReader fileReader = new StreamReader(dsPath)) { //We use this to keep track of our position in the file int position = 0; //Loop until end of file while (true) { //Read the current line line = fileReader.ReadLine(); //If we reach the end of the dialogue set, we are done reading it if (line == "<END>" && atTargetLine) { return; } //However, if we are at the DialogueSet tag with a specified number, we collect all the dialogue that starts with "@" if (position > _position) { atTargetLine = true; //Toggle on //Make sure that we are specifically encountering "<...>" if (line != STRINGNULL && (line[0].ToString() == Tokens[3] && line[line.Length - 1] == '<')) { //We should have a list of defined characters with the DefinedCharacters() function. This is used to validate that that character exists if (DefinedCharacters.Count != 0) { //We iterate through our DefinedCharacters foreach (string character in DefinedCharacters) { string name = STRINGNULL; //We make an attempt to get the whole name after "@", then we check try { name = line.Substring(1, character.Length) + ":"; } catch { } //If this character exist in the list of characters defined, we do some string manipulation if (HAS(name, character)) { //For names with _ scores replacing as spaces name = name.Replace("_", WHITESPACE); //Insert the name that's been defined using the Insert command line = line.Replace(Tokens[0], STRINGNULL).Replace(Tokens[3] + character, "[INSERT::\"" + name + "\"]"); } //If it has ???, a predefined token that a character's name is not known, we insert it. else if (HAS(name.Substring(0, Tokens[4].Length), Tokens[4])) { line = line.Replace(Tokens[0], STRINGNULL).Replace(Tokens[3] + Tokens[4], "[INSERT::\"" + Tokens[4] + ":" + "\"]"); } //If there's no character or no ??? token, this means the narrator is speaking. else if (HAS(name.Substring(0, WHITESPACE.Length), WHITESPACE)) { line = line.Replace(Tokens[0], STRINGNULL).Replace(Tokens[3] + WHITESPACE, STRINGNULL); } //Then we really check if our defined character exist. If we down, we throw an exception, and end the dialogue reading process. else if (!DefinedCharacters.Exists(x => HAS(x, line.Substring(1, line.IndexOf(WHITESPACE) - 1)))) { string unidentifiedName = line.Substring(1, line.IndexOf(WHITESPACE) - 1); throw new UnknownCharacterDefinedException("Unknown character definition at line " + (position + 1) + ". Did you define \"" + unidentifiedName + "\" under <CHARACTERS>?"); } } } else { line = line.Replace(Tokens[0], STRINGNULL).Replace(Tokens[3] + WHITESPACE, STRINGNULL); } DialogueSystem.Dialogue.Add(line); } } position++; } } } }