aboutsummaryrefslogtreecommitdiff
path: root/src/FbTk
diff options
context:
space:
mode:
Diffstat (limited to 'src/FbTk')
-rw-r--r--src/FbTk/LResource.cc52
-rw-r--r--src/FbTk/LResource.hh3
2 files changed, 40 insertions, 15 deletions
diff --git a/src/FbTk/LResource.cc b/src/FbTk/LResource.cc
index d7627c9..0909520 100644
--- a/src/FbTk/LResource.cc
+++ b/src/FbTk/LResource.cc
@@ -101,17 +101,8 @@ void LResourceManager::convert(ResourceManager &old, const std::string &new_file
101} 101}
102 102
103LResourceManager::LResourceManager(const std::string &root, Lua &l) 103LResourceManager::LResourceManager(const std::string &root, Lua &l)
104 : ResourceManager_base(root), m_l(&l) { 104 : ResourceManager_base(root), m_l(&l) {
105 l.checkstack(2); 105 setLua(l);
106 lua::stack_sentry s(l);
107
108 l.pushstring(root);
109
110 l.getfield(lua::REGISTRYINDEX, make_root);
111 l.pushstring(root);
112 l.call(1, 1);
113
114 l.readOnlySet(lua::GLOBALSINDEX);
115} 106}
116 107
117bool LResourceManager::save(const char *filename, const char *) { 108bool LResourceManager::save(const char *filename, const char *) {
@@ -127,11 +118,20 @@ bool LResourceManager::save(const char *filename, const char *) {
127} 118}
128 119
129void LResourceManager::addResource(Resource_base &r) { 120void LResourceManager::addResource(Resource_base &r) {
121 ResourceManager_base::addResource(r);
122 try {
123 doAddResource(r);
124 }
125 catch(...) {
126 ResourceManager_base::removeResource(r);
127 throw;
128 }
129}
130
131void LResourceManager::doAddResource(Resource_base &r) {
130 m_l->checkstack(5); 132 m_l->checkstack(5);
131 lua::stack_sentry s(*m_l); 133 lua::stack_sentry s(*m_l);
132 134
133 ResourceManager_base::addResource(r);
134
135 m_l->getfield(lua::REGISTRYINDEX, register_resource); 135 m_l->getfield(lua::REGISTRYINDEX, register_resource);
136 m_l->getfield(lua::GLOBALSINDEX, m_root.c_str()); 136 m_l->getfield(lua::GLOBALSINDEX, m_root.c_str());
137 m_l->pushstring(r.name()); 137 m_l->pushstring(r.name());
@@ -143,7 +143,12 @@ void LResourceManager::addResource(Resource_base &r) {
143} 143}
144 144
145void LResourceManager::removeResource(Resource_base &r) { 145void LResourceManager::removeResource(Resource_base &r) {
146 m_l->checkstack(5); 146 doRemoveResource(r);
147 ResourceManager_base::removeResource(r);
148}
149
150void LResourceManager::doRemoveResource(Resource_base &r) {
151 m_l->checkstack(4);
147 lua::stack_sentry s(*m_l); 152 lua::stack_sentry s(*m_l);
148 153
149 m_l->getfield(lua::REGISTRYINDEX, register_resource); 154 m_l->getfield(lua::REGISTRYINDEX, register_resource);
@@ -153,8 +158,25 @@ void LResourceManager::removeResource(Resource_base &r) {
153 m_l->call(3, 1); 158 m_l->call(3, 1);
154 *static_cast<Resource_base **>(m_l->touserdata(-1)) = NULL; 159 *static_cast<Resource_base **>(m_l->touserdata(-1)) = NULL;
155 m_l->pop(); 160 m_l->pop();
161}
156 162
157 ResourceManager_base::removeResource(r); 163void LResourceManager::setLua(Lua &l) {
164 l.checkstack(2);
165 lua::stack_sentry s(l);
166
167 for(ResourceList::const_iterator i = begin(); i != end(); ++i)
168 doRemoveResource(**i);
169
170 l.getfield(lua::REGISTRYINDEX, make_root);
171 l.pushstring(m_root);
172 l.call(1, 1);
173
174 l.readOnlySetField(lua::GLOBALSINDEX, m_root.c_str());
175
176 m_l = &l;
177
178 for(ResourceList::const_iterator i = begin(); i != end(); ++i)
179 doAddResource(**i);
158} 180}
159 181
160} // end namespace FbTk 182} // end namespace FbTk
diff --git a/src/FbTk/LResource.hh b/src/FbTk/LResource.hh
index 1567269..bb7f224 100644
--- a/src/FbTk/LResource.hh
+++ b/src/FbTk/LResource.hh
@@ -41,8 +41,11 @@ public:
41 virtual bool save(const char *filename, const char *); 41 virtual bool save(const char *filename, const char *);
42 virtual void addResource(Resource_base &r); 42 virtual void addResource(Resource_base &r);
43 virtual void removeResource(Resource_base &r); 43 virtual void removeResource(Resource_base &r);
44 void setLua(Lua &l);
44 45
45private: 46private:
47 void doAddResource(Resource_base &r);
48 void doRemoveResource(Resource_base &r);
46 49
47 Lua *m_l; 50 Lua *m_l;
48}; 51};