示例#1
0
        async void OnBindScriptDll(MyProgrammableBlock programmableBlock)
        {
            if (IsFaulted)
            {
                return;
            }

            if (!_proxyCache.TryGetValue(programmableBlock, out var proxy))
            {
                try
                {
                    proxy = ProgrammableBlockProxy.Wrap(programmableBlock);
                    _proxyCache[programmableBlock]   = proxy;
                    proxy.ProgrammableBlock.OnClose += OnProgrammableBlockClosed;
                }
                catch (BindingException e)
                {
                    MyLog.Default.Error($"{Plugin.Ident}: {Resources.ProgrammableBlockExtensions_OnBindScriptDll_BindingFailed}: {e}");
                    MyLog.Default.Flush();
                    MyAPIGateway.Utilities.ShowMissionScreen(
                        $"{Plugin.Ident}: {Resources.ProgrammableBlockExtensions_OnBindScriptDll_BindingFailed}",
                        currentObjective: Resources.ProgrammableBlockExtensions_OnBindScriptDll_EnableDebugging,
                        screenDescription: Resources.ProgrammableBlockExtensions_OnBindScriptDll_BindingError);
                    IsFaulted = true;
                    return;
                }
            }

            proxy.UnloadProgram();

            var fileName = await FileDialog.RequestFileName(
                Resources.ProgrammableBlockExtensions_OnBindScriptDll_BindScriptDLL,
                Resources.ProgrammableBlockExtensions_OnBindScriptDll_Filters,
                proxy.FileName).ConfigureAwait(false);

            MyLog.Default.WriteLine($"After RequestFileName {Thread.CurrentThread.ManagedThreadId} {Debugger.IsAttached}");
            MyLog.Default.Flush();

            if (fileName != null)
            {
                MyLog.Default.WriteLine($"Found {fileName} {Thread.CurrentThread.ManagedThreadId}");
                MyLog.Default.Flush();
                LoadScriptAssembly(fileName, proxy);
                MyLog.Default.WriteLine($"Loaded {fileName} {Thread.CurrentThread.ManagedThreadId}");
                MyLog.Default.Flush();
            }

            proxy.ProgrammableBlock?.RaisePropertiesChanged();
        }
示例#2
0
        void LoadScriptAssembly(string fileName, ProgrammableBlockProxy proxy)
        {
            var assembly = LoadAssembly(fileName);

            if (assembly == null)
            {
                MyAPIGateway.Utilities.ShowMissionScreen(
                    $"{Plugin.Ident}: {Resources.ProgrammableBlockExtensions_OnBindScriptDll_BindingFailed}",
                    currentObjective: Resources.ProgrammableBlockExtensions_OnBindScriptDll_BindScriptDLL,
                    screenDescription: Resources.ProgrammableBlockExtensions_LoadScriptAssembly_InvalidAssembly);
                return;
            }

            var programTypes = assembly.DefinedTypes.Where(type => !type.IsAbstract && typeof(MyGridProgram).IsAssignableFrom(type)).ToList();

            if (programTypes.Count == 0)
            {
                MyAPIGateway.Utilities.ShowMissionScreen(
                    $"{Plugin.Ident}: {Resources.ProgrammableBlockExtensions_OnBindScriptDll_BindingFailed}",
                    currentObjective: Resources.ProgrammableBlockExtensions_OnBindScriptDll_BindScriptDLL,
                    screenDescription: Resources.ProgrammableBlockExtensions_LoadScriptAssembly_InvalidAssembly);
                return;
            }

            if (programTypes.Count > 1)
            {
                MyAPIGateway.Utilities.ShowMissionScreen(
                    $"{Plugin.Ident}: {Resources.ProgrammableBlockExtensions_OnBindScriptDll_BindingFailed}",
                    currentObjective: Resources.ProgrammableBlockExtensions_OnBindScriptDll_BindScriptDLL,
                    screenDescription: Resources.ProgrammableBlockExtensions_LoadScriptAssembly_TooManyGridPrograms);
                return;
            }

            if (proxy.LoadProgram(programTypes[0]))
            {
                HasLoadedProgram = true;
            }
        }