-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameBookExtention.cs
112 lines (101 loc) · 3.31 KB
/
GameBookExtention.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using Mes.core;
using System.Text;
using System.Text.RegularExpressions;
using System.Text.Json.Serialization;
namespace Mes.Extension;
public static class SelecterRegex
{
static internal Regex regChoose = new Regex("choose:.*?,",RegexOptions.Compiled);
static internal Regex regJump = new Regex("jump:.*?,", RegexOptions.Compiled);
}
public class BranchNode
{
[JsonInclude]
public string section = "";
[JsonInclude]
public Select[] selects = new Select[] { };
public class Select
{
[JsonInclude]
public string value = "";
[JsonInclude]
public string jump = "";
public Select(string extFieldText)
{
extFieldText = extFieldText.Trim();
value = SelecterRegex.regChoose.Match(extFieldText).Value.Replace("choose:","").Replace(",","").Trim();
jump = SelecterRegex.regJump.Match(extFieldText).Value.Replace("jump:", "").Replace(",", "").Trim();
}
public Select() { }
}
public BranchNode(string section, Select[] selects)
{
this.section = section;
this.selects = selects;
}
}
public static class GameBookExtention
{
public static BranchNode[] GetGameBook()
{
return new BranchNode[] { };
}
public static IEnumerable<BranchNode> GetBranchNodes(this Mes.core.Mes mes)
{
var regJump = new Regex("jump:.*?,", RegexOptions.Compiled);
var regChoose = new Regex("choose:.*?,", RegexOptions.Compiled);
foreach (var section in mes.body.sections)
{
var SelectList = section.pieces
.Select(v => v.ext_field.Split("\n"))
.SelectMany(x => x)
.Where(ex => regJump.IsMatch(ex) || regChoose.IsMatch(ex))
.Select(ex => new BranchNode.Select(ex))
.ToArray();
yield return new BranchNode(section.name, SelectList);
}
}
public static string ExportMermaid(this Mes.core.Mes mes)
{
var branches = mes.GetBranchNodes();
StringBuilder result = new StringBuilder();
result.Append("graph TD;\n");
foreach (var branch in branches)
{
foreach (var select in branch.selects)
{
if (select.value != "")
{
result.Append($"{branch.section}-->|{select.value}|{select.jump};\n");
}
else
{
result.Append($"{branch.section}-->{select.jump};\n");
}
}
}
return result.ToString();
}
public static string ExportPlantUML(this Mes.core.Mes mes)
{
var branches = mes.GetBranchNodes();
StringBuilder result = new StringBuilder();
result.Append("@startuml\n");
foreach (var branch in branches)
{
foreach (var select in branch.selects)
{
if (select.value != "")
{
result.Append($"[{branch.section}]-->[{select.jump}]:{select.value}\n");
}
else
{
result.Append($"[{branch.section}]-->[{select.jump}]\n");
}
}
}
result.Append("@enduml");
return result.ToString();
}
}