net_solution.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*************************************************************************/
  2. /* net_solution.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "net_solution.h"
  31. #include "os/dir_access.h"
  32. #include "os/file_access.h"
  33. #include "../utils/path_utils.h"
  34. #include "../utils/string_utils.h"
  35. #include "csharp_project.h"
  36. #define SOLUTION_TEMPLATE \
  37. "Microsoft Visual Studio Solution File, Format Version 12.00\n" \
  38. "# Visual Studio 2012\n" \
  39. "%0\n" \
  40. "Global\n" \
  41. "\tGlobalSection(SolutionConfigurationPlatforms) = preSolution\n" \
  42. "%1\n" \
  43. "\tEndGlobalSection\n" \
  44. "\tGlobalSection(ProjectConfigurationPlatforms) = postSolution\n" \
  45. "%2\n" \
  46. "\tEndGlobalSection\n" \
  47. "EndGlobal\n"
  48. #define PROJECT_DECLARATION "Project(\"{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}\") = \"%0\", \"%1\", \"{%2}\"\nEndProject"
  49. #define SOLUTION_PLATFORMS_CONFIG "\t\%0|Any CPU = %0|Any CPU"
  50. #define PROJECT_PLATFORMS_CONFIG \
  51. "\t\t{%0}.%1|Any CPU.ActiveCfg = %1|Any CPU\n" \
  52. "\t\t{%0}.%1|Any CPU.Build.0 = %1|Any CPU"
  53. void NETSolution::add_new_project(const String &p_name, const String &p_guid, const Vector<String> &p_extra_configs) {
  54. if (projects.has(p_name))
  55. WARN_PRINT("Overriding existing project.");
  56. ProjectInfo procinfo;
  57. procinfo.guid = p_guid;
  58. procinfo.configs.push_back("Debug");
  59. procinfo.configs.push_back("Release");
  60. for (int i = 0; i < p_extra_configs.size(); i++) {
  61. procinfo.configs.push_back(p_extra_configs[i]);
  62. }
  63. projects[p_name] = procinfo;
  64. }
  65. Error NETSolution::save() {
  66. bool dir_exists = DirAccess::exists(path);
  67. ERR_EXPLAIN("The directory does not exist.");
  68. ERR_FAIL_COND_V(!dir_exists, ERR_FILE_BAD_PATH);
  69. String projs_decl;
  70. String sln_platform_cfg;
  71. String proj_platform_cfg;
  72. for (Map<String, ProjectInfo>::Element *E = projects.front(); E; E = E->next()) {
  73. const String &name = E->key();
  74. const ProjectInfo &procinfo = E->value();
  75. projs_decl += sformat(PROJECT_DECLARATION, name, name + ".csproj", procinfo.guid);
  76. for (int i = 0; i < procinfo.configs.size(); i++) {
  77. const String &config = procinfo.configs[i];
  78. if (i != 0) {
  79. sln_platform_cfg += "\n";
  80. proj_platform_cfg += "\n";
  81. }
  82. sln_platform_cfg += sformat(SOLUTION_PLATFORMS_CONFIG, config);
  83. proj_platform_cfg += sformat(PROJECT_PLATFORMS_CONFIG, procinfo.guid, config);
  84. }
  85. }
  86. String content = sformat(SOLUTION_TEMPLATE, projs_decl, sln_platform_cfg, proj_platform_cfg);
  87. FileAccessRef file = FileAccess::open(path_join(path, name + ".sln"), FileAccess::WRITE);
  88. ERR_FAIL_COND_V(!file, ERR_FILE_CANT_WRITE);
  89. file->store_string(content);
  90. file->close();
  91. return OK;
  92. }
  93. bool NETSolution::set_path(const String &p_existing_path) {
  94. if (p_existing_path.is_abs_path()) {
  95. path = p_existing_path;
  96. } else {
  97. String abspath;
  98. if (!rel_path_to_abs(p_existing_path, abspath))
  99. return false;
  100. path = abspath;
  101. }
  102. return true;
  103. }
  104. NETSolution::NETSolution(const String &p_name) {
  105. name = p_name;
  106. }