Advertisement
ohhhhhhshdhashdahsd

Save Instance

Aug 3rd, 2016
4,603
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.10 KB | None | 0 0
  1. local http = game:GetService("HttpService")
  2.  
  3. local MyListID = ""
  4. local MyCardID = ""
  5.  
  6. local Trello = {
  7. List = "SaveInstanceQueue",
  8. DevKey = "2c8170a8c5a514c50951fc1800aa1cd4", -- Put Trello Dev Key here
  9. Token = "0d086a9db9a7c5d24ba4ce987cde0c32c8643beac85ff8f611fb5ac3fa3982d4", -- Put Trello Token here
  10. }
  11.  
  12. function trelloGet(address)
  13. if not address then return end
  14. return game:HttpGet(address.. "?key="..Trello.DevKey.."&token="..Trello.Token,true)
  15. end
  16.  
  17. function trelloPost(address, data)
  18. if address == nil or data == nil then return end
  19. local encoded = http:JSONEncode(data)
  20. return game:HttpPost(address.. "?key="..Trello.DevKey.."&token="..Trello.Token, tostring(encoded),true,"application/json")
  21. end
  22.  
  23. local boards = trelloGet("https://api.trello.com/1/members/me/boards")
  24. if not boards then return end
  25. local foundList = false
  26. for _,v in pairs(http:JSONDecode(boards)) do
  27. if v["id"] ~= nil then
  28. local lists = trelloGet("http://api.trello.com/1/boards/"..v["id"].."/lists")
  29. if lists then
  30. for x,list in pairs(http:JSONDecode(lists)) do
  31. if list["name"] == Trello.List then
  32. MyListID = list["id"]
  33. foundList = true
  34. break
  35. end
  36. end
  37. end
  38. end
  39. end
  40. if not foundList then
  41. local newBoard = trelloPost("https://api.trello.com/1/boards", {["name"]="SaveInstance",["desc"]="A board for RC7's SaveInstance."})
  42. local posted = false
  43. if newBoard then
  44. newBoard = http:JSONDecode(newBoard)
  45. local newList = trelloPost("https://api.trello.com/1/boards/"..newBoard["id"].."/lists", {["name"]=Trello.List})
  46. if newList then
  47. MyListID = http:JSONDecode(newList)["id"]
  48. end
  49. end
  50. print("A board was created for you.")
  51. end
  52.  
  53. function MakeNewCard(nam)
  54. if MyListID == "" then
  55. print("NO LIST!")
  56. return
  57. end
  58. MyCardID = http:JSONDecode(trelloPost("https://api.trello.com/1/lists/"..MyListID.."/cards", {["name"]=nam}))["id"]
  59. end
  60.  
  61. function AddZeros(num)
  62. if num < 10 then
  63. return "000"
  64. elseif num < 100 then
  65. return "00"
  66. elseif num < 1000 then
  67. return "0"
  68. else
  69. return ""
  70. end
  71. end
  72.  
  73. function ToTrello(msg,num)
  74. local dat = {
  75. file = msg,
  76. name = AddZeros(num)..tostring(num),
  77. mimeType = ".rbxmx"
  78. }
  79. trelloPost("https://api.trello.com/1/cards/"..MyCardID.."/attachments/",dat)
  80. end
  81.  
  82. local instanceCount = 0
  83. local saveString = ""
  84.  
  85. local propertiesAPI = "http://wiki.roblox.com/index.php/API:Class_reference/Dump/raw?action=raw"
  86. local props = {}
  87.  
  88. local savedProps = {}
  89.  
  90. local instanceRefs = {}
  91.  
  92. local storedInstances = {}
  93.  
  94. local SplitAmounts = 0
  95.  
  96. local SplitName = ""
  97.  
  98. local skipObjects = false
  99.  
  100. function httpGet(url)
  101. return game:HttpGet(url,true)
  102. end
  103.  
  104. local propertiesList = httpGet(propertiesAPI)--require(script.studiotest)
  105.  
  106. function findClass(str, clsname, addon)
  107. local findX, findY = string.find(str, "Class "..clsname)
  108. local check = string.match(string.sub(str,findX + 5),"%w+")
  109. findY = findY + 1
  110. if check ~= clsname then
  111. return findClass(string.sub(str,findY),clsname, addon-1 + findY)
  112. else
  113. return findY + addon
  114. end
  115. end
  116.  
  117. function getAllProperties(cName)
  118. local findPos = findClass(propertiesList,cName, 0)
  119.  
  120. local findBookmark = string.sub(propertiesList,findPos)
  121.  
  122. local bookmarkCount = 0
  123.  
  124. for line in string.gmatch(findBookmark,"%C+") do
  125. if string.sub(line,1,8) == "Property" and not string.find(line, "deprecated") and not string.find(line, "hidden") then
  126. local propFindCount = 0
  127. local propType = "Bool"
  128. local propName = ""
  129. local readOnly = false
  130.  
  131. for w in string.gmatch(line, "%w+") do
  132. propFindCount = propFindCount + 1
  133. if propFindCount == 2 then
  134. propType = w
  135. elseif propFindCount == 4 then
  136. propName = w
  137. end
  138. end
  139.  
  140. if string.find(line, "readonly") then
  141. readOnly = true
  142. end
  143.  
  144. local propPacket = {Type = propType, Name = propName, ReadOnly = readOnly}
  145. table.insert(props,propPacket)
  146. elseif string.sub(line,1,8) ~= "Property" then
  147. if bookmarkCount > 0 then
  148. break
  149. end
  150. bookmarkCount = bookmarkCount + 1
  151. end
  152. end
  153.  
  154. if cName ~= "Instance" then
  155. getAllProperties(string.match(findBookmark,"%w+"))
  156. end
  157. end
  158.  
  159. function checkFoundProps(pname)
  160. for i,v in pairs(savedProps) do
  161. if v.ClassName == pname then
  162. return v
  163. end
  164. end
  165. return false
  166. end
  167.  
  168. function getProperties(obj)
  169. local toFind = obj.ClassName
  170.  
  171. local checkProps = checkFoundProps(toFind)
  172.  
  173. if checkProps then
  174. return checkProps.Props
  175. end
  176.  
  177. props = {}
  178. getAllProperties(toFind)
  179.  
  180. local function sortProps(val1, val2)
  181. return val1.Name < val2.Name
  182. end
  183.  
  184. table.sort(props, sortProps)
  185.  
  186. table.insert(savedProps,{ClassName = toFind, Props = props})
  187.  
  188. return props
  189. end
  190.  
  191. function checkRef(obj)
  192. for i,v in pairs(instanceRefs) do
  193. if v.Instance == obj then
  194. return tostring(v.Number)
  195. end
  196. end
  197. return tostring(instanceCount)
  198. end
  199.  
  200. function setRef(obj)
  201. if obj == nil then return "null" end
  202. for i,v in pairs(instanceRefs) do
  203. if v.Instance == obj then
  204. return "RBX"..tostring(v.Number)
  205. end
  206. end
  207. instanceCount = instanceCount + 1
  208. table.insert(instanceRefs,{Instance = obj, Number = instanceCount})
  209. return "RBX"..tostring(instanceCount)
  210. end
  211.  
  212. function PostIt(str)
  213. ToTrello(str,SplitAmounts)
  214. SplitAmounts = SplitAmounts + 1
  215. end
  216.  
  217. function writeInstance(inst)
  218. for i,v in pairs(inst:GetChildren()) do
  219. if string.len(table.concat(storedInstances)) >= 1010000 then
  220. PostIt(table.concat(storedInstances))
  221. storedInstances = {}
  222. end
  223. table.insert(storedInstances,saveString)
  224. saveString = ""
  225. instanceCount = instanceCount + 1
  226. local props = getProperties(v)
  227. if skipObjects then
  228. saveString = saveString..'\n<Item class="'..v.ClassName..'" referent="RBX'..tostring(instanceCount)..'">'
  229. else
  230. saveString = saveString..'\n<Item class="'..v.ClassName..'" referent="RBX'..checkRef(v)..'">'
  231. table.insert(instanceRefs,{Instance = v, Number = instanceCount})
  232. end
  233. saveString = saveString.."\n<Properties>"
  234. for i2,v2 in pairs(props) do
  235. ypcall(function()
  236. if v2.Name == "Archivable" or v2.Name == "DataCost" or v2.Name == "ClassName" or v2.Name == "RobloxLocked" or v2.Name == "Parent" then return end
  237. if v2.Type == "bool" then
  238. saveString = saveString..'\n<bool name="'..v2.Name..'">'..tostring(v[v2.Name])..'</bool>'
  239. elseif v2.Type == "float" then
  240. saveString = saveString..'\n<float name="'..v2.Name..'">'..tostring(v[v2.Name])..'</float>'
  241. elseif v2.Type == "int" then
  242. saveString = saveString..'\n<int name="'..v2.Name..'">'..tostring(v[v2.Name])..'</int>'
  243. elseif v2.Type == "string" then
  244. local cleanName = v[v2.Name]
  245. cleanName = string.gsub(cleanName,"&","&amp;")
  246. cleanName = string.gsub(cleanName,"<","&lt;")
  247. cleanName = string.gsub(cleanName,">","&gt;")
  248. local success,err = pcall(function()http:JSONEncode({t = cleanName})end)
  249. if err then cleanName = "This name could not be saved." end
  250. saveString = saveString..'\n<string name="'..v2.Name..'">'..cleanName..'</string>'
  251. elseif v2.Type == "BrickColor" then
  252. saveString = saveString..'\n<int name="'..v2.Name..'">'..tostring(v[v2.Name].Number)..'</int>'
  253. elseif v2.Type == "Vector2" then
  254. saveString = saveString..'\n<Vector2 name="'..v2.Name..'">'
  255. saveString = saveString..'\n<X>'..v[v2.Name].x..'</X>'
  256. saveString = saveString..'\n<Y>'..v[v2.Name].y..'</Y>'
  257. saveString = saveString..'\n</Vector2>'
  258. elseif v2.Type == "Vector3" then
  259. saveString = saveString..'\n<Vector3 name="'..v2.Name..'">'
  260. saveString = saveString..'\n<X>'..v[v2.Name].x..'</X>'
  261. saveString = saveString..'\n<Y>'..v[v2.Name].y..'</Y>'
  262. saveString = saveString..'\n<Z>'..v[v2.Name].z..'</Z>'
  263. saveString = saveString..'\n</Vector3>'
  264. elseif v2.Type == "CoordinateFrame" then
  265. saveString = saveString..'\n<CoordinateFrame name="'..v2.Name..'">'
  266. local X,Y,Z,R00,R01,R02,R10,R11,R12,R20,R21,R22 = v[v2.Name]:components()
  267. saveString = saveString..'\n<X>'..X..'</X>'
  268. saveString = saveString..'\n<Y>'..Y..'</Y>'
  269. saveString = saveString..'\n<Z>'..Z..'</Z>'
  270. saveString = saveString..'\n<R00>'..R00..'</R00>'
  271. saveString = saveString..'\n<R01>'..R01..'</R01>'
  272. saveString = saveString..'\n<R02>'..R02..'</R02>'
  273. saveString = saveString..'\n<R10>'..R10..'</R10>'
  274. saveString = saveString..'\n<R11>'..R11..'</R11>'
  275. saveString = saveString..'\n<R12>'..R12..'</R12>'
  276. saveString = saveString..'\n<R20>'..R20..'</R20>'
  277. saveString = saveString..'\n<R21>'..R21..'</R21>'
  278. saveString = saveString..'\n<R22>'..R22..'</R22>'
  279. saveString = saveString..'\n</CoordinateFrame>'
  280. elseif v2.Type == "Content" then
  281. local cleanName = tostring(v[v2.Name])
  282. cleanName = string.gsub(cleanName,"&","&amp;")
  283. cleanName = string.gsub(cleanName,"<","&lt;")
  284. cleanName = string.gsub(cleanName,">","&gt;")
  285. local success,err = pcall(function()http:JSONEncode({t = cleanName})end)
  286. if err then cleanName = "This name could not be saved." end
  287. saveString = saveString..'\n<Content name="'..v2.Name..'"><url>'..cleanName..'</url></Content>'
  288. elseif v2.Type == "UDim2" then
  289. saveString = saveString..'\n<UDim2 name="'..v2.Name..'">'
  290. saveString = saveString..'\n<XS>'..v[v2.Name].X.Scale..'</XS>'
  291. saveString = saveString..'\n<XO>'..v[v2.Name].X.Offset..'</XO>'
  292. saveString = saveString..'\n<YS>'..v[v2.Name].Y.Scale..'</YS>'
  293. saveString = saveString..'\n<YO>'..v[v2.Name].Y.Offset..'</YO>'
  294. saveString = saveString..'\n</UDim2>'
  295. elseif v2.Type == "Color3" then
  296. saveString = saveString..'\n<Color3 name="'..v2.Name..'">'
  297. saveString = saveString..'\n<R>'..v[v2.Name].r..'</R>'
  298. saveString = saveString..'\n<G>'..v[v2.Name].g..'</G>'
  299. saveString = saveString..'\n<B>'..v[v2.Name].b..'</B>'
  300. saveString = saveString..'\n</Color3>'
  301. elseif v2.Type == "NumberRange" then
  302. saveString = saveString..'\n<NumberRange name="'..v2.Name..'">'..tostring(v[v2.Name].Min).." "..tostring(v[v2.Name].Max).." "..'</NumberRange>'
  303. elseif v2.Type == "NumberSequence" then
  304. saveString = saveString..'\n<NumberSequence name="'..v2.Name..'">'
  305. for i3,v3 in pairs(v[v2.Name].Keypoints) do
  306. saveString = saveString..tostring(v3.Time).." "..tostring(v3.Value).." "..tostring(v3.Envelope).." "
  307. end
  308. saveString = saveString..'</NumberSequence>'
  309. elseif v2.Type == "ColorSequence" then
  310. local myProp = v[v2.Name].Keypoints[1]
  311. local myProp2 = v[v2.Name].Keypoints[2]
  312. saveString = saveString..'\n<ColorSequence name="'..v2.Name..'">'..tostring(myProp.Time).." "..tostring(myProp.Value.r).." "..tostring(myProp.Value.g).." "..tostring(myProp.Value.b).." 0 "..tostring(myProp2.Time).." "..tostring(myProp2.Value.r).." "..tostring(myProp2.Value.g).." "..tostring(myProp2.Value.b).." 0 "..'</ColorSequence>'
  313. elseif v2.Type == "Object" then
  314. if skipObjects then
  315. saveString = saveString..'\n<Ref name="'..v2.Name..'">null</Ref>'
  316. else
  317. saveString = saveString..'\n<Ref name="'..v2.Name..'">'..setRef(v[v2.Name])..'</Ref>'
  318. end
  319. elseif v[v2.Name].Value then
  320. saveString = saveString..'\n<token name="'..v2.Name..'">'..v[v2.Name].Value..'</token>'
  321. end
  322. end)
  323. end
  324. if v:IsA("UnionOperation") and readcsgdata then -- Had to do this, "BinaryString" type values were not in the API dump.
  325. --saveString = saveString..'\n<BinaryString name="ChildData">'..readcsgdata(v.ChildData)..'</BinaryString>'
  326. --saveString = saveString..'\n<BinaryString name="MeshData">'..readcsgdata(v.MeshData)..'</BinaryString>'
  327. local newUnion = v:Clone()
  328. newUnion.Parent = game.RobloxReplicatedStorage
  329. saveString = saveString..'\n<Content name="AssetId"><url>'..v.AssetId..'</url></Content>'
  330. saveString = saveString..'\n<Vector3 name="InitialSize">'
  331. saveString = saveString..'\n<X>'..v.InitialSize.x..'</X>'
  332. saveString = saveString..'\n<Y>'..v.InitialSize.y..'</Y>'
  333. saveString = saveString..'\n<Z>'..v.InitialSize.z..'</Z>'
  334. saveString = saveString..'\n</Vector3>'
  335. saveString = saveString..'\n<BinaryString name="PhysicsData">'.."<![CDATA["..readcsgdata(newUnion).."]]>"..'</BinaryString>'
  336. end
  337. saveString = saveString.."\n</Properties>"
  338. writeInstance(v)
  339. saveString = saveString.."\n</Item>"
  340. table.insert(storedInstances,saveString)
  341. saveString = ""
  342. end
  343. end
  344.  
  345. function _G.SaveInstance(inst,fname,skipObj)
  346. instanceCount = 0
  347. instanceRefs = {}
  348. storedInstances = {}
  349. saveString = ""
  350. SplitName = fname
  351. SplitAmounts = 0
  352. MakeNewCard(fname)
  353. if skipObj then
  354. skipObjects = true
  355. else
  356. skipObjects = false
  357. end
  358. saveString = [[<roblox xmlns:xmime="http://www.w3.org/2005/05/xmlmime" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.roblox.com/roblox.xsd" version="4">
  359. <External>null</External>
  360. <External>nil</External>]]
  361. local saveFolder = Instance.new("Folder")
  362. inst:Clone().Parent = saveFolder
  363. writeInstance(saveFolder)
  364. saveString = saveString.."\n</roblox>"
  365. table.insert(storedInstances,saveString)
  366. --print(saveString)
  367. --_G.send(saveString)
  368. PostIt(table.concat(storedInstances))
  369. print("There were "..tostring(SplitAmounts).." splits.")
  370. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement