示例#1
0
        /// <summary>
        ///     Trys to start the given modules.
        ///     Print's hint if a module was created the wrong way
        /// </summary>
        private void StartModules()
        {
            bool possibleAfterEffects = false;

            // Process each module
            foreach (Assembly moduleAssembly in _moduleAssemblies)
            {
                try
                {
                    // Search for "ModulePropert" interface class in assembly
                    // and, if given, start the module4
                    foreach (Type moduleClass in moduleAssembly.GetTypes())
                    {
                        foreach (Type moduleInterface in moduleClass.GetInterfaces())
                        {
                            if (Attribute.IsDefined(moduleInterface, typeof(ModuleProperties)))
                            {
                                // Load module properties from interface
                                ModuleProperties moduleProperties = (ModuleProperties)
                                                                    Attribute.GetCustomAttribute(moduleInterface, typeof(ModuleProperties));

                                // Moduletype is not given as startup parameter -> next module;
                                if (!ModuleTypeHandler.IsModuleTypeValid
                                        (moduleProperties.ModuleType))
                                {
                                    continue;
                                }
                                try
                                {
                                    ConsoleOutput.AppendPrefix("\t");
                                    // Start module
                                    _kernel.Get(moduleClass);
                                }
                                finally
                                {
                                    ConsoleOutput.ResetPrefix();
                                }
                            }
                        }
                    }
                }
                catch (ActivationException e)
                {
                    ConsoleOutput.WriteLine(ConsoleType.Error,
                                            $"The module ~o~{moduleAssembly.FullName}~;~ could not start, because one or more dependecies are missing!.");

                    if (possibleAfterEffects)
                    {
                        ConsoleOutput.WriteLine(ConsoleType.Note,
                                                "This could be a follow-up error, " +
                                                "because other modules have not been started.");
                    }

                    ConsoleOutput.WriteLine(ConsoleType.Error, $"{e.Message}");
                    possibleAfterEffects = true;
//#if DEBUG
//                    throw;
//#endif
                }
            }
示例#2
0
        /// <summary>
        ///     Refreshing the server resource modules.
        ///     Hint: Runs only if "DEBUG" constant is given
        /// </summary>
        public void CopyModulesToServer()
        {
#if !DEBUG
            if (ParameterHandler.IsDefault("onlyCopy"))
            {
                ConsoleOutput.WriteLine(ConsoleType.Core, "Release state. Copying modules skipped.");
                return;
            }
#endif
            ConsoleOutput.WriteLine(ConsoleType.Core,
                                    $"Refreshing server resource modules...");
            // Define constants for the folder top copy from and to copy to
            const string gtMpServerModulesFolder = @"./resources/EvoMp/dist";
            const string projectSolutionCompiledModulesFolder = @"./../EvoMp/";


            // Create the Modules folder in the resource if it doesnt exist
            if (!Directory.Exists(gtMpServerModulesFolder))
            {
                Directory.CreateDirectory(gtMpServerModulesFolder);
            }

            // Delete old modules
            List <string> oldModules = Directory.EnumerateFiles(gtMpServerModulesFolder, "EvoMp.Module.*.*",
                                                                SearchOption.AllDirectories)
                                       .Where(file => file.Contains("EvoMp.Module."))
                                       .ToList();

            // Get the DLLs from the project folders
            // (Including *.pdb files. Used for debugging)
            try
            {
                ConsoleOutput.AppendPrefix("\t");
                // Search for modules.
                List <string> newModules = Directory.EnumerateFiles(projectSolutionCompiledModulesFolder,
                                                                    "EvoMp.Module.*.*",
                                                                    SearchOption.AllDirectories).ToList();
                newModules = newModules.Select(file => file.Replace(@"\", "/")).ToList();

                newModules = newModules.Where(path => path.Contains("bin/"))
                             .Where(file =>
                                    file.ToLower().EndsWith("dll") || file.ToLower().EndsWith("pdb") ||
                                    file.ToLower().EndsWith("xml"))
                             .ToList();

                const string slash = "/";

                // Clean old modules wich existing as dll's in other modules
                foreach (string module in newModules.ToArray())
                {
                    // modulePath contains no "\" -> next
                    if (!module.Contains(slash))
                    {
                        continue;
                    }

                    string moduleFile = module.Substring(module.LastIndexOf(slash, StringComparison.Ordinal) + 1);
                    string modulePath = module.Substring(0, module.LastIndexOf(slash, StringComparison.Ordinal));

                    // ModuleFile contains no "." -> next
                    if (!moduleFile.Contains("."))
                    {
                        continue;
                    }
                    // Remove .dll .pdb etc..
                    moduleFile = moduleFile.Substring(0, moduleFile.LastIndexOf(".", StringComparison.Ordinal));

                    // Path didn't contains the name of the module file -> remove from new modules
                    if (!modulePath.Contains(moduleFile))
                    {
                        newModules.Remove(module);
                    }
                }

                // Copy new modules
                foreach (string newModule in newModules)
                {
                    string destFile = gtMpServerModulesFolder + slash + Path.GetFileName(newModule);

                    // Destfile exist & destfile is same to new file -> skip
                    if (File.Exists(destFile))
                    {
                        if (new FileInfo(destFile).LastWriteTime >= new FileInfo(newModule).LastWriteTime)
                        {
                            continue;
                        }
                    }

                    // Copy new module & write message
                    File.Copy(newModule, destFile, true);
                    if (destFile.EndsWith(".dll"))
                    {
                        ConsoleOutput.WriteLine(ConsoleType.Core,
                                                $"Copying module: ~#83cfff~\"{Path.GetFileName(destFile)}\".");
                    }
                }

                // Delete old modules
                foreach (string deleteModule in oldModules.Where(t => !newModules.Select(Path.GetFileName)
                                                                 .Contains(Path.GetFileName(t))))
                {
                    File.Delete(deleteModule);
                    ConsoleOutput.WriteLine(ConsoleType.Core,
                                            $"Delete old module: ~#83cfff~\"{Path.GetFileName(deleteModule)}\".");
                }

                ConsoleOutput.ResetPrefix();
            }
            catch (Exception exception)
            {
                // Throw exception
                throw new Exception($"Internal error in \"EvoMp.Core.Core.ModuleStructure\" " +
                                    $"{Environment.NewLine}" +
                                    $"{exception.Message}{Environment.NewLine}" +
                                    $"{exception.StackTrace}");
            }
        }
示例#3
0
        /// <summary>
        ///     Copying NuGet packages to the gtmp server files
        ///     Hint: Runs only if "DEBUG" constant is given
        /// </summary>
        public void CopyNuGetPackagesToServer()
        {
#if !DEBUG
            if (ParameterHandler.IsDefault("onlyCopy"))
            {
                ConsoleOutput.WriteLine(ConsoleType.Core, "Release state. Copying NuGet packeges skipped.");
                return;
            }
#endif
            const string serverRootFolder = ".";

            const string projectSolutionNuGetPackagesFolder = @"../EvoMp/packages";

            try
            {
                // Search for solution NuGet packages
                ConsoleOutput.WriteLine(ConsoleType.Core,
                                        $"Searching for new dependencies in " +
                                        $"~#83cfff~\"{Path.GetFullPath(projectSolutionNuGetPackagesFolder)}\\*\"~;~.");
                List <string> packageFiles = Directory.EnumerateFiles(projectSolutionNuGetPackagesFolder, "*",
                                                                      SearchOption.AllDirectories)
                                             .Where(file => file.ToLower().EndsWith("dll") || file.ToLower().EndsWith("xml"))
                                             .Where(file => file.Contains(@"lib\net45"))
                                             .Where(file => !Path.GetFileName(file).ToLower().StartsWith("evomp"))
                                             .ToList();

                // Clear duplicates
                packageFiles = packageFiles.Distinct().ToList();


                ConsoleOutput.WriteLine(ConsoleType.Core, "Using dependencies: ");
                ConsoleOutput.AppendPrefix("\t");

                List <string> usedPackagesList = new List <string>();
                // Copy new NuGet packages
                foreach (string packageFile in packageFiles)
                {
                    if (packageFile.EndsWith(".dll"))
                    {
                        string packageName = Path.GetFileName(packageFile).Replace("\\", "/");

                        // Packed already loaded -> continue;
                        if (usedPackagesList.Contains(packageName))
                        {
                            continue;
                        }

                        ConsoleOutput.WriteLine(ConsoleType.Core,
                                                $"~#83cfff~\"{packageName}\".");

                        usedPackagesList.Add(packageName);
                    }

                    // Get target filename
                    string destinationFile = serverRootFolder + @"/" + Path.GetFileName(packageFile).Replace("\\", "/");

                    // File exist -> Check creation date and delete if older
                    if (File.Exists(destinationFile))
                    {
                        // File is newest -> skip
                        if (new FileInfo(destinationFile).LastWriteTime >= new FileInfo(packageFile).LastWriteTime)
                        {
                            continue;
                        }

                        // Try to delete older file, if fails, skip file..
                        // I knew, not the best way.
                        // Feel free if u knew a better way..
                        // (But info one of the project directors about our change)
                        try
                        {
                            File.Delete(destinationFile);
                        }
                        catch (Exception)
                        {
                            continue;
                        }
                    }

                    // Copy file & message
                    File.Copy(packageFile, destinationFile);
                }

                ConsoleOutput.ResetPrefix();
            }
            catch (Exception exception)
            {
                // Throw exception
                throw new Exception($"Internal error in \"EvoMp.Core.Core.CopyNuGetPackagesToServer\" " +
                                    $"{Environment.NewLine}" +
                                    $"{exception.Message}{Environment.NewLine}" +
                                    $"{exception.StackTrace}");
            }
        }
示例#4
0
        /// <summary>
        ///     Trys to bind the given modules.
        ///     Print's hint if a module was created the wrong way
        /// </summary>
        /// <param name="modulePaths">Path to the modules, wich should binded</param>
        /// <returns>IKernel</returns>
        private void BindModules(IEnumerable <string> modulePaths)
        {
            ConsoleOutput.AppendPrefix("\t");
            // Progressing each module
            foreach (string modulePath in modulePaths)
            {
                bool hasNeededInterface = false;

                // load assembly
                Assembly moduleAssembly = Assembly.LoadFrom(modulePath);

                //Search for interface that's using the ModuleProperties attribute
                foreach (Type moduleClass in moduleAssembly.GetTypes())
                {
                    foreach (Type moduleInterface in moduleClass.GetInterfaces())
                    {
                        if (Attribute.IsDefined(moduleInterface, typeof(ModuleProperties)))
                        {
                            hasNeededInterface = true;
                            //  Main module class didn't based on BaseModule -> Exception
                            if (!moduleClass.GetAllBaseTypes().Contains(typeof(BaseModule)))
                            {
                                throw new NotValidModuleException(
                                          $"The module {modulePath} didn't base on the \"BaseModule\" abstract class." +
                                          Environment.NewLine +
                                          "Inherit from the BaseModule class.");
                            }

                            // Load module interface Attribute, to get module informations
                            ModuleProperties moduleProperties = (ModuleProperties)
                                                                Attribute.GetCustomAttribute(moduleInterface, typeof(ModuleProperties));

                            // Moduletype is not given as startup parameter -> Message & next module;
                            if (!ModuleTypeHandler.IsModuleTypeValid(moduleProperties.ModuleType))
                            {
                                ConsoleOutput.WriteLine(ConsoleType.Core,
                                                        $"~#51ff76~{moduleInterface.Name}~;~ skipped. Wrong gamemode. ~c~{moduleProperties.ModuleType}");
                                continue;
                            }

                            // Console output
                            ConsoleOutput.WriteLine(ConsoleType.Core,
                                                    $"~#51ff76~{moduleInterface.Name}~;~ -> ~#83ff9d~{moduleClass.FullName}~;~.");

                            // Add to list & bind module
                            _moduleAssemblies.Add(moduleAssembly);
                            _kernel.Bind(moduleInterface, moduleClass).To(moduleClass).InSingletonScope()
                            .WithConstructorArgument("api", context => Api).OnActivation(SharedEvents.OnOnModuleLoaded);
                        }
                    }
                }

                // No implemention of "ModuleProperties" -> exception
                if (!hasNeededInterface)
                {
                    throw new NotValidModuleException(
                              $"The module {modulePath} didn't implement the \"ModuleAttribute\" " +
                              "in the main Interface. " + Environment.NewLine +
                              "Please add the needed interface");
                }
            }

            // Sort modules by priority
            _moduleAssemblies.Sort((assembly1, assembly2) =>
            {
                int GetAssemblyPrio(Assembly ass)
                {
                    foreach (Type moduleClass in ass.GetTypes())
                    {
                        foreach (Type moduleInterface in moduleClass.GetInterfaces())
                        {
                            if (Attribute.IsDefined(moduleInterface, typeof(ModuleProperties)))
                            {
                                // Load module properties from interface
                                ModuleProperties moduleProperties = (ModuleProperties)
                                                                    Attribute.GetCustomAttribute(moduleInterface, typeof(ModuleProperties));

                                // Moduletype is not given as startup parameter -> next module;
                                if (!ModuleTypeHandler.IsModuleTypeValid
                                        (moduleProperties.ModuleType))
                                {
                                    continue;
                                }

                                return(moduleProperties.Priority);
                            }
                        }
                    }

                    return(int.MaxValue);
                }

                return(GetAssemblyPrio(assembly1).CompareTo(GetAssemblyPrio(assembly2)));
            });
            ConsoleOutput.WriteLine(ConsoleType.Core, "Sort modules done.");


            ConsoleOutput.ResetPrefix();
            // return created kernel
        }