MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame^] | 1 | // MorgenGrauen MUDlib |
| 2 | // |
| 3 | // virtual/v_compiler.c -- a general virtual compiler object |
| 4 | // |
| 5 | // $Id: v_compiler.c 9142 2015-02-04 22:17:29Z Zesstra $ |
| 6 | |
| 7 | // principle: |
| 8 | // - inherit this object into your own 'virtual_compiler.c' |
| 9 | // - customize Validate() and CustomizeObject() for you own sake |
| 10 | // |
| 11 | // * Validate() checks if a room filename given as argument (without path) |
| 12 | // is valid and returns this filename with stripped '.c'!! |
| 13 | // * CustomizeObject() uses the previous_object()->Function() strategy to |
| 14 | // customize the standard object (for example to set a description) |
| 15 | // |
| 16 | // Properties: P_STD_OBJECT, P_COMPILER_PATH |
| 17 | |
| 18 | #pragma strict_types |
| 19 | #pragma save_types |
| 20 | #pragma range_check |
| 21 | #pragma no_clone |
| 22 | #pragma pedantic |
| 23 | |
| 24 | inherit "/std/thing/properties"; |
| 25 | |
| 26 | //#define NEED_PROTOTYPES |
| 27 | |
| 28 | #include <thing/properties.h> |
| 29 | #include <defines.h> |
| 30 | #include <v_compiler.h> |
| 31 | #include <exploration.h> |
| 32 | #include <sys_debug.h> |
| 33 | #include <living/description.h> //fuer P_PARA |
| 34 | |
| 35 | // Der VC braucht das 'alte' object_name()-basierte BLUE_NAME, da sonst das |
| 36 | // Konfigurieren der von einem VC-Objekt geclonten Clones via |
| 37 | // CustomizeObject() nicht funktioniert (load_name() ermittelt den Namen des |
| 38 | // VC-Standardobjektes) |
| 39 | #ifdef BLUE_NAME |
| 40 | #undef BLUE_NAME |
| 41 | #endif |
| 42 | #define BLUE_NAME(ob) (explode(object_name(ob),"#")[0]) |
| 43 | |
| 44 | private nosave string last_loaded_file; |
| 45 | private nosave mapping objects; |
| 46 | |
| 47 | void create() |
| 48 | { |
| 49 | ::create(); |
| 50 | seteuid(getuid()); |
| 51 | SetProp(P_STD_OBJECT, "/std/room"); |
| 52 | SetProp(P_COMPILER_PATH, sprintf("/%s/", |
| 53 | implode(old_explode(object_name(this_object()), "/")[0..<2], "/"))); |
| 54 | SetProp(P_PARA, ({}) ); // keine Para-VC-Objekte |
| 55 | objects = ([]); |
| 56 | } |
| 57 | |
| 58 | // von den erbenen VCs zu ueberschreiben... |
| 59 | // TODO: aus Standardobjekt entfernen, weil durch P_PARA und QueryValidObject |
| 60 | // obsolet. |
| 61 | int NoParaObjects() { return 0; } |
| 62 | |
| 63 | string Validate(string file) |
| 64 | { |
| 65 | if(!file) return 0; |
| 66 | if(file[<2..] == ".c") file = file[0..<3]; |
| 67 | EPMASTER->PrepareVCQuery(file); |
| 68 | return file; |
| 69 | } |
| 70 | |
| 71 | // Die Funktion bekommt einen Objektnamen uebergeben und muss entscheiden, ob |
| 72 | // dieser VC dafuer zustaendig ist, das Objekt zu generieren. Jeder Wert != 0 |
| 73 | // zaehlt als 'zustaendig'. Es ist eine Art generalisiertem Validate(). Fuer |
| 74 | // maximale Nuetzlichkeit muss diese Funktion von den erbenden VCs |
| 75 | // ueberschrieben werden. |
| 76 | public int QueryValidObject(string oname) { |
| 77 | string fname, path, *pelem; |
| 78 | int para; |
| 79 | mixed ppara; |
| 80 | |
| 81 | //erstmal Validate fragen |
| 82 | pelem=explode(oname,"/"); |
| 83 | fname=pelem[<1]; |
| 84 | if (!fname=Validate(fname)) |
| 85 | return(0); //nicht zustaendig |
| 86 | // nicht im richtigen Pfad? |
| 87 | path=sprintf("%s/",implode(pelem[0..<2],"/")); |
| 88 | if (path!=QueryProp(P_COMPILER_PATH)) |
| 89 | return(0); |
| 90 | // Para-Objekt? |
| 91 | if (sscanf(fname,"%s^%d",fname,para) > 1) { |
| 92 | if (NoParaObjects()) |
| 93 | return(0); //direkt zurueck, keine Para-Objekte |
| 94 | // bestimmte Para-Dimensionen explizit erlaubt? (Wenn P_PARA nicht |
| 95 | // gesetzt ist, sind alle erlaubt!) |
| 96 | if (ppara=QueryProp(P_PARA)) { |
| 97 | if (pointerp(ppara) && member(ppara,para)!=-1) |
| 98 | return(1); |
| 99 | else if (intp(para) && ppara==para) |
| 100 | return(1); |
| 101 | // P_PARA gesetzt, aber gewuenschtes Para nicht enthalten... |
| 102 | else return(0); |
| 103 | } |
| 104 | } |
| 105 | return(1); //fall-through, offenbar zustaendig. |
| 106 | } |
| 107 | |
| 108 | mixed CustomizeObject() |
| 109 | { |
| 110 | string file; |
| 111 | |
| 112 | if(!clonep(previous_object())) |
| 113 | return Validate(explode(BLUE_NAME(previous_object()), "/")[<1]); |
| 114 | if(stringp(last_loaded_file)) file = last_loaded_file; |
| 115 | else file = Validate(explode(BLUE_NAME(previous_object()), "/")[<1]); |
| 116 | if(!file) return 0; |
| 117 | last_loaded_file = 0; |
| 118 | return file; |
| 119 | } |
| 120 | |
| 121 | // add a new object to the object list if it compiles |
| 122 | private mixed AddObject(string file) |
| 123 | { |
| 124 | object ob; |
| 125 | string err; |
| 126 | |
| 127 | // clean up the object list |
| 128 | objects = filter_indices(objects, function int (string f) { |
| 129 | return (objectp(objects[f])); } ); |
| 130 | |
| 131 | last_loaded_file = file; |
| 132 | // register new object |
| 133 | if(ob = clone_object(QueryProp(P_STD_OBJECT))) |
| 134 | objects[file] = ob; |
| 135 | return ob; |
| 136 | } |
| 137 | |
| 138 | // try to create an object for the wanted file |
| 139 | mixed compile_object(string file) |
| 140 | { |
| 141 | // validate if the file name is a correct one |
| 142 | if(file = Validate(file)) |
| 143 | return AddObject(file); |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | // return all cloned virtual objects |
| 148 | mixed QueryObjects() |
| 149 | { |
| 150 | return m_values(objects)-({0}); |
| 151 | } |
| 152 | |
| 153 | // clean up rooms that have not been destructed yet |
| 154 | int remove() { |
| 155 | |
| 156 | if(!mappingp(objects)) return 0; |
| 157 | |
| 158 | //for(ob = QueryObjects(); sizeof(ob); ob = ob[1..]) |
| 159 | foreach(object ob: QueryObjects()) { |
| 160 | ob->remove(); |
| 161 | if(objectp(ob)) destruct(ob); |
| 162 | } |
| 163 | return 1; |
| 164 | } |
| 165 | |