Пример #1
0
        /// <summary>
        /// Adds a Factorio instance to the manager.
        /// </summary>
        /// <param name="instance">The instance to add.</param>
        public ManagedFactorioInstance AddInstance(IFactorioInstance instance)
        {
            var managedInstance = instance.ToManaged();

            AddInstance(managedInstance);
            return(managedInstance);
        }
        public static bool IsExternal(this IFactorioInstance instance)
        {
            var instDir    = instance.Directory.Parent;
            var managedDir = Program.Locations.GetFactorioDir();

            return(!FileHelper.DirectoriesEqual(instDir, managedDir));
        }
Пример #3
0
 internal static ManagedFactorioInstance FromInstance(IFactorioInstance instance)
 {
     if (instance is ManagedFactorioInstance)
     {
         return((ManagedFactorioInstance)instance);
     }
     return(new ManagedFactorioInstance(instance));
 }
Пример #4
0
        /// <summary>
        /// Checkes if this instance is the Steam instance
        /// </summary>
        public static bool IsSteamInstance(this IFactorioInstance instance)
        {
            var actualInstance = instance;

            if (instance is ManagedFactorioInstance managedInstance)
            {
                actualInstance = managedInstance._baseInstance;
            }
            return(actualInstance is FactorioSteamInstance);
        }
 public static string GetName(this IFactorioInstance instance)
 {
     if (instance.IsSteamInstance())
     {
         // Steam instance has fixed name
         return("Steam");
     }
     else
     {
         string key = instance.GetUniqueKey();
         return(NameTable.GetValue(key, "Factorio"));
     }
 }
