… | |
… | |
51 | |
51 | |
52 | static f_plug_api gethook = cfapi_get_hooks; |
52 | static f_plug_api gethook = cfapi_get_hooks; |
53 | static f_plug_api object_set_property = cfapi_object_set_property; |
53 | static f_plug_api object_set_property = cfapi_object_set_property; |
54 | static f_plug_api object_insert = cfapi_object_insert; |
54 | static f_plug_api object_insert = cfapi_object_insert; |
55 | |
55 | |
56 | static HV *obj_cache; |
|
|
57 | static PerlInterpreter *perl; |
56 | static PerlInterpreter *perl; |
58 | |
57 | |
59 | static AV *cb_global, *cb_object, *cb_player, *cb_type, *cb_map; |
58 | static AV *cb_global, *cb_object, *cb_player, *cb_type, *cb_map; |
60 | |
59 | |
61 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
60 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
… | |
… | |
81 | |
80 | |
82 | if (!obj->self) |
81 | if (!obj->self) |
83 | obj->self = newSVptr (obj, klass); |
82 | obj->self = newSVptr (obj, klass); |
84 | |
83 | |
85 | return newSVsv (obj->self); |
84 | return newSVsv (obj->self); |
86 | } |
|
|
87 | |
|
|
88 | static void |
|
|
89 | SVptr_cache_set (void *ptr, SV *sv) |
|
|
90 | { |
|
|
91 | hv_store (obj_cache, (char *)&ptr, sizeof (ptr), sv, 0); |
|
|
92 | } |
|
|
93 | |
|
|
94 | static SV * |
|
|
95 | SVptr_cache_get (void *ptr) |
|
|
96 | { |
|
|
97 | SV **he = hv_fetch (obj_cache, (char *)&ptr, sizeof (ptr), 0); |
|
|
98 | |
|
|
99 | return he ? *he : 0; |
|
|
100 | } |
|
|
101 | |
|
|
102 | static SV * |
|
|
103 | newSVptr_cached (void *ptr, const char *klass) |
|
|
104 | { |
|
|
105 | SV *sv; |
|
|
106 | |
|
|
107 | if (!ptr) |
|
|
108 | return &PL_sv_undef; |
|
|
109 | |
|
|
110 | sv = SVptr_cache_get (ptr); |
|
|
111 | |
|
|
112 | if (!sv) |
|
|
113 | { |
|
|
114 | HV *hv = newHV (); |
|
|
115 | sv_magic ((SV *)hv, 0, PERL_MAGIC_ext, (char *)ptr, 0); |
|
|
116 | sv = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
|
|
117 | |
|
|
118 | SVptr_cache_set (ptr, sv); |
|
|
119 | } |
|
|
120 | |
|
|
121 | return newSVsv (sv); |
|
|
122 | } |
85 | } |
123 | |
86 | |
124 | static void |
87 | static void |
125 | clearSVptr (SV *sv) |
88 | clearSVptr (SV *sv) |
126 | { |
89 | { |
… | |
… | |
182 | |
145 | |
183 | //TODO: |
146 | //TODO: |
184 | inline SV *to_sv (New_Face * v) { return to_sv (v->name); } |
147 | inline SV *to_sv (New_Face * v) { return to_sv (v->name); } |
185 | inline SV *to_sv (treasurelist * v) { return to_sv (v->name); } |
148 | inline SV *to_sv (treasurelist * v) { return to_sv (v->name); } |
186 | |
149 | |
187 | inline SV *to_sv (UUID v) { |
150 | inline SV *to_sv (UUID v) |
|
|
151 | { |
188 | char buf[128]; |
152 | char buf[128]; |
189 | snprintf (buf, 128, "<1,%llx>", (unsigned long long)v.seq + UUID_SKIP * 2); |
153 | snprintf (buf, 128, "<1,%llx>", (unsigned long long)v.seq); |
190 | return newSVpv (buf, 0); |
154 | return newSVpv (buf, 0); |
191 | } |
155 | } |
192 | |
156 | |
193 | inline void sv_to (SV *sv, shstr &v) { v = SvOK (sv) ? SvPV_nolen (sv) : 0; } |
157 | inline void sv_to (SV *sv, shstr &v) { v = SvOK (sv) ? SvPV_nolen (sv) : 0; } |
194 | inline void sv_to (SV *sv, char * &v) { free (v); v = SvOK (sv) ? strdup (SvPV_nolen (sv)) : 0; } //TODO: verify that all simple pointers are strdup-managed |
158 | inline void sv_to (SV *sv, char * &v) { free (v); v = SvOK (sv) ? strdup (SvPV_nolen (sv)) : 0; } //TODO: verify that all simple pointers are strdup-managed |
… | |
… | |
220 | inline void sv_to (SV *sv, refptr<T> &v) { T *tmp; sv_to (sv, tmp); v = tmp; } |
184 | inline void sv_to (SV *sv, refptr<T> &v) { T *tmp; sv_to (sv, tmp); v = tmp; } |
221 | |
185 | |
222 | template<int N> |
186 | template<int N> |
223 | inline void sv_to (SV *sv, char (&v)[N]) { assign (v, SvPV_nolen (sv)); } |
187 | inline void sv_to (SV *sv, char (&v)[N]) { assign (v, SvPV_nolen (sv)); } |
224 | |
188 | |
225 | inline void sv_to (SV *sv, UUID &v) { |
189 | inline void sv_to (SV *sv, UUID &v) |
226 | char *sv_str = SvOK (sv) ? SvPV_nolen (sv) : 0; |
190 | { |
227 | unsigned int version = 0; |
191 | unsigned int version; |
228 | unsigned long long seq = 0; |
192 | unsigned long long seq; |
229 | |
193 | |
230 | if (!sv_str) |
|
|
231 | return; |
|
|
232 | |
|
|
233 | if (2 == sscanf (sv_str, "<%d.%llx>", &version, &seq) && version == 1) |
194 | if (2 != sscanf (SvPV_nolen (sv), "<%d.%llx>", &version, &seq) || 1 != version) |
234 | { |
195 | croak ("unparsable uuid: %s", SvPV_nolen (sv)); |
|
|
196 | |
235 | v.seq = seq; |
197 | v.seq = seq; |
236 | } |
|
|
237 | |
|
|
238 | return; |
|
|
239 | } |
198 | } |
240 | |
199 | |
241 | static SV * |
200 | static SV * |
242 | newSVdt_va (va_list &ap, data_type type) |
201 | newSVdt_va (va_list &ap, data_type type) |
243 | { |
202 | { |
… | |
… | |
922 | if (perl_parse (perl, xs_init, 2, argv, (char **)NULL) || perl_run (perl)) |
881 | if (perl_parse (perl, xs_init, 2, argv, (char **)NULL) || perl_run (perl)) |
923 | { |
882 | { |
924 | printf ("unable to initialize perl-interpreter, aborting.\n"); |
883 | printf ("unable to initialize perl-interpreter, aborting.\n"); |
925 | exit (EXIT_FAILURE); |
884 | exit (EXIT_FAILURE); |
926 | } |
885 | } |
927 | |
|
|
928 | obj_cache = newHV (); |
|
|
929 | } |
886 | } |
930 | |
887 | |
931 | void cfperl_main () |
888 | void cfperl_main () |
932 | { |
889 | { |
933 | dSP; |
890 | dSP; |