-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathConsoleCommands.as
More file actions
121 lines (95 loc) · 3.1 KB
/
ConsoleCommands.as
File metadata and controls
121 lines (95 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package {
import flash.display.MovieClip;
//import some stuff from the valve lib
import ValveLib.Globals;
import ValveLib.ResizeManager;
import flash.net.Socket;
import flash.utils.ByteArray;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.net.LocalConnection;
public class ConsoleCommands extends MovieClip{
//these three variables are required by the engine
public var gameAPI:Object;
public var globals:Object;
public var elementName:String;
public var socket:Socket;
public var socketReady:Boolean = false;
public var socketInit:Boolean = false;
public var count:uint = 0;
public var curObj:Object = null;
//constructor, you usually will use onLoaded() instead
public function ConsoleCommands() : void {
trace("[ConsoleCommands] ConsoleCommands UI Constructed!");
//socket= new Socket();
//socket.connect("127.0.0.1", 29000);
//socket.addEventListener(Event.CONNECT, socketConnect);
}
//this function is called when the UI is loaded
public function onLoaded() : void {
this.gameAPI.SubscribeToGameEvent("console_command", this.onConsoleCommand);
}
public function socketConnect(e:Event){
trace("CONNECTED");
socketReady = true;
socket.removeEventListener(Event.CONNECT, socketConnect);
var pID:int = this.globals.Players.GetLocalPlayer();
var isPlayer:Boolean = (pID >= 0 && pID <= 9)
trace(pID);
trace(isPlayer);
var id = curObj.pid;
if (id == -1 || pID == id || (id == -2 && isPlayer) || (id == -3 && !isPlayer)){
trace("WRITING");
writeToConsole(curObj.command);
}
}
public function onConsoleCommand(obj:Object){
trace("[ConsoleCommands] ConsoleCommand received: " + obj.pid + " -- " + obj.command);
curObj = obj;
if (!socketInit){
socketInit = true;
socket= new Socket();
socket.connect("127.0.0.1", 29000);
socket.addEventListener(Event.CONNECT, socketConnect);
return;
}
var pID:int = this.globals.Players.GetLocalPlayer();
var isPlayer:Boolean = (pID >= 0 && pID <= 9)
trace(pID);
trace(isPlayer);
var id = obj.pid;
if (id == -1 || pID == id || (id == -2 && isPlayer) || (id == -3 && !isPlayer)){
trace("WRITING");
writeToConsole(obj.command);
}
}
public function writeToConsole(command:String){
var dataRead:Function = (function(command:String, socket:Socket) {
return function(e:Event){
var ba:ByteArray = new ByteArray();
socket.readBytes(ba, 0, ba.bytesAvailable);
count += ba.length;
};
})(command, socket);
if (!socketReady){
trace("Socket Not Ready. Not executing the command.");
return;
}
var ba:ByteArray = new ByteArray();
var len:uint = command.length + 13;
ba.writeUTFBytes("CMND");
ba.writeUnsignedInt(0x00d20000);
ba.writeShort(len);
ba.writeShort(0x0000);
ba.writeUTFBytes(command);
ba.writeByte(0x00);
socket.writeBytes(ba, 0, ba.length);
socket.flush();
trace("WRITTEN");
}
public function onResize(re:ResizeManager) : * {
}
}
}