コード例 #1
0
ファイル: Rbx2Source.cs プロジェクト: ddddeessse/Rbx2Source
        private void Rbx2Source_Load(object sender, EventArgs e)
        {
            string steamDir = null;

            Process[] steamProcesses = Process.GetProcessesByName("Steam");

            if (steamProcesses.Length > 0)
            {
                foreach (Process process in steamProcesses)
                {
                    try
                    {
                        ProcessModule module  = process.MainModule;
                        string        exePath = module.FileName;

                        FileInfo info      = new FileInfo(exePath);
                        string   directory = info.DirectoryName;

                        if (info.Name == "Steam.exe")
                        {
                            string apps = Path.Combine(directory, "steamapps");
                            if (Directory.Exists(apps))
                            {
                                steamDir = directory;
                            }
                        }
                    }
                    catch
                    {
                        Console.WriteLine("Can't access this steam process.");
                    }
                }
            }

            if (steamDir == null)
            {
                try
                {
                    RegistryKey classesRoot = Registry.ClassesRoot;
                    RegistryKey steam       = classesRoot.OpenSubKey(@"SOFTWARE\Valve\Steam");
                    steamDir = steam.GetValue("SteamPath") as string;
                }
                catch
                {
                    string programFilesX86 = Environment.GetEnvironmentVariable("ProgramFiles(x86)");
                    steamDir = Path.Combine(programFilesX86, "Steam");
                    if (!Directory.Exists(steamDir))
                    {
                        string programFiles = Environment.GetEnvironmentVariable("ProgramFiles");
                        steamDir = Path.Combine(programFiles, "Steam");
                        if (!Directory.Exists(steamDir))
                        {
                            showError("Cannot find Steam on this PC!\nTry running Steam so Rbx2Source can detect it.", true);
                        }
                    }
                }
            }

            string steamPath = steamDir.Replace('/', '\\');

            gatherSourceGames(steamPath);

            string steamApps = Path.Combine(steamPath, "steamapps");

            if (Directory.Exists(steamApps))
            {
                string libraryFolders = Path.Combine(steamApps, "libraryfolders.vdf");

                if (File.Exists(libraryFolders))
                {
                    string file = File.ReadAllText(libraryFolders);

                    string[] newlines = new string[] { "\r\n", "\n" };
                    string[] lines    = file.Split(newlines, StringSplitOptions.None);

                    foreach (string line in lines)
                    {
                        string[] kvPair = getStringsInQuotes(line);

                        if (kvPair.Length == 2)
                        {
                            string key   = kvPair[0];
                            string value = kvPair[1];

                            int index = -1;

                            if (int.TryParse(key, out index))
                            {
                                value = value.Replace("\\\\", "\\");
                                gatherSourceGames(value);
                            }
                        }
                    }
                }
            }

            string savedGameSelection = Settings.GetString("SelectedGame");

            int gameCount = 0;

            gameSelect.Items.Clear();

            foreach (string key in sourceGames.Keys)
            {
                gameSelect.Items.Add(key);
                gameCount++;
            }

            if (gameCount == 0)
            {
                showError("No Source Engine games were found on this PC!", true);
            }

            gameSelect.Enabled = true;
            compile.Enabled    = true;

            loadComboBox(gameSelect, "SelectedGame");
            loadComboBox(compilerTypeSelect, "CompilerType", 1);

            string userName = Settings.GetString("Username");

            if (userName != null)
            {
                TrySetUsername(userName);
            }

            string assetId = Settings.GetString("AssetId");

            TrySetAssetId(assetId);

            selectedGame = sourceGames[gameSelect.Text];
            updateDisplays();

            CONTROLS_TO_DISABLE_WHEN_COMPILING = new List <Control>()
            {
                compile, compilerInputField, gameSelect, viewCompiledModel, compilerTypeSelect, quickCompile
            };

            Links = new Dictionary <Control, string>()
            {
                { twitterLink, "https://www.twitter.com/CloneTeee1019" },
                { AJLink, "https://www.github.com/RedInquisitive" },
                { egoMooseLink, "https://www.github.com/EgoMoose" },
                { nemsTools, "http://nemesis.thewavelength.net/index.php?p=40" }
            };

            foreach (Control link in Links.Keys)
            {
                link.Click += new EventHandler(onLinkClicked);
            }

            Task.Run(async() =>
            {
                while (!IsDisposed)
                {
                    if (assetPreview.ImageLocation != assetPreviewImage)
                    {
                        CdnPender check = WebUtility.DownloadJSON <CdnPender>(assetPreviewImage);

                        if (check.Final)
                        {
                            assetPreviewImage          = check.Url;
                            assetPreview.ImageLocation = check.Url;
                        }
                        else
                        {
                            string currentPending = assetPreviewImage; // localize this in case it changes.
                            assetPreview.Image    = loadingImage;

                            Task <string> pend = Task.Run(() => WebUtility.PendCdnUrl(currentPending, false));

                            while (!pend.IsCompleted)
                            {
                                if (assetPreviewImage != currentPending)
                                {
                                    break;
                                }

                                if (pend.IsFaulted)
                                {
                                    break;
                                }

                                await Task.Delay(100);
                            }

                            if (assetPreviewImage == currentPending)
                            {
                                if (pend.IsFaulted) // mark the preview as broken.
                                {
                                    assetPreviewImage          = "";
                                    assetPreview.ImageLocation = "";
                                    assetPreview.Image         = brokenImage;
                                }
                                else
                                {
                                    string result              = pend.Result;
                                    assetPreviewImage          = result;
                                    assetPreview.ImageLocation = result;
                                }
                            }
                        }
                    }

                    if (Debugger.IsAttached && debugImg.Image != debugImage)
                    {
                        debugImg.Image = debugImage;
                    }

                    await Task.Delay(10);
                }
            });
        }