1 | #! perl |
1 | #! perl # mandatory depends=doclet |
2 | |
2 | |
3 | our $TOPIC; |
3 | our $TOPIC; |
|
|
4 | our %DOCLET; |
4 | |
5 | |
5 | our $HELP_CHANNEL = { |
6 | our $HELP_CHANNEL = { |
6 | id => "help", |
7 | id => "help", |
7 | title => "Help", |
8 | title => "Help", |
8 | reply => "help ", |
9 | reply => "help ", |
9 | tooltip => "Online Help", |
10 | tooltip => "Online Help", |
|
|
11 | }; |
|
|
12 | |
|
|
13 | # considerable duplication between load_doclets and load_topics |
|
|
14 | sub load_doclets { |
|
|
15 | %DOCLET = (); |
|
|
16 | |
|
|
17 | my @command_list; |
|
|
18 | |
|
|
19 | for ( |
|
|
20 | [0, "command_help"], |
|
|
21 | [1, "emote_help"], |
|
|
22 | [2, "dmcommand_help"], |
|
|
23 | ) { |
|
|
24 | my ($type, $path) = @$_; |
|
|
25 | |
|
|
26 | my $paragraphs = cf::pod::load_pod "$PODDIR/$path.pod" |
|
|
27 | or die "unable to load $path"; |
|
|
28 | |
|
|
29 | my $level = 1e9; |
|
|
30 | my $rpar; |
|
|
31 | |
|
|
32 | for my $par (@$paragraphs) { |
|
|
33 | if ($par->{type} eq "head2") { |
|
|
34 | # this code taken almost verbatim from DC/Protocol.pm |
|
|
35 | |
|
|
36 | if ($par->{markup} =~ /^(\S+) (?:\s+ \( ([^\)]*) \) )?/x) { |
|
|
37 | my $cmd = $1; |
|
|
38 | my @args = split /\|/, $2; |
|
|
39 | @args = (".*") unless @args; |
|
|
40 | |
|
|
41 | $_ = $_ eq ".*" ? "" : " $_" |
|
|
42 | for @args; |
|
|
43 | |
|
|
44 | my @variants = map "$cmd$_", sort { (length $a) <=> (length $b) } @args; |
|
|
45 | |
|
|
46 | $rpar = \($DOCLET{$cmd} = &cf::pod::as_cfpod ([$par])); |
|
|
47 | |
|
|
48 | push @command_list, [$type, \@variants]; |
|
|
49 | $level = $par->{level}; |
|
|
50 | } else { |
|
|
51 | cf::error "$par->{markup}: unparsable command heading"; |
|
|
52 | } |
|
|
53 | } elsif ($par->{level} > $level) { |
|
|
54 | $$rpar .= &cf::pod::as_cfpod ([$par]); |
|
|
55 | } |
|
|
56 | |
|
|
57 | cf::cede_to_tick; |
|
|
58 | } |
|
|
59 | } |
|
|
60 | |
|
|
61 | @command_list = sort { |
|
|
62 | $a->[0] <=> $b->[0] |
|
|
63 | or $a->[1] cmp $b->[1] |
|
|
64 | } @command_list; |
|
|
65 | |
|
|
66 | cf::cede_to_tick; |
|
|
67 | |
|
|
68 | cf::face::set |
|
|
69 | "res/command_list" => cf::FT_RSRC, |
|
|
70 | JSON::XS->new->utf8->encode (\@command_list); |
|
|
71 | } |
|
|
72 | |
|
|
73 | our $DOCLET_HANDLER = ext::doclet::register command => sub { |
|
|
74 | my ($pl, $category, $command) = @_; |
|
|
75 | |
|
|
76 | my $guard = cf::lock_acquire "ext::help::loading"; |
|
|
77 | |
|
|
78 | $DOCLET{$command} |
|
|
79 | || "B<No documentation available for '$category/$command'>" |
10 | }; |
80 | }; |
11 | |
81 | |
12 | sub load_topics($$) { |
82 | sub load_topics($$) { |
13 | my ($type, $path) = @_; |
83 | my ($type, $path) = @_; |
14 | |
84 | |
… | |
… | |
32 | |
102 | |
33 | @topics |
103 | @topics |
34 | } |
104 | } |
35 | |
105 | |
36 | sub reload() { |
106 | sub reload() { |
|
|
107 | my $guard1 = cf::lock_acquire "ext::help::loading"; |
|
|
108 | my $guard2 = cf::lock_acquire "ext::resource"; |
|
|
109 | |
|
|
110 | local $Coro::current->{desc} = "help loader"; |
|
|
111 | |
37 | $TOPIC = { |
112 | $TOPIC = { |
38 | (load_topics "DM Commands" => "dmcommand_help"), |
113 | (load_topics "DM Commands" => "dmcommand_help"), |
39 | (load_topics "Emotes" => "emote_help"), |
114 | (load_topics "Emotes" => "emote_help"), |
40 | (load_topics "Commands" => "command_help"), |
115 | (load_topics "Commands" => "command_help"), |
41 | (load_topics "Generic Help Topics" => "generic_help"), |
116 | (load_topics "Generic Help Topics" => "generic_help"), |
42 | }; |
117 | }; |
|
|
118 | |
|
|
119 | load_doclets; |
43 | |
120 | |
44 | () |
121 | () |
45 | } |
122 | } |
46 | |
123 | |
47 | cf::sync_job { |
124 | cf::post_init { |
48 | my $guard = cf::lock_acquire "ext::help::loading"; |
|
|
49 | cf::async_ext { |
125 | cf::async_ext { reload }; |
50 | $Coro::current->{desc} = "help loader"; |
126 | Coro::cede; # make sure reload acquires the lock(s) |
51 | reload; |
|
|
52 | undef $guard; |
|
|
53 | }; |
|
|
54 | }; |
127 | }; |
55 | |
|
|
56 | # for lack of a better place: "media tags" |
|
|
57 | # b bold |
|
|
58 | # i italic |
|
|
59 | # ul underlined |
|
|
60 | # fixed font |
|
|
61 | # arcane font |
|
|
62 | # hand font |
|
|
63 | # strange font |
|
|
64 | # print font (default) |
|
|
65 | # color=xxx |
|
|
66 | |
128 | |
67 | cf::register_command help => sub { |
129 | cf::register_command help => sub { |
68 | my ($pl, $topic) = @_; |
130 | my ($pl, $topic) = @_; |
69 | |
131 | |
70 | if (cf::lock_active "ext::help::loading") { |
132 | if (cf::lock_active "ext::help::loading") { |
… | |
… | |
82 | push @{$topics{$v->[0]}}, $k; |
144 | push @{$topics{$v->[0]}}, $k; |
83 | } |
145 | } |
84 | |
146 | |
85 | my $res; |
147 | my $res; |
86 | while (my ($k, $v) = each %topics) { |
148 | while (my ($k, $v) = each %topics) { |
87 | $res .= "B<$k:>\n" . (join " ", sort @$v) . "\n\n"; |
149 | $res .= "T<$k:>\n\n" . (join " ", sort @$v) . "\n\n"; |
88 | } |
150 | } |
89 | |
151 | |
90 | $pl->send_msg ($HELP_CHANNEL => $res, cf::NDI_REPLY | cf::NDI_CLEAR); |
152 | $pl->send_msg ($HELP_CHANNEL => $res, cf::NDI_REPLY | cf::NDI_CLEAR); |
91 | |
153 | |
92 | } elsif (my $item = $TOPIC->{$topic}) { |
154 | } elsif (my $item = $TOPIC->{$topic}) { |