Example #1
0
        public void Export(IPackage package, IExportConfig config)
        {
            if (package?.Mod == null)
            {
                return;
            }

            var translations = Utils.GetModByName(package.ModName).Field("translations")
                               as Dictionary <string, ModTranslation>;

            if (translations == null)
            {
                return;
            }

            var file = new CustomModTranslationFile
            {
                Translations = new Dictionary <string, BaseEntry>()
            };

            foreach (var translation in translations)
            {
                file.Translations.Add(translation.Key, new BaseEntry
                {
                    Origin      = translation.Value.DefaultOrEmpty(),
                    Translation = config.WithTranslation ? translation.Value?.GetTranslation(package.Language) : ""
                });
            }

            package.AddFile(file);
        }
        public void Update(IPackage oldPackage, IPackage newPackage, IUpdateLogger logger)
        {
            Utils.LogDebug($"Updating package [{oldPackage.Name}]");

            foreach (var oldFile in oldPackage.Files)
            {
                var updater = GetUpdater(oldFile);
                if (updater is null)
                {
                    continue;
                }

                Utils.SafeWrap(() =>
                {
                    updater.Update(oldFile, newPackage.Files.FirstOrDefault(f => f.GetType() == oldFile.GetType()),
                                   logger);
                });
            }

            foreach (var file in newPackage.Files)
            {
                if (oldPackage.Files.All(f => f.GetType() != file.GetType()))
                {
                    oldPackage.AddFile(file);
                }
            }

            Utils.LogDebug($"Package [{oldPackage.Name}] updated.");
        }
Example #3
0
        public void Export(IPackage package, IExportConfig config)
        {
            var modName = package.ModName;
            var mod     = Utils.GetModByName(modName);

            if (mod == null)
            {
                return;
            }

            var file = Activator.CreateInstance(typeof(T)) as IFile;

            foreach (var prop in typeof(T).ModTranslationOwnerField())
            {
                var fieldName = prop.ModTranslationOwnerFieldName();

                var field = (IDictionary)mod.Field(fieldName);

                var entryType = prop.PropertyType.GenericTypeArguments
                                .FirstOrDefault(g => g.GetInterfaces().Contains(typeof(IEntry)));

                var entries = CreateEntries(field, entryType, package.Language, config.WithTranslation);

                var entriesOfFile = (IDictionary)prop.GetValue(file);
                foreach (var e in entries)
                {
                    entriesOfFile.Add(e.Key, e.Value);
                }
            }

            package.AddFile(file);
        }
Example #4
0
        public IPackage Load(string path, IFileLoadService fileLoadService)
        {
            Utils.LogDebug($"Loading package from {path}");

            if (!Directory.Exists(path))
            {
                Utils.LogError("Package directory doesn't exist!");
                return(null);
            }

            var packDir         = new DirectoryInfo(path);
            var packageFilePath = Path.Combine(packDir.FullName, _packageMainFileName);

            if (!File.Exists(packageFilePath))
            {
                Utils.LogError("Package main file doesn't exist!");
                return(null);
            }

            IPackage package = Utils.ReadFileAndDeserializeJson <T>(packageFilePath);

            if (package == null)
            {
                Utils.LogError("Package main file deserialization failed!");
                return(null);
            }

            var mod = Localizer.GetWrappedMod(package.ModName);

            if (mod == null)
            {
                Utils.LogError($"Package Mod {package.ModName} not loaded!");
                return(null);
            }

            package.Mod = mod;

            foreach (var fileTypeName in package.FileList)
            {
                Utils.LogDebug($"Loading file [{fileTypeName}]");

                var filePath = Path.Combine(packDir.FullName, fileTypeName + ".json");
                using (var fs = new FileStream(filePath, FileMode.Open))
                {
                    var file = fileLoadService.Load(fs, fileTypeName);

                    package.AddFile(file);
                }
            }

            Utils.LogDebug($"Package [{package.Name} loaded.]");

            return(package);
        }
Example #5
0
        public void Export(IPackage package, IExportConfig config)
        {
            if (package?.Mod == null)
            {
                return;
            }

            var modFile      = package.Mod.File;
            var asmManager   = "Terraria.ModLoader.Core.AssemblyManager".Type();
            var assemblyName = (string)asmManager.Invoke("GetModAssemblyFileName", modFile, true);
            var loadedMod    = asmManager.ValueOf("loadedMods").Invoke("get_Item", package.Mod.Name);
            var reref        = (byte[])asmManager.GetNestedType("LoadedMod", NoroHelper.Any).Method("EncapsulateReferences")
                               .Invoke(loadedMod, new object[] { modFile.GetBytes(assemblyName), null });
            var asm = AssemblyDefinition.ReadAssembly(new MemoryStream(reref));

            var file = new LdstrFile
            {
                LdstrEntries = new Dictionary <string, LdstrEntry>()
            };

            foreach (var type in asm.MainModule.GetTypes())
            {
                if (type.Namespace == null)
                {
                    continue;
                }

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

                    try
                    {
                        LogDebug($"Exporting method: [{method.GetID()}]");
                        var entry = GetEntryFromMethod(method);
                        if (entry != null && !file.LdstrEntries.ContainsKey(method.GetID()))
                        {
                            file.LdstrEntries.Add(method.GetID(), entry);
                        }
                    }
                    catch (Exception e)
                    {
                        Localizer.Log.Error(e.ToString());
                    }
                }
            }

            package.AddFile(file);
        }
