Example #1
0
        private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
        {
            MessageBox.Show(e.Exception.Message, Strings.Program_Application_ThreadException_Unhandled_Thread_Exception);

            Logger.MethodName = MethodBase.GetCurrentMethod().Name;
            Logger.Log(Strings.Program_Application_ThreadException_Unhandled_Thread_Exception, ErrorType.General,
                       e.Exception);
        }
Example #2
0
        public Assembly Compile(string input)
        {
            var results = CompileAssemblyFromSource(parameters, input);

            if (results.Errors.HasErrors)
            {
                var message =
                    new StringBuilder(
                        $"{Strings.NativeCompiler_Compile_Syntax_error}{Environment.NewLine}{Strings.Details}");
                var compilationException = CreateCompilationExceptionFromCompilerResults(results);

                if (compilationException.HasMainCodeErrors)
                {
                    message.Append(
                        $"{Environment.NewLine} {(IsScripting ? Strings.NativeCompiler_Compile_Script_errors : Strings.NativeCompiler_Compile_Expression_errors)}:");
                    message.Append(errorCollectionToString(compilationException.Errors[CompilationErrorPlace.MainCode]));
                }

                if (compilationException.HasCustomFunctionsErrors)
                {
                    message.Append($"{Environment.NewLine} {Strings.NativeCompiler_Compile_Custom_functions_errors}:");
                    message.Append(
                        errorCollectionToString(compilationException.Errors[CompilationErrorPlace.CustomFunctions]));
                }

                if (compilationException.HasInternalErrors)
                //if there is any warning in our internal code that means we are aware of it and we dont wanna show it to user :)
                {
                    message.Append($"{Environment.NewLine} {Strings.NativeCompiler_Compile_Internal_errors}:");
                    message.Append(errorCollectionToString(compilationException.Errors[CompilationErrorPlace.Internal]));
                }

                compilationException = new CompilationException(message.ToString())
                {
                    Errors = compilationException.Errors
                };
                // compilationException.Message = message;
                // var ex = new CompilationException(message) {Errors = compilerErrors};

                logger.MethodName = MethodBase.GetCurrentMethod().Name;
                // Logger.Parameters["NativeCompilerInput"] = input;
                logger.Parameters["NativeCompilerOutput"] = "";
                foreach (var str in results.Output)
                {
                    logger.Parameters["NativeCompilerOutput"] += str + Environment.NewLine;
                }
                logger.Log(compilationException.Message, ErrorType.Compilation, compilationException);

                throw compilationException;
            }

            parameters.TempFiles.KeepFiles = false;
            parameters.TempFiles.Delete();
            results.TempFiles.KeepFiles = false;
            results.TempFiles.Delete();

            return(results.CompiledAssembly);
        }
        protected Delegate Compile()
        {
            SetVariablesAndSignatures();
            _customFunctionsCSharpCode = _tslCompiler.TransformToCSharp(CustomFunctionsTslCode);
            MainCSharpCode             = _tslCompiler.TransformToCSharp(MainTslCode);
            TransformImplicitToExplicit();

            var fullCode = BuildCode();

            try
            {
                var assembly = NativeCompiler.Compile(fullCode);
                var cls      = assembly.GetType("FunctionsCreatorNS.FunctionsCreator");
                var method   = cls.GetMethod("CustomFunction", BindingFlags.Static | BindingFlags.Public);
                return(Delegate.CreateDelegate(_delegateType, method));
            }
            catch (Exception ex)
            {
                if (ex is CompilationException)
                {
                    throw;
                }

                var message =
                    Strings
                    .ErrorInExpressionSyntaxOneOfUsedFunctionsDoesNotExistIsIncompatibleWithGivenArgumentsOrYouJustMadeAMistakeWritingExpression;
                message += Environment.NewLine + Strings.Details;
                message += Environment.NewLine + ex.Message;

                Logger.Parameters["MainTslCode"]         = MainTslCode;
                Logger.Parameters["MainCSharpCode"]      = MainCSharpCode;
                Logger.Parameters["customFunctionsCode"] = _customFunctionsCSharpCode;
                Logger.MethodName = MethodBase.GetCurrentMethod().Name;
                Logger.Log(message, ErrorType.Evaluation, ex);

                throw new EvaluationException(message, ex);
            }
        }
