LiteDB icon indicating copy to clipboard operation
LiteDB copied to clipboard

Unity and WebGL compatible

Open Reddevildragg opened this issue 5 years ago • 13 comments

Hi.

Just wondering if anyone has had any luck getting the Unity Package to work on a Unity WebGL build for reading data from a database in streaming assets? Trying to get it working currently and can read the bytes, convert to memory stream and then use this to open the database. I am then able to view collection names but whenever i try and get data out i hit a memory error.

Reddevildragg avatar Mar 18 '19 18:03 Reddevildragg

Just ran into the same problem, from the StackTrace it looks like the "BsonMapper_BuildEntityMapper" is calling a CreateGenericGetter method and somewhere there crashes:

exception thrown: RuntimeError: memory access out of bounds,RuntimeError: memory access out of bounds
    at __ZN16VirtFuncInvoker0IP6Type_tE6InvokeEjP12Il2CppObject (wasm-function[30944]:18)
    at _LightLambda_MakeRunDelegateCtor_m34176183E3A928A397721E889331C32DF9C556C1 (wasm-function[43248]:1487)
    at dynCall_iii (wasm-function[73889]:16)
    at Object.dynCall_iii (blob:http://localhost:65257/532ec541-0fd6-42a4-b305-8302f3ffefcb:26549:38)
    at invoke_iii (blob:http://localhost:65257/532ec541-0fd6-42a4-b305-8302f3ffefcb:17870:31)
    at _LightLambda_GetRunDelegateCtor_m802F474EE5F98CB221B2E24609A743536CFF4517 (wasm-function[43246]:389)
    at _LightLambda_MakeDelegate_m5BF4647527466A602E0339000CBE3382BB98B4C0 (wasm-function[43245]:75)
    at _LightDelegateCreator_CreateDelegate_mAD897B269ED0186D1E2CEA54BFBD550375E072F6 (wasm-function[43243]:76)
    at _LightDelegateCreator_CreateDelegate_m1B6E2A2E3D20D75EB586A04B9F248208272329CC (wasm-function[43242]:7)
    at _Expression_1_Compile_m5F2BD4EE3DA1F051DDD2D1CF6B3467390FE06A39_gshared (wasm-function[65389]:68)
    at _Expression_1_Compile_m32BD954CB112E9E0054156346964AB1E410377CF_gshared (wasm-function[65388]:34)
    at __Z62Expression_1_Compile_m1FB0D36180E1B6DC47F68802D34167D780A5C338P54Expression_1_tB37C50B4E4CE8A441B6834EB04541A9B3D7EFF8DPK10MethodInfo (wasm-function[60469]:5)
    at _Reflection_CreateGenericGetter_mC38804D663E3223CEF6762889203F957811673B8 (wasm-function[72626]:427)
    at dynCall_iiii (wasm-function[73900]:18)
    at Object.dynCall_iiii (blob:http://localhost:65257/532ec541-0fd6-42a4-b305-8302f3ffefcb:26604:39)
    at invoke_iiii (blob:http://localhost:65257/532ec541-0fd6-42a4-b305-8302f3ffefcb:17958:32)
    at _BsonMapper_BuildEntityMapper_m055071F8F04E659DE2B52B5852F47798F4C2F590 (wasm-function[60532]:1104)
    at __ZN16VirtFuncInvoker1IbP12Il2CppObjectE6InvokeEjS1_S1_ (wasm-function[30956]:48)
    at dynCall_iiii (wasm-function[73900]:18)
    at Object.dynCall_iiii (blob:http://localhost:65257/532ec541-0fd6-42a4-b305-8302f3ffefcb:26604:39)
    at invoke_iiii (blob:http://localhost:65257/532ec541-0fd6-42a4-b305-8302f3ffefcb:17958:32)
    at _BsonMapper_GetEntityMapper_m0C474FAF7688D187B4A4C9ECDDBFD316A1729965 (wasm-function[60531]:250)
    at _LiteCollection_1__ctor_m25D52BEE978AA25FB6B787BB512AE456077A5226_gshared (wasm-function[64305]:578)
    at _LiteDatabase_GetCollection_TisRuntimeObject_mD2C4F0C29AC37244D24FAE47715543CF7AFB541A_gshared (wasm-function[59983]:79)
    at __Z122LiteDatabase_GetCollection_TisTreeElem_t579B24F7FB101E94ABCD2E1175E0C3399C797413_m32746F82B701BF9203CD9584A4926A96E750CB19P54LiteDatabase_t86EAC381EB5D0F5A34618932319926C1C0EC5E50P8String_tPK10MethodInfo (wasm-function[58913]:7)
    at dynCall_iiii (wasm-function[73900]:18)
    at Object.dynCall_iiii (blob:http://localhost:65257/532ec541-0fd6-42a4-b305-8302f3ffefcb:26604:39)
    at invoke_iiii (blob:http://localhost:65257/532ec541-0fd6-42a4-b305-8302f3ffefcb:17958:32)
    at _LiteDbTests_ExampleUsage1_m75B4E8F376215CD0E3119246D60C06609E3E24A3 (wasm-function[58919]:307)
    at __Z65RuntimeInvoker_TrueVoid_t22962CB4C05B1D89B55A6E1139F0E87A90987017PFvvEPK10MethodInfoPvPS4_ (wasm-function[55538]:17)
    at dynCall_iiiii (wasm-function[73908]:20)
    at Object.dynCall_iiiii (blob:http://localhost:65257/532ec541-0fd6-42a4-b305-8302f3ffefcb:26644:40)
    at invoke_iiiii (blob:http://localhost:65257/532ec541-0fd6-42a4-b305-8302f3ffefcb:18022:33)
    at __ZN6il2cpp2vm7Runtime6InvokeEPK10MethodInfoPvPS5_PP15Il2CppException (wasm-function[51544]:175)
    at __ZN6il2cpp2vmL17InvokeConvertThisEPK10MethodInfoPvPS4_PP15Il2CppException (wasm-function[51562]:46)
    at __ZN6il2cpp2vm7Runtime17InvokeConvertArgsEPK10MethodInfoPvPP12Il2CppObjectiPP15Il2CppException (wasm-function[51561]:632)
    at __ZN6il2cpp2vm7Runtime11InvokeArrayEPK10MethodInfoPvP11Il2CppArrayPP15Il2CppException (wasm-function[51560]:37)
    at __ZN6il2cpp6icalls8mscorlib6System10Reflection10MonoMethod14InternalInvokeEP22Il2CppReflectionMethodP12Il2CppObjectP11Il2CppArrayPS8_ (wasm-function[52712]:357)
    at _MonoCMethod_InternalInvoke_m114B2C342EE10E62D405951BB8F4549D9E54A690 (wasm-function[46958]:9)
    at dynCall_iiiiii (wasm-function[73914]:22)
    at Object.dynCall_iiiiii (blob:http://localhost:65257/532ec541-0fd6-42a4-b305-8302f3ffefcb:26674:41)
    at invoke_iiiiii (blob:http://localhost:65257/532ec541-0fd6-42a4-b305-8302f3ffefcb:18070:34)
    at _MonoMethod_Invoke_mD791F247764DAE0188BA06FD1DA51525E8F2F394 (wasm-function[47017]:211)
    at __ZN16VirtFuncInvoker5IP14PropertyInfo_tiP60PropertyInfoU5BU5D_tAD8E99B12FF99CA4F2EA37B612DE68E112B4CF7EP6Type_tP52TypeU5BU5D_t7FE623A666B49176DE123306221193E888A12F5FP65ParameterModifierU5BU5D_t63EC46F14F048DC9EF6BF1362E8AEBEA1A05A5EAE6InvokeEjP12Il2CppObjectiS3_S5_S7_S9_ (wasm-function[32140]:56)
    at _MethodBase_Invoke_m471794D56262D9DB5B5A324883030AB16BD39674 (wasm-function[46908]:15)
    at dynCall_iiiii (wasm-function[73908]:20)
    at Object.dynCall_iiiii (blob:http://localhost:65257/532ec541-0fd6-42a4-b305-8302f3ffefcb:26644:40)
    at invoke_iiiii (blob:http://localhost:65257/532ec541-0fd6-42a4-b305-8302f3ffefcb:18022:33)
    at _XunitTestRunner_RunTestOnMethod_m50DA79C9DE437775F551C88420E80681FDF4D0C9 (wasm-function[50344]:66)
    at _U3CU3Ec__DisplayClass2_1_U3CGetIteratorOverAllTestsU3Eb__1_m53D657F42CDD0EAA56CE4F82509FCD7462379005 (wasm-function[50356]:78)

cs-util avatar Aug 25 '19 09:08 cs-util

@mbdavid any hints if this is on some sort of roadmap to make LiteDB more compatible with Unity and WebGL builds?

cs-util avatar Dec 01 '19 18:12 cs-util

Hi @cs-util, I will take a look on this problem in v5 build.

mbdavid avatar Dec 03 '19 12:12 mbdavid

@mbdavid I saw you removed the v5 label, is there any news why the AOT platforms like WebGL have currently problems with LiteDB?

cs-util avatar Feb 16 '20 14:02 cs-util

Hi @cs-util, I remove because master branch are current v5 version and all updates will be run over master. You still have problems with LiteDB in unity (final v5 version)? If true, please open an bug issue with an example (I'm not Unity programer - so if you create a simple project in unity with your error will be best). I'm very interested in resolve any Unity compatibility problem

mbdavid avatar Feb 17 '20 13:02 mbdavid

@cs-util, sorry, I read "branch" not "label". But my anwser still same: I remove v5 label from all issues because I will identify only v4 because all others must be about v5.

mbdavid avatar Feb 17 '20 13:02 mbdavid

I took a shot at porting LiteDB v5 to Unity, and the mapper getter/setter stuff is probably fixable, but I stopped working on it because it seems that in v5, nearly every query runs through C#'s compiled lambda expression code, which will never work with IL2CPP.

If you're interested in using LiteDB under Unity, take a look at the branch of LiteDB v4 that I'm working on. I'm gradually working v5 features into it, and is intended for use in Unity so I'm only adding things that can work in AoT environments.

https://github.com/rejemy/UltraLiteDB

rejemy avatar Mar 03 '20 18:03 rejemy

@rejemy I just add in README a link for UltraLiteDB, thanks for this version!

mbdavid avatar Mar 03 '20 20:03 mbdavid

CPP。

如果您对在 Unity 下使用 LiteDB 感兴趣,请查看我正在开发的 LiteDB v4 分支。我正在逐渐将 v5 功能加入其中,并且打算在 Unity 中使用,所以我只添加了可以在 AoT 环境中工作的东西。

https://github.com/rejemy/UltraLiteDB Hello,

We're seeing the LiteDB same exception in our mobile app (both iOS and Android) using the latest version (4.2.0):

LuaException: c# exception:System.InvalidCastException: Specified cast is not valid. at (wrapper castclass) System.Object.__castclass_with_cache(object,intptr,intptr) at UltraLiteDB.PageService.GetPage[T] (System.UInt32 pageID) [0x0005a] in <95098536942a47358422eb3173b6b3d5>:0 at UltraLiteDB.CollectionService.Get (System.String name) [0x00013] in <95098536942a47358422eb3173b6b3d5>:0 at UltraLiteDB.UltraLiteEngine.GetCollectionPage (System.String name, System.Boolean addIfNotExits) [0x00005] in <95098536942a47358422eb3173b6b3d5>:0 at UltraLiteDB.UltraLiteEngine.Transaction[T] (System.String collection, System.Boolean addIfNotExists, System.Func2[T,TResult] action) [0x0004d] in <95098536942a47358422eb3173b6b3d5>:0 at UltraLiteDB.UltraLiteEngine.Insert (System.String collection, System.Collections.Generic.IEnumerable1[T] docs, UltraLiteDB.BsonAutoId autoId) [0x00041] in <95098536942a47358422eb3173b6b3d5>:0 at UltraLiteDB.UltraLiteCollection1[T].Insert (System.Collections.Generic.IEnumerable1[T] docs) [0x00026] in <95098536942a47358422eb3173b6b3d5>:0

IceFrogx avatar Sep 06 '21 02:09 IceFrogx

@IceFrogx Based on your stack trace it looks like you're using UltraLiteDB, so you might want to report that error over on the UltraLiteDB page (https://github.com/rejemy/UltraLiteDB), since it's not an official LiteDB port and is not supported here. It looks like some kind of corrupted DB (loaded the wrong page type), but without some kind of repro steps or attached database file, I don't know if there's much I can figure out about it.

rejemy avatar Sep 06 '21 16:09 rejemy

@IceFrogx根据您的堆栈跟踪,您似乎正在使用 UltraLiteDB,因此您可能希望在 UltraLiteDB 页面 ( https://github.com/rejemy/UltraLiteDB )上报告该错误,因为它不是官方的 LiteDB 端口并且这里不支持。它看起来像是某种损坏的数据库(加载了错误的页面类型),但没有某种重现步骤或附加的数据库文件,我不知道我是否可以弄清楚它。

thank. I use the BsonDocument of UltraLiteDB as a generic.

IceFrogx avatar Sep 17 '21 01:09 IceFrogx

Hi everyone! So, I tried to use the latest version of LiteDB (in its source form) with Unity, and I got it working and querying just fine in a Windows Desktop IL2CPP build. Had to apply the link.xml hint from this thread: https://forum.unity.com/threads/are-c-expression-trees-or-ilgenerator-allowed-on-ios.489498/page-2 to make the linker add the required assembly to the build, but that's about it... Unity seems to be actively improving in supporting the required features.

I can't check what is it about iOS, but this asset was referred to in the same thread, and it was said that it allows to compile the expressions for AOT.

Besides, this article claims that the expressions should work on iOS as well, as long as they're known at compile time and are not constructed dynamically during runtime.

What do you think about all this? Is this some new info that validates the usage of the latest LiteDB version within Unity? Or am I missing something?

noncom avatar Nov 26 '21 04:11 noncom

I used this link.xml https://gist.github.com/TigerHix/f270f2dc48222d17419bd46651926679 with the 5.0.11 and can confirm this version is working with unity 2020.3.33 LTS. I just copied the .dll from releases into my project along with the link.xml and everything is working.

wowthur avatar Apr 12 '22 11:04 wowthur