Пример #6
0
        public static string GetUniqueKey(this IFactorioInstance instance)
        {
            if (instance.IsExternal())
            {
                // We use the full path of the instance as unique key
                string result = instance.Directory.FullName.Trim().ToLower();

                // We have to sanitize the path to make sure it's a proper unique key
                result = result.Replace('/', '_');
                result = result.Replace('\\', '_');
                if (result.EndsWith("_"))
                {
                    result = result[0..^ 1];
 public static void SetName(this IFactorioInstance instance, string name)
 {
     if (instance.IsSteamInstance())
     {
         // Steam instance has fixed name
         return;
     }
     else
     {
         string key = instance.GetUniqueKey();
         NameTable[key] = name;
     }
 }
        internal static ManagedFactorioInstance CreateInternal(IFactorioInstance instance, ModManager modManager)
        {
            if (instance is null)
            {
                throw new ArgumentNullException(nameof(instance));
            }
            if (modManager is null)
            {
                throw new ArgumentNullException(nameof(modManager));
            }

            if (instance is ManagedFactorioInstance)
            {
                throw new InvalidOperationException("Already managed instances cannot be added");
            }

            return(new ManagedFactorioInstance(instance, modManager, true));
        }
Пример #9
0
        /// <summary>
        /// Starts this instance as a headless server
        /// </summary>
        /// <param name="startOptions">
        /// Server options
        /// </param>
        /// <param name="modDirectory">
        /// Optional<br/>The mod directory to be used<br/>
        /// Overrides the instances default mod directory</param>
        /// <param name="arguments">
        /// Optional command line arguments
        /// </param>
        /// <returns></returns>
        public static Process StartServer(this IFactorioInstance instance, FileInfo savegameFile, ServerStartOptions startOptions, DirectoryInfo modDirectory = null, params string[] arguments)
        {
            if (savegameFile is null)
            {
                throw new ArgumentNullException(nameof(savegameFile));
            }
            if (startOptions is null)
            {
                throw new ArgumentNullException(nameof(startOptions));
            }

            var fullArgs = startOptions.ToArgs(savegameFile);

            if (!(arguments is null) && (arguments.Length > 0))
            {
                fullArgs.AddRange(arguments);
            }

            return(instance.Start(modDirectory, null, fullArgs.ToArray()));
        }
        /// <summary>
        /// Creates a managed Factorio instance from an existing instance<br/>
        /// The created instance will not be associated with an instance manager<br/>
        /// If the instance is already managed it will be returned directly
        /// </summary>
        public static ManagedFactorioInstance FromInstance(IFactorioInstance instance, ModManager modManager)
        {
            if (instance is null)
            {
                throw new ArgumentNullException(nameof(instance));
            }
            if (modManager is null)
            {
                throw new ArgumentNullException(nameof(modManager));
            }

            if (instance is ManagedFactorioInstance managedInstance)
            {
                if (managedInstance.ModManager != modManager)
                {
                    throw new InvalidOperationException("Managed instance already has another associated mod manager");
                }
                return(managedInstance);
            }

            return(new ManagedFactorioInstance(instance, modManager, false));
        }
Пример #11
0
        /// <summary>
        /// Starts this instance
        /// </summary>
        /// <param name="modDirectory">
        /// Optional<br/>The mod directory to be used<br/>
        /// Overrides the instances default mod directory
        /// </param>
        /// <param name="savegameFile">
        /// Optional<br/>The savegame to load
        /// </param>
        /// <param name="arguments">
        /// Optional command line arguments
        /// </param>
        public static Process Start(this IFactorioInstance instance, DirectoryInfo modDirectory = null, FileInfo savegameFile = null, params string[] arguments)
        {
            var builder = new ArgumentBuilder();

            if (!(modDirectory is null))
            {
                builder.AppendArgument("--mod-directory");
                builder.AppendArgument(modDirectory.FullName);
            }

            if (!(savegameFile is null))
            {
                builder.AppendArgument("--load-game");
                builder.AppendArgument(savegameFile.FullName);
            }

            if (!(arguments is null))
            {
                builder.AppendArguments(arguments);
            }

            return(instance.Start(builder.ToString()));
        }
        public static string GetUniqueKey(this IFactorioInstance instance)
        {
            if (instance.IsExternal())
            {
                // We use the full path of the instance as unique key
                string result = instance.Directory.FullName.Trim().ToLower();

                // We have to sanitize the path to make sure it's a proper unique key
                result = result.Replace('/', '_');
                result = result.Replace('\\', '_');
                if (result.EndsWith("_"))
                {
                    result = result.Substring(0, result.Length - 1);
                }

                return(result);
            }
            else
            {
                // The directory name is already a unique key, no need to use the full path
                return(instance.Directory.Name);
            }
        }
Пример #13
0
        private static bool TryGetInstance(StartGameOptions options, out IFactorioInstance instance)
        {
            if (!string.IsNullOrEmpty(options.Uid))
            {
                foreach (var inst in Manager.ManagedInstances)
                {
                    if (inst.GetUniqueKey() == options.Uid)
                    {
                        instance = inst;
                        return(true);
                    }
                }

                Log.Error($"Factorio instance with ID {options.Uid} does not exist, aborting");
                instance = null;
                return(false);
            }

            if (!string.IsNullOrEmpty(options.Name))
            {
                foreach (var inst in Manager.ManagedInstances)
                {
                    if (inst.GetName() == options.Name)
                    {
                        instance = inst;
                        return(true);
                    }
                }

                Log.Error($"Factorio instance with name {options.Name} does not exist, aborting");
                instance = null;
                return(false);
            }

            instance = null;
            return(false);
        }
Пример #14
0
 private ManagedFactorioInstance(IFactorioInstance baseInstance)
 {
     _baseInstance = baseInstance;
 }
Пример #15
0
 /// <summary>
 /// Converts this Factorio instance into a managed instance.
 /// If the instance is already managed it will be returned unaltered.
 /// </summary>
 public static ManagedFactorioInstance ToManaged(this IFactorioInstance instance) => ManagedFactorioInstance.FromInstance(instance);
Пример #16
0
 public FactorioBaseMod(IFactorioInstance instance)
     : base(instance.BaseMod.Info.Name, instance.BaseMod.Info.DisplayName, instance.BaseMod.Info.Version, instance.BaseMod.Info.Version.ToMajor(),
            instance.BaseMod.Info.Author, instance.BaseMod.Info.Description, instance.BaseMod.Info.Dependencies, instance.BaseMod.Thumbnail)
 {
 }
Пример #17
0
 /// <summary>
 /// Starts this instance
 /// </summary>
 /// <param name="modDirectory">
 /// Optional<br/>The mod directory to be used<br/>
 /// Overrides the instances default mod directory
 /// </param>
 /// <param name="savegameFile">
 /// Optional<br/>The savegame to load
 /// </param>
 /// <param name="arguments">
 /// Optional command line arguments
 /// </param>
 public static Process Start(this IFactorioInstance instance, in DirectoryInfo?modDirectory = null, in FileInfo?savegameFile = null, params string[] arguments)
Пример #18
0
 /// <summary>
 /// Starts this instance
 /// </summary>
 /// <param name="arguments">Optional command line arguments</param>
 public static Process Start(this IFactorioInstance instance, params string[] arguments)
 => instance.Start(null, null, arguments);
 private ManagedFactorioInstance(IFactorioInstance baseInstance, ModManager modManager, bool hasManagerAttached)
 {
     _baseInstance      = baseInstance;
     ModManager         = modManager;
     HasManagerAttached = hasManagerAttached;
 }