aboutsummaryrefslogtreecommitdiffstats
path: root/lib/lkn/core/component.ex
blob: b79a6f6dfcaa4e19e7d91f24cc0f279ee15fa978 (plain)
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#
# lkn.core: an entity-component-system (ecs) implementation for lyxan
# Copyright (C) 2017 Thomas Letan <contact@thomasletan.fr>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
alias Lkn.Core.Specs

defmodule Lkn.Core.Component do
  defp cast_client(module_name, cast) do
    name = cast.fun.name
    entity_key_type = quote do
      Lkn.Core.Entity.k
    end

    cast_doc = if cast.doc != :none do
      quote do
        @doc unquote(cast.doc)
      end
    end

    arglist = Enum.map(cast.fun.arguments, &(&1.name))
    arglistcl = [{:entity_key, [], nil}|arglist]
    argtypes = [entity_key_type|Enum.map(cast.fun.arguments, &(&1.type))]

    quote do
      unquote(cast_doc)
      @spec unquote({name, [], argtypes}) :: :ok
      def unquote({name, [], arglistcl}) do
        GenServer.cast(Lkn.Core.Name.component(unquote({:entity_key, [], nil}), unquote(module_name)),
                       {:spec, {unquote(name), unquote(arglist)}})
      end
    end
  end

  defp call_client(module_name, call) do
    name = call.fun.name
    entity_key_type = quote do
      Lkn.Core.Entity.k
    end

    call_doc = if call.doc != :none do
      quote do
        @doc unquote(call.doc)
      end
    end

    arglist = Enum.map(call.fun.arguments, &(&1.name))
    arglistcl = [{:entity_key, [], nil}|arglist]
    argtypes = [entity_key_type|Enum.map(call.fun.arguments, &(&1.type))]

    quote do
      unquote(call_doc)
      @spec unquote({name, [], argtypes}) :: unquote(call.ret)
      def unquote({name, [], arglistcl}) do
        GenServer.call(Lkn.Core.Name.component(unquote({:entity_key, [], nil}), unquote(module_name)),
                       {:spec, {unquote(name), unquote(arglist)}})
      end
    end
  end

  defp cast_behaviour(cast) do
    name = cast.fun.name
    entity_key_type = quote do
      Lkn.Core.Entity.k
    end

    arglistcl = [{:::, [], [{:entity_key, [], nil}, entity_key_type]}
                 |Enum.map(cast.fun.arguments, &({:::, [], [&1.name, &1.type]}))]

    quote do
      @callback unquote({name, [], arglistcl}) :: :ok
    end
  end

  defp call_behaviour(call) do
    name = call.fun.name
    entity_key_type = quote do
      Lkn.Core.Entity.k
    end

    arglistcl = [{:::, [], [{:entity_key, [], nil}, entity_key_type]}
                 |Enum.map(call.fun.arguments, &({:::, [], [&1.name, &1.type]}))]

    quote do
      @callback unquote({name, [], arglistcl}) :: unquote(call.ret)
    end
  end

  defmacro defcomponent(name, do: block) do
    block = case block do
              {:__block__, _, x} -> x
              x -> [x]
            end

    {casts, calls, legit} = Specs.parse_specs(block, [], [], [])

    casts_client = Enum.map(casts, &(cast_client(name, &1)))
    calls_client = Enum.map(calls, &(call_client(name, &1)))

    casts_behaviour = Enum.map(casts, &(cast_behaviour(&1)))
    calls_behaviour = Enum.map(calls, &(call_behaviour(&1)))

    quote do
      defmodule unquote(name) do
        unquote(legit)
        unquote(casts_client)
        unquote(calls_client)
        unquote(casts_behaviour)
        unquote(calls_behaviour)

        def system do
          @system
        end

        defmacro __using__(_) do
          quote do
            @behaviour unquote(__MODULE__)

            use GenServer

            alias Lkn.Core.Entity
            alias Lkn.Core.Name
            alias Lkn.Core.Properties

            def specs do
              unquote(__MODULE__)
            end

            @spec start_link(Entity.k) :: GenServer.on_start
            def start_link(entity_key) do
              GenServer.start_link(__MODULE__, entity_key, name: Name.component(entity_key, unquote(__MODULE__)))
            end

            @spec read(Entity.k, Properties.prop) :: Option.t(Properties.value)
            defp read(entity_key, p) do
              Properties.read(entity_key, p)
            end

            @spec write(Entity.k, Properties.prop, Properties.value) :: :ok
            def write(entity_key, p, v) do
              Properties.write(entity_key, p, v)
            end

            def handle_cast({:spec, {name, args}}, entity_key) do
              :erlang.apply(__MODULE__, name, [entity_key|args])

              {:noreply, entity_key}
            end

            def handle_call({:spec, {name, args}}, _call, entity_key) do
              res = :erlang.apply(__MODULE__, name, [entity_key|args])

              {:reply, res, entity_key}
            end
          end
        end
      end
    end
  end
end