Cavestory Mod API
ModTextScriptCommand.h
Go to the documentation of this file.
1 /*
2  Cavestory Multiplayer API
3  Copyright (C) 2021 Johnny Ledger
4 
5  This software is provided 'as-is', without any express or implied
6  warranty. In no event will the authors be held liable for any damages
7  arising from the use of this software.
8 
9  Permission is granted to anyone to use this software for any purpose,
10  including commercial applications, and to alter it and redistribute it
11  freely, subject to the following restrictions:
12 
13  1. The origin of this software must not be misrepresented; you must not
14  claim that you wrote the original software. If you use this software
15  in a product, an acknowledgment in the product documentation would be
16  appreciated but is not required.
17  2. Altered source versions must be plainly marked as such, and must not be
18  misrepresented as being the original software.
19  3. This notice may not be removed or altered from any source distribution.
20 */
21 
26 #include <CavestoryModAPI.h>
27 
28 #pragma once
29 
33 
39 #define TSC_NAME_TO_HEX(A,B,C) (int(A) | (int(B) << 8) | (int(C) << 16))
40 
43 {
44 public: // Typedefs
45 
53  typedef void(*TSCRunFunc)(int& x, int& y, int& z, int& w, bool& bExit, int& iError);
54 
55 public: // Linking
56 
59  static ModTextScriptCommand* first;
60 
64 
65 protected: // Variables
66 
70 
71 public: // Human information
72 
75  char mCommand[4];
76 
79  char* mShortDesc;
80 
83  char* mDesc;
84 
87  char* mArguments;
88 
89 public: // Constructors
90 
94 
97  ModTextScriptCommand(TSCRunFunc pCommand, char pFirst, char pSecond, char pThird, const char* pShortDesc, const char* pDesc, const char* pArgs);
98 
102 
103 public: // Public, static methods
104 
107  static void Init();
108 };
109 
110 //---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
111 
112 inline ModTextScriptCommand::ModTextScriptCommand(TSCRunFunc pCommand, char pFirst, char pSecond, char pThird, const char* pShortDesc, const char* pDesc, const char* pArgs)
113 {
114  mCommand[0] = pFirst;
115  mCommand[1] = pSecond;
116  mCommand[2] = pThird;
117  mCommand[3] = 0;
118  mShortDesc = strdup(pShortDesc);
119  mDesc = strdup(pDesc);
120  mArguments = strdup(pArgs);
121  mExecute = pCommand;
122 
123  // Link us up
124  next = first;
125  first = this;
126 }
127 
129 {
130  if (mShortDesc)
131  free(mShortDesc);
132 
133  if (mDesc)
134  free(mDesc);
135 
136  if (mArguments)
137  free(mArguments);
138 }
139 
140 //----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
141 
143 {
144  // Populate the lists
145  for (ModTextScriptCommand* walk = ModTextScriptCommand::first; walk; walk = walk->next)
146  {
147  if (CSM_RegisterTextScriptCommand(walk->mCommand, walk->mShortDesc, walk->mDesc, walk->mArguments, walk->mExecute) == FALSE)
148  {
149  CSM_LogError("Mod/TextScript", "Failed to register TextScript command <%.3s!", walk->mCommand);
150  // Used to do it like this so the error message would still be printed to console in the event that trying to fetch the command's name crashed the game
151  //CSM_Log("Mod/TextScript", "Failed to register TextScript command <");
152  //CSM_Log("%.3s!\r\n", walk->mCommand);
153  }
154  }
155 }
156 
157 //---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
158 
159 #ifdef _TEXT_SCRIPT_FILE_
168 #define DEFINE_TEXTSCRIPT_COMMAND(NAMEA, NAMEB, NAMEC, SHORTHAND_DESC, DESC, ARGS) \
169  extern void __declspec(dllexport) __##NAMEA##NAMEB##NAMEC##__RUNFUNC__(int& x, int& y, int& z, int& w, bool& bExit, int& iError);\
170  static ModTextScriptCommand __CMD_##NAMEA##NAMEB##NAMEC##(&__##NAMEA##NAMEB##NAMEC##__RUNFUNC__, #@NAMEA, #@NAMEB, #@NAMEC, SHORTHAND_DESC, DESC, ARGS);\
171  void __##NAMEA##NAMEB##NAMEC##__RUNFUNC__(int& x, int& y, int& z, int& w, bool& bExit, int& iError)
172 
173 #define TGetMyChar (*gCurrentPtrData.Char)
174 #define TGetGameFlags (*gCurrentPtrData.GameFlags)
175 #define TGetPlayer gCurrentPtrData.Player
176 #define TGetClient gCurrentPtrData.Client
177 #define TGetKey (*gCurrentPtrData.Key)
178 #define TGetKeyTrg (*gCurrentPtrData.KeyTrg)
179 #endif
180 
182 ///
Automatically sets up every textscript command in the mod after calling ModTextScriptCommand::Init().
Definition: ModTextScriptCommand.h:43
TSCRunFunc mExecute
Execute function.
Definition: ModTextScriptCommand.h:69
char * mArguments
This command's arguments.
Definition: ModTextScriptCommand.h:87
char * mShortDesc
This command's shorthand descriptor.
Definition: ModTextScriptCommand.h:79
ModTextScriptCommand()
Blank constructor, for sorted bin list.
Definition: ModTextScriptCommand.h:93
char mCommand[4]
This command's identifier.
Definition: ModTextScriptCommand.h:75
void(* TSCRunFunc)(int &x, int &y, int &z, int &w, bool &bExit, int &iError)
Run a TextScriptCommand object's code.
Definition: ModTextScriptCommand.h:53
char * mDesc
This command's descriptor.
Definition: ModTextScriptCommand.h:83
CAVESTORY_MOD_API BOOL CSM_RegisterTextScriptCommand(const char *pCmdName, const char *pShortDesc, const char *pDesc, const char *pArgs, void(*pFuncPtr)(int &, int &, int &, int &, bool &, int &))
Register a textscript command.
#define CSM_LogError(pCategory, pFormat,...)
Log an error message to the console & to the log file (if one is open).
Definition: CSMAPI_functions.h:110
static void Init()
Initialize the text script commands.
Definition: ModTextScriptCommand.h:142
~ModTextScriptCommand()
Deconstructor.
Definition: ModTextScriptCommand.h:128