private bool CanParse(string token, ScriptSentenceContext scriptSentenceContext) { bool canParse = true; BaseInterpreterNode node = null; if (token.Equals("Clear")) { node = new TextClearNode(); } else if (token.Equals("Change")) { node = new TextChangeNode(); } else { canParse = false; } if (canParse) { nodeList.Add(node); node.Interpret(scriptSentenceContext); } else { Debug.Log("CommandTextNode FALSE canParse! token :" + token); } return(canParse); }
public override void Interpret(ScriptSentenceContext context) { string commandToken = context.CurrentToken; if (!CanParse(commandToken, context)) { throw new System.Exception("ERROR IN CommandNode! NOT DEFINDED!" + commandToken); } }
private bool CanParse(string token, ScriptSentenceContext scriptSentenceContext) { bool canParse = true; BaseInterpreterNode node = null; if (token.Equals("BackgroundImageChange")) { node = new ImageBackgroundImageChangeNode(); } else if (token.Equals("BackgroundImageClear")) { node = new ImageBackgroundImageClearNode(); } else if (token.Equals("ConsoleShow")) { node = new ImageConsoleShowNode(); } else if (token.Equals("ConsoleHide")) { node = new ImageConsoleHideNode(); } else if (token.Equals("SmallFigureImageChange")) { node = new ImageSmallFigureImageChangeNode(); } else if (token.Equals("SmallFigureImageClear")) { node = new ImageSmallFigureImageClearNode(); } else if (token.Equals("FigureImageAdd")) { node = new ImageFigureImageAddNode(); } else if (token.Equals("FigureImageRemove")) { node = new ImageFigureImageRemoveNode(); } else { canParse = false; } if (canParse) { nodeList.Add(node); node.Interpret(scriptSentenceContext); } else { Debug.Log("CommandImageNode FALSE canParse! token :" + token); } return(canParse); }
public override void Interpret(ScriptSentenceContext context) { while (true) { if (context.CurrentToken == null) { break; } BaseInterpreterNode node = new CommandNode(); node.Interpret(context); nodeList.Add(node); } }
public override void Interpret(ScriptSentenceContext context) { context.SkipToken("Engine"); while (true) { string funcToken = context.CurrentToken; if (funcToken == null) { break; } if (!CanParse(funcToken, context)) { break; } } }
public void InterpretPart(ScriptSentenceContext context) { context.SkipToken("("); while (true) { if (context.CurrentToken.Equals(")")) { context.NextToken(); if (context.CurrentToken != null) { throw new System.Exception("你咋害有!?"); } break; } string currentToken = context.CurrentToken; paraList.Add(currentToken); context.NextToken(); } }
private bool NextSecondSentence() { if (IsSecondOver()) { UnloadSecondSentence(); return(false); } ScriptSentenceContext context = secondScriptSentenceList[secondScriptPointerLineNumber]; secondScriptPointerLineNumber = secondScriptPointerLineNumber + 1; ExpressionRootNode rootNode = new ExpressionRootNode(); Debug.Log("从翻译器 开始翻译 :"); context.ShowSelfAll(); Debug.Log("从翻译器 开始翻译结束"); rootNode.Interpret(context); Debug.Log("从翻译器 开始执行"); rootNode.Execute(); Debug.Log("从翻译器 开始执行结束"); return(true); }
private bool CanParse(string token, ScriptSentenceContext scriptSentenceContext) { bool canParse = true; BaseInterpreterNode node = null; if (token.Equals("VoicePlay")) { node = new MusicVoicePlayNode(); } else if (token.Equals("VoiceStop")) { node = new MusicVoiceStopNode(); } else if (token.Equals("BGMPlay")) { node = new MusicBGMPlayNode(); } else if (token.Equals("BGMPlay")) { node = new MusicBGMStopNode(); } else { canParse = false; } if (canParse) { nodeList.Add(node); node.Interpret(scriptSentenceContext); } else { Debug.Log("CommandMusicNode FALSE canParse! token :" + token); } return(canParse); }
private bool NextFirstSentence() { if (IsOver()) { if (pointerScriptNameStack.Count == 0) { return(false); } else { // 恢复脚本上下文 ScriptPointerScriptName = pointerScriptNameStack.Pop(); ScriptPointerLineNumber = pointerLineNumberStack.Pop(); string scriptIndex = PachiGrimoire.I.constData.ScriptIndexPrefix + "_" + ScriptPointerScriptName; string scriptContext = PachiGrimoire.I.ResourceManager.Get <string>(scriptIndex); scriptSentenceList = ProcessScriptContext(scriptContext); } } // 得到当前语句 ScriptSentenceContext context = scriptSentenceList[ScriptPointerLineNumber]; // 更新已读文本 pastScriptManager.UpdatePastScript(ScriptPointerScriptName, ScriptPointerLineNumber); // 更新指针 ScriptPointerLineNumber = ScriptPointerLineNumber + 1; // 开始翻译 ExpressionRootNode rootNode = new ExpressionRootNode(); Debug.Log("主翻译器 开始翻译 :"); context.ShowSelfAll(); Debug.Log("主翻译器 开始翻译结束"); rootNode.Interpret(context); Debug.Log("主翻译器 开始执行"); rootNode.Execute(); Debug.Log("主翻译器 开始执行结束"); return(true); }
private bool CanParse(string commandToken, ScriptSentenceContext context) { bool canParse = true; if (commandToken.Equals("Engine")) { node = new CommandEngineNode(); } else if (commandToken.Equals("Music")) { node = new CommandMusicNode(); } else if (commandToken.Equals("Text")) { node = new CommandTextNode(); } else if (commandToken.Equals("Image")) { node = new CommandImageNode(); } else { canParse = false; } if (canParse) { Debug.Log("CommandNode CanParse commandToken: " + commandToken); node.Interpret(context); } else { Debug.Log("CommandNode FALSE canParse! token :" + commandToken); } return(canParse); }
public override void Interpret(ScriptSentenceContext context) { context.SkipToken("MarkPlayerSet"); InterpretPart(context); }
public override void Interpret(ScriptSentenceContext context) { context.SkipToken("SystemChangeStateToWait"); InterpretPart(context); }
public override void Interpret(ScriptSentenceContext context) { context.SkipToken("BackgroundImageChange"); InterpretPart(context); }
private List <ScriptSentenceContext> ProcessScriptContext(string scriptContext) { if (scriptContext == null || scriptContext.Length == 0) { throw new System.Exception("传了个啥?"); } Debug.Log("Process Script Context."); List <ScriptSentenceContext> sentenceList = new List <ScriptSentenceContext>(); // 1. "\r\n" -> "\n" scriptContext.Replace("\r\n", "\n"); scriptContext.Replace("\r", ""); int lastIndex = scriptContext.Length - 1; // 给最后补上'\n' if (scriptContext[lastIndex] != '\n') { scriptContext = scriptContext + "\n"; } // 2. <>和**的绝对存在 charStack.Clear(); List <string> tempList = new List <string>(); // 宅出去<>**剩下的字符串 List <bool> tempIsCompleteList = new List <bool>(); // 标记<>**字符串 int lastTailIndex = -1; int leftIndex = -1; int i; for (i = 0; i < scriptContext.Length; i++) { char ch = scriptContext[i]; switch (ch) { case '<': if (charStack.Count == 0) { charStack.Push('<'); leftIndex = i; if (i != lastTailIndex + 1) { string str = scriptContext.Substring(lastTailIndex + 1, i - lastTailIndex - 1); tempList.Add(str); tempIsCompleteList.Add(false); } } else if (charStack.Peek() == '<') { charStack.Push('<'); } else if (charStack.Peek() == '*') { // pass; } else { throw new System.Exception($"语法有问题,Stack在<前遇到了 {charStack.Peek()}"); } break; case '>': if (charStack.Count == 0) { throw new System.Exception("语法有问题,遇到了 >"); } else if (charStack.Peek() == '<') { charStack.Pop(); if (charStack.Count == 0) // <>收束 { lastTailIndex = i; string str = scriptContext.Substring(leftIndex + 1, i - leftIndex - 1); tempList.Add(str); tempIsCompleteList.Add(true); } } else if (charStack.Peek() == '*') { // pass } else { throw new System.Exception($"语法有问题,Stack在>前遇到了 {charStack.Peek()}"); } break; case '*': if (charStack.Count == 0) { charStack.Push('*'); leftIndex = i; if (i != lastTailIndex + 1) { string str = scriptContext.Substring(lastTailIndex + 1, i - lastTailIndex - 1); tempList.Add(str); tempIsCompleteList.Add(false); } } else if (charStack.Peek() == '*') { if (charStack.Count != 1) { throw new System.Exception($"语法有问题,好多*"); } // **收束 charStack.Pop(); lastTailIndex = i; string str = scriptContext.Substring(leftIndex + 1, i - leftIndex - 1); tempList.Add(str); tempIsCompleteList.Add(true); } else if (charStack.Peek() == '<') { // pass } else { throw new System.Exception($"语法有问题,Stack在*前遇到了 {charStack.Peek()}"); } break; default: break; } } if (charStack.Count != 0) { throw new System.Exception("Stack isn't clear"); } int lastStrLength = i - lastTailIndex - 1; // 把最后一段字符串加上 if (lastStrLength > 0) { tempList.Add(scriptContext.Substring(lastTailIndex + 1, lastStrLength)); tempIsCompleteList.Add(false); } // 3. ScriptReplace宏替换 for (i = 0; i < tempList.Count; i++) { bool isComplete = tempIsCompleteList[i]; if (isComplete) { continue; } // 宏替换 string str = tempList[i]; for (int j = ScriptReplaceKeys.Count - 1; j >= 0; j--) { string key = ScriptReplaceKeys[j]; if (str.Contains(key)) { string value = ScriptReplaceValues[j]; str = str.Replace(key, value); } } // 4. 空格杠二消除 str = str.Replace(" ", ""); str = str.Replace("\r", ""); tempList[i] = str; } // 5. '\n'脚本割解 // 6. 全分割,得到ScriptSentenceContext List <string> fragmentList = new List <string>(); for (i = 0; i < tempList.Count; i++) { string str = tempList[i]; bool isComplete = tempIsCompleteList[i]; if (isComplete) { fragmentList.Add(str); continue; } // 处理str,并在其中找'\n' int j; lastTailIndex = -1; int length = -1; string smallStr; for (j = 0; j < str.Length; j++) { char ch = str[j]; switch (ch) { case '\r': throw new System.Exception("有内鬼!"); case '/': // 实现注释功能 length = j - lastTailIndex - 1; int strLength = str.Length; if (length == 0 && j + 1 < strLength && str[j + 1] == '/') // 第一个就是/后边又接了一个/ // 到\n都扔 { int findHuicheIndex = j + 2; while (findHuicheIndex < strLength && str[findHuicheIndex] != '\n') { findHuicheIndex++; } if (findHuicheIndex == strLength) { throw new System.Exception("有注释,但没找到回车!实现注释功能"); } else // 跳过注释所在行 { Debug.Log("注释跳了! 内容 :" + str.Substring(j, findHuicheIndex - j + 1)); lastTailIndex = findHuicheIndex; j = findHuicheIndex; } } break; case '\n': length = j - lastTailIndex - 1; if (length != 0) { Debug.LogWarning("Error, str:" + str); Debug.LogWarning("Error, fragmentList:"); foreach (var s in fragmentList) { Debug.Log(s); } Debug.LogWarning("ErrorEnd, fragmentList."); throw new System.Exception("语法出错,\n前一定是),但事实不是!"); } lastTailIndex = j; ScriptSentenceContext scriptSentenceContext = new ScriptSentenceContext(fragmentList.ToArray()); if (scriptSentenceContext.IsCorrect == false) // 解析错误防止功能 { throw new System.Exception("ScriptSentence is not right!"); } string currentToken = scriptSentenceContext.CurrentToken; if (currentToken != null && !currentToken.Substring(0, 2).Equals("//")) // 跳过空行功能以及跳过注释辅助功能 { sentenceList.Add(scriptSentenceContext); } else { Debug.Log("放弃ScriptSentenceContext currentToken :" + currentToken); } fragmentList.Clear(); break; case '_': length = j - lastTailIndex - 1; if (length > 0) { smallStr = str.Substring(lastTailIndex + 1, length); fragmentList.Add(smallStr); } lastTailIndex = j; break; case '(': length = j - lastTailIndex - 1; if (length > 0) { smallStr = str.Substring(lastTailIndex + 1, length); fragmentList.Add(smallStr); } fragmentList.Add("("); lastTailIndex = j; break; case ')': length = j - lastTailIndex - 1; if (length > 0) { smallStr = str.Substring(lastTailIndex + 1, length); fragmentList.Add(smallStr); } fragmentList.Add(")"); lastTailIndex = j; break; case ',': length = j - lastTailIndex - 1; if (length > 0) { smallStr = str.Substring(lastTailIndex + 1, length); fragmentList.Add(smallStr); } lastTailIndex = j; break; default: break; } } } if (fragmentList.Count != 0) { foreach (string s in fragmentList) { Debug.LogWarning(s); } throw new System.Exception("怎么还有剩余的?"); } return(sentenceList); }
public override void Interpret(ScriptSentenceContext context) { context.SkipToken("SystemDebugLog"); InterpretPart(context); }
public override void Interpret(ScriptSentenceContext context) { context.SkipToken("FigureImageRemove"); InterpretPart(context); }
private bool CanParse(string token, ScriptSentenceContext scriptSentenceContext) { bool canParse = true; BaseInterpreterNode node = null; if (token.Equals("ScriptReplace")) { node = new EngineScriptReplaceAddNode(); } else if (token.Equals("ScriptIfThenElse")) { node = new EngineScriptIfThenElseNode(); } else if (token.Equals("ScriptLoadFile")) { node = new EngineScriptLoadFileNode(); } else if (token.Equals("BacklogAdd")) { node = new EngineBacklogAddNode(); } else if (token.Equals("SystemChangeStateToAnimate")) { node = new EngineSystemChangeStateToAnimate(); } else if (token.Equals("SystemChangeStateToWait")) { node = new EngineSystemChangeStateToWait(); } else if (token.Equals("MarkPlayerGet")) { node = new EngineMarkPlayerGetNode(); } else if (token.Equals("MarkPlayerSet")) { node = new EngineMarkPlayerSetNode(); } else if (token.Equals("MarkStoryGet")) { node = new EngineMarkStoryGetNode(); } else if (token.Equals("MarkStorySet")) { node = new EngineMarkStorySetNode(); } else if (token.Equals("ValuePlayerGet")) { node = new EngineValuePlayerGetNode(); } else if (token.Equals("ValuePlayerSet")) { node = new EngineValuePlayerSetNode(); } else if (token.Equals("ValueStoryGet")) { node = new EngineValueStoryGetNode(); } else if (token.Equals("ValueStorySet")) { node = new EngineValueStorySetNode(); } else if (token.Equals("ChoiceCreate")) { node = new EngineChoiceCreateNode(); } else if (token.Equals("SystemDebugLog")) { node = new EngineSystemDebugLogNode(); } else if (token.Equals("ChapterNameSet")) { node = new EngineChapterNameSetNode(); } else { canParse = false; } if (canParse) { nodeList.Add(node); node.Interpret(scriptSentenceContext); } else { Debug.Log("CommandEngineNode FALSE canParse! token :" + token); } return(canParse); }
public override void Interpret(ScriptSentenceContext context) { context.SkipToken("BacklogAdd"); InterpretPart(context); }
public override void Interpret(ScriptSentenceContext context) { context.SkipToken("ScriptIfThenElse"); InterpretPart(context); }
public override void Interpret(ScriptSentenceContext context) { context.SkipToken("ScriptLoadFile"); InterpretPart(context); }
public override void Interpret(ScriptSentenceContext context) { context.SkipToken("ChapterNameSet"); InterpretPart(context); }
public override void Interpret(ScriptSentenceContext context) { context.SkipToken("VoiceStop"); InterpretPart(context); }
public override void Interpret(ScriptSentenceContext context) { context.SkipToken("ChoiceCreate"); InterpretPart(context); }
public override void Interpret(ScriptSentenceContext context) { context.SkipToken("SmallFigureImageClear"); InterpretPart(context); }
public override void Interpret(ScriptSentenceContext context) { context.SkipToken("ConsoleShow"); InterpretPart(context); }
public abstract void Interpret(ScriptSentenceContext context);
public override void Interpret(ScriptSentenceContext context) { context.SkipToken("ValueStorySet"); InterpretPart(context); }