Elaztek Developer Hub
Blamite Game Engine - Tool (Library)
A command-line utility that aids in the creation of Blamite Cache (.map) Files.
BitmapsCommand.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <Strings/components/utils/io/io.h>
4 #include <Strings/components/utils/string/string.h>
5 #include <Strings/components/utils/converters/converters.h>
6 #include <Strings/components/logger/logger.h>
7 
8 #include <HEKGuerilla/components/tags/importers/bitmap/bitmap.h>
9 #include <HEKGuerilla/components/tags/tags.h>
10 #include <HEKGuerilla/components/settings/config/config.h>
11 
12 #include "../ToolCommand.hpp"
13 #include "../console.h"
14 #include "components/utils/utils.h"
15 
16 #ifdef TOOL_LIB_EXPORTS
17 #define TOOL_LIB_API __declspec(dllexport)
18 #else
19 #define TOOL_LIB_API __declspec(dllimport)
20 #endif
21 
31 {
32 private:
33  std::vector<std::string> supported_extensions =
34  {
35  "apng",
36  "avif",
37  "bmp",
38  "cur",
39  "gif",
40  "ico",
41  "jpg",
42  "jpeg",
43  "jpeg2k",
44  "pcx",
45  "png",
46  "qoi",
47  "svg",
48  "tga",
49  "tif",
50  "tiff",
51  "wal",
52  "webp",
53  "xbm"
54  };
55 
56 public:
58  {
59  command = "bitmaps";
60  syntax = "bitmaps <directory> [recursive]";
61  description = "scans through a given directory and imports all images as bitmap tags. resulting tags will be placed in the specified output folder. "
62  "if recursive is set to true, then all subdirectories will be scanned and imported as well. this is disabled by default. "
63  "supported formats: " + BlamStrings::Utils::String::BuildFromList(supported_extensions, ", ");
64  }
65 
66  int execute(std::vector<std::string> args)
67  {
68  Guerilla::Tags::LoadPlugins();
69 
70  std::string project_root = BlamTool::Utils::GetProjectRoot();
71 
72  std::string import_base_dir = BlamStrings::Utils::IO::NormalizePath(project_root + "data/");
73  std::string output_base_dir = BlamStrings::Utils::IO::NormalizePath(project_root + "tags/");
74 
75  bool recursive_search = false;
76  std::string input_directory = "";
77  std::string output_directory = "";
78 
79  if (args.size() == 0)
80  {
81  BlamStrings::Logger::LogEvent("no input directory path was provided, cannot compile bitmaps", BlamLogLevel::Warning);
82  return -1;
83  }
84 
85  if (args.size() > 2)
86  {
87  BlamStrings::Logger::LogEvent("too many arguments specified - skipping compilation. check your input and try again. "
88  "for additional help, use 'tool.exe help bitmaps'.", BlamLogLevel::Warning);
89  return -1;
90  }
91 
92  // Prepare arguments
93  {
94  if (args.size() == 2)
95  {
96  if (BlamStrings::Converters::StringToBool(args[1]))
97  {
98  BlamStrings::Logger::LogEvent("enabling recursive bitmap import, if you have many subdirectories this may take a long time!");
99  recursive_search = true;
100  }
101  }
102 
103  input_directory = BlamStrings::Utils::IO::NormalizePath(import_base_dir + args[0]);
104  output_directory = BlamStrings::Utils::IO::NormalizePath(output_base_dir + args[0]);
105  }
106 
107  std::vector<std::string> file_list = std::vector<std::string>();
108 
109  if (recursive_search)
110  {
111  file_list = BlamStrings::Utils::IO::GetDeepFileList(input_directory);
112  }
113  else
114  {
115  file_list = BlamStrings::Utils::IO::GetFileList(input_directory);
116  }
117 
118  for (std::string file_path : file_list)
119  {
120  if (BlamStrings::Utils::String::EndsWithAny(file_path, supported_extensions, true))
121  {
122  std::string file_output_dir = output_directory;
123 
124  if (recursive_search)
125  {
126  file_output_dir = BlamStrings::Utils::String::Replace(file_path, input_directory, output_directory);
127  file_output_dir = BlamStrings::Utils::IO::GetContainingFolder(file_output_dir);
128  }
129 
130  if (!Guerilla::Tags::Importers::Bitmap::ImportBitmap(file_path, file_output_dir))
131  {
132  BlamStrings::Logger::LogEvent("failed to import bitmap '" + file_path + "'", WSV_WARNING);
133  }
134  else
135  {
136  BlamStrings::Logger::LogEvent("successfully imported bitmap '" + file_path + "' to directory '" + file_output_dir + "'");
137  }
138  }
139  }
140 
141  Guerilla::Tags::ReleasePlugins();
142 
143  BlamStrings::Logger::LogEvent("finished.");
144  return 0;
145  }
146 };
BitmapsCommand::execute
int execute(std::vector< std::string > args)
Executes the command.
Definition: BitmapsCommand.hpp:66
utils.h
ToolCommand::syntax
std::string syntax
The syntax of the command. This should include the command name, as well as any arguments.
Definition: ToolCommand.hpp:19
ToolCommand::command
std::string command
The name of the command.
Definition: ToolCommand.hpp:18
BitmapsCommand
Class for the Bitmaps command.
Definition: BitmapsCommand.hpp:30
BitmapsCommand::BitmapsCommand
BitmapsCommand()
Definition: BitmapsCommand.hpp:57
ToolCommand
Base class representing a Tool command.
Definition: ToolCommand.hpp:15
ToolCommand::description
std::string description
A description of the command.
Definition: ToolCommand.hpp:20
BlamTool::Utils::GetProjectRoot
TOOL_LIB_API std::string GetProjectRoot()
Retrieves the current project root.
Definition: utils.cpp:6