internal ResWriterData( IResourceWriter resWriter, Stream memoryStream, String strName, String strFileName, String strFullFileName, ResourceAttributes attribute) { m_resWriter = resWriter; m_memoryStream = memoryStream; m_strName = strName; m_strFileName = strFileName; m_strFullFileName = strFullFileName; m_nextResWriter = null; m_attribute = attribute; }
} // End Sub Test public static void WriteAnyResourceFile <T>(System.Collections.Generic.Dictionary <string, object> dictionary, params object[] args) where T : System.Resources.IResourceWriter { // new System.Resources.ResXResourceWriter() // new System.Resources.ResourceWriter() using (System.Resources.IResourceWriter writer = (System.Resources.IResourceWriter)System.Activator.CreateInstance(typeof(T), args)) { foreach (System.Collections.Generic.KeyValuePair <string, object> item in dictionary) { writer.AddResource(item.Key, item.Value); } // Next item writer.Generate(); writer.Close(); } // End Using writer } // End Sub WriteAnyResourceFile
public static void Create(string name, Guid guid, string dllPath, IDictionary <string, object> resourceList, string actionPath, dynamic menuFormat, Platform.ComRegisterClass.RightClickContextMenuOptions[] association, string[] fileExtensionFilter, bool expandFileNames) { AssemblyName myAsmName = new AssemblyName(System.IO.Path.GetFileNameWithoutExtension(dllPath)); myAsmName.CodeBase = String.Concat("file:///", System.IO.Path.GetDirectoryName(dllPath)); myAsmName.CultureInfo = new System.Globalization.CultureInfo("en-US"); myAsmName.KeyPair = new StrongNameKeyPair(CreateKeyPair(System.IO.Path.GetFileNameWithoutExtension(dllPath), 1024)); myAsmName.Flags = AssemblyNameFlags.PublicKey; myAsmName.VersionCompatibility = AssemblyVersionCompatibility.SameProcess; myAsmName.HashAlgorithm = AssemblyHashAlgorithm.SHA1; myAsmName.Version = new Version("1.0.0.0"); AssemblyBuilder myAsmBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(myAsmName, AssemblyBuilderAccess.Save, System.IO.Path.GetDirectoryName(dllPath)); ModuleBuilder myModBuilder = myAsmBuilder.DefineDynamicModule("MyModule", System.IO.Path.GetFileName(dllPath)); TypeBuilder myTypeBuilder = myModBuilder.DefineType("MyType", TypeAttributes.Public, typeof(MenuExtension)); var menuFormatWriter = new JsonFx.Json.JsonWriter(); var menuFormatJson = menuFormatWriter.Write(menuFormat); AddAttribute(myTypeBuilder, typeof(ComVisibleAttribute), true); AddAttribute(myTypeBuilder, typeof(GuidAttribute), guid.ToString()); AddAttribute(myAsmBuilder, typeof(GuidAttribute), guid.ToString()); AddAttribute(myAsmBuilder, typeof(InfoStorageAttribute), name, actionPath, menuFormatJson, association, fileExtensionFilter, expandFileNames); // embed all images found in the menu into the assembly foreach (string keyname in resourceList.Keys) { string filename = (string)resourceList[keyname]; System.Drawing.Image image = System.Drawing.Image.FromFile(filename); System.IO.MemoryStream memStream = new System.IO.MemoryStream(); image.Save(memStream, System.Drawing.Imaging.ImageFormat.Bmp); byte[] rawdata = memStream.ToArray(); System.Resources.IResourceWriter rw = myModBuilder.DefineResource(keyname + ".resources", "description", ResourceAttributes.Public); rw.AddResource("image.bmp", rawdata); } myTypeBuilder.CreateType(); myModBuilder.CreateGlobalFunctions(); myAsmBuilder.Save(System.IO.Path.GetFileName(dllPath)); }
private static void CreateResourceFile(string inputFile, string outputFile) { string fileName = Path.GetFileName(inputFile); System.Resources.IResourceWriter writer = null; FileStream fs = null; BinaryReader br = null; try { //writer = new System.Resources.ResourceWriter(outputFile); writer = new System.Resources.ResXResourceWriter(outputFile); fs = new FileStream(inputFile, FileMode.Open); br = new BinaryReader(fs); byte[] myBuffer = new byte[br.BaseStream.Length]; for (long i = 0; i < br.BaseStream.Length; i++) { myBuffer[i] = br.ReadByte(); } writer.AddResource(fileName, myBuffer); writer.Generate(); //writer.Close(); } finally { if (writer != null) { writer.Close(); } if (fs != null) { fs.Close(); } if (br != null) { br.Close(); } } }
private AssemblyBuilder CreateExecutable(string name) { ILGenerator il; name = name.Replace('\\', '/'); string exename = name.Contains('/') ? name.Substring(name.LastIndexOf('/') + 1) : name; string pathname = name.Contains('/') ? name.Remove(name.LastIndexOf('/')) : null; string asmname = exename.Contains('.') ? exename.Remove(exename.LastIndexOf('.')) : exename; // // We are generating a dynamic assembly here. // // Get type information from the runtime assembly var asm_runtime = Assembly.Load("GameCreator.Runtime"); var asm_gamei = Assembly.Load("GameCreator.Runtime.Game.Interpreted"); var asm_game = Assembly.Load("GameCreator.Runtime.Game"); Type t_gamei = asm_gamei.GetType("GameCreator.Runtime.Game.Interpreted.InterpretedGame"); Type t_game = asm_game.GetType("GameCreator.Runtime.Game.Game"); Type t_ext = typeof(ResourceExtensions); Type t_obj = typeof(Framework.Object); Type t_room = typeof(Room); Type t_event = typeof(Event); Type t_lib = typeof(Framework.ActionLibrary); Type t_context = typeof(LibraryContext); Type t_rcontext = typeof(ResourceContext); // Define our dynamic assembly System.Reflection.Emit.AssemblyBuilder asm = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName(asmname), AssemblyBuilderAccess.RunAndSave, pathname); ModuleBuilder mod = asm.DefineDynamicModule(exename, exename); // Define our Program type TypeBuilder program = mod.DefineType(string.Format("{0}.Program", asmname)); System.Resources.IResourceWriter resw = mod.DefineResource(string.Format("{0}.Program.resources", asmname), string.Empty); FieldBuilder resourceManager = program.DefineField("resourceManager", typeof(System.Resources.ResourceManager), FieldAttributes.Static); MethodBuilder GetResourceManager = program.DefineMethod("GetResourceManager", MethodAttributes.Static, typeof(System.Resources.ResourceManager), Type.EmptyTypes); il = GetResourceManager.GetILGenerator(); System.Reflection.Emit.Label grm_l_1 = il.DefineLabel(); il.Emit(OpCodes.Ldsfld, resourceManager); il.Emit(OpCodes.Dup); il.Emit(OpCodes.Brtrue_S, grm_l_1); il.Emit(OpCodes.Pop); il.Emit(OpCodes.Ldstr, string.Format("{0}.Program", asmname)); il.Emit(OpCodes.Ldtoken, program); il.Emit(OpCodes.Call, typeof(Type).GetMethod("GetTypeFromHandle", BindingFlags.Static | BindingFlags.Public)); il.Emit(OpCodes.Callvirt, typeof(Type).GetProperty("Assembly").GetGetMethod()); il.Emit(OpCodes.Newobj, typeof(System.Resources.ResourceManager).GetConstructor(new Type[] { typeof(string), typeof(Assembly) })); il.Emit(OpCodes.Dup); il.Emit(OpCodes.Stsfld, resourceManager); il.MarkLabel(grm_l_1); il.Emit(OpCodes.Ret); //program.SetCustomAttribute(new CustomAttributeBuilder(typeof(STAThreadAttribute).GetConstructor(new Type[] { }), new object[] { })); // our static Main() function // { // GameCreator.Runtime.Object obj; // GameCreator.Runtime.Event ev; // GameCreator.Runtime.ActionLibrary lib; // GameCreator.Runtime.Game.Init(); // ... // lib = GameCreator.ActionLibrary.Define(id); // lib.DefineAction(actionid, kind, execution, question, func, code, args); // ... // GameCreator.Runtime.Script.Define("name", index, "code"); // ... // obj = GameCreator.Runtime.Object.Define("name", index); // ev = obj.DefineEvent(event, num); // ev.DefineAction(libid, actionid, args, appliesto, relative, not); // ... // GameCreator.Runtime.Room.Define("name", index).CreationCode = "code"; // ... // GameCreator.Runtime.Game.Name = "name"; // GameCreator.Runtime.Game.Run(); // (return) // } // GameCreator.Runtime.Object obj; // GameCreator.Runtime.Event ev; // GameCreator.Runtime.ActionLibrary lib; MethodBuilder initLibraries = program.DefineMethod("InitLibraries", MethodAttributes.Static); il = initLibraries.GetILGenerator(); foreach (ActionLibrary lib in Program.Library) { // lib = GameCreator.ActionLibrary.Define(id); il.EmitCall(OpCodes.Call, t_context.GetProperty("Current", BindingFlags.Public | BindingFlags.Static).GetGetMethod(), null); il.Emit(OpCodes.Ldc_I4, lib.LibraryID); il.EmitCall(OpCodes.Call, t_context.GetMethod("GetActionLibrary", BindingFlags.Public | BindingFlags.Instance, null, new Type[] { typeof(int) }, null), null); foreach (ActionDefinition ad in lib.Actions) { // lib.DefineAction(actionid, kind, execution, question, func, code, args); il.Emit(OpCodes.Dup); il.Emit(OpCodes.Ldc_I4, ad.ActionID); il.Emit(OpCodes.Ldc_I4, (int)ad.Kind); il.Emit(OpCodes.Ldc_I4, (int)ad.ExecutionType); il.Emit(ad.IsQuestion ? OpCodes.Ldc_I4_1 : OpCodes.Ldc_I4_0); il.Emit(OpCodes.Ldstr, ad.FunctionName); il.Emit(OpCodes.Ldstr, ad.Code); // ... , new ActionArgumentType[] { x, y, z }, il.Emit(OpCodes.Ldc_I4, ad.ArgumentCount); il.Emit(OpCodes.Newarr, typeof(ActionArgumentType)); for (int i = 0; i < ad.ArgumentCount; i++) { il.Emit(OpCodes.Dup); il.Emit(OpCodes.Ldc_I4, i); il.Emit(OpCodes.Ldc_I4, (int)ad.Arguments[i].Type); il.Emit(OpCodes.Stelem_I4); } // il.EmitCall(OpCodes.Call, t_lib.GetMethod("DefineAction", BindingFlags.Public | BindingFlags.Instance, null, new Type[] { typeof(int), typeof(ActionKind), typeof(ActionExecutionType), typeof(bool), typeof(string), typeof(string), typeof(ActionArgumentType[]) }, null), null); } il.Emit(OpCodes.Pop); } il.Emit(OpCodes.Ret); MethodBuilder defineSprites = program.DefineMethod("DefineSprites", MethodAttributes.Static); il = defineSprites.GetILGenerator(); foreach (SpriteResourceView res in Program.Sprites.Values) { // Define the resource int i = 0; foreach (Bitmap b in res.Animation.Frames) { resw.AddResource(string.Format("spr_{0}_{1}", res.ResourceID, i++), b); } // GameCreator.Runtime.Sprite.Define("name", index, subimages); il.EmitCall(OpCodes.Call, t_context.GetProperty("Current", BindingFlags.Public | BindingFlags.Static).GetGetMethod(), null); il.EmitCall(OpCodes.Call, t_context.GetProperty("Resources", BindingFlags.Public | BindingFlags.Instance).GetGetMethod(), null); il.EmitCall(OpCodes.Call, t_rcontext.GetProperty("Sprites", BindingFlags.Public | BindingFlags.Instance).GetGetMethod(), null); il.Emit(OpCodes.Ldstr, res.Name); il.Emit(OpCodes.Ldc_I4, res.ResourceID); il.Emit(OpCodes.Ldc_I4, res.Animation.FrameCount); il.EmitCall(OpCodes.Call, t_ext.GetMethod("Define", BindingFlags.Static | BindingFlags.Public, null, new Type[] { typeof(IndexedResourceManager <Sprite>), typeof(string), typeof(int), typeof(int) }, null), null); il.Emit(OpCodes.Pop); } il.Emit(OpCodes.Ret); MethodBuilder defineScripts = program.DefineMethod("DefineScripts", MethodAttributes.Static); il = defineScripts.GetILGenerator(); foreach (ScriptResourceView res in Program.Scripts.Values) { // GameCreator.Runtime.Script.Define("name", index, "code"); il.EmitCall(OpCodes.Call, t_context.GetProperty("Current", BindingFlags.Public | BindingFlags.Static).GetGetMethod(), null); il.EmitCall(OpCodes.Call, t_context.GetProperty("Resources", BindingFlags.Public | BindingFlags.Instance).GetGetMethod(), null); il.EmitCall(OpCodes.Call, t_rcontext.GetProperty("Sprites", BindingFlags.Public | BindingFlags.Instance).GetGetMethod(), null); il.Emit(OpCodes.Ldstr, res.Name); il.Emit(OpCodes.Ldc_I4, res.ResourceID); il.Emit(OpCodes.Ldstr, res.Code); il.EmitCall(OpCodes.Call, t_ext.GetMethod("Define", BindingFlags.Static | BindingFlags.Public, null, new Type[] { typeof(IndexedResourceManager <Script>), typeof(string), typeof(int), typeof(string) }, null), null); il.Emit(OpCodes.Pop); } il.Emit(OpCodes.Ret); MethodBuilder defineObjects = program.DefineMethod("DefineObjects", MethodAttributes.Static); il = defineObjects.GetILGenerator(); foreach (ObjectResourceView res in Program.Objects.Values) { // obj = GameCreator.Runtime.Object.Define("name", index); il.EmitCall(OpCodes.Call, t_context.GetProperty("Current", BindingFlags.Public | BindingFlags.Static).GetGetMethod(), null); il.EmitCall(OpCodes.Call, t_context.GetProperty("Resources", BindingFlags.Public | BindingFlags.Instance).GetGetMethod(), null); il.EmitCall(OpCodes.Call, t_rcontext.GetProperty("Objects", BindingFlags.Public | BindingFlags.Instance).GetGetMethod(), null); il.Emit(OpCodes.Ldstr, res.Name); il.Emit(OpCodes.Ldc_I4, res.ResourceID); il.EmitCall(OpCodes.Call, t_ext.GetMethod("Define", BindingFlags.Static | BindingFlags.Public, null, new Type[] { typeof(IndexedResourceManager <Framework.Object>), typeof(string), typeof(int) }, null), null); foreach (ObjectEvent ev in res.Events) { // ev = obj.DefineEvent(event, num); il.Emit(OpCodes.Dup); il.Emit(OpCodes.Ldc_I4, (int)ev.EventType); il.Emit(OpCodes.Ldc_I4, (int)ev.EventNumber); il.EmitCall(OpCodes.Call, t_obj.GetMethod("DefineEvent", BindingFlags.Public | BindingFlags.Instance, null, new Type[] { typeof(int), typeof(int) }, null), null); foreach (ActionDeclaration ad in ev.Actions) { // ev.DefineAction(libid, actionid, args, appliesto, relative, not); il.Emit(OpCodes.Dup); il.Emit(OpCodes.Ldc_I4, ad.Kind.Library.LibraryID); il.Emit(OpCodes.Ldc_I4, ad.Kind.ActionID); // ... , new string[] { "arg", "arg", "arg" }, il.Emit(OpCodes.Ldc_I4, ad.Kind.ArgumentCount); il.Emit(OpCodes.Newarr, typeof(string)); for (int i = 0; i < ad.Kind.ArgumentCount; i++) { il.Emit(OpCodes.Dup); il.Emit(OpCodes.Ldc_I4, i); il.Emit(OpCodes.Ldstr, ad.Arguments[i]); il.Emit(OpCodes.Stelem_Ref); } // il.Emit(OpCodes.Ldc_I4, ad.AppliesTo); il.Emit(ad.Relative ? OpCodes.Ldc_I4_1 : OpCodes.Ldc_I4_0); il.Emit(ad.Not ? OpCodes.Ldc_I4_1 : OpCodes.Ldc_I4_0); il.EmitCall(OpCodes.Call, t_event.GetMethod("DefineAction", BindingFlags.Public | BindingFlags.Instance, null, new Type[] { typeof(int), typeof(int), typeof(string[]), typeof(int), typeof(bool), typeof(bool) }, null), null); } il.Emit(OpCodes.Pop); } il.Emit(OpCodes.Dup); // obj.SpriteIndex = ind; il.Emit(OpCodes.Ldc_I4, res.Sprite); il.EmitCall(OpCodes.Call, t_obj.GetProperty("SpriteIndex").GetSetMethod(), null); //obj.Depth = d; il.Emit(OpCodes.Ldc_R8, res.Depth); il.EmitCall(OpCodes.Call, t_obj.GetProperty("Depth").GetSetMethod(), null); } il.Emit(OpCodes.Ret); MethodBuilder defineRooms = program.DefineMethod("DefineRooms", MethodAttributes.Static); il = defineRooms.GetILGenerator(); // Start defining rooms. // We perform a 'preorder iterative traversal' of the 'Rooms' TreeNode to ensure the rooms are defined in the correct order. // We can probably eliminate the stack by using the 'Parent' property of the nodes instead. Stack <TreeNode> nodes = new Stack <TreeNode>(); nodes.Push(Rooms.Node); while (nodes.Count != 0) { TreeNode tn = nodes.Pop(); if (tn.Tag.GetType() == typeof(RoomResourceView)) { RoomResourceView res = (RoomResourceView)(tn.Tag); // GameCreator.Runtime.Room.Define("name", index).CreationCode = "code"; il.EmitCall(OpCodes.Call, t_context.GetProperty("Current", BindingFlags.Public | BindingFlags.Static).GetGetMethod(), null); il.EmitCall(OpCodes.Call, t_context.GetProperty("Resources", BindingFlags.Public | BindingFlags.Instance).GetGetMethod(), null); il.EmitCall(OpCodes.Call, t_rcontext.GetProperty("Rooms", BindingFlags.Public | BindingFlags.Instance).GetGetMethod(), null); il.Emit(OpCodes.Ldstr, res.Name); il.Emit(OpCodes.Ldc_I4, res.ResourceID); il.EmitCall(OpCodes.Call, t_ext.GetMethod("Define", BindingFlags.Static | BindingFlags.Public, null, new Type[] { typeof(IndexedResourceManager <Room>), typeof(string), typeof(int) }, null), null); il.Emit(OpCodes.Ldstr, res.CreationCode); il.EmitCall(OpCodes.Call, t_room.GetProperty("CreationCode").GetSetMethod(), null); } tn = tn.LastNode; while (tn != null) { nodes.Push(tn); tn = tn.PrevNode; } } il.Emit(OpCodes.Ret); MethodBuilder main = program.DefineMethod("Main", MethodAttributes.Static); main.SetCustomAttribute(new CustomAttributeBuilder(typeof(STAThreadAttribute).GetConstructor(System.Type.EmptyTypes), new object[] { })); il = main.GetILGenerator(); // GameCreator.Runtime.Game.Interpreted.InterpretedGame.Initialize(); il.EmitCall(OpCodes.Call, t_gamei.GetMethod("Initialize"), null); // InitLibraries(); il.EmitCall(OpCodes.Call, initLibraries, null); // DefineScripts(); il.EmitCall(OpCodes.Call, defineScripts, null); // DefineSprites(); il.EmitCall(OpCodes.Call, defineSprites, null); // DefineObjects(); il.EmitCall(OpCodes.Call, defineObjects, null); // DefineRooms(); il.EmitCall(OpCodes.Call, defineRooms, null); // GameCreator.Runtime.Game.Name = "name"; //il.Emit(OpCodes.Ldstr, asmname); //il.EmitCall(OpCodes.Call, t_game.GetProperty("Name").GetSetMethod(), null); // GameCreator.Runtime.Game.ResourceManager = GetResourceManager(); il.EmitCall(OpCodes.Call, GetResourceManager, null); il.EmitCall(OpCodes.Call, t_game.GetProperty("ResourceManager").GetSetMethod(), null); // GameCreator.Runtime.Game.Interpreted.InterpretedGame.Run(); il.EmitCall(OpCodes.Call, t_gamei.GetMethod("Run"), null); // return statement, required il.Emit(OpCodes.Ret); program.CreateType(); asm.SetEntryPoint(main, PEFileKinds.WindowApplication); // Use 32 bit (i386) since GTK# requires it and we plan on using it later asm.Save(exename, PortableExecutableKinds.ILOnly, ImageFileMachine.I386); return(asm); }