]>
Dogcows Code - chaz/yoink/blob - resource.cc
053fc0b31849e9b37991d90f671e71ee7983800d
2 /*] Copyright (c) 2009-2010, Charles McGarvey [**************************
3 **] All rights reserved.
7 * Distributable under the terms and conditions of the 2-clause BSD license;
8 * see the file COPYING for a complete text of the license.
10 **************************************************************************/
15 #include <sys/inotify.h>
16 #include <sys/ioctl.h>
21 #include <boost/algorithm/string.hpp>
22 #include <boost/weak_ptr.hpp>
23 #include <stlplus/portability/file_system.hpp>
26 #include "resource.hh"
36 void resource::print_types()
40 void resource::manage_loader(const std::string
& extension
, loader_ptr
& loader
, bool set
)
42 static type_lookup lookup
;
46 lookup
[extension
] = loader
;
50 std::map
<std::string
,loader_ptr
>::iterator it
;
51 it
= lookup
.find(extension
);
52 if (it
!= lookup
.end()) loader
= (*it
).second
;
56 static std::string search_paths_
;
58 typedef boost::weak_ptr
<resource
> resource_weakptr
;
59 static hash
<std::string
,resource_weakptr
,hash_function
> resource_table_
;
62 //resource::type_lookup_ptr resource::type_lookup_;
63 //resource::type_lookup resource::type_lookup_;
67 static hash
<int,std::string
,hash_function
> monitor_lookup_
;
68 static int monitor_fd_
= inotify_init1(IN_NONBLOCK
);
71 int resource::reload_as_needed()
73 int num_resources
= 0;
76 log_info("hotloading?");
79 if (0 < (num_bytes
= read(monitor_fd_
, bytes
, num_bytes
)))
81 char* end
= bytes
+ num_bytes
;
84 log_warning("num_bytes:", num_bytes
);
89 struct inotify_event
* event
= (struct inotify_event
*)byte
;
91 if (event
->mask
& IN_IGNORED
)
93 log_warning("watch", event
->wd
, "removed");
97 hash
<int,std::string
,hash_function
>::iterator it
;
98 it
= monitor_lookup_
.find(event
->wd
);
99 if (it
!= monitor_lookup_
.end())
102 std::string path
= (*it
).second
;
103 monitor_lookup_
.erase(it
);
104 resource::reload(path
);
107 byte
+= sizeof(*event
) + event
->len
;
114 return num_resources
;
118 resource::~resource()
120 #ifdef USE_HOTLOADING
121 inotify_rm_watch(monitor_fd_
, wd_
);
126 resource_ptr
resource::load(const std::string
& path
)
128 std::string extension
= stlplus::extension_part(path
);
130 std::string path1
= path
;
133 log_error("trying to load missing resource:", path1
);
134 return resource_ptr();
137 hash
<std::string
,resource_weakptr
,hash_function
>::iterator it
;
138 it
= resource_table_
.find(path1
);
139 if (it
!= resource_table_
.end())
141 resource_weakptr rsrc
= (*it
).second
;
142 resource_ptr locked
= rsrc
.lock();
143 if (locked
) return locked
;
147 manage_loader(extension
, loader
);
150 resource_ptr
rsrc(loader
->load(path1
));
151 rsrc
->set_loader(path1
, loader
);
152 resource_table_
[path1
] = rsrc
;
154 #ifdef USE_HOTLOADING
155 int wd
= inotify_add_watch(monitor_fd_
, path1
.c_str(), IN_MODIFY
);
156 rsrc
->set_watch_descriptor(wd
);
157 monitor_lookup_
[wd
] = path1
;
160 log_info("loaded", rsrc
.get());
164 return resource_ptr();
168 resource_ptr
resource::reload(std::string
& path
)
170 log_info("reloading...", path
);
171 hash
<std::string
,resource_weakptr
,hash_function
>::iterator it
;
172 it
= resource_table_
.find(path
);
173 if (it
!= resource_table_
.end())
175 resource_weakptr rsrc
= (*it
).second
;
176 resource_ptr locked
= rsrc
.lock();
187 void resource::reload()
189 log_info("reloaded", path_
);
191 resource
* resource
= loader_
->load(path_
);
193 resource_
= resource
->resource_
;
194 typeinfo_
= resource
->typeinfo_
;
195 unloader_
= resource
->unloader_
;
197 #ifdef USE_HOTLOADING
198 int wd
= inotify_add_watch(monitor_fd_
, path_
.c_str(), IN_MODIFY
);
199 set_watch_descriptor(wd
);
200 monitor_lookup_
[wd
] = path_
;
207 void resource::add_search_paths(const std::string
& paths
)
209 search_paths_
= paths
;
213 bool resource::find(const std::string
& path
)
215 //std::string file = stlplus::lookup(path, search_paths_, ":");
216 //log_info("found file", file, "in", search_paths_);
217 //return !stlplus::lookup(path, search_paths_, ":").empty();
218 return find_file(path
) != "";
221 std::string
resource::find_file(const std::string
& name
)
223 //log_info("looking for", name, "in", search_paths_);
224 //return stlplus::lookup(name, search_paths_, ":");
226 std::vector
<std::string
> paths
;
227 boost::split(paths
, search_paths_
, boost::is_any_of(":"));
229 std::vector
<std::string
>::iterator it
;
230 for (it
= paths
.begin(); it
!= paths
.end(); ++it
)
234 log_info("looking for", name
, "in", *it
);
235 if (stlplus::file_exists(*it
)) return *it
;
238 return std::string();
241 FILE* resource::open_file(const std::string
& path
, const std::string
& mode
)
243 return fopen(find_file(path
).c_str(), mode
.c_str());
This page took 0.045442 seconds and 3 git commands to generate.