-
Notifications
You must be signed in to change notification settings - Fork 3
/
Image_Writer.txt
198 lines (181 loc) · 4.57 KB
/
Image_Writer.txt
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
@name Image Writer
@inputs DS:wirelink
@outputs Percent Buf State:string
@persist AI Retry Buffer
@persist [E O]:entity [Ar]:array [Img URL_PHP S]:string [I Length]
@persist [PROGRAM_NAME_SHORT]:string
@persist SPD
@persist [LS LS1 LS2]:string
#UNUSED
#@persist PROGRAM_NAME:string RGB:vector X Fin
#@model models/hunter/blocks/cube05x05x05.mdl
#Revision 2
#CHANGES
# Err handling
# Chat commands
# Minor fixes incl. percent
# Lenient url chk
# States
#TODO
# Fast mode
if(first())
{
runOnHTTP(1)
runOnChat(1)
E = entity()
O = owner()
Retry = 0
URL_PHP = "https://.../image.php"
SPD = 2 #*100%
#PROGRAM_NAME = "Image writer"
PROGRAM_NAME_SHORT = "Imgw"
function printStd(Prefix:string, Msg:string)
{
print(
"[" + PROGRAM_NAME_SHORT +
":" + Prefix + "] " +Msg
)
}
function printErr(Msg:string){printStd("Err", Msg)}
function setState(S:string)
{
State = S
printStd("State",S)
}
if(DS)
{
E:deleteWire("DS")
printStd("Info","DS WL was removed for safety reasons")
}
}
Req = httpCanRequest()
#Fin = changed(Req)&Req
if(chatClk(O)){
LS = O:lastSaid()
Exp = LS:explode(" ")
LS1 = Exp[1, string]
LS2 = Exp[2, string]
switch (LS1)
{
case "img",
hideChat(1)
if(DS)
{
if(LS2:sub(1, 4) == "http")
{
if(State == "Finished!" | State == "")
{
Img = LS2
httpRequest(Img)
timer("checkImg",1)
setState("Checking Image")
}
else
{
printErr("E2 not ready")
}
}
else
{
printErr("Invalid URL!")
}
}
else
{
printErr("No DS detected")
}
break
case "ready",
hideChat(1)
printStd("Info","httpCanRequest: "+Req:toString())
case "state",
hideChat(1)
printStd("Info",State)
break
}
}
if(clk("checkImg")) {
timer("checkImg",1)
if(Req) {
stoptimer("checkImg")
S = httpData()
if(S) {
URL = URL_PHP + format("?image=%s",Img)
httpRequest(URL)
setState("Requested URL! " + URL)
timer("getImg",1)
}
else
{
printErr("Failed! File might not exist!")
}
}
}
if(clk("getImg") | Retry == 1) {
timer("getImg",1)
if(Req) {
stoptimer("getImg")
httpRequest(URL)
S = httpData()
#X = S:find("x")
#print(X)
if(S) {
Col = S:find(":")
Dim = S:sub(1,Col-1):explode("x")
W = Dim[1,string]:toNumber()
H = Dim[2,string]:toNumber()
S = S:sub(Col+1)
#print("W: " + W + " H: " + H)
Length = S:length()
#Buffer = round(Length/W) #/12
Buffer = W*SPD
AI = I = Buf = 0
Ar = array()
DS[1048576]=1
DS[1048569]=2 #Color Mode
DS[1048572]=H #Screen Height H
DS[1048573]=W #Screen Width W
DS[1048574]=1 #Clear Screen
if(W == 0 | H == 0){
Retry = 0
printErr("Wrong dimensions! W: "+W+" H: "+H)
State = ""
}else{
Retry = 0
printStd("Info",W+"x"+H+" IMAGE LOADING")
timer("writeImg",1)
setState("Started writing!")
}
}
else{
setState("Waiting on response from: " + URL_PHP)
Retry = 1
}
}
}
if(clk("writeImg")) {
timer("writeImg",1)
while(perf()&I < Length) {
#print("Wrote: " + I + " of: " + Length)
I += 3
AI += 1
#2: R*65536 + G*256 + B;
Color = (toByte(S,I-2)-16)*65536+(toByte(S,I-1)-16)*256+(toByte(S,I)-16)
#RGB = vec((toByte(S,I-2)-16), (toByte(S,I-1)-16), (toByte(S,I)-16))
Ar[AI,number] = Color
if(AI >= Buffer) {
DS:writeArray(Buf,Ar)
AI = 0
Ar = array()
Buf += Buffer
}
}
if(I == Length) {
S = ""
DS:writeArray(Buf,Ar)
stoptimer("writeImg")
E:deleteWire("DS")
setState("Finished!")
}
Percent = int(I*100/Length)
}