Unity-Technologies / Unity-Technologies/com.unity.netcode.gameobjects

How to trigger NGO CodeGen via custom buildpipeline?

未關閉
#2,747 54 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

type:support
主要語言
C#
星號
2.3k
分支
461
平均合併
3 天 16 小時
30 天內合併 PR
20

描述

This is a long shot and I'm assuming/guessing things based on what I have researched.
I also know I'm pushing the limits here - but hey somebody have to do it :)

Background

The background for this is that my game supports mods and I'm currently working on adding multiplayer to my game using NGO.

So far things are going pretty good - but there is one major issue I still haven't figured out and it's a really big deal as mod creators will be very limited if I can't get this to work.

I'm using UMod to help support mods in my game (https://assetstore.unity.com/packages/tools/integration/umod-2-0-58293).
As far as I understand, it adds feature on top and around Asset Bundles.

The way UMod work is pretty nice.

As the developer of the game (me) I setup various things, like what scripts should be included in the "Mod Tools" and other settings. I then build the "Mod Tools" for my game from inside Unity. This result in a .unitypackage file.

As the mod creators (people from the community) you then open up a fresh Unity and import this .unitypackage file (Mod Tools) and get some Unity windows to create a "mod" and can then build a mod - which can include custom scripts.

The mod creator then "build mod" from a custom Unity menu made by UMod, which generates a .mod file that the game can then pick up. At least simply put - the important part here is the UMod builds the mod here. Remember that.

The problem

So, I have discovered that ClientRPCs, ServerRPCs and NetworkVariables are NOT working if a modder is using it in his custom scripts.

I have done some research that indicates (and here comes the guessing part) that NGO is doing some CodeGen at "game build time" - I guess here: https://github.com/Unity-Technologies/com.unity.netcode.gameobjects/blob/develop/com.unity.netcode.gameobjects/Editor/CodeGen/NetworkBehaviourILPP.cs

I'm guessing that it doesn't work, because this CodeGen is not happening on the custom scripts in the mod when it's build by UMod.

UMod supports custom BuildProcessors to run custom code when building the mods like this:
using System.Collections.Generic;
using UMod.BuildEngine;
using UMod.BuildPipeline;
using UMod.BuildPipeline.Build;
using UMod.Shared.Resources;
using UnityEngine;

// Add a build processor for script assets since we are only interested in scripts that will be compiled by uMod.
// High priority means that it will run after script compilation so build assemblies collection will already be filled out with compiled assemblies.
[UModBuildProcessor(".cs", 1000, true)]
public class PatchAssembliesExample : BuildEngineProcessor
{
    // Methods
    // Called by the build engine just after compilation if the mod includes any script content, passing all script assets as a parameter (Not interested in those).
    public override void ProcessAssetBatch(BuildContext context, IEnumerable<BuildPipelineAsset> assets)
    {
        // get assemblies
        ModAssemblyEntry[] entries = context.BuildAssemblies.Assemblies;

        // Patch all
        foreach(ModAssemblyEntry entry in entries)
        {
            PatchAssembly(context, entry);
        }
    }

    private void PatchAssembly(BuildContext context, ModAssemblyEntry entry)
    {
        // Name info
        string asmName = entry.AssemblyName;

        // Get assembly image - loadable by 'Assembly.Load'
        byte[] asmImage = entry.AssemblyImage;


        // TODO - Run custom patcher code


        // Get the patched result
        byte[] asmImagePatched = ...;

        // Check for patched
        if (asmImagePatched != null)
        {
            // Remove original assembly
            context.BuildAssemblies.RemoveFromBuild(entry);

            // Register new assembly
            if (context.BuildAssemblies.RegisterAssemblyForBuild(asmImagePatched, true) == false)
                Debug.LogWarning("Failed to register patches assembly for build! : " + asmName);
        }
    }
}

So, here comes the million dollar question that will make the sky the limits for modders of my game - how can I trigger this CodeGen for the scripts in the mod?

Note that NGO is installed as a dependency to the Mod Tools, so NGO is installed in the mod creators Unity project.

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

從 Editor/CodeGen/NetworkBehaviourILPP.cs 和 issue 中的 UMod BuildProcessor 範例開始。確認 NGO CodeGen 是否能夠處理由該自訂建置管線產生的組件,並在無法處理時記錄受支援的整合路徑或限制。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
csharp, unity
領域
game-dev, networking
Issue 類型
功能
難度
5/5
預估耗時
一週以上
活躍度
停滯
描述清晰度
需要釐清
新手友好度
25/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。