Skip to content

Commit

Permalink
1.2 修复依赖被占用的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
3DMXM committed Sep 23, 2022
1 parent 54a5ea8 commit f5fa28a
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions ScriptEngine/ReloadPlugins.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,11 @@ public void LoadMods(string path)

var defaultResolver = new DefaultAssemblyResolver();
defaultResolver.AddSearchDirectory(scirptsPath);

var dll = AssemblyDefinition.ReadAssembly(path, new ReaderParameters { AssemblyResolver = defaultResolver });
dll.Name.Name = $"{dll.Name.Name}-{DateTime.Now.Ticks}";

Debug.Log($"dll.Name.Name : {dll.Name.Name}");
//Debug.Log($"dll.Name.Name : {dll.Name.Name}");

var ms = new MemoryStream();
dll.Write(ms);
Expand All @@ -149,22 +149,32 @@ public void LoadMods(string path)
string file = Path.Combine(scirptsPath, $"{item.Name}.dll");
if (File.Exists(file))
{
Assembly.LoadFile(file);
var dllLibs = AssemblyDefinition.ReadAssembly(file, new ReaderParameters { AssemblyResolver = defaultResolver });
dllLibs.Name.Name = $"{dllLibs.Name.Name}";
var ms2 = new MemoryStream();
dllLibs.Write(ms2);
var ass2 = Assembly.Load(ms2.ToArray());

Debug.Log($"加载依赖 {item.Name} 成功");
//var fas = Assembly.Load(path);

// 将依赖附加上原dll上
dll.MainModule.AssemblyReferences.Add(dllLibs.MainModule.AssemblyReferences[0]);
// 解除原dll的占用
dllLibs.Dispose();

}

}
catch (Exception e)
{
Debug.Log($"加载依赖 {item.Name} 失败");
}

}
}

scriptManager.AddComponent(type);
Debug.Log($"加载 {path} 成功");
}
}
}
}
dll.Dispose();
}
Expand Down

0 comments on commit f5fa28a

Please sign in to comment.