-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEntityCallback.cpp
156 lines (136 loc) · 3.34 KB
/
EntityCallback.cpp
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
#include "EntityCallback.hpp"
#include "Entity.hpp"
#include "EntityScheme.hpp"
#include "EntityFieldType.hpp"
#include "../inanity/File.hpp"
#include "../inanity/script/np/Any.hpp"
#include "../inanity/script/np/State.hpp"
#include "../inanity/Log.hpp"
#include "../inanity/Exception.hpp"
BEGIN_INANITY_OIL
EntityCallback::EntityCallback(ptr<Entity> entity, ptr<Script::Np::Any> callback)
: entity(entity), callback(callback)
{
entity->OnNewCallback(this);
}
EntityCallback::~EntityCallback()
{
entity->OnFreeCallback(this);
}
void EntityCallback::FireScheme(ptr<EntityScheme> scheme)
{
try
{
ptr<Script::Np::State> scriptState = callback->GetState();
callback->Call(
scriptState->NewString("scheme"),
scriptState->WrapObject<RefCounted>(nullptr),
scriptState->WrapObject(scheme));
}
catch(Exception* exception)
{
Log::Error(exception);
}
}
void EntityCallback::FireTag(const EntityTagId& tagId, ptr<File> value)
{
try
{
ptr<Script::Np::State> scriptState = callback->GetState();
callback->Call(
scriptState->NewString("tag"),
scriptState->NewString(tagId.ToString()),
scriptState->WrapObject(value));
}
catch(Exception* exception)
{
Log::Error(exception);
}
}
void EntityCallback::FireField(const EntityFieldId& fieldId, ptr<File> value)
{
try
{
const EntityScheme::Fields& fields = entity->GetScheme()->GetFields();
EntityScheme::Fields::const_iterator i = fields.find(fieldId);
if(i == fields.end())
return;
ptr<Script::Any> scriptValue = i->second.type->
TryConvertToScript(entity->GetManager(), callback->GetState(), value);
ptr<Script::Np::State> scriptState = callback->GetState();
callback->Call(
scriptState->NewString("field"),
scriptState->NewString(fieldId.ToString()),
scriptValue);
}
catch(Exception* exception)
{
Log::Error(exception);
}
}
void EntityCallback::FireData(ptr<File> key, ptr<File> value)
{
try
{
ptr<Script::Np::State> scriptState = callback->GetState();
callback->Call(
scriptState->NewString("data"),
scriptState->WrapObject(key),
scriptState->WrapObject(value));
}
catch(Exception* exception)
{
Log::Error(exception);
}
}
void EntityCallback::EnumerateScheme()
{
FireScheme(entity->GetScheme());
}
void EntityCallback::EnumerateTag(const EntityTagId& tagId)
{
FireTag(tagId, entity->ReadTag(tagId));
}
void EntityCallback::EnumerateFields()
{
class Enumerator : public Entity::FieldEnumerator
{
private:
std::vector<std::pair<EntityFieldId, ptr<File> > > fields;
public:
void OnField(const EntityFieldId& fieldId, ptr<File> value)
{
fields.push_back(std::make_pair(fieldId, value));
}
void Fire(EntityCallback* callback)
{
for(size_t i = 0; i < fields.size(); ++i)
callback->FireField(fields[i].first, fields[i].second);
}
};
Enumerator enumerator;
entity->EnumerateFields(&enumerator);
enumerator.Fire(this);
}
void EntityCallback::EnumerateData()
{
class Enumerator : public Entity::DataEnumerator
{
private:
std::vector<std::pair<ptr<File>, ptr<File> > > data;
public:
void OnData(ptr<File> key, ptr<File> value)
{
data.push_back(std::make_pair(key, value));
}
void Fire(EntityCallback* callback)
{
for(size_t i = 0; i < data.size(); ++i)
callback->FireData(data[i].first, data[i].second);
}
};
Enumerator enumerator;
entity->EnumerateData(&enumerator);
enumerator.Fire(this);
}
END_INANITY_OIL