/// <summary> /// 简单的服务定位器 /// </summary> static ServiceLocator() { // 添加全局日志 LogManager.AddLoggerAdapter(new Log4NetLoggerAdapter()); GlobalLogger = LogManager.GetLogger(typeof(ServiceLocator)); try { // 添加服务 需要注意服务的依赖顺序 DebuggerContainer = new DebuggerContainer(); ScheduleService = new ScheduleService(); ProfileService = new ProfileService(Global.ProfilePath); ConfigManager = new ConfigManager(Global.ConfigPath); MapManager = new MapManager(Global.MapPath); WasManager = new WasManager(Global.WdfPath); DrawingService = new DrawingService(); ClientEngine = new ClientEngine(); Window = new GlWindow(); } catch (Exception e) { GlobalLogger.Error(e); MessageBox.Show(Resources.ServiceLocator_Load_Error); Environment.Exit(0); } }
private static int GenerateSources( IList <string> source, IList <string> classToGenerate, string projectAssembly = "Generated.WASM", string projectGenerationLocation = "_generated", bool force = false ) { try { var stopwatch = Stopwatch.StartNew(); ValidateArguments( source, classToGenerate, projectAssembly, projectGenerationLocation ); GlobalLogger.Info($"projectAssembly: {projectAssembly}"); GlobalLogger.Info($"projectGenerationLocation: {projectGenerationLocation}"); GlobalLogger.Info($"classToGenerate.Length: {classToGenerate.Count}"); foreach (var classToGenerateItem in classToGenerate) { GlobalLogger.Info($"classToGenerateItem: {classToGenerateItem}"); } GlobalLogger.Info($"sourceFile.Length: {source.Count}"); foreach (var sourceFileItem in source) { GlobalLogger.Info($"sourceFile: {sourceFileItem}"); } projectGenerationLocation = Path.Combine( ".", projectGenerationLocation ); var sourceDirectory = Path.Combine( ".", SOURCE_FILES_DIRECTORY_NAME ); var sourceFiles = CopyAndDownloadSourceFiles( source ); var generationList = classToGenerate; // Check for already Generated Source. var projectAssemblyDirectory = Path.Combine( projectGenerationLocation, projectAssembly ); if (Directory.Exists( projectAssemblyDirectory )) { if (!force) { GlobalLogger.Error( $"Project Assembly Directory was not empty: {projectAssemblyDirectory}" ); GlobalLogger.Error( $"Use --force to replace directory." ); return(502); } GlobalLogger.Warning( $"Deleting existing projectAssemblyDirectory: {projectAssemblyDirectory}" ); Directory.Delete( projectAssemblyDirectory, true ); } var textFormatter = new NoFormattingTextFormatter(); var writer = new ProjectWriter( projectGenerationLocation, projectAssembly ); new GenerateInteropSource().Run( projectAssembly, sourceDirectory, sourceFiles, generationList, writer, textFormatter, new Dictionary <string, string> { { "BABYLON.PointerInfoBase | type", "int" } } ); stopwatch.Stop(); GlobalLogger.Success($"Took {stopwatch.ElapsedMilliseconds}ms to Generate Source Project."); return(0); } catch (ArgumentException ex) { GlobalLogger.Error( $"Argument failure: {ex.ParamName} -> {ex.Message}" ); return(404); } catch (InvalidSourceFileException ex) { GlobalLogger.Error( $"Invalid Source File Exception: {ex.Message}" ); return(501); } }
public static string Write( ClassStatement classStatement, IEnumerable <PublicMethodStatement> methods, ClassGenerationTemplates templates ) { if (methods.Count() == 0) { return(string.Empty); } var section = new StringBuilder(); var current = 1; foreach (var method in methods) { GlobalLogger.Info($"Generating Method: {method}"); var isLast = current == methods.Count(); var isClassResponse = ClassResponseIdentifier.Identify( method.Type, method.UsedClassNames ); var isArray = ArrayResponseIdentifier.Identify( method.Type ); var template = templates.Method; var methodType = method.Type; var type = TypeStatementWriter.Write( methodType ); var typeNoModifier = TypeStatementWriter.Write( methodType, false ); var propertyArguments = string.Empty; var isNotSupported = NotSupportedIdentifier.Identify( method ); var isTask = method.Type.IsTask; var isEnum = TypeEnumIdentifier.Identify( method.Type ); var isAction = method.Type.Name == GenerationIdentifiedTypes.Action || (method.Arguments.Take(1).Any(a => a.Type.IsAction && a.Name == "callback")); var bodyTemplate = templates.ReturnTypePrimitiveTemplate; var returnTypeContent = templates.InteropFunc; var arguments = string.Empty; var argumentStrings = new List <string>(); var classNamespace = classStatement.Namespace; var namespacedMethod = string.Join( ".", classNamespace, classStatement.Name, method.Name ); var propertyIdentifier = "this.___guid"; // [[FUNCTION_GENERICS]] = functionGenerics = T, EventState, Task var functionGenerics = string.Empty; var genericSection = string.Empty; var whereConstraint = string.Empty; var taskType = TypeStatementWriter.Write( methodType, false ); var taskAsync = string.Empty; var taskAwait = string.Empty; if (classNamespace == string.Empty) { namespacedMethod = string.Join( ".", classStatement.Name, method.Name ); } // Argument Generation if (isAction) { var functionGenericsStrings = new List <string>(); var actionArgument = method.Arguments.FirstOrDefault( argument => argument.Type.Name == GenerationIdentifiedTypes.Action ); if (actionArgument != null) { foreach (var genericType in actionArgument.Type.GenericTypes) { functionGenericsStrings.Add( TypeStatementWriter.Write( genericType, ignorePrefix: true ) ); } // [[ARGUMENTS]] = arguments = T eventData, EventState eventState foreach (var argument in actionArgument.Type.Arguments.OrderBy(a => a.IsOptional)) { argumentStrings.Add( ArgumentWriter.Write( argument, true, string.Empty, ignorePrefix: false ) ); } // [[PROPERTY_ARGUMENTS]] = propertyArguments = eventData, eventState propertyArguments = string.Join( ", ", actionArgument.Type.Arguments.Select( argument => DotNetNormalizer.Normalize(argument.Name) ) ); } functionGenericsStrings.Add( "Task" ); functionGenerics = string.Join( ", ", functionGenericsStrings ); } else { // TODO: [Re-factor] : Move to Writer foreach (var argument in method.Arguments.OrderBy(a => a.IsOptional)) { argumentStrings.Add( ArgumentWriter.Write( argument, true, " = null" ) ); } propertyArguments = method.Arguments.Any() ? ", " + string.Join( ", ", method.Arguments.Select( argument => DotNetNormalizer.Normalize(argument.Name) ) ) : string.Empty; if (VoidArgumentIdenfifier.Identify(method.Arguments)) { GlobalLogger.Error( $"Found void argument in method: {method.Name}" ); continue; } } arguments = string.Join( ", ", argumentStrings ); // Template/ReturnTypeContent Dictation if (isAction) { template = templates.MethodActionTemplate; bodyTemplate = templates.ReturnTypeVoidTemplate; if (method.IsStatic) { template = templates.MethodStaticActionTemplate; } } if (isEnum) { returnTypeContent = templates.InteropFunc; } else if (isClassResponse && isArray) { returnTypeContent = templates.InteropFuncArrayClass; } else if (isClassResponse) { returnTypeContent = templates.InteropFuncClass; } else if (isArray) { returnTypeContent = templates.InteropFuncArray; } if (isTask) { returnTypeContent = templates.InteropTask; if (isClassResponse && isArray) { returnTypeContent = templates.InteropTaskArrayClass; } else if (isClassResponse || taskType == GenerationIdentifiedTypes.CachedEntity) { returnTypeContent = templates.InteropTaskClass; } else if (isArray) { returnTypeContent = templates.InteropTaskArray; } // Change up the taskType if 'void'; if (taskType == GenerationIdentifiedTypes.Void) { bodyTemplate = templates.ReturnTypeVoidTemplate; taskType = GenerationIdentifiedTypes.CachedEntity; taskAsync = "async "; taskAwait = "await "; } } if (method.IsStatic) { var classStatementIdentitiferList = new string[] { classStatement.Name, }; if (classNamespace != string.Empty) { classStatementIdentitiferList = new string[] { classStatement.Namespace, classStatement.Name, }; } propertyIdentifier = string.Join( ", ", string.Join( ".", classStatementIdentitiferList ).Split(".").Select(part => @$ "" "{part}" "") ); } // Replace the Type in the Return TypeContent to Object // This is to avoid parsing errors and just get a generic object back from method calls. if (method.Type.Name == GenerationIdentifiedTypes.Void) { bodyTemplate = templates.ReturnTypeVoidTemplate; returnTypeContent = returnTypeContent.Replace( "[[ARRAY_TYPE]]", GenerationIdentifiedTypes.CachedEntity ).Replace( "[[NEW_TYPE]]", GenerationIdentifiedTypes.CachedEntity ); } if (method.GenericTypes.Any()) { var genericTypeString = string.Join( ", ", method.GenericTypes ); // TODO: [Template] : Move to templates genericSection = $"<{genericTypeString}>"; if (isClassResponse && method.GenericTypes.Any( genericType => genericType == typeNoModifier ) ) { // TODO: [Template] : Move to templates whereConstraint = string.Join( "", method.GenericTypes.Select( genericType => $" where {genericType} : CachedEntity, new()" ) ); } } if (isNotSupported) { template = "// [[NAME]] is not supported by the platform yet"; } template = template.Replace( "[[BODY]]", bodyTemplate ).Replace( "[[RETURN_TYPE_CONTENT]]", returnTypeContent ).Replace( "[[NAMESPACED_METHOD]]", namespacedMethod ) //.Replace( // "[[CACHE_SECTION]]", // string.Empty //).Replace( // "[[CACHE_SETTTER_SECTION]]", // string.Empty //) .Replace( "[[ARRAY]]", string.Empty ).Replace( "[[STATIC]]", method.IsStatic ? "static " : string.Empty ).Replace( "[[NAME]]", DotNetNormalizer.Normalize( method.Name ) ).Replace( "[[NAME_CAPTIALIZED]]", method.Name.Captialize() ).Replace( "[[CLASS_NAME]]", classStatement.Name ).Replace( "[[TYPE]]", TypeStatementWriter.Write( methodType ) ).Replace( "[[ARRAY_TYPE]]", TypeStatementWriter.Write( methodType, false ) ).Replace( "[[NEW_TYPE]]", TypeStatementWriter.Write( methodType, false ) ).Replace( "[[TASK_TYPE]]", taskType ).Replace( "[[GENERIC_SECTION]]", genericSection ).Replace( "[[ARGUMENTS]]", arguments ).Replace( "[[WHERE_CONSTRAINT]]", whereConstraint ).Replace( "[[PROPERTY_IDENTIFIER]]", propertyIdentifier ).Replace( "[[PROPERTY]]", DotNetNormalizer.Normalize( method.Name ) ).Replace( "[[PROPERTY_ARGUMENTS]]", propertyArguments ).Replace( "[[INTERFACE_POSTFIX]]", method.IsInterfaceResponse ? Constants.INTERFACE_POSTFIX : string.Empty ).Replace( "[[FUNCTION_GENERICS]]", functionGenerics ).Replace( "[[TASK_ASYNC]]", taskAsync ).Replace( "[[TASK_AWAIT]]", taskAwait ); section.Append( template ); if (!isLast) { section.Append( Environment.NewLine ).Append( Environment.NewLine ); } current++; } return(section.ToString()); }
/// <summary> /// Runs this server /// </summary> public void Run() { Condition.Requires(Kernel, "kernel").IsNull("Expected server to not be configured yet"); // Install logging GlobalLogger = ConfigureGlobalLogger(); Condition.Requires(GlobalLogger, "GlobalLogger").IsNotNull(); AppDomain.CurrentDomain.UnhandledException += (obj, args) => GlobalLogger.Error(args.ExceptionObject as Exception, "Unhandled Exception"); Log = GlobalLogger.ForContext(GetType()); // Load all modules Log.Information("Assembling module list"); var modules = new List <AbstractModule>(); GetAllRegisteredModules(modules); // Parse Flags, and set static flags before initializing any module. Has to happen // after the module list is generated so that all required assemblies are loaded. FlagParser = new FlagParser(Log.ForContext <FlagParser>()); FlagParser.ParseCommandLineArguments(Environment.GetCommandLineArgs().Skip(1)); // Initialize every module Log.Information("Registering Modules:"); foreach (var module in modules) { Log.Information(" {module}", module); // Pre-inject the logger so that it is always available (even before the kernel is initialized) module.Logger = GlobalLogger.ForContext(module.GetType()); // Same goes for flags FlagParser.InjectFlags(module); } // Initialize Kernel Log.Information("Initializing Injection Kernel"); Kernel = new StandardKernel(modules.ToArray()); Kernel.Settings.InjectNonPublic = true; ConfigureBindings(); // Initialize modules Log.Information("Injecting module members"); foreach (var module in modules) { // Lazily initialize module injections Kernel.Inject(module); } // Run all server tasks var taskModules = modules.OfType <AbstractTaskModule>().ToArray(); Log.Information("=============================================================="); Log.Information("Starting Tasks: {taskModules}", taskModules); var tasks = taskModules.Select(x => Task.Run(async() => { try { await x.Run(); } catch (Exception ex) { Log.Error(ex, "Task execution failed"); } })).ToArray(); // Wait for all tasks to finish Task.WaitAll(tasks); Log.Information("All tasks have finished execution"); }