Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Remove code on build marked with attributes (without using compiler ifs)

Discussion in 'Scripting' started by 6565, Dec 22, 2016.

  1. 6565

    6565

    Joined:
    Feb 17, 2013
    Posts:
    19
    I was wondering if it's possible to piggy back on Unity's build process and tell it to ignore building specific classes/functions if marked by specific attributes.

    For example, we use UNET and would like to build a server only build and a client only build.
    I could go over the entire project and wrap it with compiler ifs, but I would rather use the [Client] / [Server] attribute model we've been using from the start with UNET.
     
    Zelek likes this.
  2. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    Most people would make a different project, that works as a server, because it has completely different attributes. You most likely wont have a "Play!" button in a server client, or a "Start Server" in the game
     
  3. 6565

    6565

    Joined:
    Feb 17, 2013
    Posts:
    19
    While they do handle these things differently, we do use the same code and project as it's shared between the server and the clients.
     
  4. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    Then look into prepocessor directives
     
  5. 6565

    6565

    Joined:
    Feb 17, 2013
    Posts:
    19
    gorbit, I explained that I wanted to avoid using preprocessor directives (#if) and looking for a way to piggy back on postprocessing of Unity if possible
     
  6. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,181
    The only thing that's close is the [Conditional] attribute, but that can only make void methods conditional.

    You'll either have to live with all the code in both places, or redesign such that common code is in it's own project that both of your other projects depend on.
     
  7. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    I don't think this would work anyway for what you're describing. UNet generates a bunch of code based around attributes and you may run into connection issues if you have CRC checks enabled.
     
  8. Dameon_

    Dameon_

    Joined:
    Apr 11, 2014
    Posts:
    542
    Sounds like you've incurred some tech debt by using the convenience route of treating the server and the client as one thing, and now you're trying to avoid paying that debt.

    There is a way, and it's to build a whole other script to read your other scripts and strip out the relevant bits, and put it in the build pipeline. Frankly, that's as much work as paying your debt and redesigning like @Baste suggested, and will probably just lead to more issues down the line.
     
    TaleOf4Gamers and lordofduct like this.
  9. XaeroDegreaz

    XaeroDegreaz

    Joined:
    Feb 6, 2010
    Posts:
    15
    I was also looking into this, curious how to keep server code out of the hands of end-users.

    The issue @KelsoMRK brings up about the CRC checks was also something I thought would cause a problem. I've also thought about separating the server and client into separate projects, but then how would you be able to get attributes like [Command] to work?

    For instance, methods marked with [Command] that are called from clients need to be available to clients, and this is where our problem lies.

    Is there a way to use a common project with some sort of proxy objects that I haven't come across yet? Sort of like bundling an API client per-say?

    This is a very interesting problem to solve, and it may just be my ignorance of how the new networking works -- I'd love to hear some more detailed ideas on how to solve it.
     
  10. kru

    kru

    Joined:
    Jan 19, 2013
    Posts:
    452
    It may be possible to use Fody to empty the bodies of methods tagged with specific attributes. https://github.com/jbruening/UnityFody

    I've only played with fody a bit. It seems impressive, but also extraordinarily dangerous. Heh.
     
  11. XaeroDegreaz

    XaeroDegreaz

    Joined:
    Feb 6, 2010
    Posts:
    15
    I guess that does answer the original question. However you'd still fail CRC checks between server and host unless you disable the checks which will be a security risk.