I have been trying to include a third party library into the C# client code and in the server code. The library i’m trying to include is either MessagePack or NewtonsoftJson. I did a little test on the server and it worked like a charm
Example serverside code:
private async Task OnTick()
{
List<int> nums = new List<int>();
Random r = new Random();
for (int i = 0; i < 10; i++)
{
nums.Add(r.Next(0, 100));
}
string value = JsonConvert.SerializeObject(nums);
Debug.WriteLine(value);
await Delay(100);
}
The code will serialize the list of ints to a string that I could send over the network. It runs fine without errors.
Now here is the crux, when I run the exact same code on the client which is setup the exact same as the server (adding reference to NewtonsoftJson) I get an error.
[ 270657] Failed to run a tick for CovClient: System.BadImageFormatException: Error verifying CovClient.CovClient/<OnTick>d__1:MoveNext (): Cannot load method from token 0x0a00001e for call at 0x003d
[ 270657] at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[TStateMachine] (TStateMachine& stateMachine) [0x0002c] in <a4e3abdd630b4c98a9d6f31a99197de6>:0
[ 270657] at CovClient.CovClient.OnTick () [0x00026] in <9ec3b266e323436b825bef59ddcd9cd0>:0
[ 270657] at System.Threading.Tasks.Task`1[TResult].InnerInvoke () [0x0000f] in <a4e3abdd630b4c98a9d6f31a99197de6>:0
It is trying to call the NewtonsoftJson library, but for some reason it gets loaded incorrectly (or not at all).
I might have the wrong idea how client deals with extra libraries and references… So tl;dr;
Can you include a 3rd party library (MessagePack, NewtonsoftJson) onto the client side? (Or is this not possible?)
I’ve looked into it… and it seems really bothersome to convert everything to a PCL (Portable Class Library). I tried using it withiout it being a PCL, gave me errors as suspected…
Because PCL is legacy, most 3rd party libraries don’t support it at all, which is really a downer
Atleast for me C# is out of the question. Simply because i can’t afford to convert everything, it’s way too time consuming. Other ways revolve around a “poor man’s” solution for serialization or what ever your 3rd party library does.
In my scenario I’m in need of a IoC container for dependency injection. Since I can’t use any of the third party libraries, I wrote my own. I don’t want to reinvent the wheel when possible. Can’t think of anything else right now.
According to this StackOverflow question, JSON.NET has a PCL version of the library available.
JSON.NET is available on NuGet and can be installed using the following command in the VS Package Manager:
Install-Package JSON.NET
Please note that this will only work if you are actually targeting the Portable Class Library with your solution, otherwise it’ll pick a different target.
Fortunately, it is open source, and the binaries can be found on the Github releases page:
Select the .zip file attached to a release, check the Bin folder for the Portable Class Library (PCL) AND SL5 target.
I haven’t tried this myself yet, but I looked into it since I’m likely going to need Json in the future, so please let me know if you can get this to work!
@_hb After talking to a couple people on the FiveM Discord, I had tried the PCL/SL5 DLL, but still had the same results.
I may look at it again in the future, because I can envision having to use JSON for other scripts later.
But for this particular case, I just simplified the data structures that I was transmitting, and ended up passing a bunch of arguments into the events. I needed three Vector3 objects, so I ended up passing nine float arguments! YUCK!
It’s kludgey as hell, but at least it works.
But like I said, I’m going to look at JSON again for a future script, so if you are able to get it working, please let me know! Actually, looking back as I’m typing this…we had an instance where an unrelated code change I made didn’t take effect until we cleared the cache on the server and restarted it. So it’s possible that the PCL/SL5 DLL will work, but I got the same error messages because I hadn’t cleared the cache.
I have a working script now, so I’m not willing to rewind at this point, lol…but if you want to try, it’s worth a shot!!
I think I was using the wrong DLL, because I revisited the issue and tried again, and it worked!
Either that, and/or the file Newtonsoft.Json.dll in your resource file has to be before your script. One/both of those fixed it for me! Now I’ve got everything in JSON!
In my Visual Studio, the client project is setup as :
Targeted framework : .NET Framework 4.5.2
Type: ClassLibrary
In my references, I directly link the NewtonJson.dll in the portable-net40+sl5+win8+wp8+wpa81 folder. NewtonJson was download with Nuget but not reference in the client project with Nugget (only on the server project).
In client log I have :
C:\Users\xxxxxxx\AppData\Local\FiveM\FiveM.app\citizen\clr2\lib\mono\4.5 System.Runtime.Serialization.dll is not a platform image (even though the dir matches).
I’m very confused, I don’t know where I make mistake.
Any body have an idea ?
Just to update everyone, I just ran into the issue and resolved it immediatly by following the info @_hb provided.
In the bin folder of NewtonSoft.Json select the portable-net40+win8+wpa81+wp8+sl5 dll and add that as a reference to your project. I then added it to my files array in the __resource.lua.
Copy that same dll file to the base of the resource directory.
Add Newtonsoft.Json.dll to __resource.lua
files {
'Newtonsoft.Json.dll'
}
This is for my server script. I have a client dll and a server dll in my resource, under client/ and server/ directories. The Newtonsoft dependency would not work if it was in the server directory, only works if it is in the root directory of the resource.
I know this is a few years old at this point, but for anyone having issues still with Mono V2 and wanting to try out C# for FiveM. My issue is I was putting all my dependencies in a directory as stated here. Mine was “Dependencies/” while theirs is “server/”. Make sure to place any dependencies in the root of the resource folder. Not sure why this makes a difference, but oh well!