Exemple #1
0
        static void Main(string[] args)
        {
            Log.AddChannel("perf", null);
            Log.AddChannel("debug", null);

            Game.InitializeSettings(Arguments.Empty);

            if (args.Length == 0)
            {
                PrintUsage(new InstalledMods(null), null);
                return;
            }

            var    modId         = args[0];
            string customModPath = null;

            if (File.Exists(modId) || Directory.Exists(modId))
            {
                customModPath = modId;
                modId         = Path.GetFileNameWithoutExtension(modId);
            }

            var mods = new InstalledMods(customModPath);

            if (!mods.Keys.Contains(modId))
            {
                PrintUsage(mods, null);
                return;
            }

            var modData = new ModData(mods[modId], mods);
            var utility = new Utility(modData, mods);

            args = args.Skip(1).ToArray();
            var actions = new UtilityActions();

            foreach (var commandType in modData.ObjectCreator.GetTypesImplementing <IUtilityCommand>())
            {
                var command = (IUtilityCommand)Activator.CreateInstance(commandType);
                var kvp     = new KeyValuePair <Action <Utility, string[]>, Func <string[], bool> >(command.Run, command.ValidateArguments);
                actions.Add(command.Name, kvp);
            }

            if (args.Length == 0)
            {
                PrintUsage(mods, actions);
                return;
            }

            try
            {
                var command = args[0];
                if (!actions.ContainsKey(command))
                {
                    throw new NoSuchCommandException(command);
                }

                var action             = actions[command].Key;
                var validateActionArgs = actions[command].Value;

                if (validateActionArgs.Invoke(args))
                {
                    action.Invoke(utility, args);
                }
                else
                {
                    Console.WriteLine("Invalid arguments for '{0}'", command);
                    GetActionUsage(command, action);
                }
            }
            catch (Exception e)
            {
                Log.AddChannel("utility", "utility.log");
                Log.Write("utility", "Received args: {0}", args.JoinWith(" "));
                Log.Write("utility", "{0}", e);

                if (e is NoSuchCommandException)
                {
                    Console.WriteLine(e.Message);
                }
                else
                {
                    Console.WriteLine("Error: Utility application crashed. See utility.log for details");
                    throw;
                }
            }
        }
        static void Main(string[] args)
        {
            var engineDir = Environment.GetEnvironmentVariable("ENGINE_DIR");

            if (!string.IsNullOrEmpty(engineDir))
            {
                Platform.OverrideEngineDir(engineDir);
            }

            Log.AddChannel("perf", null);
            Log.AddChannel("debug", null);

            Game.InitializeSettings(Arguments.Empty);

            var envModSearchPaths = Environment.GetEnvironmentVariable("MOD_SEARCH_PATHS");
            var modSearchPaths    = !string.IsNullOrWhiteSpace(envModSearchPaths) ?
                                    FieldLoader.GetValue <string[]>("MOD_SEARCH_PATHS", envModSearchPaths) :
                                    new[] { Path.Combine(Platform.EngineDir, "mods") };

            if (args.Length == 0)
            {
                PrintUsage(new InstalledMods(modSearchPaths, new string[0]), null);
                return;
            }

            var modId            = args[0];
            var explicitModPaths = new string[0];

            if (File.Exists(modId) || Directory.Exists(modId))
            {
                explicitModPaths = new[] { modId };
                modId            = Path.GetFileNameWithoutExtension(modId);
            }

            var mods = new InstalledMods(modSearchPaths, explicitModPaths);

            if (!mods.Keys.Contains(modId))
            {
                PrintUsage(mods, null);
                return;
            }

            var modData = new ModData(mods[modId], mods);
            var utility = new Utility(modData, mods);

            args = args.Skip(1).ToArray();
            var actions = new UtilityActions();

            foreach (var commandType in modData.ObjectCreator.GetTypesImplementing <IUtilityCommand>())
            {
                var command = (IUtilityCommand)Activator.CreateInstance(commandType);
                var kvp     = new KeyValuePair <Action <Utility, string[]>, Func <string[], bool> >(command.Run, command.ValidateArguments);
                actions.Add(command.Name, kvp);
            }

            if (args.Length == 0)
            {
                PrintUsage(mods, actions);
                return;
            }

            try
            {
                var command = args[0];
                if (!actions.ContainsKey(command))
                {
                    throw new NoSuchCommandException(command);
                }

                var action             = actions[command].Key;
                var validateActionArgs = actions[command].Value;

                if (validateActionArgs.Invoke(args))
                {
                    action.Invoke(utility, args);
                }
                else
                {
                    Console.WriteLine("Invalid arguments for '{0}'", command);
                    GetActionUsage(command, action);
                }
            }
            catch (Exception e)
            {
                Log.AddChannel("utility", "utility.log");
                Log.Write("utility", "Received args: {0}", args.JoinWith(" "));
                Log.Write("utility", "{0}", e);

                if (e is NoSuchCommandException)
                {
                    Console.WriteLine(e.Message);
                }
                else
                {
                    Console.WriteLine("Error: Utility application crashed. See utility.log for details");
                    throw;
                }
            }
        }