Example #6
0
        public void Export(IPackage package, IExportConfig config)
        {
            if (package?.Mod == null)
            {
                return;
            }

            var asm = package.Mod.Code;

            var file = new LdstrFile
            {
                LdstrEntries = new Dictionary <string, LdstrEntry>()
            };

            foreach (var type in asm.ManifestModule.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.GetMethods(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static))
                {
                    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.GetID()))
                        {
                            file.LdstrEntries.Add(method.GetID(), entry);
                        }
                    }
                    catch (Exception e)
                    {
                        Localizer.Log.Error(e.ToString());
                    }
                }
            }

            package.AddFile(file);
        }
Example #7
0
        public void Export(IPackage package, IExportConfig config)
        {
            if (package?.Mod == null)
            {
                return;
            }

            var asm = package.Mod.Code;

            var file = new LdstrFile
            {
                LdstrEntries = new Dictionary <string, LdstrEntry>()
            };

            foreach (var type in asm.ManifestModule.GetTypes())
            {
                if (type.Namespace == null)
                {
                    continue;
                }

                var methodBases = new List <MethodBase>();
                methodBases.AddRange(type.GetMethods(All | BindingFlags.DeclaredOnly));
                methodBases.AddRange(type.GetConstructors(All | BindingFlags.DeclaredOnly));
                foreach (var method in methodBases)
                {
                    if (method.DeclaringType?.Namespace == null || method.IsAbstract)
                    {
                        continue;
                    }

                    try
                    {
                        Utils.LogDebug($"Exporting method: [{method.GetID()}]");
                        var entry = GetEntryFromMethod(method);
                        if (entry != null && !file.LdstrEntries.ContainsKey(method.GetID()))
                        {
                            file.LdstrEntries.Add(method.GetID(), entry);
                        }
                    }
                    catch (Exception e)
                    {
                        Localizer.Log.Error(e.ToString());
                    }
                }
            }

            package.AddFile(file);
        }
Example #8
0
        public IPackage Load(string path, IFileLoadService fileLoadService)
        {
            Utils.LogDebug($"Loading package from {path}");

            if (!System.IO.File.Exists(path))
            {
                Utils.LogError("Package file doesn't exist!");
                return(null);
            }

            using (var zipFileToOpen = new FileStream(path, FileMode.Open))
            {
                using (var archive = new ZipArchive(zipFileToOpen, ZipArchiveMode.Read))
                {
                    var packageStream = archive.GetEntry(_packageMainFileName)?.Open();

                    IPackage package = Utils.ReadFileAndDeserializeJson <T>(packageStream);
                    if (package == null)
                    {
                        Utils.LogError("Package main file deserialization failed!");
                        return(null);
                    }

                    var mod = Localizer.GetWrappedMod(package.ModName);
                    if (mod == null)
                    {
                        Utils.LogError($"Package Mod {package.ModName} not loaded!");
                        return(null);
                    }

                    package.Mod = mod;

                    foreach (var fileTypeName in package.FileList)
                    {
                        Utils.LogDebug($"Loading file [{fileTypeName}]");

                        var fs   = archive.GetEntry($"{fileTypeName}.json")?.Open();
                        var file = fileLoadService.Load(fs, fileTypeName);

                        package.AddFile(file);
                    }

                    Utils.LogDebug($"Package [{package.Name} loaded.]");

                    return(package);
                }
            }
        }
Example #9
0
        public void Update(IPackage oldPackage, IPackage newPackage, IUpdateLogService logger)
        {
            Utils.LogDebug($"Updating package [{oldPackage.Name}]");

            foreach (var oldFile in oldPackage.Files)
            {
                dynamic temp = oldFile;
                var     s    = GetUpdateService(temp);
                s.Update(oldFile, newPackage.Files.FirstOrDefault(f => f.GetType() == oldFile.GetType()),
                         logger);
            }

            foreach (var file in newPackage.Files)
            {
                if (oldPackage.Files.All(f => f.GetType() != file.GetType()))
                {
                    oldPackage.AddFile(file);
                }
            }

            Utils.LogDebug($"Package [{oldPackage.Name}] updated.");
        }