Beispiel #1
0
        public void Import(IFile file, IMod mod)
        {
            if (file.GetType() != typeof(LdstrFile))
            {
                return;
            }

            var module = HookEndpointManager.GenerateCecilModule(mod.Code.GetName());

            var entryDict = (file as LdstrFile).LdstrEntries;

            foreach (var entryPair in entryDict)
            {
                var md = module.FindMethod(entryPair.Key);
                if (md == null)
                {
                    continue;
                }

                var method = MethodBase.GetMethodFromHandle(md.ResolveReflection().MethodHandle);

                var e = entryPair.Value;

                if (!HaveTranslation(e))
                {
                    continue;
                }

                var modification = new ILContext.Manipulator(il =>
                {
                    foreach (var instruction in il.Instrs)
                    {
                        var ins = e.Instructions.FirstOrDefault(i => instruction.MatchLdstr(i.Origin));
                        if (ins == null || string.IsNullOrEmpty(ins.Translation))
                        {
                            continue;
                        }

                        instruction.Operand = ins.Translation;

                        foreach (var label in il.Labels)
                        {
                            if (label.Target.MatchLdstr(ins.Origin))
                            {
                                label.Target = instruction;
                            }
                        }
                    }
                });

                if (!modifications.ContainsKey(method))
                {
                    HookEndpointManager.Modify(method, modification);
                    modifications.Add(method, modification);
                }
            }
        }
        public void Export(IPackage package, IExportConfig config)
        {
            if (package?.Mod == null)
            {
                return;
            }

            var asm = package.Mod.Code;

            var file = new LdstrFile();

            file.LdstrEntries = new Dictionary <string, LdstrEntry>();

            foreach (var type in HookEndpointManager.GenerateCecilModule(asm.GetName()).GetTypes())
            {
                /* The return value of GetTypes() and other methods will include types they derived from.
                 * So we should check the namespace to ensure it belongs to the assembly, but there still are
                 * some issues. */
                if (type.Namespace == null || !type.Namespace.StartsWith(package.Mod.Name))
                {
                    continue;
                }

                foreach (var method in type.Methods)
                {
                    if (method.DeclaringType?.Namespace == null || !method.DeclaringType.Namespace.StartsWith(
                            package.Mod.Name) ||
                        method.IsAbstract)
                    {
                        continue;
                    }

                    try
                    {
                        var entry = GetEntryFromMethod(method);
                        if (entry != null && !file.LdstrEntries.ContainsKey(method.GetFindableID()))
                        {
                            file.LdstrEntries.Add(method.GetFindableID(), entry);
                        }
                    }
                    catch (Exception e)
                    {
                        Localizer.Log.Error(e.ToString());
                    }
                }
            }

            package.AddFile(file);
        }