Rev 1603 | Rev 1609 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 1603 | Rev 1604 | ||
|---|---|---|---|
| 1 | 1 | ||
| 2 | var modules = new Array(); |
2 | var modules = new Array(); |
| 3 | 3 | ||
| 4 | function GetModule(name) |
4 | function GetModule(name) |
| 5 | { |
5 | { |
| 6 | if (typeof modules[name] == 'undefined') |
6 | if (typeof modules[name] == 'undefined') |
| 7 | { |
7 | { |
| 8 | LoadScript("module/" + name + ".js"); |
8 | LoadScript("module/" + name + ".js"); |
| 9 | 9 | ||
| 10 | if (eval("typeof " + name + " != 'function'")) |
10 | if (eval("typeof " + name + " != 'function'")) |
| 11 | { |
11 | { |
| 12 | Log("Failed to load " + name + " module."); |
12 | Log("Failed to load " + name + " module."); |
| 13 | return; |
13 | return; |
| 14 | } |
14 | } |
| 15 | 15 | ||
| 16 | modules[name] = eval("(new " + name + "('" + name + "'))"); |
16 | modules[name] = eval("(new " + name + "('" + name + "'))"); |
| 17 | } |
17 | } |
| 18 | 18 | ||
| 19 | return modules[name]; |
19 | return modules[name]; |
| 20 | } |
20 | } |
| 21 | 21 | ||
| 22 | function Module_OnChange(full_id, available) |
22 | function Module_OnChange(full_id, available) |
| 23 | { |
23 | { |
| 24 | var parts = full_id.split(":", 2); |
24 | var parts = full_id.split(":", 2); |
| - | 25 | ||
| - | 26 | if (legacy) |
|
| - | 27 | { |
|
| - | 28 | legacyOnState(full_id, parts[1], parts[0], available); |
|
| - | 29 | return; |
|
| - | 30 | } |
|
| 25 | 31 | ||
| 26 | var module = GetModule(parts[0]); |
32 | var module = GetModule(parts[0]); |
| 27 | 33 | ||
| 28 | if (module) |
34 | if (module) |
| 29 | { |
35 | { |
| 30 | module.SetStatus(parts[1], available); |
36 | module.SetStatus(parts[1], available); |
| 31 | } |
37 | } |
| 32 | } |
38 | } |
| 33 | 39 | ||
| 34 | function Module_OnMessage() |
40 | function Module_OnMessage() |
| 35 | { |
41 | { |
| 36 | var full_id = arguments[0]; |
42 | var full_id = arguments[0]; |
| 37 | var command = arguments[1]; |
43 | var command = arguments[1]; |
| 38 | var number_of_variables = arguments[2]; |
44 | var number_of_variables = arguments[2]; |
| 39 | 45 | ||
| 40 | var variables = new Array(); |
46 | var variables = new Array(); |
| 41 | 47 | ||
| 42 | for (var i = 3; i < number_of_variables * 2 + 3; i += 2) |
48 | for (var i = 3; i < number_of_variables * 2 + 3; i += 2) |
| 43 | { |
49 | { |
| 44 | variables[arguments[i]] = arguments[i + 1]; |
50 | variables[arguments[i]] = arguments[i + 1]; |
| 45 | } |
51 | } |
| 46 | 52 | ||
| 47 | variables.length = number_of_variables; |
53 | variables.length = number_of_variables; |
| 48 | 54 | ||
| 49 | var parts = full_id.split(":", 2); |
55 | var parts = full_id.split(":", 2); |
| - | 56 | ||
| - | 57 | if (legacy) |
|
| - | 58 | { |
|
| - | 59 | legacyOnMessage(full_id, parts[1], parts[0], command, variables); |
|
| - | 60 | return; |
|
| - | 61 | } |
|
| - | 62 | ||
| 50 | 63 | ||
| 51 | var module = GetModule(parts[0]); |
64 | var module = GetModule(parts[0]); |
| 52 | 65 | ||
| 53 | if (module) |
66 | if (module) |
| 54 | { |
67 | { |
| 55 | module.ReceiveMessage(parts[1], command, variables); |
68 | module.ReceiveMessage(parts[1], command, variables); |
| 56 | } |
69 | } |
| 57 | else |
70 | else |
| 58 | { |
71 | { |
| 59 | //Log("No one to receive this message, ignoring..."); |
72 | //Log("No one to receive this message, ignoring..."); |
| 60 | } |
73 | } |
| 61 | } |
74 | } |
| 62 | 75 | ||
| 63 | function SendModuleMessage(full_id, command, variables) |
76 | function SendModuleMessage(full_id, command, variables) |
| 64 | { |
77 | { |
| 65 | var args = new Array(); |
78 | var args = new Array(); |
| 66 | 79 | ||
| 67 | args[args.length] = full_id; |
80 | args[args.length] = full_id; |
| 68 | args[args.length] = command; |
81 | args[args.length] = command; |
| 69 | args[args.length] = variables.length; |
82 | args[args.length] = variables.length; |
| 70 | 83 | ||
| 71 | for (var i = 0; i < variables.length; i++) |
84 | for (var i = 0; i < variables.length; i++) |
| 72 | { |
85 | { |
| 73 | var obj = variables[i]; |
86 | var obj = variables[i]; |
| 74 | 87 | ||
| 75 | for (var key in obj) |
88 | for (var key in obj) |
| 76 | { |
89 | { |
| 77 | args[args.length] = key; |
90 | args[args.length] = key; |
| 78 | args[args.length] = obj[key]; |
91 | args[args.length] = obj[key]; |
| 79 | } |
92 | } |
| 80 | } |
93 | } |
| 81 | 94 | ||
| 82 | Module_SendMessage.apply(null, Array.prototype.slice.call(args, 0)); |
95 | Module_SendMessage.apply(null, Array.prototype.slice.call(args, 0)); |
| 83 | } |
96 | } |
| 84 | 97 | ||
| 85 | 98 | ||
| 86 | function Module(name) |
99 | function Module(name) |
| 87 | { |
100 | { |
| 88 | this.name_ = name; |
101 | this.name_ = name; |
| 89 | this.ids_ = new Array(); |
102 | this.ids_ = new Array(); |
| 90 | 103 | ||
| 91 | this.EventBase(); |
104 | this.EventBase(); |
| 92 | } |
105 | } |
| 93 | 106 | ||
| 94 | Extend(Module, EventBase); |
107 | Extend(Module, EventBase); |
| 95 | 108 | ||
| 96 | Module.prototype.name_; |
109 | Module.prototype.name_; |
| 97 | Module.prototype.ids_; |
110 | Module.prototype.ids_; |
| 98 | 111 | ||
| 99 | Module.prototype.SetStatus = function(id, available) |
112 | Module.prototype.SetStatus = function(id, available) |
| 100 | { |
113 | { |
| 101 | if (typeof this.ids_[id] == 'undefined') |
114 | if (typeof this.ids_[id] == 'undefined') |
| 102 | { |
115 | { |
| 103 | this.ids_[id] = available; |
116 | this.ids_[id] = available; |
| 104 | this.EventBase.prototype.Trigger.call(this, "onStatusChange", id, available); |
117 | this.EventBase.prototype.Trigger.call(this, "onStatusChange", id, available); |
| 105 | } |
118 | } |
| 106 | else if (this.ids[id] != available) |
119 | else if (this.ids[id] != available) |
| 107 | { |
120 | { |
| 108 | this.EventBase.prototype.Trigger.call(this, "onStatusChange", id, available); |
121 | this.EventBase.prototype.Trigger.call(this, "onStatusChange", id, available); |
| 109 | } |
122 | } |
| 110 | } |
123 | } |
| 111 | 124 | ||
| 112 | Module.prototype.ReceiveMessage = function(id, command, variables) |
125 | Module.prototype.ReceiveMessage = function(id, command, variables) |
| 113 | { |
126 | { |
| 114 | - | ||
| - | 127 | Log("received " + command); |
|
| 115 | } |
128 | } |
| 116 | 129 | ||
| 117 | Module.prototype.GetAvailableIds = function() |
130 | Module.prototype.GetAvailableIds = function() |
| 118 | { |
131 | { |
| 119 | var ids = new Array(); |
132 | var ids = new Array(); |
| 120 | 133 | ||
| 121 | for (var id in this.ids_) |
134 | for (var id in this.ids_) |
| 122 | { |
135 | { |
| 123 | if (this.ids_[id]) |
136 | if (this.ids_[id]) |
| 124 | { |
137 | { |
| 125 | ids[ids.length] = id; |
138 | ids[ids.length] = id; |
| 126 | } |
139 | } |
| 127 | } |
140 | } |
| 128 | 141 | ||
| 129 | return ids; |
142 | return ids; |
| 130 | } |
143 | } |
| 131 | 144 | ||
| 132 | Module.prototype.IsAvailable = function(id) |
145 | Module.prototype.IsAvailable = function(id) |
| 133 | { |
146 | { |
| 134 | if (typeof this.ids_[id] == 'undefined' || this.ids_[id] == false) |
147 | if (typeof this.ids_[id] == 'undefined' || this.ids_[id] == false) |
| 135 | { |
148 | { |
| 136 | return false; |
149 | return false; |
| 137 | } |
150 | } |
| 138 | 151 | ||
| 139 | return true; |
152 | return true; |
| 140 | } |
153 | } |
| 141 | 154 | ||
| 142 | Module.prototype.SendMessage = function(id, command, variables) |
155 | Module.prototype.SendMessage = function(id, command, variables) |
| 143 | { |
156 | { |
| 144 | if (this.IsAvailable(id)) |
157 | if (this.IsAvailable(id)) |
| 145 | { |
158 | { |
| 146 | SendModuleMessage(this.name_ + ":" + id, command, variables); |
159 | SendModuleMessage(this.name_ + ":" + id, command, variables); |
| 147 | } |
160 | } |
| 148 | } |
161 | } |
| 149 | 162 | ||