Example #4
0
        private static void GetCustomFonts()
        {
            mathFontCollection      = new PrivateFontCollection();
            scriptingFontCollection = new PrivateFontCollection();

            var pathToFont  = GlobalConfig.FullPath("Static", "fonts", "CAMBRIA.TTC");
            var pathToFont2 = GlobalConfig.FullPath("Static", "fonts", "consola.ttf");

            try
            {
                mathFontCollection.AddFontFile(pathToFont);
                scriptingFontCollection.AddFontFile(pathToFont2);
            }
            catch (Exception ex)
            {
                var nex =
                    new Exception(
                        "Probably missing " + pathToFont + " or " + pathToFont2 + " file\nDetails:" + ex.Message, ex);
                logger.MethodName = MethodBase.GetCurrentMethod().Name;
                logger.Log("Probably missing " + pathToFont + " file\nDetails:" + ex.Message, ErrorType.General, nex);
                throw nex;
            }
        }
Example #5
0
        public static void Initialize()
        {
            UnmanagedHandler = HandleUnmanagedException;

            //  var msvcr = Resources.msvcr120_x86;
            var gsl = Resources.gsl_x86;

            //  var cblas = Resources.cblas_x86;

            if (Environment.Is64BitProcess && IntPtr.Size == 8)
            {
                //msvcr = Resources.msvcr120_x64;
                gsl = Resources.gsl_x64;
                //    cblas = Resources.cblas_x64;
            }
            //else if (!Environment.Is64BitProcess && IntPtr.Size == 4)

            //else
            //  throw new Exception("Inconsistent system - IntPtr.Size and Environment.Is64BitProcess dont match");

            try
            {
                //       EmbeddedDllClass.ExtractEmbeddedDlls(GlobalConfig.msvcrDllName, msvcr);
                EmbeddedDllClass.ExtractEmbeddedDlls(GlobalConfig.gslDllName, gsl);
                //    EmbeddedDllClass.ExtractEmbeddedDlls(GlobalConfig.gslCblasDllName, cblas);
                //   System.Threading.Thread.Sleep(1000);
                NativeMethods.gsl_set_error_handler(UnmanagedHandler);
            }
            catch (Exception exception)
            {
                logger.Log("ExtractEmbeddedDlls failed", ErrorType.General, exception);
                try
                {
                    //  string msvcrTempPath = Path.Combine(Path.GetTempPath(), GlobalConfig.msvcrDllName);
                    var gslTempPath = Path.Combine(Path.GetTempPath(), GlobalConfig.gslDllName);
                    //  string cblasTempPath = Path.Combine(Path.GetTempPath(), GlobalConfig.gslCblasDllName);

                    //     File.WriteAllBytes(msvcrTempPath, msvcr);
                    // File.WriteAllBytes(cblasTempPath, cblas);
                    File.WriteAllBytes(gslTempPath, gsl);

                    //  var h0 = NativeMethods.LoadLibrary(msvcrTempPath);
                    //    var h1 = NativeMethods.LoadLibrary(cblasTempPath);
                    var h2 = NativeMethods.LoadLibrary(gslTempPath);

                    if (/*h0 == IntPtr.Zero || h1 == IntPtr.Zero ||*/ h2 == IntPtr.Zero)
                    {
                        throw new Win32Exception(
                                  $"{Strings.GSLInitializer_Initialize_Could_not_load_the_Computator_NET_modules_at_the_paths} '{gslTempPath}'{Environment.NewLine}.",
                                  new Win32Exception()); // Calls GetLastError
                    }

                    NativeMethods.gsl_set_error_handler(UnmanagedHandler);
                }
                catch (Exception exception2)
                {
                    logger.Log("LoadLibrary failed", ErrorType.General, exception2);
                    MessageBox.Show(
                        $"{Strings.Program_Main_Exception_during_startup}.{Environment.NewLine}ExtractEmbeddedDlls {Strings.Exception}:{Environment.NewLine}{exception}{Environment.NewLine}LoadLibrary {Strings.Exception}:{Environment.NewLine}{exception2}",
                        Strings.Error);
                }
            }
        }
