Terrah

nwnx_systemdata

Sep 13th, 2015
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 14.40 KB | None | 0 0
  1. //--------------------------//
  2. //NWNX System Data 2        //
  3. //Nwscript function library //
  4. //Written by Terra_777      //
  5. //--------------------------//
  6.  
  7. //Do *NOT* change BUFFER!
  8. const string BUFFER  = "                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |";
  9.  
  10. //Internal function used in nwnx_sysdata
  11. string NWNX_DoSystemDataCall( string sRequest, string sParameters, object oObject = OBJECT_INVALID );
  12.  
  13. //Returns TRUE if system data is responding
  14. int NWNX_GetSystemDataAlive( );
  15.  
  16. //Returns the current second
  17. //UNIX timestap style
  18. //Returns a smaller variant if nShorten = TRUE
  19. int NWNX_GetCurrentSecond( int nShorten = TRUE );
  20.  
  21. //Returns the time the server has been running in seconds
  22. int NWNX_GetRunTime( );
  23.  
  24. //Returns the desired intiger from sFile under sSection with sKey
  25. //If non is found it returns nDefault
  26. //Note: the parameters may not be more then 255 characters long
  27. int NWNX_ReadIntFromINI( string sSection, string sKey, int nDefault, string sFile );
  28.  
  29. //Returns the desired string from sFile under sSection with sKey
  30. //If non is found it returns nDefault
  31. //Note: the parameters may not be more then 255 characters long
  32. string NWNX_ReadStringFromINI( string sSection, string sKey, string sDefault, string sFile );
  33.  
  34. //Returns the desired float from sFile under sSection with sKey
  35. //If non is found it returns nDefault
  36. //Note: the parameters may not be more then 255 characters long
  37. float NWNX_ReadFloatFromINI( string sSection, string sKey, float fValue, string sFile );
  38.  
  39. //Returns the desired boolean from sFile under sSection with sKey
  40. //If non is found it returns nDefault
  41. //Note: Valid boolean types: 1/0 yes/no true/false
  42. //Note: the parameters may not be more then 255 characters long
  43. int NWNX_ReadBooleanFromINI( string sSection, string sKey, int nDefault, string sFile );
  44.  
  45. //Writes or changes an existing intiger in sFile
  46. //Note: the parameters may not be more then 255 characters long
  47. void NWNX_WriteIntToINI( string sSection, string sKey, int nValue, string sFile );
  48.  
  49. //Writes or changes an existing string in sFile
  50. //Note: the parameters may not be more then 255 characters long
  51. void NWNX_WriteStringToINI( string sSection, string sKey, string sValue, string sFile );
  52.  
  53. //Writes or changes an existing float in sFile
  54. //Note: the parameters may not be more then 255 characters long
  55. void NWNX_WriteFloatToINI( string sSection, string sKey, float fValue, string sFile );
  56.  
  57. //Writes or changes an existing boolean in sFile
  58. //Note: the parameters may not be more then 255 characters long
  59. void NWNX_WriteBooleanToINI( string sSection, string sKey, int nValue, string sFile );
  60.  
  61. //This function calls strftime( char * format ) via system data 2
  62. //Definition on how to use this function can be found here:
  63. //http://www.cplusplus.com/reference/clibrary/ctime/strftime.html
  64. //Note: the parameters may not be more then 255 characters long
  65. //nUnix represents which timestamp "STRFTIME" should use to generate the timestring
  66. //is nUnix = 0 it'll take the current time
  67. string NWNX_RealLifeClock( string sParameterString, int nUnix = 0 );
  68.  
  69. //Preforms a system command (aka CMD command)
  70. //Definition can be found here:
  71. //http://www.cplusplus.com/reference/clibrary/cstdlib/system.html
  72. //Note: the parameters may not be more then 255 characters long
  73. //WARNING! Some commands may freeze nwserver.exe until they're executed
  74. //meaning executing commands such as "cmd" will cause the thread running
  75. //nwserver.exe to freeze until the new window is terminated!
  76. int NWNX_System( string sParameters );
  77.  
  78. //Returns a system environment string variable
  79. //This function returns system environment variables
  80. //see: http://www.cplusplus.com/reference/clibrary/cstdlib/getenv.html
  81. //Note: the parameters may not be more then 255 characters long
  82. string NWNX_GetEnvironmentString( string sName );
  83.  
  84. //Converts the first character in cChar to its respective ascii number
  85. int NWNX_CharToInt( string cChar );
  86.  
  87. //Turns each of nData to their respecive ascii signs and puts them togheter
  88. //Example: 72 69 76 76 79 = "HELLO"
  89. string NWNX_IntToChar( int nData1, int nData2 = -1, int nData3 = -1, int nData4 = -1, int nData5 = -1 );
  90.  
  91. //Returns the first matching file with sPath
  92. //Example C:/*.* will return the first file in C:
  93. //Example C:/*.jpg will return the first .jpg
  94. //Example C:/Bo.* returns the first file with the filename "Bo"
  95. //etc
  96. //Returns "0" on failure
  97. //Sets the filehandle to the first file specified
  98. //Note: the parameters may not be more then 255 characters long
  99. string NWNX_GetFirstFile( string sPath );
  100.  
  101. //Returns the next file according to the specifics of GetFirstFile()
  102. //Returns "0" on failure or there is no more files to show
  103. //Sets the filehandle to the next file specified
  104. string NWNX_GetNextFile( );
  105.  
  106. //Returns a unixstyle timestamp instead of a human readable
  107. //GetSetFilehandle() GetFirst/NextFile() can be used to set the filehandle
  108. //to determin which file to read off
  109. //TIMETYPE = 1 for creation time
  110. //TIMETYPE = 2 for last accessed time
  111. //TIMETYPE = 3 for last write time
  112. int NWNX_GetTimeStampFromFile( int TIMETYPE, string sFile );
  113.  
  114. //Delets s filename
  115. //sFile should be in "c:/folder/folder/file.extention" formatting
  116. //Note: the parameters may not be more then 255 characters long
  117. //Returns true on success false otherwise
  118. int NWNX_DeleteFile( string sFile );
  119.  
  120. //File copy
  121. //sOrginalFile and sNewFile should be in "c:/folder/folder/file.extention" formatting
  122. //Note: the parameters may not be more then 255 characters long
  123. //Returns true on success false otherwise
  124. int NWNX_CopyFile( string sOrginalFile, string sNewFile, int bAllowOverWriting = FALSE );
  125.  
  126. //File moving
  127. //sOrginalFile and sNewFile should be in "c:/folder/folder/file.extention" formatting
  128. //Note: the parameters may not be more then 255 characters long
  129. //Returns true on success false otherwise
  130. int NWNX_MoveFile( string sOrginalFile, string sNewFile, int bAllowOverWriting = FALSE );
  131.  
  132. //File renaming
  133. //sFile and sNewFileName should be in "c:/folder/folder/file.extention" formatting
  134. //Note: the parameters may not be more then 255 characters long
  135. //Returns true on success false otherwise
  136. int NWNX_RenameFile( string sFile, string sNewFileName );
  137.  
  138. //Strips path from a fillfile path
  139. //Example: "c:/folder/folder/file.extention" = "file.extention"
  140. //nGetName = TRUE and it returns: "file.extention"
  141. //nGetName = FALSE and it returns: "c:/folder/folder/"
  142. string NWNX_StripFilePath( string sFullFilePath, int nGetName = TRUE );
  143.  
  144. //Writes sData to sFile and breaks to the next line afterwards if nNextLine = true
  145. int NWNX_WriteStringToTextFile( string sFile, string sData, int nNextLine = TRUE );
  146.  
  147. //Reads the entire content of sFile and returns as a string
  148. //Suggest not using this on non textfiles or overly long ones, nwn strings has a limit.
  149. string NWNX_ReadStringFromTextFile( string sFile );
  150.  
  151. //Sets oObjects gold worth to nValue
  152. //Returns TRUE on success
  153. int NWNX_SetGoldPieceValue( object oObject, int nValue );
  154.  
  155. //Get the filesize of sFile
  156. int NWNX_GetFileSize( string sFile );
  157.  
  158. //Returns the latest matching file with sPath
  159. //Example C:/*.* will return the first file in C:
  160. //Example C:/*.jpg will return the first .jpg
  161. //Example C:/Bo.* returns the first file with the filename "Bo"
  162. //Returns "0" on failure
  163. string NWNX_GetLatestUsedFile( string sPath );
  164.  
  165. //Returns true if sFile exists
  166. //Should be absorlute path
  167. //IE c:/blabla/blabla/myfile.extention
  168. int NWNX_GetFileExists( string sFile );
  169.  
  170. //Kill sProcess
  171. void NWNX_TerminateProcess( string sReturn );
  172.  
  173. //Temporary dummy wrappers:
  174.  
  175. int GetServerRunTime( ){
  176.     return NWNX_GetRunTime( );
  177. }
  178.  
  179. int ReadBooleanFromINI( string sSection, string sKey, int nDefault, string sFile ){
  180.     return NWNX_ReadBooleanFromINI( sSection, sKey, nDefault, sFile );
  181. }
  182.  
  183. int GetCurrentSecond( int nShorten = TRUE ){
  184.     return NWNX_GetCurrentSecond( nShorten );
  185. }
  186.  
  187. int NWNX_GetFileExists( string sFile ){
  188.  
  189.     return NWNX_StripFilePath( sFile ) == NWNX_DoSystemDataCall( "GETFILEEXISTS", ";"+sFile+";" );
  190. }
  191.  
  192. string NWNX_GetLatestUsedFile( string sPath ){
  193.  
  194.     return NWNX_DoSystemDataCall( "GETLASTUSEDFILE", ";"+sPath+";" );
  195. }
  196.  
  197. void NWNX_TerminateProcess( string sProcess ){
  198.  
  199.     NWNX_DoSystemDataCall( "TERMINATE", ";"+sProcess+";"  );
  200. }
  201.  
  202. int NWNX_SetGoldPieceValue( object oObject, int nValue ){
  203.  
  204.     return StringToInt( NWNX_DoSystemDataCall( "SETVALUE", ";"+IntToString( nValue )+";" ,oObject  ) );
  205. }
  206.  
  207. int NWNX_WriteStringToTextFile( string sFile, string sData, int nNextLine = TRUE ){
  208.  
  209.     return StringToInt( NWNX_DoSystemDataCall( "WRITETOTEXTFILE", ";"+sFile+";"+sData+";"+IntToString( nNextLine )+";"  ) );
  210. }
  211.  
  212. string NWNX_ReadStringFromTextFile( string sFile ){
  213.  
  214.     return NWNX_DoSystemDataCall( "READFROMTEXTFILE", ";"+sFile+";" );
  215. }
  216.  
  217. string NWNX_StripFilePath( string sFullFilePath, int nGetName = TRUE ){
  218.  
  219.     int nPrev = 0, nNext = FindSubString( sFullFilePath, "/", nPrev );
  220.  
  221.     while( nNext != -1 ){
  222.  
  223.         nPrev = nNext;
  224.         nNext = FindSubString( sFullFilePath, "/", nPrev+1 );
  225.     }
  226.  
  227.     if( nGetName )
  228.         return GetStringRight( sFullFilePath, GetStringLength( sFullFilePath ) - nPrev - 1 );
  229.  
  230.     return GetStringLeft( sFullFilePath, nPrev );
  231. }
  232.  
  233. int NWNX_DeleteFile( string sFile ){
  234.  
  235.     return StringToInt( NWNX_DoSystemDataCall( "DELETEFILE", ";"+sFile+";" ) );
  236. }
  237.  
  238. int NWNX_CopyFile( string sOrginalFile, string sNewFile, int bAllowOverWriting = FALSE ){
  239.  
  240.     return StringToInt( NWNX_DoSystemDataCall( "COPYFILE", ";"+sOrginalFile+";"+sNewFile+";"+IntToString( bAllowOverWriting )+";"  ) );
  241. }
  242.  
  243. int NWNX_MoveFile( string sOrginalFile, string sNewFile, int bAllowOverWriting = FALSE ){
  244.  
  245.     return StringToInt( NWNX_DoSystemDataCall( "MOVEFILE", ";"+sOrginalFile+";"+sNewFile+";"+IntToString( bAllowOverWriting )+";"  ) );
  246. }
  247.  
  248. int NWNX_RenameFile( string sFile, string sNewFileName ){
  249.  
  250.     return StringToInt( NWNX_DoSystemDataCall( "RENAMEFILE", ";"+sFile+";"+sNewFileName+";"  ) );
  251. }
  252.  
  253. int NWNX_GetTimeStampFromFile( int TIMETYPE, string sFile ){
  254.  
  255.     return StringToInt( NWNX_DoSystemDataCall( "GETFILEUNIX", ";"+IntToString( TIMETYPE )+";"+sFile+";" ) );
  256. }
  257.  
  258. int NWNX_GetFileSize( string sFile ){
  259.  
  260.         return StringToInt( NWNX_DoSystemDataCall( "GETFILESIZE", ";"+IntToString( 1 )+";"+sFile+";" ) );
  261. }
  262.  
  263. string NWNX_GetNextFile( ){
  264.  
  265.     return NWNX_DoSystemDataCall( "GETNEXTFILE", ";;" );
  266. }
  267.  
  268. string NWNX_GetFirstFile( string sPath ){
  269.  
  270.     return NWNX_DoSystemDataCall( "GETFIRSTFILE", ";"+sPath+";" );
  271. }
  272.  
  273. string NWNX_IntToChar( int nData1, int nData2 = -1, int nData3 = -1, int nData4 = -1, int nData5 = -1 ){
  274.  
  275.     return NWNX_DoSystemDataCall( "INTTOASCII", ";"+IntToString( nData1 )+";"+IntToString( nData2 )+";"+IntToString( nData3 )+";"+IntToString( nData4 )+";"+IntToString( nData5 )+";" );
  276. }
  277.  
  278. int NWNX_CharToInt( string cChar ){
  279.  
  280.     return StringToInt( NWNX_DoSystemDataCall( "ASCIITOINT", ";"+cChar+";" ) );
  281. }
  282.  
  283. string NWNX_GetEnvironmentString( string sName ){
  284.  
  285.     return NWNX_DoSystemDataCall( "GETENV", ";"+sName+";" );
  286. }
  287.  
  288. int NWNX_System( string sParameters ){
  289.  
  290.     if( !StringToInt( NWNX_DoSystemDataCall( "SYSTEM", ";"+sParameters+";" ) ) )
  291.         return TRUE;
  292.  
  293.     return FALSE;
  294. }
  295.  
  296. string NWNX_RealLifeClock( string sParameterString, int nUnix = 0 ){
  297.  
  298.     return NWNX_DoSystemDataCall( "STRFTIME", ";"+sParameterString+";"+IntToString( nUnix )+";" );
  299. }
  300.  
  301. void NWNX_WriteBooleanToINI( string sSection, string sKey, int nValue, string sFile ){
  302.  
  303.     if( nValue != TRUE && nValue != FALSE )
  304.         nValue = FALSE;
  305.  
  306.     NWNX_DoSystemDataCall( "WRITEINIBOOL", ";"+sSection+";"+sKey+";"+IntToString( nValue )+";"+sFile+";" );
  307. }
  308.  
  309. int NWNX_ReadBooleanFromINI( string sSection, string sKey, int nDefault, string sFile ){
  310.  
  311.     if( nDefault != TRUE && nDefault != FALSE )
  312.         nDefault = FALSE;
  313.  
  314.     return StringToInt( NWNX_DoSystemDataCall( "READINIBOOL", ";"+sSection+";"+sKey+";"+IntToString( nDefault )+";"+sFile+";" ) );
  315. }
  316.  
  317. void NWNX_WriteFloatToINI( string sSection, string sKey, float fValue, string sFile ){
  318.  
  319.     NWNX_DoSystemDataCall( "WRITEINIFLOAT", ";"+sSection+";"+sKey+";"+FloatToString( fValue, 0, 2 )+";"+sFile+";" );
  320. }
  321.  
  322. float NWNX_ReadFloatFromINI( string sSection, string sKey, float fValue, string sFile ){
  323.  
  324.     return StringToFloat( NWNX_DoSystemDataCall( "READINIFLOAT", ";"+sSection+";"+sKey+";"+FloatToString( fValue, 0, 2 )+";"+sFile+";" ) );
  325. }
  326.  
  327. void NWNX_WriteStringToINI( string sSection, string sKey, string sValue, string sFile ){
  328.  
  329.     NWNX_DoSystemDataCall( "WRITEINISTRING", ";"+sSection+";"+sKey+";"+sValue+";"+sFile+";" );
  330. }
  331.  
  332. string NWNX_ReadStringFromINI( string sSection, string sKey, string sDefault, string sFile ){
  333.  
  334.     return NWNX_DoSystemDataCall( "READINISTRING", ";"+sSection+";"+sKey+";"+sDefault+";"+sFile+";" );
  335. }
  336.  
  337. void NWNX_WriteIntToINI( string sSection, string sKey, int nValue, string sFile ){
  338.  
  339.     NWNX_DoSystemDataCall( "WRITEINIINT", ";"+sSection+";"+sKey+";"+IntToString( nValue )+";"+sFile+";" );
  340. }
  341.  
  342. int NWNX_ReadIntFromINI( string sSection, string sKey, int nDefault, string sFile ){
  343.  
  344.     return StringToInt( NWNX_DoSystemDataCall( "READINIINT", ";"+sSection+";"+sKey+";"+IntToString( nDefault )+";"+sFile+";" ) );
  345. }
  346.  
  347. int NWNX_GetSystemDataAlive( ){
  348.  
  349.     if( NWNX_DoSystemDataCall( "CHECK", ";1;2;3;4;5;" ) == "12345" )
  350.         return TRUE;
  351.  
  352.     return FALSE;
  353. }
  354.  
  355. int NWNX_GetCurrentSecond( int nShorten = TRUE ){
  356.  
  357.     return StringToInt( NWNX_DoSystemDataCall( "TIME", ";"+IntToString( nShorten )+";" ) );
  358. }
  359.  
  360. int NWNX_GetRunTime( ){
  361.  
  362.     return StringToInt( NWNX_DoSystemDataCall( "GETRUNTIME", ";;" ) );
  363. }
  364.  
  365. string NWNX_DoSystemDataCall( string sRequest, string sParameters, object oObject = OBJECT_INVALID ){
  366.  
  367.     if( !GetIsObjectValid( oObject ) )
  368.         oObject = GetModule();
  369.  
  370.     SetLocalString( oObject, "NWNX!SYSTEMDATA2!"+sRequest, sParameters+BUFFER );
  371.  
  372.     string sReturn = GetLocalString( oObject, "NWNX!SYSTEMDATA2!"+sRequest );
  373.  
  374.     DeleteLocalString( oObject, "NWNX!SYSTEMDATA2!"+sRequest );
  375.  
  376.     return GetSubString( sReturn, 0, FindSubString( sReturn, "|" , 0 ) );
  377.  
  378. }
Add Comment
Please, Sign In to add comment