Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Author: Pote & TheBlueOne
- @name EPGReader
- @outputs EGP:wirelink Configuration:table AsyncTasks:table
- if (first()) {
- function void print_info(Message:string) {
- printColor(vec(255,0,255), "[EGPReader] ", vec(50,50,255), "[INFO]: ", vec(180), Message)
- }
- function void print_error(Message:string) {
- printColor(vec(255,0,255), "[EGPReader] ", vec(250,0,0), "[ERROR]: ", vec(180), Message)
- }
- # Function to get the EGP wirelink.
- function wirelink get_egp() {
- return entity():isWeldedTo():wirelink()
- }
- # Function to get configuration.
- function table get_config(EGP:wirelink) {
- local Configuration = table(
- "environment" = "development",
- "users" = array(owner()),
- "pages" = array("No text loaded."),
- "performance" = 15,
- "interval" = 100,
- "current_page" = 1,
- "gui_color" = vec(255),
- "resolution" = vec2(512),
- "egp_table" = table()
- )
- # Initialize egp_table.
- Configuration["egp_table", table] = table(
- table("id" = 1, "type" = "box", "order" = 1, "position" = vec2(256), "size" = vec2(512), "color" = Configuration["gui_color", vector]/15), #### background
- table("id" = 2, "type" = "text_layout", "order" = 2, "position" = vec2(20, 27), "size" = vec2(472, 462)+vec2(1024), "font" = "consolas 24"), #### debug text
- #top menu
- table("id" = 3, "type" = "box", "order" = 3, "position" = vec2(256, 6), "size" = vec2(512, 12), "color" = Configuration["gui_color", vector]/12 ), #### grabby part
- 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
- table("id" = 5, "type" = "box", "order" = 5, "position" = vec2(0, 0), "size" = vec2(90, 1), "color" = Configuration["gui_color", vector]/8, "parent" = 3 ), #### line1
- table("id" = 6, "type" = "box", "order" = 6, "position" = vec2(0, 4), "size" = vec2(90, 1), "color" = Configuration["gui_color", vector]/8, "parent" = 3 ), #### line2
- table("id" = 7, "type" = "box", "order" = 7, "position" = vec2(0, 7), "size" = vec2(512, 1), "color" = Configuration["gui_color", vector]/8, "parent" = 3 ), #### line3
- #top menu hidden part
- 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
- 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
- #Bottom menu
- table("id" = 10, "type" = "box", "order" = 10, "position" = vec2(256, 456),"size" = vec2(512, 12), "color" = Configuration["gui_color", vector]/12 ), #### grabby part
- 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
- table("id" = 12, "type" = "box", "order" = 12, "position" = vec2(0, -3), "size" = vec2(80, 1), "color" = Configuration["gui_color", vector]/12, "parent" = 10 ), #### line1
- table("id" = 13, "type" = "box", "order" = 13, "position" = vec2(0, 1), "size" = vec2(90, 1), "color" = Configuration["gui_color", vector]/12, "parent" = 10 ), #### line2
- table("id" = 14, "type" = "box", "order" = 14, "position" = vec2(0, -6), "size" = vec2(512, 1), "color" = Configuration["gui_color", vector]/12, "parent" = 10 ), #### line3
- #Bottom menu hidden part
- table("id" = 8, "type" = "box", "position" = vec2(0, 31), "size" = vec2(512, 50), "color" = Configuration["gui_color", vector]/3, "parent" = 10), #### background box
- 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
- )
- # Add EGP wirelink to egp_table.
- foreach (I:number, Object:table = Configuration["egp_table", table]) {
- Configuration["egp_table", table][I, table]["egp", wirelink] = EGP
- }
- return Configuration
- }
- # Run async task with callback.
- function void string:async(Parameters:table, Callback:string) {
- local Result = ("async_"+This)(Parameters)[table]
- # Continue task later.
- if (Result[1, string] == "yield") {
- AsyncTasks:unshiftTable(table("async_"+This, Parameters, "callback_"+Callback))
- } else {
- ("callback_"+Callback)(Result)
- }
- }
- # Run async task.
- function void string:async(Parameters:table) {
- local Result = ("async_"+This)(Parameters)[table]
- # Continue task later.
- if (Result[1, string] == "yield") {
- AsyncTasks:unshiftTable(table("async_"+This, Parameters))
- }
- }
- # Yield async task with callback.
- function void string:async_yield(Parameters:table, Callback:string) {
- AsyncTasks:unshiftTable(table("async_"+This, Parameters, "callback_"+Callback))
- }
- # Yield async task.
- function void string:async_yield(Parameters:table) {
- AsyncTasks:unshiftTable(table("async_"+This, Parameters))
- }
- # Runs yielded async tasks.
- function void async_work() {
- local AsyncTasksCount = AsyncTasks:count()
- while (AsyncTasksCount) {
- local Task = AsyncTasks:popTable()
- local Result = Task[1, string](Task[2, table])[table]
- # Continue task later.
- if (Result[1, string] == "yield") {
- AsyncTasks:unshiftTable(Task)
- } else {
- # Call the callback if task has one.
- if (Task:exists(3)) {
- Task[3, string](Result)
- }
- }
- AsyncTasksCount--
- }
- }
- #[ Example async task.
- param url string
- ]#
- function table table:async_http_request() {
- if (httpCanRequest()) {
- httpRequest(This["url", string])
- return table()
- } else {
- # Yield if it cannot send a request.
- return table("yield")
- }
- }
- #[ Wraps text into multiple sections.
- param text string Input text.
- param chars number Maximum amount of chars in a line.
- param lines number Maximum amount of lines on a page.
- returns array Contains the sections of wrapped text.
- ]#
- function table table:async_wrap_text() {
- if (!This:exists("sections")) {
- if (!This:exists("text")) {
- print_error("Parameter text:string is missing in text wrap task.")
- return table()
- }
- if (!This:exists("chars")) {
- print_error("Parameter chars:number is missing in text wrap task.")
- return table()
- }
- if (!This:exists("lines")) {
- print_error("Parameter lines:number is missing in text wrap task.")
- return table()
- }
- This["sections", array] = array()
- This["text_i", number] = 1
- This["section_i", number] = 1
- This["line_i", number] = 1
- }
- local Text = This["text", string]
- local TextLength = Text:length()
- local Sections = This["sections", array]
- local MaxChars = This["chars", number]
- local MaxLines = This["lines", number]
- local TextI = This["text_i", number]
- local SectionI = This["section_i", number]
- local LineI = This["line_i", number]
- local SectionText = Sections[SectionI, string]
- while (TextI < TextLength) {
- # Save task state if it cannot finish within a single tick.
- if (!perf()) {
- This["text_i", number] = TextI
- This["section_i", number] = SectionI
- Sections[SectionI, string] = SectionText
- This["sections", array] = Sections
- This["line_i", number] = LineI
- return table("yield")
- }
- # Take one extra character to know whether or not the next line continues with a space.
- local Line = Text:sub(TextI, TextI+MaxChars):trimLeft()
- local LineLength = Line:length()
- local Newline = Line:find("\n")
- # If no newline is found.
- if (Newline == 0) {
- local LineSpace = Line:reverse():find(" ")
- # If no space was found.
- if (LineSpace == 0) {
- SectionText += Line:left(MaxChars) + "\n"
- TextI += LineLength-1
- } else { # Wrap text otherwise.
- local WrappedLine = Line:left(LineLength-LineSpace)
- SectionText += WrappedLine + "\n"
- TextI += WrappedLine:length()+1
- }
- } else {
- SectionText += Line:left(Newline)
- TextI += Newline
- }
- # Move to next line.
- LineI++
- # If section reached maximum amount of lines, move on to a new section.
- if (LineI > MaxLines) {
- Sections[SectionI, string] = SectionText
- SectionText = ""
- SectionI++
- LineI = 1
- }
- }
- Sections[SectionI, string] = SectionText
- return table(Sections)
- }
- #[ Performs operations on EGP objects.
- param egp wirelink The EGP wirelink.
- param id number The id of the object.
- optional param type string Creates an object of this type.
- optional param position vector2
- optional param size vector2
- optional param color vector4
- optional param angle number
- optional param align vector2
- optional param material string
- optional param parent number
- optional param text string
- optional param radius number
- optional param font string
- ]#
- function table table:async_egp() {
- if (!This:exists("id")) {
- print_error("Parameter id:number is missing in egp task.")
- return table()
- }
- if (!This:exists("egp")) {
- print_error("Parameter egp:wirelink is missing in egp task.")
- return table()
- }
- local Id = This["id", number]
- local EGP = This["egp", wirelink]
- if (This:exists("type")) {
- switch (This["type", string]) {
- case "box",
- EGP:egpBox(Id, vec2(), vec2())
- break
- case "box_outline",
- EGP:egpBoxOutline(Id, vec2(), vec2())
- break
- case "circle",
- EGP:egpCircle(Id, vec2(), vec2())
- break
- case "circle_outline",
- EGP:egpCircleOutline(Id, vec2(), vec2())
- break
- case "text",
- EGP:egpText(Id, "", vec2())
- break
- case "text_layout",
- EGP:egpTextLayout(Id, "", vec2(), vec2())
- break
- case "line",
- EGP:egpLine(Id, vec2(), vec2())
- break
- default,
- print_error("Unknown object type \""+This["type", string]+"\" to create in egp task.")
- }
- }
- local Keys = This:keys()
- local KeysCount = Keys:count()
- for (I = 1, KeysCount) {
- local Key = Keys[I, string]
- switch (Key) {
- case "position",
- EGP:egpPos(Id, This["position", vector2])
- break
- case "size",
- EGP:egpSize(Id, This["size", vector2])
- break
- case "color",
- EGP:egpColor(Id, This["color", vector])
- break
- case "angle",
- EGP:egpAngle(Id, This["angle", number])
- break
- case "align",
- local Align = This["align", vector2]
- EGP:egpAlign(Id, Align:x(), Align:y())
- break
- case "material",
- EGP:egpMaterial(Id, This["material", string])
- break
- case "parent",
- EGP:egpParent(Id, This["parent", number] )
- break
- case "text",
- EGP:egpSetText(Id, This["text", string])
- break
- case "radius",
- EGP:egpRadius(Id, This["radius", number])
- break
- case "font",
- local FontExplode = This["font", string]:explode(" ")
- EGP:egpFont(Id, FontExplode[1, string], FontExplode[2, string]:toNumber())
- break
- case "order",
- EGP:egpOrder(Id, This["order", number])
- break
- case "egp", case "id", case "type",
- break
- default,
- print_error("Unknown parameter \""+Key+"\" in egp task.")
- }
- }
- return table()
- }
- function table table:async_run_tasks() {
- if (!This:exists("init")) {
- if (!This:exists("task")) {
- print_error("Parameter \"task\" is missing in run_tasks task")
- return table()
- }
- This["i", number] = 1
- }
- local Task = This["task", string]
- local Parameters = This["parameters", table]
- local Count = This["parameters", table]:count()
- local I = This["i", number]
- while (I <= Count) {
- if (!perf()) {
- This["i", number] = I
- return table("yield")
- }
- Task:async(Parameters[I, table])
- I++
- }
- return table()
- }
- function void callback_on_text_wrapped(Result:table) {
- #print_info("Finished wrapping text. Result:\n"+jsonEncode(table(Result[1, array]), 1))
- EGP:egpSetText(2, Result[1, array][Configuration["current_page", number], string])
- Configuration["pages", array] = Result[1, array]
- }
- function void callback_on_egp_initialized(Result:table) {
- # Re-order egps.
- for (I = Configuration["egp_table", table]:count(), 1, -1) {
- EGP:egpOrderBelow(I, I+1)
- }
- }
- EGP = get_egp()
- Configuration = get_config(EGP)
- if (!EGP) {
- print_error("EGP Not Found. Self destructing...")
- selfDestruct()
- }
- # Initialize EGP.
- EGP:egpClear()
- "run_tasks":async(table("task" = "egp", "parameters" = Configuration["egp_table", table]), "on_egp_initialized")
- # Wrap text.
- 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."
- "wrap_text":async(table("text" = Text:repeat(1), "chars" = 43, "lines" = 17), "on_text_wrapped")
- runOnHTTP(1)
- runOnChat(1)
- runOnKeys(owner(), 1, array("e"))
- }
- #[ DEBUG CODE ]#
- if (changed(Configuration["current_page", number])) {
- EGP:egpSetText(2, Configuration["pages", array][Configuration["current_page", number], string])
- }
- #[ END OF DEBUG CODE ]#
- if (httpClk()) {
- }
- if (keyClk()) {
- }
- if (chatClk(owner())) {
- local Message = owner():lastSaid():trim():explode(" ")
- hideChat(1)
- switch (Message[1, string]) {
- #[
- Used to output and mutate the configuration table.
- ]#
- case "!config", case "!c",
- if (!Message:exists(2)) {
- print_info("Current configuration:")
- printColor(vec(220), Configuration:toString())
- break
- }
- if (!Message:exists(3)) {
- print_error("Specify a value.")
- break
- }
- local Keys = Message[2, string]:explode("/")
- local KeysCount = Keys:count()
- local Value = Message[3, string]
- local Table = Configuration
- for (I = 1, KeysCount) {
- local Key = Keys[I, string]
- if (Configuration:exists(Key)) {
- local Type = Table:typeids()[Key, string]
- switch (Type) {
- case "t",
- Table = Table[Key, table]
- continue
- case "s",
- Table[Key, string] = Value
- break 2
- case "n",
- Table[Key, number] = Value:toNumber()
- break 2
- default,
- print_error("Type \""+Type+"\" cannot be set.")
- break 2
- }
- } elseif (I == KeysCount) {
- local Type = Value:explode(":")[2, string]
- local Value = Value:explode(":")[1, string]
- switch (Type) {
- case "number",
- Table[Key, number] = Value:toNumber()
- break
- case "string", case "",
- Table[Key, string] = Value
- break
- default,
- print_error("Unkwown type \""+Type+"\".")
- }
- } else {
- print_error("That key does not exist.")
- }
- }
- break
- default,
- hideChat(0)
- }
- }
- async_work()
- interval(Configuration["interval", number])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement