Advertisement
Guest User

Untitled

a guest
Jul 15th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.92 KB | None | 0 0
  1. # Author: Pote & TheBlueOne
  2. @name EPGReader
  3. @outputs EGP:wirelink Configuration:table AsyncTasks:table
  4.  
  5. if (first()) {
  6. function void print_info(Message:string) {
  7. printColor(vec(255,0,255), "[EGPReader] ", vec(50,50,255), "[INFO]: ", vec(180), Message)
  8. }
  9.  
  10. function void print_error(Message:string) {
  11. printColor(vec(255,0,255), "[EGPReader] ", vec(250,0,0), "[ERROR]: ", vec(180), Message)
  12. }
  13.  
  14. # Function to get the EGP wirelink.
  15. function wirelink get_egp() {
  16. return entity():isWeldedTo():wirelink()
  17. }
  18.  
  19. # Function to get configuration.
  20. function table get_config(EGP:wirelink) {
  21. local Configuration = table(
  22. "environment" = "development",
  23. "users" = array(owner()),
  24. "pages" = array("No text loaded."),
  25. "performance" = 15,
  26. "interval" = 100,
  27. "current_page" = 1,
  28. "gui_color" = vec(255),
  29. "resolution" = vec2(512),
  30. "egp_table" = table()
  31. )
  32.  
  33. # Initialize egp_table.
  34. Configuration["egp_table", table] = table(
  35. table("id" = 1, "type" = "box", "order" = 1, "position" = vec2(256), "size" = vec2(512), "color" = Configuration["gui_color", vector]/15), #### background
  36. table("id" = 2, "type" = "text_layout", "order" = 2, "position" = vec2(20, 27), "size" = vec2(472, 462)+vec2(1024), "font" = "consolas 24"), #### debug text
  37.  
  38. #top menu
  39. table("id" = 3, "type" = "box", "order" = 3, "position" = vec2(256, 6), "size" = vec2(512, 12), "color" = Configuration["gui_color", vector]/12 ), #### grabby part
  40. table("id" = 4, "type" = "box", "order" = 4, "position" = vec2(0, 0), "size" = vec2(512, 12), "color" = Configuration["gui_color", vector], "material" = "gui/gradient_up", "parent" = 3 ), #### grabby part gradient
  41. table("id" = 5, "type" = "box", "order" = 5, "position" = vec2(0, 0), "size" = vec2(90, 1), "color" = Configuration["gui_color", vector]/8, "parent" = 3 ), #### line1
  42. table("id" = 6, "type" = "box", "order" = 6, "position" = vec2(0, 4), "size" = vec2(90, 1), "color" = Configuration["gui_color", vector]/8, "parent" = 3 ), #### line2
  43. table("id" = 7, "type" = "box", "order" = 7, "position" = vec2(0, 7), "size" = vec2(512, 1), "color" = Configuration["gui_color", vector]/8, "parent" = 3 ), #### line3
  44.  
  45. #top menu hidden part
  46. table("id" = 8, "type" = "box", "order" = 8, "position" = vec2(0, -31), "size" = vec2(512, 50), "color" = Configuration["gui_color", vector]/3, "parent" = 3), #### background box
  47. table("id" = 9, "type" = "box", "order" = 9, "position" = vec2(0, -31), "size" = vec2(512, 50), "color" = Configuration["gui_color", vector]/2, "material" = "gui/gradient_down", "parent" = 3), #### background box gradient
  48.  
  49. #Bottom menu
  50. table("id" = 10, "type" = "box", "order" = 10, "position" = vec2(256, 456),"size" = vec2(512, 12), "color" = Configuration["gui_color", vector]/12 ), #### grabby part
  51. table("id" = 11, "type" = "box", "order" = 11, "position" = vec2(0, 0), "size" = vec2(512, 12), "color" = Configuration["gui_color", vector], "material" = "gui/gradient_down", "parent" = 10 ), #### grabby part gradient
  52. table("id" = 12, "type" = "box", "order" = 12, "position" = vec2(0, -3), "size" = vec2(80, 1), "color" = Configuration["gui_color", vector]/12, "parent" = 10 ), #### line1
  53. table("id" = 13, "type" = "box", "order" = 13, "position" = vec2(0, 1), "size" = vec2(90, 1), "color" = Configuration["gui_color", vector]/12, "parent" = 10 ), #### line2
  54. table("id" = 14, "type" = "box", "order" = 14, "position" = vec2(0, -6), "size" = vec2(512, 1), "color" = Configuration["gui_color", vector]/12, "parent" = 10 ), #### line3
  55.  
  56. #Bottom menu hidden part
  57. table("id" = 8, "type" = "box", "position" = vec2(0, 31), "size" = vec2(512, 50), "color" = Configuration["gui_color", vector]/3, "parent" = 10), #### background box
  58. table("id" = 9, "type" = "box", "position" = vec2(0, 31), "size" = vec2(512, 50), "color" = Configuration["gui_color", vector]/2, "material" = "gui/gradient_up", "parent" = 10) #### background box gradient
  59.  
  60. )
  61.  
  62. # Add EGP wirelink to egp_table.
  63. foreach (I:number, Object:table = Configuration["egp_table", table]) {
  64. Configuration["egp_table", table][I, table]["egp", wirelink] = EGP
  65. }
  66.  
  67. return Configuration
  68. }
  69.  
  70. # Run async task with callback.
  71. function void string:async(Parameters:table, Callback:string) {
  72. local Result = ("async_"+This)(Parameters)[table]
  73.  
  74. # Continue task later.
  75. if (Result[1, string] == "yield") {
  76. AsyncTasks:unshiftTable(table("async_"+This, Parameters, "callback_"+Callback))
  77. } else {
  78. ("callback_"+Callback)(Result)
  79. }
  80. }
  81.  
  82. # Run async task.
  83. function void string:async(Parameters:table) {
  84. local Result = ("async_"+This)(Parameters)[table]
  85.  
  86. # Continue task later.
  87. if (Result[1, string] == "yield") {
  88. AsyncTasks:unshiftTable(table("async_"+This, Parameters))
  89. }
  90. }
  91.  
  92. # Yield async task with callback.
  93. function void string:async_yield(Parameters:table, Callback:string) {
  94. AsyncTasks:unshiftTable(table("async_"+This, Parameters, "callback_"+Callback))
  95. }
  96.  
  97. # Yield async task.
  98. function void string:async_yield(Parameters:table) {
  99. AsyncTasks:unshiftTable(table("async_"+This, Parameters))
  100. }
  101.  
  102. # Runs yielded async tasks.
  103. function void async_work() {
  104. local AsyncTasksCount = AsyncTasks:count()
  105.  
  106. while (AsyncTasksCount) {
  107. local Task = AsyncTasks:popTable()
  108. local Result = Task[1, string](Task[2, table])[table]
  109.  
  110. # Continue task later.
  111. if (Result[1, string] == "yield") {
  112. AsyncTasks:unshiftTable(Task)
  113. } else {
  114. # Call the callback if task has one.
  115. if (Task:exists(3)) {
  116. Task[3, string](Result)
  117. }
  118. }
  119.  
  120. AsyncTasksCount--
  121. }
  122. }
  123.  
  124. #[ Example async task.
  125. param url string
  126. ]#
  127. function table table:async_http_request() {
  128. if (httpCanRequest()) {
  129. httpRequest(This["url", string])
  130. return table()
  131. } else {
  132. # Yield if it cannot send a request.
  133. return table("yield")
  134. }
  135. }
  136.  
  137. #[ Wraps text into multiple sections.
  138. param text string Input text.
  139. param chars number Maximum amount of chars in a line.
  140. param lines number Maximum amount of lines on a page.
  141. returns array Contains the sections of wrapped text.
  142. ]#
  143. function table table:async_wrap_text() {
  144. if (!This:exists("sections")) {
  145. if (!This:exists("text")) {
  146. print_error("Parameter text:string is missing in text wrap task.")
  147. return table()
  148. }
  149.  
  150. if (!This:exists("chars")) {
  151. print_error("Parameter chars:number is missing in text wrap task.")
  152. return table()
  153. }
  154.  
  155. if (!This:exists("lines")) {
  156. print_error("Parameter lines:number is missing in text wrap task.")
  157. return table()
  158. }
  159.  
  160. This["sections", array] = array()
  161. This["text_i", number] = 1
  162. This["section_i", number] = 1
  163. This["line_i", number] = 1
  164. }
  165.  
  166. local Text = This["text", string]
  167. local TextLength = Text:length()
  168.  
  169. local Sections = This["sections", array]
  170.  
  171. local MaxChars = This["chars", number]
  172. local MaxLines = This["lines", number]
  173.  
  174. local TextI = This["text_i", number]
  175. local SectionI = This["section_i", number]
  176. local LineI = This["line_i", number]
  177.  
  178. local SectionText = Sections[SectionI, string]
  179.  
  180. while (TextI < TextLength) {
  181. # Save task state if it cannot finish within a single tick.
  182. if (!perf()) {
  183. This["text_i", number] = TextI
  184. This["section_i", number] = SectionI
  185. Sections[SectionI, string] = SectionText
  186. This["sections", array] = Sections
  187. This["line_i", number] = LineI
  188. return table("yield")
  189. }
  190.  
  191. # Take one extra character to know whether or not the next line continues with a space.
  192. local Line = Text:sub(TextI, TextI+MaxChars):trimLeft()
  193. local LineLength = Line:length()
  194.  
  195. local Newline = Line:find("\n")
  196.  
  197. # If no newline is found.
  198. if (Newline == 0) {
  199. local LineSpace = Line:reverse():find(" ")
  200.  
  201. # If no space was found.
  202. if (LineSpace == 0) {
  203. SectionText += Line:left(MaxChars) + "\n"
  204. TextI += LineLength-1
  205. } else { # Wrap text otherwise.
  206. local WrappedLine = Line:left(LineLength-LineSpace)
  207. SectionText += WrappedLine + "\n"
  208. TextI += WrappedLine:length()+1
  209. }
  210. } else {
  211. SectionText += Line:left(Newline)
  212. TextI += Newline
  213. }
  214.  
  215. # Move to next line.
  216. LineI++
  217.  
  218. # If section reached maximum amount of lines, move on to a new section.
  219. if (LineI > MaxLines) {
  220. Sections[SectionI, string] = SectionText
  221. SectionText = ""
  222. SectionI++
  223. LineI = 1
  224. }
  225. }
  226.  
  227. Sections[SectionI, string] = SectionText
  228.  
  229. return table(Sections)
  230. }
  231.  
  232. #[ Performs operations on EGP objects.
  233. param egp wirelink The EGP wirelink.
  234. param id number The id of the object.
  235. optional param type string Creates an object of this type.
  236. optional param position vector2
  237. optional param size vector2
  238. optional param color vector4
  239. optional param angle number
  240. optional param align vector2
  241. optional param material string
  242. optional param parent number
  243. optional param text string
  244. optional param radius number
  245. optional param font string
  246. ]#
  247. function table table:async_egp() {
  248. if (!This:exists("id")) {
  249. print_error("Parameter id:number is missing in egp task.")
  250. return table()
  251. }
  252.  
  253. if (!This:exists("egp")) {
  254. print_error("Parameter egp:wirelink is missing in egp task.")
  255. return table()
  256. }
  257.  
  258. local Id = This["id", number]
  259. local EGP = This["egp", wirelink]
  260.  
  261. if (This:exists("type")) {
  262. switch (This["type", string]) {
  263. case "box",
  264. EGP:egpBox(Id, vec2(), vec2())
  265. break
  266.  
  267. case "box_outline",
  268. EGP:egpBoxOutline(Id, vec2(), vec2())
  269. break
  270.  
  271. case "circle",
  272. EGP:egpCircle(Id, vec2(), vec2())
  273. break
  274.  
  275. case "circle_outline",
  276. EGP:egpCircleOutline(Id, vec2(), vec2())
  277. break
  278.  
  279. case "text",
  280. EGP:egpText(Id, "", vec2())
  281. break
  282.  
  283. case "text_layout",
  284. EGP:egpTextLayout(Id, "", vec2(), vec2())
  285. break
  286.  
  287. case "line",
  288. EGP:egpLine(Id, vec2(), vec2())
  289. break
  290.  
  291. default,
  292. print_error("Unknown object type \""+This["type", string]+"\" to create in egp task.")
  293. }
  294. }
  295.  
  296. local Keys = This:keys()
  297. local KeysCount = Keys:count()
  298. for (I = 1, KeysCount) {
  299. local Key = Keys[I, string]
  300. switch (Key) {
  301. case "position",
  302. EGP:egpPos(Id, This["position", vector2])
  303. break
  304.  
  305. case "size",
  306. EGP:egpSize(Id, This["size", vector2])
  307. break
  308.  
  309. case "color",
  310. EGP:egpColor(Id, This["color", vector])
  311. break
  312.  
  313. case "angle",
  314. EGP:egpAngle(Id, This["angle", number])
  315. break
  316.  
  317. case "align",
  318. local Align = This["align", vector2]
  319. EGP:egpAlign(Id, Align:x(), Align:y())
  320. break
  321.  
  322. case "material",
  323. EGP:egpMaterial(Id, This["material", string])
  324. break
  325.  
  326. case "parent",
  327. EGP:egpParent(Id, This["parent", number] )
  328. break
  329.  
  330. case "text",
  331. EGP:egpSetText(Id, This["text", string])
  332. break
  333.  
  334. case "radius",
  335. EGP:egpRadius(Id, This["radius", number])
  336. break
  337.  
  338. case "font",
  339. local FontExplode = This["font", string]:explode(" ")
  340. EGP:egpFont(Id, FontExplode[1, string], FontExplode[2, string]:toNumber())
  341. break
  342.  
  343. case "order",
  344. EGP:egpOrder(Id, This["order", number])
  345. break
  346.  
  347. case "egp", case "id", case "type",
  348. break
  349.  
  350. default,
  351. print_error("Unknown parameter \""+Key+"\" in egp task.")
  352. }
  353. }
  354.  
  355. return table()
  356. }
  357.  
  358. function table table:async_run_tasks() {
  359. if (!This:exists("init")) {
  360. if (!This:exists("task")) {
  361. print_error("Parameter \"task\" is missing in run_tasks task")
  362. return table()
  363. }
  364.  
  365. This["i", number] = 1
  366. }
  367.  
  368. local Task = This["task", string]
  369. local Parameters = This["parameters", table]
  370.  
  371. local Count = This["parameters", table]:count()
  372.  
  373. local I = This["i", number]
  374. while (I <= Count) {
  375. if (!perf()) {
  376. This["i", number] = I
  377. return table("yield")
  378. }
  379.  
  380. Task:async(Parameters[I, table])
  381. I++
  382. }
  383.  
  384. return table()
  385. }
  386.  
  387. function void callback_on_text_wrapped(Result:table) {
  388. #print_info("Finished wrapping text. Result:\n"+jsonEncode(table(Result[1, array]), 1))
  389. EGP:egpSetText(2, Result[1, array][Configuration["current_page", number], string])
  390.  
  391. Configuration["pages", array] = Result[1, array]
  392. }
  393.  
  394. function void callback_on_egp_initialized(Result:table) {
  395. # Re-order egps.
  396. for (I = Configuration["egp_table", table]:count(), 1, -1) {
  397. EGP:egpOrderBelow(I, I+1)
  398. }
  399. }
  400.  
  401. EGP = get_egp()
  402. Configuration = get_config(EGP)
  403.  
  404. if (!EGP) {
  405. print_error("EGP Not Found. Self destructing...")
  406. selfDestruct()
  407. }
  408.  
  409. # Initialize EGP.
  410. EGP:egpClear()
  411. "run_tasks":async(table("task" = "egp", "parameters" = Configuration["egp_table", table]), "on_egp_initialized")
  412.  
  413. # Wrap text.
  414. local Text = "Adolf\nHitler,\n\n\n\nthe leader of Germanys Nazi Party, was one of the most powerful and notorious dictators of the 20th century. Hitler capitalized on economic woes, popular discontent and political infighting to take absolute power in Germany beginning in 1933. Germanys invasion of Poland in 1939 led to the outbreak of World War II, and by 1941 Nazi forces had occupied much of Europe. Hitlers virulent anti-Semitism and obsessive pursuit of Aryan supremacy fueled the murder of some 6 million Jews, along with other victims of the Holocaust. After the tide of war turned against him, Hitler committed suicide in a Berlin bunker in April 1945. Early Life Adolf Hitler was born on April 20, 1889, in Branau am Inn, a small Austrian town near the Austro-German frontier. After his father, Alois, retired as a state customs official, young Adolf spent most of his childhood in Linz, the capital of Upper Austria. Not wanting to follow in his fathers footsteps as a civil servant, he began struggling in secondary school and eventually dropped out. Alois died in 1903, and Adolf pursued his dream of being an artist, though he was rejected from Viennas Academy of Fine Arts."
  415. "wrap_text":async(table("text" = Text:repeat(1), "chars" = 43, "lines" = 17), "on_text_wrapped")
  416.  
  417. runOnHTTP(1)
  418. runOnChat(1)
  419. runOnKeys(owner(), 1, array("e"))
  420. }
  421.  
  422. #[ DEBUG CODE ]#
  423. if (changed(Configuration["current_page", number])) {
  424. EGP:egpSetText(2, Configuration["pages", array][Configuration["current_page", number], string])
  425. }
  426. #[ END OF DEBUG CODE ]#
  427.  
  428. if (httpClk()) {
  429.  
  430. }
  431.  
  432. if (keyClk()) {
  433.  
  434. }
  435.  
  436. if (chatClk(owner())) {
  437. local Message = owner():lastSaid():trim():explode(" ")
  438.  
  439. hideChat(1)
  440. switch (Message[1, string]) {
  441. #[
  442. Used to output and mutate the configuration table.
  443. ]#
  444. case "!config", case "!c",
  445. if (!Message:exists(2)) {
  446. print_info("Current configuration:")
  447. printColor(vec(220), Configuration:toString())
  448. break
  449. }
  450.  
  451. if (!Message:exists(3)) {
  452. print_error("Specify a value.")
  453. break
  454. }
  455.  
  456. local Keys = Message[2, string]:explode("/")
  457. local KeysCount = Keys:count()
  458. local Value = Message[3, string]
  459.  
  460. local Table = Configuration
  461. for (I = 1, KeysCount) {
  462. local Key = Keys[I, string]
  463. if (Configuration:exists(Key)) {
  464. local Type = Table:typeids()[Key, string]
  465. switch (Type) {
  466. case "t",
  467. Table = Table[Key, table]
  468. continue
  469.  
  470. case "s",
  471. Table[Key, string] = Value
  472. break 2
  473.  
  474. case "n",
  475. Table[Key, number] = Value:toNumber()
  476. break 2
  477.  
  478. default,
  479. print_error("Type \""+Type+"\" cannot be set.")
  480. break 2
  481. }
  482. } elseif (I == KeysCount) {
  483. local Type = Value:explode(":")[2, string]
  484. local Value = Value:explode(":")[1, string]
  485. switch (Type) {
  486. case "number",
  487. Table[Key, number] = Value:toNumber()
  488. break
  489.  
  490. case "string", case "",
  491. Table[Key, string] = Value
  492. break
  493.  
  494. default,
  495. print_error("Unkwown type \""+Type+"\".")
  496. }
  497. } else {
  498. print_error("That key does not exist.")
  499. }
  500. }
  501. break
  502.  
  503. default,
  504. hideChat(0)
  505. }
  506. }
  507.  
  508. async_work()
  509.  
  510. interval(Configuration["interval", number])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement