ScriptRegistrar.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. ** Command & Conquer Renegade(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. /******************************************************************************
  19. *
  20. * FILE
  21. *
  22. * DESCRIPTION
  23. *
  24. * PROGRAMMER
  25. * Denzil E. Long, Jr.
  26. *
  27. * VERSION INFO
  28. * $Author: Byon_g $
  29. * $Revision: 5 $
  30. * $Modtime: 10/30/00 6:49p $
  31. * $Archive: /Commando/Code/Scripts/ScriptRegistrar.cpp $
  32. *
  33. ******************************************************************************/
  34. #include "always.h"
  35. #include "scriptregistrar.h"
  36. #include "scriptfactory.h"
  37. #include "dprint.h"
  38. #include <string.h>
  39. #include <assert.h>
  40. // ScriptFactory list
  41. ScriptFactory* ScriptRegistrar::mScriptFactories = NULL;
  42. /******************************************************************************
  43. *
  44. * NAME
  45. * ScriptRegistrar::RegisterScript
  46. *
  47. * DESCRIPTION
  48. * Register a Script factory
  49. *
  50. * INPUTS
  51. * ScriptFactory* factory
  52. *
  53. * RESULTS
  54. * NONE
  55. *
  56. ******************************************************************************/
  57. void ScriptRegistrar::RegisterScript(ScriptFactory* factory)
  58. {
  59. if (factory != NULL) {
  60. // DebugPrint("Registering Script '%s'\n", factory->GetName());
  61. factory->SetNext(mScriptFactories);
  62. mScriptFactories = factory;
  63. }
  64. }
  65. /******************************************************************************
  66. *
  67. * NAME
  68. * ScriptRegistrar::UnregisterScript
  69. *
  70. * DESCRIPTION
  71. * Remove a Script factory from the registery
  72. *
  73. * INPUTS
  74. * Factory - ScriptFactory to remove.
  75. *
  76. * RESULTS
  77. * NONE
  78. *
  79. ******************************************************************************/
  80. void ScriptRegistrar::UnregisterScript(ScriptFactory* factory)
  81. {
  82. ScriptFactory* previous = NULL;
  83. ScriptFactory* current = mScriptFactories;
  84. while (current != NULL) {
  85. ScriptFactory* next = current->GetNext();
  86. if (current == factory) {
  87. // Handle head of list condition
  88. if (previous == NULL) {
  89. mScriptFactories = next;
  90. } else {
  91. previous->SetNext(next);
  92. }
  93. // DebugPrint("Unregistered script '%s'\n", factory->GetName());
  94. }
  95. // Advance to next node
  96. previous = current;
  97. current = next;
  98. }
  99. }
  100. /******************************************************************************
  101. *
  102. * NAME
  103. * ScriptRegistrar::CreateScript
  104. *
  105. * DESCRIPTION
  106. * Create an instance of the specified script.
  107. *
  108. * INPUTS
  109. * ScriptName - Name of script to create.
  110. *
  111. * RESULTS
  112. * ScriptClass - New script instance.
  113. *
  114. ******************************************************************************/
  115. ScriptImpClass* ScriptRegistrar::CreateScript(const char* scriptName)
  116. {
  117. assert(scriptName != NULL);
  118. if (scriptName != NULL) {
  119. ScriptFactory* factory = mScriptFactories;
  120. while (factory != NULL) {
  121. if (stricmp(factory->GetName(), scriptName) == 0) {
  122. // DebugPrint("Creating Script '%s'\n", factory->GetName());
  123. return factory->Create();
  124. }
  125. factory = factory->GetNext();
  126. }
  127. }
  128. DebugPrint("Failed to find script '%s'\n", scriptName);
  129. return NULL;
  130. }
  131. /******************************************************************************
  132. *
  133. * NAME
  134. * ScriptRegistrar::GetScriptFactory
  135. *
  136. * DESCRIPTION
  137. * Get script factory by name.
  138. *
  139. * INPUTS
  140. * Name - Name of script
  141. *
  142. * RESULTS
  143. * ScriptFactory - Factory for specified script.
  144. *
  145. ******************************************************************************/
  146. ScriptFactory* ScriptRegistrar::GetScriptFactory(const char* name)
  147. {
  148. assert(name != NULL);
  149. if (name != NULL) {
  150. ScriptFactory* factory = mScriptFactories;
  151. while (factory != NULL) {
  152. if (stricmp(factory->GetName(), name) == 0) {
  153. return factory;
  154. }
  155. factory = factory->GetNext();
  156. }
  157. }
  158. return NULL;
  159. }
  160. /******************************************************************************
  161. *
  162. * NAME
  163. * ScriptRegistrar::GetScriptFactory
  164. *
  165. * DESCRIPTION
  166. * Get script factory at specified location
  167. *
  168. * INPUTS
  169. * Index - Index of script factory.
  170. *
  171. * RESULTS
  172. * ScriptFactory - Factory for specified script.
  173. *
  174. ******************************************************************************/
  175. ScriptFactory* ScriptRegistrar::GetScriptFactory(int index)
  176. {
  177. int count = 0;
  178. ScriptFactory* factory = mScriptFactories;
  179. while (factory != NULL) {
  180. if (count == index) {
  181. return factory;
  182. }
  183. count++;
  184. factory = factory->GetNext();
  185. }
  186. return NULL;
  187. }
  188. /******************************************************************************
  189. *
  190. * NAME
  191. * ScriptRegistrar::Count
  192. *
  193. * DESCRIPTION
  194. * Retrieve the number of registered scripts.
  195. *
  196. * INPUTS
  197. * NONE
  198. *
  199. * RESULTS
  200. * Count - Number of registered scripts
  201. *
  202. ******************************************************************************/
  203. int ScriptRegistrar::Count(void)
  204. {
  205. int count = 0;
  206. ScriptFactory* factory = mScriptFactories;
  207. while (factory != NULL) {
  208. count++;
  209. factory = factory->GetNext();
  210. }
  211. return count;
  212. }