|
| 1 | +local UserInputService = game:GetService("UserInputService") |
| 2 | +local Workspace = game:GetService("Workspace") |
| 3 | + |
| 4 | +local Extension = require("@src/core/Extension") |
| 5 | +local Iris = require("@src/Iris") |
| 6 | +local UserIds = require("@src/constants/UserIds") |
| 7 | +local assetInserters = require("@self/assetInserters") |
| 8 | +local assets = require("@include/assets") |
| 9 | +local createCoreExtension = require("@src/core/constructors/createCoreExtension") |
| 10 | +local products = require("@self/products") |
| 11 | +local types = require("@self/types") |
| 12 | +local widgets = require("@src/widgets") |
| 13 | + |
| 14 | +local ext = createCoreExtension { |
| 15 | + id = "rocket-insert", |
| 16 | + title = "Insert", |
| 17 | + description = "TBA", |
| 18 | + icon = assets.extensions.rocket, |
| 19 | + contributors = { |
| 20 | + { type = "user", id = UserIds.znotfireman }, |
| 21 | + }, |
| 22 | +} |
| 23 | + |
| 24 | +ext:newCommand({ |
| 25 | + id = "insert-asset", |
| 26 | + title = "Insert Asset", |
| 27 | + description = "Fetches and inserts the given asset ID into the game.", |
| 28 | +}, function(r: Extension.CommandRuntime) |
| 29 | + local output = r:createCommandOutput() |
| 30 | + |
| 31 | + function output.renderWindow(window: Iris.Window) |
| 32 | + local maybeAssetId, assetInput = widgets.InstantInputText({ hint = "ie. 1234567890", label = "Asset ID" }) |
| 33 | + |
| 34 | + if r.isFirstRun then |
| 35 | + window.state.position:set(UserInputService:GetMouseLocation()) |
| 36 | + assetInput.state.text:set("") |
| 37 | + maybeAssetId = "" |
| 38 | + end |
| 39 | + |
| 40 | + local assetId = tonumber(maybeAssetId) |
| 41 | + if not assetId then |
| 42 | + return |
| 43 | + end |
| 44 | + |
| 45 | + local product = products.fetch(assetId) |
| 46 | + if not product then |
| 47 | + widgets.Subtext({ |
| 48 | + if products.invalidIds[assetId] then `Invalid asset ID "{assetId}"!` else "Fetching asset...", |
| 49 | + }) |
| 50 | + return |
| 51 | + end |
| 52 | + |
| 53 | + local assetTypeId = product.AssetTypeId |
| 54 | + -- selene: allow(incorrect_standard_library_use) |
| 55 | + local maybeAssetType = Enum.AssetType:FromValue(assetTypeId) |
| 56 | + local assetTypeName = if maybeAssetType then maybeAssetType.Name else "Unknown" |
| 57 | + |
| 58 | + local inserter = assetInserters[assetTypeId] |
| 59 | + if not inserter then |
| 60 | + Iris.Text({ `Rocket doesn't support inserting assets of type {assetTypeName}!` }) |
| 61 | + widgets.Subtext({ `asset type ID: {assetTypeId}` }) |
| 62 | + return |
| 63 | + end |
| 64 | + |
| 65 | + widgets.Subtext({ `Got asset of type {assetTypeName}! (asset type ID: {assetTypeId})` }) |
| 66 | + |
| 67 | + Iris.SameLine() |
| 68 | + for _, options in inserter do |
| 69 | + if not Iris.Button({ options.label }).clicked() then |
| 70 | + continue |
| 71 | + end |
| 72 | + |
| 73 | + task.spawn(function(r: Extension.CommandRuntime, product: types.ProductInfo) |
| 74 | + window.state.isOpened:set(false) |
| 75 | + local finishRecording = r:recordChangeHistory() |
| 76 | + |
| 77 | + local success, inserted: Instance = pcall(options.insert, product) |
| 78 | + local recordingOperation = Enum.FinishRecordingOperation.Commit |
| 79 | + if success then |
| 80 | + r.log:trace(`Successfully inserted "{product.Name}" (ID: {product.AssetId}) as`, inserted) |
| 81 | + inserted.Parent = r:useSelection()[1] or Workspace |
| 82 | + r:setSelection({ inserted }) |
| 83 | + else |
| 84 | + r.log:warn(`Failed to insert "{product.Name}" (ID: {product.AssetId}):`, inserted) |
| 85 | + recordingOperation = Enum.FinishRecordingOperation.Cancel |
| 86 | + end |
| 87 | + |
| 88 | + finishRecording(recordingOperation) |
| 89 | + window.state.isOpened:set(true) |
| 90 | + r:cleanup() |
| 91 | + end, r, product) |
| 92 | + end |
| 93 | + Iris.End() |
| 94 | + end |
| 95 | + |
| 96 | + return output |
| 97 | +end) |
| 98 | + |
| 99 | +ext:newCommand({ |
| 100 | + id = "insert-avatar", |
| 101 | + title = "Insert Avatar", |
| 102 | + description = "Search, fetches, and inserts the given user's avatar into the game.", |
| 103 | +}, function() |
| 104 | + -- TODO |
| 105 | +end) |
| 106 | + |
| 107 | +ext:newCommand({ |
| 108 | + id = "insert-pkg-pesde", |
| 109 | + title = "Insert Package from pesde", |
| 110 | + description = "Search, fetches, and inserts from the pesde package manager.", |
| 111 | +}, function() |
| 112 | + -- TODO |
| 113 | +end) |
| 114 | + |
| 115 | +ext:newCommand({ |
| 116 | + id = "insert-pkg-wally", |
| 117 | + title = "Insert Package from Wally", |
| 118 | + description = "Search, fetches, and inserts from the Wally package manager.", |
| 119 | +}, function() |
| 120 | + -- TODO |
| 121 | +end) |
| 122 | + |
| 123 | +ext:newCommand({ |
| 124 | + id = "insert-git", |
| 125 | + title = "Insert from Git", |
| 126 | + description = "Inserts a Git repository into the game.", |
| 127 | +}, function() |
| 128 | + -- TODO |
| 129 | +end) |
| 130 | + |
| 131 | +return ext |
0 commit comments