Elaztek Developer Hub
Blamite Game Engine - Tool (Library)
A command-line utility that aids in the creation of Blamite Cache (.map) Files.
GenerateScriptProjectCommand.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <Strings/components/logger/logger.h>
4 #include <Strings/components/utils/io/io.h>
5 #include <Strings/components/utils/string/string.h>
6 #include <Strings/components/utils/list/list.h>
7 #include <Strings/components/3rdparty/rapidxml/rapidxml.hpp>
8 #include <HEKGuerilla/components/projects/projects.h>
9 
10 #include "../ToolCommand.hpp"
11 #include "../console.h"
12 #include "components/utils/utils.h"
13 
14 #ifdef TOOL_LIB_EXPORTS
15 #define TOOL_LIB_API __declspec(dllexport)
16 #else
17 #define TOOL_LIB_API __declspec(dllimport)
18 #endif
19 
26 {
27 private:
28  std::string include_path_placeholders[4] = {
29  "{$__INCLUDE_DIRS_DW32__}",
30  "{$__INCLUDE_DIRS_DW64__}",
31  "{$__INCLUDE_DIRS_RW32__}",
32  "{$__INCLUDE_DIRS_RW64__}"
33  };
34 
35  std::string lib_path_placeholders[4] = {
36  "{$__LIBRARY_DIRS_DW32__}",
37  "{$__LIBRARY_DIRS_DW64__}",
38  "{$__LIBRARY_DIRS_RW32__}",
39  "{$__LIBRARY_DIRS_RW64__}"
40  };
41 
42  std::string engine_lib_dir_suffixes[4] = {
43  "tag debug/Win32",
44  "tag debug/x64",
45  "cache release/Win32",
46  "cache release/x64"
47  };
48 
49  std::string default_deps = "blam.lib;Strings.lib";
50  std::string default_deps_placeholder = "{$__DEPS__}";
51 
52  std::string hek_root_placeholder = "{$__HEK_ROOT__}";
53 
54  bool verbose = false;
55  bool overwrite = false;
56 
57  std::vector<std::string> PreprocessArgs(std::vector<std::string> args)
58  {
59  std::vector<std::string> processed_args = std::vector<std::string>();
60 
61  for (int i = 0; i < args.size(); i++)
62  {
63  if (args[i] == "-verbose")
64  {
65  verbose = true;
66  }
67  else if (args[i] == "-overwrite")
68  {
69  overwrite = true;
70  }
71  else
72  {
73  processed_args.push_back(args[i]);
74  }
75  }
76 
77  return processed_args;
78  }
79 
80 public:
82  {
83  command = "generate-script-project";
84  syntax = "generate-script-project [-verbose] [-overwrite]";
85  description = "generates a preconfigured visual studio project for the current project";
86  }
87 
88  int execute(std::vector<std::string> args)
89  {
90  args = PreprocessArgs(args);
91 
92  std::string project_root = BlamTool::Utils::GetProjectRoot();
93  std::string project_filename = BlamStrings::Utils::IO::GetFileNameWithoutExtension(project_root) + ".blam";
94 
95  if (args.size() > 0)
96  {
97  project_filename = args[0];
98  }
99 
100  BlamProject* project_info = Guerilla::Projects::LoadProject(project_root + project_filename);
101 
102  if (!project_info->IsLoaded())
103  {
104  BlamStrings::Logger::LogEvent("failed to create stub project: could not load project info (tried to "
105  "read from file path: " + project_root + project_filename + ")", WSV_ERROR);
106 
107  if (args.size() == 0)
108  {
109  BlamStrings::Logger::LogEvent("project path was assumed from directory path - if your .blam file "
110  "has a different name, use " + syntax + " to specify the filename", WSV_WARNING);
111  }
112 
113  delete project_info;
114  return -1;
115  }
116 
117  std::string script_project_base_dir = "./content/blam/default_script_project/";
118 
119  // Copy .sln file
120  {
121  std::string source_sln_path = script_project_base_dir + "BLAM_GAME_TITLE.sln";
122  std::string destination_sln_path = project_root + project_info->project_name + ".sln";
123 
124  if (!CopyTextFile(source_sln_path, destination_sln_path, "visual studio solution"))
125  {
126  delete project_info;
127  return -1;
128  }
129  }
130 
131  // Copy .vcxproj.user file
132  {
133  std::string source_path = script_project_base_dir + "BLAM_GAME_TITLE.vcxproj.user";
134  std::string destination_path = project_root + project_info->project_name + ".vcxproj.user";
135 
136  if (!CopyTextFile(source_path, destination_path, ".vcxproj.user"))
137  {
138  delete project_info;
139  return -1;
140  }
141  }
142 
143  std::string destination_vcxproj_path = project_root + project_info->project_name + ".vcxproj";
144  std::string hek_root = BlamStrings::Utils::IO::GetApplicationDir();
145 
146  if (!BlamStrings::Utils::IO::FileExists(destination_vcxproj_path) || overwrite)
147  {
148  std::string vcxproj_file_contents = BlamStrings::Utils::IO::GetFileContentsAsString(script_project_base_dir + "BLAM_GAME_TITLE.vcxproj");
149 
150  for (int i = 0; i < 4; i++)
151  {
152  std::string engine_dep_path = project_info->GetEngineDependenciesPath() + "/" + engine_lib_dir_suffixes[i] + "/";
153  std::string engine_include_path = project_info->GetEngineDependenciesPath() + "/include/";
154 
155  vcxproj_file_contents = str_replace(vcxproj_file_contents, include_path_placeholders[i], engine_include_path);
156  vcxproj_file_contents = str_replace(vcxproj_file_contents, lib_path_placeholders[i], engine_dep_path + "/lib");
157 
158  vcxproj_file_contents = str_replace(vcxproj_file_contents, default_deps_placeholder, default_deps);
159  vcxproj_file_contents = str_replace(vcxproj_file_contents, hek_root_placeholder, hek_root);
160  }
161 
162  if (!BlamStrings::Utils::IO::CreateNewFile(destination_vcxproj_path, vcxproj_file_contents))
163  {
164  BlamStrings::Logger::LogEvent("failed to create new vcxproj: CreateNewFile returned false", WSV_ERROR);
165  return -1;
166  }
167  }
168  else
169  {
170  BlamStrings::Logger::LogEvent("not copying default vcxproj: file already exists", WSV_WARNING);
171  BlamStrings::Logger::LogEvent("hint: use -overwrite argument to overwrite your current .vcxproj if "
172  "desired, but this will cause data loss!", WSV_WARNING);
173  }
174 
175  BlamStrings::Logger::LogEvent("script project created");
176 
177  delete project_info;
178  return 0;
179  }
180 
181  bool CopyTextFile(std::string source, std::string destination, std::string debug_file_title)
182  {
183  if (!BlamStrings::Utils::IO::FileExists(destination) || overwrite)
184  {
185  std::string contents = BlamStrings::Utils::IO::GetFileContentsAsString(source);
186 
187  if (!BlamStrings::Utils::IO::CreateNewFile(destination, contents))
188  {
189  BlamStrings::Logger::LogEvent("failed to create new " + debug_file_title + ": CreateNewFile returned false", WSV_ERROR);
190  return false;
191  }
192  }
193  else
194  {
195  BlamStrings::Logger::LogEvent("not copying default " + debug_file_title + ": file already exists", WSV_WARNING);
196  BlamStrings::Logger::LogEvent("hint: use -overwrite argument to overwrite the existing file if "
197  "desired, but this will cause data loss!", WSV_WARNING);
198  }
199 
200  return true;
201  }
202 };
GenerateScriptProjectCommand::CopyTextFile
bool CopyTextFile(std::string source, std::string destination, std::string debug_file_title)
Definition: GenerateScriptProjectCommand.hpp:181
utils.h
GenerateScriptProjectCommand::execute
int execute(std::vector< std::string > args)
Executes the command.
Definition: GenerateScriptProjectCommand.hpp:88
GenerateScriptProjectCommand::GenerateScriptProjectCommand
GenerateScriptProjectCommand()
Definition: GenerateScriptProjectCommand.hpp:81
GenerateScriptProjectCommand
Class for the generate-script-project command.
Definition: GenerateScriptProjectCommand.hpp:25
ToolCommand
Base class representing a Tool command.
Definition: ToolCommand.hpp:15
BlamTool::Utils::GetProjectRoot
TOOL_LIB_API std::string GetProjectRoot()
Retrieves the current project root.
Definition: utils.cpp:6
TOOL_LIB_API
#define TOOL_LIB_API
Definition: GenerateScriptProjectCommand.hpp:17