better LuaUserData error handling and add Steam.GetWorkshopCollection

This commit is contained in:
Evil Factory
2022-05-19 14:26:21 -03:00
parent 6ee6b5cd15
commit ca0da04ad0
3 changed files with 50 additions and 20 deletions

View File

@@ -4,6 +4,8 @@ local AddCallMetaTable = LuaSetup.AddCallMetaTable
local CreateStatic = LuaSetup.CreateStatic
local CreateEnum = LuaUserData.CreateEnumTable
require("DefaultLib/Utils/SteamApi")
defaultLib["Byte"] = CreateStatic("Barotrauma.LuaByte", true)
defaultLib["UShort"] = CreateStatic("Barotrauma.LuaUShort", true)
defaultLib["Float"] = CreateStatic("Barotrauma.LuaFloat", true)

View File

@@ -0,0 +1,26 @@
local descriptor = LuaUserData.RegisterType("Barotrauma.LuaCsSteam")
LuaUserData.AddMethod(descriptor, "GetWorkshopCollection", function (id, callback)
id = tostring(id)
Networking.RequestPostHTTP("https://api.steampowered.com/ISteamRemoteStorage/GetCollectionDetails/v1/", function (result)
local data = json.parse(result)
if data.response.collectiondetails.children == nil then
callback()
return
end
local workshopItems = {}
for key, value in pairs(data.response.collectiondetails[1].children) do
table.insert(workshopItems, value.publishedfileid)
end
if callback then
callback(workshopItems)
end
end,
"collectioncount=1&publishedfileids[0]=" .. id,
"application/x-www-form-urlencoded")
end)

View File

@@ -34,8 +34,7 @@ namespace Barotrauma
if (type == null)
{
GameMain.LuaCs.HandleException(new Exception($"Tried to register a type that doesn't exist: {typeName}."));
return null;
throw new ScriptRuntimeException($"Tried to register a type that doesn't exist: {typeName}.");
}
return UserData.RegisterType(type);
@@ -47,8 +46,7 @@ namespace Barotrauma
if (type == null)
{
GameMain.LuaCs.HandleException(new Exception($"Tried to unregister a type that doesn't exist: {typeName}."));
return;
throw new ScriptRuntimeException($"Tried to unregister a type that doesn't exist: {typeName}.");
}
UserData.UnregisterType(type);
@@ -85,8 +83,7 @@ namespace Barotrauma
if (type == null)
{
GameMain.LuaCs.HandleException(new Exception($"Tried to create a static userdata of a type that doesn't exist: {typeName}."));
return null;
throw new ScriptRuntimeException($"Tried to create a static userdata of a type that doesn't exist: {typeName}.");
}
MethodInfo method = typeof(UserData).GetMethod(nameof(UserData.CreateStatic), 1, new Type[0]);
@@ -100,8 +97,7 @@ namespace Barotrauma
if (type == null)
{
GameMain.LuaCs.HandleException(new Exception($"Tried to create an enum table with a type that doesn't exist:: {typeName}."));
return null;
throw new ScriptRuntimeException($"Tried to create an enum table with a type that doesn't exist:: {typeName}.");
}
Dictionary<string, object> result = new Dictionary<string, object>();
@@ -132,8 +128,7 @@ namespace Barotrauma
{
if (IUUD == null)
{
GameMain.LuaCs.HandleException(new Exception($"Tried to use a UserDataDescriptor that is null to make {fieldName} accessible."));
return;
throw new ScriptRuntimeException($"Tried to use a UserDataDescriptor that is null to make {fieldName} accessible.");
}
var descriptor = (StandardUserDataDescriptor)IUUD;
@@ -146,8 +141,7 @@ namespace Barotrauma
if (field == null)
{
GameMain.LuaCs.HandleException(new Exception($"Tried to make field '{fieldName}' accessible, but the field doesn't exist."));
return;
throw new ScriptRuntimeException($"Tried to make field '{fieldName}' accessible, but the field doesn't exist.");
}
descriptor.RemoveMember(fieldName);
@@ -170,8 +164,7 @@ namespace Barotrauma
{
if (IUUD == null)
{
GameMain.LuaCs.HandleException(new Exception($"Tried to use a UserDataDescriptor that is null to make {methodName} accessible."));
return;
throw new ScriptRuntimeException($"Tried to use a UserDataDescriptor that is null to make {methodName} accessible.");
}
var descriptor = (StandardUserDataDescriptor)IUUD;
@@ -184,8 +177,7 @@ namespace Barotrauma
if (method == null)
{
GameMain.LuaCs.HandleException(new Exception($"Tried to make method '{methodName}' accessible, but the method doesn't exist."));
return;
throw new ScriptRuntimeException($"Tried to make method '{methodName}' accessible, but the method doesn't exist.");
}
descriptor.RemoveMember(methodName);
@@ -196,8 +188,7 @@ namespace Barotrauma
{
if (IUUD == null)
{
GameMain.LuaCs.HandleException(new Exception($"Tried to use a UserDataDescriptor that is null to add method {methodName}."));
return;
throw new ScriptRuntimeException($"Tried to use a UserDataDescriptor that is null to add method {methodName}.");
}
var descriptor = (StandardUserDataDescriptor)IUUD;
@@ -210,12 +201,23 @@ namespace Barotrauma
}));
}
public static void AddField(IUserDataDescriptor IUUD, string fieldName, DynValue value)
{
if (IUUD == null)
{
throw new ScriptRuntimeException($"Tried to use a UserDataDescriptor that is null to add field {fieldName}.");
}
var descriptor = (StandardUserDataDescriptor)IUUD;
descriptor.RemoveMember(fieldName);
descriptor.AddMember(fieldName, new DynValueMemberDescriptor(fieldName, value));
}
public static void RemoveMember(IUserDataDescriptor IUUD, string memberName)
{
if (IUUD == null)
{
GameMain.LuaCs.HandleException(new Exception($"Tried to use a UserDataDescriptor that is null to remove the member {memberName}."));
return;
throw new ScriptRuntimeException($"Tried to use a UserDataDescriptor that is null to remove the member {memberName}.");
}
var descriptor = (StandardUserDataDescriptor)IUUD;