-
Notifications
You must be signed in to change notification settings - Fork 2
/
imageloader.txt
383 lines (326 loc) · 12.4 KB
/
imageloader.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
@name imageloader
@inputs Digi:wirelink Digis:array
@persist [URL Image ImageData WantedUrl Prefix]:string Step OldPercent
@persist MaxRes:vector2 AutoScaleToNumberOfScreens NumScreenSqrt AutoTeleportScreens ScaleUp
@persist KeepAspectRatio DetectScreenRatio BackgroundColor:vector
@persist LoadShare:array ImageSize:vector2
@persist LoadShare_Start LoadShare_Stop SendLoadShare_Index
@persist LoadShare_Output_Start LoadShare_Output_Stop LoadShare_Step
@persist LinkedDigis:array
@persist DataSize ScreenSize:vector2
@persist G:gtable
if (first()|dupefinished()|~Digi|~Digis) {
#Source https://github.com/Divran/e2imageloader
#########################
#Options here
#Enter image here
#Image = "http://i.imgur.com/xn4cYl4.png?fb"
Image = "http://i.imgur.com/xn4cYl4.png"
#Or leave it empty to use chat commands
#[ To use chat commands, aim at the digital screen
that you want to change, then type the prefix
followed immediately by an URL
example:
>http://i.imgur.com/xn4cYl4.png?fb ]#
#Set max res here, image will be downscaled if it's larger
MaxRes = vec2(512,512)
# If this is enabled, will ignore the MaxRes setting above and will instead
# automatically scale the max res to 512 x ScreenNum
# If only 1 screen is connected, it will use the MaxRes value above
AutoScaleToNumberOfScreens = 1
# if enabled, teleports all screens into a grid orientation automatically
# some screens have strange rotations, so if the angles are wrong, consider turning this
# off and positioning them manually instead
#ifdef E:setPos(V)
AutoTeleportScreens = 1
#else
AutoTeleportScreens = 0
#endif
# Automatically scale up the image if it's smaller than the max resolution
ScaleUp = 0
#[ Background color.
Only works if KeepAspectRatio is enabled
Or if the image has transparency ]#
BackgroundColor = vec(255)
#[ Set to 1 to keep aspect ratio by placing a background
(change color above) around the image, if it is not square
If 0, and the image is not square, it will be stretched to fit
the whole digi screen ]#
KeepAspectRatio = 1
#[ Set to 1 to attempt to detect the ratio of the digital screen.
If you are using a digital screen that isn't square shaped
this could be useful. However, it probably won't work
for every screen, since some props are rotated differently.
This option only works if KeepAspectRatio is enabled. ]#
DetectScreenRatio = 1
#[ If your screen has a different rotation, you can try
adjusting this function.]#
function vector entity:getScreenSize() {
local Size = This:boxSize() #Get prop size
Size = Size - vec(2) #Remove a bit around the edges
#You can add more models here
local FlippedModels = table(
"models/props/cs_office/tv_plasma.mdl"=1,
"models/kobilica/wiremonitorbig.mdl"=1,
"models/kobilica/wiremonitorrt.mdl"=1,
"models/kobilica/wiremonitorrtbig.mdl"=1,
"models/kobilica/wiremonitorsmall.mdl"=1,
"models/props_phx/construct/metal_tubex2.mdl"=1
)
if (FlippedModels[This:model(),number]) {
return vec(Size:y(),Size:z(),1)
} else {
#Every other model
return vec(Size:x(),Size:y(),0)
}
}
#Change this to your server php script address
URL = "http://localhost/a/imageloader.php"
#Chat command prefix
Prefix = ">"
###########################
#No more options beyond this point
if (!->Digi & !->Digis) {
#ifdef entity:wirelink()
#[
local E = entity():isWeldedTo()
if (E:type() == "gmod_wire_digitalscreen") {
local DigiVar = entity():isWeldedTo():wirelink()
LinkedDigis = array(DigiVar)
NumScreenSqrt = 1
} else {
print("Wire up the digi screen first!")
exit()
}
]#
##[
#find all nearby screens
findIncludePlayerProps(owner())
findByClass("gmod_wire_digitalscreen")
LinkedDigis = findToArray()
for(I=1,LinkedDigis:count()) {
LinkedDigis[I,wirelink] = LinkedDigis[I,entity]:wirelink()
}
NumScreenSqrt = sqrt(LinkedDigis:count())
#]#
#else
print("Wire up the digi screen first!")
exit()
#endif
} elseif (->Digi) {
LinkedDigis = array(Digi)
NumScreenSqrt = 1
} elseif (->Digis) {
LinkedDigis = array()
for(I=1,Digis:count()) {
LinkedDigis[I,wirelink] = Digis[I,entity]:wirelink()
}
NumScreenSqrt = sqrt(LinkedDigis:count())
}
if (AutoScaleToNumberOfScreens & NumScreenSqrt>1) {
MaxRes = vec2(512 * NumScreenSqrt)
}
function number validImage(STR:string) {
return Image != "" & Image:findRE("https?://.*%..+%..+") != 0
}
if (validImage(Image)) {
timer("start",1)
}
function setRes(Width,Height) {
Width = min(Width / NumScreenSqrt,512)
Height = min(Height / NumScreenSqrt,512)
#Width = 2^ceil(log2(Width)/log2(2))
#Height = 2^ceil(log2(Height)/log2(2))
Width = ceil(Width)
Height = ceil(Height)
for(I=1,LinkedDigis:count()) {
LinkedDigis[I,wirelink][1048573] = Width
LinkedDigis[I,wirelink][1048572] = Height
}
ScreenSize = vec2(Width,Height)
}
function setDefaultOptions() {
for(I=1,LinkedDigis:count()) {
local DigiVar = LinkedDigis[I,wirelink]
DigiVar[1048573] = 512 #width
DigiVar[1048572] = 512 #height
DigiVar[1048569] = 2 #2=24-bit
DigiVar[1048574] = 1 #Clear screen
DigiVar[1048574] = 0
}
}
setDefaultOptions()
runOnHTTP(1)
runOnChat(1)
DataSize = 6
G = gTable("imageloader",0)
dsSetScope(0)
dsJoinGroup("imageloader-main")
LoadShare = dsProbe("imageloader",0)
print("Found " + LoadShare:count() + " loadsharing E2s!")
} elseif (dsClk("register")) {
LoadShare = dsProbe("imageloader",0)
print("Found " + LoadShare:count() + " loadsharing E2s!")
} elseif (chatClk(owner())) {
if (lastSaid():sub(1,Prefix:length()) == Prefix) {
Image = lastSaid():sub(Prefix:length()+1)
if (validImage(Image)) {
local Aim = owner():aimEntity()
local Found = owner():aimEntity() == entity()
if (!Found) {
for(I=1,LinkedDigis:count()) {
if (LinkedDigis[I,wirelink]:entity() == Aim) {
Found = 1
break
}
}
}
if (Found) {
timer("start",1)
}
} else {
print("Invalid image specified")
}
hideChat(1)
}
} elseif (httpClk() & httpRequestUrl() == WantedUrl) {
ImageData = httpData()
local ImageSizeEnd = ImageData:find(";",0)
local ImageSizeStr = ImageData:sub(1,ImageSizeEnd-1)
ImageData = ImageData:sub(ImageSizeEnd+1)
local ImageSizeExp = ImageSizeStr:explode("x")
local W = ImageSizeExp[1,string]:toNumber()
local H = ImageSizeExp[2,string]:toNumber()
ImageSize = vec2(W,H)
setRes(W,H)
if (LoadShare:count() > 0) {
timer("send_loadshare",1)
SendLoadShare_Index = 0
LoadShare_Step = ImageData:length() / DataSize
function number roundUp( N:number, Mult:number ) {
if (Mult == 0) {return floor(N)}
local R = N % Mult
if (R == 0) {return floor(N)}
return floor(N + Mult - R)
}
LoadShare_Step = roundUp(LoadShare_Step / (LoadShare:count() + 1),DataSize)
LoadShare_Output_Start = 0
LoadShare_Output_Stop = LoadShare_Step
LoadShare_Start = 0
LoadShare_Stop = LoadShare_Step
} else {
LoadShare_Start = 0
LoadShare_Stop = ImageData:length() / DataSize
timer("step",1)
}
G["stop",number] = 0
print("Loading image of size " + W + "x" + H)
} elseif (clk("start") | clk("check again")) {
stoptimer("step")
#we use a gtable here instead of datasignals
#because datasignals cause a lot of ops
#which crashes the e2 on servers with low ops caps
G["stop",number] = 1
#dsSend("stop","imageloader","derp")
#1048573 = set width
#1048572 = set height
#1048574 = clear screen
#1048569 = color mode
#<1048500 = RGB Data
if (!httpCanRequest()) {
timer("check again",1000)
exit()
}
setDefaultOptions()
local ScreenRatio = 1
if (DetectScreenRatio) {
local Size = LinkedDigis[1,wirelink]:entity():getScreenSize()
ScreenRatio = round(Size:x() / Size:y(),3)
if (AutoTeleportScreens) {
local IsFlipped = !Size:z()
Size += vec(1)
local Pos = vec(0,-Size:y(),Size:x()*NumScreenSqrt*0.9)
local PosStep = vec(0,Size:y(),0)
for(I=1,LinkedDigis:count()) {
LinkedDigis[I,wirelink]:entity():setPos(entity():toWorld(Pos))
LinkedDigis[I,wirelink]:entity():setAng(entity():toWorld(IsFlipped * ang(90,0,0)))
Pos += PosStep
if (I % NumScreenSqrt == 0) {
Pos -= vec(0,Size:y()*NumScreenSqrt,Size:x())
}
}
}
}
local RND = randint(1,1000)
ImageData = ""
Step = 0
OldPercent = 0
WantedUrl = URL+
"?image="+httpUrlEncode(Image)+
"&maxres="+MaxRes:x()+"x"+MaxRes:y()+
"&keepaspect="+KeepAspectRatio+
"&screenratio="+ScreenRatio+
"&bgcolor="+httpUrlEncode(format("%d,%d,%d",
BackgroundColor:x(),
BackgroundColor:y(),
BackgroundColor:z()
))+
(ScaleUp ? "&scaleup=1" : "") +
"&rnd=" + RND
#print(WantedUrl)
print("Loading image '" + Image + "'")
httpRequest(WantedUrl)
} elseif (clk("send_loadshare")) {
if (!perf()) {
timer("send_loadshare",100)
exit()
}
SendLoadShare_Index++
local E = LoadShare[SendLoadShare_Index,entity]
LoadShare_Output_Start = LoadShare_Output_Stop + 1
LoadShare_Output_Stop = LoadShare_Output_Stop + LoadShare_Step
dsSendDirect("load",E,table(
"wirelink" = LinkedDigis,
"imagedata" = ImageData,
"imagesize" = ImageSize,
"maxres" = MaxRes,
"start" = LoadShare_Output_Start,
"stop" = LoadShare_Output_Stop,
"screensize" = ScreenSize
))
if (SendLoadShare_Index < LoadShare:count()) {
timer("send_loadshare",1)
} else {
timer("step",10)
}
} elseif (clk("step")) {
while(perf()) {
local Color = ImageData:sub(Step*DataSize+1,Step*DataSize+DataSize):toNumber(16)
if (Color != 0) {
if (NumScreenSqrt == 1) {
LinkedDigis[1,wirelink][Step] = Color
} else {
local X = Step % ImageSize:x()
local Y = floor(Step / ImageSize:y())
local DigiX = floor(X / ScreenSize:x())
local DigiY = floor(Y / ScreenSize:y()) * NumScreenSqrt
LinkedDigis[DigiX+DigiY+1,wirelink][X % ScreenSize:x() + ScreenSize:x() * (Y % ScreenSize:y())] = Color
}
}
Step++
}
if (Step <= LoadShare_Stop) {
#Only print status if percent difference is greater than 25%
#And only if the image is larger than approx 256x256
if (ImageData:length() >= 589824) {
local Percent = floor((Step*DataSize+DataSize)/ImageData:length()*100)
if (Percent - OldPercent >= 25) {
print(Percent + "% done")
OldPercent = Percent
}
}
timer("step",10)
} else {
print("100% done")
}
}