Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by TheNoobieWaffle · Oct 29, 2017 at 10:05 AM · assemblyassemblies

Using Dynamically Generated Assemblies for Mods

So, I have this nifty code in my Unity project:

 using System;
 using System.Reflection;
 using System.CodeDom.Compiler;
 using System.Collections.Generic;
 using System.IO;
 
 using UnityEngine;
 
 public class ModLoader {
 
     private List<Mod> mods;
     public IReadOnlyList<Mod> Mods {
         get { return mods.AsReadOnly(); }
     }
 
     public ModLoader() {
         mods = new List<Mod>();
 
         LoadMod("mod.txt");
         Debug.Log(mods[0].ModAssembly.GetType("ModData").GetField("modName").GetValue(null));
         Debug.Log(mods[0].ModAssembly.GetType("ModData").GetMethod("Str").Invoke(null, BindingFlags.Static, null, null, null));
         mods[0].ModAssembly.GetType("ModData").GetMethod("Run").Invoke(null, BindingFlags.Static, null, null, null);
     }
 
     public void LoadMod(string filePath) {
         if (mods.Count >= 999) {
             Debug.LogError("Cannot load mod. Max mods loaded (999).");
             return;
         }
 
         string generatedName = "ModAssembly" + (mods.Count < 10 ? "00" : (mods.Count < 100 ? "0" : "")) + mods.Count + ".dll";
         filePath = (Application.dataPath + @"\Mods" + ((filePath.Replace("/", @"\"))[0] == '/' ? "" : "/")).Replace("/", @"\") + filePath;
         Debug.Log("Generated Name: " + generatedName + "      File Path: " + filePath);
 
         CompilerParameters parameters = new CompilerParameters();
         parameters.GenerateExecutable = false;
         parameters.GenerateInMemory = true;
         parameters.OutputAssembly = generatedName;
         CompilerResults r = CodeDomProvider.CreateProvider("CSharp").CompileAssemblyFromFile(parameters, filePath);
 
         mods.Add(new Mod(Assembly.LoadFrom(generatedName), "test"));
 
         File.Delete(Directory.GetParent(Application.dataPath) + @"\" + generatedName);
     }
 }

and as you can see, in the constructor I use the LoadMod method. The source of the file it uses is:

 public class ModData {
     public static string modName = "Super kool mod";
     public static string modVersion = "1.2.1";
 
     public static void Run() {
 
     }
     
     public static string Str() {
         return "This is generated! :3";
     }
 }

And, that all works as expected. The console will read: "Super kool mod", then "This is generated! :3". My question is: is it possible to access anything from my current project from that code that is loaded? I'm thinking that mods with full control could be really beneficial. Of course, I would have to do a kind-of scan to make sure it does not do anything malicious. Anyway, that's aside the point. If I try to add using UnityEngine; in the loaded code, I get this error: FileNotFoundException. Now, is there anywhere I can place the UnityEngine.dll so that it can be found? Is what I am trying to do possible? I think it is not, but I figured I would try and ask before giving up hope.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Bunny83 · Oct 29, 2017 at 12:15 PM

You need to add all external referenced assemblies in the "ReferencedAssemblies" collection of the "CompilerParameters".

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TheNoobieWaffle · Oct 29, 2017 at 10:02 PM 0
Share

Would I still be able to access the classes in my Unity project?

I added UnityEngine.dll to the referenced assemblies, and was able to instantiate a new game object and see if Application.isPlaying without issue. But suppose my Unity project has a C# class called "Game". Can I access that?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

72 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

ApplicationException: Unable to find a suitable compiler? 1 Answer

Unity/C# Added assembelies, MonoDevelop builds, Unity won't run 0 Answers

IL2CPP mod support 1 Answer

Assembly Definition Files exclude specified subfolder 0 Answers

Keep assemblies in separated dll instead of GameAssembly.dll 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges