IsFileLocked() public static method

public static IsFileLocked ( FileInfo file ) : bool
file System.IO.FileInfo
return bool
コード例 #1
0
        private static void Load()
        {
            if (!loaded)
            {
                loaded = true;

                var pluginsFolder = @".\Plugins\";
                var sharedFolder  = Path.Combine(pluginsFolder, "Shared");

                if (!Utils.IsProcessElevated)
                {
                    MessageBox.Show("Elevated administrator privileges not detected, you may run into issues! If you are running via Steam, please start Steam with elevated administrator privileges.", "Terraria",
                                    MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                if (Utils.IsFileLocked(new FileInfo("Shared.dll")))
                {
                    MessageBox.Show("You can only have a single instance of Terraria running at once. Please use Task Manager to close any extra Terraria processes and try again.", "Terraria",
                                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                    Environment.Exit(0);
                }
                if (!Directory.Exists(pluginsFolder))
                {
                    MessageBox.Show(@"Your Terraria\Plugins folder is missing.", "Terraria",
                                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                    Environment.Exit(0);
                }
                if (!Directory.Exists(sharedFolder))
                {
                    MessageBox.Show(@"Your Terraria\Plugins\Shared folder is missing.", "Terraria",
                                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                    Environment.Exit(0);
                }

                var references = AppDomain.CurrentDomain
                                 .GetAssemblies()
                                 .Where(a => !a.IsDynamic && !string.IsNullOrEmpty(a.Location))
                                 .Select(a => a.Location).ToList();
                var jsonBaseName = "Newtonsoft.Json.dll";
                if (!references.Any(s => s.Contains(jsonBaseName)))
                {
                    // Dynamic compilation requires assemblies to be stored on file, thus we must extract the Newtonsoft.Json.dll embedded resource to a temp file if we want to use it.
                    var assembly     = Assembly.GetEntryAssembly();
                    var error        = "Could not extract Newtonsoft.Json.dll from Terraria.";
                    var resourceName = assembly.GetManifestResourceNames().FirstOrDefault(s => s.Contains(jsonBaseName));
                    if (resourceName == null)
                    {
                        throw new Exception(error);
                    }

                    var newtonsoftFileName = Path.Combine(".", jsonBaseName);
                    if (!File.Exists(newtonsoftFileName))
                    {
                        using (var stream = assembly.GetManifestResourceStream(resourceName))
                        {
                            if (stream == null)
                            {
                                throw new Exception(error);
                            }

                            using (var fileStream = new FileStream(newtonsoftFileName, FileMode.Create))
                            {
                                stream.CopyTo(fileStream);
                            }
                        }
                    }

                    references.Add(newtonsoftFileName);
                }

                var sharedSource = Directory.EnumerateFiles(sharedFolder, "*.cs", SearchOption.AllDirectories).ToArray();
                if (sharedSource.Length > 0)
                {
                    references.Add(Generate("Shared.dll", references.ToArray(), sharedSource));
                }

                var referencesArray = references.ToArray();
                foreach (var filename in Directory.EnumerateFiles(pluginsFolder, "*.cs"))
                {
                    Load(Path.GetFileNameWithoutExtension(filename), referencesArray, filename);
                }

                foreach (var folder in Directory.EnumerateDirectories(pluginsFolder))
                {
                    var name = Path.GetFileName(folder);
                    if (name == "Shared")
                    {
                        continue;
                    }

                    Load(name, referencesArray, Directory.EnumerateFiles(folder, "*.cs", SearchOption.AllDirectories).ToArray());
                }

                // Load hotkey binds
                var result = IniAPI.GetIniKeys("HotkeyBinds").ToList();
                foreach (var keys in result)
                {
                    var val = IniAPI.ReadIni("HotkeyBinds", keys, null);
                    var key = ParseHotkey(keys);

                    if (string.IsNullOrEmpty(val) || !val.StartsWith("/") || key == null)
                    {
                        MessageBox.Show("Invalid record in [HotkeyBinds]: " + key + ".", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else
                    {
                        RegisterHotkey(val, key);
                    }
                }
            }
        }
コード例 #2
0
ファイル: Loader.cs プロジェクト: skulltraill/TerrariaPatcher
        private static void Load()
        {
            if (!loaded)
            {
                loaded = true;

                try
                {
                    var pluginsFolder = @".\Plugins\";
                    var sharedFolder  = Path.Combine(pluginsFolder, "Shared");

                    if (!Utils.IsProcessElevated)
                    {
                        MessageBox.Show("Elevated administrator privileges not detected, you may run into issues! If you are running via Steam, please start Steam with elevated administrator privileges.", "Terraria",
                                        MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    if (Utils.IsFileLocked(new FileInfo("Shared.dll")))
                    {
                        MessageBox.Show("You can only have a single instance of Terraria running at once. Please use Task Manager to close any extra Terraria processes and try again.", "Terraria",
                                        MessageBoxButtons.OK, MessageBoxIcon.Error);
                        Environment.Exit(0);
                    }
                    if (!Directory.Exists(pluginsFolder))
                    {
                        MessageBox.Show(@"Your Terraria\Plugins folder is missing.", "Terraria",
                                        MessageBoxButtons.OK, MessageBoxIcon.Error);
                        Environment.Exit(0);
                    }
                    if (!Directory.Exists(sharedFolder))
                    {
                        MessageBox.Show(@"Your Terraria\Plugins\Shared folder is missing.", "Terraria",
                                        MessageBoxButtons.OK, MessageBoxIcon.Error);
                        Environment.Exit(0);
                    }

                    var references = AppDomain.CurrentDomain
                                     .GetAssemblies()
                                     .Where(a => !a.IsDynamic && !string.IsNullOrEmpty(a.Location))
                                     .Select(a => a.Location).ToList();
                    ExtractAndReference(references, "Terraria.Libraries.JSON.NET.Newtonsoft.Json.dll");
                    ExtractAndReference(references, "Terraria.Libraries.ReLogic.ReLogic.dll", true);

                    Load(references.ToArray(), Directory.EnumerateFiles(pluginsFolder, "*.cs", SearchOption.AllDirectories).ToArray());

                    // Load hotkey binds
                    var result = IniAPI.GetIniKeys("HotkeyBinds").ToList();
                    foreach (var keys in result)
                    {
                        var val = IniAPI.ReadIni("HotkeyBinds", keys, null);
                        var key = ParseHotkey(keys);

                        if (string.IsNullOrEmpty(val) || !val.StartsWith("/") || key == null)
                        {
                            MessageBox.Show("Invalid record in [HotkeyBinds]: " + key + ".", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        }
                        else
                        {
                            RegisterHotkey(val, key);
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                    throw;
                }
            }
        }