/// <summary> /// Creates a list of TDefinitions, loading defintitions found in Categories and Includes. /// </summary> /// <typeparam name="TDefinition"></typeparam> /// <param name="sourceDefinitions"></param> /// <returns></returns> public static List <TDefinition> Flatten <TDefinition>(List <DefinitionBase> sourceDefinitions) where TDefinition : DefinitionBase { var result = new List <TDefinition>(sourceDefinitions.Count); foreach (var baseDef in sourceDefinitions) { var def = baseDef as TDefinition; if (def != null) { result.Add(def); } else if (baseDef is CategoryDefinition) { var category = (CategoryDefinition)baseDef; foreach (var kvp in category.DefinitionIncludes) { result.AddRange(DefinitionInclude.Flatten <TDefinition>(kvp.Value)); } } } return(result); }
/// <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); } }