【问题标题】:Add Embedded Resource to specific path from Assembly compiled at Runtime将嵌入式资源添加到运行时编译的程序集中的特定路径
【发布时间】:2021-03-24 01:43:25
【问题描述】:
public void Compile()
    {
        string OutputPath = Application.dataPath + "/" + Path.Combine(Consts.OutputPath, DLLName) + ".dll";
        Debug.Log(OutputPath);
        var csc = new CSharpCodeProvider(new Dictionary<string, string>() { { "CompilerVersion", "v3.5"} });
        var parameters = new CompilerParameters(new[] { "mscorlib.dll", "System.Core.dll", "C:/Program Files/Unity/Editor/Data/Managed/UnityEngine.dll", Application.dataPath + "/" + Consts.IPADLLPath }, OutputPath, true);
        string ManifestPath = Application.dataPath + "/Exporters/manifest.json";
        string ManifestJson = File.ReadAllText(ManifestPath);
        ManifestJson = ManifestJson.Replace("Template", DLLName);
        Debug.Log(ManifestJson);
        File.WriteAllText(ManifestPath, ManifestJson);
        if (csc.Supports(GeneratorSupport.Resources))
        {
            parameters.EmbeddedResources.Add(ManifestPath);
        }
        parameters.GenerateExecutable = true;
        parameters.CompilerOptions = "/optimize -target:library";
        string Code = Consts.BSIPAPluginStarter;
        List<string> Usings = new List<string>();
        Usings.Add(Consts.UsingIPA);
        for(int i = 0; i < scriptCollection.Length; i++)
        {
            string FileStr = File.ReadAllText(Path.Combine(Application.dataPath, scriptCollection[i]));

            int pFrom = FileStr.IndexOf("Using ") + "Using ".Length;
            int pTo = FileStr.LastIndexOf(";");
            //Debug.Log(string.Format("{0}, {1}", pFrom, pTo));

            string result = FileStr.Substring(pFrom-3, (pTo - pFrom)+4);
            Usings.Add(result);
            FileStr = FileStr.Remove(pFrom - 3, (pTo - pFrom) + 5);
            Debug.Log(FileStr);
            //Debug.Log(result);
            Code += FileStr;
        }
        string NewCode = "";
        Usings.ForEach((string Using) => { NewCode += Using; });
        NewCode += Code;
        Code = NewCode;
        Debug.Log(Code);
        if(File.Exists(OutputPath))
        {
            string[] Files = Directory.EnumerateFiles(Path.GetDirectoryName(OutputPath)).Where((string path) => { return path.Contains(DLLName); }).ToArray();
            for (int i = 0; i < Files.Length; i++)
            {
                Debug.Log(Files[i]);
                File.Delete(Files[i]);
            }
        }
        CompilerResults results = csc.CompileAssemblyFromSource(parameters, Code);
        results.Errors.Cast<CompilerError>().ToList().ForEach(error => Debug.LogError(error.ErrorText + " " + error.Line + ":" + error.Column));
        EditorUtility.DisplayDialog("Export Succesful", string.Format("Exported To {0}, Remember this doesn't work for quest and will only work on pc, any object that uses the scripts you made won't have any scripts attactched to them!", OutputPath), "OK");
    }

我的编译函数确实为我提供了一个 .dll。但我的问题是 manifest.json 需要位于资源中的某个路径。

在这张图片中,它直接位于 manifest.json,但我需要它位于 MyCustomScript.manifest.json。

具有正确路径的 Manifest.json 的 dll 示例

编辑:我设法通过将 manifest.json 重命名为 MyCustomScript.manifest.json 来解决它

【问题讨论】:

    标签: c# embedded-resource runtime-compilation


    【解决方案1】:

    如果您使用 MSBuild 进行构建,则可以在项目文件中进行设置,例如:

    <ItemGroup>
      <EmbeddedResource Include="foo.json">
        <LogicalName>bar.json</LogicalName>
      </EmbeddedResource>
    </ItemGroup>
    

    看看MSBuild如何将这个传递给csc.exe,它变成了

    /resource:foo.json,bar.json
    

    您也可以在documentation page for the /resource flag 上确认此语法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-17
      • 1970-01-01
      • 2018-09-21
      • 2018-10-15
      • 2017-11-27
      • 1970-01-01
      相关资源
      最近更新 更多