Skip to content

Commit

Permalink
fix scenario
Browse files Browse the repository at this point in the history
  • Loading branch information
khjxiaogu committed Dec 17, 2024
1 parent b925f5a commit d505335
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.teammoeg.frostedheart.content.scenario;

public class CommandNotFoundException extends ScenarioExecutionException {

public CommandNotFoundException() {
}

public CommandNotFoundException(String message) {
super(message);
}

public CommandNotFoundException(String message, Throwable cause) {
super(message, cause);
}

public CommandNotFoundException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}

public CommandNotFoundException(Throwable cause) {
super(cause);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ public <V> void addTypeAdapter(Class<? super V> cls,TypeAdapter<V,T> conv) {
public void callCommand(String name, T scenarioVM, Map<String, String> params) {
ScenarioMethod<T> command = commands.get(name);
if (command == null) {
throw new ScenarioExecutionException("Can not find command " + name);
throw new CommandNotFoundException("Can not find command " + name);
}
command.execute(scenarioVM, params);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,18 @@

import java.util.Map;

import com.teammoeg.frostedheart.content.scenario.CommandNotFoundException;
import com.teammoeg.frostedheart.content.scenario.runner.ScenarioCommandContext;

public class CommandNode implements Node {
String command;
Map<String, String> params;

public CommandNode(String command, Map<String, String> params) {
String origText;
public CommandNode(String command, Map<String, String> params,String origText) {
super();
this.command = command.toLowerCase();
this.params = params;
this.origText=origText;
}

@Override
Expand All @@ -50,7 +52,11 @@ public boolean isLiteral() {

@Override
public void run(ScenarioCommandContext runner) {
runner.callCommand(command, params);
try {
runner.callCommand(command, params);
}catch(CommandNotFoundException ex) {
runner.thread().appendLiteral(origText);
}
}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ private static record ParseResult(List<NodeInfo> node,ParserState state) {


}
private NodeInfo createCommand(String command, Map<String, String> params,ParserState state) {
return new NodeInfo(createCommandNode(command,params),state);
private NodeInfo createCommand(String command, Map<String, String> params,ParserState state,String text) {
return new NodeInfo(createCommandNode(command,params,text),state);
}
public static final Map<String,BiFunction<String,Map<String, String>,Node>> nodeFactory=new HashMap<>();
static {
Expand All @@ -82,16 +82,16 @@ private NodeInfo createCommand(String command, Map<String, String> params,Parser
nodeFactory.put("emb",EmbNode::new);
nodeFactory.put("label",LabelNode::new);
//nodeFactory.put("p",ParagraphNode::new);
nodeFactory.put("save",SavepointNode::new);
//nodeFactory.put("save",SavepointNode::new);
nodeFactory.put("sharp",ShpNode::new);
nodeFactory.put("include",IncludeNode::new);
nodeFactory.put("call",CallNode::new);
}
private Node createCommandNode(String command, Map<String, String> params) {
private Node createCommandNode(String command, Map<String, String> params,String text) {
BiFunction<String, Map<String, String>, Node> factory=nodeFactory.get(command);
if(factory!=null)
return factory.apply(command, params);
return new CommandNode(command, params);
return new CommandNode(command, params,text);

}
public Scenario parseString(String name,List<String> code) {
Expand Down Expand Up @@ -169,7 +169,7 @@ private NodeInfo parseAtCommand(StringParseReader reader) {
String command = parseLiteralOrString(reader, -1);
reader.skipWhitespace();
//System.out.println("cmd:"+command);
if(!reader.has()) return createCommand(command, params,state);
if(!reader.has()) return createCommand(command, params,state,reader.fromStart());
while (reader.has()) {
String name = parseLiteralOrString(reader, '=');
reader.skipWhitespace();
Expand All @@ -182,7 +182,7 @@ private NodeInfo parseAtCommand(StringParseReader reader) {
params.put(name, val);
reader.skipWhitespace();

if (!reader.has()||reader.eat('#')) return createCommand(command, params,state);
if (!reader.has()||reader.eat('#')) return createCommand(command, params,state,reader.fromStart());
}
return new NodeInfo(new LiteralNode(reader.fromStart()),state);

Expand All @@ -194,7 +194,7 @@ private NodeInfo parseBarackCommand(StringParseReader reader) {
String command = parseLiteralOrString(reader, ']');
reader.skipWhitespace();

if(reader.eat(']')) return createCommand(command, params,state);
if(reader.eat(']')) return createCommand(command, params,state,reader.fromStart());
while (reader.has()) {
String name = parseLiteralOrString(reader, '=');
reader.skipWhitespace();
Expand All @@ -205,7 +205,7 @@ private NodeInfo parseBarackCommand(StringParseReader reader) {
String val = parseLiteralOrString(reader, ']');
params.put(name, val);
reader.skipWhitespace();
if(reader.eat(']'))return createCommand(command, params,state);
if(reader.eat(']'))return createCommand(command, params,state,reader.fromStart());
}
return new NodeInfo(new LiteralNode(reader.fromStart()),state);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.teammoeg.frostedheart.content.scenario.CommandNotFoundException;
import com.teammoeg.frostedheart.content.scenario.FHScenario;
import com.teammoeg.frostedheart.content.scenario.ScenarioExecutionException;
import com.teammoeg.frostedheart.content.scenario.parser.Node;
Expand Down Expand Up @@ -318,6 +319,10 @@ public boolean tickWaitIfNeeded() {
}
return false;
}
@Override
public void appendLiteral(String text) {
scene.appendLiteral(text);
}
public void tickMain(ScenarioContext ctx) {
runIfNeeded(ctx);
tickWaitIfNeeded();
Expand Down Expand Up @@ -398,6 +403,7 @@ public void callCommand(ScenarioCommandContext ctx,String name,Map<String,String
jump(ctx.context(),ctx.context().macros.get(name));
}else
FHScenario.callCommand(name, ctx, cparams);

}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,6 @@ default void jump(ScenarioContext ctx,String scenario,String label) {
* Pop call stack.
*/
void popCallStack(ScenarioContext ctx);

void appendLiteral(String text);
}

0 comments on commit d505335

Please sign in to comment.