versionUpdate.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. ** Command & Conquer Generals(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. // FILE: versionUpdate.cpp //////////////////////////////////////////////////////
  19. // Generals version number class updater
  20. // Author: Matthew D. Campbell, November 2001
  21. // SYSTEM INCLUDES ////////////////////////////////////////////////////////////
  22. #define WIN32_LEAN_AND_MEAN // only bare bones windows stuff wanted
  23. #include <windows.h>
  24. #include <lmcons.h>
  25. #include <stdlib.h>
  26. #include <stdio.h>
  27. #include <string.h>
  28. // Local defines
  29. #define VERSION_BUILDNUM "VERSION_LOCALBUILDNUM"
  30. #define VERSION_STRING "LOCAL_BUILD_STRING"
  31. #define VERSION_BUILDUSER "VERSION_BUILDUSER"
  32. #define VERSION_BUILDLOC "VERSION_BUILDLOC"
  33. #define FORMAT "#define " VERSION_STRING " \"%d\"\n"
  34. #define COMMENTS "// Do not modify this file by hand. Auto-created and\n// Updated by versionUpdate.\n"
  35. #define NUMFMT "#define %s %d\n"
  36. #define STRFMT "#define %s \"%s\"\n"
  37. static void writeVersion(char *file, int build)
  38. {
  39. FILE *filePtr = fopen(file, "w");
  40. // Clobber the file. Hey, this is a simple program.
  41. if (file)
  42. {
  43. if (filePtr)
  44. {
  45. unsigned long bufSize = UNLEN + 1;
  46. char userName[UNLEN + 1];
  47. if (!GetUserName(userName, &bufSize))
  48. {
  49. strcpy(userName, "unknown");
  50. }
  51. bufSize = MAX_COMPUTERNAME_LENGTH + 1;
  52. char computerName[MAX_COMPUTERNAME_LENGTH + 1];
  53. if (!GetComputerName(computerName, &bufSize))
  54. {
  55. strcpy(computerName, "unknown");
  56. }
  57. printf("Build is by %s at %s\n", userName, computerName);
  58. fprintf(filePtr, COMMENTS);
  59. fprintf(filePtr, FORMAT, build);
  60. fprintf(filePtr, NUMFMT, VERSION_BUILDNUM, build);
  61. fprintf(filePtr, STRFMT, VERSION_BUILDUSER, userName);
  62. fprintf(filePtr, STRFMT, VERSION_BUILDLOC, computerName);
  63. fclose(filePtr);
  64. }
  65. else
  66. {
  67. printf("Cannot write file\n");
  68. }
  69. }
  70. else
  71. {
  72. printf("No file to write\n");
  73. }
  74. }
  75. static void usage(char *progname)
  76. {
  77. if (progname)
  78. {
  79. printf ("Usage: %s versionfile.h", progname);
  80. }
  81. }
  82. // strtrim ====================================================================
  83. /** Trim leading and trailing whitespace from a character string (in place). */
  84. //=============================================================================
  85. static char* strtrim(char* buffer)
  86. {
  87. if (buffer != NULL) {
  88. // Strip leading white space from the string.
  89. char * source = buffer;
  90. while ((*source != 0) && ((unsigned char)*source <= 32))
  91. {
  92. source++;
  93. }
  94. if (source != buffer)
  95. {
  96. strcpy(buffer, source);
  97. }
  98. // Clip trailing white space from the string.
  99. for (int index = strlen(buffer)-1; index >= 0; index--)
  100. {
  101. if ((*source != 0) && ((unsigned char)buffer[index] <= 32))
  102. {
  103. buffer[index] = '\0';
  104. }
  105. else
  106. {
  107. break;
  108. }
  109. }
  110. }
  111. return buffer;
  112. }
  113. int APIENTRY WinMain(HINSTANCE hInstance,
  114. HINSTANCE hPrevInstance,
  115. LPSTR lpCmdLine,
  116. int nCmdShow)
  117. {
  118. /*
  119. ** Convert WinMain arguments to simple main argc and argv
  120. */
  121. int argc = 1;
  122. char * argv[20];
  123. argv[0] = NULL;
  124. char * token = strtok(lpCmdLine, " ");
  125. while (argc < 20 && token != NULL)
  126. {
  127. argv[argc++] = strtrim(token);
  128. token = strtok(NULL, " ");
  129. }
  130. int build = 0;
  131. if (argc != 2)
  132. {
  133. usage(argv[0]);
  134. }
  135. else
  136. {
  137. char *target = argv[argc-1];
  138. FILE *filePtr;
  139. if (target) {
  140. filePtr = fopen(target, "r+");
  141. if (filePtr)
  142. {
  143. char buffer[256];
  144. char *stringPtr = NULL;
  145. while (!feof(filePtr))
  146. {
  147. fread(buffer, 256, 1, filePtr);
  148. if ((stringPtr = strstr(buffer, VERSION_STRING)) != NULL)
  149. {
  150. char *ptr;
  151. // Looking for '#define VERSION "x.y.z"'
  152. ptr = strtok(stringPtr, " "); // The VERSION
  153. ptr = strtok(NULL, "\n"); // The remainder
  154. if (*ptr == '\"')
  155. {
  156. ptr++; // Inc past the first "
  157. build = atoi(ptr);
  158. fclose(filePtr);
  159. build++;
  160. printf ("Local build is %d\n", build);
  161. writeVersion(target, build);
  162. break;
  163. } else
  164. {
  165. printf ("Local build is 0. Oops, didn't find a string of the format: '#define VERSION \"x.y.z\"'");
  166. }
  167. } // End if if (strstr
  168. } // End of while
  169. } // End of if filePtr
  170. else
  171. {
  172. // Didn't find the file, write a new one
  173. printf ("Local build is %d\n", build);
  174. writeVersion(target, build);
  175. }
  176. }
  177. }
  178. return 0;
  179. }