• “Just being a mediocre driver has never been my ambition. That's not my style” ― Michael Schumacher
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Have a chance to win a copy of F1 2017 The Game (PC) by following RaceDepartment on Twitter, Facebook, Instagram, Twitch, Steam and / or YouTube.

Assembly Editing and Reflexil Instruction

Discussion in 'Motorsport Manager' started by TheFlamingRed, Jan 11, 2017.

  1. TheFlamingRed

    TheFlamingRed

    Messages:
    855
    Ratings:
    +131
    [You may want to sticky this in the mods forum as this is purely a resource thread for modifying the Assembly-CSharp.dll]



    The amount of modding now taking place in the Assembly-CSharp.dll is increasing. Over the last month, my mod, which normally involved changing some random variables, started to become getting more complex, with several new functions being created. As we're currently using ILSPY and REFLEXIL, changing code isn't as simple as writing some C#, but rather altering the 'instructions' and 'opcode' in the dll, altering the code indirectly.

    I was talking to Hudson (who many of you may remember did the original blue-flag fix) who taught me how to use these tools, and we talked about storing our changed codes somewhere, mostly for reference, but may benefit some other modders who wish to change their own dlls - or maybe get some more people into modding the game in general.


    To edit the Assembly-CSharp.dll you will need:
    ILSPY (http://ilspy.net/)
    REFLEXIL pluggin for ilspy (http://reflexil.net/)


    I'll post below examples of modified code and what they do (I won't show simple number changes, as that would not really be showing anything at all). I'll focus on the areas of the game I have modded as part of my Balance Mod where changes to code has taken place. I won't do them all at once, but when I find time I'll share the code and some insights here.

    Others who are modifying this file, please share your work also so we can get a decent database of changes.

    UPDATE:


    So, when I first started modifying the code, we could really only go one level - via an Injector, I could change some values. Then, one day, we leveled up! Level 2 let us use Relfexil/ILSpy to edit more of the code and begin really making some changes.

    This weekend we upgraded to Level 3! We have a new tool provided to us (and thanks to Robery Eady who provided the link). Now we have DNSpy. https://github.com/0xd4d/dnSpy/releases

    What's so special about DNSpy? Well, this:

    [​IMG]
    Yes! We can now edit the code directly using normal coding methods!

    As an example, in just a few minutes, I created that so during a collision, the part chosen is more random.

    Original ILSpy
    [​IMG]

    Edited with DNspy
    [​IMG]

    Checking with ILspy that everything is fine!
    [​IMG]


    And it works fine!
    Race 1:

    [​IMG]
    [​IMG]
    [​IMG]
    Race 2:

    [​IMG]
    (obviously, damange to these parts isn't going to be added to the mod, but I could have collisions to the rear mean its Rear Wing Damage 20% of the time, and Suspension 50% of the time, and no damage the last 30% of the time)


    So, now I can edit the code, this means that:

    • I can make far more complex changes.
    • I can make them faster
    • As I am literally changing code, I can copy all my changes into a .txt document. This means that when I have to update the mod to a new dev patch, I can use CTRL+C and CRTL+V to update an entire class, instead of spending an hour having to remake the opcodes!
    This means that I can now add as many features as I desire, and I am nearly unrestricted. Because, though I have not began to test it yet, DNSPY has this:

    [​IMG]
    If I can add classes, I can create entirely new features from scratch. Define new variables, craft new systems. I can make as many rules and manipulate variables however I wish to craft them. This means I can probably make new 'Season Rules', extend the amount of tiers, bascially anything I want.

    I still cannot make new "Game Objects", or make new UI, but the scope of watch's acheiveable has probably increased 10-fold. Which is why I asked for wishlists... Anything Code Related is probably now possible. Some things will be simpler then others, but now almost anything is possible.

    All this means is that we have New Powers. And more importantly, it means more people are probably going to be able to edit the code themselves (learning opcodes is was a new and long process, but as a mathematician, I can IF and For anything with ease just from seeing examples). This may mean I can get some more collaborators and help making new features!

     
    Last edited: Apr 3, 2017
  2. TheFlamingRed

    TheFlamingRed

    Messages:
    855
    Ratings:
    +131

    GameTimer
    GetSimulationTimeScale


    [​IMG]

    I wanted to change the gamespeeds as I felt the fastest 'fast-forward' option was far too slow/ Particularly as I spend a lot of time testing my mod, and I really want to get through these races quicker.

    However, the original game speeds are fixed, can cannot be edited simply by changing a number. This is because the number is part of an Array, and by default in this dll, the numbers stored in the arrays are set to Private, and thus, cannot be modified. So with a simple option not available, I gutted this section of code with new IF statements. (Big thanks to Hudson here as he led me through the process of converting my desired IF statements into the Instructions and OpCode. I have been able to use this example to modify various other areas of the game)


    [​IMG]

    Something strange has happened to this code since editing it. It's gone all 'Global' on me, which I assume is some sort of reference issue. However in this case, the code does what it's intended so if it isn't broke, don't fix it.

    I once tried a x50 speed, and while it theoretically works, the game struggles to keep up on my system and is very choppy. You can get away with an x20 though.

    For those wanting an x1... while its possible I don't recommend it. The Physics of the cars are simplified, and watching races at x1 speed doesn't look good.


     
    Last edited: Jan 11, 2017
  3. TheFlamingRed

    TheFlamingRed

    Messages:
    855
    Ratings:
    +131

    CrasgDirector
    OnSessionStarting


    [​IMG]

    This was an interesting one. It seems before the green lights even go off, the number of crashes which will happen in the race have already been decided, with the upper limit being two.

    Apart from that being quite static, even the Upper Limit of 2 is not always achievable. I changed it so that there should by 8 crashes each and every race, yet sometimes there would only be 5, other times only 3.... in Short, the values are Upper Bounds. Even if the mSessionCrashCount is 2, only one crash may occur depending what happens in the race.

    Needless to say sometimes we want more then two crashes ever, so this was a simple extension of the code to allow for upto 5 crashes in a single race, with probabilities for each occurance.


    [​IMG]



     
    Last edited: Jan 11, 2017
  4. TheFlamingRed

    TheFlamingRed

    Messages:
    855
    Ratings:
    +131

    AISafetyCarBehaviour
    OnEnter



    [​IMG]

    A nice short and sweet one. The variable mLapsLength = 1 is the number of laps that the safety car is deployed for. I don't like how it's such a predictable event. Sure, if its too long some people may get bored, which is probably the reason the Devs decided to stick with 1 lap.

    However, a few posts ago I increased the fast forward speed, so if you're behind a safety car for a few laps and you don't want to watch, you can now let time pass you buy quickly.

    So in the most recent experimental edition of my balance mod, I changed the length of the safety car period to be anywhere between 1 and 4 laps.

    To do this, is a simple bit of code, a half dozen instruction all in one line. Bit it can be quite powerful as it can be used to randomise a number of different things which would normally have only one value. There was nothing really creative here, I stole the line directly from the Virtual Safety Car coding in 'Crash Behavior' and re-purposed it for the real safety car.


    [​IMG]


     
    Last edited: Jan 12, 2017
  5. Maurits Vernel

    Maurits Vernel

    Messages:
    86
    Ratings:
    +14
    Do you think you can have a look at editing the file so we can have more than ten teams in the game?
     
  6. TheFlamingRed

    TheFlamingRed

    Messages:
    855
    Ratings:
    +131
    Unlikely, it being more of a physical issue. 20 grid slots on the maps, several UI spaces designed to only handle 10 teams. There's a lot of issues with it and would likely need a full team to make any progress what-so-ever, with a likely chance that it wouldn't be possible with our current resources.

    If it's an consolation, I can probably adjust it so you can have 9 teams in a series (this might actually be quite simple).
     
  7. dimagico

    dimagico

    Messages:
    332
    Ratings:
    +19
    which could happens... if Manor will not be on grid.:ninja::cool:
     
  8. Beynasuh

    Beynasuh

    Messages:
    489
    Ratings:
    +28
    Hi FlamingRed, have a question about the Variable Safety Car Deployment. If I wanted to change the min and max to 2-5 which values (or lines need I to change). [Have ILSpy version 2.4.0.1963 & Reflexil v2.1]
     
  9. TheFlamingRed

    TheFlamingRed

    Messages:
    855
    Ratings:
    +131
    Ooooh, Requests.

    CrashDirector
    SetVirtualSafetCarFlag()

    You need to get your screen looking like this:
    [​IMG]

    this.mVirtualSafetyFlagDuration = (float)(Mathf.RoundToInt((float)global::RandomUtility.GetRandomInc(3, 8)) * 15);
    This is the key line: It's saying A Random Amount, Minimum = 3 * 15 = 45s. Maximum = 8 * 15 = 120s

    You can see the "15" part of the equation is in line "10": Opcode: "idc.i4.s". Instruction: "SByte" "15"
    You can change 15 to anything you like, and the Max and Min formula will update. Currently in line 11, the 15 is being "Mul"tiplied to the section before it. You can change that to a "add"ition, "sub"traction or "div"isor

    The values 3 and 8 are actually Lines "05" and "06". The Opcode "idc.i4.3" is just the number 3. And you guessed it. "idc.i4.8" is the number 8. This style of numbers using only the Opcode only go from 0 to 8, so feel free to change the opcode to whichever range you want. If you want a value outside that, you need to change the Opcode to something else, like "idc.i4.s" "Sbyte" "Any Number".

    That should give you enough to go on. Look for patterns in the codes, and you'll be able to alter the code in more ways.
     
  10. Beynasuh

    Beynasuh

    Messages:
    489
    Ratings:
    +28
    Thanks for the help FlamingRed

    Edit: That is for the Virtual SC I guess, and what about the real Safety Car. How can I change his min and max variables.
     
    Last edited: Jan 20, 2017
  11. nonleague

    nonleague

    Messages:
    15
    Ratings:
    +0
    Hi FlamingRed I have read all about your Balance Mod and would like to try it although because I am new to Motorsport Manager I would like to get to grips with the game (currently using Enzo's mod) but would love to increase the speed of the race like you have shown above. Unfortunately for me, I have never modded anything in my life and have downloaded both tools that you have advised but have now spent 2 hours looking for a guide upon how to use them to successfully edit the said file.

    I have found the file which needs editing in ILSpy but it is locked and will not allow me to edit anything, further more I cannot open Reflexi to have a table like you have shown in the screenshot above. Please could you give me a quick step by step guide upon how to use both of these tools successfully or a link to some kind of guide.

    Perhaps it would be good if you could create an edited .dll file with the new speeds so people can just replace the existing .dll with the new speeds already created.

    Thanks in advance!
     
  12. TheFlamingRed

    TheFlamingRed

    Messages:
    855
    Ratings:
    +131
    Um... I'll add a couple of screenshots showing folder layout in my ilspy which may help? Do you see the little gear symbol in il spy?

    I read the original message but never saw the edit until today.

    Post 4 - second screenshot - lines 12 and 13 are the min /max number of laps. It's using the id4.ic.x system of numbering which I told you about when editing VSC times)
     
  13. nonleague

    nonleague

    Messages:
    15
    Ratings:
    +0
    I have downloaded Reflector. This is what I see but it's content is slightly different than your screenshot. Can you guide me from here please?
     

    Attached Files:

  14. TheFlamingRed

    TheFlamingRed

    Messages:
    855
    Ratings:
    +131
    Ah, you're using the standard reflexi, as opposed to the ilspy version... I assume though that you can just right click the lines in the instructions and modify them as I have on mine
     
  15. nonleague

    nonleague

    Messages:
    15
    Ratings:
    +0
    I had downloaded ILSpy 2.1 but I could not find a way to open Reflexi so I downloaded Reflector as I followed a guide on youtube. What I need to know now is how do I edit the file to add the code lines that you have made? Also, why does your original screenshot say GameTimer when that word is not included in my screenshot?
     
  16. TheFlamingRed

    TheFlamingRed

    Messages:
    855
    Ratings:
    +131
    It does... in a different place... look at the top left of the screenshot you have.
     
  17. nonleague

    nonleague

    Messages:
    15
    Ratings:
    +0
    Okay so I have now right clicked the lines within Reflector and have tried to copy your screenshot although at line 12 it does not give me the ability to change it to 15 like you have. Take a look at my screenshot below please and could you explain why or where I am going wrong.



    Am I supposed to just edit lines only or am I supposed to write code?
     

    Attached Files:

  18. TheFlamingRed

    TheFlamingRed

    Messages:
    855
    Ratings:
    +131
    You'd probably do better deleting most of it... and starting again adding in new instructions from the start...
    Remember, you can change the top box (opcode) and the operand type as well.
     
  19. nonleague

    nonleague

    Messages:
    15
    Ratings:
    +0
    So sorry I am taking up your time with this but I have absolutely no idea with code and/or modding. If you look at the screenshot I posted above, it shows that the Operand instruction -> (15) Idarg.o does not exist in my drop down menu. How can I create this?
     
  20. TheFlamingRed

    TheFlamingRed

    Messages:
    855
    Ratings:
    +131
    thats because at line 15 you have "ret". You need to manually create most of the lines here. If you right click the instruction space, you should be able to "add" new lines.