Rev 1610 | Rev 1619 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 1610 | Rev 1614 | ||
|---|---|---|---|
| 1 | 1 | ||
| 2 | var modules = new Array(); |
2 | var modules = new Array(); |
| - | 3 | var module_aliases = new Array(); |
|
| - | 4 | ||
| - | 5 | function CreateModuleAlias(alias, module_name, module_id, extra) |
|
| - | 6 | { |
|
| - | 7 | module_aliases[alias] = { "is_group" : false, "module_name" : module_name, "module_id" : module_id, "extra" : extra }; |
|
| - | 8 | } |
|
| - | 9 | ||
| - | 10 | function CreateModuleAliasGroup(alias, aliases) |
|
| - | 11 | { |
|
| - | 12 | module_aliases[alias] = { "is_group" : true, "aliases" : aliases }; |
|
| - | 13 | } |
|
| 3 | 14 | ||
| 4 | function GetModule(name) |
15 | function GetModule(name) |
| 5 | { |
16 | { |
| 6 | if (typeof modules[name] == 'undefined') |
17 | if (typeof modules[name] == 'undefined') |
| 7 | { |
18 | { |
| 8 | LoadScript("module/" + name + ".js"); |
19 | LoadScript("module/" + name + ".js"); |
| 9 | 20 | ||
| 10 | if (eval("typeof " + name + " != 'function'")) |
21 | if (eval("typeof " + name + " != 'function'")) |
| 11 | { |
22 | { |
| 12 | Log("Failed to load " + name + " module."); |
23 | Log("Failed to load " + name + " module."); |
| 13 | return; |
24 | return; |
| 14 | } |
25 | } |
| 15 | 26 | ||
| 16 | modules[name] = eval("(new " + name + "('" + name + "'))"); |
27 | modules[name] = eval("(new " + name + "('" + name + "'))"); |
| 17 | } |
28 | } |
| 18 | 29 | ||
| 19 | return modules[name]; |
30 | return modules[name]; |
| 20 | } |
31 | } |
| 21 | 32 | ||
| 22 | function Module_OnChange(full_id, available) |
33 | function Module_OnChange(full_id, available) |
| 23 | { |
34 | { |
| 24 | var parts = full_id.split(":", 2); |
35 | var parts = full_id.split(":", 2); |
| 25 | 36 | ||
| 26 | if (global_legacy) |
37 | if (global_legacy) |
| 27 | { |
38 | { |
| 28 | legacyOnState(full_id, parts[1], parts[0], available); |
39 | legacyOnState(full_id, parts[1], parts[0], available); |
| 29 | return; |
40 | return; |
| 30 | } |
41 | } |
| 31 | 42 | ||
| 32 | var module = GetModule(parts[0]); |
43 | var module = GetModule(parts[0]); |
| 33 | 44 | ||
| 34 | if (module) |
45 | if (module) |
| 35 | { |
46 | { |
| 36 | module.SetStatus(parts[1], available); |
47 | module.SetStatus(parts[1], available); |
| 37 | } |
48 | } |
| 38 | } |
49 | } |
| 39 | 50 | ||
| 40 | function Module_OnMessage() |
51 | function Module_OnMessage() |
| 41 | { |
52 | { |
| 42 | var full_id = arguments[0]; |
53 | var full_id = arguments[0]; |
| 43 | var command = arguments[1]; |
54 | var command = arguments[1]; |
| 44 | var number_of_variables = arguments[2]; |
55 | var number_of_variables = arguments[2]; |
| 45 | 56 | ||
| 46 | var variables = new Array(); |
57 | var variables = new Array(); |
| 47 | 58 | ||
| 48 | for (var i = 3; i < number_of_variables * 2 + 3; i += 2) |
59 | for (var i = 3; i < number_of_variables * 2 + 3; i += 2) |
| 49 | { |
60 | { |
| 50 | variables[arguments[i]] = arguments[i + 1]; |
61 | variables[arguments[i]] = arguments[i + 1]; |
| 51 | } |
62 | } |
| 52 | 63 | ||
| 53 | variables.length = number_of_variables; |
64 | variables.length = number_of_variables; |
| 54 | 65 | ||
| 55 | var parts = full_id.split(":", 2); |
66 | var parts = full_id.split(":", 2); |
| 56 | 67 | ||
| 57 | if (global_legacy) |
68 | if (global_legacy) |
| 58 | { |
69 | { |
| 59 | legacyOnMessage(full_id, parts[1], parts[0], command, variables); |
70 | legacyOnMessage(full_id, parts[1], parts[0], command, variables); |
| 60 | return; |
71 | return; |
| 61 | } |
72 | } |
| 62 | 73 | ||
| 63 | 74 | ||
| 64 | var module = GetModule(parts[0]); |
75 | var module = GetModule(parts[0]); |
| 65 | 76 | ||
| 66 | if (module) |
77 | if (module) |
| 67 | { |
78 | { |
| 68 | module.ReceiveMessage(parts[1], command, variables); |
79 | module.ReceiveMessage(parts[1], command, variables); |
| 69 | } |
80 | } |
| 70 | else |
81 | else |
| 71 | { |
82 | { |
| 72 | //Log("No one to receive this message, ignoring..."); |
83 | //Log("No one to receive this message, ignoring..."); |
| 73 | } |
84 | } |
| 74 | } |
85 | } |
| 75 | 86 | ||
| 76 | function SendModuleMessage(full_id, command, variables) |
87 | function SendModuleMessage(full_id, command, variables) |
| 77 | { |
88 | { |
| 78 | var args = new Array(); |
89 | var args = new Array(); |
| 79 | 90 | ||
| 80 | args[args.length] = full_id; |
91 | args[args.length] = full_id; |
| 81 | args[args.length] = command; |
92 | args[args.length] = command; |
| 82 | args[args.length] = variables.length; |
93 | args[args.length] = variables.length; |
| 83 | 94 | ||
| 84 | for (var i = 0; i < variables.length; i++) |
95 | for (var i = 0; i < variables.length; i++) |
| 85 | { |
96 | { |
| 86 | var obj = variables[i]; |
97 | var obj = variables[i]; |
| 87 | 98 | ||
| 88 | for (var key in obj) |
99 | for (var key in obj) |
| 89 | { |
100 | { |
| 90 | args[args.length] = key; |
101 | args[args.length] = key; |
| 91 | args[args.length] = obj[key]; |
102 | args[args.length] = obj[key]; |
| 92 | } |
103 | } |
| 93 | } |
104 | } |
| 94 | 105 | ||
| 95 | Module_SendMessage.apply(null, Array.prototype.slice.call(args, 0)); |
106 | Module_SendMessage.apply(null, Array.prototype.slice.call(args, 0)); |
| 96 | } |
107 | } |
| 97 | 108 | ||
| 98 | 109 | ||
| 99 | function Module(name) |
110 | function Module(name) |
| 100 | { |
111 | { |
| 101 | this.name_ = name; |
112 | this.name_ = name; |
| 102 | this.ids_ = new Array(); |
113 | this.ids_ = new Array(); |
| 103 | 114 | ||
| 104 | this.EventBase(); |
115 | this.EventBase(); |
| 105 | } |
116 | } |
| 106 | 117 | ||
| 107 | Extend(Module, EventBase); |
118 | Extend(Module, EventBase); |
| 108 | 119 | ||
| 109 | Module.prototype.name_; |
120 | Module.prototype.name_; |
| 110 | Module.prototype.ids_; |
121 | Module.prototype.ids_; |
| 111 | 122 | ||
| 112 | Module.prototype.SetStatus = function(id, available) |
123 | Module.prototype.SetStatus = function(id, available) |
| 113 | { |
124 | { |
| 114 | if (typeof this.ids_[id] == 'undefined') |
125 | if (typeof this.ids_[id] == 'undefined') |
| 115 | { |
126 | { |
| 116 | this.ids_[id] = available; |
127 | this.ids_[id] = available; |
| 117 | this.EventBase.prototype.Trigger.call(this, "onStatusChange", id, available); |
128 | this.EventBase.prototype.Trigger.call(this, "onStatusChange", id, available); |
| 118 | } |
129 | } |
| 119 | else if (this.ids[id] != available) |
130 | else if (this.ids[id] != available) |
| 120 | { |
131 | { |
| 121 | this.EventBase.prototype.Trigger.call(this, "onStatusChange", id, available); |
132 | this.EventBase.prototype.Trigger.call(this, "onStatusChange", id, available); |
| 122 | } |
133 | } |
| 123 | } |
134 | } |
| 124 | 135 | ||
| 125 | Module.prototype.ReceiveMessage = function(id, command, variables) |
136 | Module.prototype.ReceiveMessage = function(id, command, variables) |
| 126 | { |
137 | { |
| 127 | //Log("received " + command); |
138 | //Log("received " + command); |
| 128 | } |
139 | } |
| 129 | 140 | ||
| 130 | Module.prototype.GetAvailableIds = function() |
141 | Module.prototype.GetAvailableIds = function() |
| 131 | { |
142 | { |
| 132 | var ids = new Array(); |
143 | var ids = new Array(); |
| 133 | 144 | ||
| 134 | for (var id in this.ids_) |
145 | for (var id in this.ids_) |
| 135 | { |
146 | { |
| 136 | if (this.ids_[id]) |
147 | if (this.ids_[id] && typeof this.ids_[id] != 'function') |
| 137 | { |
148 | { |
| 138 | ids[ids.length] = id; |
149 | ids[ids.length] = id; |
| 139 | } |
150 | } |
| 140 | } |
151 | } |
| 141 | 152 | ||
| 142 | return ids; |
153 | return ids; |
| - | 154 | } |
|
| - | 155 | ||
| - | 156 | Module.prototype.GetAvailableNames = function() |
|
| - | 157 | { |
|
| - | 158 | var result = new Array(); |
|
| - | 159 | var available_ids = this.GetAvailableIds(); |
|
| - | 160 | ||
| - | 161 | for (var alias in module_aliases) |
|
| - | 162 | { |
|
| - | 163 | if (module_aliases[alias]["is_group"]) |
|
| - | 164 | { |
|
| - | 165 | for (var n = 0; n < module_aliases[alias]["aliases"].length; n++) |
|
| - | 166 | { |
|
| - | 167 | var group_alias = module_aliases[alias]["aliases"][n]; |
|
| - | 168 | ||
| - | 169 | if (module_aliases[group_alias]["module_name"] == this.name_ && available_ids.contains(module_aliases[group_alias]["module_id"])) |
|
| - | 170 | { |
|
| - | 171 | result.push(alias); |
|
| - | 172 | break; |
|
| - | 173 | } |
|
| - | 174 | } |
|
| - | 175 | } |
|
| - | 176 | else |
|
| - | 177 | { |
|
| - | 178 | if (module_aliases[alias]["module_name"] == this.name_ && available_ids.contains(module_aliases[alias]["module_id"])) |
|
| - | 179 | { |
|
| - | 180 | result.push(alias); |
|
| - | 181 | } |
|
| - | 182 | } |
|
| - | 183 | } |
|
| - | 184 | ||
| - | 185 | return result; |
|
| 143 | } |
186 | } |
| 144 | 187 | ||
| 145 | Module.prototype.IsAvailable = function(id) |
188 | Module.prototype.IsAvailable = function(id) |
| 146 | { |
189 | { |
| 147 | if (typeof this.ids_[id] == 'undefined' || this.ids_[id] == false) |
190 | if (typeof this.ids_[id] == 'undefined' || this.ids_[id] == false) |
| 148 | { |
191 | { |
| 149 | return false; |
192 | return false; |
| 150 | } |
193 | } |
| 151 | 194 | ||
| 152 | return true; |
195 | return true; |
| 153 | } |
196 | } |
| 154 | 197 | ||
| 155 | Module.prototype.SendMessage = function(id, command, variables) |
198 | Module.prototype.SendMessage = function(id, command, variables) |
| 156 | { |
199 | { |
| 157 | if (this.IsAvailable(id)) |
200 | if (this.IsAvailable(id)) |
| 158 | { |
201 | { |
| 159 | SendModuleMessage(this.name_ + ":" + id, command, variables); |
202 | SendModuleMessage(this.name_ + ":" + id, command, variables); |
| 160 | } |
203 | } |
| 161 | } |
204 | } |
| 162 | 205 | ||
| 163 | 206 | ||
| 164 | // Console Commands |
207 | // Console Commands |
| 165 | 208 | ||
| 166 | function modulelist() |
209 | function modulelist() |
| 167 | { |
210 | { |
| 168 | var result = ""; |
211 | var result = ""; |
| 169 | 212 | ||
| 170 | for (var name in modules) |
213 | for (var name in modules) |
| 171 | { |
214 | { |
| 172 | result += name + ": " + modules[name].GetAvailableIds().toString() + "\n"; |
215 | result += name + ": " + modules[name].GetAvailableIds().toString() + "\n"; |
| 173 | } |
216 | } |
| 174 | 217 | ||
| 175 | return result; |
218 | return result; |
| 176 | } |
219 | } |
| 177 | 220 | ||
| 178 | RegisterConsoleCommand(modulelist); |
221 | RegisterConsoleCommand(modulelist); |
| 179 | 222 | ||
| 180 | 223 | ||