private BooModuleManager GetModuleManager()
        {
            //if(booModuleManager == null)
            {
                var mgr = booModuleManager = new BooModuleManager(CustomSkillsPlugin.Instance,
                                                                  ScriptHelpers.GetReferences(),
                                                                  ScriptHelpers.GetDefaultImports(),
                                                                  ScriptHelpers.GetEnsuredMethodSignatures());

                mgr.AssemblyNamePrefix = "skill_";
            }

            return(booModuleManager);
        }
Пример #2
0
        //private static void LoadScripts(string basePath, List<ClassDefinition> classDefs)
        private static void LoadScripts(List <Tuple <string, ClassDefinition> > filesAndDefs)
        {
            const string AssemblyNamePrefix = "ClassDef_";

            LevelingPlugin.Instance.LogPrint("Compiling Class scripts...", TraceLevel.Info);

            //get script files paths
            var scriptedDefs = filesAndDefs.Where(d => !string.IsNullOrWhiteSpace(d.Item2.ScriptPath));
            var booScripts   = scriptedDefs.Select(d => Path.Combine(Path.GetDirectoryName(d.Item1), d.Item2.ScriptPath))
                               .ToList();

            var newModuleManager = new BooModuleManager(LevelingPlugin.Instance,
                                                        ScriptHelpers.GetReferences(),
                                                        ScriptHelpers.GetDefaultImports(),
                                                        GetEnsuredMethodSignatures());

            newModuleManager.AssemblyNamePrefix = AssemblyNamePrefix;

            foreach (var f in booScripts)
            {
                newModuleManager.Add(f);
            }

            Dictionary <string, CompilerContext> results = null;

            if (ModuleManager != null)
            {
                results = newModuleManager.IncrementalCompile(ModuleManager);
            }
            else
            {
                results = newModuleManager.Compile();
            }

            ModuleManager = newModuleManager;

            //link!
            foreach (var def in scriptedDefs)
            {
                //var fileName = Path.Combine(basePath, def.ScriptPath);
                var fileName = Path.Combine(Path.GetDirectoryName(def.Item1), def.Item2.ScriptPath);

                //if newly compile assembly, examine the context, and try to link to the new assembly
                if (results.TryGetValue(fileName, out var context))
                {
                    var scriptAssembly = context.GeneratedAssembly;

                    if (scriptAssembly != null)
                    {
                        var result = def.Item2.LinkToScriptAssembly(scriptAssembly);

                        //if(!result)
                        //	//	CustomNpcsPlugin.Instance.LogPrint($"Failed to link {kvp.Key}.", TraceLevel.Info);
                    }
                }
                else
                {
                    var scriptAssembly = ModuleManager[fileName];

                    if (scriptAssembly != null)
                    {
                        var result = def.Item2.LinkToScriptAssembly(scriptAssembly);

                        //if(!result)
                        //	//	CustomNpcsPlugin.Instance.LogPrint($"Failed to link {kvp.Key}.", TraceLevel.Info);
                    }
                }
            }
        }
Пример #3
0
        /// <summary>
        /// Loads in json definition files, and attempts to compile and link to any related scripts.
        /// </summary>
        protected virtual void LoadDefinitions()
        {
            //CustomNpcsPlugin.Instance.LogPrint("Using new definition loading, include paths aren't 100% reliable yet!",TraceLevel.Warning);

            var include = DefinitionInclude.Load <TCustomType>(ConfigPath);

            Definitions = DefinitionInclude.Flatten <TCustomType>(include);

            var rootResult = new ValidationResult(ConfigPath);

            rootResult.Source = ConfigPath;

            foreach (var def in Definitions)
            {
                var name   = "";
                var result = def.Validate();

                if (!string.IsNullOrWhiteSpace(def.Name))
                {
                    name = $" - '{def.Name}'";
                }

                //result.Source = $"{def.FilePath}[{def.LineNumber},{def.LinePosition}]{name}";

                //CustomNpcsPlugin.Instance.LogPrint(result);
                rootResult.Children.Add(result);
            }

            CustomNpcsPlugin.Instance.LogPrint(rootResult);

            //Definitions = DefinitionLoader.LoadFromFile<TCustomType>(ConfigPath);

            CustomNpcsPlugin.Instance.LogPrint("Compiling scripts...", TraceLevel.Info);

            //get script files paths
            var booScripts = Definitions.Where(d => !string.IsNullOrWhiteSpace(d.ScriptPath))
                             .Select(d => Path.Combine(BasePath, d.ScriptPath))
                             .ToList();

            var newModuleManager = new BooModuleManager(CustomNpcsPlugin.Instance,
                                                        ScriptHelpers.GetReferences(),
                                                        ScriptHelpers.GetDefaultImports(),
                                                        GetEnsuredMethodSignatures());

            newModuleManager.AssemblyNamePrefix = AssemblyNamePrefix;

            foreach (var f in booScripts)
            {
                newModuleManager.Add(f);
            }

            Dictionary <string, CompilerContext> results = null;

            if (ModuleManager != null)
            {
                results = newModuleManager.IncrementalCompile(ModuleManager);
            }
            else
            {
                results = newModuleManager.Compile();
            }

            ModuleManager = newModuleManager;

            var scriptedDefinitions = Definitions.Where(d => !string.IsNullOrWhiteSpace(d.ScriptPath));

            foreach (var def in scriptedDefinitions)
            {
                var fileName = Path.Combine(BasePath, def.ScriptPath);

                //if newly compile assembly, examine the context, and try to link to the new assembly
                if (results.TryGetValue(fileName, out var context))
                {
                    var scriptAssembly = context.GeneratedAssembly;

                    if (scriptAssembly != null)
                    {
                        var result = def.LinkToScriptAssembly(scriptAssembly);

                        //if(!result)
                        //	//	CustomNpcsPlugin.Instance.LogPrint($"Failed to link {kvp.Key}.", TraceLevel.Info);
                    }
                }
                else
                {
                    var scriptAssembly = ModuleManager[fileName];

                    if (scriptAssembly != null)
                    {
                        var result = def.LinkToScriptAssembly(scriptAssembly);

                        //if(!result)
                        //	//	CustomNpcsPlugin.Instance.LogPrint($"Failed to link {kvp.Key}.", TraceLevel.Info);
                    }
                }
            }

            definitionMap = new Dictionary <string, TCustomType>();

            foreach (var def in Definitions)
            {
                definitionMap.Add(def.Name.ToLowerInvariant(), def);
            }
        }