· 4 years ago · Sep 06, 2021, 09:26 AM
1-- [[ Leave this parameter here to make sure that the filestream can find it ]]
2local api_user_key = nil --This is generated using the login info
3-- [[ Below this point we no longer care that much about filestream precision ]]
4
5--[[ INSTRUCTIONS
6If unstructions are unclear and you get stuck in the microwave, ping me in the Satisfactory FIN server https://discord.gg/ardXtDka | Braindrift#5880
7
8Feel free to make it better, change and share. No copyright attached
9Credit for utility functions go to liukun on gist
10https://gist.github.com/liukun
11https://gist.github.com/liukun/f9ce7d6d14fa45fe9b924a3eed5c3d99
12
13IN GAME SETUP
14A computer with:
15- EEPROM
16- CPU
17- RAM
18- Hard drive holder with a hard drive inserted
19- Internet Card
20You'll need to setup the HD so that you can use external .lua files.
21See https://docs.ficsit.app/ficsit-networks/latest/lua/examples/drive.html for examples on how to setup an HD.
22
23SCRIPT SETUP
24- In your HD folder, create a new .lua file
25- Paste this script into the file
26- Setup your main script so that it reads this script, easiest way is to use fs.doFile
27- Set the value of 'nameOfThisScript' to your filename.lua
28- Set the value of myDrive to you HD UUID.
29
30PASTEBIN: GENERATE api_user_key
31- Create a user
32- Paste your username, password, and api_dev_key (found at pastebin.com/doc_api when logged in) to their parameters in this script
33- You should now be able to start the computer and generate a user key. (this will only be done once)
34- Confirm that api_user_key got a value, it should look something like '6c6d3fe13b19bbd6e479b705df0a607f'
35
36PASTEBIN: SETUP RESOURCE REFERENCE TABLE
37- Create a new paste, private or public is up to you.
38- The paste should contain a .lua table named 'Resources' which lists the key/value pairs for your pastebin scripts.
39- Key will be used as name for the generated .lua file
40- Value is a pastebin reference key to the scripts you want to download and generate files from
41- Each entry should look something like this -> myFilename = "sTxq9Xun"
42- The paste key references can be found at its adress, for example: https://pastebin.com/7wvvszW4 where '7wvvszW4' is the key.
43
44REF TABLE TEMPLATE
45Resources = {
46 Utilities = "sTxq9Xun", --Utility Functions
47 ScreenHelpers = "tVwt4Ta" --Helpers for screens
48}
49
50Once all this is setup, you should be able to run the script again - by starting the computer in Satisfactory - which will generate a .lua file for each reference in the Resources table.
51]]
52
53-- THESE VALUES SHOULD BE FILLED IN BY YOU. ALL OF THEM ARE STRING VALUES
54local api_dev_key = nil --Your Pastebin developer key goes here. You'll find it at pastebin.com/doc_api when logged in
55local api_user_name = nil --Your Pastebin user name. Create an account at pastebin.com
56local api_user_password = nil --Your Pastebin password goes here
57local api_lib_resources = nil --Ref to a Pastebin where you keep references to all scripts you wish to load
58local nameOfThisScript = nil --The name of this script for easy accessed read/write. Don't forget the .lua file ending
59local myDrive = nil --UUID of your FIN Hard-drive.
60-- See https://docs.ficsit.app/ficsit-networks/latest/lua/examples/drive.html for examples on how to setup an HD.
61
62local fs = filesystem
63local card = computer.getPCIDevices(findClass("FINInternetCard"))[1]
64HardDrive = "/dev/" .. myDrive
65
66local nameOfRefLib = "LibResources" -- Just the name, dont add .lua or similar
67local filenamePrefix = 'pb' -- Prefix for file name generation to easily identify what files are generated
68local luaFileEnding = '.lua' -- File ending added to generated filenames
69
70-- URLs to Pastebin api calls
71local pbURLRaw = "https://pastebin.com/api/api_raw.php"
72local pbURLlogin = "https://pastebin.com/api/api_login.php"
73
74-- Error Messages
75local badDevKey_prefix = "api_dev_key is invalid, make sure you have a registered PasteBin account."
76local badDevKey_sufix = "\nLog in to pastebin.com and navigate to the API documentation where you'll find your unique developer API key.\nPaste the key to api_dev_key"
77local badUsername = "No username is set, enter your Pastebin username to api_user_name"
78local badPassword = "No password is set, enter your Pastebin password to api_user_password"
79local badLogin = "Invalid username or password. \nEnter your Pastebin username to api_user_name.\nEnter your Pastebin password to api_user_password."
80local badAccount = "Make sure you have a valid Pastebin account"
81
82-------------------- Execution of this file, is launched at the bottom of this script --------------------
83local function Execute()
84 GenerateUserKey()
85 if api_lib_resources then
86 local file = FetchPastebinFile(api_lib_resources)
87 if file then
88 SaveFile(file, nameOfRefLib)
89 LoadResources()
90 else
91 computer.panic("Pastebin reference file not found, make sure api_lib_resources is set to a valid pastebin key")
92 end
93 else
94 computer.panic("api_lib_resources not valid")
95 end
96end
97
98-------------------- Generates a user key if needed --------------------
99function GenerateUserKey()
100 if api_user_key == nil then
101 if api_dev_key == nil then
102 computer.panic(badDevKey_prefix .. badDevKey_sufix)
103 elseif api_user_name == nil then
104 computer.panic(badUsername)
105 elseif api_user_password == nil then
106 computer.panic(badPassword)
107 end
108
109 local bodyGetUserKey = 'api_dev_key=' .. api_dev_key .. '&api_user_name=' .. api_user_name .. '&api_user_password=' .. urlEncode(api_user_password)
110 local request = card:request(pbURLlogin, "POST", bodyGetUserKey, "Content-Type", "Application/x-www-form-urlencoded")
111 local _, data = request:await()
112
113 if data == "Bad API request, use POST request, not GET" then
114 computer.panic(data)
115 elseif data == "Bad API request, invalid api_dev_key" then
116 computer.panic(data, badDevKey_prefix .. badDevKey_sufix)
117 elseif data == "Bad API request, invalid login" then
118 computer.panic(data .. badLogin)
119 elseif data == "Bad API request, account not active" then
120 computer.panic(data, badAccount)
121 elseif data == "Bad API request, invalid POST parameters" then
122 computer.panic(data)
123 end
124
125 if nameOfThisScript == nil then
126 computer.panic("nameOfThisScript need to be set to the name of this script")
127 end
128
129 -- Read this file and add new api_user_key value
130 local file = filesystem.open("/" .. nameOfThisScript, "r")
131 all = WriteUserKey(file, data)
132 file:close()
133 api_user_key = tostring(data)
134
135 -- Overwrite this file with new api_user_key
136 file = fs.open("/".. nameOfThisScript, "w")
137 file:write(all)
138 file:close()
139 print("Userkey generated:", api_user_key, "\nUser key is stored in", nameOfThisScript)
140 else
141 print("Userkey found, continuing execution")
142 end
143end
144
145-------------------- Parse through file and add key value to api_user_key --------------------
146function WriteUserKey(filestream, key)
147 local buf = ""
148 local out
149 while true do
150 out = filestream:read(256) -- 256 is chunk size
151
152 if out then
153 if string.find(out, "api_user_key") then
154 local pattern = "api_user_key" .. " = nil" -- broken up to differantiate this instance from the instance we want
155 out = string.gsub(out, pattern, "api_user_key = \"" .. key .. "\"")
156 end
157 buf = buf .. out
158
159 else
160 break
161 end
162 end
163 return buf
164end
165
166-------------------- Retrieve a pastebin file specified by api_file_key --------------------
167function FetchPastebinFile(api_file_key)
168 local bodyGetRaw = 'api_option=show_paste&api_user_key=' .. api_user_key .. '&api_dev_key=' .. api_dev_key .. '&api_paste_key=' .. urlEncode(api_file_key)
169 local request = card:request(pbURLRaw, "POST", bodyGetRaw, "Content-Type", "Application/x-www-form-urlencoded")
170 local _, data = request:await()
171
172 if data == "Bad API request, invalid api_option" then
173 computer.panic(data .. ":", "Has bodyGetRaw been altered?")
174 elseif data == "Bad API request, invalid api_dev_key" then
175 computer.panic(data .. ":", badDevKey_prefix .. badDevKey_sufix)
176 elseif data == "Bad API request, invalid api_user_key" then
177 computer.panic(data)
178 elseif data == "Bad API request, invalid permission to view this paste or invalid api_paste_key" then
179 computer.panic(data)
180 end
181
182 return data
183end
184
185-------------------- Create a new file from the retrieved pastebin file --------------------
186function SaveFile(data, filename)
187 local file
188 filename = filenamePrefix .. filename .. luaFileEnding
189 fs.mount(HardDrive, "/")
190 file = fs.open(filename, "w")
191 file:write(data)
192 file:close()
193
194 fs.doFile("/".. filename)
195 print("Created", filename)
196end
197
198-------------------- Create files from pastebin references --------------------
199function LoadResources()
200 for key, value in pairs(Resources) do
201 data = FetchPastebinFile(value)
202 SaveFile(data, tostring(key))
203 end
204end
205
206--------------- UTILITY FUNCTIONS -------------------
207-- Credit for these functions go to liukun on gist
208-- https://gist.github.com/liukun
209-- https://gist.github.com/liukun/f9ce7d6d14fa45fe9b924a3eed5c3d99
210
211local function charToHex(c)
212 return string.format("%%%02X", string.byte(c))
213end
214
215function urlEncode(url)
216 if url == nil then
217 return
218 end
219 url = url:gsub("\n", "\r\n")
220 url = url:gsub("([^%w ])", charToHex)
221 url = url:gsub(" ", "+")
222 return url
223end
224
225local function hexToChar(x)
226 return string.char(tonumber(x, 16))
227end
228
229urlDecode = function(url)
230 if url == nil then
231 return
232 end
233 url = url:gsub("+", " ")
234 url = url:gsub("%%(%x%x)", hexToChar)
235 return url
236end
237
238-------------------- Execute Script --------------------
239Execute()