de4dot-cex/AssemblyData/AssemblyService.cs

94 lines
2.6 KiB
C#
Raw Normal View History

2011-09-22 10:55:30 +08:00
/*
2015-10-30 05:45:26 +08:00
Copyright (C) 2011-2015 de4dot@gmail.com
2011-09-22 10:55:30 +08:00
This file is part of de4dot.
de4dot is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
de4dot is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with de4dot. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Reflection;
using System.Threading;
namespace AssemblyData {
2013-10-03 00:07:28 +08:00
public abstract class AssemblyService : MarshalByRefObject, IAssemblyService {
2011-09-22 10:55:30 +08:00
ManualResetEvent exitEvent = new ManualResetEvent(false);
2013-10-03 00:07:28 +08:00
protected Assembly assembly = null;
2011-09-28 10:30:00 +08:00
AssemblyResolver assemblyResolver = new AssemblyResolver();
2013-10-03 00:07:28 +08:00
public static AssemblyService Create(AssemblyServiceType serviceType) {
switch (serviceType) {
case AssemblyServiceType.StringDecrypter:
return new StringDecrypterService();
case AssemblyServiceType.MethodDecrypter:
return new MethodDecrypterService();
2013-10-26 03:45:34 +08:00
case AssemblyServiceType.Generic:
return new GenericService();
2013-10-03 00:07:28 +08:00
default:
throw new ArgumentException("Invalid assembly service type");
}
}
public static Type GetType(AssemblyServiceType serviceType) {
switch (serviceType) {
case AssemblyServiceType.StringDecrypter:
return typeof(StringDecrypterService);
case AssemblyServiceType.MethodDecrypter:
return typeof(MethodDecrypterService);
2013-10-26 03:45:34 +08:00
case AssemblyServiceType.Generic:
return typeof(GenericService);
2013-10-03 00:07:28 +08:00
default:
throw new ArgumentException("Invalid assembly service type");
}
}
2011-09-22 10:55:30 +08:00
2013-01-19 20:03:57 +08:00
public void DoNothing() {
2011-09-22 10:55:30 +08:00
}
2013-10-26 03:45:34 +08:00
public virtual void Exit() {
exitEvent.Set();
}
2013-01-19 20:03:57 +08:00
public void WaitExit() {
exitEvent.WaitOne();
}
public override object InitializeLifetimeService() {
return null;
}
2013-10-03 00:07:28 +08:00
protected void CheckAssembly() {
2011-09-22 10:55:30 +08:00
if (assembly == null)
2013-10-03 00:07:28 +08:00
throw new ApplicationException("LoadAssembly() hasn't been called yet.");
2011-09-22 10:55:30 +08:00
}
2013-10-03 00:07:28 +08:00
protected void LoadAssemblyInternal(string filename) {
2011-09-22 10:55:30 +08:00
if (assembly != null)
throw new ApplicationException("Only one assembly can be explicitly loaded");
try {
2013-01-19 20:03:57 +08:00
assembly = assemblyResolver.Load(filename);
2011-09-22 10:55:30 +08:00
}
2014-02-16 04:14:59 +08:00
catch (BadImageFormatException ex) {
throw new ApplicationException(string.Format("Could not load assembly {0}. Maybe it's 32-bit or 64-bit only?", filename), ex);
2011-09-22 10:55:30 +08:00
}
}
}
}