Example #1
0
        public LdstrEntry Merge(LdstrEntry main, LdstrEntry addition)
        {
            var result = new LdstrEntry {
                Instructions = new List <BaseEntry>()
            };

            var mainE = main;
            var addE  = addition;

            foreach (var ins in mainE.Instructions)
            {
                if (!result.Instructions.Exists(i => i.Origin == ins.Origin))
                {
                    result.Instructions.Add(ins.Clone() as BaseEntry);
                }
            }

            foreach (var ins in addE.Instructions)
            {
                if (!result.Instructions.Exists(i => i.Origin == ins.Origin))
                {
                    result.Instructions.Add(ins.Clone() as BaseEntry);
                }
            }

            return(result);
        }
Example #2
0
        internal bool HaveTranslation(LdstrEntry entry)
        {
            foreach (var ins in entry.Instructions)
            {
                if (!string.IsNullOrEmpty(ins.Translation))
                {
                    return(true);
                }
            }

            return(false);
        }
        public void MergeEntry_Correct()
        {
            var service = new HarmonyLdstrFileImport();

            var main = new LdstrEntry()
            {
                Instructions = new List <BaseEntry>()
                {
                    new BaseEntry()
                    {
                        Origin      = "Origin1",
                        Translation = "Translation1"
                    },
                    new BaseEntry()
                    {
                        Origin      = "Origin2",
                        Translation = "Translation2"
                    },
                }
            };

            var addition = new LdstrEntry()
            {
                Instructions = new List <BaseEntry>()
                {
                    new BaseEntry()
                    {
                        Origin      = "Origin1",
                        Translation = "AnotherTranslation1"
                    },
                    new BaseEntry()
                    {
                        Origin      = "AnotherOrigin2",
                        Translation = "Translation2"
                    },
                    new BaseEntry()
                    {
                        Origin      = "Origin3",
                        Translation = "Translation3"
                    },
                }
            };

            var result = service.Merge(main, addition);

            result.Instructions.Count.Should().Be(4);

            result.Instructions.Should().ContainSingle(i => i.Origin == "Origin1" && i.Translation == "Translation1");
            result.Instructions.Should().ContainSingle(i => i.Origin == "Origin2" && i.Translation == "Translation2");
            result.Instructions.Should().ContainSingle(i => i.Origin == "AnotherOrigin2" && i.Translation == "Translation2");
            result.Instructions.Should().ContainSingle(i => i.Origin == "Origin3" && i.Translation == "Translation3");
        }
Example #4
0
        private LdstrEntry GetEntryFromMethod(MethodBase method)
        {
            var instructions = GetInstructions(method);

            if (instructions == null)
            {
                return(null);
            }

            var entry = new LdstrEntry {
                Instructions = new List <BaseEntry>()
            };

            for (var i = 0; i < instructions.Count; i++)
            {
                var ins = instructions[i];
                if (ins.opcode == OpCodes.Ldstr && !string.IsNullOrWhiteSpace(ins.operand.ToString()))
                {
                    // Filter methods in blacklist1
                    if (i < instructions.Count - 1)
                    {
                        var next = instructions[i + 1];
                        if (next.opcode == OpCodes.Call || next.opcode == OpCodes.Calli ||
                            next.opcode == OpCodes.Callvirt)
                        {
                            if (_blackList1.Any(m => (next.operand as MethodBase).GetID() == m.GetID()))
                            {
                                continue;
                            }
                        }
                    }

                    // Filter methods in blacklist2
                    if (i < instructions.Count - 2)
                    {
                        var afterNext = instructions[i + 2];
                        if (afterNext.opcode == OpCodes.Call || afterNext.opcode == OpCodes.Calli ||
                            afterNext.opcode == OpCodes.Callvirt)
                        {
                            if (_blackList2.Any(m => (afterNext.operand as MethodBase).GetID() == m.GetID()))
                            {
                                continue;
                            }
                        }
                    }

                    // No need to add a same string
                    if (entry.Instructions.Exists(e => e.Origin == ins.operand.ToString()))
                    {
                        continue;
                    }

                    entry.Instructions.Add(new BaseEntry
                    {
                        Origin      = ins.operand.ToString(),
                        Translation = ""
                    });
                }
            }

            return(entry.Instructions.Count == 0 ? null : entry);
        }
Example #5
0
        private LdstrEntry GetEntryFromMethod(MethodDefinition method)
        {
            var instructions = method?.Body?.Instructions;

            if (instructions == null)
            {
                return(null);
            }

            var entry = new LdstrEntry {
                Instructions = new List <BaseEntry>()
            };

            for (var i = 0; i < instructions.Count; i++)
            {
                var ins = instructions[i];
                if (ins.OpCode == OpCodes.Ldstr && !string.IsNullOrWhiteSpace(ins.Operand.ToString()))
                {
                    // Filter methods in blacklist1
                    if (i < instructions.Count - 1)
                    {
                        var next      = instructions[i + 1];
                        var operandId = "";
                        if (next.OpCode == OpCodes.Call || next.OpCode == OpCodes.Callvirt)
                        {
                            operandId = (next.Operand as MethodReference).GetID();
                        }
                        else if (next.OpCode == OpCodes.Calli)
                        {
                            operandId = (next.Operand as CallSite).GetID();
                        }

                        if (!string.IsNullOrWhiteSpace(operandId) && _blackList1.Any(m => operandId == m?.GetID()))
                        {
                            continue;
                        }
                    }

                    // Filter methods in blacklist2
                    if (i < instructions.Count - 2)
                    {
                        var afterNext = instructions[i + 2];
                        var operandId = "";
                        if (afterNext.OpCode == OpCodes.Call || afterNext.OpCode == OpCodes.Callvirt)
                        {
                            operandId = (afterNext.Operand as MethodReference).GetID();
                        }
                        else if (afterNext.OpCode == OpCodes.Calli)
                        {
                            operandId = (afterNext.Operand as CallSite).GetID();
                        }

                        if (!string.IsNullOrWhiteSpace(operandId) && _blackList2.Any(m => operandId == m?.GetID()))
                        {
                            continue;
                        }
                    }

                    // No need to add a same string
                    if (entry.Instructions.Exists(e => e.Origin == ins.Operand.ToString()))
                    {
                        continue;
                    }

                    entry.Instructions.Add(new BaseEntry
                    {
                        Origin      = ins.Operand.ToString(),
                        Translation = ""
                    });
                }
            }

            return(entry.Instructions.Count == 0 ? null : entry);
        }