Add Embedded Resource to specific path from Assembly compiled at Runtime

501 views Asked by At
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");
    }

I have my Compile function that does provide me a .dll. But my issue is that manifest.json needs to be at a certain path in the resources.

In this image it's directly at manifest.json but I need it at MyCustomScript.manifest.json.

Example of dll with Manifest.json at correct path

Example of dll with Manifest.json at correct path

EDIT: I managed to work around it by renaming manifest.json to MyCustomScript.manifest.json

1

There are 1 answers

0
Jimmy On

If you were building using MSBuild, this would be settable in the project file like:

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

Looking at how MSBuild passes this to csc.exe, it turns into

/resource:foo.json,bar.json

You can also confirm this syntax on the documentation page for the /resource flag.