Exemple #1
0
        static void Main(string[] args)
        {
            Stopwatch s = new Stopwatch();

            s.Start();
            using (FileStream fs = new FileStream(@"input/data.win", FileMode.Open))
            {
                GMDataReader reader = new GMDataReader(fs, fs.Name);
                foreach (GMWarning w in reader.Warnings)
                {
                    Console.WriteLine(string.Format("[WARN: {0}] {1}", w.Level, w.Message));
                }

                ProjectFile pf = new ProjectFile(reader.Data, Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "project"),
                                                 (ProjectFile.WarningType type, string info) =>
                {
                    Console.WriteLine($"Project warn: {type} {info ?? ""}");
                });

                bool first = !Directory.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "project"));
                if (first)
                {
                    pf.AddAllAssetsToJSON(pf.Paths, "paths");
                    pf.AddAllAssetsToJSON(pf.Sounds, "sounds");
                    pf.AddAllAssetsToJSON(pf.Objects, "objects");
                    pf.Save();
                }
                else
                {
                    pf.Load();
                    pf.PurgeUnmodifiedAssets(pf.Paths);
                    pf.PurgeUnmodifiedAssets(pf.Sounds);
                    pf.PurgeUnmodifiedAssets(pf.Objects);
                }

                Directory.CreateDirectory("output");
                using (FileStream fs2 = new FileStream("output/data.win", FileMode.Create))
                {
                    using (GMDataWriter writer = new GMDataWriter(reader.Data, fs2, fs2.Name, reader.Length))
                    {
                        if (!first)
                        {
                            pf.ConvertToData();
                        }
                        writer.Write();
                        writer.Flush();
                        foreach (GMWarning w in writer.Warnings)
                        {
                            Console.WriteLine(string.Format("[WARN: {0}] {1}", w.Level, w.Message));
                        }
                    }
                }
            }
            s.Stop();
            Console.WriteLine(string.Format("Took {0} ms, {1} seconds.", s.Elapsed.TotalMilliseconds, Math.Round(s.Elapsed.TotalMilliseconds / 1000, 2)));

            Console.ReadLine();
        }
        public static void Run(IConsole console, ProjectFile pf, ProjectConfig cfg, bool verbose)
        {
            try
            {
                pf.LoadAll();
            }
            catch (Exception e)
            {
                console.Error.WriteLine($"Failed to load project: {e}");
                return;
            }

            List <Command> commands = new List <Command>()
            {
                new Command(new[] { "exit", "quit" },
                            "Exits this shell.",
                            "[exit|quit]",
                            args =>
                {
                    // TODO prompt to save anything? maybe not?
                    return(Command.CommandResult.Quit);
                }),

                new Command(new[] { "reload" },
                            "Reloads the project as it currently is on disk.",
                            "reload <optional:data>",
                            args =>
                {
                    if (!ReloadProject(console, ref pf, cfg, verbose, (args.Length == 2 && args[1] == "data")))
                    {
                        return(Command.CommandResult.Quit);
                    }
                    return(Command.CommandResult.None);
                }),

                new Command(new[] { "add" },
                            "Adds an asset from game data to the project.",
                            "add <asset_type> <asset_names>",
                            args =>
                {
                    if (args.Length != 3)
                    {
                        return(Command.CommandResult.InvalidSyntax);
                    }
                    switch (args[1].ToLowerInvariant())
                    {
                    case "path":
                    case "paths":
                        AddAsset(console, args[2], pf.Paths, pf);
                        break;

                    case "sprite":
                    case "sprites":
                        AddAsset(console, args[2], pf.Sprites, pf);
                        break;

                    case "sound":
                    case "sounds":
                        AddAsset(console, args[2], pf.Sounds, pf);
                        break;

                    case "object":
                    case "objects":
                        AddAsset(console, args[2], pf.Objects, pf);
                        break;

                    case "background":
                    case "backgrounds":
                        AddAsset(console, args[2], pf.Backgrounds, pf);
                        break;

                    case "font":
                    case "fonts":
                        AddAsset(console, args[2], pf.Fonts, pf);
                        break;

                    case "room":
                    case "rooms":
                        AddAsset(console, args[2], pf.Rooms, pf);
                        break;

                    default:
                        return(Command.CommandResult.InvalidSyntax);
                    }
                    return(Command.CommandResult.None);
                }),

                new Command(new[] { "apply" },
                            "Applies the project to the input data file, resulting in output.",
                            "apply",
                            args =>
                {
                    try
                    {
                        pf.LoadAll();

                        using FileStream fs       = new FileStream(Path.Combine(cfg.OutputDirectory, pf.DataHandle.Filename), FileMode.Create);
                        using GMDataWriter writer = new GMDataWriter(pf.DataHandle, fs, fs.Name, pf.DataHandle.Length);

                        console.Output.WriteLine("Converting to data...");
                        pf.ConvertToData();
                        console.Output.WriteLine("Writing main data file...");
                        writer.Write();
                        writer.Flush();
                        foreach (GMWarning w in writer.Warnings)
                        {
                            console.Output.WriteLine($"[WARN: {w.Level}] {w.Message}"); // todo formatting
                        }
                    }
                    catch (Exception e)
                    {
                        console.Error.WriteLine($"Failed to apply project: {e}");
                    }

                    if (!ReloadProject(console, ref pf, cfg, verbose))
                    {
                        return(Command.CommandResult.Quit);
                    }

                    return(Command.CommandResult.None);
                })
            };
            int helpLength = commands.Max(c => c.Usage.Length) + 1;

            console.Error.WriteLine();
            console.Error.WriteLine("DogScepter project shell");

            bool running = true;

            while (running)
            {
                console.Output.Write("> ");
                string   command = console.Input.ReadLine();
                string[] args    = command.Split(' ', StringSplitOptions.RemoveEmptyEntries);

                if (args.Length >= 1)
                {
                    string name = args[0].ToLowerInvariant();

                    if (name == "help")
                    {
                        if (args.Length == 2)
                        {
                            string  helpName = args[1].ToLowerInvariant();
                            Command cmd      = commands.Find(c => c.Names.Contains(helpName));
                            if (cmd != null)
                            {
                                console.Output.WriteLine(cmd.Description);
                                console.Output.WriteLine(cmd.Usage);
                                console.Output.WriteLine();
                                continue;
                            }
                        }

                        foreach (var cmd in commands)
                        {
                            console.Output.WriteLine(cmd.Usage + new string(' ', helpLength - cmd.Usage.Length) +
                                                     " |  " + cmd.Description);
                        }
                    }
                    else
                    {
                        Command cmd = commands.Find(c => c.Names.Contains(name));
                        if (cmd == null)
                        {
                            console.Error.WriteLine($"Unknown command \"{args[0]}\"");
                        }
                        else
                        {
                            switch (cmd.Function(args))
                            {
                            case Command.CommandResult.InvalidSyntax:
                                console.Error.WriteLine("Invalid syntax; proper usage:");
                                console.Error.WriteLine("  " + cmd.Usage);
                                break;

                            case Command.CommandResult.Quit:
                                running = false;
                                break;
                            }
                        }
                    }
                }

                console.Output.WriteLine();
            }
        }
    }