static void Main(string[] args) { if (args.Length == 0) { Console.WriteLine(@"Expected file name. Please run: ./ardaans file"); return; } string filePath = args[0]; // C:/Users/yoann/Desktop/test.asm try { byte[] code = Assembler.AssembleFile(filePath); var vm = new VirtualMachine(code); vm.Run(); vm.PrintState(); } catch (FileNotFoundException e) { Console.WriteLine(e.Message); } catch (IOException e) { Console.WriteLine(e.Message); } catch (FailedAssemblingException e) { Console.WriteLine(e.Message); } }
public void AssembleFile() { var asm = new Assembler(); var exe64 = asm.AssembleFile("PE64DEMO.ASM"); var exe32 = asm.AssembleFile("PEDEMO.ASM"); }
public bool ConvertAssemblyReferences(string strDllPath) { try { if (!File.Exists(strDllPath)) { throw new FileNotFoundException(strDllPath); } string strBackUpFile = Util.Backup(strDllPath); string strTempFile = strDllPath + Constants.VORTEX_TEMP_SUFFIX; File.Copy(strDllPath, strTempFile, true); AssemblyDefinition modAssembly = AssemblyDefinition.ReadAssembly(strTempFile); var references = modAssembly.MainModule.AssemblyReferences; AssemblyNameReference ummRef = references.FirstOrDefault(res => res.Name == Constants.UMM_ASSEMBLY_REF_NAME); if (ummRef == null) { // There's no assembly to convert - we're good to go. return(true); } AssemblyNameReference newRef = new AssemblyNameReference( VortexPatcher.InstallerAssembly.Name.Name, VortexPatcher.InstallerVersion); int idx = modAssembly.MainModule.AssemblyReferences.IndexOf(ummRef); modAssembly.MainModule.AssemblyReferences.Insert(idx, newRef); modAssembly.MainModule.AssemblyReferences.Remove(ummRef); modAssembly.Write(strDllPath); modAssembly.Dispose(); // Disassemble and replace unwanted namespace/class calls. string strDisassembled = Disassembler.DisassembleFile(strDllPath); string strReplaced = Regex.Replace(strDisassembled, "UnityModManagerNet.UnityModManager", "VortexHarmonyInstaller.ModTypes"); // Prepare for File.Delete(strTempFile); File.Delete(strDllPath); // Re-assemble the mod file. File.WriteAllText(strTempFile, strReplaced); Assembler.AssembleFile(strTempFile, strDllPath); File.Delete(strTempFile); File.Delete(strBackUpFile); return(true); } catch (Exceptions.AssemblyIsInjectedException exc) { // We already dealt with this mod, no need to hijack its calls again. VortexPatcher.Logger.Debug(exc); return(true); } catch (Exception exc) { Util.RestoreBackup(strDllPath); VortexPatcher.Logger.Error("Assembly conversion failed", exc); return(false); } //string strTempFile = strDllPath + Constants.VORTEX_TEMP_SUFFIX; //File.Copy(strDllPath, strTempFile, true); //// UMM mods reference the UnityModManagerNet.UnityModManager assembly. //// we need to replace this reference with our own assembly. //using (AssemblyDefinition modAssembly = AssemblyDefinition.ReadAssembly(strTempFile, new ReaderParameters { ReadWrite = true })) //{ // var methodDefinitions = modAssembly.Modules.SelectMany(mod => ModuleDefinitionRocks.GetAllTypes(mod)) // .SelectMany(t => t.Methods) // .Where(method => null != method.Body); // // Get the type reference we want to inject. // TypeDefinition modEntryTypeDef = Util.GetTypeDef(VortexPatcher.InstallerAssembly, "ModEntry"); // MethodDefinition modEntryCtor = Util.GetCtorDef(modEntryTypeDef); // modAssembly.MainModule.ImportReference(modEntryTypeDef); // foreach (MethodBody body in methodDefinitions.Select(m => m.Body)) // { // ILProcessor processor = body.GetILProcessor(); // List<Instruction> instructions = body.Instructions // .Where(instr => instr.ToString().Contains(Constants.UMM_TYPENAME)) // .ToList(); // foreach (var instr in instructions) // { // switch (instr.OpCode.Name) // { // case "ldftn": // // This is a Function/Method definition - replace parameter types. // MethodDefinition methDef = instr.Operand as MethodDefinition; // if (null != methDef) // Util.ConvertParameterTypes(methDef, modEntryTypeDef, "ModEntry"); // break; // case "newobj": // MethodReference methRef = instr.Operand as MethodReference; // if (null != methRef) // Util.ConvertGenericArguments(methRef, modEntryTypeDef); // break; // case "stfld": // break; // case "ldfld": // break; // case "call": // break; // } // //string // //string newstring = what.Replace("UnityModManagerNet.UnityModManager", "VortexHarmonyInstaller.ModTypes"); // //var newVal = processor.Create(instr.OpCode, VortexPatcher.InstallerAssembly); // //processor.Replace(instr, newVal); // //var stringEndArg = GetStringArgument(instr); // //var writeInstruction = processor.Create(OpCodes.Call, changeTextMethodRef); // //processor.InsertAfter(stringEndArg, writeInstruction); // } // } // if (modAssembly == null) // throw new Exceptions.AssemblyReplacementException( // String.Format("Failed to read assembly: {0}", strDllPath)); // var references = modAssembly.MainModule.AssemblyReferences; // AssemblyNameReference ummRef = references.FirstOrDefault(res => res.Name == Constants.UMM_ASSEMBLY_REF_NAME); // if (ummRef == null) // { // // There's no assembly to convert - we're good to go. // return true; // } // AssemblyNameReference newRef = new AssemblyNameReference( // VortexPatcher.InstallerAssembly.Name.Name, // VortexPatcher.InstallerVersion); // int idx = modAssembly.MainModule.AssemblyReferences.IndexOf(ummRef); // modAssembly.MainModule.AssemblyReferences.Insert(idx, newRef); // modAssembly.MainModule.AssemblyReferences.Remove(ummRef); // modAssembly.Write(strDllPath); // return true; //} }