875 lines
30 KiB
C#
Raw Normal View History

2024-12-14 18:27:59 +08:00
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
namespace XMLTool
{
public class AppData
{
2024-12-18 16:20:06 +08:00
public PreLoad preLoad;
2024-12-14 18:27:59 +08:00
public List<Module> Modules { get; set; }
}
2024-12-18 16:20:06 +08:00
public class PreLoad
{
public Action action;
}
2024-12-14 18:27:59 +08:00
public class Module
{
public string type { get; set; }
public string Scene { get; set; }
public string ModuleName { get; set; }
public List<Operation> Operations { get; set; }
public List<Device> Devices { get; set; }
public Dictionary<string, FSM> FSM = new Dictionary<string, FSM>();
public Dictionary<string, Action> ActionDict { get; set; }
public Score score;
}
public class Device
{
public string Name { get; set; }
public string Icon { get; set; }
public string HighColor { get; set; }
public string Path { get; set; }
public string Tip { get; set; }
public string BoxColliderSize { get; set; }
public string BoxColliderCenter { get; set; }
public bool MeshCollider { get; set; }
}
public class Score
{
public List<ScoreStep> scores;
}
public class ScoreStep
{
public string step;
public string name;
public string sum;
public string bind;
public float value;
}
public class ActionItem
{
public string Name { get; set; }
public string Type { get; set; }
public string Value { get; set; }
public List<ActionItem> SubActions { get; set; }
}
public class Action : ActionItem
{
}
public class Condition : ActionItem
{
}
public class FSM
{
public string Name { get; set; }
public List<Transision> Transisions { get; set; }
public Dictionary<string, State> StateDict { get; set; }
}
public class State
{
public string Name { get; set; }
public StatePart Enter { get; set; }
public StatePart Exit { get; set; }
}
public class StatePart
{
public Action Action { get; set; }
}
public class Transision
{
public string Name { get; set; }
public string From { get; set; }
public string To { get; set; }
public Condition Action { get; set; }
}
public class Operation
{
public string moduleType { get; set; }
public List<Step> Steps { get; set; }
}
public class Step
{
public Step Parent { get; set; }
public string Name { get; set; }
public Action Reset { get; set; }
public Action Start { get; set; }
public Action Finished { get; set; }
public bool isFinished = false;
public List<Step> SubSteps { get; set; }
}
public class XmlParser
{
public static AppData ParseXml(string xmlString)
{
// <20><><EFBFBD><EFBFBD>XML
XDocument doc = XDocument.Parse(xmlString);
// <20><>ȡ<EFBFBD><C8A1>Ԫ<EFBFBD><D4AA>
XElement appDataElement = doc.Root;
// <20><>ʼ<EFBFBD><CABC>AppData<74><61><EFBFBD><EFBFBD>
AppData appData = new AppData
{
Modules = new List<Module>(),
};
2024-12-18 16:20:06 +08:00
// <20><><EFBFBD><EFBFBD>Ԥ<EFBFBD><D4A4><EFBFBD>ض<EFBFBD><D8B6><EFBFBD>
var preLoadElement = appDataElement.Element("PreLoad");
2024-12-19 11:01:50 +08:00
if (preLoadElement!=null)
2024-12-18 16:20:06 +08:00
{
2024-12-19 11:52:56 +08:00
appData.preLoad = new PreLoad();
2024-12-19 11:01:50 +08:00
foreach (XElement actionElement in preLoadElement.Elements("Action"))
{
var action = ParseAction(actionElement);
appData.preLoad.action = action;
}
2024-12-18 16:20:06 +08:00
}
2024-12-19 11:01:50 +08:00
2024-12-18 16:20:06 +08:00
// <20><><EFBFBD><EFBFBD>ģ<EFBFBD><C4A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
LoadModules(appDataElement, appData);
return appData;
}
public static void LoadModules(XElement appDataElement, AppData appData)
{
2024-12-14 18:27:59 +08:00
foreach (XElement moduleElement in appDataElement.Elements("Module"))
{
2024-12-18 16:20:06 +08:00
LoadModule(moduleElement, appData);
}
}
public static void LoadModule(XElement moduleElement, AppData appData)
{
if (moduleElement != null)
{
Module module = new Module()
2024-12-14 18:27:59 +08:00
{
2024-12-18 16:20:06 +08:00
Devices = new List<Device>(),
ActionDict = new Dictionary<string, Action>(),
Operations = new List<Operation>(),
};
appData.Modules.Add(module);
// <20><><EFBFBD><EFBFBD>ģ<EFBFBD><C4A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
module.type = moduleElement.Element("Type")?.Value;
module.ModuleName = moduleElement.Element("Name")?.Value;
module.Scene = moduleElement.Element("Scene")?.Value;
// <20><><EFBFBD><EFBFBD><EFBFBD>
foreach (XElement deviceElement in moduleElement.Elements("Device"))
{
Device device = new Device
2024-12-14 18:27:59 +08:00
{
2024-12-18 16:20:06 +08:00
Name = deviceElement.Element("Name")?.Value,
Icon = deviceElement.Element("Icon")?.Value,
HighColor = deviceElement.Element("HighLight")?.Attribute("color")?.Value,
Path = deviceElement.Element("Path")?.Value,
Tip = deviceElement.Element("Tip")?.Value,
MeshCollider = deviceElement.Element("MeshCollider") != null,
BoxColliderCenter = deviceElement.Element("BoxCollider")?.Attribute("center")?.Value,
BoxColliderSize = deviceElement.Element("BoxCollider")?.Attribute("size")?.Value,
2024-12-14 18:27:59 +08:00
2024-12-18 16:20:06 +08:00
};
module.Devices.Add(device);
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
foreach (XElement actionElement in moduleElement.Elements("Action"))
{
var action = ParseAction(actionElement);
2024-12-14 18:27:59 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2024-12-18 16:20:06 +08:00
module.ActionDict.Add(action.Name, action);
}
// <20><><EFBFBD><EFBFBD>״̬<D7B4><CCAC>
foreach (XElement fsmElement in moduleElement.Elements("FSM"))
{
FSM fsm = new FSM()
2024-12-14 18:27:59 +08:00
{
2024-12-18 16:20:06 +08:00
Name = fsmElement.Attribute("name")?.Value,
StateDict = new Dictionary<string, State>(),
Transisions = new List<Transision>(),
};
// <20><><EFBFBD><EFBFBD>״̬
foreach (var stateNode in fsmElement.Elements("State"))
2024-12-14 18:27:59 +08:00
{
2024-12-18 16:20:06 +08:00
State state = new State()
2024-12-14 18:27:59 +08:00
{
2024-12-18 16:20:06 +08:00
Name = stateNode.Attribute("name")?.Value,
2024-12-14 18:27:59 +08:00
};
2024-12-18 16:20:06 +08:00
fsm.StateDict.Add(state.Name, state);
2024-12-14 18:27:59 +08:00
2024-12-18 16:20:06 +08:00
XElement enterElement = stateNode.Element("Enter");
XElement exitElement = stateNode.Element("Exit");
if (enterElement != null)
2024-12-14 18:27:59 +08:00
{
2024-12-18 16:20:06 +08:00
state.Enter = new StatePart();
state.Enter.Action = ParseAction(enterElement.Element("Action"));
2024-12-14 18:27:59 +08:00
}
2024-12-18 16:20:06 +08:00
if (exitElement != null)
2024-12-14 18:27:59 +08:00
{
2024-12-18 16:20:06 +08:00
state.Exit = new StatePart();
state.Exit.Action = ParseAction(exitElement.Element("Action"));
}
2024-12-14 18:27:59 +08:00
2024-12-18 16:20:06 +08:00
}
// <20><><EFBFBD><EFBFBD>״̬ת<CCAC><D7AA>
foreach (var transNode in fsmElement.Elements("Transision"))
2024-12-14 18:27:59 +08:00
{
2024-12-18 16:20:06 +08:00
Transision trans = new Transision()
2024-12-14 18:27:59 +08:00
{
2024-12-18 16:20:06 +08:00
Name = transNode.Attribute("name")?.Value,
From = transNode.Attribute("from")?.Value,
To = transNode.Attribute("to")?.Value,
2024-12-14 18:27:59 +08:00
};
2024-12-18 16:20:06 +08:00
trans.Action = ParseCondition(transNode.Element("Condition"));
fsm.Transisions.Add(trans);
2024-12-14 18:27:59 +08:00
}
2024-12-18 16:20:06 +08:00
module.FSM.Add(fsm.Name, fsm);
}
2024-12-14 18:27:59 +08:00
2024-12-18 16:20:06 +08:00
foreach (var operationNode in moduleElement.Elements("Operation"))
{
//module.Operations
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2024-12-14 18:27:59 +08:00
2024-12-18 16:20:06 +08:00
var op = new Operation()
2024-12-14 18:27:59 +08:00
{
2024-12-18 16:20:06 +08:00
Steps = new List<Step>(),
};
op.moduleType = operationNode.Attribute("moduleType")?.Value;
foreach (XElement stepNode in operationNode.Elements("Step"))
{
op.Steps.Add(ParserStep(stepNode, null));
}
module.Operations.Add(op);
}
XElement scoreNode = moduleElement.Element("Score");
if (scoreNode != null)
{
module.score = new Score()
{
scores = new List<ScoreStep>()
};
foreach (var item in scoreNode.Elements("Item"))
{
module.score.scores.Add(
new ScoreStep()
{
step = item.Attribute("step")?.Value,
name = item.Attribute("name")?.Value,
sum = item.Attribute("sum")?.Value,
bind = item.Attribute("bind")?.Value,
});
2024-12-14 18:27:59 +08:00
}
}
}
}
public static Step ParserStep(XElement stepNode, Step parent)
{
Step step = new Step();
step.Name = stepNode.Attribute("name")?.Value;
step.Reset = ParseAction(stepNode.Element("Reset")?.Element("Action"));
step.Start = ParseAction(stepNode.Element("Start")?.Element("Action"));
step.Finished = ParseAction(stepNode.Element("Finished")?.Element("Action"));
step.SubSteps = ParserSteps(stepNode, step);
step.Parent = parent;
return step;
}
public static List<Step> ParserSteps(XElement step, Step parent)
{
List<Step> steps = new List<Step>();
if (step.Elements("Step").Any())
{
foreach (var item in step.Elements("Step"))
{
steps.Add(ParserStep(item, parent));
}
}
return steps;
}
public static Action ParseAction(XElement action)
{
if (action == null)
{
return null;
}
string type = action.Attribute("type").Value;
Action newAction = null;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>չAction<6F><6E><EFBFBD>ֶ<EFBFBD>
switch (type)
{
case "Move":
case "Rotate":
2024-12-18 14:26:20 +08:00
case "Scale":
2024-12-14 18:27:59 +08:00
{
var act = new MoveOrAction();
act.to = Utility.GetVector3FromStrArray(action.Attribute("to").Value);
act.time = float.Parse(action.Attribute("time").Value);
newAction = act;
}
break;
case "Anim":
{
2024-12-18 11:17:08 +08:00
var act = new StringListAction();
var animName = action.Attribute("animName");
if (animName != null)
{
act.args.Add(animName.Value);
}
else
{
act.args.Add("");
}
var reset = action.Attribute("reset");
if (reset != null)
{
act.args.Add(reset.Value);
}
else
{
act.args.Add("false");
}
2024-12-14 18:27:59 +08:00
newAction = act;
}
break;
case "UITools":
{
var act = new StringListAction();
act.args.Add(action.Attribute("devices").Value);
act.args.Add(action.Attribute("answers").Value);
var setActive = action.Attribute("setActive");
if (setActive != null)
{
act.args.Add(setActive.Value);
}
else
{
act.args.Add("true");
}
var rightLabel = action.Attribute("rightLabel");
if (rightLabel != null)
{
act.args.Add(rightLabel.Value);
}
else
{
act.args.Add("");
}
var wrongLabel = action.Attribute("wrongLabel");
if (wrongLabel != null)
{
act.args.Add(wrongLabel.Value);
}
else
{
act.args.Add("");
}
var rightEvent = action.Attribute("rightEvent");
if (rightEvent != null)
{
act.args.Add(rightEvent.Value);
}
else
{
act.args.Add("");
}
var wrongEvent = action.Attribute("wrongEvent");
if (wrongEvent != null)
{
act.args.Add(wrongEvent.Value);
}
else
{
act.args.Add("");
}
var rightScore = action.Attribute("rightScore");
if (rightScore != null)
{
act.args.Add(rightScore.Value);
}
else
{
act.args.Add("");
}
var wrongScore = action.Attribute("wrongScore");
if (wrongScore != null)
{
act.args.Add(wrongScore.Value);
}
else
{
act.args.Add("");
}
var scoreStepName = action.Attribute("scoreStepName");
if (scoreStepName != null)
{
act.args.Add(scoreStepName.Value);
}
else
{
act.args.Add("");
}
2024-12-17 18:07:20 +08:00
2024-12-14 18:27:59 +08:00
newAction = act;
}
break;
case "TextQuestion":
{
var act = new StringListAction();
act.args.Add(action.Attribute("title").Value);
act.args.Add(action.Attribute("options").Value);
act.args.Add(action.Attribute("answers").Value);
act.args.Add(action.Attribute("btns").Value);
act.args.Add(action.Attribute("wait").Value);
act.args.Add(action.Attribute("showAnswer").Value);
newAction = act;
}
break;
case "UIShow":
{
var act = new StringListAction();
XAttribute isShow = action.Attribute("isShow");
if (isShow == null)
{
act.args.Add("true");
}
else
{
act.args.Add(isShow.Value);
}
newAction = act;
}
break;
case "Hint":
{
var act = new StringListAction();
XAttribute time = action.Attribute("time");
if (time != null)
{
act.args.Add(time.Value);
}
else
{
act.args.Add("-1");
}
XAttribute icon = action.Attribute("icon");
if (icon != null)
{
act.args.Add(icon.Value);
}
else
{
act.args.Add("true");
}
XAttribute audio = action.Attribute("audio");
if (audio != null)
{
act.args.Add(audio.Value);
}
else
{
act.args.Add("");
}
newAction = act;
}
break;
case "Show":
{
var act = new StringListAction();
XAttribute isShow = action.Attribute("isShow");
if (isShow != null)
{
act.args.Add(isShow.Value);
}
else
{
act.args.Add("true");
}
newAction = act;
}
break;
2024-12-16 15:45:19 +08:00
case "CameraSwitch":
{
var act = new StringListAction();
XAttribute isShow = action.Attribute("nearPos");
if (isShow != null)
{
act.args.Add(isShow.Value);
}
else
{
act.args.Add("0,0,0");
}
XAttribute nearRot = action.Attribute("nearRot");
if (nearRot != null)
{
act.args.Add(nearRot.Value);
}
else
{
act.args.Add("0,0,0");
}
XAttribute nearTime = action.Attribute("nearTime");
if (nearTime != null)
{
act.args.Add(nearTime.Value);
}
else
{
act.args.Add("0");
}
XAttribute normalPos = action.Attribute("normalPos");
if (normalPos != null)
{
act.args.Add(normalPos.Value);
}
else
{
act.args.Add("0,0,0");
}
XAttribute normalRot = action.Attribute("normalRot");
if (normalRot != null)
{
act.args.Add(normalRot.Value);
}
else
{
act.args.Add("0,0,0");
}
XAttribute farTime = action.Attribute("farTime");
if (farTime != null)
{
act.args.Add(farTime.Value);
}
else
{
act.args.Add("0");
}
XAttribute isNear = action.Attribute("isNear");
if (isNear != null)
{
act.args.Add(isNear.Value);
}
else
{
act.args.Add("false");
}
newAction = act;
}
break;
case "CameraLock":
{
var act = new StringListAction();
XAttribute isMove = action.Attribute("isMove");
if (isMove != null)
{
act.args.Add(isMove.Value);
}
else
{
act.args.Add("true");
}
XAttribute isRotate = action.Attribute("isRotate");
if (isRotate != null)
{
act.args.Add(isRotate.Value);
}
else
{
act.args.Add("true");
}
newAction = act;
}
break;
2024-12-17 09:39:38 +08:00
case "TextTip":
{
var act = new StringListAction();
2024-12-17 09:49:59 +08:00
XAttribute audio = action.Attribute("audio");
if (audio != null)
{
act.args.Add(audio.Value);
}
else
{
act.args.Add("");
}
2024-12-17 09:39:38 +08:00
XAttribute btns = action.Attribute("btns");
if (btns != null)
{
act.args.Add(btns.Value);
}
else
{
act.args.Add("");
}
newAction = act;
}
break;
2024-12-17 18:07:20 +08:00
case "Video":
{
var act = new StringListAction();
XAttribute size = action.Attribute("size");
if (size != null)
{
act.args.Add(size.Value);
}
else
{
act.args.Add("792,458");
}
XAttribute offset = action.Attribute("offset");
if (offset != null)
{
act.args.Add(offset.Value);
}
else
{
act.args.Add("0,0");
}
XAttribute finishedEvent = action.Attribute("finishedEvent");
if (finishedEvent != null)
{
act.args.Add(finishedEvent.Value);
}
else
{
act.args.Add("");
}
XAttribute closeEvent = action.Attribute("closeEvent");
if (closeEvent != null)
{
act.args.Add(closeEvent.Value);
}
else
{
act.args.Add("");
}
newAction = act;
}
break;
2024-12-18 13:16:36 +08:00
case "HighLight":
{
var act = new StringListAction();
XAttribute isHigh = action.Attribute("isHigh");
if (isHigh != null)
{
act.args.Add(isHigh.Value);
}
else
{
act.args.Add("true");
}
XAttribute color = action.Attribute("color");
if (color != null)
{
act.args.Add(color.Value);
}
else
{
act.args.Add("");
}
newAction = act;
}
break;
2024-12-18 16:20:06 +08:00
case "LoadRes":
{
var act = new StringListAction();
XAttribute resType = action.Attribute("resType");
if (resType != null)
{
act.args.Add(resType.Value);
}
else
{
act.args.Add("");
}
newAction = act;
}
break;
2024-12-19 09:47:07 +08:00
case "Audio":
{
//string path, string audioType, string loop, string finishedEvent, string volume,
var act = new StringListAction();
XAttribute audioType = action.Attribute("audioType");
if (audioType != null)
{
act.args.Add(audioType.Value);
}
else
{
act.args.Add("Sound");
}
XAttribute loop = action.Attribute("loop");
if (loop != null)
{
act.args.Add(loop.Value);
}
else
{
act.args.Add("false");
}
XAttribute waitFinished = action.Attribute("waitFinished");
if (waitFinished != null)
{
act.args.Add(waitFinished.Value);
}
else
{
act.args.Add("");
}
XAttribute volume = action.Attribute("volume");
if (volume != null)
{
act.args.Add(volume.Value);
}
else
{
act.args.Add("");
}
XAttribute isPlay = action.Attribute("isPlay");
if (isPlay != null)
{
act.args.Add(isPlay.Value);
}
else
{
act.args.Add("true");
}
newAction = act;
}
break;
case "Line":
{
var act = new StringListAction();
XAttribute color = action.Attribute("color");
if (color != null)
{
act.args.Add(color.Value);
}
else
{
act.args.Add("");
}
XAttribute width = action.Attribute("width");
if (width != null)
{
act.args.Add(width.Value);
}
else
{
act.args.Add("0.1");
}
XAttribute lineScale = action.Attribute("lineScale");
if (lineScale != null)
{
act.args.Add(lineScale.Value);
}
else
{
act.args.Add("1,1");
}
newAction = act;
}
break;
2024-12-14 18:27:59 +08:00
default:
newAction = new Action();
break;
}
newAction.Type = action.Attribute("type")?.Value;
newAction.Name = action.Attribute("name")?.Value;
newAction.Value = action.Attribute("value")?.Value;
newAction.SubActions = ParseActions(action);
return newAction;
}
public static Condition ParseCondition(XElement action)
{
Condition newAction = new Condition
{
Type = action.Attribute("type")?.Value,
Name = action.Attribute("name")?.Value,
Value = action.Attribute("value")?.Value,
SubActions = ParseActions(action)
};
return newAction;
}
public static ActionItem ParserActionItem(XElement element)
{
ActionItem actItem = null;
switch (element.Name.LocalName)
{
case "Action":
actItem = ParseAction(element);
break;
case "Condition":
actItem = ParseCondition(element);
break;
}
return actItem;
}
private static List<ActionItem> ParseActions(XElement actionElement)
{
List<ActionItem> actions = new List<ActionItem>();
foreach (XElement action in actionElement.Elements())
{
actions.Add(ParserActionItem(action));
}
return actions;
}
}
}