debug.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  1. class Debug
  2. {
  3. private static const string LOG_DEBUG = "Debug";
  4. private static const string LOG_DEBUG_ACTION = "Action";
  5. private static const string LOG_DEBUG_SYMPTOM = "Symptom";
  6. private static const string LOG_DEBUG_INV_MOVE = "Inv Move";
  7. private static const string LOG_DEBUG_INV_RESERVATION = "Inv Rrsv";
  8. private static const string LOG_DEBUG_INV_HFSM = "HFSM";
  9. private static const string LOG_DEBUG_QUICKBAR = "Quickbar";
  10. private static const string LOG_DEBUG_BASEBUILDING = "Base Building";
  11. private static const string LOG_DEBUG_BLEEDING_CHANCES = "Bleeding";
  12. private static const string LOG_DEBUG_TRIGGER = "Trigger";
  13. private static const string LOG_DEBUG_PARTICLE = "Particle";
  14. private static const string LOG_DEBUG_TF = "TestFramework";
  15. private static const string LOG_DEBUG_WEIGHT = "Weight";
  16. private static const string LOG_DEBUG_MELEE = "Melee";
  17. private static const string LOG_DEBUG_WEATHER = "Weather";
  18. private static const string LOG_INFO = "Info";
  19. private static const string LOG_WARNING = "Warning";
  20. private static const string LOG_ERROR = "Error";
  21. private static const string LOG_DEFAULT = "n/a";
  22. private static ref array<Shape> m_DebugShapes;
  23. static Widget m_DebugLayoutCanvas;
  24. static CanvasWidget m_CanvasDebug;
  25. static string GetDebugName(Managed entity)
  26. {
  27. if (!entity)
  28. return "";
  29. Object obj;
  30. if (CastTo(obj, entity))
  31. return obj.GetDebugNameNative();
  32. return entity.GetDebugName();
  33. }
  34. static void InitCanvas()
  35. {
  36. if (!m_DebugLayoutCanvas)
  37. {
  38. m_DebugLayoutCanvas = GetGame().GetWorkspace().CreateWidgets("gui/layouts/debug/day_z_debugcanvas.layout");
  39. m_CanvasDebug = CanvasWidget.Cast( m_DebugLayoutCanvas.FindAnyWidget( "CanvasWidget" ) );
  40. }
  41. }
  42. static void ClearCanvas()
  43. {
  44. if (m_CanvasDebug)
  45. m_CanvasDebug.Clear();
  46. }
  47. static void CanvasDrawLine(float x1, float y1, float x2, float y2, float width, int color)
  48. {
  49. InitCanvas();
  50. m_CanvasDebug.DrawLine(x1, y1, x2, y2, width, color);
  51. }
  52. /**
  53. \brief Draws a "point" on the screen at x,y coordinates
  54. Debug.ClearCanvas();
  55. for(int i = 0; i < 700;i++)
  56. {
  57. float val = i/700;
  58. float y = Easing.EaseInOutExpo(val);
  59. Debug.CanvasDrawPoint(i,y*700,ARGBF( 0.6, 1, 1, 1 ));
  60. }
  61. */
  62. static void CanvasDrawPoint(float x1, float y1, int color)
  63. {
  64. CanvasDrawLine(x1, y1, x1+1, y1, 1, color);
  65. }
  66. static void Init()
  67. {
  68. m_DebugShapes = new array<Shape>;
  69. }
  70. static void DestroyAllShapes()
  71. {
  72. for ( int i = 0; i < m_DebugShapes.Count(); ++i )
  73. {
  74. if ( m_DebugShapes.Get(i) )
  75. {
  76. m_DebugShapes.Get(i).Destroy();
  77. }
  78. }
  79. m_DebugShapes.Clear();
  80. }
  81. static void RemoveShape(out Shape shape)
  82. {
  83. if (!shape) return;
  84. for ( int i = 0; i < m_DebugShapes.Count(); i++ )
  85. {
  86. Shape found_shape = m_DebugShapes.Get(i);
  87. if ( found_shape && found_shape == shape )
  88. {
  89. found_shape.Destroy();
  90. m_DebugShapes.Remove(i); // Mandatory! Otherwise the Destroy() function causes crash!
  91. shape = null;
  92. return;
  93. }
  94. }
  95. }
  96. /**
  97. \brief Prints debug message with normal prio
  98. \param msg \p string Debug message for print
  99. \return \p void None
  100. @code
  101. Debug.Log("Hello World");
  102. >> [Log]: Hello World;
  103. @endcode
  104. */
  105. static void Log(string message = LOG_DEFAULT, string plugin = LOG_DEFAULT, string author = LOG_DEFAULT, string label = LOG_DEFAULT, string entity = LOG_DEFAULT)
  106. {
  107. LogMessage(LOG_DEBUG, plugin, entity, author, label, message);
  108. }
  109. static void ActionLog(string message = LOG_DEFAULT, string plugin = LOG_DEFAULT, string author = LOG_DEFAULT, string label = LOG_DEFAULT, string entity = LOG_DEFAULT)
  110. {
  111. LogMessage(LOG_DEBUG_ACTION, plugin, entity, author, label, message);
  112. }
  113. static void SymptomLog(string message = LOG_DEFAULT, string plugin = LOG_DEFAULT, string author = LOG_DEFAULT, string label = LOG_DEFAULT, string entity = LOG_DEFAULT)
  114. {
  115. LogMessage(LOG_DEBUG_SYMPTOM, plugin, entity, author, label, message);
  116. }
  117. static void InventoryMoveLog(string message = LOG_DEFAULT, string plugin = LOG_DEFAULT, string author = LOG_DEFAULT, string label = LOG_DEFAULT, string entity = LOG_DEFAULT)
  118. {
  119. LogMessage(LOG_DEBUG_INV_MOVE, plugin, entity, author, label, message);
  120. }
  121. static void InventoryReservationLog(string message = LOG_DEFAULT, string plugin = LOG_DEFAULT, string author = LOG_DEFAULT, string label = LOG_DEFAULT, string entity = LOG_DEFAULT)
  122. {
  123. LogMessage(LOG_DEBUG_INV_RESERVATION, plugin, entity, author, label, message);
  124. }
  125. static void InventoryHFSMLog(string message = LOG_DEFAULT, string plugin = LOG_DEFAULT, string author = LOG_DEFAULT, string label = LOG_DEFAULT, string entity = LOG_DEFAULT)
  126. {
  127. LogMessage(LOG_DEBUG_INV_HFSM, plugin, entity, author, label, message);
  128. }
  129. static void QuickbarLog(string message = LOG_DEFAULT, string plugin = LOG_DEFAULT, string author = LOG_DEFAULT, string label = LOG_DEFAULT, string entity = LOG_DEFAULT)
  130. {
  131. LogMessage(LOG_DEBUG_QUICKBAR, plugin, entity, author, label, message);
  132. }
  133. static void BaseBuildingLog(string message = LOG_DEFAULT, string plugin = LOG_DEFAULT, string author = LOG_DEFAULT, string label = LOG_DEFAULT, string entity = LOG_DEFAULT)
  134. {
  135. LogMessage(LOG_DEBUG_BASEBUILDING, plugin, entity, author, label, message);
  136. }
  137. static void BleedingChancesLog(string message = LOG_DEFAULT, string plugin = LOG_DEFAULT, string author = LOG_DEFAULT, string label = LOG_DEFAULT, string entity = LOG_DEFAULT)
  138. {
  139. LogMessage(LOG_DEBUG_BLEEDING_CHANCES, plugin, entity, author, label, message);
  140. }
  141. static void TriggerLog(string message = LOG_DEFAULT, string plugin = LOG_DEFAULT, string author = LOG_DEFAULT, string label = LOG_DEFAULT, string entity = LOG_DEFAULT)
  142. {
  143. LogMessage(LOG_DEBUG_TRIGGER, plugin, entity, author, label, message);
  144. }
  145. static void ParticleLog(string message = LOG_DEFAULT, Managed caller = null, string function = "", Managed entity = null)
  146. {
  147. LogMessage(LOG_DEBUG_PARTICLE, GetDebugName(caller), GetDebugName(entity), "", function, message);
  148. }
  149. static void TFLog(string message = LOG_DEFAULT, TestFramework caller = null, string function = "")
  150. {
  151. LogMessage(LOG_DEBUG_TF, GetDebugName(caller), "", "", function, message);
  152. }
  153. static void WeightLog(string message = LOG_DEFAULT, Managed caller = null, string function = "", Managed entity = null)
  154. {
  155. /*
  156. LogMessage(LOG_DEBUG_WEIGHT, GetDebugName(caller), GetDebugName(entity), "", function, message);
  157. */
  158. }
  159. static void MeleeLog(Entity entity, string message = LOG_DEFAULT, string plugin = LOG_DEFAULT, string author = LOG_DEFAULT, string label = LOG_DEFAULT)
  160. {
  161. string logMessage = string.Format("%1: %2", entity.GetSimulationTimeStamp(), message);
  162. LogMessage(LOG_DEBUG_MELEE, plugin, GetDebugName(entity), author, label, logMessage);
  163. }
  164. static void WeatherLog(string message = LOG_DEFAULT, string plugin = LOG_DEFAULT, string author = LOG_DEFAULT, string label = LOG_DEFAULT, string entity = LOG_DEFAULT)
  165. {
  166. if (!LogManager.IsWeatherLogEnabled())
  167. {
  168. return;
  169. }
  170. LogMessage(LOG_DEBUG_WEATHER, plugin, entity, author, label, message);
  171. }
  172. /**
  173. \brief Prints debug message with normal prio
  174. \param msg \p string Debug message for print
  175. \return \p void None
  176. @code
  177. Debug.Log("Hello World");
  178. >> [Log]: Hello World;
  179. @endcode
  180. */
  181. static void LogInfo(string message = LOG_DEFAULT, string plugin = LOG_DEFAULT, string author = LOG_DEFAULT, string label = LOG_DEFAULT, string entity = LOG_DEFAULT)
  182. {
  183. LogMessage(LOG_INFO, plugin, entity, author, label, message);
  184. }
  185. /**
  186. \brief Prints debug message as warning message
  187. \param msg \p string Debug message for warning print
  188. \return \p void None
  189. @code
  190. Debug.LogWarning("Hello World, this is warning log");
  191. >> [Warning]: Hello World, this is warning log
  192. @endcode
  193. */
  194. static void LogWarning(string message = LOG_DEFAULT, string plugin = LOG_DEFAULT, string author = LOG_DEFAULT, string label = LOG_DEFAULT, string entity = LOG_DEFAULT)
  195. {
  196. LogMessage(LOG_WARNING, plugin, entity, author, label, message);
  197. }
  198. /**
  199. \brief Prints debug message as error message
  200. \param msg \p string Debug message for error print
  201. \return \p void None
  202. @code
  203. Debug.LogError("Hello World, this is error log");
  204. >> [Error]: Hello World, this is error log
  205. @endcode
  206. */
  207. static void LogError(string message = LOG_DEFAULT, string plugin = LOG_DEFAULT, string author = LOG_DEFAULT, string label = LOG_DEFAULT, string entity = LOG_DEFAULT)
  208. {
  209. LogMessage(LOG_ERROR, plugin, entity, author, label, message);
  210. }
  211. static void LogArrayInt(array<int> arr = NULL, string plugin = LOG_DEFAULT, string author = LOG_DEFAULT, string label = LOG_DEFAULT, string entity = LOG_DEFAULT)
  212. {
  213. if (arr == null)
  214. return;
  215. for (int i = 0; i < arr.Count(); i++)
  216. {
  217. LogMessage(LOG_DEBUG, plugin, entity, author, label, arr.Get(i).ToString());
  218. }
  219. }
  220. static void LogArrayString(array<string> arr = NULL, string plugin = LOG_DEFAULT, string author = LOG_DEFAULT, string label = LOG_DEFAULT, string entity = LOG_DEFAULT)
  221. {
  222. if (arr == null)
  223. return;
  224. for (int i = 0; i < arr.Count(); i++)
  225. {
  226. LogMessage(LOG_DEBUG, plugin, entity, author, label, arr.Get(i));
  227. }
  228. }
  229. static void ReceivedLogMessageFromServer(string message)
  230. {
  231. if (!LogManager.IsLogsEnable())
  232. {
  233. return;
  234. }
  235. SaveLog(message);
  236. }
  237. static void ClearScriptLogs()
  238. {
  239. ClearLogs();
  240. }
  241. static Shape DrawBox(vector pos1, vector pos2, int color = 0x1fff7f7f)
  242. {
  243. return DrawBoxEx(pos1, pos2, color, ShapeFlags.TRANSP|ShapeFlags.NOZWRITE);
  244. }
  245. static Shape DrawBoxEx(vector pos1, vector pos2, int color = 0x1fff7f7f, ShapeFlags flags = ShapeFlags.TRANSP|ShapeFlags.NOZWRITE)
  246. {
  247. Shape shape = Shape.Create(ShapeType.BBOX, color, flags, pos1, pos2);
  248. if (( flags & ShapeFlags.ONCE ) == 0)
  249. m_DebugShapes.Insert(shape);
  250. return shape;
  251. }
  252. static Shape DrawCube(vector pos, float size = 1, int color = 0x1fff7f7f)
  253. {
  254. vector min = pos;
  255. vector max = pos;
  256. float size_h = size * 0.5;
  257. min[0] = min[0] - size_h;
  258. min[1] = min[1] - size_h;
  259. min[2] = min[2] - size_h;
  260. max[0] = max[0] + size_h;
  261. max[1] = max[1] + size_h;
  262. max[2] = max[2] + size_h;
  263. Shape shape = Shape.Create(ShapeType.DIAMOND, color, ShapeFlags.TRANSP|ShapeFlags.NOZWRITE, min, max);
  264. m_DebugShapes.Insert(shape);
  265. return shape;
  266. }
  267. static Shape DrawSphere(vector pos, float size = 1, int color = 0x1fff7f7f, ShapeFlags flags = ShapeFlags.TRANSP|ShapeFlags.NOOUTLINE)
  268. {
  269. Shape shape = Shape.CreateSphere(color, flags, pos, size);
  270. if (( flags & ShapeFlags.ONCE ) == 0)
  271. m_DebugShapes.Insert(shape);
  272. return shape;
  273. }
  274. static Shape DrawFrustum(float horizontalAngle, float verticalAngle, float length, int color = 0x1fff7f7f, ShapeFlags flags = ShapeFlags.TRANSP|ShapeFlags.WIREFRAME)
  275. {
  276. Shape shape = Shape.CreateFrustum(horizontalAngle, verticalAngle, length, color, flags);
  277. if (( flags & ShapeFlags.ONCE ) == 0)
  278. m_DebugShapes.Insert(shape);
  279. return shape;
  280. }
  281. static Shape DrawCylinder(vector pos, float radius, float height = 1, int color = 0x1fff7f7f, ShapeFlags flags = ShapeFlags.TRANSP|ShapeFlags.NOOUTLINE )
  282. {
  283. Shape shape = Shape.CreateCylinder(color, flags, pos, radius, height);
  284. if (( flags & ShapeFlags.ONCE ) == 0)
  285. m_DebugShapes.Insert(shape);
  286. return shape;
  287. }
  288. static array<Shape> DrawCone(vector pos, float lenght, float halfAngle, float offsetAngle, int color = 0xFFFFFFFF, int flags = 0)
  289. {
  290. array<Shape> shapes = new array<Shape>;
  291. vector endL, endR;
  292. Math3D.ConePoints(pos, lenght, halfAngle, offsetAngle, endL, endR);
  293. // Left side
  294. shapes.Insert( Debug.DrawLine(pos, endL, color, flags) );
  295. // Rigth side
  296. shapes.Insert( Debug.DrawLine(pos, endR, color, flags) );
  297. // Top side
  298. shapes.Insert( Debug.DrawLine(endL, endR, color, flags) );
  299. // Middle (height) line
  300. shapes.Insert( Debug.DrawLine(pos, pos + Vector(Math.Cos(offsetAngle), 0, Math.Sin(offsetAngle)).Normalized() * lenght, color, flags) );
  301. return shapes;
  302. }
  303. static void CleanupDrawShapes(array<Shape> shapes)
  304. {
  305. foreach (Shape shape : shapes)
  306. Debug.RemoveShape(shape);
  307. shapes.Clear();
  308. }
  309. /**
  310. DrawLine
  311. \nFlags:
  312. \n ShapeFlags.NOZBUFFER
  313. \n ShapeFlags.NOZUPDATE
  314. \n ShapeFlags.DOUBLESIDE
  315. \n ShapeFlags.WIREFRAME
  316. \n ShapeFlags.TRANSP
  317. \n ShapeFlags.ONCE
  318. \n ShapeFlags.NOOUTLINE
  319. \n ShapeFlags.NOCULL
  320. */
  321. static Shape DrawLine(vector from, vector to, int color = 0xFFFFFFFF, int flags = 0)
  322. {
  323. vector pts[2]
  324. pts[0] = from;
  325. pts[1] = to;
  326. Shape shape = Shape.CreateLines(color, flags, pts, 2);
  327. if (( flags & ShapeFlags.ONCE ) == 0)
  328. m_DebugShapes.Insert(shape);
  329. //m_DebugShapes.Debug();
  330. return shape;
  331. }
  332. static Shape DrawLines(vector[] positions, int count, int color = 0xFFFFFFFF, int flags = 0)
  333. {
  334. Shape shape = Shape.CreateLines(color, flags, positions, count);
  335. if (( flags & ShapeFlags.ONCE ) == 0)
  336. m_DebugShapes.Insert(shape);
  337. return shape;
  338. }
  339. static Shape DrawArrow(vector from, vector to, float size = 0.5, int color = 0xFFFFFFFF, int flags = 0)
  340. {
  341. Shape shape = Shape.CreateArrow(from, to, size, color, flags);
  342. m_DebugShapes.Insert(shape);
  343. return shape;
  344. }
  345. /**
  346. \brief Returns some of base config classes strings like CfgVehicles, CfgWeapons, etc. for searching purposes
  347. \param base_classes \p out TStringArray Array containing some of base config classes
  348. */
  349. static void GetBaseConfigClasses( out TStringArray base_classes )
  350. {
  351. base_classes.Clear();
  352. base_classes.Insert(CFG_VEHICLESPATH);
  353. base_classes.Insert(CFG_WEAPONSPATH);
  354. base_classes.Insert(CFG_MAGAZINESPATH);
  355. base_classes.Insert(CFG_AMMO);
  356. base_classes.Insert(CFG_WORLDS);
  357. base_classes.Insert(CFG_SURFACES);
  358. base_classes.Insert(CFG_SOUND_SETS);
  359. base_classes.Insert(CFG_SOUND_SHADERS);
  360. base_classes.Insert(CFG_NONAI_VEHICLES);
  361. base_classes.Insert(CFG_SOUND_TABLES);
  362. }
  363. /**
  364. \brief Returns config classes containing search_string in name
  365. \param search_string \p string String to search in class names
  366. \param filtered_classes \p out TStringArray Array containing filtered classes based on search_string
  367. \param only_public \p bool Set to true to return only public classes, set to false to return all filtered classes
  368. */
  369. static void GetFiltredConfigClasses( string search_string, out TStringArray filtered_classes, bool only_public = true )
  370. {
  371. TStringArray searching_in = new TStringArray;
  372. GetBaseConfigClasses( searching_in );
  373. filtered_classes.Clear();
  374. search_string.ToLower();
  375. for ( int s = 0; s < searching_in.Count(); ++s )
  376. {
  377. string config_path = searching_in.Get(s);
  378. int objects_count = GetGame().ConfigGetChildrenCount(config_path);
  379. for (int i = 0; i < objects_count; i++)
  380. {
  381. string childName;
  382. GetGame().ConfigGetChildName(config_path, i, childName);
  383. if ( only_public )
  384. {
  385. int scope = GetGame().ConfigGetInt( config_path + " " + childName + " scope" );
  386. if ( scope == 0 )
  387. {
  388. continue;
  389. }
  390. }
  391. string nchName = childName;
  392. nchName.ToLower();
  393. if ( nchName.Contains(search_string) != -1)
  394. {
  395. filtered_classes.Insert(childName);
  396. }
  397. }
  398. }
  399. }
  400. //---------------------------------------------------------------
  401. //-------private
  402. private static bool m_EnabledLogs; //! DEPRECATED
  403. private static string LogMessage(string level, string plugin, string entity, string author, string label, string message)
  404. {
  405. if (GetGame() == null || !LogManager.IsLogsEnable())
  406. return string.Empty;
  407. bool is_server_log = ( GetGame().IsServer() && GetGame().IsMultiplayer() );
  408. // Formation output to external file
  409. // %date{MM-dd HH:mm:ss} | %Enviroment | %Level | %Module | %Entity | %Author | %Label | %Message
  410. string date = GetDate();
  411. string env = "Client";
  412. string msg = string.Empty;
  413. if ( is_server_log )
  414. {
  415. env = "Server";
  416. }
  417. msg = string.Format("%1 | %2 | %3 | %4 | %5 | %6 | %7", date, env, level, plugin, entity, label, message);
  418. if ( is_server_log )
  419. {
  420. SaveLog(msg);
  421. #ifdef DIAG_DEVELOPER // do not send log to clients on retail
  422. Param1<string> msg_p = new Param1<string>(msg);
  423. CallMethod(CALL_ID_SEND_LOG, msg_p);
  424. #endif
  425. }
  426. else
  427. {
  428. SaveLog(msg);
  429. }
  430. return msg;
  431. }
  432. private static void SaveLog(string log_message)
  433. {
  434. if (log_message.Length() == 0)
  435. return;
  436. //! Only beneficial for diag and developers, removed on retail
  437. #ifdef DIAG_DEVELOPER
  438. #ifndef SERVER
  439. CachedObjectsParams.PARAM1_STRING.param1 = log_message;
  440. GetDispatcher().CallMethod(CALL_ID_SCR_CNSL_ADD_PRINT, CachedObjectsParams.PARAM1_STRING);
  441. #endif
  442. #endif
  443. //! Avoid writing to this file as it will grow indefinitely
  444. #ifdef DIAG_DEVELOPER
  445. //! Active when '-logToFile=1' is set
  446. #ifdef LOG_TO_FILE
  447. FileHandle fileHandle = OpenFile(GetFileName(), FileMode.APPEND);
  448. if (fileHandle != 0)
  449. {
  450. FPrintln(fileHandle, log_message);
  451. CloseFile(fileHandle);
  452. }
  453. #endif
  454. #endif
  455. //! Active when '-logToRpt=1' is set
  456. #ifdef LOG_TO_RPT
  457. PrintToRPT("" + log_message);
  458. #endif
  459. //! Active when '-logToScript=1' is set
  460. #ifdef LOG_TO_SCRIPT
  461. Print(string.Format("%1", log_message));
  462. #endif
  463. }
  464. static void ClearLogs()
  465. {
  466. if (FileExist(GetFileName()))
  467. {
  468. FileHandle fileHandle = OpenFile(GetFileName(), FileMode.WRITE);
  469. if (fileHandle == 0)
  470. return;
  471. FPrintln(fileHandle, "");
  472. CloseFile(fileHandle);
  473. }
  474. }
  475. static string GetFileName()
  476. {
  477. return CFG_FILE_SCRIPT_LOG_EXT;
  478. }
  479. private static string GetDate()
  480. {
  481. int year;
  482. int month;
  483. int day;
  484. int hour;
  485. int minute;
  486. int second;
  487. GetYearMonthDay(year, month, day);
  488. GetHourMinuteSecond(hour, minute, second);
  489. string date = month.ToStringLen(2) + "-" + day.ToStringLen(2) + " " + hour.ToStringLen(2) + ":" + minute.ToStringLen(2) + ":" + second.ToStringLen(2);
  490. return date;
  491. }
  492. };
  493. class LogManager
  494. {
  495. static bool m_DoLogs;
  496. static bool m_DoActionDebugLog;
  497. static bool m_DoSymptomDebugLog;
  498. static bool m_DoInventoryMoveLog;
  499. static bool m_DoInventoryReservationLog;
  500. static bool m_DoInventoryHFSMLog;
  501. static bool m_DoSyncLog;
  502. static bool m_DoQuickbarLog;
  503. static bool m_DoBaseBuildingLog;
  504. static bool m_DoWeaponLog;
  505. static bool m_DoWeatherLog;
  506. static bool m_DoBleedingChanceLog;
  507. static void Init()
  508. {
  509. #ifdef ENABLE_LOGGING
  510. m_DoLogs = true;
  511. #else
  512. m_DoLogs = IsCLIParam("doLogs");
  513. #endif
  514. m_DoActionDebugLog = IsCLIParam("doActionLog");
  515. m_DoSymptomDebugLog = IsCLIParam("doSymptomLog");
  516. m_DoInventoryMoveLog = IsCLIParam("doInvMoveLog");
  517. m_DoInventoryReservationLog = IsCLIParam("doInvReservLog");
  518. m_DoInventoryHFSMLog = IsCLIParam("doInvHFSMLog");
  519. m_DoSyncLog = IsCLIParam("doSyncLog");
  520. m_DoQuickbarLog = IsCLIParam("doQuickbarLog");
  521. m_DoBaseBuildingLog = IsCLIParam("doBaseBuildingLog");
  522. m_DoWeaponLog = IsCLIParam("doWeaponLog");
  523. m_DoWeatherLog = IsCLIParam("doWeatherLog");
  524. }
  525. static bool IsLogsEnable()
  526. {
  527. return m_DoLogs;
  528. }
  529. static void SetLogsEnabled(bool enable)
  530. {
  531. m_DoLogs = enable;
  532. }
  533. static bool IsActionLogEnable()
  534. {
  535. return m_DoActionDebugLog;
  536. }
  537. static void ActionLogEnable(bool enable)
  538. {
  539. m_DoActionDebugLog = enable;
  540. }
  541. static bool IsInventoryMoveLogEnable()
  542. {
  543. return m_DoInventoryMoveLog;
  544. }
  545. static void InventoryMoveLogEnable(bool enable)
  546. {
  547. m_DoInventoryMoveLog = enable;
  548. }
  549. static bool IsInventoryReservationLogEnable()
  550. {
  551. return m_DoInventoryReservationLog;
  552. }
  553. static void InventoryReservationLogEnable(bool enable)
  554. {
  555. m_DoInventoryReservationLog = enable;
  556. }
  557. static bool IsInventoryHFSMLogEnable()
  558. {
  559. return m_DoInventoryHFSMLog;
  560. }
  561. static void InventoryHFSMLogEnable(bool enable)
  562. {
  563. m_DoInventoryHFSMLog = enable;
  564. }
  565. static bool IsSyncLogEnable()
  566. {
  567. return m_DoSyncLog;
  568. }
  569. static void SyncLogEnable(bool enable)
  570. {
  571. m_DoSyncLog = enable;
  572. }
  573. static bool IsQuickbarLogEnable()
  574. {
  575. return m_DoQuickbarLog;
  576. }
  577. static void QuickbarLogEnable(bool enable)
  578. {
  579. m_DoQuickbarLog = enable;
  580. }
  581. static bool IsBaseBuildingLogEnable()
  582. {
  583. return m_DoBaseBuildingLog;
  584. }
  585. static void BaseBuildingLogEnable(bool enable)
  586. {
  587. m_DoBaseBuildingLog = enable;
  588. }
  589. static bool IsSymptomLogEnable()
  590. {
  591. return m_DoSymptomDebugLog;
  592. }
  593. static void SymptomLogEnable(bool enable)
  594. {
  595. m_DoSymptomDebugLog = enable;
  596. }
  597. static bool IsWeaponLogEnable()
  598. {
  599. return m_DoWeaponLog;
  600. }
  601. static void WeaponLogEnable(bool enable)
  602. {
  603. m_DoWeaponLog = enable;
  604. }
  605. static bool IsWeatherLogEnabled()
  606. {
  607. return m_DoWeatherLog;
  608. }
  609. static bool IsBleedingChancesLogEnable()
  610. {
  611. return m_DoBleedingChanceLog;
  612. }
  613. static void BleedingChancesLogEnable(bool enable)
  614. {
  615. m_DoBleedingChanceLog = enable;
  616. }
  617. }
  618. enum WeightDebugType
  619. {
  620. NONE = 0,
  621. RECALC_FORCED = 1,
  622. RECALC_DIRTY = 2,
  623. DUMP_STACK = 4,
  624. SET_DIRTY_FLAG = 8,
  625. }
  626. class WeightDebug
  627. {
  628. private static ref map<EntityAI, ref WeightDebugData> m_WeightDebugData = new map<EntityAI, ref WeightDebugData>();
  629. static WeightDebugType m_VerbosityFlags;
  630. //-------------------------------------------------------------
  631. static WeightDebugData GetWeightDebug(EntityAI entity)
  632. {
  633. if (!m_WeightDebugData.Get(entity))
  634. {
  635. WeightDebugData data = new WeightDebugData(entity);
  636. m_WeightDebugData.Insert(entity,data);
  637. return data;
  638. }
  639. return m_WeightDebugData.Get(entity);
  640. }
  641. //-------------------------------------------------------------
  642. static void ClearWeightDebug()
  643. {
  644. m_WeightDebugData.Clear();
  645. }
  646. //-------------------------------------------------------------
  647. static void PrintAll(EntityAI entity)
  648. {
  649. GameInventory inv = entity.GetInventory();
  650. if (!inv)
  651. return;
  652. array<EntityAI> items = new array<EntityAI>;
  653. inv.EnumerateInventory(InventoryTraversalType.PREORDER, items);
  654. for(int i = 0; i < items.Count(); i++)
  655. {
  656. EntityAI item = items.Get(i);
  657. if (m_WeightDebugData.Get(item))
  658. {
  659. m_WeightDebugData.Get(item).Output();
  660. }
  661. }
  662. }
  663. //-------------------------------------------------------------
  664. static void SetVerbosityFlags(WeightDebugType type)
  665. {
  666. m_VerbosityFlags = type;
  667. }
  668. }
  669. class WeightDebugData
  670. {
  671. string m_Classname;
  672. string m_Weight;
  673. int m_InventoryDepth;
  674. string m_CalcDetails;
  675. //-------------------------------------------------------------
  676. void WeightDebugData(EntityAI entity)
  677. {
  678. m_Classname = entity.GetType();
  679. m_InventoryDepth = entity.GetHierarchyLevel();
  680. }
  681. //-------------------------------------------------------------
  682. void SetWeight(float weight)
  683. {
  684. m_Weight = weight.ToString();
  685. }
  686. //-------------------------------------------------------------
  687. void SetCalcDetails(string details)
  688. {
  689. m_CalcDetails = details;
  690. }
  691. //-------------------------------------------------------------
  692. void AddCalcDetails(string details)
  693. {
  694. m_CalcDetails += "+ "+details;
  695. }
  696. //-------------------------------------------------------------
  697. void Output()
  698. {
  699. string spaces;
  700. for(int i = 0; i < m_InventoryDepth;i++)
  701. spaces+="--------";
  702. Print(spaces+">" + m_Classname + " Overall entity weight: "+ m_Weight + " Calculation details:" + m_CalcDetails);
  703. }
  704. //-------------------------------------------------------------
  705. }