debug.c 23 KB

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