diff --git a/SyncAPI.lua b/SyncAPI.lua index 4db97e9..897f2dd 100644 --- a/SyncAPI.lua +++ b/SyncAPI.lua @@ -1,5 +1,7 @@ local HttpService = game:GetService('HttpService') local RunService = game:GetService('RunService') +local InsertService = game:GetService("InsertService") +local MarketplaceService = game:GetService("MarketplaceService") -- References SyncAPI = script.Parent; @@ -24,10 +26,47 @@ Options = { CreatedInstances = {}; LastParents = {}; +-- Provide cached data for requests +local assetTypeCache = {} +local imageIdCache = {} +local IsHttpServiceEnabled = nil + -- Determine whether we're in tool or plugin mode ToolMode = (Tool.Parent:IsA 'Plugin') and 'Plugin' or 'Tool' -local IsHttpServiceEnabled = nil +-- Get asset type from asset id +local function getAssetType(assetId) + if assetTypeCache[assetId] then + return assetTypeCache[assetId] + end + + local success, result = pcall(MarketplaceService.GetProductInfo, MarketplaceService, assetId, Enum.InfoType.Asset) + + if success and result and result.AssetTypeId then + assetTypeCache[assetId] = result.AssetTypeId + return result.AssetTypeId + else + return nil + end +end + +-- Extract imageid from decalid via insertservice +local function getImageFromDecal(assetId) + if imageIdCache[assetId] then + return imageIdCache[assetId] + end + + local success, result = pcall(function() + return string.match(InsertService:LoadAsset(assetId):FindFirstChildWhichIsA("Decal").Texture, "^.-(%d+)/?$") + end) + + if success and result then + imageIdCache[assetId] = result + return success and result + else + return nil + end +end -- List of actions that could be requested Actions = { @@ -1587,7 +1626,7 @@ Actions = { -- Push serialized data to server local Response = HttpService:JSONDecode( HttpService:PostAsync( - 'http://f3xteam.com/bt/export', + 'https://f3xteam.com/bt/export', HttpService:JSONEncode { data = SerializedBuildData, version = 3, userId = (Player and Player.UserId) }, Enum.HttpContentType.ApplicationJson, true @@ -1618,7 +1657,7 @@ Actions = { -- Perform test HTTP request local DidSucceed, Result = pcall(function () - return HttpService:GetAsync('https://google.com') + return HttpService:GetAsync('https://www.google.com/robots.txt') end) -- Determine whether HttpService is enabled based on whether request succeeded @@ -1634,17 +1673,31 @@ Actions = { ['ExtractMeshFromAsset'] = function (AssetId) -- Returns the first found mesh in the given asset + -- Ensure valid asset ID is given + assert(type(AssetId) == 'number', 'Invalid asset ID'); + + -- Add automatic checking for image assets which works client-sided for better UX + if getAssetType(AssetId) == 1 then + return string.format([[{"success":true,"textureID":%d}]], AssetId) + end + -- Offload action to server-side if API is running locally if RunService:IsClient() and not RunService:IsStudio() then return SyncAPI.ServerEndpoint:InvokeServer('ExtractMeshFromAsset', AssetId); end; - -- Ensure valid asset ID is given - assert(type(AssetId) == 'number', 'Invalid asset ID'); + -- Add support for decals which can be inserted via InsertService + if getAssetType(AssetId) == 13 then + local imageId = getImageFromDecal(AssetId) + + if imageId then + return string.format([[{"success":true,"textureID":%d}]], imageId) + end + end -- Return parsed response from API return HttpService:JSONDecode( - HttpService:GetAsync('http://f3xteam.com/bt/getFirstMeshData/' .. AssetId) + HttpService:GetAsync('https://f3xteam.com/bt/getFirstMeshData/' .. AssetId) ); end; @@ -1652,13 +1705,30 @@ Actions = { ['ExtractImageFromDecal'] = function (DecalAssetId) -- Returns the first image found in the given decal asset + -- Ensure valid asset ID is given + assert(type(DecalAssetId) == 'number', 'Invalid asset ID'); + + -- Add automatic checking for image assets which works client-sided for better UX + if getAssetType(DecalAssetId) == 1 then + return DecalAssetId + end + -- Offload action to server-side if API is running locally if RunService:IsClient() and not RunService:IsStudio() then return SyncAPI.ServerEndpoint:InvokeServer('ExtractImageFromDecal', DecalAssetId); end; + -- Add support for decals which can be inserted via InsertService + if getAssetType(DecalAssetId) == 13 then + local imageId = getImageFromDecal(DecalAssetId) + + if imageId then + return imageId + end + end + -- Return direct response from the API - return HttpService:GetAsync('http://f3xteam.com/bt/getDecalImageID/' .. DecalAssetId); + return HttpService:GetAsync('https://f3xteam.com/bt/getDecalImageID/' .. DecalAssetId); end; @@ -1955,4 +2025,4 @@ return { end; -}; \ No newline at end of file +};