Example #6
0
 public void LogError(string message, ErrorType errorType, Exception ex)
 {
     logger.MethodName = MethodBase.GetCurrentMethod().Name;
     logger.Log(message, ErrorType.General, ex);
 }
Example #7
0
        public static void Initialize(IMessagingService messagingService)
        {
            UnmanagedHandler = HandleUnmanagedException;


            byte[] gsl;

            if (Environment.Is64BitProcess && IntPtr.Size == 8)
            {
                gsl = GlobalConfig.IsUnix
                    ? (GlobalConfig.IsMacOS ? Resources.libgsl_osx_amd64 : Resources.libgsl_amd64)
                    : Resources.gsl_x64;
            }
            else if (!Environment.Is64BitProcess && IntPtr.Size == 4)
            {
                gsl = GlobalConfig.IsUnix
                    ? (GlobalConfig.IsMacOS ? Resources.libgsl_osx_i686 : Resources.libgsl_i686)
                    : Resources.gsl_x86;
            }
            else
            {
                throw new PlatformNotSupportedException("Inconsistent operating system. Handles only 32 and 64 bit OS.");
            }

            var cblas = Environment.Is64BitProcess
                ? (GlobalConfig.IsMacOS ? Resources.libgslcblas_osx_amd64 : Resources.libgslcblas_amd64)
                : (GlobalConfig.IsMacOS ? Resources.libgslcblas_osx_i686 : Resources.libgslcblas_i686);

            try
            {
                EmbeddedDllClass.ExtractEmbeddedDlls(GlobalConfig.GslDllName, gsl);


                if (GlobalConfig.IsUnix)
                {
                    EmbeddedDllClass.ExtractEmbeddedDlls(GlobalConfig.GslCblasDllName, cblas);
                }

                switch (Settings.Default.CalculationsErrors)
                {
                case CalculationsErrors.ReturnNAN:
                    NativeMethods.gsl_set_error_handler_off();
                    break;

                case CalculationsErrors.ShowError:
                    NativeMethods.gsl_set_error_handler(UnmanagedHandler);
                    break;
                }
            }
            catch (Exception exception)
            {
                logger.Log("ExtractEmbeddedDlls failed", ErrorType.General, exception);
                try
                {
                    var gslTempPath = Path.Combine(Path.GetTempPath(), GlobalConfig.GslDllName);

                    File.WriteAllBytes(gslTempPath, gsl);

                    if (GlobalConfig.IsUnix)
                    {
                        var cblasTempPath = Path.Combine(Path.GetTempPath(), GlobalConfig.GslCblasDllName);
                        File.WriteAllBytes(cblasTempPath, cblas);

                        var h1 = NativeMethods.dlopen(cblasTempPath, NativeMethods.RTLD.RTLD_GLOBAL);
                        if (h1 == IntPtr.Zero)
                        {
                            throw new Win32Exception(
                                      $"{Strings.GSLInitializer_Initialize_Could_not_load_the_Computator_NET_modules_at_the_paths} '{cblasTempPath}'{Environment.NewLine}.",
                                      new Win32Exception()); // Calls GetLastError
                        }
                    }

                    var h2 = GlobalConfig.IsUnix ? NativeMethods.dlopen(gslTempPath, NativeMethods.RTLD.RTLD_GLOBAL) : NativeMethods.LoadLibrary(gslTempPath);

                    if (h2 == IntPtr.Zero)
                    {
                        throw new Win32Exception(
                                  $"{Strings.GSLInitializer_Initialize_Could_not_load_the_Computator_NET_modules_at_the_paths} '{gslTempPath}'{Environment.NewLine}.",
                                  new Win32Exception()); // Calls GetLastError
                    }

                    NativeMethods.gsl_set_error_handler(UnmanagedHandler);
                }
                catch (Exception exception2)
                {
                    var funcName = GlobalConfig.IsUnix ? "dlopen" : "LoadLibrary";
                    logger.Log($"{funcName} failed", ErrorType.General, exception2);
                    messagingService.Show(
                        $"{Strings.Program_Main_Exception_during_startup}.{Environment.NewLine}ExtractEmbeddedDlls {Strings.Exception}:{Environment.NewLine}{exception}{Environment.NewLine}{funcName} {Strings.Exception}:{Environment.NewLine}{exception2}",
                        Strings.Error);
                }
            }
        }